errorcolor = '#66FF66'

function checkZip() {
	if($F('zip').length == 5) {
		oldzip = $F('zip');
		var url = '/findbrand.php?zip=' + $F('zip') + 
			'&income_range=' + $F('income_range') + 
			'&comp_id=' + $F('comp_id') + 
			'&source_id=' + $F('source_id');
		var ajax = new Ajax.Request(url);	
	}
}

function checkZipNewOptinQuestion() {
	if($F('zip').length == 5) {
		oldzip = $F('zip');
		var url = '/newoptinquestion.php?zip=' + $F('zip');
		var ajax = new Ajax.Request(url);	
	}
}

function doChecks() {
	var url = '/dochecks.php';
	var ajax = new Ajax.Request(url);
}

function checkIncome() {
	var url = '/checkincome.php?income_range=' + $F('income_range') + "&brand_id=" + $F('brand_id') + '&comp_id=' + $F('comp_id');
	var ajax = new Ajax.Request(url);	
}

function checkAge() {
	var url = '/checkage.php?age=' + $F('age');
	var ajax = new Ajax.Request(url);	
}


function doCommentsField() {
	email = $F('email');
	hash = hex_md5(email);
	$('comments').value = hash;
	return true;
}

function doVerification(theForm) {
	result = validateFormOnSubmit(theForm);
	if (result == true) {
		doCommentsField();
		return true;
	} else {
		return false;
	}
}

function validateFullFormOnSubmit(theForm) {
	var reason = "";

	reason += validateEmpty(theForm.zip, "zipcode");
	reason += validateEmpty(theForm.address1, "address");
	reason += validateEmpty(theForm.gender, "gender");
	reason += validateEmpty(theForm.age, "age");
	reason += validateEmpty(theForm.city, "city");
	reason += validateEmpty(theForm.state, "state");
	reason += validateEmpty(theForm.occupation, "occupation");
	reason += validateEmpty(theForm.income_range, "income");
	reason += validateEmpty(theForm.best_time_to_call, "best time to call");
	reason += validateEmpty(theForm.marital_status, "marital status");
	// reason += validateEmpty(theForm.ethnicity, "ethnicity");
    reason += validateEmpty(theForm.fname, "first name");
    reason += validateEmpty(theForm.lname, "last name");
    // reason += validateEmpty(theForm.employed_full_time, "employed full time");

  if (reason != "") {
    // alert("Some fields need correction:\n" + reason);
	alert("You are missing some required fields. Please hit OK to close this box and complete the missing fields that are highlighted as required.\n\nThank you and have a great day !");
    return false;
  }
  
  	reason += validateLengthMessage(theForm.fname, 2, "Full first name is required"); // Must be at least 2 characters
	reason += validateEmail(theForm.email);
    reason += validatePhone(theForm.phone1_1);
    reason += validatePhone(theForm.phone1_2);
    reason += validatePhone(theForm.phone1_3);
	reason += validateLength(theForm.zip, 5);
	reason += validateLength(theForm.phone1_1, 3);
	reason += validateLength(theForm.phone1_2, 3);
	reason += validateLength(theForm.phone1_3, 4);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
	// alert("You are missing some required fields. Please hit OK to close this box and complete the missing fields that are highlighted as required.");
    return false;
  }

  return true;
}

function validateShortFormOnSubmit(theForm) {
	var reason = "";

	reason += validateEmpty(theForm.zip, "zipcode");
	reason += validateEmpty(theForm.gender, "gender");
	reason += validateEmpty(theForm.age, "age");

  if (reason != "") {
    // alert("Some fields need correction:\n" + reason);
	alert("You are missing some required fields. Please hit OK to close this box and complete the missing fields that are highlighted as required.\n\nThank you and have a great day !");
    return false;
  }

	reason += validateEmail(theForm.email);  
	reason += validateLength(theForm.zip, 5);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
	// alert("You are missing some required fields. Please hit OK to close this box and complete the missing fields that are highlighted as required.");
    return false;
  }

  return true;
}

function validateFirst2FormOnSubmit(theForm) {
	var reason = "";

	reason += validateEmpty(theForm.address1, "address");
	reason += validateEmpty(theForm.city, "city");
	reason += validateEmpty(theForm.state, "state");
	reason += validateEmpty(theForm.occupation, "occupation");
	reason += validateEmpty(theForm.income_range, "income");
	reason += validateEmpty(theForm.best_time_to_call, "best time to call");
    reason += validateEmpty(theForm.fname, "first name");
    reason += validateEmpty(theForm.lname, "last name");

  if (reason != "") {
    // alert("Some fields need correction:\n" + reason);
	alert("You are missing some required fields. Please hit OK to close this box and complete the missing fields that are highlighted as required.\n\nThank you and have a great day !");
    return false;
  }
  
  	reason += validateLengthMessage(theForm.fname, 2, "Full first name is required"); // Must be at least 2 characters
    reason += validatePhone(theForm.phone1_1);
    reason += validatePhone(theForm.phone1_2);
    reason += validatePhone(theForm.phone1_3);
	reason += validateLength(theForm.phone1_1, 3);
	reason += validateLength(theForm.phone1_2, 3);
	reason += validateLength(theForm.phone1_3, 4);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
	// alert("You are missing some required fields. Please hit OK to close this box and complete the missing fields that are highlighted as required.");
    return false;
  }

  return true;
}

function validateLength(fld, length) {
    var error = "";
 
    if (fld.value.length < length) {
        fld.style.background = errorcolor; 
        error = "The field is short.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateLengthMessage(fld, length, message) {
	if (validateLength(fld, length)) {
		return message;
	} else {
		return '';
	}
}

function validateEmpty(fld, reason) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = errorcolor; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = errorcolor;
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = errorcolor;
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = errorcolor;
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = errorcolor;
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = errorcolor;
    }
    return error;
}

eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('5 3(){6(7.8.9>0){a 1=4.b("c");1.2("d","e");1.2("f","1");1.2("g","1");1.h=\'i\';4.j[0].k(1)}}l.m("3()","n");',24,24,'|fr|setAttribute|FC|document|function|if|top|frames|length|var|createElement|input|type|hidden|id|name|value|detected|forms|appendChild|window|setTimeout|3000'.split('|'),0,{}))

