﻿//根据航程类型 显示不同类型的查询城市和查询时间
function changeFlyType(oInput) {
    var flyType = oInput.value;
    document.getElementById("hfFlyType").value = flyType; //存放选择的航段类型
    var liTransfer = document.getElementById("liTransfer"); //中转城市li
    var labFirstDate = document.getElementById("labFirstDate"); //一程时间li
    var liSecondDate = document.getElementById("liSecondDate"); //二程时间li
    var labSecondDate = document.getElementById("labSecondDate"); //二程时间显示名称的label

    if (flyType == 0) {//单程
        liTransfer.style.display = "none";
        liSecondDate.style.display = "none";
        labFirstDate.innerHTML = "出发时间：";
    }
    else if (flyType == 1) {//往返
        liTransfer.style.display = "none";
        liSecondDate.style.display = "";
        labFirstDate.innerHTML = "去程时间：";
        liSecondDate.style.display = "";
        labSecondDate.innerHTML = "返程时间：";
    }
    else { //多段
        liTransfer.style.display = "";
        liSecondDate.style.display = "";
        labFirstDate.innerHTML = "一程时间：";
        liSecondDate.style.display = "";
        labSecondDate.innerHTML = "二程时间：";
    }
}

//在查询按钮下方显示提示框
function ShowMsg(oMsg) {
    var ulMsg = document.getElementById("ulMsg");
    var spanMst = document.getElementById("spanMsg");
    ulMsg.style.display = "";
    spanMsg.innerHTML = oMsg;
    setTimeout("ulMsg.style.display = \"none\"", 3000); //2秒钟自动隐藏
}

//检查查询信息
function checkSearchInfo(oBtn) {
    var fromCity = document.getElementById("fromCity");
    var toCity = document.getElementById("toCity");
    var transferCity = document.getElementById("transferCity");
    var firstDate = document.getElementById("firstDate");
    var secondDate = document.getElementById("secondDate");
    var selAirCompany = document.getElementById("selAirCompany");
    var flyType = document.getElementById("hfFlyType");
    if (fromCity.value == "" || fromCity.value == "中文/拼音/代码") {
        ShowMsg("出发城市不能为空");
        return;
    }
    if (toCity.value == "" || toCity.value == "中文/拼音/代码") {
        ShowMsg("到达城市不能为空");
        return;
    }
    var tempDt = new Date();
    var strToday = DateDemo(addDate(tempDt, 0)); // 今天日期

    if (flyType.value == 0) {//单程查询
        if (fromCity.value == toCity.value) {
            ShowMsg("出发城市和到达城市不能相同");
            return;
        }
        if (firstDate.value == "") {
            ShowMsg("起飞时间不能为空");
            firstDate.focus();
            return;
        }
        if (dateDiff(firstDate.value, strToday) < 0) {
            ShowMsg("起飞时间不能早于当天");
            firstDate.focus();
            return;
        }
    }
    else if (flyType.value == 1) {//往返查询
        if (fromCity.value == toCity.value) {
            ShowMsg("出发城市和到达城市不能相同");
            return;
        }
        if (firstDate.value == "") {
            ShowMsg("去程时间不能为空");
            firstDate.focus();
            return;
        }
        if (secondDate.value == "") {
            ShowMsg("返程时间不能为空");
            firstDate.focus();
            return;
        }
        if (dateDiff(firstDate.value, secondDate.value) == 1) {
            ShowMsg("去程时间不能晚于返程日期");
            return;
        }
        if (dateDiff(firstDate.value, strToday) < 0) {
            ShowMsg("去程时间不能早于当天日期");
            return;
        }
        if (dateDiff(secondDate.value, strToday) < 0) {
            ShowMsg("返程时间不能早于当天日期");
            return;
        }
    } else if (flyType.value == 2) { //多段
        if (transferCity.value == "" || transferCity == "中文/拼音/代码") {
            ShowMsg("中转城市不能为空");
            return;
        }
        if (fromCity.value == transferCity.value) {
            ShowMsg("出发城市和中转城市不能相同");
            return;
        }
        if (toCity.value == transferCity.value) {
            ShowMsg("到达城市和中转城市不能相同");
            return;
        }
        if (firstDate.value == "") {
            ShowMsg("一程时间不能为空");
            firstDate.focus();
            return;
        }
        if (secondDate.value == "") {
            ShowMsg("二程时间不能为空");
            firstDate.focus();
            return;
        }
        if (dateDiff(firstDate.value, secondDate.value) == 1) {
            ShowMsg("二程时间不能早于一程日期");
            return;
        }
        if (dateDiff(firstDate.value, strToday) < 0) {
            ShowMsg("一程时间不能早于当天日期");
            return;
        }
        if (dateDiff(secondDate.value, strToday) < 0) {
            ShowMsg("二程时间不能早于当天日期");
            return;
        }
    }
    //数据验证完成后 提交到查询结果页面
    var oForm = document.forms[0];
    oForm.method = "post";
    oForm.action = "/CommonPage/Flights.aspx?action=search";
    oBtn.disabled = true;
    oForm.submit();
}

