﻿        //============================================================================
        //-                   Copyright © 2006, 2011 Maxxcom Sas                     -
        //-                           All rights reserved.                           -
        //-                                                                          -
        //-    Questo codice sorgente è protetto da copyright in base alle leggi e   -
        //-    ai trattati internazionali. L'uso non autorizzato, la riproduzione    -
        //-    parziale o totale, l'alterazione, la distribuzione di questo codice   -
        //-    sorgente saranno perseguite in sede penale e civile in ottemperanza   -
        //-    alle leggi esistenti. Sono soggette alle rispettive licenze freeware, -
        //-    GPL, LGPL o di altro tipo, le parti di codice sorgente acquisite da   -
        //-    terze parte anche senza il consenso scritto. In tal caso i nomi degli -
        //-    autori sono riportati a monte delle procedure utilizzate e/o incluse. -
        //-    Eventuali omissioni o errori potranno essere comunicati all'indirizzo -
        //-    email info@maxxcom.it per una pronta correzione e/o integrazione.     -
        //-                                                                          -
        //-    www.maxxcom.it                                                        -
        //============================================================================

        window.onunload = function () {
            //l'hanlder on-load inibisce la Back-Forward Cache su firefox
        }

        jQuery(document).ready(function () {

            $(window).bind('hashchange', function (e) {
                var idx = e.getState("selectedIndex") || "-1";
                var origen = $("#cboFrom :selected").val();
                if (origen.length > 0) {
                    $('#cboTo').html("");
                    $('#cboTo').html(aptList[origen]);
                    document.getElementById('cboTo').options[idx].selected = true;
                }

                var data = e.getState("DataPartenza") || null;
                if (data != null) {
                    DataPartenza.SetText(data);
                }

                data = e.getState("DataRitorno") || null;
                if (data != null) {
                    DataRitorno.SetText(data);
                }

            });

            $('#cboTo').live("change", function () {
                var toIndex = document.getElementById('cboTo').selectedIndex;
                $.bbq.pushState({ selectedIndex: toIndex });
                return false;
            });

            $(window).trigger('hashchange');

        });

        function ChangeDestination() {
            var selected = $("#cboFrom :selected");
            var origen = selected.val();
            if (origen.length > 0) {
                $('#cboTo').html("");
                $('#cboTo').html(aptList[origen]);
                document.getElementById('cboTo').options[0].selected = true;
            }
        }

        window.TravelType = function () {
            var Travelmode = getObject(window.OneWay);
            if (Travelmode.type == "hidden")
                return Travelmode.value;
            if (Travelmode.checked)
                return "OW";
            else
                return "RT";
        }

        window.CheckDepartDate = function (e) {
            var dRitorno = DataRitorno.GetValue();
            if (dRitorno != null) {
                if (e.value <= dRitorno) {
                    DataRitorno.SetIsValid(true);
                }
                else {
                    DataRitorno.SetDate(e.value);
                    DataRitorno.SetIsValid(true);
                    $.bbq.pushState({ DataRitorno: DataRitorno.GetText() });
                }
            }
            $.bbq.pushState({ DataPartenza: DataPartenza.GetText() });
        }

        window.CheckArrivalDate = function (e) {
            var dPartenza = DataPartenza.GetValue();
            if (e.value < dPartenza) {
                alert(oTexts.ErrWrongRetDate);
                return true;
            }
            else {
                $.bbq.pushState({ DataRitorno: DataRitorno.GetText() });
                return false;
            }
        }

        window.DisableRoundTripFields = function () {
            DataRitorno.SetDate(null);
            DataRitorno.SetEnabled(false);
            $.bbq.pushState({ DataRitorno: DataRitorno.GetText() });
        }

        window.EnableRoundTripFields = function () {
            var dRitorno = DataRitorno.GetValue();
            var dPartenza = DataPartenza.GetValue();
            if (dRitorno == null) {
                DataRitorno.SetDate(dPartenza);
                $.bbq.pushState({ DataRitorno: DataRitorno.GetText() });
            }
            if (dRitorno < dPartenza) {
                DataRitorno.SetDate(dPartenza);
                $.bbq.pushState({ DataRitorno: DataRitorno.GetText() });
            }
            DataRitorno.SetEnabled(true);
        }

        window.SetReturnDate = function (s, e) {
            if (TravelType() == "RT") {
                var dRitorno = DataRitorno.GetValue();
                var dPartenza = DataPartenza.GetValue();
                if (dRitorno == null) {
                    DataRitorno.SetDate(dPartenza);
                    DataRitorno.SetIsValid(true);
                    $.bbq.pushState({ DataRitorno: DataRitorno.GetText() });
                }
                if (dRitorno < dPartenza) {
                    DataRitorno.SetDate(dPartenza);
                    DataRitorno.SetIsValid(true);
                    $.bbq.pushState({ DataRitorno: DataRitorno.GetText() });
                }
            }
            return true;
        }

        window.checkDates = function () {

            var dPartenza = DataPartenza.GetValue();
            var dRitorno = DataRitorno.GetValue();

            if (dPartenza == null) {
                alert(oTexts.ErrWrongDepDate);
                return false;
            }

            if (TravelType() == "RT") {
                if (dRitorno == null) {
                    alert(oTexts.ErrWrongRetDate);
                    return false;
                }

                if (dRitorno < dPartenza) {
                    alert(oTexts.ErrWrongRetDate);
                    return false;
                }

            }

            return true;
        }

        window.checkAirports = function () {

            var Departure = $("#cboFrom :selected").val();
            var Arrival = $("#cboTo :selected").val();
            var depIndex = getObject('cboFrom').selectedIndex;
            var arrIndex = getObject('cboTo').selectedIndex;

            if (Departure.length == 0 || depIndex <= 0) {
                alert(oTexts.ErrDeparture);
                return false;
            }
            else if (Arrival.length == 0 || arrIndex <= 0) {
                alert(oTexts.ErrArrival);
                return false;
            }
            else if (Departure == Arrival) {
                alert(oTexts.ErrSameValue);
                return false;
            }
            return true;
        }

        window.checkPassengers = function () {

            var oAdults = getObject(window.Adulti);
            var oChildren = getObject(window.Ragazzi);
            var oInfants = getObject(window.Bambini);

            if (oAdults == null) return false;
            if (oChildren == null) return false;
            if (oInfants == null) return false;

            var adults = oAdults.selectedIndex;
            var children = oChildren.selectedIndex;
            var infants = oInfants.selectedIndex;

            var passengers = adults + children;

            if (infants > 0) {
                if (adults <= 0) {
                    alert(oTexts.ErrInfAlone);
                    return false;
                }
                if (adults < infants) {
                    alert(oTexts.ErrNumInfants);
                    return false;
                }
            }
            if (children > 0) {
                // REMARK QUI PER UMNR
                /*
                if (adults <= 0) 
                {
                alert( oTexts.ErrChildAlone );
                return false;
                }
                */
                // REMARK QUI PER UMNR
            }
            if (adults + children == 0) {
                alert(oTexts.ErrMinPaxes);
                return false;
            }
            if (adults + children + infants > window.MaxSeats) {
                alert(oTexts.ErrMaxPaxes.replace("{0}", window.MaxSeats));
                return false;
            }
            return true;

        }

        window.Validate = function () {

            //controllo date
            if (!checkDates()) {
                return false;
            }

            //controllo aeroporti
            //if (!checkAirports()) 
            //{
            //    return false;
            //};

            //controllo passeggeri
            if (!checkPassengers()) {
                return false;
            }

            return true;
        }

