$(document).ready(function(){

	// form pop up centered to screen
	var windowY = $(window).width();
	var windowX = $(window).height();
	var leftPosition = (windowY/2) - (380/2);
	var topPosition = (windowX/2) - (460/2);
	$('#form').css({left: leftPosition, top: topPosition});


	// form province/states viewable depending on country 
	$('#countries').change(function(){
			var val = $('#countries').val();
			if (val == "CA")
			{
				$('#provinces').showOptionGroup();
				$('#states').hideOptionGroup();
				$('#pro-stat').selectFirstOption();
				
			}
			else {
				$('#provinces').hideOptionGroup();
				$('#states').showOptionGroup();
				$('#pro-stat').selectFirstOption();
			}
	});	


	$.fn.selectFirstOption = function() {
		var option = $(this).find("option:first").attr("selected", true);
	}

	$.fn.hideOptionGroup = function() {
		$(this).hide();
		$(this).children().each(function(){
			$(this).attr("disabled", "disabled").removeAttr('selected');
		});
		$(this).appendTo($(this).parent());
	}

	$.fn.showOptionGroup = function() {
		$(this).show();
		$(this).children().each(function() {
			$(this).removeAttr('disabled');
		});
	$(this).prependTo($(this).parent());
	$(this).parent().animate({scrollTop:0}, 0);
	}

	
	// form "i am not of legal age" link pop up
	 	$('.legal-msg > a').click(function(event){
				event.preventDefault();
				alert('Sorry. You are too young to enter this site.')
			});


	// Form validation 
	if (ageVerified) {
		$("#form").hide();
		$("#overlay").hide();
	}
	else {
	}

	// Handle form when user submits it
	$("#submit").click(checkscript);
	
});


/********************************** 
* FORM AGE VALIDATION FUNCTIONS 
**********************************/

//  Age has been verified when we have the age verfication cookie
var ageVerified = false;

if ($.cookie("overage") != null) {
	ageVerified = true;
}

// find out minimum age given country and province 
function getMinAge(country, province) {
	if(country == "US") {
		return 21;
	}
	else if(country == "CA") {
		if(province == "AB" || province == "MB" || province == "QC") {
			return 18;
		}
		else {
			return 19;
		}
	}
	else{
		return 18;
	}
}
	
// calculate user age in years
function calcAgeInYears(birthdate) {
	var currentDate = new Date(); 

	// determine time elapsed in milliseconds
	var timeElapsed = currentDate - birthdate;


	var millisecondsPerYear = 365.26 * 24 * 60 * 60 * 1000;
	return Math.floor(timeElapsed / millisecondsPerYear);
}

// check to see if user age is greater than minimum age
function isOverage(birthdate, country, province){
	var minAge = getMinAge(country, province); 
	var age = calcAgeInYears(birthdate); 
	var overage = age >= minAge;
	return overage;
}

// check to see if user is over age
// create a cookie 
function checkscript(event){
	event.preventDefault();
	
	var country = document.verificationForm.country.value;
	var province = document.verificationForm.province.value;
	var day = document.verificationForm.day.value;
	var month = document.verificationForm.month.value;
	var year = document.verificationForm.year.value;
	var birthdate = new Date(year, month - 1, day -1);
	var overage = isOverage(birthdate, country, province);

	if (overage)
	{
		$.cookie("overage", "true");
		$("#form").hide();
		$("#overlay").hide();
	}
	else
	{
		if (country == "Select a Country" || province == "Select a State/Province" || day == "DD" || month == "MM" || year == "YYYY")
		{
			alert('Please fill out entire form to continue.');
		}
		else {
			alert('Sorry. You are too young to enter this site.');
		}
	}
}


