function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
$('#departure_date').datetimepicker({
    format: 'YYYY-MM-DD',
    minDate: moment(),
    maxDate: moment().add(1, 'year'),
    useCurrent: false
});
$('#return_date').datetimepicker({
    format: 'YYYY-MM-DD',
    minDate: moment(),
    maxDate: moment().add(1, 'year'),
    useCurrent: false
});


$("#departure_date").on("dp.change", function (e) {
    $('#return_date').data("DateTimePicker").minDate(e.date);
    $('#return_date').data("DateTimePicker").show();

    var dd = $("#departure_date").val().split('-');
    var dDate = new Date(parseInt(dd[0], 10), parseInt(dd[1], 10) - 1, parseInt(dd[2], 10));

    var rd = $("#return_date").val().split('-');
    var rDate = new Date(parseInt(rd[0], 10), parseInt(rd[1], 10) - 1, parseInt(rd[2], 10));
    if (dDate > rDate) {
        dDate.setDate(dDate.getDate() + 7);
        var month = (dDate.getMonth() + 1);
        var strMonth = month <= 9 ? ("0" + month) : month;
        $("#return_date").val(dDate.getFullYear() + "-" + strMonth + "-" + dDate.getDate());
    }
});


$('#se_departure_date').datetimepicker({
    format: 'YYYY-MM-DD',
    minDate: minDate,
    maxDate: maxDate,
    useCurrent: false

});
$('#se_return_date').datetimepicker({
    format: 'YYYY-MM-DD',
    minDate: minDate,
    maxDate: maxDate,
    useCurrent: false

});
$("#se_departure_date").on("dp.change", function (e) {
    $('#se_return_date').data("DateTimePicker").minDate(e.date);
    $('#se_return_date').data("DateTimePicker").show();

    var dd = $("#se_departure_date").val().split('-');
    var dDate = new Date(parseInt(dd[0], 10), parseInt(dd[1], 10) - 1, parseInt(dd[2], 10));

    var rd = $("#se_return_date").val().split('-');
    var rDate = new Date(parseInt(rd[0], 10), parseInt(rd[1], 10) - 1, parseInt(rd[2], 10));
    if (dDate > rDate) {
        dDate.setDate(dDate.getDate() + 7);
        var month = (dDate.getMonth() + 1);
        var strMonth = month <= 9 ? ("0" + month) : month;
        $("#se_return_date").val(dDate.getFullYear() + "-" + strMonth + "-" + dDate.getDate());
    }
});




