function doCancelBack(e){
  var targ;
  if (!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  return true;
}

function bindFormBtnHandler(){
	if(document.all)
		document.forms["servicesForm"].attachEvent('onsubmit',doCancelBack);
	else
		document.forms["servicesForm"].addEventListener('onsubmit',doCancelBack,false);
}

var errors;

function checkForm(form){

	errors = new Array;
						
	with (form){
		if (form.id == "enrollForm"){
		
			if (typeof dshipNameTxt != "undefined" && Trim(dshipNameTxt.value) == "") errors.push("ERROR: Dealership name is a required field");
			
			if (typeof signatorFnTxt != "undefined" && Trim(signatorFnTxt.value) == "") errors.push("ERROR: Dealership Authorized Signator first name is a required field");
			if (typeof signatorLnTxt != "undefined" && Trim(signatorLnTxt.value) == "") errors.push("ERROR: Dealership Authorized Signator last name is a required field");
			if (typeof signatorPhnTxt != "undefined" && Trim(signatorPhnTxt.value) == "") errors.push("ERROR: Dealership Authorized Signator phone is a required field");
			if (typeof signatorEmlTxt != "undefined" && Trim(signatorEmlTxt.value) == "") errors.push("ERROR: Dealership Authorized Signator email is a required field");
			
			if (typeof sysAdminFnTxt != "undefined" && Trim(sysAdminFnTxt.value) == "") errors.push("ERROR: Dealership CID System Administrator first name is a required field");
			if (typeof sysAdminLnTxt != "undefined" && Trim(sysAdminLnTxt.value) == "") errors.push("ERROR: Dealership CID System Administrator last name is a required field");
			if (typeof sysAdminPhnTxt != "undefined" && Trim(sysAdminPhnTxt.value) == "") errors.push("ERROR: Dealership CID System Administrator phone is a required field");
			if (typeof sysAdminEmlTxt != "undefined" && Trim(sysAdminEmlTxt.value) == "") errors.push("ERROR: Dealership CID System Administrator email is a required field");
			
			if (typeof dwidTxt != "undefined" && Trim(dwidTxt.value) == "") {
				errors.push("ERROR: The Dealerworld ID is a required field");
			}
			else {
				// perform alphanumeric check
				if (!/[A-Za-z0-9]/.test(dwidTxt.value)) errors.push("ERROR: The Dealerworld ID may only consist of alphanumeric (letters and numbers) characters");
			}	
						
			if (typeof zipTxt != "undefined" && Trim(zipTxt.value) != ""){
				if (badNumCheck(zipTxt.value,5)){
					errors.push("ERROR: The Dealership zip code must be 5 digits");
				}
			}
			
			if (typeof zipFourTxt != "undefined" && Trim(zipFourTxt.value) != ""){
				if (badNumCheck(zipFourTxt.value,4)){
					errors.push("ERROR: The optional 4-digit Dealership zip code must be 4 digits");
				}
			}
			
			if (typeof signatorPhnTxt != "undefined" && Trim(signatorPhnTxt.value) != ""){
				if (badNumCheck(signatorPhnTxt.value,10)){
					errors.push("ERROR: The Dealership Authorized Signator phone number must be 10 digits with no dashes or parentheses. (e.g.5554365828)");
				}
			}
			
			if (typeof sysAdminExtTxt != "undefined" && Trim(sysAdminExtTxt.value) != ""){
				if (!/^\d+$/.test(sysAdminExtTxt.value)) errors.push("ERROR: The phone extension field must consist of numbers only.");
			}
			
			if (typeof signatorExtTxt != "undefined" && Trim(signatorExtTxt.value) != ""){
				if (!/^\d+$/.test(signatorExtTxt.value)) errors.push("ERROR: The phone extension field must consist of numbers only.");
			}
			
			if (typeof signatorEmlTxt != "undefined" && Trim(signatorEmlTxt.value) != ""){
				if (BadEmail(signatorEmlTxt.value)){
					errors.push("ERROR: The Dealership Authorized Signator email address is not valid");
				}
			}
			
			if (typeof sysAdminPhnTxt != "undefined" && Trim(sysAdminPhnTxt.value) != ""){
				if (badNumCheck(sysAdminPhnTxt.value,10)){
					errors.push("ERROR: The Dealership CID System Administrator phone number must be 10 digits with no dashes or parentheses. (e.g.5554365828)");
				}
			}
			
			if (typeof sysAdminEmlTxt != "undefined" && Trim(sysAdminEmlTxt.value) != ""){
				if (BadEmail(sysAdminEmlTxt.value)){
					errors.push("ERROR: The Dealership CID System Administrator email address is not valid");
				}
			}	
		}
		
		if (form.id == "servicesForm"){
			if (typeof CIDfnTxt != "undefined" && Trim(CIDfnTxt.value) == "") errors.push("ERROR: Dealership Certified Internet Manager first name is a required field");
			if (typeof CIDlnTxt != "undefined" && Trim(CIDlnTxt.value) == "") errors.push("ERROR: Dealership Certified Internet Manager last name is a required field");
			if (typeof CIDphnTxt != "undefined" && Trim(CIDphnTxt.value) == "") errors.push("ERROR: Dealership Certified Internet Manager phone is a required field");
			if (typeof CIDemlTxt != "undefined" && Trim(CIDemlTxt.value) == "") errors.push("ERROR: Dealership Certified Internet Manager email is a required field");
			
			//5868 Changed the Phone Format
			if (typeof CIDphnTxt != "undefined" && Trim(CIDphnTxt.value) != ""){
				if (phoneFormatValidate(CIDphnTxt.value)){
				errors.push("ERROR: The Dealership Certified Internet Manager phone number must be in the following format : xxx-xxx-xxxx");
				}
			}
			
			if (typeof CIDextTxt != "undefined" && Trim(CIDextTxt.value) != ""){
				if (!/^\d+$/.test(CIDextTxt.value)) errors.push("ERROR: The phone extension field must consist of numbers only.");
			}
			
			if (typeof CIDemlTxt != "undefined" && Trim(CIDemlTxt.value) != ""){
				if (BadEmail(CIDemlTxt.value)){
					errors.push("ERROR: The Dealership Certified Internet Manager email address is not valid");
				}
			}
			
			if (GMOSclmpDDL.disabled == false && GMOSclmpDDL.options[GMOSclmpDDL.selectedIndex].value == ''){
				errors.push("ERROR: Certified Lead Management Provider is required");
			}
			
			// bacs were removed from services page.			
			/*var bacs = document.getElementById("GMOSbacContain").getElementsByTagName("input");
			for (var i = 0; i < bacs.length; i++){
				//alert(bacs[i]);
				if (Trim(bacs[i].value) != "" && badNumCheck(bacs[i].value,6)){
					errors.push("ERROR: BAC codes must be 6 digits.");
					break;
				}	
				
				
			} */
			
			
		}
		
		if (form.id == "termsForm"){
		
			if ((typeof CIDTermsChk != "undefined" && !CIDTermsChk.checked) || (typeof GMOSTermsChk != "undefined" && !GMOSTermsChk.checked) || (typeof GMOSSaturnTermsChk != "undefined" && !GMOSSaturnTermsChk.checked) || (typeof GMDWTermsChk != "undefined" && !GMDWTermsChk.checked)) {
				errors.push("You must accept the terms and conditions by selecting each of the the three (3) checkboxes above in order to continue.");
			}
		}	
		
		if (form.id == "contactForm"){
		
			if (typeof fnTxt != "undefined" && Trim(fnTxt.value) == "") errors.push("ERROR: First name is a required field");
			if (typeof lnTxt != "undefined" && Trim(lnTxt.value) == "") errors.push("ERROR: Last name is a required field");
			if (typeof emTxt != "undefined" && Trim(emTxt.value) == "") errors.push("ERROR: Email is a required field");
			if (typeof phnTxt != "undefined" && Trim(phnTxt.value) == "") errors.push("ERROR: Phone is a required field");
			if (typeof dsnTxt != "undefined" && Trim(dsnTxt.value) == "") errors.push("ERROR: Dealership name is a required field");
			if (typeof bacTxtbx != "undefined" && Trim(bacTxtbx.value) == "") errors.push("ERROR: The BAC code is a required field");
			
			if (typeof phnTxt != "undefined" && Trim(phnTxt.value) != ""){
				if (badNumCheck(phnTxt.value,10)){
					errors.push("ERROR: The phone number must be 10 digits with no dashes or parentheses. (e.g.5554365828)");
				}
			}
			
			if (typeof emTxt != "undefined" && Trim(emTxt.value) != ""){
				if (BadEmail(emTxt.value)){
					errors.push("ERROR: The email address is not valid");
				}
			}
			
			if (typeof bacTxtbx != "undefined" && Trim(bacTxtbx.value) != ""){
				if (badNumCheck(bacTxtbx.value,6)){
					errors.push("ERROR: The BAC code must be 6 digits");
				}
			}
		
		}
		if (form.id == "ChangeInfo"){
		    
			
			if (typeof dshipNameTxt != "undefined" && Trim(dshipNameTxt.value) == "") errors.push("ERROR: Dealership name is a required field");
			if (typeof bacTxt != "undefined" && Trim(bacTxt.value) == "") errors.push("ERROR: Dealership BAC code is a required field");
			if (typeof bacTxt != "undefined" && Trim(bacTxt.value) != ""){
				if (badNumCheck(bacTxt.value,6)){
					errors.push("ERROR: The BAC code must be 6 digits");
				}
			}
			if (typeof requestorTxt != "undefined" && Trim(requestorTxt.value) == "") errors.push("ERROR: Requestor name is a required field");
			if (typeof requestorEmailTxt != "undefined" && Trim(requestorEmailTxt.value) == "") errors.push("ERROR: Requestor email is a required field");
			if (typeof requestorEmailTxt != "undefined" && Trim(requestorEmailTxt.value) != ""){
				if (BadEmail(requestorEmailTxt.value)){
					errors.push("ERROR: The Requestor email address is not valid");
				}
			}
			if (typeof cidEmail_1_Txt != "undefined" && Trim(cidEmail_1_Txt.value) != ""){
				if (BadEmail(cidEmail_1_Txt.value))
					errors.push("ERROR: The GM Certified Intern Dealer \'Email 1\' address is not valid");
				else 
					radioCheck(cidEmail_1_Type,'GM Certified Intern Dealer \'Email 1\'');	
			}

			if (typeof cidEmail_2_Txt != "undefined" && Trim(cidEmail_2_Txt.value) != ""){
				if (BadEmail(cidEmail_2_Txt.value))
					errors.push("ERROR: The GM Certified Intern Dealer \'Email 2\' address is not valid");
				else
					radioCheck(cidEmail_2_Type,'GM Certified Intern Dealer \'Email 2\'');
			}

			if (typeof cidEmail_3_Txt != "undefined" && Trim(cidEmail_3_Txt.value) != ""){
				if (BadEmail(cidEmail_3_Txt.value))
					errors.push("ERROR: The GM Certified Intern Dealer \'Email 3\' address is not valid");
				else
					radioCheck(cidEmail_3_Type,'GM Certified Intern Dealer \'Email 3\'');
			}

			if (typeof dlrWebsiteEmail_1_Txt != "undefined" && Trim(dlrWebsiteEmail_1_Txt.value) != ""){
				if (BadEmail(dlrWebsiteEmail_1_Txt.value))
					errors.push("ERROR: The GM Dealer Website \'Email 1\' address is not valid");
				else
					radioCheck(dlrWebsiteEmail_1_Type,'GM Dealer Website \'Email 1\'');
			}

			if (typeof dlrWebsiteEmail_2_Txt != "undefined" && Trim(dlrWebsiteEmail_2_Txt.value) != ""){
				if (BadEmail(dlrWebsiteEmail_2_Txt.value))
					errors.push("ERROR: The GM Dealer Website \'Email 2\' address is not valid");
				else
					radioCheck(dlrWebsiteEmail_2_Type,'GM Dealer Website \'Email 2\'');
			}

			if (typeof dlrWebsiteEmail_3_Txt != "undefined" && Trim(dlrWebsiteEmail_3_Txt.value) != ""){
				if (BadEmail(dlrWebsiteEmail_3_Txt.value))
					errors.push("ERROR: The GM Dealer Website \'Email 3\' address is not valid");
				else
					radioCheck(dlrWebsiteEmail_3_Type,'GM Dealer Website \'Email 3\'');
			}

			if (typeof SvcLeadsEmail_1_Txt != "undefined" && Trim(SvcLeadsEmail_1_Txt.value) != ""){
				if (BadEmail(SvcLeadsEmail_1_Txt.value))
					errors.push("ERROR: The Service Leads \'Email 1\' address is not valid");
				else 
					radioCheck(SvcLeadsEmail_1_Type,'Service Leads \'Email 1\'');
			}

			if (typeof SvcLeadsEmail_2_Txt != "undefined" && Trim(SvcLeadsEmail_2_Txt.value) != ""){
				if (BadEmail(SvcLeadsEmail_2_Txt.value))
					errors.push("ERROR: The Service Leads \'Email 2\' address is not valid");
				else 
					radioCheck(SvcLeadsEmail_2_Type,'Service Leads \'Email 2\'');
			}

			if (typeof SvcLeadsEmail_3_Txt != "undefined" && Trim(SvcLeadsEmail_3_Txt.value) != ""){
				if (BadEmail(SvcLeadsEmail_3_Txt.value))
					errors.push("ERROR: The Service Leads \'Email 3\' address is not valid");
				else 
					radioCheck(SvcLeadsEmail_3_Type,'Service Leads \'Email 3\'');
			}

			if (typeof PartsEmail_1_Txt != "undefined" && Trim(PartsEmail_1_Txt.value) != ""){
				if (BadEmail(PartsEmail_1_Txt.value))
					errors.push("ERROR: The Parts Leads \'Email 1\' address is not valid");
				else 
					radioCheck(PartsEmail_1_Type,'Parts Leads \'Email 1\'');
			}

			if (typeof PartsEmail_2_Txt != "undefined" && Trim(PartsEmail_2_Txt.value) != ""){
				if (BadEmail(PartsEmail_2_Txt.value))
					errors.push("ERROR: The Parts Leads \'Email 2\' address is not valid");
				else
					radioCheck(PartsEmail_2_Type,'Parts Leads \'Email 2\'');
			}

			if (typeof PartsEmail_3_Txt != "undefined" && Trim(PartsEmail_3_Txt.value) != ""){
				if (BadEmail(PartsEmail_3_Txt.value))
					errors.push("ERROR: The Parts Leads \'Email 3\' address is not valid");
				else 
					radioCheck(PartsEmail_3_Type,'Parts Leads \'Email 3\'');
			}

			if (typeof HandraiserEmail_1_Txt != "undefined" && Trim(HandraiserEmail_1_Txt.value) != ""){
				if (BadEmail(HandraiserEmail_1_Txt.value))
					errors.push("ERROR: The Handraiser Leads \'Email 1\' address is not valid");
				else
					radioCheck(HandraiserEmail_1_Type,'Handraiser Leads \'Email 1\'');
			}

			if (typeof HandraiserEmail_2_Txt != "undefined" && Trim(HandraiserEmail_2_Txt.value) != ""){
				if (BadEmail(HandraiserEmail_2_Txt.value))
					errors.push("ERROR: The Handraiser Leads \'Email 2\' address is not valid");
				else 
					radioCheck(HandraiserEmail_2_Type,'Handraiser Leads \'Email 2\'');
			}

			if (typeof HandraiserEmail_3_Txt != "undefined" && Trim(HandraiserEmail_3_Txt.value) != ""){
				if (BadEmail(HandraiserEmail_3_Txt.value))
					errors.push("ERROR: The Handraiser Leads \'Email 3\' address is not valid");
				else 
					radioCheck(HandraiserEmail_3_Type,'Handraiser Leads \'Email 3\'');
			}

			if (typeof UsedVehicleSalesEmail_1_Txt != "undefined" && Trim(UsedVehicleSalesEmail_1_Txt.value) != ""){
				if (BadEmail(UsedVehicleSalesEmail_1_Txt.value))
					errors.push("ERROR: The Used Vehicle Sales \'Email 1\' address is not valid");
				else
					radioCheck(UsedVehicleSalesEmail_1_Type,'Used Vehicle Sales \'Email 1\'');
			}

			if (typeof UsedVehicleSalesEmail_2_Txt != "undefined" && Trim(UsedVehicleSalesEmail_2_Txt.value) != ""){
				if (BadEmail(UsedVehicleSalesEmail_2_Txt.value))
					errors.push("ERROR: The Used Vehicle Sales \'Email 2\' address is not valid");
				else
					radioCheck(UsedVehicleSalesEmail_2_Type,'Used Vehicle Sales \'Email 2\'');
			}

			if (typeof UsedVehicleSalesEmail_3_Txt  != "undefined" && Trim(UsedVehicleSalesEmail_3_Txt .value) != ""){
				if (BadEmail(UsedVehicleSalesEmail_3_Txt .value))
					errors.push("ERROR: The Used Vehicle Sales \'Email 3\' address is not valid");
				else 
					radioCheck(UsedVehicleSalesEmail_3_Type,'Used Vehicle Sales \'Email 3\'');
			}

			if (typeof DefaultEmail_1_Txt  != "undefined" && Trim(DefaultEmail_1_Txt .value) != ""){
				if (BadEmail(DefaultEmail_1_Txt .value))
					errors.push("ERROR: The Default \'Email 1\' address is not valid");
				else 
					radioCheck(DefaultEmail_1_Type,'Default \'Email 1\'');
			}

			if (typeof DefaultEmail_2_Txt  != "undefined" && Trim(DefaultEmail_2_Txt .value) != ""){
				if (BadEmail(DefaultEmail_2_Txt .value))
					errors.push("ERROR: The Default \'Email 2\' address is not valid");
				else 
					radioCheck(DefaultEmail_2_Type,'Default \'Email 2\'');
			}

			if (typeof DefaultEmail_3_Txt  != "undefined" && Trim(DefaultEmail_3_Txt .value) != ""){
				if (BadEmail(DefaultEmail_3_Txt .value))
					errors.push("ERROR: The Default \'Email 3\' address is not valid");
				else
					radioCheck(DefaultEmail_3_Type,'Default \'Email 3\'');
			}

			if (typeof ReportsEmail_1_Txt  != "undefined" && Trim(ReportsEmail_1_Txt .value) != ""){
				if (BadEmail(ReportsEmail_1_Txt .value))
					errors.push("ERROR: The Reports \'Email 1\' address is not valid");
				else
					radioCheck(ReportsEmail_1_Type,'Reports \'Email 1\'');
			}

			if (typeof ReportsEmail_2_Txt  != "undefined" && Trim(ReportsEmail_2_Txt .value) != ""){
				if (BadEmail(ReportsEmail_2_Txt .value))
					errors.push("ERROR: The Reports \'Email 2\' address is not valid");
				else 
					radioCheck(ReportsEmail_2_Type,'Reports \'Email 2\'');
			}

			if (typeof ReportsEmail_3_Txt  != "undefined" && Trim(ReportsEmail_3_Txt .value) != ""){
				if (BadEmail(ReportsEmail_3_Txt .value))
					errors.push("ERROR: The Reports \'Email 3\' address is not valid");
				else 
					radioCheck(ReportsEmail_3_Type,'Reports \'Email 3\'');
			}
		}
	}
	
	if (errors.length){
	
		if (checkForm.errorBox ) {
			// clear the error div
			element = document.getElementById("error");
			document.getElementById("error").removeChild(element.firstChild);
		}
		checkForm.errorBox = document.createElement("div");
		document.getElementById("error").appendChild(checkForm.errorBox);																
		
		for (var i = 0; i < errors.length; i++) 
		{
			var p = document.createElement("p");
			p.appendChild(document.createTextNode(errors[i]));
			checkForm.errorBox.appendChild(p)
		}
		return false;
	}
	else {
		return true;
	}
	
	
}

function radioCheck (radioElement,label){

	var radioSelected = false;
	
	for (i=0; i<radioElement.length; i++) {
		if (radioElement[i].checked)
			radioSelected = true; 
	}
	
	if (!radioSelected)
		errors.push("ERROR: The email format (XML or Text) is required for the " + label + " field.");
		
	return;
}



function badNumCheck(num,maxlength){
	var pattern = new RegExp("^\\d{" + maxlength + "}$");
	if (!pattern.test(num)) {
		return true;
	}
}

//5868 For CIDphnTxt(Dealership Certified Internet Manager phone number) format
function phoneFormatValidate(num)
{
	var pattern = new RegExp(/^[1-9]\d{2}\-\d{3}\-\d{4}$/);
	if (!pattern.test(num)) {
		return true;
	}
}	

function BadEmail(str){
	var regexp = "";
	regexp = /@.*@/;
	if (regexp.test(str)) return true;
	regexp = /[, ;]/;
	if (regexp.test(str)) return true;
	regexp = /.+@[A-Za-z0-9\-]+\.[A-Za-z0-9\-.]*$/;
	if (regexp.test(str)) return false;
	return true;
}
function Trim(str){
	var regexp = "";
	regexp = / $/;
	while (regexp.test(str)) str = str.substr(0,str.length - 1);
	regexp = /^ /;
	while (regexp.test(str)) str = str.substr(1);
	return str;
}