$(document).ready(function() {
	

	$.datepicker.setDefaults($.datepicker.regional['']);


	$('#checkindatesimple').datepicker({
		defaultDate: +2,
		changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy',
		showOn: 'both', buttonImage: urlImage+'icons/calendar.gif', buttonImageOnly: true,
		buttonText: 'dd/mm/yy',
		yearRange: '-0:+5',
		minDate: '-1y',
		maxDate: '+5y',
		numberOfMonths: 2,
		onClose: function(dateText, inst) { splitDatesSimple(); },
		onChangeMonthYear: function(year, month, inst) {
			$(this).datepicker('setDate', inst.selectedDay+'/'+(inst.selectedMonth+1)+'/'+inst.selectedYear);
		}
	});
	$('#checkindatesimple').datepicker('option',$.datepicker.regional[langidtwoletter]);
	$('#checkindatesimple').datepicker('option','dateFormat','dd/mm/yy');
	$("#checkindatesimple").keydown(function(event) {event.preventDefault();});
	if(document.getElementById('checkindatesimple')) { splitDatesSimple(); }

	$('#checkindate').datepicker({
		defaultDate: +2,
		changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy',
		buttonText: 'dd/mm/yy',
		yearRange: '-0:+5',
		minDate: '-1y',
		maxDate: '+5y',
		numberOfMonths: 2,
		showOn: 'both', buttonImage: urlImage+'icons/calendar.gif', buttonImageOnly: true,
		onClose: function(dateText, inst){
			var checkindate = $.datepicker.parseDate('dd/mm/yy', $('#checkindate').val());
			var checkoutdate = $.datepicker.parseDate('dd/mm/yy', $('#checkoutdate').val());
			
			if(checkoutdate <= checkindate){
				checkoutdate = new Date(checkindate.getTime() + (1000 * 60 * 60 * 24 * 2) + (1000 * 60 * 60 * 2));
				$('#checkoutdate').datepicker('setDate', checkoutdate);
			}
			
			splitDates();
		},
		onChangeMonthYear: function(year, month, inst) {
			$(this).datepicker('setDate', inst.selectedDay+'/'+(inst.selectedMonth+1)+'/'+inst.selectedYear);
		}
	});
	
	$('#checkindate').datepicker('option',$.datepicker.regional[langidtwoletter]);
	$('#checkindate').datepicker('option','dateFormat','dd/mm/yy');
	$("#checkindate").keydown(function(event) {event.preventDefault();});
	
	$('#checkoutdate').datepicker({
		defaultDate: +2,
		changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy',
		buttonText: 'dd/mm/yy',
		yearRange: '-0:+5',
		minDate: '-1y',
		maxDate: '+5y',
		numberOfMonths: 2,
		showOn: 'both', buttonImage: urlImage+'icons/calendar.gif', buttonImageOnly: true,
		onClose: function(dateText, inst){
			var checkindate = $.datepicker.parseDate('dd/mm/yy', $('#checkindate').val());
			var checkoutdate = $.datepicker.parseDate('dd/mm/yy', $('#checkoutdate').val());
			
			if(checkoutdate <= checkindate){
				checkindate = new Date(checkoutdate.getTime() - (1000 * 60 * 60 * 24 * 2) + (1000 * 60 * 60 * 2));
				$('#checkindate').datepicker('setDate', checkindate);
			}
			
			splitDates();
		},
		onChangeMonthYear: function(year, month, inst) {
			$(this).datepicker('setDate', inst.selectedDay+'/'+(inst.selectedMonth+1)+'/'+inst.selectedYear);
		}
	});
	$('#checkoutdate').datepicker('option',$.datepicker.regional[langidtwoletter]);
	$('#checkoutdate').datepicker('option','dateFormat','dd/mm/yy');
	$("#checkoutdate").keydown(function(event) {event.preventDefault();});
	if(document.getElementById('checkindate') && document.getElementById('checkindate').value != '') { splitDates(); }
	
	$('#checkindatebooking').datepicker({
		defaultDate: +2,
		changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy',
		buttonText: 'dd/mm/yy',
		showOn: 'both', buttonImage: urlImage+'icons/calendar.gif', buttonImageOnly: true,
		onClose: function(dateText, inst){
			var checkindate = $.datepicker.parseDate('dd/mm/yy', $('#checkindatebooking').val());
			var checkoutdate = $.datepicker.parseDate('dd/mm/yy', $('#checkoutdatebooking').val());
			
			if(checkoutdate < checkindate){
				checkindate = new Date(checkoutdate.getTime() - (1000 * 60 * 60 * 24 * 2) + (1000 * 60 * 60 * 2));
				$('#checkoutdatebooking').datepicker('setDate', checkindate);
			}
			
			splitDatesBooking();
		},
		onChangeMonthYear: function(year, month, inst) {
			$(this).datepicker('setDate', inst.selectedDay+'/'+(inst.selectedMonth+1)+'/'+inst.selectedYear);
		}
	});
	
	$('#checkindatebooking').datepicker('option',$.datepicker.regional[langidtwoletter]);
	$('#checkindatebooking').datepicker('option','dateFormat','dd/mm/yy');
	$("#checkindatebooking").keydown(function(event) {event.preventDefault();});
	
	$('#checkoutdatebooking').datepicker({
		defaultDate: +2,
		changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy',
		buttonText: 'dd/mm/yy',
		showOn: 'both', buttonImage: urlImage+'icons/calendar.gif', buttonImageOnly: true,
		onClose: function(dateText, inst){
			var checkindate = $.datepicker.parseDate('dd/mm/yy', $('#checkindatebooking').val());
			var checkoutdate = $.datepicker.parseDate('dd/mm/yy', $('#checkoutdatebooking').val());
			
			if(checkoutdate < checkindate){
				checkindate = new Date(checkoutdate.getTime() - (1000 * 60 * 60 * 24 * 2) + (1000 * 60 * 60 * 2));
				$('#checkindatebooking').datepicker('setDate', checkindate);
			}
			
			splitDatesBooking();
		},
		onChangeMonthYear: function(year, month, inst) {
			$(this).datepicker('setDate', inst.selectedDay+'/'+(inst.selectedMonth+1)+'/'+inst.selectedYear);
		}
	});
	$('#checkoutdatebooking').datepicker('option',$.datepicker.regional[langidtwoletter]);
	$('#checkoutdatebooking').datepicker('option','dateFormat','dd/mm/yy');
	$("#checkoutdatebooking").keydown(function(event) {event.preventDefault();});
	if(document.getElementById('checkindatebooking')) { splitDatesBooking(); }
});
