﻿function trim(s) {
    return s.replace(/^\s+|\s+$/g, '');
}

function comboBoxClearItems(comboBox) {
    var table = document.getElementById(comboBox + "_D_L_D_T");
    while (table.rows.length > 0) {
        table.deleteRow(0);
    }
}

function comboBoxItemAdd(comboBox, item) {
    var table = document.getElementById(comboBox + "_D_L_D_T");

    var newRow = table.insertRow(table.rows.length); //_D_L_D_T
    newRow.className = "ListBoxItemRow";

    newRow.id = comboBox + "_D_L_D_T_Row_" + table.rows.length.toString();

    var newCell = newRow.insertCell(newRow.cells.length);
    newCell.className = "ListBoxItem";
    newCell.id = newRow.id + "_Cell_" + newRow.cells.length.toString();

    newCell.innerHTML = item.toString();

    return newRow;
}

function comboBoxButtonClick(comboBox) {
    var dpw = document.getElementById(comboBox + "_D_PW"); //_D_PW

    if (dpw.style.visibility == "visible") {
        document.getElementById(comboBox + "_I").blur();
    }
    else {
        document.getElementById(comboBox + "_I").focus();
    }
}

function comboBoxShow(comboBox) {
    var dpw = document.getElementById(comboBox + "_D_PW"); //_D_PW
    var dld = document.getElementById(comboBox + "_D_L_D"); // _D_L_D
    var obj = document.getElementById(comboBox);

    dld.style.pixelWidth = obj.style.pixelWidth - 2;
    dpw.style.top = obj.style.bottom;
    dpw.style.left = obj.style.left;

    if (dpw.style.visibility == "visible") {
        dpw.style.visibility = "hidden";
    }
    else {
        dpw.style.visibility = "visible";
    }
}

function comboBoxItemClick(comboBox, e) {
    var i = document.getElementById(comboBox + "_I"); // _L

    var target = e.target ? e.target : e.srcElement;
    if (target && (target.nodeType == 3 || target.nodeType == 4)) {
        target = target.parentNode;
    }

    if (target.firstChild != null && target.firstChild.nodeValue != null && target.firstChild.nodeValue.replace(/\s/g, "") != "") {
        i.value = trim(target.firstChild.nodeValue);

        comboBoxHide(comboBox);
    }
}

function comboBoxHide(comboBox) {
    var dpw = document.getElementById(comboBox + "_D_PW"); // _D_PW

    dpw.style.visibility = "hidden";
}

var selectedRadioButton = "RadioRoundTrip";

function radioButtonClick(s) {
    s.className = "RadioButtonSelected";

    var flights1 = document.getElementById("Flights1");
    var flights2 = document.getElementById("Flights2");

    switch (s.id) {
        case "RadioRoundTrip":
            selectedRadioButton = "RadioRoundTrip";

            document.getElementById("RadioOneWay").className = "RadioButton";
            document.getElementById("RadioMultiDestination").className = "RadioButton";

            flights1.style.display = "";
            flights2.style.display = "none";

            document.getElementById("Flights1Return").style.display = "";

            break;
        case "RadioOneWay":
            selectedRadioButton = "RadioOneWay";

            document.getElementById("RadioRoundTrip").className = "RadioButton";
            document.getElementById("RadioMultiDestination").className = "RadioButton";

            flights1.style.display = "";
            flights2.style.display = "none";

            document.getElementById("Flights1Return").style.display = "none";

            break;
        case "RadioMultiDestination":
            selectedRadioButton = "RadioMultiDestination";

            document.getElementById("RadioRoundTrip").className = "RadioButton";
            document.getElementById("RadioOneWay").className = "RadioButton";

            flights1.style.display = "none";
            flights2.style.display = "";
            break;
    }
}

var checkBoxNonStopFightsOnlyChecked = false;
var checkBoxRefundableOptionChecked = false;
var checkBoxNoDates = false;

function checkBoxClick(s) {
    if (s.id == "CheckBoxNonStopFightsOnly") {
        checkBoxNonStopFightsOnlyChecked = !checkBoxNonStopFightsOnlyChecked;
        s.className = checkBoxNonStopFightsOnlyChecked ? "CheckBoxChecked" : "CheckBox";
    }
    else if (s.id == "CheckBoxRefundableOption") {
        checkBoxRefundableOptionChecked = !checkBoxRefundableOptionChecked;
        s.className = checkBoxRefundableOptionChecked ? "CheckBoxChecked" : "CheckBox";
    }
    else if (s.id == "CheckBoxNoDates") {
        checkBoxNoDates = !checkBoxNoDates;
        s.className = checkBoxNoDates ? "CheckBoxChecked" : "CheckBox";
    }
}

function browserOffset() {
    return navigator.appName == "Netscape" ? 3 : 0;
}

function findPos(obj) {
    var curleft = 0;
    var curtop = 0;

    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }

    var offset = browserOffset();
    return [curleft + offset, curtop + offset];
}

var airportListName;
var editAirport;

