
/*  Global Variables  */
var errors = new Array();
var valid;
var states = new Array;
var i = -1;
states[++i] = "AL-ALABAMA";
states[++i] = "AK-ALASKA";
states[++i] = "AZ-ARIZONA";
states[++i] = "AR-ARKANSAS";
states[++i] = "CA-CALIFORNIA";
states[++i] = "CO-COLORADO";
states[++i] = "CT-CONNECTICUT";
states[++i] = "DE-DELAWARE";
states[++i] = "DC-DISTRICT OF COLUMBIA";
states[++i] = "FL-FLORIDA";
states[++i] = "GA-GEORGIA";
states[++i] = "HI-HAWAII";
states[++i] = "ID-IDAHO";
states[++i] = "IL-ILLINOIS";
states[++i] = "IN-INDIANA";
states[++i] = "IA-IOWA";
states[++i] = "KS-KANSAS";
states[++i] = "KY-KENTUCKY";
states[++i] = "LA-LOUISIANA";
states[++i] = "ME-MAINE";
states[++i] = "MD-MARYLAND";
states[++i] = "MA-MASSACHUSETTS";
states[++i] = "MI-MICHIGAN";
states[++i] = "MN-MINNESOTA";
states[++i] = "MS-MISSISSIPPI";
states[++i] = "MO-MISSOURI";
states[++i] = "MT-MONTANA";
states[++i] = "NE-NEBRASKA";
states[++i] = "NV-NEVADA";
states[++i] = "NH-NEW HAMPSHIRE";
states[++i] = "NJ-NEW JERSEY";
states[++i] = "NM-NEW MEXICO";
states[++i] = "NY-NEW YORK";
states[++i] = "NC-NORTH CAROLINA";
states[++i] = "ND-NORTH DAKOTA";
states[++i] = "OH-OHIO";
states[++i] = "OK-OKLAHOMA";
states[++i] = "OR-OREGON";
states[++i] = "PA-PENNSYLVANIA";
states[++i] = "RI-RHODE ISLAND";
states[++i] = "SC-SOUTH CAROLINA";
states[++i] = "SD-SOUTH DAKOTA";
states[++i] = "TN-TENNESSEE";
states[++i] = "TX-TEXAS";
states[++i] = "UT-UTAH";
states[++i] = "VT-VERMONT";
states[++i] = "VA-VIRGINIA";
states[++i] = "WA-WASHINGTON";
states[++i] = "WV-WEST VIRGINIA";
states[++i] = "WI-WISCONSIN";
states[++i] = "WY-WYOMING";
	states[++i] = "PR-PUERTO RICO";
	states[++i] = "VI-VIRGIN ISLANDS";
	states[++i] = "AA-MILITARY AMERICAS";
	states[++i] = "AE-MILITARY";
	states[++i] = "AP-MILITARY PACIFIC";


function validate_form (thisform) {
	valid = true;
	errors = new Array();
	with (thisform) {	
	
		if ( !Media.value ) {
			mark_error(Media, "Required");
		} else {
			no_error(Media);
		}
				
		validate_word(FirstName, 1, true);
		validate_word(LastName, 1, true);
		if ( FirstName.value == LastName.value ) {
			mark_error(FirstName, "Invalid");
			mark_error(LastName, "Invalid");
		}
		
		validate_email(Email, true);
		
		validate_address(Address, true);
		// validate_address(Address2, false);
		validate_word(City, 1, true);
		validate_state(State, true);
		validate_zip(Zip, "USA", true);
		
		validate_phone(Phone, true);
		// validate_phone(EvePhone, false);
		// validate_date_input(DOB, -1, true);
			
		/*/ Check form validity before exiting /*/
		if (!valid) {
		// if there is invalid input
			var errorMessage = document.getElementById("frm_message");
			errorMessage.className = "err";
			errorMessage.innerHTML = '<h2>Please fix the following errors</h2>\n<ul>\n';
			while (errors.length) {
			// while there are still errors to list,
				errorMessage.innerHTML += '<li class="err">' + errors.shift() + '</li>\n';
				// display the first error in the list (it will be removed from the list)
			}
			errorMessage.innerHTML += '</ul><br />\n';
			
			window.scrollBy(0,-2000);
			// scroll to top
			
	//		alert("Some of the data entered contains errors.\n" +
	//				"Please fix these errors and try again.");
			// pop up a message window to alert the user of errors.
		}
		else { // if form is valid and about to be submitted
			submit_btn.disabled = true;
		}
	return valid;
	} // close with()
} // close validate_form