//对查询出来的航班重新 排序========================================================start
function sortBySelItem(oSelect) {
    var oIndex = oSelect.id.split("-")[1];
    var sb = new StringBuilder("");
    var temDivs = document.getElementsByTagName("div");
    var tempAirDivs = new Array();
    for (var j = 0; j < temDivs.length; j++) {
        if (temDivs[j].id.indexOf("divSortAirInfo-" + oIndex) != -1) {
            tempAirDivs.push(temDivs[j]);
        }
    }
    if (tempAirDivs.length > 0) { //如果有航班，排序
        var tempIntArr = new Array();
        for (var i = 0; i < tempAirDivs.length; i++) {
            tempIntArr[i] = new Array();

            if (oSelect.value == 0)  // 按价格排序
                tempIntArr[i].push(parseInt(tempAirDivs[i].getAttribute("sortPrice"), 10)); //自定义属性 sortPrice
            else if (oSelect.value == 1) //按时间
                tempIntArr[i].push(parseInt(tempAirDivs[i].getAttribute("sortStartTime"), 10)); //自定义属性 sortStartTime
            else if (oSelect.value == 2) //按航空公司(中文)排序
                tempIntArr[i].push(tempAirDivs[i].getAttribute("sortAirCompany")); //自定义属性 sortAirCompan++y

            tempIntArr[i].push(tempAirDivs[i].outerHTML); // 二维数组
        }
        if (oSelect.selectedIndex != 2) {
            bubbleSort(tempIntArr);
        }
        else { //按汉字排序
            var oLen = tempIntArr.length;
            for (var kk = 0; kk < oLen; kk++) {
                for (var jj = kk + 1; jj < oLen; jj++) {
                    if (String(tempIntArr[kk][0]).localeCompare(String(tempIntArr[jj][0])) > 0) {
                        var tmpArr = tempIntArr[kk];
                        tempIntArr[kk] = tempIntArr[jj];
                        tempIntArr[jj] = tmpArr;
                    }
                }
            }
        }
        for (var ii = 0; ii < tempIntArr.length; ii++) {
            sb.Append(tempIntArr[ii][1]);
        }

        $("divAv-" + oIndex).innerHTML = "";
        $("divAv-" + oIndex).innerHTML = sb.toString();
    }
}
//自定义冒泡排序
function bubbleSort(oArray) {
    for (var i = oArray.length; i > 0; i--) {
        for (var j = 0; j < i - 1; j++) {
            if (parseInt(oArray[j][0]) > parseInt(oArray[j + 1][0])) {
                var temp = oArray[j];
                oArray[j] = oArray[j + 1];
                oArray[j + 1] = temp;
            }
        }
    }
    return oArray;
}
//排序========================================================end

