function prepareDates() {
    updateDatePicker();
        
    $("select#monitoring_location_id").change(function(){
        $("select.monitoringWindowSelector").hide();
        $('select#monitoring_window_id_' + $(this).val()).show();
        $('input#monitoring_window_id').val($('select#monitoring_window_id_' + $(this).val()).val());
        updateDatePicker();
        $(this).parents('div.column').find("a.representative").remove();
        $(this).parents('div.column').find("img.representative").remove();
        $(this).parents('div.column').append('<a class="fancy representative" href="/trails/' + $(this).val() + '/representative_large.jpg"><img class="representative" src="/trails/' + $(this).val() + '/representative_medium.jpg" width="300" height="200"/></a>');
    });
    
    $("select.monitoringWindowSelector").change(function(){
        $("input#monitoring_window_id").val($(this).val());
        updateDatePicker();
    });
    
    $("select#yearSelector").change(function(){
        $("input#year").val($(this).val());
        updateDatePicker();
    });
    
    $('.hasDefaultValue').focus(function(){
        if ($.trim($(this).val()) == $(this).attr("title")) {
            $(this).val("");
            $(this).addClass("hasValue");
        }
    }).blur(function(){
        if ($.trim($(this).val()) == "") {
            $(this).val($(this).attr("title"));
            $(this).removeClass("hasValue");
        }
    });
}

function updateDatePicker() {
    var selectedMonitoringLocation = $("select#monitoring_location_id").val();
    var selectedYear = $("select#yearSelector").val();
    var selectedMonitoringWindow = $("select#monitoring_window_id_" + $("select#monitoring_location_id").val() + "").val();
    if (selectedMonitoringLocation != '' && selectedMonitoringWindow != '') {
        dateParts = $("select#monitoring_window_id_" + $("select#monitoring_location_id").val() + " option:selected").attr('title').split('_');
        var startMonth = dateParts[0];
        var startDay = dateParts[1];
        var endMonth = dateParts[2];
        var endDay = dateParts[3];
        var minDate = new Date(selectedYear, startMonth - 1, startDay);
        var maxDate = new Date(selectedYear, endMonth - 1, endDay);
        $("div#datepicker").datepicker('destroy');
        $("div#datepicker").datepicker({
            'minDate': minDate,
            'maxDate': maxDate,
            'onSelect': function(dateText) { 
                var dateParts = dateText.split('/');
                month = parseInt(dateParts[0], 10); // Radix (10) is necessary or it breaks on 08 and 09.  No idea why.
                day = parseInt(dateParts[1], 10); // Radix (10) is necessary or it breaks on 08 and 09.  No idea why.
                year = dateParts[2];
                $('input#year').val(year);
                $('input#month').val(month);
                $('input#day').val(day);
    //            $('div#datelabel').text($('input#datelabelfeed').val());
            }/*,
            altField: '#datelabelfeed',
            altFormat: 'DD, MM d, yy'*/
        });
        
        if ($("input#day").val() != '' && $("input#month").val() != '' && $("input#year").val() != '') {
            $("div#datepicker").datepicker('setDate', new Date($("input#year").val(), $("input#month").val() - 1, $("input#day").val()));
        } else {
            $("div#datepicker").datepicker('setDate', minDate);
        }
    } else {
        $("div#datepicker").datepicker('destroy');
    }
}