var cityStateCount = 0;


//used in remind_email.jsp, remind_email_brochure.jsp, reming_email_referral.jsp
function isEmailAddr(email)
{
/*
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
*/

  var testresults = false;


  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(email)) {
    testresults=true
  }

  return (testresults)

}

//used in remind_email.jsp
function verifyEmailForm(objForm)
{
	if(!isEmailAddr(objForm.emailAddress.value)) {
		alert('Please enter a valid email address.');
		objForm.emailAddress.select();
		objForm.emailAddress.focus();
		return(false);
	}
	return(true);
}

//used in customer pages
function verifyCustRecord(objForm){
  if(parseInt(objForm.customerNumber.value) == 0 ){
    alert("You have to Save before Continuing.");
    return(false);
  }

  //enable city, state
	document.customerForm.city.disabled = false;
  document.customerForm.stateProvince.disabled = false;

  return (true);
}

//used in customer pages
function validateLookup(objForm){
  if(!(objForm.countryCD.options[objForm.countryCD.selectedIndex].value == "USA" || objForm.countryCD.options[objForm.countryCD.selectedIndex].value == "CAN") ) {
    alert("Lookup is available only for USA and CANADA customers.");
    return(false);
  }
  return (true);
}

var validateCustomerAccount = true;
function verifyCustomerInfo(objForm) {
  var alertString = "The following fields are required\n";
  var displayAlert = false;

  if(objForm.firstName.value.length < 2) {
    alertString += "- First Name.\n";
    displayAlert = true;
  }

  if(objForm.lastName.value.length < 2) {
    alertString += "- Last Name.\n";
    displayAlert = true;
  }

  if(objForm.addressLine1.value.length < 2) {
    alertString += "- Address1.\n";
    displayAlert = true;
  }
  
  // check birthdate information for website customers (not OrderTix)
  if(objForm.identifier.value == "club" ) {
    if(objForm.birth_month.value.length < 1) {
      alertString += "- Birth Month.\n";
      displayAlert = true;
    }
    if(objForm.birth_day.value.length < 1) {
      alertString += "- Birth Date.\n";
      displayAlert = true;
    }
  }
  
  // check press info for press customers
  if(objForm.identifier.value == "press") {
    if(objForm.jobTitle.value.length < 2) {
      alertString += "- Job Title.\n";
      displayAlert = true;
    }
    if(objForm.press_website.value.length < 2) {
      alertString += "- Press Website.\n";
      displayAlert = true;
    }
    if(objForm.publication_or_station.value.length < 2) {
      alertString += "- Publication / Station.\n";
      displayAlert = true;
    }
    if(objForm.preferred_contact_method.value.length < 2) {
      alertString += "- Preferred Contact Method.\n";
      displayAlert = true;
    }
    if(objForm.medium.value.length < 2) {
      alertString += "- Medium.\n";
      displayAlert = true;
    }
    if(objForm.sector.value.length < 2) {
      alertString += "- Sector.\n";
      displayAlert = true;
    }
  }



  
  //validate for phone number
  if(objForm.phoneNumber[0].value.length < 10 && objForm.phoneNumber[1].value.length < 10  ) {
    alertString += "- Phone Number.\n";
    displayAlert = true;
  }
  

  //validate for phone type selection if phone number had been selected.
  if(objForm.phoneNumber[0].value.length > 0){
    if(objForm.phoneType[0].options[objForm.phoneType[0].selectedIndex].value == "" ){
      alertString += "- Phone Type.\n";
      displayAlert = true;
    }
  }

  if(objForm.phoneNumber[1].value.length > 0){
    if(objForm.phoneType[1].options[objForm.phoneType[1].selectedIndex].value == "" ){
      alertString += "- Phone Type.\n";
      displayAlert = true;
    }
  }

  if(displayAlert) {
    alert(alertString);
    return (false);
  }
  
  if(verifyCityStateZip(objForm) == false){
	  return (false);
  }
  //verify email info
  if(validateCustomerAccount) {

    if(!verifyCustomerAccountInfo(objForm)) {
      return false;
    }
  }



  return (true);

}

function verifyFITCustomerInfo(objForm) {
 // alert("inside verifyFITCustomerInfo");
  var alertString = "The following fields are required\n";
  var displayAlert = false;

  if(objForm.firstName.value.length < 2) {
    alertString += "- First Name.\n";
    displayAlert = true;
  }  

  if(objForm.lastName.value.length < 2) {
    alertString += "- Last Name.\n";
    displayAlert = true;
  }  

  //alert("displayAlert: "+displayAlert);
  if(displayAlert) {
    alert(alertString);
    return (false);
  }
  //enable the city, state fields
  objForm.city.disabled = false;
  objForm.stateProvince.disabled = false;
  return (true);
    
}