function verify_lead(DOB, Zip, State, selfemployed) {
	/** age restricted to 23 to 63 **/
	var y = new Array
	var d = new Date
	d = d.getFullYear()
	y = DOB.value.split("/")
	if ( (d - y[2] > 63) || (d - y[2] < 23) ) {
		mark_error(DOB, "does not qualify. Must be 23 to 63 years of age to qualify");
		return;
	}
	
	/** offer not valid in some states **/
	switch (State.value) {
		case 'NY':  case 'NJ':  case 'VT':  case 'ND':
		case 'SD':  case 'MN':  case 'KY':  case 'HI':
			mark_error(State, "of " + State.value + " not eligible for coverage");
			return;
	}
	
	
	/** offer not valid in some zip codes **/
	var z = Zip.value.substr(0,3)
	switch (z) {
		case "150":  case "151":  case "152":  case "153":  case "154":
		case "155":  case "156":  case "157":  case "158":  case "159":
		case "162":  case "163":  case "164":  case "165":  case "166":
		case "181":  case "188":  case "202":  case "203":  case "204":
		case "205":  case "238":  case "246":  case "255":  case "256":
		case "258":  case "260":  case "261":  case "262":  case "264":
		case "266":  case "283":  case "289":  case "326":  case "367":
		case "369":  case "370":  case "373":  case "374":  case "375":
		case "378":  case "379":  case "382":  case "383":  case "384":
		case "385":  case "387":  case "389":  case "392":  case "393":
		case "396":  case "397":  case "398":  case "399":  case "432":
		case "433":  case "437":  case "438":  case "439":  case "444":
		case "445":  case "449":  case "450":  case "451":  case "452":
		case "453":  case "454":  case "455":  case "456":  case "457":
		case "459":  case "462":  case "463":  case "464":  case "466":
		case "470":  case "471":  case "472":  case "473":  case "474":
		case "476":  case "477":  case "478":  case "479":  case "482":
		case "485":  case "486":  case "487":  case "498":  case "499":
		case "503":  case "504":  case "507":  case "508":  case "511":
		case "515":  case "516":  case "520":  case "521":  case "522":
		case "523":  case "524":  case "525":  case "609":  case "623":
		case "629":  case "634":  case "635":  case "638":  case "654":
		case "655":  case "656":  case "657":  case "658":  case "679":
		case "681":  case "683":  case "690":  case "706":  case "713":
		case "714":  case "716":  case "718":  case "733":  case "734":
		case "737":  case "738":  case "739":  case "743":  case "744":
		case "745":  case "746":  case "747":  case "748":  case "753":
		case "754":  case "755":  case "756":  case "757":  case "758":
		case "759":  case "762":  case "763":  case "766":  case "767":
		case "768":  case "772":  case "776":  case "777":  case "779":
		case "835":  case "838":  case "856":  case "865":  case "873":
		case "877":  case "878":  case "879":  case "884":  case "893":
		case "898":
			mark_error(Zip, "code not eligible for coverage");
			return;
	}
	
	/** MA & WA residents must be self-employed **/
	if ( (State.value == 'MA' ||  State.value == 'WA') && selfemployed.value != 'true' ) {
			mark_error(State, " of " + State.value + " residents must be self-employed to qualify");
			return;
	}
} // end verify_lead


function validate_email(field, required) {
	/*/
	/*		Check the syntax of the email address entered
	/*		- local must be between 2 and 64 characters
	/*				[one character is allowed but not common]
	/*		- local contains only alphanumerics, +, -, _, and .
	/*				[more characters are allowed but not common]
	/*		- local can't start or end with .
	/*		- local can't containt consecuive .'s
	/*		- domain must be between 2 and 255 characters
	/*				[one character is allowed but not common]
	/*		- domain containts only alphanumerics, -, and .
	/*    - top level domain must exist 
	/*				[tld list accurate as of 2008/03/26]
	/*/
	
	var validemail = "^[a-z0-9+_-][a-z0-9+_.-]{0,62}[a-z0-9+_-]@" +
					"[a-z0-9][a-z0-9.-]{0,251}[a-z0-9][.]" +
					"(biz|com|edu|gov|info|int|mil|name|net|org|aero|asia|cat|coop|jobs|mobi|museum|pro|tel|travel|" +
					"ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|" +
					"bt|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|" +
					"es|et|eu|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|" +
					"hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|" +
					"ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|" +
					"ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|" +
					"sg|sh|si|sk|sl|sm|sn|sr|st|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tr|tt|tv|tw|tz|ua|ug|uk|us|" +
					"uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|za|zm|zw)$";
	
	validemail = new RegExp(validemail, "i");
	bademail = new RegExp("[.][.]");
	
	if ( !field.value && required ) {
		mark_error(field, "Required");
	} else if (field.value) {
		if ( !validemail.test(field.value) || bademail.test(field.value) ) {
			mark_error(field, "Invalid");
		} else {
			no_error(field);
		}
	} else {
		no_error(field);
	}
} // end validate_email

