/* Minification failed. Returning unminified contents.
(8,22): run-time error CSS1031: Expected selector, found '='
(8,22): run-time error CSS1025: Expected comma or open brace, found '='
(9,13): run-time error CSS1031: Expected selector, found '='
(9,13): run-time error CSS1025: Expected comma or open brace, found '='
(10,19): run-time error CSS1031: Expected selector, found '='
(10,19): run-time error CSS1025: Expected comma or open brace, found '='
(11,15): run-time error CSS1031: Expected selector, found '='
(11,15): run-time error CSS1025: Expected comma or open brace, found '='
(12,20): run-time error CSS1031: Expected selector, found '='
(12,20): run-time error CSS1025: Expected comma or open brace, found '='
(15,4): run-time error CSS1031: Expected selector, found '('
(15,4): run-time error CSS1025: Expected comma or open brace, found '('
(18,4): run-time error CSS1031: Expected selector, found '('
(18,4): run-time error CSS1025: Expected comma or open brace, found '('
(25,10): run-time error CSS1031: Expected selector, found 'getStorage('
(25,10): run-time error CSS1025: Expected comma or open brace, found 'getStorage('
(33,10): run-time error CSS1031: Expected selector, found 'setStorage('
(33,10): run-time error CSS1025: Expected comma or open brace, found 'setStorage('
(42,10): run-time error CSS1031: Expected selector, found 'setCookie('
(42,10): run-time error CSS1025: Expected comma or open brace, found 'setCookie('
(49,10): run-time error CSS1031: Expected selector, found 'getCookie('
(49,10): run-time error CSS1025: Expected comma or open brace, found 'getCookie('
(63,1): run-time error CSS1019: Unexpected token, found '$'
(63,2): run-time error CSS1019: Unexpected token, found '('
(63,3): run-time error CSS1019: Unexpected token, found ''#departDate''
(63,16): run-time error CSS1019: Unexpected token, found ')'
(63,18): run-time error CSS1030: Expected identifier, found 'datepicker('
(63,18): run-time error CSS1019: Unexpected token, found 'datepicker('
(63,29): run-time error CSS1019: Unexpected token, found '{'
(64,15): run-time error CSS1030: Expected identifier, found ' '
(64,20): run-time error CSS1031: Expected selector, found 'Date('
(64,20): run-time error CSS1025: Expected comma or open brace, found 'Date('
(84,2): run-time error CSS1019: Unexpected token, found ')'
(87,4): run-time error CSS1031: Expected selector, found '('
(87,4): run-time error CSS1025: Expected comma or open brace, found '('
(91,1): run-time error CSS1019: Unexpected token, found '$'
(91,2): run-time error CSS1019: Unexpected token, found '('
(91,3): run-time error CSS1019: Unexpected token, found ''#arriveDate''
(91,16): run-time error CSS1019: Unexpected token, found ')'
(91,18): run-time error CSS1030: Expected identifier, found 'datepicker('
(91,18): run-time error CSS1019: Unexpected token, found 'datepicker('
(91,29): run-time error CSS1019: Unexpected token, found '{'
(92,15): run-time error CSS1030: Expected identifier, found ' '
(92,16): run-time error CSS1031: Expected selector, found ''today''
(92,16): run-time error CSS1025: Expected comma or open brace, found ''today''
(102,2): run-time error CSS1019: Unexpected token, found ')'
(105,4): run-time error CSS1031: Expected selector, found '('
(105,4): run-time error CSS1025: Expected comma or open brace, found '('
(112,1): run-time error CSS1019: Unexpected token, found '$'
(112,2): run-time error CSS1019: Unexpected token, found '('
(112,3): run-time error CSS1019: Unexpected token, found ''#roundTrip''
(112,15): run-time error CSS1019: Unexpected token, found ')'
(112,17): run-time error CSS1030: Expected identifier, found 'click('
(112,17): run-time error CSS1019: Unexpected token, found 'click('
(112,32): run-time error CSS1031: Expected selector, found '('
(112,32): run-time error CSS1025: Expected comma or open brace, found '('
(120,2): run-time error CSS1019: Unexpected token, found ')'
(123,4): run-time error CSS1031: Expected selector, found '('
(123,4): run-time error CSS1025: Expected comma or open brace, found '('
(127,5): run-time error CSS1062: Expected semicolon or closing curly-brace, found '$'
(128,5): run-time error CSS1062: Expected semicolon or closing curly-brace, found '$'
(134,10): run-time error CSS1031: Expected selector, found 'GetAirlines('
(134,10): run-time error CSS1025: Expected comma or open brace, found 'GetAirlines('
(292,10): run-time error CSS1031: Expected selector, found 'checkAirline('
(292,10): run-time error CSS1025: Expected comma or open brace, found 'checkAirline('
(318,9): run-time error CSS1030: Expected identifier, found 'change('
(318,9): run-time error CSS1031: Expected selector, found 'change('
(318,9): run-time error CSS1025: Expected comma or open brace, found 'change('
(362,2): run-time error CSS1019: Unexpected token, found ')'
(367,10): run-time error CSS1031: Expected selector, found 'SetOriginList('
(367,10): run-time error CSS1025: Expected comma or open brace, found 'SetOriginList('
(440,10): run-time error CSS1031: Expected selector, found 'SetDestinationList('
(440,10): run-time error CSS1025: Expected comma or open brace, found 'SetDestinationList('
(474,11): run-time error CSS1030: Expected identifier, found 'change('
(474,11): run-time error CSS1031: Expected selector, found 'change('
(474,11): run-time error CSS1025: Expected comma or open brace, found 'change('
(476,2): run-time error CSS1019: Unexpected token, found ')'
(481,10): run-time error CSS1031: Expected selector, found 'GetAirlineSchedules('
(481,10): run-time error CSS1025: Expected comma or open brace, found 'GetAirlineSchedules('
(548,10): run-time error CSS1031: Expected selector, found 'GetAirlineSchedulesWithVerifycation('
(548,10): run-time error CSS1025: Expected comma or open brace, found 'GetAirlineSchedulesWithVerifycation('
(601,15): run-time error CSS1031: Expected selector, found '='
(601,15): run-time error CSS1025: Expected comma or open brace, found '='
(602,15): run-time error CSS1031: Expected selector, found '='
(602,15): run-time error CSS1025: Expected comma or open brace, found '='
(603,16): run-time error CSS1031: Expected selector, found '='
(603,16): run-time error CSS1025: Expected comma or open brace, found '='
(605,11): run-time error CSS1030: Expected identifier, found 'change('
(605,11): run-time error CSS1031: Expected selector, found 'change('
(605,11): run-time error CSS1025: Expected comma or open brace, found 'change('
(631,2): run-time error CSS1019: Unexpected token, found ')'
(633,1): run-time error CSS1019: Unexpected token, found '$'
(633,2): run-time error CSS1019: Unexpected token, found '('
(633,3): run-time error CSS1019: Unexpected token, found ''#passenger''
(633,15): run-time error CSS1019: Unexpected token, found ')'
(633,17): run-time error CSS1030: Expected identifier, found 'click('
(633,17): run-time error CSS1019: Unexpected token, found 'click('
(633,32): run-time error CSS1031: Expected selector, found '('
(633,32): run-time error CSS1025: Expected comma or open brace, found '('
(639,2): run-time error CSS1019: Unexpected token, found ')'
(641,1): run-time error CSS1019: Unexpected token, found '$'
(641,2): run-time error CSS1019: Unexpected token, found '('
(641,3): run-time error CSS1019: Unexpected token, found ''#doneSelect''
(641,16): run-time error CSS1019: Unexpected token, found ')'
(641,18): run-time error CSS1030: Expected identifier, found 'click('
(641,18): run-time error CSS1019: Unexpected token, found 'click('
(641,33): run-time error CSS1031: Expected selector, found '('
(641,33): run-time error CSS1025: Expected comma or open brace, found '('
(643,2): run-time error CSS1019: Unexpected token, found ')'
(645,1): run-time error CSS1019: Unexpected token, found '$'
(645,2): run-time error CSS1019: Unexpected token, found '('
(645,11): run-time error CSS1031: Expected selector, found ')'
(645,11): run-time error CSS1025: Expected comma or open brace, found ')'
(686,2): run-time error CSS1019: Unexpected token, found ')'
(691,10): run-time error CSS1031: Expected selector, found 'submitBlockOnForm('
(691,10): run-time error CSS1025: Expected comma or open brace, found 'submitBlockOnForm('
(701,1): run-time error CSS1019: Unexpected token, found '$'
(701,2): run-time error CSS1019: Unexpected token, found '('
(701,11): run-time error CSS1031: Expected selector, found ')'
(701,11): run-time error CSS1025: Expected comma or open brace, found ')'
(714,2): run-time error CSS1019: Unexpected token, found ')'
 */