function submitForm() {
    var validator = $("#flightSearch").validate({
        showErrors: function () {
            if (this.settings.highlight) {
                for (var i = 0; this.errorList[i]; ++i) {
                    this.settings.highlight.call(this, this.errorList[i].element,
                        this.settings.errorClass, this.settings.validClass);
                }
            }
            if (this.settings.unhighlight) {
                for (var i = 0, elements = this.validElements() ; elements[i]; ++i) {
                    this.settings.unhighlight.call(this, elements[i],
                        this.settings.errorClass, this.settings.validClass);
                }
            }
        },
        rules: {
            fromCity: {
                required: true,
                minlength: 3
            },
            toCity: {
                required: true,
                minlength: 3
            },
            departure_date: {
                required: true,
            },
            return_date: {
                required: {
                    depends: function (element) {
                        if ($('#hfTripType').val() == '2') {
                            return true;
                        } else {
                            return false;
                        }
                    }
                }
            },
            Adult: {
                required: true,
                range: [1, 9]

            },
        },
        errorElement: "span",
        messages: {
            txtFrom: {
                required: "Please enter a valid Origin City.",
                minlength: "Origin City must consist of at least 3 characters"
            },
            txtTo: {
                required: "Please enter a valid Destination City.",
                minlength: "Destination City must consist of at least 3 characters",

            },
            departure_date: {
                required: "Please enter a valid Depart Date.",

            },
            return_date: {
                required: "Please enter a valid Return Date.",
            },
        }
    });

    if (validator.form()) {

        var validationFlag = true;
        var strError = "";
        try {
            if ($("#fromCity").val().trim().length < 3) {
                strError += " Atleast 3 characters required, departure city / airport code!";
                validationFlag = false;
            }
            if ($("#toCity").val().trim().length == 0 || $("#toCity").val().trim().length < 3) {
                strError += " Atleast 3 characters required return city / airport code!";
                validationFlag = false;
            }
            if ($.trim($("#fromCity").val()) == $.trim($("#toCity").val()) && ($.trim($("#toCity").val()).length > 0 || $.trim($("#fromCity").val()).length > 0)) {
                strError += " Source and Destination City can't be same !";
                validationFlag = false;
            }
            if ($("#departure_date").val() == "") {
                validationFlag = false;
                strError += "<p>&raquo; Departure date can't be blank!</p>";
            }

            if ($("#hfTripType").val() == "2") {
                if ($("#return_date").val() == "") {
                    validationFlag = false;
                    strError += "<p>&raquo; Return date can't be blank!</p>";
                }
                else {
                    var dd = $("#departure_date").val().split('-');
                    var dDate = new Date(parseInt(dd[0], 10), parseInt(dd[1], 10) - 1, parseInt(dd[2], 10));

                    var rd = $("#return_date").val().split('-');
                    var rDate = new Date(parseInt(rd[0], 10), parseInt(rd[1], 10) - 1, parseInt(rd[2], 10));
                    if (dDate > rDate) {
                        strError += "<p>&raquo; Returning date always equal or greater than departure date!</p>";
                        validationFlag = false;
                    }
                }
            }

            if (!validationFlag) {
                alert(strError);
            }
        }
        catch (err) {
            alert(err.toString());
            validationFlag = false;
        }
        if (validationFlag == true) {
            setLocalStorage();

            var arrFromCity = $("#fromCity").val().split(',');
            var restFromCity = "";
            for (var i = 1; i < arrFromCity.length; i++) {
                restFromCity += ((i == 1 ? "" : ", ") + arrFromCity[i]);
            }
            var arrFCity = arrFromCity[0].split(')')
            var fromCityName = arrFCity.length >= 2 ? arrFCity[1] : $("#fromCity").val();
            var fromArpName = getAirportCode($("#fromCity").val());

            var arrToCity = $("#toCity").val().split(',');
            var restToCity = "";
            for (var i = 1; i < arrToCity.length; i++) {
                restToCity += ((i == 1 ? "" : ", ") + arrToCity[i]);
            }
            var arrTCity = arrToCity[0].split(')')
            var toCityName = arrTCity.length >= 2 ? arrTCity[1] : $("#toCity").val();
            var toArpName = getAirportCode($("#toCity").val());

            var fromDate = $("#departure_date").val().split('-');
            var fDate = "";
            if (fromDate.length == 3) {
                fDate = months[parseInt(fromDate[1]) - 1] + " " + fromDate[2] + ", " + fromDate[0];
            }
            else {
                fDate = $("#departure_date").val();
            }
            var toDate = $("#return_date").val().split('-');;
            var tDate = "";
            if (toDate.length == 3) {
                tDate = months[parseInt(toDate[1]) - 1] + " " + toDate[2] + ", " + toDate[0];
            }
            else {
                fDate = $("#departure_date").val();
            }
            $("#porgressBarOrg").html("<p>" + fromCityName + "</p><span>" + restFromCity + "</span>");
            $("#porgressBarDest").html("<p>" + toCityName + "</p><span> " + restToCity + "</span>");
            $("#porgressBarDate").html((fDate + " - " + ($('#hfTripType').val() == 1 ? "Oneway" : tDate)));
            $("#oneway_roundTrip_wait1").show();
            $("#oneway_roundTrip_wait2").show();
            $("#Multi_City_wait").hide();
            $("#topDivWebsite").hide();
            $("#SearchingProgressBar").show();
        }
        return validationFlag;
    }
    else
        return false;
}

