createnamespace('kiosk', 'kiosk.carsearch', 'kiosk.carsearch.form');
kiosk.carsearch.form = function () { this.init(); };
$.extend(kiosk.carsearch.form, {
    formControls: null,

    init: function () {
        // Initialise the search form
        this.initialiseFormControls();
        this.bindEventHandlers();
    },

    unbindEventHandlers: function () {
        $("#Country").unbind("change");
        $("#Location").unbind("change");
        $("#pickupCalendar").unbind("change");
        $("#pickupDepot").unbind("change");
        $("#dropoffDepot").unbind("change");
    },

    bindEventHandlers: function () {
        this.getDataForCountry();
        this.getDataForLocation();
        this.getDataForDate();
        this.getPickupDepotTimes();
        this.getDropoffDepotTimes();
    },

    getDataForCountry: function () {
        $("#Country").change(function () {
            if ($(this).val() != "loading...") {
                var loading = [];
                loading.push({ Text: "loading..." });
                kiosk.carsearch.form.unbindEventHandlers();

                $("#dropoffTime").fillSelect(loading);

                $.post("/Search/GetCarSearchData/?date=" + $("#pickupCalendar").val() + "&country=" + $(this).val(), function (resp, textStatus, jqXHR) {
                    if (jqXHR.status == 503) {
                        window.setTimeout(this.getDropoffDepotTimes(), 2000);
                    }
                    else {
                        // update the form with the json data.

                        $("#Location").fillSelect(resp.Locations);
                        $("#dropoffDepot").fillSelect(resp.Depots);
                        $("#pickupDepot").fillSelect(resp.Depots);
                        $("#pickupTime").fillSelect(resp.PickupDepotOpeningTimes);
                        $("#pickupTime").val(resp.SelectedPickupTime);
                        $("#dropoffTime").fillSelect(resp.DropoffDepotOpeningTimes);
                        $("#dropoffTime").val(resp.SelectedPickupTime);

                        kiosk.carsearch.form.bindEventHandlers();
                    }
                }, 'json');
            }
        });
    },

    getDataForLocation: function () {
        $("#Location").change(function () {
            if ($(this).val() != "loading...") {
                var loading = [];
                loading.push({ Text: "loading..." });
                kiosk.carsearch.form.unbindEventHandlers();

                $("#dropoffDepot").fillSelect(loading);
                $("#pickupDepot").fillSelect(loading);
                $("#dropoffTime").fillSelect(loading);
                $("#pickupTime").fillSelect(loading);
                $.post("/Search/GetCarSearchData/?date=" + $("#pickupCalendar").val() + "&country=" + $("#Country").val() + "&location=" + $(this).val(), function (resp, textStatus, jqXHR) {
                    if (jqXHR.status == 503) {
                        window.setTimeout(this.getDropoffDepotTimes(), 2000);
                    }
                    else {
                        // update the form with the json data.

                        $("#dropoffDepot").fillSelect(resp.Depots);
                        $("#pickupDepot").fillSelect(resp.Depots);
                        $("#pickupTime").fillSelect(resp.PickupDepotOpeningTimes);
                        $("#pickupTime").val(resp.SelectedPickupTime);
                        $("#dropoffTime").fillSelect(resp.DropoffDepotOpeningTimes);
                        $("#dropoffTime").val(resp.SelectedDropoffTime);

                        kiosk.carsearch.form.bindEventHandlers();
                    }
                }, 'json');
            }
        });
    },

    getPickupDepotTimes: function () {
        $("#pickupDepot").change(function () {
            if ($(this).val() != "loading...") {
                var loading = [];
                loading.push({ Text: "loading..." });
                kiosk.carsearch.form.unbindEventHandlers();

                $("#pickupTime").fillSelect(loading);
                $.post("/Search/GetTimesForDepot/" + $(this).val(), function (resp, textStatus, jqXHR) {
                    if (jqXHR.status == 503) {
                        window.setTimeout(this.getPickupDepotTimes(), 2000);
                    }
                    else {
                        $("#pickupTime").fillSelect(resp.DepotOpeningTimes);
                        $("#pickupTime").val(resp.SelectedPickupTime);
                        $("#dropoffDepot").val($("#pickupDepot").val());

                        kiosk.carsearch.form.bindEventHandlers();
                    }
                }, 'json');
            }
        });
    },

    getDropoffDepotTimes: function () {
        $("#dropoffDepot").change(function () {
            if ($(this).val() != "loading...") {
                var loading = [];
                loading.push({ Text: "loading..." });
                kiosk.carsearch.form.unbindEventHandlers();

                $("#dropoffTime").fillSelect(loading);
                $.post("/Search/GetTimesForDepot/" + $(this).val(), function (resp, textStatus, jqXHR) {
                    if (jqXHR.status == 503) {
                        window.setTimeout(this.getDropoffDepotTimes(), 2000);
                    }
                    else {
                        $("#dropoffTime").fillSelect(resp.DepotOpeningTimes);
                        $("#dropoffTime").val(resp.SelectedDropoffTime);

                        kiosk.carsearch.form.bindEventHandlers();
                    }
                }, 'json');
            }
        });
    },


    getDataForDate: function () {
        $("#pickupCalendar").change(function () {
            var loading = [];
            loading.push({ Text: "loading..." });
            var country = $("#Country").val();
            var location = $("#Country").val();
            kiosk.carsearch.form.unbindEventHandlers();

            $("#Country").fillSelect(loading);
            $("#Location").fillSelect(loading);
            $("#dropoffDepot").fillSelect(loading);
            $("#pickupDepot").fillSelect(loading);
            $.post("/Search/GetCarSearchData/?date=" + $(this).val() + "&country=" + country + "&location=" + location, function (resp, textStatus, jqXHR) {
                if (jqXHR.status == 503) {
                    window.setTimeout(this.getDropoffDepotTimes(), 2000);
                }
                else {
                    // update the form with the json data.

                    $("#Country").fillSelect(resp.Countries);
                    $("#Location").fillSelect(resp.Locations);
                    $("#dropoffDepot").fillSelect(resp.Depots);
                    $("#pickupDepot").fillSelect(resp.Depots);
                    $("#pickupTime").fillSelect(resp.PickupDepotOpeningTimes);
                    $("#pickupTime").val(resp.SelectedPickupTime);
                    $("#dropoffTime").fillSelect(resp.DropoffDepotOpeningTimes);
                    $("#dropoffTime").val(resp.SelectedDropoffTime);

                    kiosk.carsearch.form.bindEventHandlers();

                }
            }, 'json');
        });
    },

    initialiseFormControls: function () {
        // Initialise the form controls collection
        this.formControls = {
            pickup: $('#pickupCalendar'),
            dropoff: $('#dropoffCalendar')
        };

        // Setup start date calendar
        this.formControls.pickup.datepicker({
            numberOfMonths: 2,
            dateFormat: 'dd M yy',
            minDate: kiosk.carsearch.minDate,
            maxDate: kiosk.carsearch.maxDate,
            onClose: function () {
                var pickup = $(this).datepicker('getDate');
                // Update the minimum end date
                if (pickup != null) {
                    kiosk.carsearch.form.formControls.dropoff.datepicker('option', 'minDate', new Date(pickup.getFullYear(), pickup.getMonth(), pickup.getDate() + 1));
                }

                // Set end date
                if (pickup)
                    kiosk.carsearch.form.formControls.dropoff.datepicker("setDate", new Date(pickup.getFullYear(), pickup.getMonth(), pickup.getDate() + 7));
            }
        });

        // Setup end date calendar
        var pickup = this.formControls.pickup
        
        if (pickup.datepicker('getDate') != null)
            pickup = new Date(pickup.datepicker('getDate'));
        else
          pickup = new Date();
         
        this.formControls.dropoff.datepicker({
            numberOfMonths: 2,
            dateFormat: 'dd M yy',
            minDate: new Date(pickup.getFullYear(), pickup.getMonth(), pickup.getDate() + 1),
            maxDate: kiosk.carsearch.maxDate
        });

    }





});