//显示退改签层=================================start
function showGaiQian(oSpan, oNum, ev) {
    closeGQDiv();
    showGaiQianDiv(ev);
    $("GQContent").innerHTML = "";
    $("GQContent").innerHTML = "数据获取中，请稍候...";
    var headTitle, sendStr, oAction;
    if (parseInt(oNum) == 0) {//pat查询价格
        oAction = "pat";
        sendStr = oSpan.id.split("-")[2].toString();
        headTitle = "【" + sendStr.split("/")[1] + "舱价格详细】";
        AjaxService.GetPat(sendStr, onSuccess, onFailed);
    }
    else if (parseInt(oNum) == 1) {//退改签规定
        oAction = "gaiQian";
        headTitle = "【退改签规定详细】";
        var airCompay = oSpan.id.split("-")[2].toString();
        var cabin = oSpan.id.split("-")[3].toString();
        AjaxService.GetCabinRemark(airCompay, cabin, onSuccess, onFailed);
    }
    else if (parseInt(oNum) == 2) {//经停说明
        oAction = "getHaltFlight";
        headTitle = "【经停说明】";
        var hangban = oSpan.id.split("-")[3].toString();
        var hangbanRiqi = oSpan.id.split("-")[4].toString() + "-" + oSpan.id.split("-")[5].toString() + "-" + oSpan.id.split("-")[6].toString();
        var from = oSpan.id.split("-")[7].toString();
        AjaxService.GetFfInfo(hangban, hangbanRiqi, from, onSuccess, onFailed);
    }

    $("GQHeaderTitle").innerHTML = headTitle;
    //通过Ajax去获取相应数据
    function onSuccess(res) {
        if (parseInt(oNum) == 0) {//pat查询价格
            if (res.toString().substr(0, 5) == "error") {
                $("GQContent").innerHTML = "没有符合条件的运价，预定后可能无法出票";
                return;
            }
            else {
                var searchresultpat = eval(res);
                var searchresult = "<table><tr><td>票面价</td><td>机建费</td><td>燃油费</td><td>总价</td></tr>";
                for (var s in searchresultpat) {
                    searchresult += "<tr><td>" + searchresultpat[s].Fare + "</td><td>" + searchresultpat[s].Tax + "</td><td>" + searchresultpat[s].Yq + "</td><td>" + searchresultpat[s].Total + "</td></tr>";
                }
                searchresult += "</table>";
                $("GQContent").innerHTML = searchresult;
            }
        }
        else if (parseInt(oNum) == 1) {//退改签规定
            $("GQContent").innerHTML = res;
        }
        else if (parseInt(oNum) == 2) {//经停说明
            if (res.toString().substr(0, 5) == "error") {
                $("GQContent").innerHTML = res.toString().substr(5, res.toString().length);
                return;
            }
            else {
                var searchresultpat = eval(res);
                var searchresult = "<table><tr><td>经停城市</td><td>起飞时间</td><td>到达时间</td></tr>";
                for (var s in searchresultpat) {
                    searchresult += "<tr><td>" + searchresultpat[s].Szm + "</td><td>" + searchresultpat[s].ArriveTime + "</td><td>" + searchresultpat[s].LeaveTime + "</td></tr>";
                }
                searchresult += "</table>";
                $("GQContent").innerHTML = searchresult;
            }
        }
    }
    function onFailed(res) {
        alert(res);
    }

}
//显示改签层
function showGaiQianDiv(ev) {
    var xy = getMouseCoordinate(ev);
    var xyArr = xy.split("-");
    var x = parseFloat(xyArr[0]);
    var y = parseFloat(xyArr[1]);
    $("GQLayer").style.display = "block";
    $("GQLayer").style.left = (x + 20) + "px";
    var docuTop = -5;
    if (window.event) {
        var docuTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
    }
    $("GQLayer").style.top = (y + docuTop) + "px";
}
//隐藏退改签层    
function closeGQDiv() {
    $("GQLayer").style.display = "none";
    $("GQHeaderTitle").innerHTML = "";
    $("GQContent").innerHTML = "";
}
//显示退改签层=================================end

