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 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] + "_yearsLicensedTB";
	var BirthdateText = control.value;
//debugger		

	
	yearsLicensed = CalcYearsLicensed(BirthdateText)
	eval("document.forms[0]."+yearsLiscControlName+".value = " + 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 = 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);
}
