﻿var inputValue1 = "";
var inputValue2 = "";
var inputValue3 = "";
var resList1 = null;
var resList2 = null;
var resList3 = null;
var selectedIndex = 0;
function getBox(obj, keyup) {
    var list = $("#list");
    $("#country").hide();
    $("#city").hide();
    $("#AirInc").hide();
    $("#DestinationCity").hide();
    list.show();
    var creatbox = $("#creatbox");
    selectedIndex = 0;
    if (obj.value == "输入或者选择您想去的国家或者城市")
        obj.value = "";
    if (obj.value == inputValue1 && keyup != null) return;
    inputValue1 = obj.value;
    if (creatbox.isHidden()) {
        creatbox.show();
        var pos = $(obj).position();
        creatbox.position(pos.top + 32, pos.left);
    }
    var par = { r: Math.random() };
    if (keyup != null)
        par = { r: Math.random(), key: escape(inputValue1) };
    Ajax.get("/Handler/CountryAndCity_Handler.ashx", callback, par);
    function callback(s) {
        list.html(s);
        list = $("#list");
        resList1 = list.find("a");
        resList1[selectedIndex].className = "hover";
    }
}
$.domReady(
   function() {
       $("#InputCountry").bindEvent("keyup", function(e) { searchAll(e, resList1, this); });
       $("#SpringCountry").bindEvent("keyup", function(e) { searchAll(e, resList2, this); });
       $("#IputAirInc").bindEvent("keyup", function(e) { searchAll(e, resList3, this); });
   }
);
   function searchAll(e, resList, o) {
       if (resList == null) return;
       var key = $.event.key(e);
       switch (key) {
           case 40:
               if (selectedIndex + 1 < resList.length) {
                   resList[selectedIndex].className = "";
                   selectedIndex++;
                   resList[selectedIndex].className = "hover";
               }
               break;
           case 38:
               if (selectedIndex - 1 >= 0) {
                   resList[selectedIndex].className = "";
                   selectedIndex--;
                   resList[selectedIndex].className = "hover";
               }
               break;
           case 13:
               $(resList[selectedIndex]).click();
               break;
           default:
               switch (o.id) {
                   case "InputCountry": getBox(o, 0); break;
                   case "SpringCountry": getDestinationCity(o, 0); break;
                   case "IputAirInc": getAirInc(o, 0); break;
               }
               break;
       }
   }
