var phoneNumberDelimiters = "()-. ";
var digitsInUSPhoneNumber = 10;

//--------------PHONE VALIDATION SECTION--------------
function reformatUSPhone (USPhone)
{   return (reformat (USPhone, "(", 3, ") ", 3, "-", 4, "", 10))
	
}

function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}
function isDigit (c){
   return ((c >= "0") && (c <= "9"))
}
function isInteger (s){
    var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return true;
       else return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function OpenNext()
{
			var res=Page_ClientValidate();
			//alert(res);
			if (res==true)
			{
				document.Form1.submit(); 
				openDetailWindow('RequestSent.aspx','AutoDetail','width=700,height=800,scrollbars=yes,resizable=yes,menubar=no,menubar=yes,toolbar=no,copyhistory=no');
				
			}
}
 function OnEmailValidate(source, clientside_arguments)
   {     
	  clientside_arguments.IsValid=false;
	  if(document.Form1.Control0_emailCB.checked==false)
	   {
		clientside_arguments.IsValid=true; 
		return; 
	  }   
      if(document.Form1.Control0_emailCB.checked==true&&document.Form1.Control0_emailTB.value!=null&&document.Form1.Control0_emailTB.value!="")
      {
			//alert(document.Form1.Control0_emailTB.value);
			clientside_arguments.IsValid=true;
	  }
}
function OnDayPhoneValidate(source, clientside_arguments)
   {     
	  clientside_arguments.IsValid=false;
	  if(document.Form1.Control0_dayphoneCB.checked==false)
	   {
		clientside_arguments.IsValid=true; 
		return; 
	  }  
      if(document.Form1.Control0_dayphoneCB.checked==true&&document.Form1.Control0_dayphoneTB.value!=null&&document.Form1.Control0_dayphoneTB.value!="")
      {
			//alert(document.Form1.Control0_emailTB.value);
			clientside_arguments.IsValid=true;
	  }
}
function OnEvePhoneValidate(source, clientside_arguments)
   {     
	  clientside_arguments.IsValid=false;
	  if(document.Form1.Control0_evephoneCB.checked==false)
	  {
		clientside_arguments.IsValid=true; 
		return; 
	  }   
      if(document.Form1.Control0_evephoneCB.checked==true&&document.Form1.Control0_evephoneTB.value!=null&&document.Form1.Control0_evephoneTB.value!="")
      {
			//alert(document.Form1.Control0_emailTB.value);
			clientside_arguments.IsValid=true;
	  }
}
function OnCellPhoneValidate(source, clientside_arguments)
   {     
	  clientside_arguments.IsValid=false;
	  if(document.Form1.Control0_cellCB.checked==false)
	  {
		clientside_arguments.IsValid=true; 
		return; 
	  }
		 
      if(document.Form1.Control0_cellCB.checked==true&&document.Form1.Control0_cellTB.value!=null&&document.Form1.Control0_cellTB.value!="")
      {
			//alert(document.Form1.Control0_emailTB.value);
			clientside_arguments.IsValid=true;
	  }
}
function OnMailValidate(source, clientside_arguments)
   {     
	  clientside_arguments.IsValid=false;
	  if(document.Form1.Control0_mailCB.checked==false)
	  {
		clientside_arguments.IsValid=true; 
		return; 
	  }  
      if(document.Form1.Control0_mailCB.checked==true&&document.Form1.Control0_streetTB.value!=null&&document.Form1.Control0_streetTB.value!="")
      {
			//alert(document.Form1.Control0_emailTB.value);
			clientside_arguments.IsValid=true;
			return;
	  }
}
function OnContactValidate(source, clientside_arguments)
{
	clientside_arguments.IsValid=true;;
	  if(document.Form1.Control0_emailCB.checked==false&&document.Form1.Control0_dayphoneCB.checked==false&&
	  document.Form1.Control0_evephoneCB.checked==false&&document.Form1.Control0_cellCB.checked==false&&document.Form1.Control0_mailCB.checked==false)
	  {
		clientside_arguments.IsValid=false; 
		return; 
	  }  
      
}
function checkUSPhone (theField)
{
//debugger
	var bOkay = true;
	if ((isEmpty(theField.value))) bOkay = true;
	else
	{  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
				
		if (!isUSPhoneNumber(normalizedPhone, false)) 
			bOkay = false;
		else 
		{
			theField.value = reformatUSPhone(normalizedPhone).substring(0,14)
			if (theField.value.substring(1,2) == "0" || theField.value.substring(6,7) == "0" ) 			
			{				
				bOkay = false;		
				theField.value = "";
			}
		}
	}

	return bOkay
}
function isUSPhoneNumber (s){
    if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return !defaultRequired;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s.substring(0,digitsInUSPhoneNumber)) && s.length == digitsInUSPhoneNumber)
}
function stripCharsInBag (s, bag){
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
		//fix to not allow characters into the phone number
        if (isDigit(c)) returnString += c;
		//old code if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function reformat (s){
    var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
			resultString += s.substring(sPos, sPos + arg);
			if (resultString.length >= digitsInUSPhoneNumber) resultString = resultString + " "
           sPos += arg;
       }
    }
    return resultString;
}