function validate_address(field, required) {
	/*/
	/*   This is a basic check to for an address to see if the
	/*   user enetered both a street and a house or box number
	/*   Paramaters are the field object and a boolean to signal 
	/*   if the field is required
	/*/
	
	var string = field.value;
	
	if ( !string && required ) {
		mark_error(field, "Required");
		valid = false;
	}
	else if ( string ) {
		var address = new RegExp("([0-9].*[ ].*[a-z]|[a-z].*[ ].*[0-9])", "i");
		var invalidChars = new RegExp("[^0-9a-z .#'-]", "i");
		
		if ( !address.test(string) || invalidChars.test(string) ) {
			mark_error(field, "Invalid");
			valid = false;
		}
		else {
			no_error(field);
		}
	}
	else { // field is blank and not required
		no_error(field);
	}
} // end validate_address

function validate_state(field, required) {
	if (field.value != '') {
		var input = field.value.toUpperCase();
		var i = -1;
		
		field.className = 'err';
		while (++i < states.length) {
			if ( (input == states[i].substring(0,2)) || (input == states[i].substring(3)) ) {
				field.value = states[i].substring(0,2);
				field.className = '';
	}	}	}
	else if ( (field.value == '') && required ) {
		field.className = 'err';
	}
} // end validate_state

function validate_zip(field, country, required) {
	/*/
	/*   Basic check of postal code syntax
	/*   Country may be 'USA', 'CAN', or 'both'
	/*/
	
	var value = field.value;
	
	if ( !value && required ) {
		mark_error(field, "Required");
		valid = false;
	}
	else if ( value ) {
		if ( country=='USA' ) {
			var validUSzip = new RegExp("^[0-9]{5}(-[0-9]{4})?$");
			var islandzip = new RegExp("^00[012346789][0-9]{2}$");
			
			if ( !validUSzip.test(value) || islandzip.test(value) ) {
				mark_error(field, "Invalid");
				valid = false;
			} else {
				no_error(field);
		}	}
		else if ( country=='CAN' ) {
		// the case of 'both' would only enter the if above if this were an else if
			var validCAzip = new RegExp("^[a-z][0-9][a-z][ -]?[0-9][a-z][0-9]$", "i");
			var invalidCA = new RegExp("[dfioqu]", "i");
			
			if ( !validCAzip.test(value) || invalidCA.test(value) ) {
				mark_error(field, "Invalid");
				valid = false;
			} else {
				no_error(field);
		}	}
	} else { // field is blank and not required
		no_error(field);
	}
} // end validate_zip

function validate_phone(field, required) {
	/*/
	/*   This function accepts a phone number and checks for the following
	/*   - must be 10 digits
	/*   - can't be the same digit 10 times
	/*   - area code can't start with 0 or 1
	/*   - 9 can't be the second number of area code
	/*   - area code can't be #11
	/*   - the local exchange can't begin with 0 or 1
	/*   - the local exchange can't be 555
	/*/
	
	var number = field.value.replace(RegExp(/[). (-]/g), "");
		
	if ( !number && required ) {
		mark_error(field, "Required");
		valid = false;
	}
	else if ( number ) {
		var validphone = new RegExp("^[2-9][0-8][0-9][2-9][0-9]{6}$");
		var badphone555 = new RegExp("^.{3}555");
		var badphone_11 = new RegExp("^.11");
		var badphone1num = new RegExp("^(0{10}|1{10}|2{10}|3{10}|4{10}|5{10}|6{10}|7{10}|8{10}|9{10})$");

		if ( !validphone.test(number) ||
					badphone555.test(number) ||
					badphone_11.test(number) ||
					badphone1num.test(number) ) {
			mark_error(field, "Invalid");
			valid = false;
		}
		else {
			no_error(field);
		}
	} // end if(number)
	else { // field is blank and not required
		no_error(field);
	}
} // end validate_phone

function validate_phone2 (field_a, field_b, required) {
	/*/
	/*   This id the same as the above but also compares to a
	/*   "confirm phone" field.
	/*/
	
	var number = field_a.value.replace(RegExp(/[). (-]/g), "");
	var confirmation = field_b.value.replace(RegExp(/[). (-]/g), "");
	
	var validphone = new RegExp("^[2-9][0-8][0-9][2-9][0-9]{6}$");
	var badphone555 = new RegExp("^.{3}555");
	var badphone_11 = new RegExp("^.11");
	var badphone1num = new RegExp("^(0{10}|1{10}|2{10}|3{10}|4{10}|5{10}|6{10}|7{10}|8{10}|9{10})$");
	
	if ( !number || 
				!validphone.test(number) ||     
				badphone555.test(number) ||    
				badphone_11.test(number) ||
				badphone1num.test(number) ) {
		mark_error(field_a, "Invalid");
		valid = false;
	}
	else if ( number != confirmation ) {
	// if number is valid but does not match confirmation field
		mark_error(field_a, "Phone must match");
		mark_error_basic(field_b);
		valid = false;
	}
	else { 
		no_error(field_a); 
		no_error(field_b); 
	}
} // end validate_phone2