function airportsListShow(s) {
    airportListName = s;
    var d = document.getElementById(s);
    var list = document.getElementById("AirportsList");

    if (list.style.display == "block") {
        list.style.display = "none";
    }
    else {
        var pos = findPos(d);

        list.style.left = pos[0].toString() + "px";
        list.style.top = (pos[1] + d.clientHeight + 2).toString() + "px";
        list.style.width = (d.clientWidth < 250 ? "250" : d.clientWidth.toString()) + "px";
        list.style.display = "block";

        window.editAirport = document.getElementById("Edit" + s);

        list.focus();
    }
}

function airportsListHide(name) {
    var list = document.getElementById(name.toString());
    list.style.display = "none";
}

function buildAirportList(s) {
    airportListName = s;
    editAirport = document.getElementById("Edit" + s);

    var strVal = editAirport.value.toLowerCase();
    var list = document.getElementById("AirportsList2");
    var listDoc = window.frames["AirportsList2"].document;

    if (strVal.length > 2) {
        var d = document.getElementById(s);
        var pos = findPos(d);

        list.style.left = pos[0].toString() + "px";
        list.style.top = (pos[1] + d.clientHeight + 2).toString() + "px";

        //search
        foundValues = new Array()
        for (a in a_values) {
            for (i = 0; i < a_values[a].length; i++) {
                str = a_values[a][i].split(":");
                if (str[0].toLowerCase().indexOf(strVal) == 0) {
                    foundValues.unshift(a_values[a][i] + ", " + getCountryAndState(a));
                }
                else if (str[1].toLowerCase().indexOf(strVal) == 0) {
                    foundValues[foundValues.length] = a_values[a][i] + ", " + getCountryAndState(a);
                }
            }
        }

        if (foundValues.length > 0) {
            list.style.display = 'block';
            list.style.height = foundValues.length > 10 ? 170 : foundValues.length * 17;
            listDoc.clear();
            listDoc.write("<body style='font:11px Meiryo;margin:0;padding:0; background-color: C9E8FC;'>");
            for (i = 0; i < foundValues.length; i++) {
                str = foundValues[i].split(":");
                listDoc.write("<div style=\"padding:2;cursor:pointer\" onMouseOver=\"this.style.backgroundColor='70BFF7'\" onMouseOut=\"this.style.backgroundColor='transparent'\" onClick=\"window.parent.editAirport.value = '" + str[0] + "';window.parent.airportsListHide('AirportsList2');\">[" + str[0] + "] " + str[1] + "</div>");
            }
            listDoc.write("</body>");
            listDoc.close();
        }
        else list.style.display = 'none';
    }
    else list.style.display = 'none';
}

function getCountryAndState(sCode) {
    contry_state = sCode.split(":");
    country = "";
    state = contry_state[1];
    for (s in country_values) {
        if (country_values[s].indexOf(contry_state[0]) == 0) {
            country = country_values[s].split(":")[1];
        }
    }
    if (state != "All") return state + ", " + country;
    else return country;
}

function calendarShow(obj, s) {
    cObj = obj;
    cBody = document.getElementById("Calendar");

    var d = document.getElementById(s);
    var pos = findPos(d);

    cBody.style.left = pos[0].toString() + "px";
    cBody.style.top = (pos[1] + d.clientHeight + 2).toString() + "px";

    window.frames["Calendar"].selectDate(obj.value, obj.attributes["minDate"].value, obj.attributes["maxDate"].value);
    cBody.style.display = 'block';
    cObj.select();
}

function calendarHide(obj) {
    if (!document.all || document.activeElement.id == 'Calendar') return;
    window.frames["Calendar"].selectDate(obj.value, obj.attributes["minDate"].value, obj.attributes["maxDate"].value);
    doHideCal(obj);
}

function doHideCal(obj) {
    cBody = document.getElementById("Calendar");
    cBody.style.display = 'none';
    cObj.value = window.frames["Calendar"].selDate;
    if (cObj.attributes["raiseChangeEvent"].value == 'true') {
        calChange(cObj);
    }
}

function calChange(calObj) {
    switch (calObj.name) {
        case "EditFlights1DepartDate":
            document.getElementById("EditFlights1ReturnDate").attributes["minDate"].value = calObj.value;
            break;
        case "flyDate_1":
            if (formObj["flyDate_2"]) {
                formObj["flyDate_2"].attributes["minDate"].value = calObj.value;
            }
            break;
        case "flyDate_2":
            if (formObj["flyDate_3"]) {
                formObj["flyDate_3"].attributes["minDate"].value = calObj.value;
            }
            break;
        case "flyDate_3":
            if (formObj["flyDate_4"]) {
                formObj["flyDate_4"].attributes["minDate"].value = calObj.value;
            }
            break;
        case "flyDate_4":
            if (formObj["flyDate_5"]) {
                formObj["flyDate_5"].attributes["minDate"].value = calObj.value;
            }
            break;
        case "flyDate_5":
            if (formObj["flyDate_6"]) {
                formObj["flyDate_6"].attributes["minDate"].value = calObj.value;
            }
            break;
        case "flyDate_6":
            if (formObj["flyDate_7"]) {
                formObj["flyDate_7"].attributes["minDate"].value = calObj.value;
            }
            break;
        case "flyDate_7":
            if (formObj["flyDate_8"]) {
                formObj["flyDate_8"].attributes["minDate"].value = calObj.value;
            }
            break;
    }
}

