function setupAjaxPost(){
	$("input").change(function(){
		postForm();
	});
	$("select").change(function(){
		postForm();
	});
}

function postForm(){
	data = $("form#hire_form").serialize();
	$.post('/bikehire/detail/', data, function(data){
		$("#bike-detail").remove();
		$("#rh-col").append(data);
	});
}

function setMinDate() {
	var tdp = $('#id_hired_until');
	var fdp = $("#id_hired_from");
	if (fdp.datepicker("getDate") != null){
		minDate = fdp.datepicker("getDate");
		tdp.datepicker('option', 'minDate', minDate);
		if (tdp.datepicker('getDate')){
			if (tdp.datepicker('getDate').getTime() < minDate.getTime()){
				tdp.datepicker('setDate', minDate);
			}
		}
	}
	return false;
}

function setupDateSelect() {
	
	$('#id_hired_from').datepicker({
        onSelect: setMinDate,
        dateFormat: "dd/mm/yy",
        firstDay: 0,
		minDate:new Date(2009, 6 - 1, 6),
		maxDate:new Date(2009, 9 - 1, 6),
		showAnim:'fadeIn',
        changeFirstDay: false,
    });

    $('#id_hired_until').datepicker({
        dateFormat: "dd/mm/yy",
        firstDay: 0,
		minDate:new Date(2009, 6 - 1, 6),
		maxDate:new Date(2009, 9 - 1, 6),
		showAnim:'fadeIn',
        changeFirstDay: false,
    });

}

function setupTripSelect() {
	
	if (!exists("#id_trip")) return;
	
    var tripbox = $("#id_trip");
    var val = tripbox.val();

    // add the change listener. it should load in trip dates
    tripbox.change(function() {
        loadTripDates($(this).val());
    });

    // load dates if the initial value is not nothing
    if (val != "") loadTripDates(val);

	// hide the custom dates box if it's not needed
    if (datebox_initial_value == OTHER_DATES) {
        showCustomDates();
    }

}

function fixGChatLink() {
	if (! exists("a.googlechat")) return;
	
    $("a.googlechat").click(function() {
        var h = $(this).attr("href");
        window.open(h, 'Google Chat', 'width=300,height=500');
        return false;
    });
}

function setupHireForm() {
	setupDateSelect();
	setupAjaxPost();
}

$(function() {
    fixGChatLink();
    setupHireForm();
});
