function ageVerified(remember, country) {
	if (remember) {
		$.cookie("ageVerified42BELOW", country, { expires:365 });
	} else {
		$.cookie("ageVerified42BELOW", country);
	}
	location.reload(false);
}

$(function() {

	function selectValue(select) {
		var option = select.options[select.selectedIndex];
		if (option.value != "")
			return option.value;
		return option.text;
	}
	
	$('#ageverification-frm').submit(function(ev) {
		ev.preventDefault();
		
		var user_country = selectValue(this.elements.country);
		if (user_country == "x") user_country = "";
		if (selectValue(this.elements.year) == "" || selectValue(this.elements.month) == "" || selectValue(this.elements.day) == "" || user_country == "") {
			alert("Can we have a valid date of birth and country please?");
			return;
		}
		
		var birthday = new Date(selectValue(this.elements.year), selectValue(this.elements.month) - 1, selectValue(this.elements.day));
		var birthMil = birthday.valueOf();
		
		if (user_country == "United States" || user_country ==  "Oman" || user_country ==  "Pakistan" || user_country ==  "Sri Lanka" || user_country ==  "United Arab Emirates" || user_country ==  "Kiribati" || user_country ==  "Japan") {
			ageCheck = 21;
		} else {
			ageCheck = 18;
		}
		
		var now = new Date();
		var ageCheckDate = new Date( (now.getFullYear() - ageCheck), now.getMonth(), now.getDate() );
		var ageMil = ageCheckDate.valueOf();
		
		if (birthMil > ageMil) {
			if (user_country == "United States") {
				alert("You are being redirected to the Century Council website as you have indicated that you are below the legal drinking age.");
				location = "http://www.centurycouncil.org/";
			} else {
				alert("You are being redirected to the EFRD website as you have indicated that you are below the legal drinking age.");
				location = "http://www.efrd.org/";
			}
			return;
		}
		
		ageVerified(this.elements.remember.checked, user_country);
	});
});