<!--

// Generic Functions And Global Variables
var errorFree = true;
var theError = "Please provide the following information so we can better assist you:\n";
var submitted = false;

//Removes Commas from numeric fields
function strip_commas(field) {
    if(field.value.length != 0) {
	re = /[,$]/gi;
	str = field.value;
	field.value = str.replace(re, "");
    }
}

/*
function HeightInInches(feet, inches, height) {
    if(!isNaN(feet.value) && !isNaN(inches.value)) {
	height.value = (feet.value * 12) + inches.value;
    }
}
*/

function req_text(field, msg) {

    if(field.value.length == 0) {
	addMsg(field, msg);
    }
}

function req_ssn_3_fields(ac, pre, num, msg) {

    if(ac.value.length != 3 || pre.value.length != 2 || num.value.length != 4 || isNaN(ac.value) || isNaN(pre.value) || isNaN(num.value)) {
	addMsg(ac, msg);
    }
}

function req_phone_3_fields(ac, pre, num, msg) {

    if(ac.value.length != 3 || pre.value.length != 3 || num.value.length != 4 || isNaN(ac.value) || isNaN(pre.value) || isNaN(num.value)) {
	addMsg(ac, msg);
    }
}


function req_number(field, msg) {

    strip_commas(field);
    if(field.value.length == 0 || isNaN(field.value)) {
	addMsg(field, msg);
    }
}


function req_number_amt(field, amt, msg) {

    strip_commas(field);
    if(field.value.length == 0 || isNaN(field.value)) {
	addMsg(field, msg);
    }
    else if(field.value < amt) {
	addMsg(field, msg + " Must be greater than: " + amt);
    }
}


function req_one_number(field1, field2, msg) {

    strip_commas(field1);
    strip_commas(field2);
    if((field1.value.length == 0 || isNaN(field1.value)) && (field2.value.length == 0 || isNaN(field2.value))) {
	addMsg(field1, msg);
    }
}


function opt_number(field, msg, zeroed) {

    strip_commas(field);
    if(field.value.length != 0 && isNaN(field.value)) {
	addMsg(field, msg + " (not required)");
    }
    else if(field.value.length == 0 && zeroed) {
	field.value = 0;
    }
}


function req_number_length(field, length, msg) {

    strip_commas(field);
    if(field.value.length != length || isNaN(field.value)) {
	addMsg(field, msg);
    }
}


function opt_number_length(field, length, msg) {

    strip_commas(field);
    if(field.value.length != 0 && (field.value.length != length || isNaN(field.value))) {
	addMsg(field, msg + " (not required)");
    }
}


//Checks if a certain combo option is selected and throws an error if it is
function req_combo(combo, index, msg) {

    if(combo.options[index].selected) {
	addMsg(combo, msg);
    }
}


//Checks a text field if a certain combo option is selected
function req_text_w_combo(combo, index, field, msg) {

    if(combo.options[index].selected && field.value.length == 0) {
	addMsg(field, msg);
    }
}


//Checks a numeric field if a certain combo option is selected
function req_number_w_combo(combo, index, field, msg) {

    strip_commas(field);

    if(combo.options[index].selected && (field.value.length == 0 || isNaN(field.value))) {
	addMsg(field, msg);
    }
}

function req_regexp(field, exp_text, msg) {

    req_regexpRE = new RegExp(exp_text);
    if(!req_regexpRE.test(field.value)) {
	addMsg(field, msg);
    }
}


/*
function chk_Contact(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    req_text(form.FirstName, "First Name");
    req_text(form.LastName, "Last Name");
    req_text(form.Address, "Address");
    req_text(form.City, "City");
    req_combo(form.State, 0, "State");
    req_number_length(form.Zip, 5, "Zip Code");
    opt_number_length(form.Zip4, 4, "Zip+4");
    req_phone_3_fields(form.DayPhoneArea, form.DayPhonePre, form.DayPhoneNum, "Day Phone");
    req_phone_3_fields(form.EveningPhoneArea, form.EveningPhonePre, form.EveningPhoneNum, "Evening Phone");
    req_regexp(form.email, ".+@.+[.].+", "E-mail Address");
    req_text(form.TimeToCall, "Best time to call");
}
*/


function addMsg(control, msg) {
    theError += "\n\t-" + msg;
    if(errorFree) {
	control.focus();
    }
    errorFree = false;
}


function finish_Validation (theForm) {

   if(!errorFree)
	{
	    alert(theError);
	    return false;
	}
    else if(!submitted)
	{
	    submitted = true;
	    return true;
	}
   return false;
}

function validate_rate_alert(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";

	req_text(form.firstname, "First Name");
	req_text(form.lastname, "Last Name");
	req_regexp(form.email, ".+@.+[.].+", "E-mail Address");
	req_number_length(form.zip, 5, "Zip Code");
	req_number(form.loanamount, "Loan Amount")
	return finish_Validation(form);
}

