// 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

function validateContactInfo(theForm){
	errorFree = true;
    theError = "You have entered in the following information improperly:\n";


	req_text(theForm.name, "Name");
	req_text(theForm.company, "Company");
	req_regexp(theForm.email, ".+@.+\\..+", "Email Address");
   	req_phone_3_fields(theForm.phoneAC, theForm.phonePRE, theForm.phoneNUM, "Phone");
   req_combo(theForm.profession, 0, "Profession");

	
    	
	 return finish_Validation(theForm);
}


