﻿//签证搜索
//需要fn.js
//黄起程
//2008-10-15

var searchFn = {
    visaSearchTimer: null,
    initSearch: function()
    { //初始化页面对象
        //国家输入
        var inputChange = $$.idObj("countryInput").onchange = $$.idObj("countryInput").onkeyup = function(){
            var c = document.getElementById("countryInput").value.trim().lower();
            var csObj = $$.idObj("countrySelecter");
            if (c.length == 0)
                return;

            var ops = $$.array(csObj.options);
            var findGid = "";
            var findCountry = ""
            findIndex = -1;
            ops.find(function(item, index)
            {
                if (index > 0 && findIndex < 1)
                {
                    var cArr = item.value.getArray('|');
                    //中文 || 英文
                    if (cArr[1].trim().lower().startWith(c) || cArr[2].trim().lower().startWith(c))
                    {
                        findIndex = index;
                        findGid = cArr[0].trim();
                        findCountry = cArr[1].trim();
                        return true;
                    }
                }
                return false;
            });
            if (findIndex > 0 && findIndex != csObj.selectedIndex)
            {
                csObj.options[findIndex].selected = true;
                if (findGid.length > 0)
                    searchFn.showVisaType(findGid);
                if (findCountry.length > 0)
                {
                    var visaNewsInfoAreaObj = $$.idObj("VisaNewsInfoArea");
                    if (visaNewsInfoAreaObj != null)
                        $$.loadVisaNewsInfo(visaNewsInfoAreaObj, findCountry);
                }
            }
        }

        $$.idObj("countryInput").onfocus = function()
        {
            var c = document.getElementById("countryInput").value.trim();
            searchFn.visaSearchTimer = $$.tick(function()
            {
                if (document.getElementById("countryInput").value.trim() == c)
                    return;
                else
                {
                    c = document.getElementById("countryInput").value.trim();
                    inputChange();
                }
            }, 0.01);
        }
        $$.idObj("countryInput").onblur = function()
        {
            $$.tickend(searchFn.visaSearchTimer);
        }
        //国家选择
        $$.idObj("countrySelecter").onchange = function()
        {
            if (this.value.isNOrE())
                return;
            var gArray = this.value.getArray("|");
            $$.idObj("countryInput").value = gArray[1].trim();

            var gid = gArray[0].trim();
            var country = gArray[1].trim();
            searchFn.showVisaType(gid);
            if (country.length > 0)
            {
                var visaNewsInfoAreaObj = $$.idObj("VisaNewsInfoArea");
                if (visaNewsInfoAreaObj != null)
                    $$.loadVisaNewsInfo(visaNewsInfoAreaObj, country);
            }
        }
        //搜索按钮
        $$.idObj("btnVisaSearch").onclick = function()
        {
            var countrySelecterObj = $$.idObj("countrySelecter");
            if (countrySelecterObj.value.trim() == "")
            {
                alert("请选择所属国家！");
                countrySelecterObj.focus();
                return;
            }
            var typeSelecterObj = $$.idObj("typeSelecter");
            if (typeSelecterObj.options.length == 0)
            {
                alert("对不起，此国家暂无任何签证信息！");
                countrySelecterObj.focus();
                return;
            }

            //var country = countrySelecterObj.value.getArray("|")[1];//中文
            var countryEn = countrySelecterObj.value.getArray("|")[2]; //英文
            var type = typeSelecterObj.value.trim();
            var url;

            if (type != "" && type != "all")
                url = "search.aspx?countryEn={0}&TypeID={1}".format(countryEn, type);
            else
                url = "search.aspx?countryEn={0}&TypeID=all".format(countryEn);

            if ($$.url.toString().lower().indexOf("utm_source") >= 0)
                url += "&utm_source={0}&utm_medium={1}&utm_campaign={2}".format($$.q("utm_source"), $$.q("utm_medium"), $$.q("utm_campaign"));
            //alert(url);
            $$.direct(url);
        }

        //初始化搜索框
        var gidObj = $$.idObj("hidRequestGID");
        var typeIDObj = $$.idObj("hidRequestTypeID");
        var csObj = $$.idObj("countrySelecter");
        if (gidObj != null || gidObj.value != "")
        {
            var ops = $$.array(csObj.options);
            var findIndex = -1;
            ops.find(function(item, index)
            { //国家列表中 选中默认项(当前国家)
                if ((index > 0) && (item.value.getArray('|')[0] == gidObj.value))
                {
                    csObj.options[index].selected = true;
                    findIndex = index;
                    return true;
                }
                return false;
            });

            if (findIndex > 0)
                searchFn.showVisaType(gidObj.value, typeIDObj.value);
        }
        onload = function()
        {
            var visaNewsInfoAreaObj = $$.idObj("VisaNewsInfoArea");
            if (visaNewsInfoAreaObj != null)//首页
                $$.loadVisaNewsInfoDefault(visaNewsInfoAreaObj); //加载签证新闻
        }
    },
    showVisaType: function(gid, selectedType)
    {
        var typeSelecterObj = $$.idObj("typeSelecter");
        typeSelecterObj.html("");

        if (gid == null || gid == "")
        {
            var countrySelecterObj = $$.idObj("countrySelecter");
            if (countrySelecterObj == null)
                return;
            gid = countrySelecterObj.value.getArray("|")[0];
            if (gid == null || gid == "")
                return;
        }
        $$.req("xml/XMLType.aspx?GID={0}".format(gid), function(rsp)
        {
            var nodes = $$.nodes(rsp, "//group"); //.getElementsByTagName("group");
            //nodes = $$.array(nodes);

            if (nodes.length == 0)
                return;

            typeSelecterObj.options[0] = new Option("全部签证类型", "all");
            typeSelecterObj.options[0].selected = true;

            nodes.each(function(item)
            {
                typeSelecterObj.options[typeSelecterObj.options.length] = new Option(item.getAttribute("name"), item.getAttribute("value"));
                if (selectedType != null && selectedType != "all" && selectedType != "" && selectedType == item.getAttribute("value"))
                    typeSelecterObj.options[typeSelecterObj.options.length - 1].selected = true;
            });

        }, null, true, null);
    },
    initCountry: function(obj)
    {
        $$.update(obj, "ajax/loadmodule.aspx?ctrl=SearchModule2", null, function(rsp){
            searchFn.initSearch();
        }, function(rsp){
        });
    }
}
Object.extendFn( $$,searchFn );