function se_submitForm() {
    var validationFlag = true;
    var strError = "";
    if ($("#se_departure_date").val() == "") {
        validationFlag = false;
        strError += "Departure date can't be blank!\n\r";
    }

    if ($("#se_hfTripType").val() == "2") {

        if ($("#se_return_date").val() == "") {
            validationFlag = false;
            strError += "Return date can't be blank!\n\r";
        }
        else {
            var dd = $("#se_departure_date").val().split('-');
            var dDate = new Date(parseInt(dd[0], 10), parseInt(dd[1], 10) - 1, parseInt(dd[2], 10));

            var rd = $("#se_return_date").val().split('-');
            var rDate = new Date(parseInt(rd[0], 10), parseInt(rd[1], 10) - 1, parseInt(rd[2], 10));
            if (dDate > rDate) {
                strError += "Returning date always equal or greater than departure date!\n\r";
                validationFlag = false;
            }
        }
    }
    if (validationFlag) {
        var arrFromCity = $("#se_fromCity").val().split(',');
        var restFromCity = "";
        for (var i = 1; i < arrFromCity.length; i++) {
            restFromCity += ((i == 1 ? "" : ", ") + arrFromCity[i]);
        }
        var arrFCity = arrFromCity[0].split(')')
        var fromCityName = arrFCity.length >= 2 ? arrFCity[1] : $("#se_fromCity").val();
        var fromArpName = getAirportCode($("#se_fromCity").val());

        var arrToCity = $("#se_toCity").val().split(',');
        var restToCity = "";
        for (var i = 1; i < arrToCity.length; i++) {
            restToCity += ((i == 1 ? "" : ", ") + arrToCity[i]);
        }
        var arrTCity = arrToCity[0].split(')')
        var toCityName = arrTCity.length >= 2 ? arrTCity[1] : $("#se_toCity").val();
        var toArpName = getAirportCode($("#se_toCity").val());

        var fromDate = $("#se_departure_date").val().split('-');
        var fDate = "";
        if (fromDate.length == 3) {
            fDate = months[parseInt(fromDate[1]) - 1] + " " + fromDate[2] + ", " + fromDate[0];
        }
        else {
            fDate = $("#se_departure_date").val();
        }
        var toDate = $("#se_return_date").val().split('-');;
        var tDate = "";
        if (toDate.length == 3) {
            tDate = months[parseInt(toDate[1]) - 1] + " " + toDate[2] + ", " + toDate[0];
        }
        else {
            fDate = $("#se_departure_date").val();
        }
        $("#porgressBarOrg").html("<p>" + fromCityName + "</p><span>" + restFromCity + "</span>");
        $("#porgressBarDest").html("<p>" + toCityName + "</p><span>"  + restToCity + "</span>");
        $("#porgressBarDate").html((fDate + " - " + ($('#se_hfTripType').val() == 1 ? "Oneway" : tDate)));
        $("#oneway_roundTrip_wait1").show();
        $("#oneway_roundTrip_wait2").show();
        $("#Multi_City_wait").hide();
        $("#topDivWebsite").hide();
        $(".searach_popup").hide();
        $("#SearchingProgressBar").show();
    }
    else {
        alert(strError);
    }
    return validationFlag;
}



