// JavaScript Document
function removeLeadingAndTrailingChar(inputString)
{
	var returnString = inputString.replace(/^ {1,}/, '');
	returnString = inputString.replace(/ {1,}$/, '');
	return returnString;
}
function isValidMTextBox(MyTextBox, MsgText)
{
	MyTextBox.value=removeLeadingAndTrailingChar(MyTextBox.value);
	if ((MyTextBox.value==""))
		{
			alert("Enter "+ MsgText);
			MyTextBox.focus();
			return false;
		}
return true;
}
function isValidAlphabetSpace(MyTextBox, MsgText)
{
	MyTextBox.value=removeLeadingAndTrailingChar(MyTextBox.value);
	if (! ( MyTextBox.value.match(/^([a-zA-Z ])*$/))  ) {
			alert("Enter Valid "+ MsgText);
			MyTextBox.focus();
			return false;
		}
return true;
}
function isValidNMTextBox(MyTextBox, MsgText)
{
	MyTextBox.value=removeLeadingAndTrailingChar(MyTextBox.value);
return true;
}
function isValidMSelectBox(MySelectBox, MsgText)
{
	if (MySelectBox.selectedIndex==0)
	{
		alert("Select "+ MsgText);
		MySelectBox.focus();
		return false;
	}
return true;
}
function isValidEmail(MyTextBox) {
	var emailAddress = MyTextBox.value;
	emailAddress = emailAddress.replace(/ */g, '');
	if(emailAddress == ""){
		alert("Please enter email");
		MyTextBox.focus();
		return false;		
	}
	if (! ( emailAddress.match(/^([\w.\-+])+@(([\w\-])+\.)+([a-zA-Z]{3})$/))  ) {
		alert("Enter valid Email");
		MyTextBox.focus();
		return false;
		}
	MyTextBox.value = emailAddress;
	return true;
}
function isValidZip(MyTextBox) {
	var ZipValue = MyTextBox.value;
	ZipValue = ZipValue.replace(/ */g, '');
	if(ZipValue == "")
	{
		alert("Please enter zipcode");
		MyTextBox.focus();
		return false;
	}	
	if(ZipValue == 0)
	{
		alert("Please enter valid zipcode");
		MyTextBox.focus();
		return false;
	}	
	if (! ( ZipValue.match(/^([0-9]{5,5})$/))  ) {
		alert("Enter valid Zip Code");
		MyTextBox.focus();
		return false;
		}
	MyTextBox.value = ZipValue;		
	return true;
}
function isValidPhone(MyTextBox, MsgText) {
	var PhoneValue = MyTextBox.value;
	PhoneValue = PhoneValue.replace(/[ \-()]*/g, '');
	if(PhoneValue =="")
	{
		alert("Please enter "+MsgText);
		MyTextBox.focus();
		return false;	
	}	
	if(PhoneValue ==0 || PhoneValue < 1000000000)
	{
		alert("Enter valid "+MsgText);
		MyTextBox.focus();
		return false;	
	}
	if (! ( PhoneValue.match(/^([1]{0,1})([0-9]{10,10})$/))  ) {
		alert("Enter valid "+MsgText);
		MyTextBox.focus();
		return false;
		}
	var ph = PhoneValue;
	var phVal = 0;
	var fc = 	ph.charAt(0);
	for(var i=1;i<10;i++)
	{
		if(fc == ph.charAt(i))
		{
			phVal +=1;	 
		}
	}
	if(phVal==9)
	{
		alert("Please enter valid phone number");
		MyTextBox.focus();
		return false;
	}
	
	//MyTextBox.value = PhoneValue;				
	return true;
}
function formatPhoneNumber(phoneValue){
	return ( ( phoneValue.length == 10 )? "("+phoneValue.substring(0,3)+") " + phoneValue.substring(3,6)+ " " +phoneValue.substring(6,10)  : phoneValue  );
}
function validateMakeORModelORTrim(element, msg) {
	if(element && ( element.type != 'hidden')) {
		var val = '';
		if(element.type == 'select-one') val = element[element.selectedIndex].value; else val = element.value;
		if(val=='')  { alert ('Select '+msg); element.focus(); return false; }
	}
	return true;
}

