var cErrorMessages = 'The following errors(s) have occurred:\n';
function verify_policy( theform ){
	var bSUBMIT = true;

	if( !verify_info( theform ) ) bSUBMIT = false;

	if( !bSUBMIT ){
		alert( cErrorMessages );
		cErrorMessages = 'The following errors(s) have occurred:\n';
	}
	

	return bSUBMIT;
}


	
function verify_info( theform ){
	var bFuncSubmit = true;

	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

	if( theform.Firstname.value == '' ){
		cErrorMessages += ' - Please enter your First Name.\n';
		bFuncSubmit = false;
	}
	if( theform.Lastname.value == '' ){
		cErrorMessages += ' - Please enter your Last Name.\n';
		bFuncSubmit = false;
	}
	if( theform.Telephone.value == '' ){
		cErrorMessages += ' - Please enter your Telephone Number.\n';
		bFuncSubmit = false;
	}
	if( theform.Address1.value == '' ){
		cErrorMessages += ' - Please enter your Address.\n';
		bFuncSubmit = false;
	}
	if( theform.City.value == '' ){
		cErrorMessages += ' - Please enter your City.\n';
		bFuncSubmit = false;
	}
	if( theform.StateID.value == '' ){
		cErrorMessages += ' - Please enter your State.\n';
		bFuncSubmit = false;
	}
	if( theform.username.value == '' ){
		cErrorMessages += ' - Please enter your username.\n';
		bFuncSubmit = false;
	}
	
	if( theform.Firstname2.value == '' ){
		cErrorMessages += ' - Please enter your Billing First Name.\n';
		bFuncSubmit = false;
	}
	if( theform.Lastname2.value == '' ){
		cErrorMessages += ' - Please enter your Billing Last Name.\n';
		bFuncSubmit = false;
	}
	if( theform.Telephone2.value == '' ){
		cErrorMessages += ' - Please enter your Billing Telephone Number.\n';
		bFuncSubmit = false;
	}
	if( theform.Address21.value == '' ){
		cErrorMessages += ' - Please enter your Billing Address.\n';
		bFuncSubmit = false;
	}
	if( theform.City2.value == '' ){
		cErrorMessages += ' - Please enter your Billing City.\n';
		bFuncSubmit = false;
	}
	if( theform.StateID2.value == '' ){
		cErrorMessages += ' - Please enter your Billing State.\n';
		bFuncSubmit = false;
	}

	if( theform.mobile_number.value == '' ){
		cErrorMessages += ' - Please enter the mobile phone number.\n';
		bFuncSubmit = false;
	}
	
	if (theform.MobileCarrierID.selectedIndex == 0){
		cErrorMessages += ' - Please select Mobile Carrier.\n';
		bFuncSubmit = false;
	}
	
	if (theform.MobileBrandID.selectedIndex == 0){
		cErrorMessages += ' - Please select Mobile Device.\n';
		bFuncSubmit = false;
	}

	if( theform.SSN.value == '' ){
		cErrorMessages += ' - Please enter the Social Security number.\n';
		bFuncSubmit = false;
	}
	if (theform.SSN.value.length < 9) {
		cErrorMessages += ' - Social Security Number mus be 9 digits without special characters.\n';
		bFuncSubmit = false;
	}
	if (theform.mobile_number.value.length < 10) {
		cErrorMessages += ' - Mobile number must be 10 digits including area code.\n';
		bFuncSubmit = false;
	}
	for (var i = 0; i < theform.mobile_number.value.length; i++) {
                if (iChars.indexOf(theform.mobile_number.value.charAt(i)) != -1) {
                alert ("The mobile number has special characters. \nThese are not allowed.\n");
                bFuncSubmit = false;
        		}
     }

	if( theform.CC_Type.value == '' ){
		cErrorMessages += ' - Please enter the Credit Card Type.\n';
		bFuncSubmit = false;
	}
	if( theform.CC_Number.value == '' ){
		cErrorMessages += ' - Please enter the Credit Card Number.\n';
		bFuncSubmit = false;
	}
	if( theform.Zipcode.value == ''){
		cErrorMessages += ' - Please enter a Zip Code or Postal Code.\n';
		bFuncSubmit = false;
	}
	if( !validateZIP(theform.Zipcode.value) && theform.country.value == 'USA'){
		cErrorMessages += ' - Please enter a valid US Zipcode.\n';
		bFucnSubmit = false;
	}
	if( !isPostCode(theform.Zipcode.value) && theform.country.value == 'Canada'){
		cErrorMessages += ' - Please enter a valid Canadian Postal Code.\n';
		bFucnSubmit = false;
	}
	if( theform.Email_Address.value == '' || !isValidEmail(theform.Email_Address.value) ){
		cErrorMessages += ' - Please enter a valid Email Address.\n';
		bFuncSubmit = false;
	}
	if ( !theform.term_agree[0].checked){
        cErrorMessages += ' - You must accept the terms and conditions to open a new policy.\n'; 
        bFuncSubmit = false;
	}

	var esn_value = 'no';


	if( theform.esn_decimal.value != '' ){
		esn_value = 'yes';
	
		if (!validateESN(theform.esn_decimal.value)) {
			cErrorMessages += ' - ESN must be 11 digits (numbers only).\n';
			bFuncSubmit = false;
		}
	}

	

	
	if( theform.esn_hex.value != '' ){
		esn_value = 'yes';
		if (theform.esn_hex.value.length < 9) {
			cErrorMessages += ' - Hexadecimal ESN must be 9 characters.\n';
			bFuncSubmit = false;
		}
	}
	
	if( theform.meid.value != '' ){
		esn_value = 'yes';
	}

	

	if( theform.imei.value != '' ){
		esn_value = 'yes';

		if( theform.sim.value == '' ){
			 cErrorMessages += ' - Please Enter a  SIM value. \n'; 
      		  bFuncSubmit = false;
		}

	}
	

	if( esn_value == 'no'){
		 cErrorMessages += ' - Please Enter either an ESN value or an IMEI & SIM value or a MEID value. \n'; 
        bFuncSubmit = false;
	}

	return bFuncSubmit;
}