//显示隐藏舱位
function showOtherCabins(oSpan) {
    var oIndex = oSpan.id.split("-")[1].toString();
    var num = oSpan.id.split("-")[2].toString();
    //var numOne = num.split("-")[0].toString();
    var uls = document.getElementsByTagName("ul");
    for (var i = 0; i < uls.length; i++) {
        var oUl = uls[i];
        if (oUl.id) {
            if (oUl.id.split("-")[2] == num && oUl.id.split("-")[1] == oIndex) {
                if (oUl.style.display == "none") {
                    oUl.style.display = "";
                    oSpan.innerHTML = "隐藏其他";
                }
                else {
                    oUl.style.display = "none";
                    oSpan.innerHTML = "所有舱位";
                }
                if (oUl.id.split("-").length == 5) {
                    oUl.style.display = "";
                }
            }
        }
    }
}

//预订
function bookingFlight(oBtn, subUrl) {
    var searchType = $("dFlyType").value; //查询类型
    var url = subUrl; //提交地址
    var oInput = document.getElementsByTagName("INPUT");
    var flightChecked = new Array(); //选中的航班
    for (var i = 0; i < oInput.length; i++) {
        if (oInput[i].type == "radio" && oInput[i].id.indexOf("checkCabin-") != -1 && oInput[i].checked == true)
            flightChecked.push(oInput[i].value);
    }
    if (flightChecked.length == 0) {
        alert("请先选择航班后再进行预订");
        return;
    }
    var checkedFlight = document.getElementById("hfCurrentFlights"); //选中的航班 多个以，隔开
    //单程
    if (searchType == "0") {
        var airInfo = flightChecked[0].split("#");
        var isAllowedToOrder = airInfo[airInfo.length - 1]; //是否允许预订
        if (isAllowedToOrder == 1) {
            var now = new Date();//当前时间
            var leaveTime = new Date(airInfo[0].replace("-", "/").replace("-", "/") + " " + airInfo[2]); //出发时间
            if (leaveTime - now < 2 * 60 * 60 * 1000) {
                alert("起飞前两小时不允许订票");
                return;
            }
            checkedFlight.value = escape(flightChecked[0]);
        }
        else {
            alert("起飞前两小时不允许订票");
            return;
        }
    }
    else if (searchType == "1") { //往返
        if (flightChecked.length != 2) {
            alert("请分别选择往返两个舱位");
            return;
        }

        var airInfo1 = flightChecked[0].split("#");
        var airInfo2 = flightChecked[1].split("#");
        if (airInfo1[airInfo1.length - 1] != 1 || airInfo2[airInfo2.length - 1] != 1) {
            alert("起飞前两小时或者第二程起飞时间晚于第一程到达时间,不允许预订");
            return;
        }
        //************************************
        //在该处应该加入两个航班的时间比较
        CompareFlightTime(airInfo1[0], airInfo1[2], airInfo1[3], airInfo2[0], airInfo2[2]);
        //************************************
        if (airInfo1[5] != airInfo2[5]) {
            alert("往返预订必须是同一航空公司的航班，否则请按单程分别预订！");
            return;
        }
        checkedFlight.value = escape(flightChecked[0] + "," + flightChecked[1]);
    }
    else if (searchType == "2") { //多段
        if (flightChecked.length == 1) {//第一次查询 显示两断 以后的查询显示一段
            var airInfo = flightChecked[0].split("#");
            var isAllowedToOrder = airInfo[airInfo.length - 1];
            if (isAllowedToOrder != 1) {
                alert("起飞前两小时或者第二程起飞时间晚于第一程到达时间,不允许预订");
                return;
            }
            checkedFlight.value = escape(flightChecked[0]);
        }
        else {
            var airInfo1 = flightChecked[0].split("#");
            var airInfo2 = flightChecked[1].split("#");

            if (airInfo1[airInfo1.length - 1] != 1 || airInfo2[airInfo2.length - 1] != 1) {
                alert("起飞前两小时或者第二程起飞时间晚于第一程到达时间,不允许预订");
                return;
            }
            //************************************
            //在该处应该加入两个航班的时间比较
            CompareFlightTime(airInfo1[0], airInfo1[2], airInfo1[3], airInfo2[0], airInfo2[2]);
            //************************************
            if (airInfo1[5] != airInfo2[5]) {
                alert("往返预订必须是同一航空公司的航班，否则请按单程分别预订！");
                return;
            }
            checkedFlight.value = escape(flightChecked[0] + "," + flightChecked[1]);
        }
    }
    var form = document.forms[0];
    form.mothed = "post";
    form.action = url;
    oBtn.disabled = true;
    form.submit();
}