// ============================================
// Airline.js - Fixed Version
// ============================================

// ============================================
// Global Variables
// ============================================
var domesticAirlines = ["QZ", "QG", "GA", "JT", "JTB", "SJ", "TNU", "TN", "XN"];
var airline = $("#airline");
var airlineRoutes = new Array();
var originAir = $("#origin");
var destinationAir = $("#destination");

// Initialize default values
if (typeof window.defaultOriginValue === "undefined" || window.defaultOriginValue === "") {
    window.defaultOriginValue = originAir.val();
}
if (typeof window.defaultDestinationValue === "undefined" || window.defaultDestinationValue === "") {
    window.defaultDestinationValue = destinationAir.val();
}

// ============================================
// Helper Functions
// ============================================
function getStorage(key) {
    try {
        return localStorage.getItem(key);
    } catch (e) {
        return null;
    }
}

function setStorage(key, value) {
    try {
        localStorage.setItem(key, value);
        return true;
    } catch (e) {
        return false;
    }
}

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ') c = c.substring(1);
        if (c.indexOf(name) === 0) return c.substring(name.length, c.length);
    }
    return "";
}

// ============================================
// Date Picker Initialization
// ============================================
$('#departDate').datepicker({
    startDate: new Date(),
    format: "DD, d M yyyy",
    language: 'id'
}).on('changeDate', function (e) {
    var oldDate = new Date(e.date);
    var newDate = new Date(e.date);
    newDate.setDate(oldDate.getDate() + 3);
    var month = newDate.getMonth() + 1;
    if (month < 10) {
        month = '0' + month;
    }
    $("#brangkatFormatted").val(e.format('yyyy-mm-dd'));

    $('#arriveDate').datepicker('remove');
    $('#arriveDate').datepicker({
        startDate: oldDate,
        format: "DD, d M yyyy",
        language: 'id'
    });
    $('#arriveDate').datepicker('setDate', newDate);
});

