function faderIn(fadeInEles) {
	$A($$(fadeInEles)).each(function(ele) {
		Effect.Fade(ele, {
			from: 0.0,
			to: 1.0,
			duration: 1.0
		});
	});
}

function successful()
{
	// Clear input fields before showing "success" screen.
	$("ipodContest").reset();
	
	if (browser != "Not IE")
	{
		$("ipodContest").remove();
	}
	
	var fadeRemove = "#sect_contest h1, #sect_contest div.column, #sect_contest div.column div.contestHeading, #sect_contest div.clear";
	var insertConf = "<h1>&nbsp;</h1><div class=\"column colLeft\"><div class=\"contestHeading\"><h2>Thanks for entering the CGA iPod touch contest!</h2></div><a href=\"home.html\" title=\"Go back to the home page\" id=\"ieNoThis\" onclick=\"launchClicks(); return false;\"><img src=\"images/button_home_off.gif\" alt=\"Home button\" width=\"67\" height=\"22\" class=\"homeButton\" /></a><p class=\"indicia\">&dagger; Must be age of majority in residing province and a resident of Canada. No purchase necessary.<br />Contest closes February 15, 2011. See the full <a href=\"contest_rules_2011.html\" title=\"Contest rules\" class=\"contestRules\" onclick=\"launchRulesPop(); return false;\">contest rules</a> for more details.</p><div class=\"clear\">&nbsp;</div></div><div class=\"column\"><p><a href=\"\" title=\"\" class=\"next\"><!-- <img src=\"images/button_\" alt=\"\" width=\"95\" height=\"22\" /> --></a></p></div>";
	var fadeInEles ="#sect_contest h1, #sect_contest div.column, #sect_contest div.clear";
	
	$A($$(fadeRemove)).each(function(ele) {
		Effect.Fade(ele, {
			duration: 1.0
		});
	});
	
	setTimeout("$A($$('" + fadeRemove + "')).each(function(ele) {ele.remove();}); $('content').insert('" + insertConf + "'); $A($$('" + fadeInEles + "')).each(function(ele) {ele.setStyle({opacity: 0});}); faderIn('" + fadeInEles + "');", 1000);
}

function failure(oError) {
	if (oError.type === 'user')
	{
		switch(oError.parameter)
		{
			case 'first_name':
				$("alert-firstName").innerHTML = oError.message;
				break;
				
			case 'last_name':
				$("alert-lastName").innerHTML = oError.message;
				break;
				
			case 'email':
				$("alert-email").innerHTML = oError.message;
				break;
				
			case 'phone':
				$("alert-phone").innerHTML = oError.message;
				break;
				
			case 'province':
				$("alert-province").innerHTML = oError.message;	// TODO: Add #alert-province element
				break;
				
			case 'dobd':
				$("alert-dob").innerHTML = oError.message;
				break;
				
			case 'dobm':
				$("alert-dob").innerHTML = oError.message;
				break;
				
			case 'doby':
				$("alert-dob").innerHTML = oError.message;
				break;
		
			default:
				window.alert(oError.message);
		}
	}
	else if (oError.type === 'system')
	{
		window.alert(oError.message);
	}
	else
	{
		window.alert("Sorry, but there was an error that prevented your information from being submitted.  Please try again.");
	}
}

