// Lead Exchange Scripts
// Created Origionally By Joshua Bauer (joshdba@yahoo.com)
// 2000

// Generic Functions And Global Variables

var errorFree;
var theError;
var submitted = false;


function comboNav(theForm, comboBox, badVal, ref) {

    if(val != badVal) {
	document.top.location = ref+val;
    }
}


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();
	}
    }

    theError += msg;
    errorFree = false;
    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 gotoPage(theForm, comboBox, pageLocation, badVal) {
    
    comboValue = eval("theForm." + comboBox + "[theForm." + comboBox + ".selectedIndex].value");

    if(comboValue != badVal) {
	self.top.location = pageLocation + comboValue;
    }
}


//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) {
	theError += "\n\t-" + msg;
	if(errorFree) {
	    field.focus();
	}
	errorFree = false;
    }
}


function req_phone_3_fields(ac, pre, num, msg) {

    if(ac.value.length == 0 || pre.value.length == 0 || num.value.length == 0 || isNaN(ac.value) || isNaN(pre.value) || isNaN(num.value)) {
	theError += "\n\t-" + msg;
	errorFree = false;
    }
}


function req_number(field, msg) {

    strip_commas(field);
    if(field.value.length == 0 || isNaN(field.value)) {
	theError += "\n\t-" + msg;
	if(errorFree) {
	    field.focus();
	}
	errorFree = false;
    }
}


function req_number_length(field, length, msg) {

    strip_commas(field);
    if(field.value.length != length || isNaN(field.value)) {
	theError += "\n\t-" + msg;
	if(errorFree) {
	    field.focus();
	}
	errorFree = false;
    }
}


//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) {
	theError += "\n\t-" + msg;
	if(errorFree) {
	    field.focus();
	}
	errorFree = false;
    }
}


//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))) {
	theError += "\n\t-" + msg;
	if(errorFree) {
	    field.focus();
	}
	errorFree = false;
    }
}

//URL   "http://.+\..+\..+"
//Email ".+@.+\..+"
function req_regexp(field, exp_text, msg) {

    req_regexpRE = new RegExp(exp_text);
    if(!req_regexpRE.test(field.value)) {
	theError += "\n\t-" + msg;
	if(errorFree) {
	    field.focus();
	}
	errorFree = false;
    }
}


function finish_Validation (theForm) {

   if(!errorFree)
	{
	    alert(theError);
	    return false;
	}
    else if(!submitted)
	{
	    submitted = true;
	    //theForm.submit();
	    return true;
	}
}


// Form Validators

function validate_newmember_form(theForm) {

    errorFree = true;
    theError = "You have entered in the following information improperly:\n";

    checkbox_validator(theForm, "licence", "\n\t-States Licenced in");
    checkbox_validator(theForm, "type", "\n\t-Types of leads");

    req_text(theForm.firstname, "First Name");
    req_text(theForm.lastname, "Last Name");
    req_text(theForm.coname, "Company Name");
    req_text(theForm.address, "Street Address");
    req_text(theForm.city, "City");
    req_number_length(theForm.zip, 5, "Zip Code");
    req_phone_3_fields(theForm.phone_ac, theForm.phone_pre, theForm.phone_num, "Phone");
    req_regexp(theForm.email, ".+@.+\..+", "Email Address");
    req_text(theForm.username, "Username");

    if(theForm.password1.value.length == 0 || theForm.password2.value.length == 0) {
	theError += "\n\t-Password";
	if (errorFree) {
	    theForm.password1.focus();
	}
	errorFree = false;
    }
    
    if(theForm.website[0].checked) {
	req_regexp(theForm.url, "http://.+\..+\..+", "Your URL");
    }

    if(!errorFree) {
	alert(theError);
	return false;
    }
    else if (theForm.password1.value != theForm.password2.value) {
	alert("Passwords don't match!");
	theForm.password1.value = "";
	theForm.password2.value = "";
	theForm.password1.focus();
	return false;
    }
    else {
    	//theForm.submit();
    }
    return true;
}

var urls;
function validate_step2(theForm, area, URL) {

    errorFree = true;
    theError = "You have entered in the following information improperly:\n";
    
    if(area) {
        checkbox_validator(theForm, 'areaCodes', '\t- Please choose and Area Code\n');
    }

//      if(URL) {
//  	first = true;
//  	for (web in urls) {
//  	    if(first) {
//  		first = URL_validator(theForm, web, '\t-Customer forms.\n');
//  	    }
//  	    else {
//  		URL_validator(theForm, web, '');
//  	    }
//  	}
//      }

    if(errorFree) {
	return true;
    }
    else {
	alert(theError);
	return false;
    }
}


function validate_othername(theForm) {
    return true;
}


