var errfound = false;
var firstsubmit = false;
var GM = "\" field is required.\nPlease fill in all required fields.";
var CM = "\nPlease make the appropriate changes and try again.";
var EM="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-@.";
function fixPhone(phone){
 temp="";
 for (i=0;i<phone.length;i++) {
  if (parseInt(phone.substring(i,i+1))>=0) {
   temp=temp+phone.substring(i,i+1);
  }
 }
 while (temp.substring(0,1)=="1"){
  temp=temp.substring(1,temp.length);
 }
 document.contactForm.phone.value=temp;
}

function fixZip(zip) {
 temp="";
 for (i=0;i<zip.length;i++) {
  if (parseInt(zip.substring(i,i+1))>=0) {
   temp=temp+zip.substring(i,i+1);
  }
 }
 if (temp.length>=9) {
  zip=temp.substring(0,5)+"-"+temp.substring(5,9);
 } else {
  zip=temp.substring(0,5);
 }
 document.contactForm.zip.value=zip;
}

//Verify not a PO Box address
function ValidPOB(address) {
 var ValidPOB = false;
 temp="";
 if (address.length>0) {
  address = address.toUpperCase();
  for (i=0;i<Address.length;i++) {
   if (address.substring(i,i+1)!="." && address.substring(i,i+1)!="," && address.substring(i,i+1)!=" ") {
    temp=temp+address.substring(i,i+1);
   }
  }
  address = temp;
  if (address.substring(0,3)!="POB" && address.substring(0,4)!="POST" && address.substring(0,3)!="BOX") {
   ValidPOB = true;
  } 
 } else {
  ValidPOB = true;
 }
 return(ValidPOB);
}

//verify not a APO address
function ValidCity(city) {
 var ValidCity = false;
 temp="";
 if (city.length>0) {
  city = city.toUpperCase();
  for (i=0;i<city.length;i++) {
   if (city.substring(i,i+1)!="." && city.substring(i,i+1)!="," && city.substring(i,i+1)!=" ") {
    temp=temp+city.substring(i,i+1);
   }
  }
  city = temp;
  if (city.substring(0,3)!="APO" && city.substring(0,3)!="AFO") {
   ValidCity = true;
  }
 } else {
  ValidCity = true;
 }
 return (ValidCity);
}

//Reset if address changed
function Address() {
 firstsubmit = false;
}

// main validation function
function validate() {
	errfound = false;

	with (document.contactForm) {
		if (areaCode.value == "" || prefix.value == "" || suffix.value == "" || 
			!areaCode.value.match(/^\d{3}$/) || !prefix.value.match(/^\d{3}$/) || !suffix.value.match(/^\d{4}$/)) {
			error("You must enter a valid phone number.",areaCode);
		}
		
		if (emailAddress.value.indexOf("@") == -1 || emailAddress.value.indexOf("..") != -1 || emailAddress.value.indexOf(".") == -1 || emailAddress.value.indexOf("@.") != -1 || emailAddress.value.indexOf(".@") != -1 || emailAddress.value.indexOf("@-") != -1 || emailAddress.value.indexOf("-@") != -1 || emailAddress.value.indexOf("@_") != -1 || emailAddress.value.indexOf("_@") != -1 || emailAddress.value.substr(emailAddress.value.indexOf("@"),emailAddress.value.length-1).indexOf(".") == -1 || emailAddress.value.indexOf("@@") != -1)
   			error("The Email Address you have entered is invalid.\nPlease enter a valid Email Address.",emailAddress);
		for (i=0;i<emailAddress.value.length;i++) {
   			temp=emailAddress.value.substring(i,i+1);
   			if (EM.indexOf(temp) == -1) {
   				error("Your e-mail address contains invalid characters.\nPlease enter a valid email address.",emailAddress);
   				break;
   			}
  		}
		if (emailAddress.value != emailConfirm.value)
   			error("The Email Addresses you entered do not match.\nPlease enter matching email addresses.",emailConfirm);
   			
   		var count = document.getElementsByName('productLines[]').length;
		var is_checked = false;
		
		for (var i = 0; i < count; i++) {
		    if (document.contactForm.elements['productLines[]'][i].checked) {
		        is_checked = true;
		        break;
		    }
		}
		
		if (is_checked == false) {
		    error("Please select at least one equipment type.", null);
		}
  	}

	return !errfound;
}

// display an error alert
function error(text,element) {
 if (errfound) return;
 window.alert(text);
 errfound = true;
 if (element != null && element!="") element.focus();
}		
function isDate(birthdate) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 2-30-2000 would not be accepted.\nFormay mm/dd/yyyy.';
    if ((birthdate.value.match(RegExPattern)) && (birthdate.value!='')) {
       return;
    } else {
		error(errorMessage, birthdate);
    }
}


$(document).ready(function() {
	setStoreValues(document.contactForm, document.contactForm.country);
});

