// JavaScript Document

//Trim
function trimAll(sString) 
	{
		while (sString.substring(0,1) == ' ')
		{
		sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
		sString = sString.substring(0,sString.length-1);
		}
	return sString;
	}
	
//First Name - display and clear message on text box
function clear_txt_first_name()
{
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_first_name.value)=="First Name")
		frm.txt_first_name.value="";
		frm.txt_first_name.focus();
}

function check_txt_first_name()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_first_name.value)=="")
		frm.txt_first_name.value="First Name";
}

//Last Name - display and clear message on text box
function clear_txt_last_name()
{
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_last_name.value)=="Last Name")
		frm.txt_last_name.value="";
		frm.txt_last_name.focus();
}

function check_txt_last_name()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_last_name.value)=="")
		frm.txt_last_name.value="Last Name";
}

//Address - display and clear message on text box
function clear_txt_address()
{
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_address.value)=="Address")
		frm.txt_address.value="";
		frm.txt_address.focus();
}

function check_txt_address()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_address.value)=="")
		frm.txt_address.value="Address";
}

//City - display and clear message on text box
function clear_txt_city()
{
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_city.value)=="City")
		frm.txt_city.value="";
		frm.txt_city.focus();
}

function check_txt_city()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_city.value)=="")
		frm.txt_city.value="City";
}

//State - display and clear message on text box
function clear_txt_state()
{
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_state.value)=="State")
		frm.txt_state.value="";
		frm.txt_state.focus();
}

function check_txt_state()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_state.value)=="")
		frm.txt_state.value="State";
}

//Zip - display and clear message on text box
function clear_txt_zip()
{
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_zip.value)=="Zip")
		frm.txt_zip.value="";
		frm.txt_zip.focus();
}

function check_txt_zip()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_zip.value)=="")
		frm.txt_zip.value="Zip";
}

//Phone - display and clear message on text box
function clear_txt_phone()
{
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_phone.value)=="Phone")
		frm.txt_phone.value="";
		frm.txt_phone.focus();
}

function check_txt_phone()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_phone.value)=="")
		frm.txt_phone.value="Phone";
}

//E-mail - display and clear message on text box
function clear_txt_email()
{
	if(document.getElementById('error_car'))
	document.getElementById('error_car').style.visibility = "hidden";
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_email.value)=="E-mail")
		frm.txt_email.value="";
		frm.txt_email.focus();
}

function check_txt_email()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_email.value)=="")
		frm.txt_email.value="E-mail";
}

//Driver's License No - display and clear message on text box
function clear_txt_license_no()
{
	var frm = document.car_insurance_frm;	
		if(trimAll(frm.txt_license_no.value)=="Driver's License No")
		frm.txt_license_no.value="";
		frm.txt_license_no.focus();
}

function check_txt_license_no()
{
var frm = document.car_insurance_frm;
		if(trimAll(frm.txt_license_no.value)=="")
		frm.txt_license_no.value="Driver's License No";
}

//

