$(document).ready(function() {
	$("#linkedDatesFrom").datepicker({
		minDate: new Date(2001, 1 - 1, 1),
		maxDate: new Date(2010, 12 - 1, 31),
		beforeShow: readLinkedFrom,
		onSelect: updateLinkedFrom,
		showOn: "both",
		buttonImage: "/wp-content/plugins/vacation-planner/images/calendar.gif",
		buttonImageOnly: true
	});
	$("#month-from, #year-from").change(checkLinkedDaysFrom);
	
	$("#linkedDatesTo").datepicker({
		minDate: new Date(2001, 1 - 1, 1),
		maxDate: new Date(2010, 12 - 1, 31),
		beforeShow: readLinkedTo,
		onSelect: updateLinkedTo,
		showOn: "both",
		buttonImage: "/wp-content/plugins/vacation-planner/images/calendar.gif",
		buttonImageOnly: true
	});
	$("#month-to, #year-to").change(checkLinkedDaysTo);
});

function readLinkedFrom() { 
    $('#linkedDatesFrom').val($('#month-from').val() + '/' + 
        $('#day-from').val() + '/' + $('#year-from').val()); 
    return {}; 
} 

function readLinkedTo() { 
    $('#linkedDatesTo').val($('#month-to').val() + '/' + 
        $('#day-to').val() + '/' + $('#year-to').val()); 
    return {}; 
}
 
// Update three select controls to match a date picker selection 
function updateLinkedTo(date) { 
    $('#month-to').val(date.substring(0, 2)); 
    $('#day-to').val(date.substring(3, 5)); 
    $('#year-to').val(date.substring(6, 10)); 
}

// Update three select controls to match a date picker selection 
function updateLinkedFrom(date) { 
    $('#month-from').val(date.substring(0, 2)); 
    $('#day-from').val(date.substring(3, 5)); 
    $('#year-from').val(date.substring(6, 10)); 
} 
 
// Prevent selection of invalid dates through the select controls 
function checkLinkedDaysFrom() { 
    var daysInMonth = 32 - new Date($('#year-from').val(), 
        $('#month-from').val() - 1, 32).getDate(); 
    $('#day-from option').attr('disabled', ''); 
    $('#day-from option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
    if ($('#day-from').val() > daysInMonth) { 
        $('#day-from').val(daysInMonth); 
    } 
}

// Prevent selection of invalid dates through the select controls 
function checkLinkedDaysTo() { 
    var daysInMonth = 32 - new Date($('#year-to').val(), 
        $('#month-to').val() - 1, 32).getDate(); 
    $('#day-to option').attr('disabled', ''); 
    $('#day-to option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
    if ($('#day-to').val() > daysInMonth) { 
        $('#day-to').val(daysInMonth); 
    } 
}
