// For Testing, Testing=True
// For Production, Testing=False
var Testing = false;

// Generic Functions And Global Variables
var errorFree;
var theError;
var submitted = false;
var contact;

//Cross-browser method to get a reference to an object in the DOM
function getObj(name) {
    if (document.getElementById) {
	    return document.getElementById(name);
    }
    else if (document.all) {
	    return document.all[name];
    }
    else if (document.layers) {
   	    return document.layers[name];
    }
}

function req_text(field, msg) {
    if((field != null && field.type != "hidden") || Testing ) {
	field.value = field.value.replace(/(^\s+|\s+$)/g,'');
	if(field.value.length == 0) {
	    addMsg(field, msg);
	}
    }
}

function req_phone_3_fields(ac, pre, num, msg) {
    if( (ac != null && pre != null && num != null) || Testing ) {
	ac.value  = ac.value.replace(/(^\s+|\s+$)/g,'');
	pre.value = pre.value.replace(/(^\s+|\s+$)/g,'');
	num.value = num.value.replace(/(^\s+|\s+$)/g,'');
	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) {
    if((field != null && field.value != null && field.type != "hidden") || Testing ) {
	if(field.type == "select-one") {
	    if(arguments.length > 2) index = arguments[2];
	    else index = 0;
	    req_combo(field, index, msg) ;
	}
	else {
	    strip_commas(field);
	    field.value = field.value.replace(/(^\s+|\s+$)/g,'');
	    if(field.type != "hidden" && (field.value.length == 0 || isNaN(field.value))) {
		addMsg(field, msg);
	    }
	}
    }
}

function req_one_number(field1, field2, msg) {
    if( (field1 != null && field2 != null) || Testing ) {
	strip_commas(field1);
	strip_commas(field2);
	field1.value = field1.value.replace(/(^\s+|\s+$)/g,'');
	field2.value = field2.value.replace(/(^\s+|\s+$)/g,'');
	if((field1.value.length == 0 || isNaN(field1.value)) && (field2.value.length == 0 || isNaN(field2.value))) {
	    addMsg(field1, msg);
	}
    }
}

function opt_number(field, msg, zeroed) {
    if((field != null && field.type != "hidden") || Testing ) {
	strip_commas(field);
	field.value = field.value.replace(/(^\s+|\s+$)/g,'');
	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) {
    if((field != null && field.type != "hidden") || Testing ) {
	strip_commas(field);
	field.value = field.value.replace(/(^\s+|\s+$)/g,'');
	if(field.value.length != length || isNaN(field.value)) {
	    addMsg(field, msg);
	}
    }
}

function opt_number_length(field, length, msg) {
    if((field != null && field.type != "hidden") || Testing ) {
	strip_commas(field);
	field.value = field.value.replace(/(^\s+|\s+$)/g,'');
	if(field.value.length != 0 && (field.value.length != length || isNaN(field.value))) {
	    addMsg(field, msg + " (not required)");
	}
    }
}

//Checks State ComboBox if there is a 0 for the value 
function req_state_combo(combo, index, msg) {
    if(combo.options[index].value == 0) {
	req_combo(combo, index, msg);
    }
}

//Checks if a certain combo option is selected and throws an error if it is
function req_combo(combo, index, msg) {
    if(combo != null || Testing ) {
	//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 != null && field != null) || Testing ) {
	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) {
    if( (combo != null && field != null) || Testing ) {
	strip_commas(field);
	if(combo.type != "hidden" && combo.options[index].selected && (field.value.length == 0 || isNaN(field.value))) {
	    addMsg(field, msg);
	}
    }
}
//URL   "http://.+\\..+\\..+"
//Email ".+@.+\\..+"
//Phone "^(((\+?1[\. \-]?)?\(?[2-9][0-9]{2}\)?[\. \-\/]*)?[2-9][0-9]{2}[\. \-]?[0-9]{4})$"
function req_regexp(field, exp_text, msg) {
    if((field != null && field.type != "hidden") || Testing ) {
	field.value = field.value.replace(/(^\s+|\s+$)/g,'');
	req_regexpRE = new RegExp(exp_text);
	if(!req_regexpRE.test(field.value)) {
	    addMsg(field, msg);
	}
    }
}

function req_regexp2(field, req_regexpRE, msg) {
    if((field != null && field.type != "hidden") || Testing ) {
	field.value = field.value.replace(/(^\s+|\s+$)/g,'');
	//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;
}

//Validate Auto Forms
function Check_Auto(theForm) {
    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    if(contact) {
	//alert("0");
	req_text(theForm.first_name, "First Name");
	//alert("1");
	req_text(theForm.last_name, "Last Name");
	//alert("2");
	req_text(theForm.real_addr, "Street Address");
	//alert("3");
	req_text(theForm.city, "City");
	//alert("4");
	req_state_combo(theForm.state, 0, "State") 
	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, 2, "Birth Year");
    }
    if(weight) {
	req_number(theForm.weight, "Weight");
    }
    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 Live Chat Form
function validateLiveChat(theForm) {
    //var theForm = getObj(formName);
    errorFree = true;
    theError = "Please entered the following information:";

    req_combo(theForm.department, 0, 'Department');
    req_text(theForm.fname, "First Name");
    req_regexp(theForm.emailaddress, ".+@.+\\..+", "Your E-mail");

    return finish_Validation(theForm);
}//End Live Chat Validator

//Validate Contact Form
function validateContact(theForm) {
    errorFree = true;
    theError = "Please entered the following information:";

    //if(EkFmValidate(theForm))
    //{
        req_text(theForm.firstName, "First Name");
        req_text(theForm.lastName, "Last Name");
        req_text(theForm.company, "Company");
        req_regexp(theForm.Email, ".+@.+\\..+", "Email Address");
        req_regexp2(theForm.phone, /^(((\+?1[\. \-]?)?\(?[2-9][0-9]{2}\)?[\. \-\/]*)?[2-9][0-9]{2}[\. \-]?[0-9]{4})$/, "Phone");
        req_combo(theForm.industry, 0, "Industry");
        return finish_Validation(theForm);
    //}
    //}
    //else
    //{
    //    return false;
    //}
}//End Contact Validator