// Set initial depart date
if ($('#departDate').val() === '') {
    $('#departDate').datepicker('setDate', 'today');
}

$('#arriveDate').datepicker({
    startDate: 'today',
    format: "DD, d M yyyy",
    language: 'id'
}).on('changeDate', function (e) {
    var newDate = new Date(e.date);
    var month = newDate.getMonth() + 1;
    if (month < 10) {
        month = '0' + month;
    }
    $("#kembaliReformatted").val(newDate.getFullYear() + "-" + month + "-" + newDate.getDate());
});

// Set initial return date
if ($('#arriveDate').val() === '') {
    $('#arriveDate').datepicker('setDate', '+3d');
}

// ============================================
// Round Trip Checkbox Handler
// ============================================
$('#roundTrip').click(function () {
    if (this.checked) {
        $('#arriveDate').show();
        $('#roundTrip').val(true);
    } else {
        $('#arriveDate').hide();
        $('#roundTrip').val(false);
    }
});

// Initialize round trip visibility
if ($("#roundTrip").is(':checked')) {
    $('#arriveDate').show();
    $('#roundTrip').val(true);
} else {
    $('#arriveDate').hide();
    $('#roundTrip').val(false);
}

// ============================================
// Get Airlines Function - FIXED VERSION
// ============================================
function GetAirlines(element) {
    console.log("GetAirlines called");

    // Prevent multiple clicks
    if (element && element !== window && element !== document) {
        $(element).attr('onclick', null).off('click');
    }

    var domesticOptions = new Array();
    var interOptions = new Array();
    var dataStorage = getStorage("airlineListInd");

    if (dataStorage && JSON.parse(dataStorage).length > 0) {
        $.blockUI({
            message: '<img alt="loading" src="/Content/images/Loading.gif" /><br/>Sedang mengambil maskapai'
        });

        airline.empty();
        airline.append(new Option("Semua Maskapai", "ALL"));

        var data = JSON.parse(dataStorage);

        for (var i = 0; i < data.length; i++) {
            var item = data[i];
            var newOption = new Option(item.name, item.id);

            if (typeof window.defaultAirlineValue !== "undefined" && item.id === window.defaultAirlineValue) {
                newOption.selected = true;
            }

            if (item.id.toLowerCase() !== "all") {
                var status = "ok";
                for (var ii = 0; ii < domesticAirlines.length; ii++) {
                    if (item.id.toLowerCase() === domesticAirlines[ii].toLowerCase()) {
                        domesticOptions.push(newOption);
                        status = "done";
                        break;
                    }
                }
                if (status === "ok") {
                    interOptions.push(newOption);
                }
            }
        }

        // Append domestic airlines first
        if (domesticOptions.length > 0) {
            for (var i = 0; i < domesticOptions.length; i++) {
                airline.append(domesticOptions[i]);
            }
        }

        // Then append international airlines
        if (interOptions.length > 0) {
            for (var i = 0; i < interOptions.length; i++) {
                airline.append(interOptions[i]);
            }
        }

        $.unblockUI();

        if (typeof window.defaultAirlineValue !== "undefined") {
            airline.change();
        }

        // Trigger check airline
        setTimeout(function () {
            if (window.defaultAirlineValue && window.defaultAirlineValue !== "") {
                checkAirline(window.defaultAirlineValue);
            } else {
                // Set default to ALL if no airline specified
                $("#airline").val("ALL");
                $("#airline").change();
            }
        }, 300);

    } else {
        // Fetch from server
        $.blockUI({
            message: '<img alt="loading" src="/Content/images/Loading.gif" /><br/>Sedang mengambil maskapai untuk pertama kali'
        });

        $.ajax({
            url: "/Airlines/getAirlinesList",
            type: 'POST',
            async: true,
            dataType: 'json',
            data: {},
            cache: true,
            success: function (result) {
                airline.empty();
                airline.append(new Option("Semua Maskapai", "ALL"));

                if (result.length > 0) {
                    setStorage("airlineListInd", JSON.stringify(result));
                }

                for (var i = 0; i < result.length; i++) {
                    var item = result[i];
                    var newOption = new Option(item.name, item.id);

                    if (typeof window.defaultAirlineValue !== "undefined" && item.id === window.defaultAirlineValue) {
                        newOption.selected = true;
                    }

                    if (item.id.toLowerCase() !== "all") {
                        var status = "ok";
                        for (var ii = 0; ii < domesticAirlines.length; ii++) {
                            if (item.id.toLowerCase() === domesticAirlines[ii].toLowerCase()) {
                                domesticOptions.push(newOption);
                                status = "done";
                                break;
                            }
                        }
                        if (status === "ok") {
                            interOptions.push(newOption);
                        }
                    }
                }

                if (domesticOptions.length > 0) {
                    for (var i = 0; i < domesticOptions.length; i++) {
                        airline.append(domesticOptions[i]);
                    }
                }

                if (interOptions.length > 0) {
                    for (var i = 0; i < interOptions.length; i++) {
                        airline.append(interOptions[i]);
                    }
                }

                $.unblockUI();

                if (typeof window.defaultAirlineValue !== "undefined") {
                    airline.change();
                }

                setTimeout(function () {
                    if (window.defaultAirlineValue && window.defaultAirlineValue !== "") {
                        checkAirline(window.defaultAirlineValue);
                    } else {
                        $("#airline").val("ALL");
                        $("#airline").change();
                    }
                }, 300);
            },
            error: function () {
                $.unblockUI();
                console.error("Failed to fetch airlines list");
            }
        });
    }
}