function validateQuickQuote(thisForm)
{
	//setCookies("fname","lname","1234567890","test@test.com","95008");
	//alert("out side JS");
	if(document.getElementById("zipFlag").value!=1)
	{
//		alert('inside js');
		if(!(validateMakeORModelORTrim(thisForm.make, 'Make'))) return false;
		if(!(validateMakeORModelORTrim(thisForm.make[1], 'Make'))) return false;	
		if(!(validateMakeORModelORTrim(thisForm.model, 'Model'))) return false;
		if(!(validateMakeORModelORTrim(thisForm.model[1], 'Model'))) return false;	
		if(!(validateMakeORModelORTrim(thisForm.trim, 'Trim'))) return false;	
		if(!(validateMakeORModelORTrim(thisForm.trim[1], 'Trim'))) return false;	
	
		if(!(isValidMTextBox(thisForm.firstName, 'First Name'))) return false;
		if(!(isValidAlphabetSpace(thisForm.firstName, 'First Name'))) return false;
		if(!(isValidMTextBox(thisForm.lastName, 'Last Name'))) return false;
		if(!(isValidAlphabetSpace(thisForm.lastName, 'Last Name'))) return false;	
		if(!(isValidPhone(thisForm.phoneNumber, 'Phone Number'))) return false;
		if(!(isValidZip(thisForm.zipCode))) return false;	
		if(!(isValidEmail(thisForm.emailAddress))) return false;
		//setCookies(fname,lname,phonenumber, emailid,zipcode);
		//setCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value);
		var dealersLength = 0;
		if(document.getElementById('noofdealers')!=null){
			dealersLength = parseInt(document.getElementById('noofdealers').value);
		}
		if(dealersLength !=0)
		{
			var isChecked =false;
			for(var k=0;k<dealersLength;k++){
				if(document.getElementById('dealer_'+k).checked ){
					isChecked  = true;
					break;
				}
			}
			if(!isChecked){
				alert('Please select at least one dealer');
				document.getElementById("checkAllBox").focus();
				return false;
			}
	if(getCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value)){

		return setCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value);

 }else{return false;}
 		alert(isChecked);
		return isChecked;
		}else{
	if(getCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value)){

		return setCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value);

 }else{return false;}	
		}
	
		return true;
	}
	else{
	//alert("Else part");
	return false;
	}
}
/*This function is only for Model,Trim,PPC Dealer*/
/*This function is only for Model,Trim,PPC Dealer*/
function validateDealerForm(thisForm)
{
	if(document.getElementById("zipFlag").value!=1)
	{
		if(!(isValidMTextBox(thisForm.firstName, 'First Name'))) return false;
		if(!(isValidAlphabetSpace(thisForm.firstName, 'First Name'))) return false;
		if(!(isValidMTextBox(thisForm.lastName, 'Last Name'))) return false;
		if(!(isValidAlphabetSpace(thisForm.lastName, 'Last Name'))) return false;	
		if(!(isValidPhone(thisForm.phoneNumber, 'Phone Number'))) return false;
		if(!(isValidEmail(thisForm.emailAddress))) return false;

		var dealersLength = 0;
		if(document.getElementById('noofdealers')!=null){
			dealersLength = parseInt(document.getElementById('noofdealers').value);
		}
		if(dealersLength !=0)
		{
			var isChecked =false;
			for(var k=0;k<dealersLength;k++){
				if(document.getElementById('dealer_'+k).checked ){
					isChecked  = true;
					break;
				}
			}
			if(!isChecked){
				alert('Please select at least one dealer');
				document.getElementById("checkAllBox").focus();
				return false;
			}
			if(getCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value)){
				return setCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value);

			}else{
				 return false;
			}
		return isChecked;
		}else{
			if(getCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value)){

					return setCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value);

			 }else{
				 return false;
			}	
		}
		
	}
	else {
		return false;
	}
}
/*
function validateDealerForm(thisForm)
{
	if(document.getElementById("zipFlag").value!=1)
	{
		if(!(isValidMTextBox(thisForm.firstName, 'First Name'))) return false;
		if(!(isValidAlphabetSpace(thisForm.firstName, 'First Name'))) return false;
		if(!(isValidMTextBox(thisForm.lastName, 'Last Name'))) return false;
		if(!(isValidAlphabetSpace(thisForm.lastName, 'Last Name'))) return false;	
		if(!(isValidPhone(thisForm.phoneNumber, 'Phone Number'))) return false;
		if(!(isValidEmail(thisForm.emailAddress))) return false;

		var dealersLength = parseInt(document.getElementById('noofdealers').value);
		if(dealersLength !=0)
		{
			var isChecked =false;
			for(var k=0;k<dealersLength;k++){
				if(document.getElementById('dealer_'+k).checked ){
					isChecked  = true;
					break;
				}
			}
			if(!isChecked){
				alert('Please select at least one dealer');
				return false;
			}

			if(getCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value)){
		return setCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value);

 }else{return false;}
		return isChecked;
		}else{
	if(getCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value)){
		return setCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value);

 }else{return false;}	
		}
	}
	else {return false;}
}
*/
function validateDealerLocatorQuickQuote(thisForm)
{
	
	var dealerCount = thisForm.dealerCount.value;
	var selectedDlrCnt = 0;
	if(dealerCount > 1){
		for(i=0; i<dealerCount; i++){
			if(thisForm.dealer[i].checked)
			selectedDlrCnt++;
		}
	}
	else if(dealerCount ==1){
			if(thisForm.dealer.checked)
			selectedDlrCnt++;
		}
	if(selectedDlrCnt > 4){
		alert('Select Only 4 Dealers');
		return false;
	}
	if(!(validateMakeORModelORTrim(thisForm.make, 'Make'))) return false;
	if(!(validateMakeORModelORTrim(thisForm.make[1], 'Make'))) return false;	
	if(!(validateMakeORModelORTrim(thisForm.model, 'Model'))) return false;
	if(!(validateMakeORModelORTrim(thisForm.model[1], 'Model'))) return false;	
	if(!(validateMakeORModelORTrim(thisForm.trim, 'Trim'))) return false;	
	if(!(validateMakeORModelORTrim(thisForm.trim[1], 'Trim'))) return false;	

	if(!(isValidMTextBox(thisForm.firstName, 'First Name'))) return false;
	if(!(isValidAlphabetSpace(thisForm.firstName, 'First Name'))) return false;
	if(!(isValidMTextBox(thisForm.lastName, 'Last Name'))) return false;
	if(!(isValidAlphabetSpace(thisForm.lastName, 'Last Name'))) return false;	
	if(!(isValidPhone(thisForm.phoneNumber, 'Phone Number'))) return false;
	if(!(isValidZip(thisForm.zipCode))) return false;	
	if(!(isValidEmail(thisForm.emailAddress))) return false;
	return true;
}
/*This function is for only PPC Pages witout zipcode*/
function validateQuotePPC(thisForm)
{
	if(!(isValidMTextBox(thisForm.firstName, 'First Name'))) return false;
	if(!(isValidAlphabetSpace(thisForm.firstName, 'First Name'))) return false;
	if(!(isValidMTextBox(thisForm.lastName, 'Last Name'))) return false;
	if(!(isValidAlphabetSpace(thisForm.lastName, 'Last Name'))) return false;	
	if(!(isValidPhone(thisForm.phoneNumber, 'Phone Number'))) return false;
	if(!(isValidZip(thisForm.zipCode))) return false;	
	if(!(isValidEmail(thisForm.emailAddress))) return false;
if(getCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value)){
		return setCookies(thisForm.firstName.value,thisForm.lastName.value,thisForm.phoneNumber.value,thisForm.emailAddress.value,thisForm.zipCode.value);
 }else{
	 return false;
	}

	return true;
}

