var emailregex = /^[\w\_\-\.]+@[\w\_\-\.]+\.\w+$/;

//define available digits
var PHdigits = "0123456789";

// non-digit characters which are allowed in phone numbers
var PHdelimiters = "()- .\/";

// characters which are allowed in US phone numbers
var PHvalidchar = PHdigits + PHdelimiters;

// U.S. phone numbers have 10 digits, without delimiters.
// They are formatted as 123 456 7890 or (123) 456-7890.
var PHnumofdigits = 10;

var PHalert = "Please enter a 10 digit U.S. phone number.";

var PHdefaultEmptyOK = false;

var ZIPdigits = "0123456789";

//a zip contains 5 digits
//this script does not accomodate for specific zip codes like 57252-3101
var ZIPnumofdigits = 5;

var ZIPalert = "Please enter a 5 digit U.S. zip code.";

var ZIPdefaultEmptyOK = true;


function checkEmail(theField, emptyOK)
{
	if(theField.value.length == 0 && (emptyOK))
	{
		return true;
	}
	else
	{
		if(!theField.value.match(emailregex))
			{
			alert("The email address  " + theField.value + "  is not in a correct format.\n Please try again");
			theField.focus();
			theField.select();
			return false;
		}
	}

}

function PHstripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

//************************* isUSPhoneNumber Function *******************************
//called from checkUSPhone function

function isUSPhoneNumber (s)
{   
    return (PHisInteger(s) && s.length == PHnumofdigits)
}

//************************* PHisInteger Function *******************************
//called from isUSPhoneNumber function

