$(document).ready(function() {
	$("form.loadMessage").submit(function () {

		if($(this).hasClass("checkFormInput")) {
			var res = checkFormInput();
			if(res) loadingMessage();
			else return false;
		}
		else  loadingMessage();
	});
	$("a.loadMessage").click(function () {
		loadingMessage();
	});
	
	if(document.getElementById('dateFrom') && document.getElementById('dateFrom').value  == '' ) {
		var now = new Date();
  		var startTime = now.getTime() + (1000 * 60 * 60 * 24 * 3);
  		var startDate = new Date(startTime);
  		var day = pad(startDate.getDate(),1);
  		var month = pad(startDate.getMonth() + 1,2);
  		
  		document.getElementById('dateFrom').value = day + "/" + month + "/" + startDate.getFullYear();
  	}
});

function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

// Block the screen with message while page is reloading
function loadingMessage() {
	$("#loading-message" ).dialog({
		autoOpen : false,
		modal:true,
		resizable: false,
		width: 100,
		minHeight: '100',
		draggable: false,
		dialogClass: 'loading-message'
	});
	$("#loading-message" ).dialog("open");
}

function checkFormInput() {
	var msg = "";
	var fromDate;

	if(document.getElementById('property_id_2') && document.getElementById('property_id_2').value == '')
		msg += "- Please select a Parador\n";
		
	if(document.getElementById('children').value < 1 && document.getElementById('adults').value < 1) {
		msg += "- There needs to be at least 1 adult or 1 child per room\n";
	}
	fromDate = checkDate(document.getElementById('dateFrom').value);
	if(!fromDate) msg += "- The ckeck-in date is not valid\n";
	
	if(eval(document.getElementById('adults').value + "+" + document.getElementById('children').value) > 3)
		msg += "- You have selected more than 3 people to occupy one room. Please adjust the number of adults or children.";

	if(msg.length > 0) {
		alert(msg);
		return false;
	}
	else {
		/*
		document.getElementById('requestAvailability').action=document.getElementById('requestAvailability').action + '&property_id=' + document.getElementById('property_id_2').value;
		*/
		return true;
	}
}

function checkFormInputPousada() {
	var msg = "";
	var fromDate;

	if(document.getElementById('property_id_2') && document.getElementById('property_id_2').value == '')
		msg += "- Please select a Pousada\n";
		
	fromDate = checkDate(document.getElementById('dateFrom').value);
	if(!fromDate) msg += "- The ckeck-in date is not valid\n";

	if(msg.length > 0) {
		alert(msg);
		return false;
	}
	else {
		document.getElementById('requestAvailability').action=document.getElementById('requestAvailability').action + '&property_id=' + document.getElementById('property_id_2').value;
		
		return true;
	}
}

var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function checkDate(datestring) {
	var x = document.forms[0].elements;
	
	// Assume a date in a format split up with slashes i.e. 15/07/2007
	var dateparts = datestring.split("/");
	
	// Use 10 base number system, otherwise 08 and 09 will become invalid numbers (OCT-base)
	var day = parseInt(dateparts[0], 10);
	var month = parseInt(dateparts[1], 10);
	var year = parseInt(dateparts[2], 10);

	if (!day || !month || !year) return false;
	
	// Check if month is valid
	if(month > 12 || month < 1) return false;
	
	// If leap year, February will have 29 days
	if (year/4 == parseInt(year/4)) monthLength[1] = 29;

	// unvalid number for day
	if (day > monthLength[month-1] || day < 1) return false;

	// Reset monthLength for next date check
	monthLength[1] = 28;

	var now = new Date();
	now = now.getTime(); //NN3

	var dateToCheck = new Date();
	dateToCheck.setYear(year);
	dateToCheck.setMonth(month-1);
	dateToCheck.setDate(day);
	var checkDate = dateToCheck.getTime();

	var futureDate = (now < checkDate);
	var pastDate = (now > checkDate);
	if(!futureDate) return false;
	
	return dateToCheck;
}