function test(o, countryId) {
    var creatbox = $("#creatbox");
    var input = $("#InputCountry");
    $("#cid").val(countryId);
    var s = o.innerHTML;
    var arr = s.split('-');
    switch (arr[1].length) {
        case 4:
            $("#goA").val(escape("/Destination.aspx"))
            input.val(arr[0] + " - " + arr[2]);
            //当长度为2时候是国家
            break;
        case 5:
            $("#goA").val(escape("/Springboard.aspx"));
            var b = input.val(arr[2] + " (" + arr[1] + ") " + "- " + arr[0]);
            //当为3的时候是城市
            break;
        default:
            break;
    }
    creatbox.hide();


}
function onb(id,value) {
    var input = $(id);
    if (input.val() == "") {
        input.val(value);
    }
}
function getCity(o) {
    $("#cid").val(o.value);
    $("#goA").val(escape("/Destination.aspx"));//编码
    $("#InputCountry").val($(o).selectedText());
    $("#city").html("<div class='loading'><p>正在为您加载加载数据，请稍后...</p><img src='/images/loading.gif' alt=''/></div>");
    Ajax.get("/Handler/City_Handler.ashx", callback, { r: Math.random(), Id: o.value });
    function callback(s) {
        $("#city").html(s);
    }
}
function getDestinationCity(obj, keyup) {
    selectedIndex = 0;
    var creatbox = $("#creatbox");
    var list = $("#DestinationCity");
    $("#list").hide();
    $("#country").hide();
    $("#city").hide();
    $("#AirInc").hide();
    $("#DestinationCity").show();
    if (obj.value == "输入或者选择您想出发的城市")
        obj.value = "";
    if (obj.value == inputValue2 && keyup != null) return;
    inputValue2 = obj.value;
    if (creatbox.isHidden()) {
        creatbox.show();
        var pos = $(obj).position();
        creatbox.position(pos.top + 32, pos.left);
    }
    var par = { r: Math.random() };
    if (keyup != null)
        par = { r: Math.random(), key: escape(inputValue2) };
    Ajax.get("/Handler/DestinationCity_Handler.ashx", callback, par);
    function callback(s) {
        list.html(s);
        list = $("#DestinationCity");
        resList2 = list.find("a");
        resList2[selectedIndex].className = "hover";
    }
    

}
function getAirInc(obj, keyup) {
    var creatbox = $("#creatbox");
    var list = $("#AirInc");
    $("#list").hide();
    $("#country").hide();
    $("#city").hide();
    $("#DestinationCity").hide();
    $("#AirInc").show();
    if (obj.value == "输入或者选择您信赖的航空公司")
        obj.value = "";
    if (obj.value == inputValue3 && keyup != null) return;
    inputValue3 = obj.value;
    selectedIndex = 0;
    if (creatbox.isHidden()) {
        creatbox.show();
        var pos = $(obj).position();
        creatbox.position(pos.top + 32, pos.left);
    }

    var par = { r: Math.random() };
    if (keyup != null)
        par = { r: Math.random(), key: escape(inputValue3) };
    Ajax.get("/Handler/AirInc_Handler.ashx", callback, par);
    function callback(s) {
        list.html(s);
        list = $("#AirInc");
        resList3 = list.find("a");
        resList3[selectedIndex].className = "hover";
    }
}
function setCity(o) {
    if ($("#A").isHidden() == false) {
        $("#InputCountry").val(o.innerHTML);
        $("#goA").val(escape("/Springboard.aspx"));
    }
    if ($("#B").isHidden() == false) {
        $("#SpringCountry").val(o.innerHTML);
        $("#goB").val(escape("/DestinationCountry.aspx"));
    }
    var creatbox = $("#creatbox");
    creatbox.hide();
}
function setAirInc(o) {
    $("#IputAirInc").val(o.innerHTML);
    $("#goC").val(escape("/AirInc.aspx"))
    var creatbox = $("#creatbox");
    creatbox.hide();
}
function chkForm() {
    var input = $("#InputCountry");
    var code = input.val();
    if (code.indexOf('-') > -1)
        return true;
    else {
        alert("输入正确格式");
        return false;
    }
    if ($("#InputCountry").val() != "输入或者选择您想去的国家或者城市" && $("#InputCountry").val() != "")
        return true;
    else {
        alert("请选择您想去的城市");
        return false;
    }
}
function chkForm_Spring() {
    if ($("#SpringCountry").val() != "输入或者选择您想出发的城市" && $("#InputCountry").val() != "")
        return true;
    else {
        alert("请选择您想出发的城市");
        return false;
    }
}
function chkForm_AirInc() {
    if ($("#IputAirInc").val() != "输入或者选择您信赖的航空公司" && $("#IputAirInc").val() != "")
        return true;
    else {
        alert("请选择您信赖的航空公司");
        return false;
    }
}
function FlightSearch(obj, object_Z, object, box) {
    var createSpringbox = $(box);
    var pos = $(obj).position();
    createSpringbox.position(pos.top + 25, pos.left);
    if (createSpringbox.isHidden())
        createSpringbox.show();
    else
        createSpringbox.hide();
    $(box + "_1").html(object_Z);
    $(box + "_2").html(object_Z);
    $(box + "_3").html(object_Z);
    $(box + "_Hidden").val(object);
}
function LoadingTo(obj,box) {
    var pathValue = $(box + "_Hidden").val();
    window.open( obj + pathValue);//打开新窗口
}

function closeBox(obj) {
    $(obj).hide();
}
function TicketsSearch(obj, Springboard, Springboard_Z, Destination, Destination_Z) {
//    alert(document.documentElement.scrollHeight + "," + document.body.scrollHeight);
    mask = UI.createMask();
    var TicketsBox = $("#createTicketsBox");
    var pos = $(obj).position();
    TicketsBox.position(pos.top, pos.left+200);
    if (TicketsBox.isHidden())
        TicketsBox.show();
    else
        TicketsBox.hide();
    $("#startCity").html(Springboard_Z);
    $("#begioncity").val(Springboard);
    $("#_endcity").html(Destination_Z);
    $("#endcity").val(Destination);
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var day = now.getDate();

    $("#startime").val(year + '-' + month + '-' + day);
    $("#backtime").val(year + '-' + month + '-' + (parseInt(day) + 5));

}
function closeMark(obj) {
    $(obj).hide();
    document.body.removeChild(document.getElementById("UI_Mask"));
}
