// Onload handler
$(function() {
	// Hide form, unless flag is set (after submit) not to do it
	if (window.location.toString().indexOf('nohide=1') == -1) {
	  $('#formpje').hide();
	}
	
	// Reserveer nu link shows the form
	// Disabled, moved to dehorecaplanner.nu
	/*
	$('#maaknu a').click(function() {
		$('#formpje').show('slow');
		window.setTimeout(function() {
			$.scrollTo( '#formpje', 800 );
		}, 1000);
		
		return false;
	});
	*/
	
	// Enable lunch time selection
	$('#tijdstip_lunch').click(function() {
		$('select').each(function() {
			this.disabled = false});
		$('#tijd-diner').hide();
		$('#tijd-lunch').show();
	});
	
	// Enable diner time selection
	$('#tijdstip_diner').click(function() {
		$('select').each(function() {
			this.disabled = false});
		$('#tijd-lunch').hide();
		$('#tijd-diner').show();
	});
	
	// Check if reservation for today is still possible
	$('#formpje').submit(function() {
		return checkDatumTijd();
	});
});

function checkDatumTijd() {
	var now = new Date();
	// Use parseFloat because parseInt sometimes thinks the dates are octal numbers
	var selectedDate = parseFloat(document.forms[0].Date_Day.options[document.forms[0].Date_Day.selectedIndex].value);
	var selectedMonth = parseFloat(document.forms[0].Date_Month.options[document.forms[0].Date_Month.selectedIndex].value);
	var selectedYear = parseFloat(document.forms[0].Date_Year.options[document.forms[0].Date_Year.selectedIndex].value);

	if (selectedYear > now.getFullYear())
	  return true;
	if (selectedYear >= now.getFullYear() && selectedMonth > now.getMonth()+1)
	  return true;
	if (selectedYear >= now.getFullYear() && selectedMonth >= now.getMonth()+1 && selectedDate > now.getDate())
	 return true;

	// In the past - sillyness!
	if (selectedYear < now.getFullYear() || 
		selectedMonth < now.getMonth()+1 ||
		selectedDate < now.getDate()) {
		alert(meldingVerleden);
		return false;
	}

	// Today after 16:00
	if (	selectedYear == now.getFullYear() && 
			selectedMonth == now.getMonth()+1 &&
			selectedDate == now.getDate() &&
			now.getHours() >= 16) {
		alert(melding);
		return false;
	}
	
	// Today before 16:00 - ok
	return true;
}