var stateArray = new Array( "AL", "AR", "AZ", "CO", "CT", "DC", "DE", "GA", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "Other" );

function cnnPageOnload()
{
	populateDropdown( cnnGetObject( 'dobm' ), 1, 12, "des" );
	populateDropdown( cnnGetObject( 'dobd' ), 1, 31, "des" );
	populateDropdown( cnnGetObject( 'doby' ), 1900, 2005, "asc" );
	populateStateDropdown( cnnGetObject( 'state' ) );
}

function populateDropdown( obj, start, end, order ) {
	if( obj ) {
		if( order == "des" ) {
			for( var optCounter=start; optCounter <= end; optCounter++ ) {
				obj.options[obj.options.length] = new Option( optCounter, optCounter );
			}
		} else {
			for( var optCounter=end; optCounter >= start; optCounter-- ) {
				obj.options[obj.options.length] = new Option( optCounter, optCounter );
			}
		}
	}
}

function populateStateDropdown( obj ) {
	if( obj ) {
		for( var optCounter=0; optCounter <= stateArray.length; optCounter++ ) {
			obj.options[obj.options.length] = new Option( stateArray[optCounter], stateArray[optCounter] );
		}
	}
}
function getGender( form ) {
	for ( var i=0; i < form.gender.length; i++ ) {
		if( form.gender[i].checked ) { return form.gender[i].value; }
	}
}

function submitThis( form ) {
	detSubmit = determineSubmit( form );
	if( detSubmit ) {
		alert( detSubmit );
		form.redirect.value="/magazine/specials/tailgate/2005/gutcheck/error.html";
		return false;
	} else {
		form.redirect.value="/magazine/specials/tailgate/2005/gutcheck/send.html";
		form.response3.value = getGender( form );
		form.name.value = form.fname.value + ' ' + form.lname.value;
		form.response4.value = form.dobm.value + '/' + form.dobd.value + '/' + form.doby.value;
		return true;
	}
}

function subPop( url, name, width, scrolling ) {
	if( scrolling == "yes" ) width += 18;
	widgets = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scrolling + ',resizable=no,width=' + width + ',height=600';
	popupWin = window.open( url, name, widgets );
	popupWin.opener.top.name = "opener";
	popupWin.focus();
}

function determineSubmit( form ) { // driver
	var isFilled = allFilled( form );
	if( isFilled != "" ) return isFilled;
	else {
		var isLegal = giveWarning( form );
		return isLegal;
	}
}

function IsGender ( form ) {
	// if the button group is an array (one button is not an array)
	if (form.gender[0]) { for (var i=0; i<form.gender.length; i++) { if (form.gender[i].checked) return true; } }
	// if the one button is checked, return zero
	else { if (form.gender.checked) { return true; } }
	// if we get to this point, no radio button is selected
	return false;
}

function allFilled( form ) { // when necessary fields are empty, return an error message to alert user
	var error = "";
	if( !isValidEmail( form.from.value ) )		error = "Your email address should follow this format: someone@somewhere.[com,org,mil,net,biz,tv,etc.]";
	else if( form.fname.value == "" )		error = "Please enter your first name";
	else if( form.lname.value == "" )		error = "Please enter your last name";
	else if( form.response1.value == "" )		error = "Please enter your city";
	else if( !IsGender( form ) )			error = "Please enter your gender";
	else if( form.dobm.value == "" )		error = "Please enter your month of birth";
	else if( form.dobd.value == "" )		error = "Please enter your day of birth";
	else if( form.doby.value == "" )		error = "Please enter your year of birth";
	else if( form.response2.value == "" )		error = "Please select your state";
	return error;
}

function giveWarning( form ) { // determines if the applicant is eligible
	var ageLimitAge = 18;
	var result = "";
	var tooYoung = "We're sorry, you must be " + ageLimitAge.toString() + " years of age to enter.";
	var wrongCountry = "";
	var wrongState = "";
	today = new Date();
	ageLimit = new Date();
	ageLimit.setYear( today.getYear() - ageLimitAge );
	givenDOB = new Date();
	givenDOB.setYear( form.doby.value );
	givenDOB.setDate( form.dobd.value );
	givenDOB.setMonth( form.dobm.value - 1 ); // months start with 0
	if(  ( givenDOB.getYear() ) >  ( ageLimit.getYear() )  ) { result = tooYoung; }
	else if( ( ( givenDOB.getYear() ) == ( ageLimit.getYear() )) && ( ( givenDOB.getMonth()) >  ( ageLimit.getMonth()))) { result = tooYoung; }
	else if( ( ( givenDOB.getYear() ) == ( ageLimit.getYear() )) && ( ( givenDOB.getMonth()) == ( ageLimit.getMonth())) && ( ( givenDOB.getDate() ) >  ( ageLimit.getDate() ))) { result = tooYoung; }
	else result = isValidDate( form.dobm.value, form.dobd.value, form.doby.value )

	return result;
}

function isValidDate( month, day, year) { // determines if the birthdate is a legal date
	var result = ""
	if ((month <= 0) || (month > 12)) { result = "Invalid month"; }
	if ((day <= 0) || (day > 31)) { result = "Invalid date"; }
	if ((month == 2) && (day > 29)) { result = "Invalid date"; }             // February can't be greater than 29 (leap year calculation comes later)
	if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {     // check for months with only 30 days
		if (day > 30) { result = "Invalid date"; }
	}
	if ((year <= 0) || (year > 9999)) { result = "Invalid year"; }           // check for leap year if the month and day is Feb 29
	if ((month == 2) && (day == 29)) {
		var div4 = year % 4;
		var div100 = year % 100;
		var div400 = year % 400;
		if (div4 != 0) { result = "Invalid date"; }                      // if not divisible by 4, then not a leap year so Feb 29 is invalid
		if ((div100 == 0) && (div400 != 0)) { result = "Invalid Date"; } // if year is divisible by 100 and not 400, then it's not a leap year so Feb 29 is invalid
	}
	return result;
}

function isValidEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1) {
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1) {
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1) {
		return false;
	}
	if (str.indexOf(" ")!=-1) {
		return false;
	}
	return true;					
}