function setLocalStorage() {
    
    try {
        var seg = [];
        var s = {
            originAirport: getAirportCode($("#fromCity").val().trim()),
            destinationAirport: getAirportCode($("#toCity").val().trim()),
            originAirportFull: $("#fromCity").val().trim(),
            destinationAirportFull: $("#toCity").val().trim(),
            travelDate: $("#departure_date").val().trim()
        }
        seg.push(s);
        if ($("#hfTripType").val() == "2") {
            var s1 = {
                originAirport: getAirportCode($("#toCity").val().trim()),
                destinationAirport: getAirportCode($("#fromCity").val().trim()),
                originAirportFull: $("#toCity").val().trim(),
                destinationAirportFull: $("#fromCity").val().trim(),
                travelDate: $("#return_date").val().trim()
            }
            seg.push(s1);
        }
        var itin = {
            segment: seg,
            searchDirectFlight: $("#isDirectFlight").is(':checked'),
            tripType: $("#hfTripType").val(),
            adults: $("#Adult").val(),
            child: $("#Child").val(),
            infants: $("#Infant").val(),
            cabinType: $("#Cabin").val(),
            airline: ""
        }
        var itinerary = [];
        itinerary.push(itin)
        if (localStorage.getItem("f11ghtsm0j0898jhf98d") != null) {
            var ldata = JSON.parse(localStorage.getItem("f11ghtsm0j0898jhf98d"));
            var ctr = 0;
            for (var i = 0; i < ldata.length && ctr < 2; i++) {
                if (itin.segment[0].originAirport != ldata[i].segment[0].originAirport || itin.segment[0].destinationAirport != ldata[i].segment[0].destinationAirport || itin.segment[0].travelDate != ldata[i].segment[0].travelDate || itin.tripType != ldata[i].tripType) {
                    itinerary.push(ldata[i]);
                    ctr++;
                }
                else if (itin.tripType == ldata[i].tripType && ldata[i].tripType.tripType == "2") {
                    if (itin.segment[1].travelDate != ldata[i].segment[1].travelDate) {
                        itinerary.push(ldata[i]);
                        ctr++;
                    }
                }
            }
        }
        localStorage.setItem("f11ghtsm0j0898jhf98d", JSON.stringify(itinerary));

    } catch (e) {

    }
}
/*function setSearchEngine() {
  
    try {
        if ($("#HomePageindex").length != 0) {
            if (localStorage.getItem("f11ghtsm0j0898jhf98d") != null) {
                var itinerary = JSON.parse(localStorage.getItem("f11ghtsm0j0898jhf98d"));
                $("#fromCity").val(itinerary[0].segment[0].originAirportFull);
                $("#toCity").val(itinerary[0].segment[0].destinationAirportFull);

                var dd = itinerary[0].segment[0].travelDate.split('/');
                if (dd.length == 1)
                    dd = itinerary[0].segment[0].travelDate.split('-');
                var dDate = new Date(parseInt(dd[0], 10), parseInt(dd[1], 10) - 1, parseInt(dd[2], 10));
                if (minDate < dDate) {
                    $("#departure_date").val(itinerary[0].segment[0].travelDate);
                }
                if (itinerary[0].segment.length == 2) {
                    var dd1 = itinerary[0].segment[1].travelDate.split('/');
                    if (dd1.length == 1)
                        dd1 = itinerary[0].segment[1].travelDate.split('-');
                    var rDate = new Date(parseInt(dd1[0], 10), parseInt(dd1[1], 10) - 1, parseInt(dd1[2], 10));
                    if (dDate < rDate) {
                        $("#return_date").val(itinerary[0].segment[1].travelDate);
                    }
                    else {
                        var rd = $("#departure_date").val().split('/');
                        if (rd.length == 1)
                            rd = $("#departure_date").val().split('-');
                        var dDate = new Date(parseInt(rd[0], 10), parseInt(rd[1], 10) - 1, parseInt(rd[2], 10));
                        dDate.setDate(dDate.getDate() + 7);
                        var month = (dDate.getMonth() + 1)
                        var strMonth = month <= 9 ? ("0" + month) : month;
                        $("#return_date").val(dDate.getFullYear() + "-" + strMonth + "-" + dDate.getDate());
                    }
                }
                else {
                    var rd = $("#departure_date").val().split('/');
                    if (rd.length == 1)
                        rd = $("#departure_date").val().split('-');
                    var dDate = new Date(parseInt(rd[0], 10), parseInt(rd[1], 10) - 1, parseInt(rd[2], 10));
                    dDate.setDate(dDate.getDate() + 7);
                    var month = (dDate.getMonth() + 1)
                    var strMonth = month <= 9 ? ("0" + month) : month;
                    $("#return_date").val(dDate.getFullYear() + "-" + strMonth + "-" + dDate.getDate());
                }
                $("#Adult").val((itinerary[0].adults != null && itinerary[0].adults != undefined) ? itinerary[0].adults : "1");
                $("#Child").val((itinerary[0].child != null && itinerary[0].child != undefined) ? itinerary[0].child : "0");
                $("#Infant").val((itinerary[0].infants != null && itinerary[0].infants != undefined) ? itinerary[0].infants : "0");
                $("#Cabin").val((itinerary[0].cabinType != null && itinerary[0].cabinType != undefined) ? itinerary[0].cabinType : "1");
                $("#hfTripType").val(itinerary[0].tripType);

                ChangeTripType(itinerary[0].tripType);

                
            }
            else {
               
            }
        }
        else {
          
        }

    } catch (e) {
       
    }
}*/
function SearchFlightFromHistory(itin) {
    try {
        if ($("#HomePageindex").length != 0) {
            if (localStorage.getItem("f11ghtsm0j0898jhf98d") != null) {
                var itinerary = JSON.parse(localStorage.getItem("f11ghtsm0j0898jhf98d"));
                $("#fromCity").val(itinerary[itin].segment[0].originAirportFull);
                $("#toCity").val(itinerary[itin].segment[0].destinationAirportFull);

                var dd = itinerary[itin].segment[0].travelDate.split('/');
                if (dd.length == 1)
                    dd = itinerary[itin].segment[0].travelDate.split('-');
                var dDate = new Date(parseInt(dd[0], 10), parseInt(dd[1], 10) - 1, parseInt(dd[2], 10));
                if (minDate < dDate) {
                    $("#departure_date").val(itinerary[itin].segment[0].travelDate);
                }
                if (itinerary[itin].segment.length == 2) {
                    var dd1 = itinerary[itin].segment[1].travelDate.split('/');
                    if (dd1.length == 1)
                        dd1 = itinerary[itin].segment[1].travelDate.split('-');
                    var rDate = new Date(parseInt(dd1[0], 10), parseInt(dd1[1], 10) - 1, parseInt(dd1[2], 10));
                    if (dDate < rDate) {
                        $("#return_date").val(itinerary[itin].segment[1].travelDate);
                    }
                    else {
                        var rd = $("#departure_date").val().split('/');
                        if (rd.length == 1)
                            rd = $("#departure_date").val().split('-');
                        var dDate = new Date(parseInt(rd[0], 10), parseInt(rd[1], 10) - 1, parseInt(rd[2], 10));
                        dDate.setDate(dDate.getDate() + 7);
                        var month = (dDate.getMonth() + 1)
                        var strMonth = month <= 9 ? ("0" + month) : month;
                        $("#return_date").val(dDate.getFullYear() + "-" + strMonth + "-" + dDate.getDate());
                    }
                }
                else {
                    var rd = $("#departure_date").val().split('/');
                    if (rd.length == 1)
                        rd = $("#departure_date").val().split('-');
                    var dDate = new Date(parseInt(rd[0], 10), parseInt(rd[1], 10) - 1, parseInt(rd[2], 10));
                    dDate.setDate(dDate.getDate() + 7);
                    var month = (dDate.getMonth() + 1)
                    var strMonth = month <= 9 ? ("0" + month) : month;
                    $("#return_date").val(dDate.getFullYear() + "-" + strMonth + "-" + dDate.getDate());
                }

                $("#Adult").val((itinerary[itin].adults != null && itinerary[itin].adults != undefined) ? itinerary[itin].adults : "1");
                $("#Child").val((itinerary[itin].child != null && itinerary[itin].child != undefined) ? itinerary[itin].child : "0");
                $("#Infant").val((itinerary[itin].infants != null && itinerary[itin].infants != undefined) ? itinerary[itin].infants : "0");
                $("#Cabin").val((itinerary[itin].cabinType != null && itinerary[itin].cabinType != undefined) ? itinerary[itin].cabinType : "1");
                $("#hfTripType").val(itinerary[itin].tripType);

                var arrFromCity = $("#fromCity").val().split(',');
                var restFromCity = "";
                for (var i = 1; i < arrFromCity.length; i++) {
                    restFromCity += ((i == 1 ? "" : ", ") + arrFromCity[i]);
                }
                var arrFCity = arrFromCity[0].split(')')
                var fromCityName = arrFCity.length >= 2 ? arrFCity[1] : $("#fromCity").val();
                var fromArpName = getAirportCode($("#fromCity").val());

                var arrToCity = $("#toCity").val().split(',');
                var restToCity = "";
                for (var i = 1; i < arrToCity.length; i++) {
                    restToCity += ((i == 1 ? "" : ", ") + arrToCity[i]);
                }
                var arrTCity = arrToCity[0].split(')')
                var toCityName = arrTCity.length >= 2 ? arrTCity[1] : $("#toCity").val();
                var toArpName = getAirportCode($("#toCity").val());

                var fromDate = $("#departure_date").val().split('-');
                var fDate = "";
                if (fromDate.length == 3) {
                    fDate = months[parseInt(fromDate[1]) - 1] + " " + fromDate[2] + ", " + fromDate[0];
                }
                else {
                    fDate = $("#departure_date").val();
                }
                var toDate = $("#return_date").val().split('-');;
                var tDate = "";
                if (toDate.length == 3) {
                    tDate = months[parseInt(toDate[1]) - 1] + " " + toDate[2] + ", " + toDate[0];
                }
                else {
                    fDate = $("#departure_date").val();
                }
                $("#porgressBarOrg").html("<p>" + fromCityName + "</p><span>" + restFromCity + "</span>");
                $("#porgressBarDest").html("<p>" + toCityName + "</p><span>" + restToCity + "</span>");
                $("#porgressBarDate").html((fDate + " - " + ($('#hfTripType').val() == 1 ? "Oneway" : tDate)));
                $("#oneway_roundTrip_wait1").show();
                $("#oneway_roundTrip_wait2").show();
             
                $("#topDivWebsite").hide();
                $("#SearchingProgressBar").show();
                $("#flightSearch").submit();
            }
        }
    } catch (e) {
        $(".searchpop1").hide();
    }
}