function submitbtn()
{

	var frm = document.car_insurance_frm;
	frm.HiddenAction.value="CarInsurance";
	frm.HiddenDriverBtn.value="Driver";
	//frm.Hidden_additional_policy.value=frm.additional_policy.checked;
		//alert(frm.additional_policy[0].checked);
	
	var alert_message="";
	var focus_field="";
	var msgCount = 0;
	
	if (trimAll(frm.txt_first_name.value) == "" || trimAll(frm.txt_first_name.value) == "First Name") 
	{	
	msgCount++;
	alert_message += msgCount+'. Your First name.\n';
	focus_field = "txt_first_name";
	}
	else
	{
	   
	   if(!(/^[a-zA-Z\'\-\,\. ]+$/.test(trimAll(frm.txt_first_name.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. Your First name should be text.\n';
			focus_field = "txt_first_name";
	   }
	  else if(!(/.*[a-zA-Z].*/.test(trimAll(frm.txt_first_name.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. Your First name should contain text.\n';
			focus_field = "txt_first_name";
	   }
	}
	
	
	if (trimAll(frm.txt_last_name.value) == "" || trimAll(frm.txt_last_name.value) == "Last Name") 
	{	
	msgCount++;
	alert_message += msgCount+'. Your Last name.\n';
	
	if(focus_field=="")
	focus_field = "txt_last_name";
	}
	else
	{
	   if(!(/^[a-zA-Z\'\-\,\. ]+$/.test(trimAll(frm.txt_last_name.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. Your Last name should be text.\n';
			if(focus_field=="")
			focus_field = "txt_last_name";
	    }
		else if(!(/.*[a-zA-Z].*/.test(trimAll(frm.txt_last_name.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. Your Last name should contain text.\n';
			if(focus_field=="")
			focus_field = "txt_last_name";
	   }
	
	}		
	
	if (trimAll(frm.txt_address.value) == "" || trimAll(frm.txt_address.value) == "Address") 
	{	
	msgCount++;
	alert_message += msgCount+'. Your Address.\n';
	
	if(focus_field=="")
	focus_field = "txt_address";
	}
	else if(!(/.*[a-zA-Z].*/.test(trimAll(frm.txt_address.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. Your Address should contain text.\n';
			if(focus_field=="")
			focus_field = "txt_address";
	   }
	 
	
	if (trimAll(frm.txt_city.value) == "" || trimAll(frm.txt_city.value) == "City") 
	{	
	msgCount++;
	alert_message += msgCount+'. Your City.\n';
	
	if(focus_field=="")
	focus_field = "txt_city";
	}
	else
	{
	   if(!(/^[a-zA-Z\'\-\,\. ]+$/.test(trimAll(frm.txt_city.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. City should be text.\n';
			if(focus_field=="")
			focus_field = "txt_city";
	    }
		else if(!(/.*[a-zA-Z].*/.test(trimAll(frm.txt_city.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. Your City should contain text.\n';
			if(focus_field=="")
			focus_field = "txt_city";
	   }
	
	}
	
	if (trimAll(frm.txt_state.value) == "" || trimAll(frm.txt_state.value) == "State") 
	{	
	msgCount++;
	alert_message += msgCount+'. Your State.\n';
	
	if(focus_field=="")
	focus_field = "txt_state";
	}
	else
	{
	   if(!(/^[a-zA-Z\'\-\,\. ]+$/.test(trimAll(frm.txt_state.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. State should be text.\n';
			if(focus_field=="")
			focus_field = "txt_state";
	    }
		else if(!(/.*[a-zA-Z].*/.test(trimAll(frm.txt_state.value))))
	   {
			msgCount++;
			alert_message += msgCount+'. Your State should contain text.\n';
			if(focus_field=="")
			focus_field = "txt_state";
	   }
	
	
	}
	
	if (trimAll(frm.txt_zip.value) == "" || trimAll(frm.txt_zip.value) == "Zip") 
	{	
	msgCount++;
	alert_message += msgCount+'. Your Zip.\n';
	
	if(focus_field=="")
	focus_field = "txt_zip";
	}
	else 
	{
	  if(isNaN(frm.txt_zip.value))
	  {
	    msgCount++;
		alert_message += msgCount+'. Your Zip should be numeric.\n';
	
		if(focus_field=="")
		focus_field = "txt_zip";
	  
	  }
		else if(!(/^\d+$/.test(frm.txt_zip.value))) 
		{ 
			msgCount++;
			alert_message += msgCount+'. Your Zip should be Valid\n'; 
			if(focus_field=="")
			focus_field = "txt_zip";		
		}
		else if(frm.txt_zip.value<=0)
		{
				msgCount++;
				alert_message += msgCount+'. Your Zip should be Valid\n';
				if(focus_field=="")
				focus_field = "txt_zip";
		}
	  else if(frm.txt_zip.value.length!=5)
	  {
	    msgCount++;
		alert_message += msgCount+'. Your Zip should be 5 digit.\n';
	
		if(focus_field=="")
		focus_field = "txt_zip";
	  
	  }
	 	
	}
	
	
	if (trimAll(frm.txt_phone.value) == "" || trimAll(frm.txt_phone.value) == "Phone") 
	{	
	msgCount++;
	alert_message += msgCount+'. Your Phone.\n';
	
	if(focus_field=="")
	focus_field = "txt_phone";
	}
	else
	{
		var num = frm.txt_phone.value.replace(/[^\d]/g,'');
		 if(num.length != 10 || num==0) 
		 {
			msgCount++;
			alert_message += msgCount+'. Your valid phone number (10 digit) with area code.\n';
			
			if(focus_field=="")
			focus_field = "txt_phone";            
		 }
		  else
		  {
		   frm.txt_phone.value = num.substring(0,3) + "-" + num.substring(3, 6) + "-" + num.substring(6);
		  }
	}
	
	if(frm.txt_email.value=="" || frm.txt_email.value=="E-mail")
	{
		msgCount++;
		alert_message += msgCount+'. Your E-mail.\n';
		
		if(focus_field=="")
		focus_field = "txt_email";		
	}
	else
	{
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.txt_email.value))	//This condition will true if the e-mail id is valid	
		{
			//ok;
		}
		else
		{
			msgCount++;
			alert_message += msgCount+'. Valid E-mail.\n';
		
			if(focus_field=="")
			focus_field = "txt_email";		

		}
	}
function is_numeric( mixed_var ) {

    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: David
    // +   improved by: taith
    // +   bugfixed by: Tim de Koning
    // *     example 1: is_numeric(186.31);
    // *     returns 1: true
    // *     example 2: is_numeric('Kevin van Zonneveld');
    // *     returns 2: false
    // *     example 3: is_numeric('+186.31e2');
    // *     returns 3: true
    // *     example 4: is_numeric('');
    // *     returns 4: false
 
    if (mixed_var === '') {
        return false;
    }
 
    return !isNaN(mixed_var * 1);
}

var numLicense;
var myOldString;	
var myNewString;	
	if (trimAll(frm.txt_license_no.value) == "" || trimAll(frm.txt_license_no.value) == "Driver's License No") 
	{	
	msgCount++;
	alert_message += msgCount+'. Driver\'s License No.\n';
	
	if(focus_field=="")
	focus_field = "txt_license_no";
	}
	else 
	{
	 myOldString = frm.txt_license_no.value;
	 myNewString = myOldString.replace(" ", "");
	 numLicense = is_numeric( myNewString );
	  if(!(/^[a-zA-Z0-9 ]+$/.test(frm.txt_license_no.value)))
	  {
			msgCount++;
			alert_message += msgCount+'. Driver\'s License number not Valid.\n';
			
			if(focus_field=="")
			focus_field = "txt_license_no";

	  }
	  else if(numLicense == true && parseFloat(myNewString) < 1)
	  {
			msgCount++;
			alert_message += msgCount+'. Driver\'s License number not Valid.\n';
			
			if(focus_field=="")
			focus_field = "txt_license_no";	    
	  }
	  
	}
	
	
	
	var radio_choice = false;

	for (counter = 0;  counter < frm.additional_policy.length; counter++)
	{
	if (frm.additional_policy[counter].checked)
	radio_choice = true; 
	}
	if (!radio_choice)
	{
	//alert("If you need additional drivers, Please select the Yes option");
	//frm.additional_policy[0].focus();
	//return (false);
	msgCount++;
	alert_message += msgCount+'. If you need additional drivers, Please select the Yes option.\n';
	
	if(focus_field=="")
	focus_field = "additional_policy";
	}
	
	var noofdrivers=frm.HiddenBtn.value;
	
	//var add_polcy=frm.additional_policy.checked;
	var add_polcy=frm.additional_policy[0].checked;
	//alert(frm.additional_policy[0].checked);
	if(add_polcy==true)
	{	
		var num = document.getElementById('getPolyValue').value;
		var count = 0;
		var poly_count;
		var vech_name;	
		
		for(i=0;i<num;i++)
		{ 
			count = i+1; 
			var add_vech=frm.additional_policy[0].checked;
			
			poly_count = document.getElementById('license_no'+count).value;
			vech_name = "license_no"+count;
			var txtval = ". Driver's License No";
			
			if (trimAll(poly_count) == "" || trimAll(poly_count) == "Driver's License No"+count) 
			{ 
				msgCount++; 
				alert_message += msgCount+txtval+count+'\n';			
				
				if(focus_field=="")
				focus_field = vech_name; 
			}
			else 
			{ 
			myOldString = poly_count;
			myNewString = myOldString.replace(" ", "");
			numLicense = is_numeric(myNewString);
			
				if(!(/^[a-zA-Z0-9 ]+$/.test(poly_count)))
				{ 
					msgCount++;
					alert_message += msgCount+txtval+count+' not Valid.\n';		
					if(focus_field=="")
					focus_field = vech_name;
				}
				else if(numLicense == true && parseFloat(myNewString) < 1)
				{
					msgCount++;
					alert_message += msgCount+txtval+count+' not Valid.\n';
					if(focus_field=="")
					focus_field = vech_name;
				}
			}	
		}
	}
	
	        var cn=1;
            var arr = new Array();
			arr[1]=frm.txt_license_no.value;
			if(document.getElementById('license_no1'))
			{
			arr[2]=frm.license_no1.value;
			 cn++;
			}
			
			if(document.getElementById('license_no2'))
			{
			arr[3]=frm.license_no2.value;
			cn++;
			}
			if(document.getElementById('license_no3'))
			{
			arr[4]=frm.license_no3.value;
			cn++;
			}
			if(document.getElementById('license_no4'))
			{
			cn++;
			arr[5]=frm.license_no4.value;
			}
		     var repeat=0;
			for(var i=1;i<cn;i++)
			{
			  for(var j=i+1;j<=cn;j++)
			   if(trimAll(arr[i])!='' && trimAll(arr[j])!='' && trimAll(arr[i])==trimAll(arr[j]))
			   {
			    repeat=1;
               }
			}   
			if(repeat==1)
			{
			msgCount++;
	            alert_message += msgCount+'.  Driver\'s License number repeating.\n';
			}
	if (frm.contact_day.value == '0') 
	{	
	msgCount++;
	alert_message += msgCount+'. Select contact day.\n';
	
	if(focus_field=="")
	focus_field = "contact_day";
	}
	else
	{
	//ajaxDay();
	}
	
	var len = frm.elements['contact_time[]'].length;
	i = 0;
	countTime=0;
	var chosen = "";
	for (i = 0; i < len; i++)
	{
		if (frm.contact_time[i].selected)
		{
		countTime++;
		//chosen = chosen + frm.contact_day[i].value + "\n"
			if(frm.contact_time[i].value =='0')
			{
			chosen='TimeTitle';
			}			
		}
	}
	
	if (countTime == '0' || chosen=='TimeTitle') 
	{	
	msgCount++;
	alert_message += msgCount+'. Select valid contact time.\n';
	
	if(focus_field=="")
	focus_field = "contact_time";
	}


	var terms = frm.member_cb.checked;
	if(terms == false)
	{
		msgCount++;
		alert_message += msgCount+'. I have read and agree to the Privacy Statement  and Terms & Conditions\n'; 
		if(focus_field=="")
		focus_field = "member_cb";
		           
	}
	

//Alert
	if(msgCount>0)
	{
	var alertmessage ='Please enter the following fields\n\n'+alert_message;
	alert(alertmessage);	
		//Focus
		if(focus_field == "txt_first_name")
		{
		frm.txt_first_name.focus();	
		}
		else if(focus_field == "txt_last_name")

		{
		frm.txt_last_name.focus();	
		}
		else if(focus_field == "txt_address")
		{
		frm.txt_address.focus();	
		}
		else if(focus_field == "txt_city")
		{
		frm.txt_city.focus();	
		}
		else if(focus_field == "txt_state")
		{
		frm.txt_state.focus();	
		}
		else if(focus_field == "txt_zip")
		{
		frm.txt_zip.focus();	
		}
		else if(focus_field == "txt_phone")
		{
		frm.txt_phone.focus();	
		}
		else if(focus_field == "txt_email")
		{
		frm.txt_email.focus();	
		}
		else if(focus_field == "txt_license_no")
		{
		frm.txt_license_no.focus();	
		}
		else if(focus_field == "additional_policy")
		{
		frm.additional_policy[0].focus();	
		}
		else if(focus_field == "license_no1")
		{
		frm.license_no1.focus();	
		}
		else if(focus_field == "license_no2")
		{
		frm.license_no2.focus();	
		}
		else if(focus_field == "license_no3")
		{
		frm.license_no3.focus();	
		}
		else if(focus_field == "license_no1")
		{
		frm.license_no4.focus();	
		}
		else if(focus_field == "contact_day")
		{
		frm.contact_day.focus();	
		}
		else if(focus_field == "contact_time")
		{
		frm.contact_time.focus();	
		}
		
		return false;
	}
	
		document.getElementById('SubmitBtn').innerHTML = '<img src="../Images/CU_Form_Submitbtn.gif" width="141" height="31" tabindex="1">';
	
	frm.email_address.value = frm.txt_email.value; //Asign to Constant Contact	
	frm.action="car-insurance";			
	frm.submit(); 
	
}