function check_zip(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
	req_number_length(form.zip, 5, "Zip Code");
	return finish_Validation(form);
}

function validate_newsletter_signup(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_text(form.firstname, "First Name");
    req_text(form.lastname, "Last Name");
    req_regexp(form.email, ".+@.+[.].+", "E-mail Address");
	return finish_Validation(form);
}

function validate_multi_step_2(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_combo(form.CreditHistory, 0, "Credit Rating");
    req_number(form.LoanAmount, "Loan Amount");
    req_number(form.PropertyValue, "Current Value"); 
    if(form.Rate1) {
		req_number(form.Rate1, "Rate");
    }   
	return finish_Validation(form);
}

function validate_multi_step_3(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_text(form.FirstName, "First Name");
    req_text(form.LastName, "Last Name");
    req_text(form.Address, "Address");
    req_number(form.DayPhonePre, "Phone Prefix");
    req_number(form.DayPhoneNum, "Phone Suffix");
    req_regexp(form.Email, ".+@.+[.].+", "E-mail Address");
    req_number(form.Income, "Monthly Income");
    req_number(form.Debt, "Monthly Debt");
    return finish_Validation(form);
}

function validate_single_step(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_number(form.LoanAmount, "Loan Amount");
    req_number(form.PropertyValue, "Property Value");
    req_number_length(form.zip, 5, "Zip Code");
    req_text(form.FirstName, "First Name");
    req_text(form.LastName, "Last Name");
    //req_ssn_3_fields(form.SSNa, form.SSNb, form.SSNc, "Social Security Number")
    req_phone_3_fields(form.HomePhonea, form.HomePhoneb, form.HomePhonec, "Home Phone Number")
    //req_regexp(form.Email, ".+@.+[.].+", "E-mail Address");
    req_regexp(form.Email, "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$", "E-mail Address");
    req_number(form.Debt, "Monthly Expenses");
    
    return finish_Validation(form);
}

function find_zip() {
	window.open('/search/find_zip_code.html', 'search_win', "left=100, top=100, height=325, width=600, resizable=yes, scrollbars=yes");
	return false;
}

function set_parent_zip(zip_code) {
	window.opener.search.zip.value = zip_code;
	this.close();
}

function validate_payment_calculator(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
	req_number_amt(form.LoanAmount, 0, "Loan Amount");
	req_number_amt(form.PropertyValue, 0, "Property Value");
	req_number_length(form.zip, 5, "Zip Code");
	opt_number(form.AnnualTaxes, "Annual Taxes");
	opt_number(form.HazardIns, "Hazard Insurance");
	return finish_Validation(form);
}

function validate_qualify_caluclator(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_number_amt(form.YearlyIncome, 0, "Yearly Income");
    req_number_length(form.zip, 5, "Zip Code");
    
    opt_number(form.AnnualTaxes, "Annual Taxes");
    opt_number(form.HazardIns, "HazardInsurance");
    
    return finish_Validation(form);
}

function validate_appsf_form(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_text(form.FirstName, "First Name");
    req_text(form.LastName, "Last Name");
    req_number_length(form.zip, 5, "Zip Code");
    req_text(form.HomePhone, "Home Phone");
    req_regexp(form.Email, ".+@.+[.].+", "E-mail Address");
    
    return finish_Validation(form);
}

function validate_ratesf_form(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_text(form.FirstName, "First Name");
    req_text(form.LastName, "Last Name");
    req_text(form.HomePhone, "Home Phone");
    req_number_length(form.zip, 5, "Zip Code");
    req_regexp(form.Email, ".+@.+[.].+", "E-mail Address");
    
    return finish_Validation(form);
}

function validate_prequalsf_form(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_text(form.FirstName, "First Name");
    req_text(form.LastName, "Last Name");
    req_regexp(form.Email, ".+@.+[.].+", "E-mail Address");
	req_number(form.PropertyValue, "Home Value");
	req_number(form.LoanAmount, "Loan Amount");
	req_number(form.Income, "Monthly Income");
	req_number(form.Debt, "Monthly Debt");
    req_text(form.HomePhone, "Home Phone");
	req_number_length(form.Zip, 5, "Zip Code");
    
    return finish_Validation(form);
}

function validate_contact_form(form) {
	errorFree = true;
    theError = "Please correct the following information:\n";
    
    req_text(form.FirstName, "First Name");
    req_text(form.LastName, "Last Name");
    req_text(form.HomePhone, "Home Phone");
    req_number_amt(form.zip, 5, "Zip Code");
    //req_number_length(form.zip, 5, "Zip Code");
    req_regexp(form.Email, ".+@.+[.].+", "E-mail Address");
    
    return finish_Validation(form);    
}	

//-->