// ============================================
// Check Airline Function - FIXED
// ============================================
function checkAirline(airlineID) {
    console.log("checkAirline called with:", airlineID);

    if (airlineID !== "" && airlineID !== null && airlineID !== "null") {
        var hasValue = false;
        $("#airline > option").each(function () {
            if (this.value === airlineID) {
                hasValue = true;
            }
        });

        if (hasValue === true) {
            $("#airline").val(airlineID);
            $("#airline").change();
            $('#airlineName').val(airline[0][airline[0].selectedIndex].text);
            $('#airlineName').change();
            console.log("Airline set to:", airlineID);
        } else {
            console.warn("Airline ID not found in options:", airlineID);
        }
    }
}

// ============================================
// Airline Change Event
// ============================================
airline.change(function () {
    var airlineValue = $(this).val();
    console.log("Airline changed to:", airlineValue);

    var dataStorage = getStorage(airlineValue);

    if (dataStorage && JSON.parse(dataStorage).length > 0) {
        $.blockUI({
            message: '<img alt="loading" src="/Content/images/Loading.gif" /><br/>Mohon tunggu, sedang mengambil rute ' + airlineValue
        });

        var data = JSON.parse(dataStorage);
        airlineRoutes[airlineValue] = new Array();
        var index = 0;
        for (var i = 0; i < data.length; i++) {
            airlineRoutes[airlineValue][index] = [
                data[i].origin,
                data[i].destination,
                data[i].originName,
                data[i].destinationName
            ];
            index++;
        }
        $.unblockUI();
    }

    SetOriginList();

    // Show/hide flight class for Garuda Indonesia
    if (airlineValue === "GA") {
        if ($("#flightClass").hasClass("hidden")) {
            $("#flightClass").removeClass("hidden");
        }
        if (!$("#flightSearchButton").hasClass("pull-right")) {
            $("#flightSearchButton").addClass("pull-right");
        }
    } else {
        if (!$("#flightClass").hasClass("hidden")) {
            $("#flightClass").addClass("hidden");
        }
        if ($("#flightSearchButton").hasClass("pull-right")) {
            $("#flightSearchButton").removeClass("pull-right");
        }
    }
});

