$(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'}); 

	$('a.overlay').click(function(event){
		event.preventDefault(); 
		if ($(this).attr('rel') != "" && $(this).attr('rel') != undefined)
		{
			var rel = $(this).attr('rel'); 
			var begin_brace = rel.lastIndexOf('['); 
			var end_brace = rel.lastIndexOf(']'); 
		
			rel_val = rel.substr(begin_brace + 1, rel.length - begin_brace - 2); 
			array_rel = rel_val.split(",");
			params = new Array(); 			
			for (var i = 0; i < array_rel.length; i++)
			{
				key_value = array_rel[i].split("=");
				key = key_value[0];
				value = key_value[1]; 
				switch ($.trim(key))
				{
					case "width" : 
						var width = value; 
					break; 

					case "height" : 
						var height = value; 
					break; 

					case "title":
						var title = value; 
					break; 

					case "is_ajax" : 
						var is_ajax = value; 
					break; 				

					case "content" : 
						var content = value; 

				}
			}	//--- end for ---//

		
			callOverlay($(this), width, height, title, is_ajax, content); 
			
		}
		else 
		{
			callOverlay($(this)); 
		}

	
	}); 

 
});

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

