﻿// JScript 文件

//当前选中的ID
var curtID = "DropItem0";
var xmlHttp_SearchSuggest = false;

//使得innerHTML内的script代码可以执行(变量定义类的代码也可以执行)
var setInnerHTML = function (el, htmlCode) {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
        htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
        htmlCode = htmlCode.replace(/<script([^>]*)>/gi,'<script$1 defer>');
        el.innerHTML = htmlCode;
        el.removeChild(el.firstChild);
    } else {
        var el_next = el.nextSibling;
        var el_parent = el.parentNode;
        el_parent.removeChild(el);
        el.innerHTML = htmlCode;
        if (el_next) {
            el_parent.insertBefore(el, el_next)
        } else {
            el_parent.appendChild(el);
        }
    }
}

function Suggest(e)
{
    var value;
    if (!e)
    {
        var e = window.event;
    }
    if (e.keyCode)
    {

        value = e.keyCode;
    }
    else if(e.which) 
    {
        value = e.which;
    }

    //判断按下的是否为箭头
    //var value= event.keyCode;
    if(value == 38 || value == 40)  //38-up 40-dw
    {
        ChkChange(curtID,value);
        return;
    }
    
    if(document.getElementById("kw")==null || document.getElementById("DropSuggest")==null)
    {
        return;
    }
    var keyword = document.getElementById("kw");
    var divSgt = document.getElementById("DropSuggest");           
    
    //如果已经删除了所有的输入,则不再显示
    if(keyword.value=="")
    {
        divSgt.className = "DropHide";
        return;
    }
    else
    {           
        //刷新列表,并显示列表
        //重置选中的ID
        curtID = "DropItem0";
        RefreshContent(keyword.value);
        divSgt.className = "DropShow";
    }
    
}

//隐藏列表,并且将已经选中的设为空
function Hide()
{
    document.getElementById("DropSuggest").className = "DropHide";
    curtID = "DropItem0";
}

//刷新列表
function RefreshContent(Keyword)
{
    //修改DropSuggest的内容    
    try 
    {
        xmlHttp_SearchSuggest = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            xmlHttp_SearchSuggest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e2)
        {
            xmlHttp_SearchSuggest = false;
        }
    }
    if (!xmlHttp_SearchSuggest && typeof XMLHttpRequest != 'undefined') {
        xmlHttp_SearchSuggest = new XMLHttpRequest();
    }
    
    var url = "/SearchSuggest.aspx?keyword=" + Keyword;
    xmlHttp_SearchSuggest.open("Get", url, true);//如果此处为POST，会出现Length Required的错误
    xmlHttp_SearchSuggest.setRequestHeader("Content-Type","text/xml");
    xmlHttp_SearchSuggest.setRequestHeader("Content-Type","GBK"); 
    xmlHttp_SearchSuggest.onreadystatechange = callBack_Suggest;        
    xmlHttp_SearchSuggest.send(null);
}

function callBack_Suggest() 
{
    //if(xmlHttp_SearchSuggest.readyState == 1)
    //{
    //  document.getElementById("DropSuggest").innerHTML = "读取中....";
    //}
    if (xmlHttp_SearchSuggest.readyState == 4) 
    {
        //document.getElementById("DropSuggest").innerHTML = xmlHttp_SearchSuggest.responseText);    //这种用法不能执行里面的script        
        setInnerHTML(document.getElementById("DropSuggest"),xmlHttp_SearchSuggest.responseText);
        
        divSgt = document.getElementById("DropSuggest");
        if( typeof Sgt_Arr != "undefined")
        {
            if(Sgt_Arr.length <= 1)
            {
                divSgt.className = "DropHide";
                return;
            }
            divSgt.className = "DropShow";
        }
        return;
    }    
}

//用键盘上下箭头来切换所选
function ChkChange(senderId,KeyValue)
{
    if(Sgt_Arr==null)
    {
        return;
    }
    for(i=0; i< Sgt_Arr.length ; i++)
    {
        if( Sgt_Arr[i] == senderId)
        {
            if(KeyValue == 40)  //Down
            {
                if(i==Sgt_Arr.length-1)
                {
                    curtID = Sgt_Arr[0];
                }
                else
                {
                    curtID = Sgt_Arr[i+1];
                }
            }
            else    //up
            {
                if( i==0)
                {
                    curtID = Sgt_Arr[Sgt_Arr.length-1];
                }
                else
                {
                    curtID = Sgt_Arr[i-1];
                }      
            }
            break;
        }
    }
    ItemSelect(curtID); 
}

//选中某个项
function ItemSelect(senderId)
{
    if(Sgt_Arr==null)
    {
        return;
    }
    for(i=0; i< Sgt_Arr.length ; i++)
    {
        if(document.getElementById(Sgt_Arr[i])!=null)
        {
            document.getElementById(Sgt_Arr[i]).className="DropItem";
        }
    }
    if(document.getElementById(senderId)!=null)
    {                
        if(senderId!="DropItem0")
        {
            document.getElementById(senderId).className="DropItem_Hover";                    
        }
        document.getElementById("kw").value=document.getElementById(senderId).innerHTML;
    }
}