// Generic Functions And Global Variables
var errorFree;
var theError;
var submitted = false;
var contact;
var mybgColor =  'C0C0C0';
var generalMortgage = false;

//Removes Commas from numeric fields
function strip_commas(field) {
    re = /[,$]/gi;
    str = field.value;
    field.value = str.replace(re, "");
}

function launchCalc(calc_name) 
{
    if(arguments.length > 1) mybgColor = arguments[1];
    else  mybgColor = "FFFFFF";
    popupObj = window.open ('http://CommonElements.iLeads.com/Calculators/' + calc_name + '?color=' + mybgColor,'Form','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=0,width=625,height=375');
    //popupObj = window.open ('/' + calc_name + '?color=' + mybgColor,'Form','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=0,width=625,height=375');
    popupObj.focus();
}

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_phone_3_fields(ac, pre, num, msg) {
    if(ac.value.length != 3 || isNaN(ac.value)) {
	addMsg(ac, msg);
    }
    else if(pre.value.length != 3 || isNaN(pre.value)) {
	addMsg(pre, msg);
    }
    else if(num.value.length != 4  || isNaN(num.value)) {
	addMsg(num, msg);
    }
}

function req_number(field, msg) {
    strip_commas(field);
    if(field.type != "hidden" && (field.value.length == 0 || isNaN(field.value))) {
	addMsg(field, msg);
    }
}


function req_number_min_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) {
    //alert(index + " - " + combo.options[index].selected);
    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.type != "hidden" && combo.options[index].selected && (field.value.length == 0 || isNaN(field.value))) {
	addMsg(field, msg);
    }
}
//URL   "http://.+\\..+\\..+"
//Email ".+@.+\\..+"
function req_regexp(field, exp_text, msg) {
    req_regexpRE = new RegExp(exp_text);
    if(!req_regexpRE.test(field.value)) {
	addMsg(field, msg);
    }
}