// ============================================
// Set Origin List
// ============================================
function SetOriginList() {
    console.log("SetOriginList called");

    // Store current values
    if (typeof window.defaultOriginValue === "undefined" || window.defaultOriginValue === "") {
        window.defaultOriginValue = originAir.val();
    }

    originAir.empty();

    if (typeof window.defaultDestinationValue === "undefined" || window.defaultDestinationValue === "") {
        window.defaultDestinationValue = destinationAir.val();
    }

    destinationAir.empty();

    var currentAirline = airline.val();

    if (airlineRoutes[currentAirline] !== undefined) {
        var newOption = new Option("Pilih Satu", "", true);
        originAir.append(newOption);

        var tempID = "";
        for (var i = 0; i < airlineRoutes[currentAirline].length; i++) {
            var originID = airlineRoutes[currentAirline][i][0];
            if (tempID !== originID) {
                if ($('#origin option[value="' + originID + '"]').length === 0) {
                    newOption = new Option(
                        airlineRoutes[currentAirline][i][2],
                        airlineRoutes[currentAirline][i][0]
                    );

                    if (typeof window.defaultOriginValue !== "undefined" &&
                        originID === window.defaultOriginValue) {
                        newOption.selected = true;
                    }
                    originAir.append(newOption);
                }
                tempID = originID;
            }
        }

        originAir.change();
        window.defaultOriginValue = "";

    } else {
        var newOption = new Option("Pilih Satu", "", true);
        originAir.append(newOption);
        originAir.change();
    }

    // Check for helper values from server
    if ($("#originhelp").val() !== "") {
        originAir.val($("#originhelp").val());
        originAir.change();
        $("#originhelp").val("");
    }

    if ($("#destinationhelp").val() !== "" && originAir.val() !== "") {
        destinationAir.val($("#destinationhelp").val());
        $("#destinationhelp").val("");
        destinationAir.change();

        // Auto-fetch schedules if all required fields are filled
        if (airline.val() && originAir.val() && destinationAir.val()) {
            GetAirlineSchedules();
        }
    }
}