function validateMakeModelZip(thisForm)
{
	if(!(validateMakeORModelORTrim(thisForm.make, 'Make'))) return false;
	if(!(validateMakeORModelORTrim(thisForm.make[1], 'Make'))) return false;	
	if(!(validateMakeORModelORTrim(thisForm.model, 'Model'))) return false;
	if(!(validateMakeORModelORTrim(thisForm.model[1], 'Model'))) return false;		
	if(!(isValidZip(thisForm.zipCode))) return false;	
return true;
}
/*For changing action URL in index.jsp*/
function validateMakeModelZipNew(thisForm)
{
	if(!(validateMakeORModelORTrim(thisForm.make, 'Make'))) return false;
	if(!(validateMakeORModelORTrim(thisForm.make[1], 'Make'))) return false;	
	if(!(validateMakeORModelORTrim(thisForm.model, 'Model'))) return false;
	if(!(validateMakeORModelORTrim(thisForm.model[1], 'Model'))) return false;		
	if(!(isValidZip(thisForm.zipCode))) return false;

	/*var makeValue = thisForm.make[thisForm.make.selectedIndex].innerHTML;
	var modelValue = thisForm.model[thisForm.model.selectedIndex].innerHTML;*/

	//makeValue = (makeValue.toLowerCase()).replace(/ */g, '');
	//modelValue = (modelValue.toLowerCase()).replace(/ */g, '');
	//alert(makeValue);
	//alert(modelValue);
	/*thisForm.action = "research/"+makeValue+"_"+modelValue;
	thisForm.submit();*/
	//return true;
	
	var modelValue = thisForm.model[thisForm.model.selectedIndex].value;
	//alert(modelValue);
	thisForm.action = modelValue;
	thisForm.submit();
}

function validateMakeZip(thisForm)
{
	if(!(validateMakeORModelORTrim(thisForm.dealerMake, 'Make'))) return false;
	if(!(validateMakeORModelORTrim(thisForm.dealerMake[1], 'Make'))) return false;	
	if(!(isValidZip(thisForm.zipcode))) return false;	
return true;
}
function validateDealerSelected(thisForm)
{
var cnt = parseInt(thisForm.dealerCount.value);
if (cnt == 1 ) {
	if  ( document.getElementById('dealer_0').checked   ) return true;	
alert ( 'Select Dealer' ) ; 

}
else if (cnt > 1 ) {
	for ( i = 0 ; i <  cnt ; i++ ) 
	if  ( document.getElementById('dealer_'+i).checked   ) return true;
alert ( 'Select Atleast One Dealer' ) ; 
}
return false;
}