function addMsg(control, msg) {
    if(control.type != "hidden") {
	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;
}
// Form Validator scripts
//insurance and mortgage validators

// Mortgage Common Checks
function mortgage_common(theForm, newHome) {
    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    All_Common(theForm, false, false);
    
    if(generalMortgage) {
	req_combo(theForm.prod_type, 0, "Type of loan you're interested in?") ;
    }
    req_number_w_combo(theForm.self_employed, 0, theForm.years_employed, "How long have you been with your employer?");
    req_number(theForm.monthly_gross, "How much do you gross each month?");
    req_number(theForm.monthly_debt, "How much do you owe each month?");
    req_number(theForm.cash_needed, "What is the total loan amount desired?");
    req_number(theForm.how_long_owned, "How long have you owned your home?");
    req_number(theForm.home_sale_price, "Your home's original sale price?");
    req_number(theForm.mortgage_balance, "Your current mortgage balance?");
    if(newHome) {
	req_number(theForm.money_down, "How much money do you want to put down on your new home?");
	req_number(theForm.plan_to_pay, "What are you planning to pay for your new home?");
    }
    req_number(theForm.monthly_payment, "Your current mortgage monthly payment?");
    req_number(theForm.current_rate, "Current mortgage interest rate");
    req_number(theForm.current_worth, "How much is your home currently worth?");
    req_number_w_combo(theForm.second_mortgage, 1, theForm.second_balance, "2nd mortgage balance?");
    req_number_w_combo(theForm.second_mortgage, 1, theForm.second_payment, "2nd mortgage monthly payment?");
    req_number_w_combo(theForm.second_mortgage, 1, theForm.second_rate, "2nd mortgage interest rate?");

    return finish_Validation(theForm);
}

//Insurance Common Checks
function All_Common(theForm, birth, weight) {
    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    if(contact) {
	req_text(theForm.first_name, "First Name");
	req_text(theForm.last_name, "Last Name");
	req_text(theForm.real_addr, "Street Address");
	req_text(theForm.city, "City");
	req_number_length(theForm.zip_code, 5, "Zip Code");
	req_phone_3_fields(theForm.day_phone_ac, theForm.day_phone_pre, theForm.day_phone_num, "Day Phone");
	req_phone_3_fields(theForm.eve_phone_ac, theForm.eve_phone_pre, theForm.eve_phone_num, "Evening Phone");
	req_regexp(theForm.email_addr, ".+@.+\\..+", "Email Address");
    }
    if(birth) {
	req_number_length(theForm.year, 4, "Birth Year");
    }
    if(weight) {
	req_number(theForm.weight, "Weight");
    }
}

// Form Validator scripts

//Validate Annuity Forms
function Check_Annuity(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    All_Common(theForm, true, false);
    req_number(theForm.amt_to_invest, "Amount to Invest");
    req_number(theForm.plan_to_add, "Lump Sum Payment");
    return finish_Validation(theForm);
}// End Annuity Validator

//Validate Auto Forms
function Check_Auto(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    All_Common(theForm, true, false);
    req_text(theForm.drivers, "Drivers License");
    req_number(theForm.paying_now, "How much are you paying now?");
    req_number(theForm.number_drivers, "Number of Drivers in Your Household?");
    req_number(theForm.number_vehicles, "Number of Vehicles in Your Household?");
    req_number(theForm.years_had, "How Many Years Have You Had Your Driver's License?");
    req_number_length(theForm.year_made, 4, "What Year Was Your Car Manufactured? (YYYY)");
    req_text(theForm.car_make, "What Is The Make Of Your Car?");
    req_text(theForm.car_model, "What Is The Model Of Your Car?");
    return finish_Validation(theForm);
}//End Auto Validator

//Validate DebtConsol Forms
function Check_DebtConsol(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;

    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    if(contact) {
	req_text(theForm.FirstName, "First Name");
	req_text(theForm.LastName, "Last Name");
	req_text(theForm.Address, "Street Address");
	req_text(theForm.City, "City");
	req_combo(theForm.State, 0, "State");
	req_number_length(theForm.Zip, 5, "Zip Code");
	req_phone_3_fields(theForm.DayPhoneArea, theForm.DayPhonePre, theForm.DayPhoneNum, "Day Phone");
	req_phone_3_fields(theForm.EveningPhoneArea, theForm.EveningPhonePre, theForm.EveningPhoneNum, "Evening Phone");
	req_regexp(theForm.email, ".+@.+\..+", "Email Address");
    }

    //alert("test");

    req_combo(theForm.debtamt, 0, "Amount of unsecured debt");
    req_combo(theForm.annualincome, 0, "Your approximate annual income");
    return finish_Validation(theForm);
}//End DebtConsol Validator

//Validate Disability Form
function Check_Disability(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    All_Common(theForm, true, true);
    req_text_w_combo(theForm.self, 1, theForm.employer, "If 'No', who is your employer?");
    req_text(theForm.business_type, "What type of business are you employed with?");
    req_text(theForm.position, "What is your position?");
    req_text(theForm.occupation, "Occupation");
    req_number(theForm.gross, "Present Monthly Gross Income");
    req_number(theForm.benefit, "Monthly Benefit Requested");
    return finish_Validation(theForm);
} //End Disability Validator

//Validate GroupHealth Form
function Check_GroupHealth(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    All_Common(theForm, false, false);
    //Employee 1
    req_text(theForm.ename1, "Employee 1's Name");
    req_text(theForm.occu1, "Employee 1's Occupation");
    req_number(theForm.age1, "Employee 1's Age");
    req_number(theForm.salary1, "Employee 1's Salary");
    //Employee 2
    if(theForm.ename2.value.length != 0 || theForm.age2.value.length !=0 || theForm.occu2.value.length !=0 || theForm.salary2.value.length !=0) {
	req_text(theForm.ename2, "Employee 2's Name");
	req_text(theForm.occu2, "Employee 2's Occupation");
	req_number(theForm.age2, "Employee 2's Age");
	req_number(theForm.salary2, "Employee 2's Salary");
    }
    //Employee 3
    if(theForm.ename3.value.length != 0 || theForm.age3.value.length !=0 || theForm.occu3.value.length !=0 || theForm.salary3.value.length !=0) {
	req_text(theForm.ename3, "Employee 3's Name");
	req_text(theForm.occu3, "Employee 3's Occupation");
	req_number(theForm.age3, "Employee 3's Age");
	req_number(theForm.salary3, "Employee 3's Salary");
    }
    //Employee 4
    if(theForm.ename4.value.length != 0 || theForm.age4.value.length !=0 || theForm.occu4.value.length !=0 || theForm.salary4.value.length !=0) {
	req_text(theForm.ename4, "Employee 4's Name");
	req_text(theForm.occu4, "Employee 4's Occupation");
	req_number(theForm.age4, "Employee 4's Age");
	req_number(theForm.salary4, "Employee 4's Salary");
    }
    //Employee 5
    if(theForm.ename5.value.length != 0 || theForm.age5.value.length !=0 || theForm.occu5.value.length !=0 || theForm.salary5.value.length !=0) {
	req_text(theForm.ename5, "Employee 5's Name");
	req_text(theForm.occu5, "Employee 5's Occupation");
	req_number(theForm.age5, "Employee 5's Age");
	req_number(theForm.salary5, "Employee 5's Salary");
    }
    return finish_Validation(theForm);
}//End GroupHealth Validator

//Validate Health Form
function Check_Health(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    All_Common(theForm, true, true);
    req_number(theForm.dependents, "How many dependents do you have?");
    if(theForm.dependents.value != 0) {
	req_text(theForm.old_dependents, "How old are those dependents?");
	//req_regexp(theForm.old_dependents, "^\d+(\s*,\s*\d+)*$", "How old are those dependents?");
    }
    req_text(theForm.occupation, "Occupation");
    req_text(theForm.current_company, "Your current insurance company?");
    req_number(theForm.pay_month, "How much are you paying per month?");
    return finish_Validation(theForm);
}//End Health Validation

//Validate HomeOwners Form
function Check_HomeOwners(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    All_Common(theForm, false, false);
    req_number(theForm.purchase, "Purchase Price (or) Replacement cost of your home");
    req_number_length(theForm.year_built, 4, "Year Built");
    req_number(theForm.square_feet, "Square Feet");
    req_number(theForm.fireplaces, "Number of Fireplaces");
    req_number(theForm.bedrooms, "Number of Bedrooms");
    req_number(theForm.bathrooms, "Number of Bathrooms");
    return finish_Validation(theForm);
}//End HomeOwners Validation

//Validate Life Form
function Check_Life(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    All_Common(theForm, true, true);
    req_number(theForm.paying_per_year, "Amount Currently Paying Per Year");
    return finish_Validation(theForm);
}//End Life Validation

//Validate LTC Form
function Check_LTC(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    All_Common(theForm, true, true);
    return finish_Validation(theForm);
}//End LTC Validation

//DebtConsolidation
function Check_DebtConsolidation(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    return mortgage_common(theForm, false);
}
//HomeImprovement
function Check_HomeImprovement(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    return mortgage_common(theForm, false);
}
//NewLoan
function Check_NewLoan(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    return mortgage_common(theForm, true);
}
//Refinance
function Check_Refinance(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    return mortgage_common(theForm, false);
}
//Second
function Check_Second(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;
    return mortgage_common(theForm, false);
}
// generalMortgage
// Example (http://www.florida-home-mortgage-loan-rates-company.com/mortgage_quote.htm)
// Same as regular types, but there is a Combo Box
// for selecting the type of loan desired and some hidden fields.
// The check for that Combo Box was integrated into the
// general Mortgage function using the generalMortgage value as the switch.
function Check_generalMortgage(theForm) {
    contact = true;
    generalMortgage = true;
    return mortgage_common(theForm, false);
}

// 3 Part Mortgage Forms, PT2
function Check_MortPT2(theForm, newHome) {

    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    req_number(theForm.monthly_payment, "Your current mortgage monthly payment?");
    req_number(theForm.current_rate, "Current mortgage interest rate");
    req_number(theForm.cash_needed, "What is the total loan amount desired?");
    req_number(theForm.current_worth, "How much is your home currently worth?");
    req_number(theForm.home_sale_price, "Your home's original sale price?");
    req_number(theForm.how_long_owned, "How long have you owned your home?");
    req_number(theForm.mortgage_balance, "Your current mortgage balance?");
    req_number_w_combo(theForm.second_mortgage, 1, theForm.second_balance, "2nd mortgage balance?");
    req_number_w_combo(theForm.second_mortgage, 1, theForm.second_payment, "2nd mortgage monthly payment?");
    req_number_w_combo(theForm.second_mortgage, 1, theForm.second_rate, "2nd mortgage interest rate?");
    if(newHome) {
	req_number(theForm.money_down, "How much money do you want to put down on your new home?");
	req_number(theForm.plan_to_pay, "What are you planning to pay for your new home?");
    }

    return finish_Validation(theForm);
}
// 3 Part Mortgage Forms, PT3
function Check_MortPT3(theForm) {
    if(arguments.length > 1) contact = arguments[1];
    else contact = true;

    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    All_Common(theForm, false, false);

    req_number_w_combo(theForm.self_employed, 0, theForm.years_employed, "How long have you been with your employer?");
    req_number(theForm.monthly_gross, "How much do you gross each month?");
    req_number(theForm.monthly_debt, "How much do you owe each month?");
    
    return finish_Validation(theForm);
}


// 2 Part Mortgage Forms, PT1
function Check_2PtMortgage_Pt1(theForm, newHome) {
    if(arguments.length > 2) generalMortgage = arguments[2];
    else generalMortgage = false;

    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    if(generalMortgage) {
	req_combo(theForm.prod_type, 0, "Type of loan you're interested in?") ;
    }
    req_number(theForm.current_worth, "How much is your home currently worth?");
    req_number(theForm.cash_needed, "What is the total loan amount desired?");
    req_number(theForm.mortgage_balance, "Your home's original sale price?");
    req_number(theForm.current_rate, "Current mortgage interest rate");
    opt_number(theForm.second_balance, "2nd mortgage balance? (must be a number)");
    opt_number(theForm.second_rate, "2nd mortgage rate? (must be a number)");

    if(newHome) {
	req_number(theForm.money_down, "How much money do you want to put down on your new home?");
	req_number(theForm.plan_to_pay, "What are you planning to pay for your new home?");
    }

    return finish_Validation(theForm);
}


// 2 Part Mortgage Forms, PT2
function Check_2PtMortgage_Pt2(theForm) {

    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    req_text(theForm.FirstName, "First Name");
    req_text(theForm.LastName, "Last Name");
    req_text(theForm.Address, "Street Address");
    req_text(theForm.City, "City");
    req_number_length(theForm.Zip, 5, "Zip Code");
    req_combo(theForm.State, 0, "State");
    req_phone_3_fields(theForm.DayPhoneArea, theForm.DayPhonePre, theForm.DayPhoneNum, "Day Phone");
    req_regexp(theForm.emailAddr, ".+@.+\..+", "Email Address");

    return finish_Validation(theForm);
}

//Check email friend Page
function Check_EmailFriend(theForm) {
    errorFree = true;
    theError = "You have entered in the following information improperly:\n";
    req_text(theForm.emailto, "Your friend's email address");
    req_text(theForm.emailsubject, "Subject");
    req_text(theForm.emailfrom, "Your email address");
    req_text(theForm.emailmessage, "Type in a message");

    return finish_Validation(theForm);
}

