function customRange(input) {
    return {minDate: (input.id == 'enddateHotel' ? $('#startdateHotel').datepicker('getDate') : null)};
}

$(document).ready(function(){
						  
	$("form#acc_form input#destination").autocomplete({
		type: 'post',
		source: baseURL+language+'/searchForms/get_location/',
		minLength: 2,
		select: function( event, ui ) {
			$('input[name=destinationId]').val(ui.item.id);
			$('input#destination').css('color','#13a0d7');
		}
		 
	});
	
	$('input#destination').keypress(function() {
		$('input#destination').css('color','');
	});
	
	$("#startdateHotel").datepicker({
		defaultDate: +5,
		dateFormat: "dd/mm/yy",
		changeMonth: true,
		changeYear :true,
		numberOfMonths: 2,
		minDate: +1,
		maxDate: '+2Y',
		showAnim:'fadeIn'
	});
	$("img.ui-datepicker-trigger").click(function(){
		$(this).prev("em").find("input").datepicker("show");
	});
	$("#enddateHotel").datepicker({
		dateFormat: "dd/mm/yy",
		changeMonth: true,
		changeYear :true,
		numberOfMonths: 2,
		maxDate: '+2Y',
		showAnim:'fadeIn',
		beforeShow: function(input) {
			start = $("input#startdateHotel").datepicker('getDate');
			if(start != null)
				$("input#enddateHotel").datepicker('option', 'minDate', start);
			else
				$("input#enddateHotel").datepicker('option', 'minDate', '+1');
		}
	});

	$("input[name='boardType[]']").click(function()
	{
		if($(this).attr('value') == 0)
		{
			var checked_status = this.checked;
			$("input[type='checkbox']").each(function()
			{
				this.checked = checked_status;
			});
		}
		var checked = $("input[type='checkbox'][name='boardType[]'][value!='0']:checked").length;
		var unchecked = $("input[type='checkbox'][name='boardType[]'][value!='0']:not(:checked)").length;
		var total = checked + unchecked;
		if(unchecked > 0)
			$("input[name='boardType[]'][value='0']").attr('checked','');
		if(checked == total)
			$("input[name='boardType[]'][value='0']").attr('checked','checked');
	});

	for( var j = 1; j <= 3; j++ )
	{
		for( var i = 4; i >= 1; i-- )
			$('#r'+j+'c_ages'+i+'Hotel').parent().parent().parent().hide("fast");
		$('#trRoom'+j+'AgesHotel').hide("fast");
		if(j > 1)
		{
			$('#trRoom'+j+'ValuesHotel').hide("fast");
			$('#trRoom'+j+'AgesHotel').hide("fast");
		}
	}

	$('#roomsHotel').change(function(){
		for( var i = 3; i >= 1; i-- )
			if(i <= $(this).val())
			{
				$('#trRoom'+i+'ValuesHotel').show("fast");
				for(var p = 2; p<=i; p++)
					$('#r'+p+'cHotel').trigger("change");
			}else
			{
				$('#trRoom'+i+'ValuesHotel').hide("fast");
				$('#trRoom'+i+'AgesHotel').hide("fast");
			}
	});

	for( var m = 1; m <= 3; m++ )
	{
		$('#r'+m+'cHotel').attr('rel', m);
		$('#r'+m+'cHotel').change(function(){
			var _m =$(this).attr('rel');
			if( $(this).val() > 0 )
			{
				$('#trRoom'+_m+'AgesHotel').slideDown();
				for( var i = 9; i >= 1; i-- )
					if( i <= $(this).val() )
						$('#r'+_m+'c_ages'+i+'Hotel').slideDown();
					else
						$('#r'+_m+'c_ages'+i+'Hotel').slideUp();
			}else
				$('#trRoom'+_m+'AgesHotel').slideUp();
		});
	}

	$("#acc_form").validate({
		rules: {
			 destination: "required",
			 destinationId: "required",
			 enddate: "required",
			 startdate: "required"
			},
		 messages: {
			 destination: "Please enter a destination from the list",
			 destinationId: "Please enter a destination from the list",
			 enddate: "Please enter your return date",
			 startdate: "Please enter your leave date"
		   },
		submitHandler: function(form) {
			//$.blockUI(block_options);
			form.submit();
		   },
		errorPlacement: function(error, element) {
		 error.insertAfter("#errorMessagesHotel");
		},
		errorElement: "li"
 	});
});