// ============================================
// Set Destination List
// ============================================
function SetDestinationList(originID) {
    console.log("SetDestinationList called with:", originID);

    destinationAir.empty();
    var newOption = new Option("Pilih Satu", "", true);
    destinationAir.append(newOption);

    var currentAirline = airline.val();

    if (airlineRoutes[currentAirline] !== undefined) {
        for (var i = 0; i < airlineRoutes[currentAirline].length; i++) {
            if (originID === airlineRoutes[currentAirline][i][0]) {
                var destinationID = airlineRoutes[currentAirline][i][1];
                newOption = new Option(
                    airlineRoutes[currentAirline][i][3],
                    airlineRoutes[currentAirline][i][1]
                );

                if (typeof window.defaultDestinationValue !== "undefined" &&
                    destinationID === window.defaultDestinationValue) {
                    newOption.selected = true;
                }
                destinationAir.append(newOption);
            }
        }

        destinationAir.change();
        window.defaultDestinationValue = "";
    }
}

// ============================================
// Origin Change Event
// ============================================
originAir.change(function () {
    SetDestinationList(originAir.val());
});

// ============================================
// Get Airline Schedules
// ============================================
function GetAirlineSchedules() {
    console.log("GetAirlineSchedules called");

    var airlineID = airline.val();
    var tripType = "OneWay";

    var departDate = $("#brangkatFormatted").val();
    var returnDate = $("#kembaliReformatted").val();

    if ($("#roundTrip").is(':checked')) {
        tripType = "RoundTrip";
    } else {
        returnDate = null;
    }

    var paxAdult = $("#ipaxAdult").val();
    var paxChild = $("#ipaxChild").val();
    var paxInfant = $("#ipaxInfant").val();

    var originID = originAir.val();
    var originDetail = $("#origin option:selected").text();
    var destinationID = destinationAir.val();
    var destinationDetail = $("#destination option:selected").text();

    var airlineDetail = airline[0][airline[0].selectedIndex].text;
    var airlineFlightClass = $("#airlineFlightClass").val();

    console.log("Schedule params:", {
        airlineID: airlineID,
        origin: originID,
        destination: destinationID,
        departDate: departDate,
        returnDate: returnDate
    });

    $.blockUI({
        message: '<img alt="loading" src="/Content/images/Loading.gif" /><br/>Sedang mengambil jadwal penerbangan'
    });

    $.post("/Airlines/getAllSchedules", {
        airlineDetail: airlineDetail,
        airlineID: airlineID,
        tripType: tripType,
        origin: originID,
        destination: destinationID,
        departDate: departDate,
        returnDate: returnDate,
        paxAdult: paxAdult,
        paxChild: paxChild,
        paxInfant: paxInfant,
        promoCode: "",
        airlineAccessCode: "",
        originDetail: originDetail,
        destinationDetail: destinationDetail,
        airlineFlightClass: airlineFlightClass
    }, function (result) {
        $("#ScheduleView").html(result);
        $.unblockUI();
    }).fail(function () {
        $.unblockUI();
        alert("Gagal mengambil jadwal. Silakan coba lagi.");
    });
}

// ============================================
// Get Schedules with Verification
// ============================================
function GetAirlineSchedulesWithVerifycation() {
    var airlineID = $("#airlineIDVer").val();
    var tripType = "RoundTrip";

    var departDate = $("#departDateVer").val();
    var returnDate = $("#returnDateVer").val();

    if ($("#tripTypeVer").val() === 'OneWay' || $("#tripTypeVer").val() === '1') {
        tripType = "OneWay";
        returnDate = null;
    }

    var paxAdult = $("#paxAdultVer").val();
    var paxChild = $("#paxChildVer").val();
    var paxInfant = $("#paxInfantVer").val();

    var originID = $("#originIDVer").val();
    var destinationID = $("#destinationIDVer").val();

    var originDetail = $("#originDetail").val();
    var destinationDetail = $("#destinationDetail").val();

    var airlineAccessCode = $("#verifycationInput").val();
    var airlineDetail = $('#airlineName').val();

    $.blockUI({
        message: '<img alt="loading" src="/Content/images/Loading.gif" /><br/>Sedang mengambil jadwal penerbangan'
    });

    $.post("/Airlines/getAllSchedules", {
        airlineDetail: airlineDetail,
        airlineID: airlineID,
        tripType: tripType,
        destinationDetail: destinationDetail,
        originDetail: originDetail,
        origin: originID,
        destination: destinationID,
        departDate: departDate,
        returnDate: returnDate,
        paxAdult: paxAdult,
        paxChild: paxChild,
        paxInfant: paxInfant,
        promoCode: "",
        airlineAccessCode: airlineAccessCode
    }, function (result) {
        $("#ScheduleView").html(result);
        $.unblockUI();
    });
}

