function faderIn(fadeInEles) {
	$A($$(fadeInEles)).each(function(ele) {
		Effect.Fade(ele, {
			from: 0.0,
			to: 1.0,
			duration: 1.0
		});
	});
}

function successful() {
	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 18 years of age or older and a resident of Canada. No purchase necessary.<br />Contest closes October 6, 2009. See the full <a href=\"contest_rules.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() {
	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, postalCodePrefix, postalCodeSuffix, phoneArea, phonePrefix, phoneSuffix, conf) {
	var valid = true;
	var validDate = /\d{1,2}/;
	var validEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var validPostalPrefix = /[a-zA-Z][0-9][a-zA-Z]/;
	var validPostalSuffix = /[0-9][a-zA-Z][0-9]/;
	var validPhoneThree = /[2-9][0-9]{2}/;
	var validPhoneFour = /[2-9][0-9]{3}/;
	var name = null;
	var dob = null;
	var postalCode = null;
	var phone = null;
	// 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) {
		if ((dobDay < 1) || (dobDay > 29)) {
			$("alert-dob").insert("Valid date of birth required.");
			valid = false;
		}
	} else if ((dobMonth == 4) || (dobMonth == 9) || (dobMonth == 11)) {
		if ((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;
	}
	dobYear = "19" + dobYear;
	/*if (dobYear >= 1995) {
		if (dobMonth >= 8) {
			if (dobDay > 14) {
				$("alert-dob").insert("Sorry, you&rsquo;re too young.");
				valid = false;
			}
		}
	}*/
	// Email check
	if (!validEmail.test(email)) {
		$("alert-email").insert("Valid email required.");
		valid = false;
	}
	// Postal code check
	if (!validPostalPrefix.test(postalCodePrefix) || !validPostalSuffix.test(postalCodeSuffix)) {
		$("alert-postalCode").insert("Valid postal code required.");
		valid = false;
	}
	// Phone number check
	if (!validPhoneThree.test(phoneArea) || !validPhoneThree.test(phonePrefix) || !validPhoneFour.test(phoneSuffix)) {
		$("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;
	}
	if (valid == false) 
		return false;
	else {
		//name = firstName + " " + lastName;
		dob = dobDay + "/" + dobMonth + "/" + dobYear;
		postalCode = postalCodePrefix + " " + postalCodeSuffix;
		phone = "(" + phoneArea + ") " + phonePrefix + "-" + phoneSuffix;
		var data = {
			firstname: firstName,
			lastname:  lastName,
			dob: dobYear + "-" + dobMonth + "-" + dobDay,
			email: email,
			postalCode: postalCodePrefix + " " + postalCodeSuffix,
			phone: "(" + phoneArea + ") " + phonePrefix + "-" + phoneSuffix,
			newsEvents: newsEvents
		};
		// window.alert("Success.");*/
		//successful();
		new Ajax.Request("scripts/api.php", {
			method: "post",
			parameters: "json=" + Object.toJSON(data),
			onSuccess: function(transport) {
					/*
					This onSuccess function tests to make sure not only 
					that the form’s data is passed to the PHP file, but 
					that the data is successully entered into the 
					database.
					*/
				var response = transport.responseText;
				if (response === 'SUCCESS') 
					successful();
				else
					failure();
				},
			onFailure: failure
		});
	}
}

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-postalCode_last").value, $("input-phone_area").value, $("input-phone_first").value, $("input-phone_last").value, $("input-confirm").checked);
		ec.stop();
	});
}