// Perform all necessary tasks to initialize the form
function initForm() {
	// Fetch the country data and set the date format for the preferred country
	countryData =  $.data(target, 'countryData');
	$("#countryDropdown").val( prefCountry );	// Set the right value in the dropdown

	// IF the country is US, hide remember me option
	if(prefCountry == "US") {
		$("#remember").css("display", "none");
	}
}

// Process the form input
function processForm() {

	countryData = $.data(target, 'countryData');
	// Fetch the input data
	var dateFormat = $("#dateFormat").val();
	var country = $("#countryDropdown option:selected").val();
	var txtDate1 = $("#txtDate1").val();
	var txtDate2 = $("#txtDate2").val();
	var txtYear = $("#txtYear").val();
	var language = $("#language").val();
	var lda = countryData[ country ].lda;

	var txtDay = txtDate1;
	var txtMonth = txtDate2;

	// Check the input
	if( txtDay <= 31 && txtMonth <= 12 && txtYear > 1900 && txtYear < 2009 ) {
		var dateNow = new Date();
		var dateOfBirth=new Date();
		dateOfBirth.setFullYear( txtYear, txtMonth-1, txtDay );
   		var visitorAge = Math.floor((dateNow.getTime() - dateOfBirth.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
		// Is the visitor of legal drinking age?
		if( visitorAge >= lda) {
			return true;
		}else{
			displayError( "underaged" );
			return false;
		}

	}else{
		displayError( "invalidinput" );
		return false;
	}
	return false;
}

function displayError( code ) {
	var message = "";

	switch(code) {
		case "underaged":
		message = "Sorry, you are not old enough to visit this website.";
		break;

		case "invalidinput":
		message = "Sorry, you are not of legal drinking age.";
		break;

		default:
		message = "An error occured";
		break;
	}

	$("#errorMessage").html( message );
}

// Set the prefered language
function setPreferedLanuage( language ) {
	switch( language ) {
		case "nl" : $("#radioDutch").click();$("#lblDutch").addClass("selected"); break;
		case "fr" : $("#radioFrench").click();$("#lblFrench").addClass("selected"); break; 
		case "en" : $("#radioEnglish").click();$("#lblEnglish").addClass("selected"); break;
	}
}


