// Perform all necessary tasks to initialize the form
function initForm() {
	// Fetch the country data and set the date format for the preferred country
	countryData =  window.countryData;
	dateFormat = countryData[prefCountry].dateFormat;
	
	$("#countryDropdown").val( prefCountry );	// Set the right value in the dropdown
	updateDateFormat(dateFormat);				// Set the right date format
	
	setPreferedLanuage( prefLanguage );
}

// Process the form input
function processForm() {
	$("#btnEnter").val("Enter Website");
	countryData =  window.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;
	
	if(dateFormat == "dmy") {
		var txtDay = txtDate1;
		var txtMonth = txtDate2;
	}else{
		var txtMonth = txtDate1;
		var txtDay = 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 = "The date of birth you provided is not valid.";
		break;

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

	}

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

}

// Perform necessary tasks when the country is changed
function onCountryChange() {
	countryData =  window.countryData;
	var newCountry = $("#countryDropdown option:selected").val();
	try {
		var newDateFormat = countryData[ newCountry ].dateFormat;
	}
	catch(err) {
		// Fallback to default if country not in list
		var newDateFormat = "mdy";
	}
	updateDateFormat( newDateFormat );

}

// function to handle the changes of the dateformat
function updateDateFormat( newFormat ) {
	// Make sure the visual update is done
	if( newFormat == "mdy" ) {
		$("#txtDate1").val( "MM" );
		$("#txtDate2").val( "DD" );
	}else{
		$("#txtDate1").val( "DD" );
		$("#txtDate2").val( "MM" );
	}

	// Set the hidden input field
	$("#dateFormat").val( newFormat );
}

// 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;
	}
}

