$(document).ready(function(){

	$("#txtCheckInDate").datepicker({
		showOn: 'button', 
		buttonImage: '/image/calendar.gif', 
		buttonImageOnly: true,
		beforeShow: customDateFunction,
	    onSelect: function(dateText, inst) { 
			$('#txtCheckOutDate').val(get_date_after(dateText, 1)); 
		}		
	});


	$('#txtCheckInDate').datepicker('option', {dateFormat: 'dd/mm/yy'}); 
	$('#txtCheckInDate').datepicker($.datepicker.regional['th']); 
	
	
	
	$("#txtCheckOutDate").datepicker({
		showOn: 'button', 
		buttonImage: '/image/calendar.gif', 
		buttonImageOnly: true,
		beforeShow: customDateFunction

	});
	$('#txtCheckOutDate').datepicker('option', {dateFormat: 'dd/mm/yy'}); 

 
});

/*
use the dateMin and dateMax attributes to enable
the date selection to not end before the start date and 
not start before the end date
*/
function customDateFunction(input) 
{   
	// if the button called is checkin
	// set the maxDate to the checkout date
	if (input.id == "txtCheckInDate") 
    {
		var dateMax; 
		if ($("#txtCheckOutDate").datepicker("getDate") != null) 
		{
			dateMax = $("#txtCheckOutDate").datepicker("getDate"); 
		}
		//return {maxDate: dateMax, minDate: new Date()};                     
		return {minDate: new Date()}; 
	}
	else if (input.id == "txtCheckOutDate")
	{ 
		var dateMin; 
		//if ($("#txtCheckInRate").datepicker("getDate") != null)
		//	dateMin = $("#txtCheckInRate").datepicker("getDate"); 

		var tmpCheckOut = $('#txtCheckOutDate').val(); 
		$('#txtCheckOutDate').val(get_date_after($('#txtCheckInDate').val(), 1)); 

		dateMin = $('#txtCheckOutDate').datepicker('getDate'); 

		$('#txtCheckOutDate').val(tmpCheckOut); 
		return { minDate: dateMin  }; 
	}
}