function setYearsLisc(control)
{

	// since we can have multiple controls on the same page, we must determin the control number
	// so that we set the years licensed for only one control. 
	var controlName = control.id;
	var controlNumber = controlName.split('_');
	var yearsLiscControlName = controlNumber[0] + "_usYearsLicensedTB";
	var BirthdateText = control.value;
//debugger	
	var nameText=document.getElementById(controlNumber[0]+"_driverNameTB").value;
   if(nameText==null||nameText=="")
	return;//no year validation if name is missing
	
	yearsLicensed = CalcYearsLicensed2(control,BirthdateText);
	if((yearsLicensed<-1)||isNaN(yearsLicensed)||(yearsLicensed>97))
	{
		
		if(!isNaN(yearsLicensed))
			document.getElementById(controlNumber[0] + "_rangeValidator").style.display="block";
		yearsLicensed=0;
		eval("document.forms[0]."+yearsLiscControlName+".value = " + yearsLicensed);
		document.getElementById(controlNumber[0] + "_birthDateTB").focus();
		if(document.getElementById(controlNumber[0] + "_Button1")!=null)
			document.getElementById(controlNumber[0] + "_Button1").disabled=true;
		return;
	}
	else {
		if(yearsLicensed<0)
			yearsLicensed=0;
		document.getElementById(controlNumber[0] + "_rangeValidator").style.display="none";
		if(document.getElementById(controlNumber[0] + "_Button1")!=null)
			document.getElementById(controlNumber[0] + "_Button1").disabled=false;
		
		}
		
	eval("document.forms[0]."+yearsLiscControlName+".value = " + yearsLicensed);
	//hide star
	

}	
function CalcYearsLicensed2(control,BirthdateText)
{

	//debugger
	var Birthdate = BirthdateText.split('/');
	var now = new Date();
	var nYear = Birthdate[2]
	var nMonth = Birthdate[0]-1;
	var nDate = Birthdate[1];
	//alert("Length "+nYear.length)
	if((!isNaN(nYear))&&nYear.length==2)
	{

		 nYear=parseInt(nYear);
		 nYear=1900+nYear;
		 if(now.getFullYear-nYear>100)
			nYear=nYear+100;
		//reset birthdate control for range validation
		var controlName = control.id;
		var controlNumber = controlName.split('_');
		var birthDateControl = controlNumber[0] + "_birthDateTB";
		var newDate=Birthdate[0]+"/"+Birthdate[1]+"/"+nYear;
		//alert(newDate);
		document.getElementById(birthDateControl).value=newDate;
		//eval("document.forms[0]."+birthDateControl+".value = "+newDate);  
	}
	//alert("year is "+nYear);	
	var YearDiff =0;
	
	YearDiff=now.getFullYear() - nYear;

	var yearsLicensed = YearDiff-16;
	
	if (now.getMonth() == nMonth)	
	{
		if (now.getDate()<nDate) 
			yearsLicensed--;
	}
	if (now.getMonth() < nMonth)
	{
		yearsLicensed--;		
	}

	

	return yearsLicensed;
}
	
function CalcYearsLicensed(BirthdateText)
{

	//debugger
	var Birthdate = BirthdateText.split('/');
	var now = new Date();
	var nYear = Birthdate[2]
	var nMonth = Birthdate[0]-1;
	var nDate = Birthdate[1];
	var YearDiff =0;
	
	YearDiff=now.getFullYear() - nYear;

	var yearsLicensed = YearDiff-16;
	
	if (now.getMonth() == nMonth)	
	{
		if (now.getDate()<nDate) 
			yearsLicensed--;
	}
	if (now.getMonth() < nMonth)
	{
		yearsLicensed--;		
	}

	if (yearsLicensed < 0) yearsLicensed = 0;	
	if (yearsLicensed >97) yearsLicensed = 0;

		
	if (isNaN(yearsLicensed))
		yearsLicensed = 0;

	return yearsLicensed;
}
function checkMilesOneWay(theField)
{
//debugger
	if (theField.value == 0)
		theField.value = 1;
}	
		
		

function checkYearsLicensed(control)
{
	// since we can have multiple controls on the same page, we must determine the control number
	// so that we get the years licensed from the yearsLicensedHidden field for the current control. 
	var controlName = control.id;
	var controlNumber = controlName.split('_');
	var birthDateControl = controlNumber[0] + "_birthDateTB";
	var BirthdateText;
	
	eval("BirthdateText = document.forms[0]."+birthDateControl+".value");   
	
//debugger

	MaxYearsLicensed = CalcYearsLicensed(BirthdateText)
	
	if ( control.value > MaxYearsLicensed)
		control.value = MaxYearsLicensed;
	else if ( control .value < 0)
		control.value = 0;

}	




// (THIS ALLOWS US TO USE AN EXTERNAL WINDOW)
//-- AND BE ABLE TO OPEN IT AGAIN IF IT WAS CLOSED 
//-- AND ALWAYS SEND THE URL TO THE SAME WINDOW
x = 0;
function openDetailWindow(theURL,winName,features) 
{ 

//debugger
 if (typeof x != "object" || x.closed) 
	x = open(theURL,winName,features);
 else
	x.location = theURL;
	
 	x.focus();
	x.opener = self;	
	
  
}

function GetHomeQuote(theURL,theTarget)
{
	window.open(theURL,theTarget);
}