function ChangeTripType(ddlType) {

    if (ddlType == "1") {

        $('#rt').removeClass('selected-menu');
        $('#mc').removeClass('selected-menu');
        $('#ow').addClass('selected-menu');
        $('#hfTripType').val(1);
        document.getElementById("return_date").disabled = true;
        $('#RoundTrip_OneWay').show();
        $('#MultiCity').hide();
    }
    else if (ddlType == "2") {
        $('#rt').addClass('selected-menu');
        $('#mc').removeClass('selected-menu');
        $('#ow').removeClass('selected-menu');
        $('#hfTripType').val(2);
        document.getElementById("return_date").disabled = false;
        $('#RoundTrip_OneWay').show();
        $('#MultiCity').hide();
    }
    else if (ddlType == "3") {
        $('#rt').removeClass('selected-menu');
        $('#ow').removeClass('selected-menu');
        $('#mc').addClass('selected-menu');
        $('#hfTripType').val(3);
        $('#RoundTrip_OneWay').hide();
        $('#MultiCity').show();
    }
    else {
        $('#rt').addClass('selected-menu');
        $('#ow').removeClass('selected-menu');
        $('#mc').removeClass('selected-menu');
        $('#hfTripType').val(2);
        document.getElementById("return_date").disabled = false;
        $('#RoundTrip_OneWay').show();
        $('#MultiCity').hide();
    }

}
function ddlTypejourneyChange(ddlType) {
    if (ddlType == "1") {
        $('#ow').removeClass('selected-menu');
        $('#rt').removeClass('selected-menu');
        $('#rt').removeClass('activeTab');
        $('#ow').addClass('activeTab');
        $('#hfTripType').val(1);
        document.getElementById("return_date").disabled = true;
    }
    else if (ddlType == "2") {
        $('#ow').removeClass('selected-menu');
        $('#rt').removeClass('selected-menu');
        $('#rt').addClass('activeTab');
        $('#ow').removeClass('activeTab');
        $('#hfTripType').val(2);
        document.getElementById("return_date").disabled = false;
    }
    else {
        $('#ow').removeClass('selected-menu');
        $('#rt').removeClass('selected-menu');
        $('#rt').addClass('active');
        $('#ow').removeClass('activeTab');
        $('#hfTripType').val(2);
        document.getElementById("return_date").disabled = false;
    }
}
var orgIndex = 0;
var destIndex = 0;
var selectFrom = false;
var selectTo = false;
function monkeyPatchAutocomplete() {
    var oldFn = $.ui.autocomplete.prototype._renderItem;
    $.ui.autocomplete.prototype._renderItem = function (ul, item) {
        var re = new RegExp("^" + this.term, "i");
        var t = item.label.replace(re, "<span>" + this.term + "</span>");
        return $("<li></li>").data("item.autocomplete", item).append("<a>" + t + "</a>").appendTo(ul)
    }
}

