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\"><br/><h2>Merci de participer au concours iPod touch CGA!</h2></div><a href=\"home.html\" title=\"Accueil\" 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; Doit avoir 18 ans ou plus et &ecirc;tre un r&eacute;sident du Canada. Aucun achat n&eacute;cessaire.<br />Le concours se termine le 6 octobre 2009.  Voyez le <a href=\"contest_rules.html\" title=\"R&egrave;glement du concours\" class=\"contestRules\" onclick=\"launchRulesPop(); return false;\">R&egrave;glement du concours</a> pour conna&icirc;tre les d&eacute;tails.</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("Successful!");
}

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("Veuillez entrer le pr&eacute;nom");
		valid = false;
	}
	// Last name check
	if (lastName == 0) {
		$("alert-lastName").insert("Veuillez entrer le nom de famille");
		valid = false;
	}
	// Date of birth check
	if (!validDate.test(dobDay) || !validDate.test(dobMonth) || !validDate.test(dobYear)) {
		$("alert-dob").insert("Veuillez entrer la date de naissance");
		valid = false;
	} else if ((dobMonth < 1) || (dobMonth > 12)) {
		$("alert-dob").insert("Veuillez entrer la date de naissance");
		valid = false;
	} else if (dobMonth == 2) {
		if ((dobDay < 1) || (dobDay > 29)) {
			$("alert-dob").insert("Veuillez entrer la date de naissance");
			valid = false;
		}
	} else if ((dobMonth == 4) || (dobMonth == 9) || (dobMonth == 11)) {
		if ((dobDay < 1) || (dobDay > 30)) {
			$("alert-dob").insert("Veuillez entrer la date de naissance");
			valid = false;
		}
	} else if ((dobDay < 1) || (dobDay > 31)) {
		$("alert-dob").insert("Veuillez entrer la date de naissance");
		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("Veuillez entrer le courriel");
		valid = false;
	}
	// Postal code check
	if (!validPostalPrefix.test(postalCodePrefix) || !validPostalSuffix.test(postalCodeSuffix)) {
		$("alert-postalCode").insert("Veuillez entrer le code postal");
		valid = false;
	}
	// Phone number check
	if (!validPhoneThree.test(phoneArea) || !validPhoneThree.test(phonePrefix) || !validPhoneFour.test(phoneSuffix)) {
		$("alert-phone").insert("Veuillez entrer le t&eacute;l&eacute;phone");
		valid = false;
	}
	// Contest rules confirmation check
	if (conf != true) {
		$("alert-confirm").insert("Vous devez contr&ocirc;ler le cadre ci-dessous pour entrer.");
		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();
	});
}
