﻿var searchFlightHeader = new function () {
    var myBase = this;
    var baseAirport =
    { 'PAR': 'Paris, FRANCE [PAR]', 'LYS': 'Lyon, FRANCE [LYS]', 'MRS': 'Marseille, FRANCE [MRS]',
        'NYC': 'New York, ETATS UNIS D\'AMERIQUE [NYC]', 'BKK': 'Bangkok, THAÏLANDE [BKK]',
        'RUN': 'Saint Denis de la Réunion, RÉUNION [RUN]'
    }
    this.departureAirportCombobox = null;
    this.outboundDatePicker = null;
    this.returnDatePicker = null;
    this.returnDatePanel = null;
    this.submitButton = null;
    this.waitPanel = null;

    this.pickAirport = function (sender, direction, iata) {
        var combo = null;
        if (direction == 0) combo = this.departureAirportCombobox;
        else combo = this.destinationAirportCombobox;
        performPickAirport(combo, iata);
        sender.blur();
    }

    function performPickAirport(combo, iata) {
        var comboItem = new Telerik.Web.UI.RadComboBoxItem();
        comboItem.set_text(baseAirport[iata]);
        comboItem.set_value(iata);
        combo.clearItems();
        combo.trackChanges();
        combo.get_items().add(comboItem);
        comboItem.select();
        combo.commitChanges();
    }

    this.departure_onClientItemsRequesting = function (sender, args) {
        performRequesting(args.get_context(), 0);
    }

    this.destination_onClientItemsRequesting = function (sender, args) {
        performRequesting(args.get_context(), 1);
    }

    this.radCalendar_onValueChanged = function (sender, e) {
        if (sender.isEmpty()) return;

        var obj0 = myBase.outboundDatePicker; // Date de départ
        var obj1 = myBase.returnDatePicker; // Date de retour
        var date1 = new Date(obj0.get_selectedDate());
        var date2 = new Date(obj1.get_selectedDate());
        var isOutboundCalendarEvent = (sender == obj0.get_dateInput());

        if (date1 > date2) { // La date de départ est plus grand que la date de retour
            if (isOutboundCalendarEvent) obj1.set_selectedDate(myBase._addDays(date1, 7));
            else obj0.set_selectedDate(date2);
        }
    }

    this.radCalendar_onBlur = function (sender, e) {
        if (sender.isEmpty()) {
            alert('Attention : Vous devez saisir une date dans le calendrier !');
            sender.set_selectedDate(sender.get_minDate());
        }
    }

    this.tripType_onChange = function (sender, isOneWay) {
        this.returnDatePanel.style.visibility = (isOneWay ? 'hidden' : '');
        sender.blur();
    }

    function performRequesting(context, direction) {
        context['direction'] = direction;
    }

    this.radComboBox_onFailed = function (sender, e) {
        var msg = 'Une erreur c\'est produite lors de la recherche des villes !\n';
        msg = msg + 'Le webmaster a été prévenu de cet incident et vous présentes ses excuses pour la gêne occasionnée.\n';
        msg = msg + 'Revenez d\'ici quelques instants.';
        alert(msg);
    }

    this.onClientDropDownClosed = function (sender, e) {
        if (e.get_domEvent().keyCode == 13) {
            if (sender.get_highlightedItem() == null) sender.clearSelection();
        } else if (sender.get_selectedItem() == null) sender.clearSelection();

        sender.clearItems();
        if (e.get_domEvent().stopPropagation) e.get_domEvent().stopPropagation();
    }


    /* Région : Procédures privées */
    this._addDays = function (o, d) {
        o.setDate(o.getDate() + d); return o;
    }

    this.validateForm = function (validationGroup) {
        var isPageValid = null;
        if (typeof (validationGroup) != 'undefined') isPageValid = Page_ClientValidate(validationGroup);
        else isPageValid = Page_ClientValidate();
        if (isPageValid) {
            this.submitButton.style.display = 'none';
            this.waitPanel.style.display = 'block';
        } else this.submitButton.blur();
    }
}