function validate_modify_form(theForm) {
    
    var errorFree = true;
    var theError = "You have entered in the following information improperly:\n";
    if(theForm.firstname.value.length == 0) {
	theError += "\n\t-First Name";
	if (errorFree) {
	    theForm.firstname.focus();
	}
	errorFree = false;
    }
    
    if(theForm.lastname.value.length == 0) {
	theError += "\n\t-Last Name";
	if (errorFree) {
	    theForm.lastname.focus();
	}
	errorFree = false;
    }
    
    if(theForm.coname.value.length == 0) {
	theError += "\n\t-Company Name";
	if (errorFree) {
	    theForm.coname.focus();
	}
	errorFree = false;
    }
    
    if(theForm.address.value.length == 0) {
	theError += "\n\t-Street Address";
	if (errorFree) {
	    theForm.address.focus();
	}
	errorFree = false;
    }
    
    if(theForm.city.value.length == 0) {
	theError += "\n\t-City";
	if (errorFree) {
	    theForm.city.focus();
	}
	errorFree = false;
    }
    
    if(theForm.zip.value.length !=5 || isNaN(theForm.zip.value)) {
	theError += "\n\t-Zip Code";
	if (errorFree) {
	    theForm.zip.focus();
	}
	errorFree = false;
    }

          if(theForm.phone_ac.value.length != 3 || theForm.phone_pre.value.length != 3 || theForm.phone_num.value.length != 4 || isNaN(theForm.phone_ac.value) || isNaN(theForm.phone_pre.value) || isNaN(theForm.phone_num.value))
      	{
      	    theError += "\n\t-Phone";
      	    errorFree = false;
      	}
    
          if(theForm.fax_ac.value.length != 3 || theForm.fax_pre.value.length != 3 || theForm.fax_num.value.length != 4 || isNaN(theForm.fax_ac.value) || isNaN(theForm.fax_pre.value) || isNaN(theForm.fax_num.value))
      	{
      	    theError += "\n\t-Fax";
      	    errorFree = false;
      	}
    
    myRE = new RegExp(".+@.+\..+");
    if(!myRE.test(theForm.email.value)) {
	theError += "\n\t-Email Address";
	if (errorFree) {
	    theForm.email.focus();
	}
	errorFree = false;
    }
    
    myRE = new RegExp("http://.+\..+\..+");
    if(theForm.website[0].checked && !myRE.test(theForm.url.value)) {
	theError += "\n\t-Your URL";
	if (errorFree) {
	    theForm.url.focus();
	}
	errorFree = false;
    }

    if(!errorFree) {
	alert(theError);
    }
    else {
    	theForm.submit();
    }
}


function Login_Popup() {
    Login = window.open('/html/member.html', 'Login', 'toolbar=no,width=310,height=177,status=no,scrollbars=no,resizable=no,screenX=10,screenY=10');
    Login.focus();
}


function Complete_Login() {
    self.opener.top.location="/html/My_Account.cfm";
    self.close();       
}


function Admin_Login() {
    self.opener.top.location="/html/admin.cfm";
    self.close();       
}

var Join;
function join_popup() {
    Join = window.open('/html/createaccount.html', 'Join', 'toolbar=no,width=472,height=500,status=no,scrollbars=yes,resizable=no,screenX=10,screenY=10');
    Join.focus();
}


function special_join_popup() {
    //  Join = window.open('/html/createaccount.html', 'Join', 'toolbar=no,width=472,height=500,status=no,scrollbars=yes,resizable=no,screenX=10,screenY=10');
//      Join.focus();
//      Join.opener = self.opener;
    self.opener.location = "../html/Sign_up_now.html";
    self.close();
}


function start_join() {
    self.opener.top.location="/html/newmember.cfm";
    self.close();
}


function Thanks_Popup() {
    Thanks = window.open('/html/thank_you.cfm', 'Thanks', 'toolbar=no,width=360,height=367,status=no,scrollbars=no,resizable=no');
    Thanks.focus();
}


function Error_Popup() {
    Error = window.open('/html/error.html', 'Error', 'toolbar=no,width=200,height=178,status=no,scrollbars=no,resizable=no');
    Error.focus();
}


function mail_popup() {

    Thanks = window.open('/html/Mailing_List.html', 'Mail', 'toolbar=no,width=376,height=500,status=no,scrollbars=yes,resizable=no');
    Thanks.focus();
}


function TR_popup() {

    TR = window.open('/html/TRPreview.cfm', 'TRF', 'toolbar=no,width=626,height=500,status=no,scrollbars=yes,resizable=no');
    TR.focus();
}


function generic_popup(page) {

    if(arguments.length > 1) {
	width = arguments[1];
	height = arguments[2];
    }
    else {
	width = 360;
	height = 166;
    }

    Thanks = window.open(page, 'Generic', 'toolbar=no,width=' + width + ',height=' + height + ',status=no,scrollbars=no,resizable=no');
    Thanks.focus();
    document.location = '/scripts/My_Account.cfm';
}


function validate_nametaken_form(theForm) {
    if(theForm.password1.value != theForm.password2.value)
	{
	    alert("Both Passwords Must Match");
	    theForm.password1.value = "";
	    theForm.password2.value = "";
	    theForm.password1.focus();
	    return false;
	}
    else
	{
	    return true;
	}
}


function validate_oneChecked(theArray) {

    for(i=0; i < theArray.length; i++) {
	if(theArray[i].checked) {
	    return true;
	}
    }
    return false;
}

function Admin_States(form, option) {
    if (option == "all" && form.all.checked) {
	form.none.checked = false;
	check_All(form, 'licence');
    }
    else if(option == "none" && form.none.checked) {
	form.all.checked = false;
	uncheck_All(form, 'licence');
    }
}


function checked_leads(form)
{
    

    if(isNaN(form.elements["ID"].length)) {
	if(form.elements["ID"].checked) {
	    return true;
	}
    }
    else {
	for(var i = 0; i < form.elements["ID"].length; i++) {
	    if(form.elements["ID"][i].checked) {
		return true;
	    }
	}
    }
    alert('Please Choose a Lead');
    return false;
}