function submitContest(firstName, lastName, dobDay, dobMonth, dobYear, newsEvents, email, province, phone, conf, privacyPolicy)
{
	var valid = true;
	var validDate = /\d{1,2}/;
	// var validEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var validEmail = /^[a-z0-9][-a-z0-9_+.']*@([-a-z0-9]+\.)+[a-z]{2,6}$/i;

	// var validPostalPrefix = /[a-zA-Z][0-9][a-zA-Z]/;
	var validPostalCode = /^[A-Z][0-9][A-Z][ -]?[0-9][A-Z][0-9]$/i;
	
	var validPhoneThree = /[2-9][0-9]{2}/;
	var validPhoneFour = /[2-9][0-9]{3}/;
	var validPhone = /^\(?[\d]{3}\)?[\s\-.]*[\d]{3}[\s\-.]*[\d]{4}$/;
	var name = null;
	var dob = null;
	var postalCode = null;
	
	// Reset error fields
	if ($("alert-firstName") != null)
	{
		$("alert-firstName").innerHTML = '';
	}
	
	if ($("alert-lastName") != null)
	{
		$("alert-lastName").innerHTML = '';
	}
	
	if ($("alert-email") != null)
	{
		$("alert-email").innerHTML = '';
	}
	
	if ($("alert-phone") != null)
	{
		$("alert-phone").innerHTML = '';
	}
	
	if ($("alert-dob") != null)
	{
		$("alert-dob").innerHTML = '';
	}
	
	// First name check
	if (firstName == 0)
	{
		$("alert-firstName").insert("First name required.");
		valid = false;
	}
	
	// Last name check
	if (lastName == 0)
	{
		$("alert-lastName").insert("Last name required.");
		valid = false;
	}
	
	// Date of birth check
	if (!validDate.test(dobDay) || !validDate.test(dobMonth) || !validDate.test(dobYear))
	{
		$("alert-dob").insert("Valid date of birth required.");
		valid = false;
	}
	else if ((dobMonth < 1) || (dobMonth > 12))
	{
		$("alert-dob").insert("Valid date of birth required.");
		valid = false;
	}
	else if (dobMonth == 2 && (dobDay < 1 || dobDay > 29))
	{
		$("alert-dob").insert("Valid date of birth required.");
		valid = false;
	}
	else if ((dobMonth == 4 || dobMonth == 9 || dobMonth == 11) && (dobDay < 1 || dobDay > 30))
	{
		$("alert-dob").insert("Valid date of birth required.");
		valid = false;
	}
	else if (dobDay < 1 || dobDay > 31)
	{
		$("alert-dob").insert("Valid date of birth required.");
		valid = false;
	}
	else if (dobYear != 0 && dobMonth != 0 && dobDay != 0)
	{
		var validAge = 18;
		
		if (province == 'AB' || province == 'MB' || province == 'NB' || province == 'ON' || province == 'PE' || province == 'QC' || province == 'SK')
		{
			validAge = 18;
		}
		else
		{
			validAge = 19;
		}

		var theDate = new Date();
		var dob = new Date(dobYear, dobMonth, dobDay);

		var year_diff = theDate.getFullYear() - dobYear;
		var month_diff = (theDate.getMonth() + 1) - dobMonth;
		var day_diff = theDate.getDate() - dobDay;

		if (month_diff < 0)
		{
			year_diff--;
		}
		else if ((month_diff == 0) && (day_diff < 0))
		{
			year_diff--;
		}

		if (year_diff < validAge)
		{
			$("alert-dob").insert("You must be of age of majority.");
			valid = false;
		}	
	}
	
	// Email check
	if (!validEmail.test(email))
	{
		$("alert-email").insert("Valid email required.");
		valid = false;
	}
		
	// Phone number check
	if (!validPhone.test(phone))
	{
		$("alert-phone").insert("Valid phone number required.");
		valid = false;
	}

	// Contest rules confirmation check
	if (conf != true)
	{
		$("alert-confirm").insert("You must check the box below to enter.");
		valid = false;
	}
	
	// Privacy policy confirmation check
	if (privacyPolicy != true)
	{
		$("alert-privacyPolicy").insert("You must check the box below to enter.");
		valid = false;
	}
	
	if (valid == false)
	{
		return false;
	}
	else
	{
		new Ajax.Request("scripts/api2011.php", {
			method: 'POST',
			parameters: $('ipodContest').serialize(false),
			onSuccess: function(transport) {
				//
				// This onSuccess function tests to make sure not only 
				// that the form data is passed to the PHP file, but 
				// that the data is successully entered into the 
				// database.
				//

				var response = transport.responseJSON;
				
				if (response.response === 'success')
				{
					successful();
				}
				else
				{
					failure(response.error);
				}
			},
			onFailure: failure
		});
	}
	
	return false;
}

function contestForm() {
	$("enterContest").observe("click", function(ec) {
		$$("#ipodContest span.alert").each(function(s) {
			s.update("");
		});
		submitContest($("input-firstName").value, $("input-lastName").value, $("input-dobDay").value, $("input-dobMonth").value, $("input-dobYear").value, $("input-newsEvents").checked, $("input-email").value, $("input-postalCode_first").value, $("input-phone_area").value, $("input-confirm").checked, $("input-privacyPolicy").checked);
		ec.stop();
	});
}
