// Generic Functions And Global Variables

var errorFree = true;
var theError = "You have entered in the following information improperly:\n";
var submitted = false;

//  List of generic functions
/*
    strip_commas(form.field);
    HeightInInches(form.feet, form.inches, form.height);
    req_text(form.field, "");
    req_phone_3_fields(form.ac, pre, num, "");
    req_number(form.field, "");
    opt_number(form.field, "", zeroed);
    req_number_length(form.field, length, "");
    req_one_number(form.field1, form.field2, "");
    opt_number_length(form.field, length, "");
    checkbox_validator(form, box, "");
    req_combo(form.combo, index, "");
    req_text_w_combo(form.combo, index, field, "");
    req_number_w_combo(form.combo, index, field, "");
    req_regexp(form.field, exp_text, "");
    finish_Validation (form.theForm);
*/


//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_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.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)");
    }
}


function checkbox_validator(form, box, msg)
{
    if(isNaN(form.elements[box].length)) {
	if(form.elements[box].checked) {
	    return true;
	}
	if(errorFree) {
	    form.elements[box].focus();
	}
    }
    else {
	for(var i = 0; i < form.elements[box].length; i++) {
	    if(form.elements[box][i].checked) {
		return true;
	    }
	}
	if(errorFree) {
	    form.elements[box][0].focus();
	}
    }

    addMsg(form.elements[box][0], msg);
    return false;
}


//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);
    }
}

//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) {
    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;
	    //theForm.submit();
	    return true;
	}
   return false;
}

// Form Validator script
function ChkInsInfoReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";

    req_text(form.name, "Your Name");
    req_text(form.phone, "Phone");
    req_text(form.email, "E-mail");
    checkbox_validator(form, "type", "Products you carry.");

    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkMortInfoReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";

    req_text(form.name, "Your Name");
    req_text(form.phone, "Phone");
    req_text(form.email, "E-mail");
    checkbox_validator(form, "loan", "Products you carry.");

    return finish_Validation(form);
    //return false;
}

function BuyLeadsContactFormValidator(TheForm) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";

    req_text(TheForm.Name, "Your Name");
    req_combo(TheForm.State, 0, "State");
    opt_number_length(TheForm.Zip, 5, "Zip Code");
    req_phone_3_fields(TheForm.PhoneAreaCode, TheForm.PhonePrefix, TheForm.PhoneNumber, "Phone");
    req_regexp(TheForm.Email, ".+@.+\\..+", "E-mail Address");
    checkbox_validator(TheForm, "LeadTypes", "Type of leads you are interested in selling.");
    req_number(TheForm.DailyLeadQty, "Approximate quantity available per day");
    return finish_Validation(TheForm);

}