function verifyCityStateZip(objForm) {
  var alertString = "The following fields are required\n";
  var displayAlert = false;
  //if the country is other than USA and CAN, then city, state are to be entered
  var isUSAorCAN = false;
  if(objForm.countryCD.options[objForm.countryCD.selectedIndex].value == "USA" || objForm.countryCD.options[objForm.countryCD.selectedIndex].value == "CAN" ) {
    isUSAorCAN = true;
  }

  //for USA, CAN zipPostalCode is must.
  if(isUSAorCAN) {
    if(objForm.zipPostalCode.value.length == 0) {
      alertString += "- Zipcode.\n";
      displayAlert = true;
    }
  }

  if(objForm.countryCD.selectedIndex == 0) {
    alertString += "- Country.\n";
    displayAlert = true;
  }
  

  //validate city state selection
  if(cityStateCount > 0 ) {
    if(typeof(objForm.cityStateSelect.length) == "undefined" ) {
      if(objForm.cityStateSelect.checked == false) {
        alertString += "- City State Selection.\n";
        displayAlert = true;
      }
    }
    else {
      var cityStateSelected = false;
      for(var i=0; i < objForm.cityStateSelect.length; i++) {
        if(objForm.cityStateSelect[i].checked == true) {
          cityStateSelected = true;
          break;
        }
        if(cityStateSelected == false) {
          alertString += "- City State Selection.\n";
          displayAlert = true;
        }
      } //end of for
    } //end of else
  }

  //enfore zip lookup when city and state are not present for USA or CAN country
  if(isUSAorCAN) {
    if(objForm.city.value.length < 2 ||  objForm.stateProvince.value.length < 2) {
      alert("Click on 'Select' for the appropriate City, State & Country combination. ");
      return (false);
    }
    else {
      objForm.city.disabled = false;
      objForm.stateProvince.disabled = false;
    }
  }
  
  if(displayAlert) {
    alert(alertString);
    return (false);
  }

  return (true);
  

}


function verifyCustomerAccountInfo(objForm) {
  var alertString = "The following fields are required\n";
  var displayAlert = false;

  if(!isEmailAddr(objForm.email.value)) {
    alertString += "- Email.\n";
    displayAlert = true;
  }  

  if(!isEmailAddr(objForm.repeatEmail.value)) {
    alertString += "- Repeat Email.\n";
    displayAlert = true;
  }  

  if(objForm.email.value.length > 0 && objForm.email.value != objForm.repeatEmail.value) {
    alertString += "- Email and Repeat Email do not match.\n";
    displayAlert = true;
  }

  if(!validatePassword(objForm.password.value)) {
    alertString += "- Password should be 6 - 20 char. length with no spaces.\n";
    displayAlert = true;
  }
  if(!validatePassword(objForm.repeatPassword.value) ) {
    alertString += "- Repeat Password should be 6 - 20 char. length with no spaces.\n";
    displayAlert = true;
  }

  if(objForm.password.value != objForm.repeatPassword.value) {
    alertString += "- Password and Repeat Password do not match.\n";
    displayAlert = true;
  }

  if(displayAlert) {
    alert(alertString);
    return (false);
  }

  return (true);
    
}





//this is used to dynamically change the state/province field when a country other than USA is selected .
function enableCityState(objFormItem) {
  //if no country is selected, default to USA
  if(objFormItem.selectedIndex == 0) {
    for(i=1; i<objFormItem.length; i++){
      if(objFormItem.options[i].value == "USA") {
        objFormItem.options[i].selected = true;
        break;
      }
    }
  }


  var objForm  = objFormItem.form;
	if((objFormItem.options[objFormItem.selectedIndex].value == "USA" || objFormItem.options[objFormItem.selectedIndex].value == "CAN")) {
    if(objForm.city.value.length > 0) {
      document.getElementById('cityLabel').style.display = 'inline';
      objForm.city.style.display = 'inline';
      document.getElementById('stateLabel').style.display = 'inline';
      objForm.stateProvince.style.display = 'inline';
      objForm.stateProvince.disabled = true;
      objForm.city.disabled = true;
    } else {
      document.getElementById('cityLabel').style.display = 'none';
      objForm.city.style.display = 'none';
      document.getElementById('stateLabel').style.display = 'none';
      objForm.stateProvince.style.display = 'none';
    }

	}
  else {
    document.getElementById('cityLabel').style.display = 'inline';
    objForm.city.style.display = 'inline';
    document.getElementById('stateLabel').style.display = 'inline';
    objForm.stateProvince.style.display = 'inline';
    objForm.stateProvince.disabled = false;
    objForm.city.disabled = false;


  }

}


//this function is used to enable city, state before submitting the form so that the values are passed to action.
//Because, disabled form fields are not passed to a server program(Servlet, JSP, Action)
function submitForm(objForm) {
  objForm.stateProvince.disabled = false;
  objForm.city.disabled = false;
  return true;
}


