var datebox_initial_value;
var OTHER_DATES = -2;
var WHERE_HEARD_OTHER = 7;
var customDatePickerGenerated = false;

function loadTripDates(trip_id) {
	hideCustomDates();
    var box = $("#id_trip_date");
    box.disabled = true;
    if (trip_id != "") {
        box.html("<option value='-1'>Loading dates...</option>");
        $.getJSON("/enquire/api/dates_for_trip/" + trip_id + "/",
        function(data) {
            if (data.length) {
                if (data.length > 1) {
                    $("#id_trip_date").html("<option value='-1'>Select a date</option>");
                } else {
                    $("#id_trip_date").html("");
                }

                box.disabled = false;
                $.each(data,
                function(i, item) {
                    opt = "<option value='" + item.i + "'>"
                    if (item.n) {
                        opt += item.n + ": ";
                    }
                    opt += item.f + " to " + item.t + "</option>"
                    box.append(opt);
                });
                opt = "<option value='-2'>Other dates...</option>"
                box.append(opt);
            } else {
                box.disabled = true;
                $("#id_trip_date").html("<option value='-1'>No dates available. Sorry!</option>");
            }

            box.val(datebox_initial_value);

        });

    } else {
        box.disabled = true;
        $("#id_trip_date").html("<option value='-1'>Select a trip first</option>");
    };
}

function setupCustomDatePickers() {
	var date_el = $("#custom_dates");
    date_el.append("<div id='from_date_picker'><p>From Date</p></div><div id='to_date_picker'><p>To Date</p></div><p>Choose your own dates here if you don't want to stick with ours!</p>");

    
    var from_date = $('#id_from_date').val();
    from_date = from_date.split("-");
    from_date = new Date(from_date[0], from_date[1] - 1, from_date[2]);

    var to_date = $('#id_to_date').val();
    to_date = to_date.split("-");
    to_date = new Date(to_date[0], to_date[1] - 1, to_date[2]);

    $('#from_date_picker').datepicker({
        onSelect: setMinDate,
        altFormat: "yy-mm-dd",
        dateFormat: "yy-mm-dd",
        firstDay: 0,
        changeFirstDay: false,
        altField: '#id_from_date'
    });

    $('#to_date_picker').datepicker({
        altFormat: "yy-mm-dd",
        dateFormat: "yy-mm-dd",
        firstDay: 0,
        changeFirstDay: false,
        altField: '#id_to_date'
    });



    $('#to_date_picker').datepicker('setDate', to_date);
    $('#from_date_picker').datepicker('setDate', from_date);

	customDatePickerGenerated = true;
}

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

function hideCustomDates(){
	$('#custom_dates').hide();
}

function showCustomDates() {
    var date_el = $('#custom_dates');
    if (!customDatePickerGenerated) setupCustomDatePickers();
    date_el.show();
}

function setupWhereSelect() {
	var wh = $("#id_where_heard");
	var who = $('#where_heard_other');
	var id_who = $('#id_where_heard_other');
    if (wh.val() != WHERE_HEARD_OTHER) who.hide();

    wh.change(function() {
        if (wh.val() == WHERE_HEARD_OTHER) {
            who.show();
            id_who.focus();
        } else {
            who.hide();
        }
    });
}

function setupDateSelect() {
	
	if (!exists("#id_trip_date")) return;
	
    var datebox = $("#id_trip_date");

    // get the datebox initial value.
    datebox_initial_value = datebox.val();

    

    // add the change listener
    datebox.change(function() {
        if ($(this).val() == OTHER_DATES) {
            showCustomDates();
        } else {
            hideCustomDates();
        }
    });

    datebox.html("<option value='-1'>Select a trip first</option>");
    datebox.disabled = true;

}

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 setupEnquiryForm() {
	$('#to_date').hide();
    $('#from_date').hide();

    setupWhereSelect();
	setupDateSelect();
	setupTripSelect();
}

$(function() {
    setupEnquiryForm();
});