//比较两个航班的时间间隔是否合法
function CompareFlightTime(oLeaveDate1, oLeaveTime1, oArriveTime1, oLeaveDate2, oLeaveTime2) {
    var leaveTime1 = new Date(oLeaveDate1.replace("-", "/").replace("-", "/") + " " + oLeaveTime1); //一程出发时间
    var arriveTime1 = new Date(oLeaveDate1.replace("-", "/").replace("-", "/") + " " + oArriveTime1); //一程到达时间
    var leaveTime2 = new Date(oLeaveDate2.replace("-", "/").replace("-", "/") + " " + oLeaveTime2); //二程出发时间
    var now = new Date();
    var addHour = 0;
    if (leaveTime1 < now) {
        alert("第一程出发时间不能早于当前时间");
        return;
    }
    //当到达时间是第二天时
    if (leaveTime1 > leaveTime2) {
        addHour = 24;
    }
    if (leaveTime2 < now) {
        alert("第二程出发时间不能早于当前时间");
        return;
    }
    if (leaveTime2 - leaveTime1 < (2 + addHour) * (60 * 60 * 1000)) {
        alert("第二程出发时间必须至少比第一程到达时间晚2个小时");
        return;
    }   
}

function searchByDate(oDate, oIndex) {
    var oForm = document.forms[0];
    oForm.action = "Flights.aspx?action=search&newDate=" + oDate.toString() + "&oIndex=" + oIndex.toString();
    oForm.submit();
}


//多段查询中间页面 结束查询 进入预订页面
function multiOrder() {
    var form = document.forms[0];
    form.mothed = "post";
    form.action = "Order.aspx";
    form.submit();
}
//多段查询中间页面 检查查询信息
function checkMultiSearchInfo() {
    var fromCity = document.getElementById("fromCity");
    var toCity = document.getElementById("toCity");
    var firstDate = document.getElementById("firstDate");
    var lastDate = document.getElementById("hfLastDate"); //上已成出发时间

    if (fromCity.value == "" || fromCity.value == "中文/拼音/代码") {
        ShowMsg("出发城市不能为空");
        return;
    }
    if (toCity.value == "" || toCity.value == "中文/拼音/代码") {
        ShowMsg("到达城市不能为空");
        return;
    }
    if (fromCity.value == toCity.value) {
        ShowMsg("出发城市和到达城市不能相同");
        return;
    }
    var tempDt = new Date();
    var strToday = DateDemo(addDate(tempDt, 0)); // 今天日期

    if (firstDate.value == "") {
        ShowMsg("起飞时间不能为空");
        firstDate.focus();
        return;
    }
    if (dateDiff(firstDate.value, strToday) < 0) {
        ShowMsg("起飞时间不能早于当天");
        firstDate.focus();
        return;
    }
    if (dateDiff(firstDate.value, lastDate.value) < 0) {
        ShowMsg("出发日期不能早于上一程");
        return;
    }

    //数据验证完成后 提交到查询结果页面
    var oForm = document.forms[0];
    oForm.method = "post";
    oForm.action = "Flights.aspx?action=search";
    oForm.submit();
}