function PHisInteger (s)
{   var i;

    if (PHisEmpty(s)) 
       if (PHisInteger.arguments.length == 1) return PHdefaultEmptyOK;
       else return (PHisInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!PHisDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

//************************* isEmpty Function *******************************
//called from PHisInteger function

// Check whether string s is empty.
function PHisEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//************************* PHisDigit Function *******************************
//called from PHisInteger function

// Returns true if character c is a digit 
// (0 .. 9).
function PHisDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


//************************* PHwarnInvalid Function ****************************
//called from checkUSPhone function

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.
function PHwarnInvalid (theField, s)
{   alert(s)
    theField.focus()
    theField.select()
    return false;
}

//************************* checkUSPhone Function *******************************
//called from form 

// Check that string theField.value is a valid US Phone.

function checkUSPhone (theField, emptyOK)
{ 

if(theField.value.length == 0 && (emptyOK))
	{
		return true;
	}
else
{  
 var PHnormalized = PHstripCharsInBag(theField.value, PHdelimiters)
 if (!isUSPhoneNumber(PHnormalized, false)) 
    return PHwarnInvalid(theField, PHalert);
 else 
 {  // if you don't want to reformat as (123) 456-789, comment next line out
    theField.value = reformatUSPhone(PHnormalized)
    return true;
 }
}

}


//************************* PHreformat Function *******************************
//called from reformatUSPhone function

//           "(", 3, ") ", 3, "-", 4)

function PHreformat (s)
{   var arg;
    var stringPos = 0;
    var resultString = "";
    for (var i = 1; i < PHreformat.arguments.length; i++) {
       arg = PHreformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(stringPos, stringPos + arg);
           stringPos += arg;
       }
    }
    return resultString;
}

//************************* reformatUSPhone Function *******************************
//called from checkUSPhone function

// takes USPhone, a string of 10 digits
// and reformats as (123) 456-789
function reformatUSPhone (USPhone)
{   return (PHreformat (USPhone, "(", 3, ") ", 3, "-", 4))
}


//************************* checkUSZip Function *******************************
//called from form 

// Check that string theField.value is a valid US Zip Code.

function checkUSZip(theField, emptyOK)
{  

if(theField.value.length == 0 && (emptyOK))
	{
		return true;
	}
else
{
 
 var normalized = theField.value
 if (!isUSZip(normalized, false)) 
    return ZIPwarnInvalid(theField, ZIPalert);
 else 
 { 
    return true;
 }

} 

}

//************************* isUSZip Function *******************************
//called from checkUSZip function

function isUSZip (s)
{   
    return (ZIPisInteger(s) && s.length == ZIPnumofdigits)
}

//************************* ZIPisInteger Function *******************************
//called from isUSZip function

function ZIPisInteger (s)
{   var i;

    if (ZIPisEmpty(s)) 
       if (ZIPisInteger.arguments.length == 1) return ZIPdefaultEmptyOK;
       else return (ZIPisInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!ZIPisDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

//************************* ZIPisEmpty Function *******************************
//called from ZIPisInteger function

// Check whether string s is empty.
function ZIPisEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//************************* ZIPisDigit Function *******************************
//called from ZIPisInteger function

// Returns true if character c is a digit 
// (0 .. 9).
function ZIPisDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


//************************* ZIPwarnInvalid Function ****************************
//called from checkUSZip function

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.
function ZIPwarnInvalid (theField, s)
{   	
	alert(s);
        theField.focus();
    	theField.select();
        return false;
}

function submitForm(theForm) {
	// need to verify that the required fields have values.
	// firstName, lastName, phone, email address, zip code, dealership
	if ( theForm.firstName.value == "") {
		alert ("You must enter a first name.");
		 theForm.firstName.focus();
    	
		return false;
	}
	if ( theForm.lastName.value == "") {
		alert ("You must enter a last name.");
		theForm.lastName.focus();
		return false;
	}
	
	if ( theForm.address.value == "") {
		alert ("You must enter address.");
		theForm.address.focus();
		return false;
	}
	if ( theForm.city.value == "") {
		alert ("You must enter city.");
		theForm.city.focus();
		return false;
	}
	if ( theForm.state.value == "") {
		alert ("You must enter state.");
		theForm.state.focus();
		return false;
	}
	
	if ( theForm.zip.value == "") {
		alert ("You must enter a zip code.");
		theForm.zip.focus();
		return false;
	}
	if ( theForm.phone.value == "") {
		alert ("You must enter a phone number.");
		theForm.phone.focus();
		return false;
	}
	if ( theForm.email.value == "") {
		alert ("You must enter an email address.");
		theForm.email.focus();
		return false;
	}
	
}
	function submitForm2(theForm) {
	// need to verify that the required fields have values.
	// firstName, lastName, phone, email address, zip code, dealership
	if ( theForm.ssn1.value == "") {
		alert ("You must enter social security number.");
		 theForm.ssn1.focus();
    	
		return false;
	}
	if ( theForm.ssn2.value == "") {
		alert ("You must enter a social security number.");
		theForm.ssn2.focus();
		return false;
	}
	
	if ( theForm.ssn3.value == "") {
		alert ("You must enter social security number.");
		theForm.ssn3.focus();
		return false;
	}
	if ( theForm.DOB_month.value == "") {
		alert ("You must enter month.");
		theForm.DOB_month.focus();
		return false;
	}
	if ( theForm.DOB_day.value == "") {
		alert ("You must enter day.");
		theForm.DOB_day.focus();
		return false;
	}
	if ( theForm.DOB_year.value == "") {
		alert ("You must enter year.");
		theForm.DOB_year.focus();
		return false;
	}
	if ( theForm.mortgage.value == "") {
		alert ("You must enter amount.");
		theForm.state.focus();
		return false;
	}
	
	if ( theForm.employer.value == "") {
		alert ("You must enter employer.");
		theForm.employer.focus();
		return false;
	}
	if ( theForm.occupation.value == "") {
		alert ("You must enter occupation.");
		theForm.occupation.focus();
		return false;
	}
	if ( theForm.gross_income.value == "") {
		alert ("You must enter a montly income.");
		theForm.email.focus();
		return false;
	}
	if ( theForm.time_employed.value == "") {
		alert ("You must enter the period.");
		theForm.time_employed.focus();
		return false;
	} 
	
	if ( theForm.check_agreement.checked == false) {
		alert ("You must check the box to confirm that you read the Privacy Statement, agreement to receive Electronic Documents, and state-specific notices.");
		theForm.time_employed.focus();
		return false; 
	}
	
	
}