$(document).ready(function () {

    monkeyPatchAutocomplete();
    var actionUrl = '';

    $('#fromCity').autocomplete({
        selectFirst: true,
        highlight: true,
        source: function (request, response) {
            var url = actionUrl + "/" + request.term;
            $.ajax({
                url: url,
                type: 'GET',
                success: function (data) {
                    orgIndex = 0;
                    response($.map(data, function (item) {
                        return {
                            label: ("(" + item.airportCode + ") " + item.cityName + ", " + item.airportName + (item.stateName == "" ? "" : (", " + item.stateName)) + ", " + item.countryName)
                        }
                    }));
                }
            })
        },
        open: function (event, ui) { selectFrom = true; },
        select: function (event, ui) {
            $(this).val(ui.item.label);
            orgIndex = 1;
            selectFrom = false;
            return false;
        },
        close: function (event, ui) {
            selectFrom = false;
        },
        minLength: 2,
        autoFocus: true
    }).blur(function () {
        if (selectFrom) {
            $("#fromCity").val($('ul.ui-autocomplete li:first a').text());
            orgIndex = 1;
        }
    });

    $("#toCity").autocomplete({
        selectFirst: true,
        source: function (request, response) {
            var url = actionUrl + "/" + request.term;
            $.ajax({
                url: url,
                type: 'GET',
                success: function (data) {
                    destIndex = 0;
                    response($.map(data, function (item) {
                        return {
                            label: ("(" + item.airportCode + ") " + item.cityName + ", " + item.airportName + (item.stateName == "" ? "" : (", " + item.stateName)) + ", " + item.countryName)
                        }
                    }));
                }
            })
        },
        open: function (event, ui) {
            selectTo = true;
        },
        select: function (event, ui) {
            $(this).val(ui.item.label);
            destIndex = 1;
            selectTo = false;
            return false;
        },
        close: function (event, ui) {
            selectTo = false;
        },
        minLength: 2,
        autoFocus: true
    }).blur(function () {
        if (selectTo) {
            $("#toCity").val($('ul.ui-autocomplete li:first a').text());
            destIndex = 1;
        }
    });

});


