var tlos_field_checks = new Array();
var tlos_field_checks_special = new Object();
var tlos_entry_form = (document.forms.length ==1)? document.forms[0] : document.forms.sweeps_entry;
var tlos_action_orig = tlos_entry_form.action;
var tlos_prepop = new Object();
var tlos_today = '2009-11-07';
var latest_birthdate = '1989-09-21';
tlos_field_checks.push( {
	name: 'email',
	type: 'email',
	message: 'Please be sure your e-mail address is entered correctly, without spaces or extra characters, as in: \'name@isp.com.\''
} );
tlos_field_checks.push( {
	name: 'firstname',
	type: 'basic',
	message: 'Please provide your first name.'
} );
tlos_field_checks.push( {
	name: 'lastname',
	type: 'basic',
	message: 'Please provide your last name.'
} );
tlos_field_checks.push( {
	name: 'area_code',
	type: 'area_code',
	message: 'Please provide your area code.'
} );
tlos_field_checks.push( {
	name: 'phone',
	type: 'phone_only',
	message: 'Please provide your phone number.'
} );
tlos_field_checks.push( {
	name: 'readrules',
	type: 'checked',
	message: 'Please indicate that you have read and agree to the official rules.'
} );
tlos_field_checks_special['too_young'] = 'Sorry, you must be at least 18 years of age (as of September 21, 2007) to enter.';
tlos_field_checks_special['already_entered'] = 'Sorry, you may only enter once per day. Please come back and try again tomorrow.';
tlos_field_checks_special['contest_over'] = 'Thank you for your interest in the NBC Sports &quot;Line of Scrimmage Sweepstakes&quot; presented by Toyota. The sweepstakes period has ended.';

// uses globals tlos_action_orig, tlos_entry_form
tlos_entry_form.onsubmit = function() {
	var action_true = tlos_action_orig;
	var action_false = 'javascript:void(0)';
	var fcheck = check_reqfields();
	if (typeof(fcheck) == 'boolean') { 	// req fields have been filled in properly, now check if entered already
		if (typeof(user_entered_today) != 'undefined' && user_entered_today && tlos_entry_form.email.value.toLowerCase() == tlos_prepop['email'].toLowerCase()) {
			tlos_entry_form.action = action_false;
			alert_errors(tlos_field_checks_special.already_entered);
			return false;
		} else {						// check age
			var birthdate = tlos_entry_form.birth_year.value + '-' + ('00'+tlos_entry_form.birth_month.value).substring(('00'+tlos_entry_form.birth_month.value).length-2) + '-' + ('00'+tlos_entry_form.birth_day.value).substring(('00'+tlos_entry_form.birth_day.value).length-2);
			if (birthdate > latest_birthdate) {
				tlos_entry_form.action = action_false;
				alert_errors(tlos_field_checks_special.too_young);
				return false;
			} else {
				tlos_entry_form.action = action_true;
				return true;
			}
		}
	} else {							// req fields have not been filled in properly
		tlos_entry_form.action = action_false;
		alert_errors(fcheck.join("\n"));
		return false;
	}
	
}
// alert errors
function alert_errors(jstring) {
	alert ('Sorry, your entry couldn\'t be accepted. Please check these items, and try again:\n\n' + jstring);
}
// uses globals tlos_field_checks, tlos_entry_form
function check_reqfields() {
	var output = new Array();
	for (var jx=0;jx<tlos_field_checks.length;jx++) {
		var theval = tlos_entry_form[tlos_field_checks[jx].name].value;
		switch (tlos_field_checks[jx].type) {
			case 'basic' :
				if (theval.length < 1) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			case 'word' :
				if (theval.search(/\w/) < 0) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			case 'word_only' :
				if (theval.search(/\W/) > -1) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			case 'number' :
				if (theval.search(/\D/) > -1) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			case 'phone' :
				var ta = theval.match(/\d/g);
				if (ta == null || ta.length < 10) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			case 'phone_only' :
				var ta = theval.match(/\d/g);
				if (ta == null || ta.length < 7) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			case 'area_code' :
				var ta = theval.match(/\d/g);
				if (ta == null || ta.length < 3) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			case 'email' :
				if (theval.toLowerCase().search(/^[\w-\.]+@([\w-]+\.)+[a-z]{2,5}$/) < 0) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			case 'checked' :
				if (!tlos_entry_form[tlos_field_checks[jx].name].checked) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
			default :
				if (theval.toLowerCase() != tlos_field_checks[jx].type.toLowerCase()) {
					output.push(tlos_field_checks[jx].message);
				}
			break;
		}
	}
	return (output.length > 0)? output : true;
}