// Generic Functions And Global Variables
var errorFree;
var theError;
var submitted = false;
var contact;
var mybgColor =  'C0C0C0';

//Removes Commas from numeric fields
function strip_commas(field) {
    re = /[,$]/gi;
    str = field.value;
    field.value = str.replace(re, "");
}


function req_text(field, msg) {
    if(field.value.length == 0) {
	addMsg(field, msg);
    }
}

function req_number_length(field, length, msg) {
    strip_commas(field);
    if(field.value.length != length || isNaN(field.value)) {
	addMsg(field, msg);
    }
}

function req_number(field, msg) {
    strip_commas(field);
    if(field.value.length == 0 || isNaN(field.value)) {
	addMsg(field, msg);
    }
}


//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_regexp_w_combo(combo, index, field, exp_text, msg) {
    if(combo.options[index].selected) {
	req_regexp(field, exp_text, 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 checkbox_validator(form, box, msg) {
    if(isNaN(form.elements[box].length)) {
	if(form.elements[box].checked) {
	    return true;
	}
	addMsg(form.elements[box], msg);
    }
    else {
	for(var i = 0; i < form.elements[box].length; i++) {
	    if(form.elements[box][i].checked) {
		return true;
	    }
	}
	addMsg(form.elements[box][0], msg);
    }
    return false;
}


function check_All(form, box) {
    
    if(isNaN(form.elements[box].length)) {
	form.elements[box].checked = true;
    }
    else {
	for(var i = 0; i < form.elements[box].length; i++) {
	    form.elements[box][i].checked = true;
	}
    }
}


function uncheck_All(form, box) {
    
    if(isNaN(form.elements[box].length)) {
	form.elements[box].checked = false;
    }
    else {
	for(var i = 0; i < form.elements[box].length; i++) {
	    form.elements[box][i].checked = false;
	}
    }
}

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;
}
// Form Validator scripts
//insurance and mortgage validators

// InfoCheck
function InfoCheck(theForm) {
    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    req_text(theForm.name, "Name");
    req_text(theForm.agency, "Agency 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_text(theForm.phone, "Phone Number");
    req_combo(theForm.leads, 0, "Delivery Type");
    req_regexp_w_combo(theForm.leads, 2, theForm.email, ".+@.+\..+", "Email Address");
    //checkbox_validator(theForm, "type", "Type of Leads");

    return finish_Validation(theForm);
}// End InfoCheck


function PricingCheck(theForm) {
    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    req_number(theForm.qty, "Please enter a quantity of leads using only numeric characters.");

    return finish_Validation(theForm);
}


function CartCheck(theForm) {
    if(checkbox_validator(theForm, "Remove", "")) {
	return confirm('Are you sure you want to remove the selected items?');
    }
    else {
	alert("If you wish to remove a lead order please select the appropriate checkbox and submit the form again.");
	return false;
    }
}

function RollStates(theForm) {
    var i;
    theForm.stateRoll.value = "";
    alert(theForm.states.length);
    for(i = 0; i < theForm.states.length; i++) {
	if(theForm.states[i].checked) {
	    theForm.stateRoll.value = theForm.states[i].value;
	    alert(theForm.states[i].value);
	}
    }
    alert(i);
}