$(document).ready(function () {
    //setSearchEngine();
    $("#txtPaxDetail").click(function () {
        $(".passenger-div").slideDown();
    });
    $(".PaxDropArrow").click(function () {
        $(".passenger-div").slideDown();
    });
    $(".closePaxDiv").click(function () {
        $(".passenger-div").slideUp();
    });


    $("#se_txtPaxDetail").click(function () {
        $(".passenger-div2").slideDown();
    });
    $("#travellerDiv2").click(function () {
        $(".passenger-div").slideDown();
    });
    $(".closePaxDiv2").click(function () {
        $(".passenger-div2").slideUp();
    }); 

    $('body').click(function (event) {
        if ($("#travellerDiv").length > 0) {
            if (!$('#travellerDiv').is(':hover')) {
                $('.passenger-div').hide();
            }
        }
        if ($("#travellerDiv2").length > 0) {
            if (!$('#travellerDiv2').is(':hover')) {
                $('.passenger-div2').hide();
            }
        }       
    });

    $("#fromCity").blur(function () {
        if (orgIndex == 0 && selectFrom)
            $('#fromCity').val("");
    });
    $("#toCity").blur(function () {
        if (destIndex == 0 && selectTo)
            $('#toCity').val("");
    });
    $(".close_btn").click(function () {
        $(".searach_popup").hide();
    });



   
});