// ============================================
// Passenger Counter Handlers
// ============================================
var ipaxAdult = $("#ipaxAdult");
var ipaxChild = $("#ipaxChild");
var ipaxInfant = $("#ipaxInfant");

ipaxAdult.change(function () {
    var adultCount = parseInt($(this).val());

    // Update child options
    var tempChild = ipaxChild.val();
    ipaxChild.empty();
    for (var i = 0; i < 10 - adultCount; i++) {
        var newOption = new Option(i, i);
        if (tempChild == i) {
            newOption.selected = true;
        }
        ipaxChild.append(newOption);
    }
    ipaxChild.change();

    // Update infant options
    var tempInfant = ipaxInfant.val();
    ipaxInfant.empty();
    for (var i = 0; i < adultCount + 1; i++) {
        var newOption = new Option(i, i);
        if (tempInfant == i) {
            newOption.selected = true;
        }
        ipaxInfant.append(newOption);
    }
    ipaxInfant.change();
});

$('#passenger').click(function () {
    if ($('#layer').is(":hidden") === true) {
        $('#layer').attr("hidden", false);
    } else {
        $('#layer').attr("hidden", true);
    }
});

$('#doneSelect').click(function () {
    $('#layer').attr("hidden", true);
});

$(document).on('click', '.number-spinner button', function () {
    var btn = $(this),
        oldValue = btn.closest('.number-spinner').find('input').val().trim(),
        newVal = 0;

    var category = btn.closest('.number-spinner').find('input').attr('id');
    var defaultValue = (category === "ipaxAdult") ? 1 : 0;

    var allow = true;

    var adult = parseInt($('#ipaxAdult').val());
    var child = parseInt($('#ipaxChild').val());
    var baby = parseInt($('#ipaxInfant').val());

    if (btn.attr('data-dir') === 'up') {
        if (adult >= 9 && category === "ipaxAdult") {
            allow = false;
        }
        if (category === "ipaxChild" && child >= 8) {
            allow = false;
        }
        if (category === "ipaxInfant" && baby >= adult) {
            allow = false;
        }

        newVal = (allow) ? parseInt(oldValue) + 1 : oldValue;
    } else {
        newVal = (oldValue > defaultValue) ? parseInt(oldValue) - 1 : defaultValue;
    }

    if (category === "ipaxAdult") {
        adult = newVal;
    } else if (category === "ipaxChild") {
        child = newVal;
    } else {
        baby = newVal;
    }

    $('#passenger').val(adult + " Dewasa, " + child + " Anak, " + baby + " Bayi");

    btn.closest('.number-spinner').find('input').val(newVal);
});

// ============================================
// Form Submit Handler
// ============================================
function submitBlockOnForm() {
    $.blockUI({
        message: '<img alt="loading" src="/Content/images/Loading.gif" /><br/>Mohon tunggu...'
    });
    return true;
}

// ============================================
// Document Ready
// ============================================
$(document).ready(function () {
    console.log("Airline JS loaded");

    // Initialize default values if they exist
    if (typeof window.defaultAirlineValue !== "undefined" && window.defaultAirlineValue !== "") {
        console.log("Default airline value found:", window.defaultAirlineValue);
    }

    // If airline is already selected on page load
    if (airline.val() && airline.val() !== "") {
        console.log("Airline already selected:", airline.val());
        airline.change();
    }
});
