$(function()
{

	var errors = "";
	
	$("#CID").change(function()
	{
		
		var curVal = $(this).val();
		
		if(curVal >= $("#COD").val())
		{
		
			$("#COD option").each(function(i, el)
			{
			
				if($(this).val() === curVal)
				{
				
					$(this).next().attr("selected", "selected");
					
				}
				
			});
			
		}
	
	});
	
	$("#COD").change(function()
	{
	
		var curVal = $(this).val();
		
		if(curVal <= $("#CID").val())
		{
		
			$("#CID option").each(function(i, el)
			{
			
				if($(this).val() === curVal)
				{
				
					$(this).prev().attr("selected", "selected");
					
				}
				
			});
		
		}
	
	});
	
	$("form#booking-form").submit(function()
	{
	
		if($("#txtAdults").val() === '')
		{
		
			errors+= "Please fill in the number of adults\n";
			$("#txtAdults").addClass("error");
		
		} else {
			
			$("#txtAdults").removeClass("error");
			
		}
	
		if($("#txtNumRooms").val() === '')
		{
		
			errors+= "Please fill in the number of rooms you require\n";
			$("#txtNumRooms").addClass("error");
		
		} else {
			
			$("#txtNumRooms").removeClass("error");
		
		}
		
		var now = new Date("<?php echo date('F', time()).' '.date('d', time()). ', '.date('Y', time()); ?> ");
		
		var fromDate = new Date($("#CIM").val()+" "+$("#CID").val()+", "+$("#CIY").val());
		var toDate = new Date($("#COM").val()+" "+$("#COD").val()+", "+$("#COY").val());
		
		if(now > fromDate || now > toDate)
		{
		
			errors+= "Please choose a date in the future\n";
		
		}
		
		if(fromDate >= toDate)
		{
		
			errors+= "Please choose a check-out date after the check-in date\n";
		
		}
		
		if(errors !== "")
		{
		
			alert(errors);
			errors = "";
		
			return false;
			
		}
	
	});
	
});