//this function copies the city/state values to appropriate fields.
function copyValues(objSelect) {
  var objForm = objSelect.form;
//when the no. of city/state combination is 1, fields are accessed directly.
//Otherwise, appropriate index is used.
  if(cityStateCount == 1) {
    objForm.city.disabled = false;
    objForm.city.value = objForm.lookupCity.value;
    objForm.zipPostalCode.value = objForm.lookupZipCode.value;
    objForm.stateProvince.disabled = false;
    objForm.stateProvince.value = objForm.lookupStateProvinceCode.value;

    for(var a = 0; a < objForm.countrySelect.length; a++) {
      if (objForm.countrySelect.options[a].value == objForm.lookupCountryCode.value) {
        objForm.countrySelect.options[a].selected = true;
        break;
      }
    }
  }
  else {
    var index = objSelect.value;
    objForm.city.disabled = false;
    objForm.city.value = objForm.lookupCity[index].value;
    objForm.zipPostalCode.value = objForm.lookupZipCode[index].value;
    objForm.stateProvince.disabled = false;
    objForm.stateProvince.value = objForm.lookupStateProvinceCode[index].value

    for(var a = 0; a < objForm.countrySelect.length; a++) {
      if (objForm.countrySelect.options[a].value == objForm.lookupCountryCode[index].value) {
        objForm.countrySelect.options[a].selected = true;
        break;
      }
    }//end of for
  }
  
  // update visibility after inserting city/state name
  enableCityState(objForm.countrySelect);
}

function resetCityState() {
    cityStateCount = 0;
	document.getElementById("cityState").innerHTML="";
}

function copyAndRefresh(form) {
  copyValues(form.cityStateSelect);  
  form.REFRESH.value="YES";
  form.submit();
}

function focusBirthDay(objForm) {
  if(objForm.birth_month.value.length == 2){
    objForm.birth_day.focus();
  }
}

function focusBirthYear(obForm) {
  if(obForm.birth_day.value.length == 2){
    obForm.birth_year.focus();
  }
}

function checkRoutingNumber(objRoutingNumber) {
    var strRoutNbr = objRoutingNumber.value; 



    //multiply each digit with the weight factor and sum it.
    // 1 2 3 4 5 6 7 8
    // 3 7 1 3 7 1 3 7

    var sum =  parseInt(strRoutNbr.substring(0,1)) * 3 ;
    sum +=  parseInt(strRoutNbr.substring(1,2)) * 7 ;
    sum +=  parseInt(strRoutNbr.substring(2,3)) * 1 ;
    sum +=  parseInt(strRoutNbr.substring(3,4)) * 3 ;
    sum +=  parseInt(strRoutNbr.substring(4,5)) * 7 ;
    sum +=  parseInt(strRoutNbr.substring(5,6)) * 1 ;
    sum +=  parseInt(strRoutNbr.substring(6,7)) * 3 ;
    sum +=  parseInt(strRoutNbr.substring(7,8)) * 7 ;
    //9th digit is the check digit
    var checkDigit = parseInt(strRoutNbr.substring(8,9))

    //substract the sum from the next highest multiple of 10. The result is the check digit.
    //TO DO this, get the last digit of the sum string. Substract it from 10.
    var strSum = ""+sum;
    var calculatedCheckDigit = parseInt(strSum.substring(strSum.length-1, strSum.length));
    if(calculatedCheckDigit != 0) {
      calculatedCheckDigit = 10 - calculatedCheckDigit;
    }

//alert("checkDigit: "+checkDigit );
//alert("calculatedCheckDigit: "+calculatedCheckDigit );
    if(checkDigit != calculatedCheckDigit) {
      alert("Please check the Routing#");
      objRoutingNumber.select();
      objRoutingNumber.focus();
      return false;
    }
  }

function validateUserName(value) {
	   var rgxSp = /\s/;
	   if(rgxSp.test(value) || value.length < 6 || value.length > 20 ){
	     return false;		     
	   }   
	   return true;
}	

function validatePassword(value) {
var rgxSp = /\s/;
if(rgxSp.test(value) || value.length < 6 || value.length > 20 ){
  return false;		     
}   
return true;
}	

function verifyOldNewPasswords(objForm) {
	var displayAlert = false;
	var alertString = "The following fields are required.\n";
 if(objForm.oldPass.value.length == 0){
 	alertString +="Old Password\n ";
 	displayAlert = true;
 }	
 if(objForm.newPass.value.length == 0){
 	alertString +="New Password\n ";
 	displayAlert = true;
 }
 if(objForm.newPassTwo.value.length == 0){
 	alertString +="Retype New Password\n ";
 	displayAlert = true;
 }
 if(!validatePassword(objForm.newPass.value) || !validatePassword(objForm.newPassTwo.value)){
 	alertString +="Password should not contain any spaces\n ";
 	displayAlert = true;
 } 
	if(objForm.newPass.value != objForm.newPassTwo.value) {
	   //alert("'New Password' and 'Retype New Password' must match exactly.");
	   //return false;
		alertString +="New Password' and 'Retype New Password' must match exactly\n ";
		displayAlert = true;	   
	}
	if(displayAlert) {
		  alert(alertString);
		  return false; 
	}
	  
return true;
}
  