function validateCompareCount(thisForm) {
	var cnt = 0;
	if ( ( document.getElementById('izmopix1').value != '') && ( document.getElementById('izmopix1').value != 'undefined') &&  ( document.getElementById('izmopix1').value != null) ) cnt = cnt + 1 ; 
	if ( ( document.getElementById('izmopix2').value != '') && ( document.getElementById('izmopix2').value != 'undefined') &&  ( document.getElementById('izmopix2').value != null) ) cnt = cnt + 1 ; 
	if ( ( document.getElementById('izmopix3').value != '') && ( document.getElementById('izmopix3').value != 'undefined') &&  ( document.getElementById('izmopix3').value != null) ) cnt = cnt + 1 ; 
	if ( cnt >=2 ) return true;
	alert ('To Compare Cars, Add Atleast Two Vehicles');
	return false;
}

function setMaxLength(thisTextBox,size) {
	thisTextBox.maxLength = size ;
}

function setTextField(field, initialValue) {
	if (field.value == '') {
		field.value = initialValue;
	}
}
function myvalidnumber(e)
{
var mykey = window.event ? e.keyCode : e.which;
		if ((mykey <= 47 || mykey > 57) && mykey !=13 && mykey !=0 && mykey != 8) 
			{
		return false;
			}
return true;			
}
function validateMPG(thisForm){
	if 	(( parseInt(thisForm.mpg.value) == 0 ) || ( thisForm.mpg.value == '' )) {
		thisForm.mpg.focus();
		thisForm.mpg.select();
		return false;
	}
	return true;
}
function setCookies(fname,lname,phonenumber,emailid,zipcode)
{
		var expires = 1 * 1000 * 60 * 60 * 24;
		var today = new Date();
		var exdate=new Date(today.getTime() + (expires));
		document.cookie = emailid.toLowerCase()+"="+phonenumber+";expires="+exdate.toGMTString()+";path=/";  
		return true;
}
function getCookies(fname,lname,phonenumber, emailid,zipcode)
{
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(emailid.toLowerCase()+ "=");
	    if (c_start!=-1){ 
			c_start=c_start + emailid.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			var cookieValue= document.cookie.substring(c_start);
			var allcooKieValues = cookieValue.split(";");
			if(phonenumber ==allcooKieValues[0]){
						 alert("As per our policy, we do not allow more than one submission within a 24 hour period.");
						 return false;
			}
	    } 
	  }
	return true;
}

function eraseCookie(emailld,phoneNum){
		if (document.cookie.length>0) {
				c_start=document.cookie.indexOf(emailld.toLowerCase()+ "=");
				if(c_start!=-1){
					c_start=c_start + emailld.length+1; 
					var cookieValue= document.cookie.substring(c_start);
					var allcooKieValues = cookieValue.split(";");
					if(phoneNum ==allcooKieValues[0]){
						document.cookie = emailld.toLowerCase()+"="+phoneNum+";expires=Thu, 01-Jan-1970 00:00:01 GMT"+";path=/";  
					}
				}
		}
}

function deleteCookies(){
	var emailId = "";
	var phoneNum = "";
	if(document.getElementById('emailAddress')!=null){
			emailId = document.getElementById('emailAddress').value;
	}
	if(document.getElementById('phoneNumber')!=null){
			phoneNum = document.getElementById('phoneNumber').value;
	}
	if(emailId!="" && phoneNum!=""){
		eraseCookie(emailId,phoneNum);
	}

}
/*function validateMakeModelTrim(thisButton) {
	alert('reached');
	var thisId = thisButton.id ; 
alert (thisId.substring(8,9))  ;
	if (( typeof (thisForm.make[1]) == 'object')  && ( typeof (thisForm.make[1].type) == 'string')  && ( thisForm.make[1].type != 'hidden')  ){
		if(thisForm.make[1].value=='')  { alert ('Select Make'); thisForm.make[1].focus(); return false; }
	} 
	if (( typeof (thisForm.model[1]) == 'object')  && ( typeof (thisForm.model[1].type) == 'string')  && ( thisForm.model[1].type != 'hidden')   ){
		if(thisForm.model[1].value=='')  { alert ('Select Model'); thisForm.model[1].focus(); return false; }
	} 	
	if (( typeof (thisForm.trim[1]) == 'object')  && ( typeof (thisForm.trim[1].type) == 'string')  && ( thisForm.trim.type[1] != 'hidden')  ){
		if(thisForm.trim[1].value=='')  { alert ('Select Trim'); thisForm.trim[1].focus(); return false; }
	} 
	return false;
}
*/