function MinusPax(pax) {
    var adult = parseInt($("#Adult").val());
    var child = parseInt($("#Child").val());
    var infant = parseInt($("#Infant").val());
    var InfantWs = parseInt($("#InfantWs").val());
    var totpax = adult + child + infant + InfantWs;

    if (pax == "Adult") {
        if (adult > 1) {
            if ((adult * 2) == child) {
                alert("Please decrease child first!!");
            }
            else if (adult == (infant + InfantWs)) {
                alert("Please decrease infant first!!");
            }
            else {
                $("#" + pax).val(adult - 1);
            }
        }
    }
    else if (pax == "Child") {
        if (child > 0) {
            $("#" + pax).val(child - 1);
        }
    }
    else if (pax == "Infant") {
        if (infant > 0) {
            $("#" + pax).val(infant - 1);
        }
    }
    else if (pax == "InfantWs") {
        if (infant > 0) {
            $("#" + pax).val(infant - 1);
        }
    }
    setPaxDropBox();
}
function PlusPax(pax) {
    var adult = parseInt($("#Adult").val());
    var child = parseInt($("#Child").val());
    var infant = parseInt($("#Infant").val());
    var InfantWs = parseInt($("#InfantWs").val());
    var totpax = adult + child + infant + InfantWs;
    if (totpax <= 8) {
        if (pax == "Adult") {
            $("#" + pax).val((adult + 1).toString());
        }
        else if (pax == "Child") {
            if (child < (adult * 2)) {
                $("#" + pax).val((child + 1).toString());
            }
        }
        else if (pax == "Infant") {
            if ((infant + InfantWs) < (adult)) {
                $("#" + pax).val((infant + 1).toString());
            }
        }
    }
    setPaxDropBox();
}
function setPaxDropBox() {
    var adult = parseInt($("#Adult").val());
    var child = parseInt($("#Child").val());
    var infant = parseInt($("#Infant").val());
    var InfantWs = parseInt($("#InfantWs").val());
    var totpax = adult + child + infant + InfantWs;

    $("#txtPaxDetail").val(totpax.toString() + " Traveler / " + $("#Cabin option:selected").text());
}




function se_MinusPax(pax) {
    var adult = parseInt($("#se_Adult").val());
    var child = parseInt($("#se_Child").val());
    var infant = parseInt($("#se_Infant").val());
    var InfantWs = parseInt($("#se_InfantWs").val());
    var totpax = adult + child + infant + InfantWs;

    if (pax == "se_Adult") {
        if (adult > 1) {
            if ((adult * 2) == child) {
                alert("Please decrease child first!!");
            }
            else if (adult == (infant + InfantWs)) {
                alert("Please decrease infant first!!");
            }
            else {
                $("#" + pax).val(adult - 1);
            }
        }
    }
    else if (pax == "se_Child") {
        if (child > 0) {
            $("#" + pax).val(child - 1);
        }
    }
    else if (pax == "se_Infant") {
        if (infant > 0) {
            $("#" + pax).val(infant - 1);
        }
    }
    else if (pax == "se_InfantWs") {
        if (infant > 0) {
            $("#" + pax).val(infant - 1);
        }
    }
    se_setPaxDropBox();
}
function se_PlusPax(pax) {
    var adult = parseInt($("#se_Adult").val());
    var child = parseInt($("#se_Child").val());
    var infant = parseInt($("#se_Infant").val());
    var InfantWs = parseInt($("#se_InfantWs").val());
    var totpax = adult + child + infant + InfantWs;
    if (totpax <= 8) {
        if (pax == "se_Adult") {
            $("#" + pax).val((adult + 1).toString());
        }
        else if (pax == "se_Child") {
            if (child < (adult * 2)) {
                $("#" + pax).val((child + 1).toString());
            }
        }
        else if (pax == "se_Infant") {
            if ((infant + InfantWs) < (adult)) {
                $("#" + pax).val((infant + 1).toString());
            }
        }
    }
    se_setPaxDropBox();
}
function se_setPaxDropBox() {
    var adult = parseInt($("#se_Adult").val());
    var child = parseInt($("#se_Child").val());
    var infant = parseInt($("#se_Infant").val());
    var InfantWs = parseInt($("#se_InfantWs").val());
    var totpax = adult + child + infant + InfantWs;

    $("#se_txtPaxDetail").val(totpax.toString() + " Traveler / " + $("#se_Cabin option:selected").text());
}






