createnamespace('kiosk', 'kiosk.search', 'kiosk.search.form');
kiosk.search.form = function () { this.init(); };
$.extend(kiosk.search.form, {
    formControls: null,

    init: function () {
        // Initialise the search form
        this.initialiseFormControls();
    },

    initialiseFormControls: function () {
        // Initialise the form controls collection
        this.formControls = {
            startDate: $('#checkInCalendar'),
            endDate: $('#checkOutCalendar'),
            destinationDropDown: $('#arrivalGateway'),
            childrenDropDown: $('#children')
          
        };

        this.getLocations();

        // Setup start date calendar
        this.formControls.startDate.datepicker({
            numberOfMonths: 2,
            dateFormat: 'dd M yy',
            minDate: kiosk.search.minDate,
            maxDate: kiosk.search.maxDate,
            
            onClose: function () {
                var startDate = $(this).datepicker('getDate');
                // Update the minimum end date
                if (startDate != null) {
                    kiosk.search.form.formControls.endDate.datepicker('option', 'minDate', new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + 1));
                }

                // Set end date
                kiosk.search.form.formControls.endDate.datepicker("setDate", new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + 7));
            }
        });

        // Setup end date calendar
        this.formControls.endDate.datepicker({
            numberOfMonths: 2,
            dateFormat: 'dd M yy',
            minDate: new Date(kiosk.search.minDate.getFullYear(), kiosk.search.minDate.getMonth(), kiosk.search.minDate.getDate() + 1)
        });

        this.formControls.childrenDropDown.change(function () {
            var childAgeSectionDiv = $("#childAgeSection");
            var childAgeInputDiv = $("#childAgeInput");
            var numChildren = $("#children").val();

            $(childAgeInputDiv).html("");

            if (numChildren > 0) {
                $(childAgeSectionDiv).show();
                var i = 0;
                for (i = 0; i < numChildren; i++) {
                    $(childAgeInputDiv).append("<label for='childAge" + i + "' class='error ValidationError' style='display: none'>Please enter the age of this child (2-17)</label> ");
                    $(childAgeInputDiv).append("<input type='text' id='childAge" + i + "' name='childAge" + i + "' class='required childAge digits error' min='2' max='17' />");
                }
            }
            else {
                $(childAgeSectionDiv).hide();
            }

        });


         

    },

    getLocations: function () {
        this.formControls.destinationDropDown.change(function () {
            var loading = [];
            loading.push({ Text: "loading..." });
            $("#destination").fillSelect(loading);
            $('#FindHotels').attr("disabled", "disabled");
            $('#FindHotels').attr("src", "/_assets/images/buttons/find_hotels_grey.gif");
            $.post("/Home/GetLocations/" + $(this).val(), function (locations, textStatus, jqXHR) {
                if (jqXHR.status == 503) {
                    window.setTimeout(this.getLocations(), 2000);
                }
                else {
                    $("#destination").fillSelect(locations);
                    $('#FindHotels').removeAttr("disabled");
                    $('#FindHotels').attr("src", "/_assets/images/buttons/find_hotels.gif");
                }
            }, 'json');
        });
    }

});