//=============


//============



function verify_trans( theform ){
	var bFuncSubmit = true;
	var bAddOn =  true;
	if( bAddOn ){
		if( theform.transdate.value == '' ){
			cErrorMessages += ' - Please select a Transit date.\n';
			bFuncSubmit = false;
		}
		if( theform.transhours.options[theform.transhours.selectedIndex].value == '' ){
			cErrorMessages += ' - Please select a transit time.\n';
			bFuncSubmit = false;
		}
		if( theform.start_location.options[theform.start_location.selectedIndex].value == '' ){
			cErrorMessages += ' - Please select shuttle service pickup location.\n';
			bFuncSubmit = false;
		}
		if( theform.end_location.options[theform.end_location.selectedIndex].value == '' ){
			cErrorMessages += ' - Please select a shuttle service drop off location.\n';
			bFuncSubmit = false;
		}
		if( theform.start_location.options[theform.start_location.selectedIndex].value == theform.end_location.options[theform.end_location.selectedIndex].value ){
			cErrorMessages += ' - Your pick-up and drop-off locations can not be the same. You must travel from a Continental Guest Services hotel to the airport or from an airport to the hotel.\n';
			bFuncSubmit = false;
		}
	}

	return bFuncSubmit;
}

function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10) {
		//alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			//alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
			//alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
	}
	return true;
}

function isPostCode(entry){ // CANADIAN CODES ONLY
	entry=entry.replace(" ", "");
	strlen=entry.length; if(strlen!=6) {return false;}
	entry=entry.toUpperCase();    // in case of lowercase characters
	// Check for legal characters in string - note index starts at zero
	if('ABCEGHJKLMNPRSTVXY'.indexOf(entry.charAt(0))<0) {return false;}
	if('0123456789'.indexOf(entry.charAt(1))<0) {return false;}
	if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2))<0) {return false;}
	if('0123456789'.indexOf(entry.charAt(3))<0) {return false;}
	if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4))<0) {return false;}
	if('0123456789'.indexOf(entry.charAt(5))<0) {return false;}
	return true;
}

function validateESN(field) {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=11) {
		//alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			//alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
			//alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
	}
	return true;
}


