var datepickers = function() {
    $('#date-pick').datePicker({ createButton:false })
	    .bind( 'click', function() {
    		updateSelects($(this).dpGetSelected()[0], $('#check_in_day'),$('#check_in_month'),$('#check_in_year'));
    		$(this).dpDisplay();
    		return false;
    	})
    	.bind( 'dateSelected', function(e, selectedDate, $td, state) {
    		updateSelects(selectedDate, $('#check_in_day'),$('#check_in_month'),$('#check_in_year'));
    	});
    $('#date-pick2').datePicker({ createButton:false })
	    .bind( 'click', function() {
    		updateSelects($(this).dpGetSelected()[0], $('#check_out_day'),$('#check_out_month'),$('#check_out_year'));
    		$(this).dpDisplay();
    		return false;
    	})
    	.bind( 'dateSelected', function(e, selectedDate, $td, state) {
    		updateSelects(selectedDate, $('#check_out_day'),$('#check_out_month'),$('#check_out_year'));
    	});
   
   $('#check_in_day, #check_in_month, #check_in_year').bind('change', function() { setSelectedDate($('#date-pick'),$('#date-pick2'),$('#check_in_day'),$('#check_in_month'),$('#check_in_year'),$('#Arrive')); });
   $('#check_out_day, #check_out_month, #check_out_year').bind('change', function() { setSelectedDate($('#date-pick2'),null,$('#check_out_day'),$('#check_out_month'),$('#check_out_year'),$('#Depart')) } );
   $('#check_in_day').trigger('change');
   $('#check_out_day').trigger('change');
   
   $('#date-pick').bind(
    		'dpClosed',
    		function(e, selectedDates)
    		{
    			var d = selectedDates[0];
    			if (d) {
    				d = new Date(d);
    				$('#Arrive').val(d.asString());
    				
    			    updateSelects(d, $('#check_in_day'), $('#check_in_month'), $('#check_in_year'));
    				
    				$('#date-pick2').dpSetStartDate(d.addDays(1).asString());
    				//$('#date-pick2').dpSetSelected(d.addDays(1).asString());
    				
    				updateSelects(d, $('#check_out_day'), $('#check_out_month'), $('#check_out_year'));
    				$('#Depart').val(d.asString());
    				
    			}
    		}
    );
    
    $('#date-pick2').bind(
    		'dpClosed',
    		function(e, selectedDates)
    		{
    			var d = selectedDates[0];
    			if (d) {
    				d = new Date(d);
    				$('#Depart').val(d.asString());
    				updateSelects(d, $('#check_out_day'), $('#check_out_month'), $('#check_out_year'));
    			}
    		}
    );
}

function handlePicker (dpel, day, month, year, dpel2) {
    dpel.datePicker({ createButton:false })
       .bind( 'click', function() {
    		updateSelects($(this).dpGetSelected()[0], day, month, year);
    		$(this).dpDisplay();
    		return false;
    	}).bind( 'dateSelected', function(e, selectedDate, $td, state) {
    		updateSelects(selectedDate, day, month, year);
    	}
    	).bind( 'dpClosed', function(e, selected) {
    		updateSelects(selected[0], day, month, year);
    	});
    	
   day.bind('change', function() { setSelectedDate(dpel,dpel2,day,month,year) } );
   month.bind('change', function() { setSelectedDate(dpel,dpel2,day,month,year) });
   year.bind('change', function() { setSelectedDate(dpel,dpel2,day,month,year) } );
   day.trigger('change');
}
var setSelectedDate = function (dpel,dpel2,d,m,y,f) {
    var dt = new Date( y.val(), m.val()-1, d.val() );
    f.val(dt.asString());
    dpel.dpSetSelected(dt.asString());
    if(dpel2 != undefined) {
    	var d2 = new Date( dt );
    	d2 = d2.addDays(1);
    	dpel2.dpSetSelected(d2.asString());
    	dpel2.dpSetStartDate(d2.addDays(-1).asString());
    	dpel2.dpDisplay();
    	dpel2.dpClose();
    }
}

var updateSelects = function (selectedDate, ds, ms, ys)
{
	selectedDate = new Date(selectedDate);
	var d = selectedDate.getDate();
	var m = selectedDate.getMonth();
	var y = selectedDate.getFullYear();
	(ds[0]).selectedIndex = d - 1;
	(ms[0]).selectedIndex = m;
	ys.val(y);
	ds.trigger('change');
}