function validate_word(field, num_words, required) {
	/*/
	/*   Basic check to prevent key mashing
	/*   Word(s) in string must contain a vowel and a consonant
	/*   num_words is how many words (at least) should be in the string
	/*/

	var string = field.value;
	
	if ( (!string || string.toUpperCase() == field.id.toUpperCase())  && required ) {
		mark_error(field, "Required");
		valid = false;
	}
	else if ( string ) {
		var invalidChar = new RegExp("[^a-z.' -]", "i");
		var consonants = "[bcdfghjklmnpqrstvwxz]";
		var vowels = "[aeiouy]";
		var wordHasVowel, wordHasCons;
		
		wordHasVowel = vowels;
		wordHasCons = consonants;
		num_words -= 1;
		
		while (num_words > 0) {
			wordHasVowel += "[a-z]*[ '-][a-z]*" + vowels;
			wordHasCons += "[a-z]*[ '-][a-z]*" + consonants;
			num_words -= 1;
		}
		
		wordHasVowel = new RegExp(wordHasVowel, "i");
		wordHasCons = new RegExp(wordHasCons, "i");
				
		if ( wordHasVowel.test(string) && 
				wordHasCons.test(string) &&
				!invalidChar.test(string) ) {
			no_error(field);
		}
		else {
			mark_error(field, "Invalid");
			valid = false;
		}
	} // end if(string)
	else { // field is blank and not required
		no_error(field);
	}
} // end validate_word

function validate_date_input(field, scope, required) {
	/*/
	/*   Checks a text field for a properly formatted date
	/*   Date must be formatted M/D/YYYY
	/*   The date must also actually exit (30 days have September...)
	/*   scope indicates the range the date must be in
	/*      1: future (including today)
	/*      0: any date
	/*     -1: past (including today)
	/*/
	
	var day, mo, yr
	var parts = new Array;
	
	parts = field.value.split("/");
	
	mo = parts[0];
	day = parts[1];
	yr = parts[2];
	
	var d = mo + "/" + day + "/" + yr
	// reconstruct to help ensure all parts were entered
	
	if (required || d != "//") {
		if ( !RegExp("^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$").test(d) ) {
			mark_error(field, "Invalid");
		}
		else if ( (mo == 2 || mo == 4 || mo == 6 || mo == 9 || mo == 11) && (day == 31) ) {
			mark_error(field, "Invalid");
		}
		else if ( (mo == 2) && ( (yr % 4 != 0 && day > 28) || (yr % 4 == 0 && day > 29) ) ) {
			mark_error(field, "Invalid");
		}
		else {
			d = new Date();
			d.setFullYear(yr, mo-1, day); // Jan=0, Feb=1, ...
			/*/
			/*   TO DO:
			/*      Fix this. It's not comparing dates correctly.
			/*/
			if ( (scope == 1) && (Date() > d) ) {
				// date in the past, needs to be future
				mark_error(field, "Invalid");
			}
			if ( (scope == -1) && (Date() < d) ) {
				// date in the future, needs to be past
				mark_error(field, "Invalid");
			}
			else { 
				no_error(field);
	}	}	}
	else if (required && d == "//") {
		mark_error(field, "Required");
	}
	else { // field is blank and not required
		no_error(field);
	}
} // end validate_date_input


/*/
/*   Marking and Clearing Errors
/*/
function mark_error(field, message) {
	/*/
	/*   First param is field, second is error message
	/*   Function changes field to error class
	/*   Adds error message in field label
	/*   Adds error message to an array to be displayed later
	/*/
	errors.push(field.id + " " + message) 
	//document.getElementById(field.id + "_err").innerHTML = message;
	document.getElementById(field.id).className = "err";
	valid = false;
}
function mark_error_basic(field) {
	/*/
	/*   Same as above without error message
	/*/
	
	mark_outline(field)
	
}
function mark_outline(field) {
	document.getElementById(field.id).className = "err";
}
function no_error(field) {
	/*/
	/*   Param is id of valid field
	/*   Function endures that error market is blank if there are no errors.
	/*/

	clear_outline(field)
	//document.getElementById(field.id + "_err").innerHTML = '';
}
function clear_outline(field) {
	document.getElementById(field.id).className = "";
}
