$.datepicker.setDefaults({showOn: 'both', buttonImageOnly: true, 
    buttonImage: '/furniture/images/buttons/calender.gif', buttonText: 'Calendar'});
    

// Prepare to show a date picker linked to three select controls 
function readLinked(input, datepicker) { 
    $('#linkedDates').val( 
        $('#pickup_date_month').val() + '/' + 
        $('#pickup_date_day').val() + '/' +  
        $('#pickup_date_year').val() 
    ); 
    return {}; 
} 
 
// Update three select controls to match a date picker selection 
function updateLinked(date, datepicker) { 
    $('#pickup_date_month').val(date.substring(0, 2).replace(/^0+/,"")); 
    $('#pickup_date_day').val(date.substring(3, 5)); 
    $('#pickup_date_year').val(date.substring(6, 10)); 
    
    
    // Add a check before doing this:
    // If #linkedDates2 is greater than #linkedDates, and #linkedDates2
    // has been altered from the original (i.e. 7 days ahead of today)
    // then DON'T UPDATE!!! Don't wipe out people's choices...
    
    $('#linkedDates2').datepicker('setDate', new Date($('#linkedDates').datepicker('getDate').getTime() + 3600000 * 24 * 7));
    newdate = $('#linkedDates2').datepicker('getDate');
    newdate = (newdate.getMonth()+1 < 10 ? '0' : '') + (newdate.getMonth() + 1) + "/" + (newdate.getDate() < 10 ? '0' : '') + newdate.getDate() + "/" + newdate.getFullYear();
    updateDropoffLinked(newdate, $('#linkedDates2').datepicker());
    
    $('#linkedDates2').datepicker('change', { minDate: $('#linkedDates').datepicker('getDate') } );
}
 
$('#pickup_date_month, #pickup_date_year').change(checkLinkedDays); 
 
// Prevent selection of invalid dates through the select controls 
function checkLinkedDays() { 
    var daysInMonth = 32 - new Date($('#pickup_date_year').val(), 
    $('#pickup_date_month').val() - 1, 32).getDate(); 
    $('#pickup_date_day option').attr('disabled', ''); 
    $('#pickup_date_day option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
    if ($('#pickup_date_day').val() > daysInMonth) { 
        $('#pickup_date_day').val(daysInMonth); 
    } 
} 

///////////////////////////////////////////////////////////////////////////////
// Prepare to show a date picker linked to three select controls 
function readDropoffLinked(input, datepicker) { 
    $('#linkedDates2').val( 
        $('#dropoff_date_month').val() + '/' + 
        $('#dropoff_date_day').val() + '/' +  
        $('#dropoff_date_year').val() 
    ); 
    return {}; 
} 
 
// Update three select controls to match a date picker selection 
function updateDropoffLinked(date, datepicker) { 
    $('#dropoff_date_month').val(date.substring(0, 2).replace(/^0+/,"")); 
    $('#dropoff_date_day').val(date.substring(3, 5)); 
    $('#dropoff_date_year').val(date.substring(6, 10)); 
} 
 
$('#dropoff_date_month, #dropoff_date_year').change(checkDropoffLinkedDays); 
 
// Prevent selection of invalid dates through the select controls 
function checkDropoffLinkedDays() { 
    var daysInMonth = 32 - new Date($('#dropoff_date_year').val(), 
    $('#dropoff_date_month').val() - 1, 32).getDate(); 
    $('#dropoff_date_day option').attr('disabled', ''); 
    $('#dropoff_date_day option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
    if ($('#dropoff_date_day').val() > daysInMonth) { 
        $('#dropoff_date_day').val(daysInMonth); 
    } 
}



$(document).ready(function() {
  $('#linkedDates').datepicker({ 
    minDate: new Date(new Date().getTime() + (3600*24*3*1000)),  
    maxDate: new Date(new Date().getTime() + (3600*24*365*3*1000)),
    beforeShow: readLinked,     
    onSelect: updateLinked 
  }); 
  $('#linkedDates2').datepicker({ 
    minDate: new Date(new Date().getTime() + (3600*24*3*1000)),  
    maxDate: new Date(new Date().getTime() + (3600*24*365*3*1000)),
    beforeShow: readDropoffLinked,     
    onSelect: updateDropoffLinked 
  }); 
  $('#search_form').submit(function() {
      if ($("#pickup").val() == "") {
        alert("Please select a pickup location!");
        return false;
      }
  });
});



///////////////////////////////////////////////////////////////////////////
