﻿
    var arrList = new Array();//要搜索的数据
    var objouter, objInput, objInputId = "Search_tb_Keywords";//控件ID
    var selectedIndex = -1, intTmp,oldkey,returnResult;
    //初始化
    function smanPromptList() {
        this.style = "overflow:hidden;width:383px;height:auto;background:#FFFFFF;border: solid 1px #118AE8;font-size:13px; font-family:Verdana;cursor:default;"
        if (arrList.constructor != Array) {
            alert('smanPromptList初始化失败:第一个参数非数组!');
            return;
        }
        document.write("<div id='__smanDisp' style='z-index:999;position:absolute;display:none;" + this.style + "' onblur></div>");
        document.write("<style type=\"text/css\">.sman_selectedStyle{background-Color:#3366CC;color:#FFFFFF;}</style>");

        objouter = document.getElementById("__smanDisp") //显示的DIV对象 
        objInput = document.getElementById(objInputId);  //文本框对象
        if (objInput == null) {
            alert('smanPromptList初始化失败:没有找到"' + objInputId + '"文本框');
            return;
        }
        objInput.onblur = function() { objouter.style.display = 'none'; }
        objInput.onkeyup = checkKeyCode;
        objInput.onfocus = checkAndShow;
    }

    function getAbsoluteHeight(ob) {
        return ob.offsetHeight;
    }
    function getAbsoluteWidth(ob) {
        return ob.offsetWidth;
    }
    function getAbsoluteLeft(ob) {
        var s_el = 0, el = ob;
        while (el) {
            s_el = s_el + el.offsetLeft;
            el = el.offsetParent;
        };
        return s_el;
    }
    function getAbsoluteTop(ob) {
        var s_el = 0, el = ob;
        while (el) {
            s_el = s_el + el.offsetTop;
            el = el.offsetParent;
        };
        return s_el;
    }
    function outSelection(Index) {
        objInput.value = objouter.children[Index].innerText.Trim();
        objouter.style.display = 'none';
    }
    function divPosition() {
        objouter.style.top = getAbsoluteHeight(objInput) + getAbsoluteTop(objInput);
        objouter.style.left = getAbsoluteLeft(objInput)-1;
        objouter.style.width = getAbsoluteWidth(objInput);
    }
    
    function chageSelection(isUp) {
        if (objouter.style.display == 'none') {
            objouter.style.display = 'none';
        }
        else {
            if (isUp)
                selectedIndex++;
            else
                selectedIndex--;
        }
        var maxIndex = objouter.children.length;
        if (selectedIndex < 0) { selectedIndex = 0; }
        if (selectedIndex > maxIndex) { selectedIndex = maxIndex; }
        if (selectedIndex == maxIndex) { selectedIndex = -1; }

        for (intTmp = 0; intTmp < maxIndex; intTmp++) {
            if (intTmp == selectedIndex) {
                objouter.children[intTmp].className = "sman_selectedStyle";
                objInput.value = objouter.children[selectedIndex].innerText.Trim();
            }
            else {
                objouter.children[intTmp].className = "";
            }
        }
    }
    function checkKeyCode() {
        var ie = (document.all) ? true : false;
        if (ie) 
        {
            var keyCode = event.keyCode;
            if (keyCode == 40 || keyCode == 38) //如果是上下键
            {
                var isUp = false;
                if (keyCode == 40)//按下键
                {
                    isUp = true;
                }
                chageSelection(isUp)
            }
            else if (keyCode == 13) 
            {
                outSelection(selectedIndex);
            }
            else 
            {
                checkAndShow();
            }
        }
        else 
        {
            checkAndShow();
        }
    }

    function checkAndShow() {
        var strInput = objInput.value.Trim();
        if(strInput=="")
        {
            objouter.style.display = 'none';
            return;
        }
        if(strInput==oldkey)
        {
            if(returnResult!='')
            {
                objouter.style.display = '';
            }
            return;
        }
        
        oldkey=strInput;
        divPosition();
        selectedIndex = -1;
        arrList.length = 0;
        //objouter.innerHTML = "";
        

        //webservice调用---和后台程序有关系
        Check.GetAutoList(strInput,
        function doresult(result)
        {
            returnResult=result;
            if(result=='')
            {
                objouter.style.display = 'none';
                return;
            }
            //=======================这里修改数据================================
            result=result.substring(0,(result.length-1));
            result="["+result+"]";
            //===================================================================
            var data = eval('(' + result + ')');
            var myvalue='';
            for (var j = 0; j < data.length; j++) {

                arrList[j] = data[j];
                myvalue += addOption(arrList[j], strInput);
            }
            objouter.innerHTML=myvalue;
            objouter.style.display = '';
        });
    }
    //显示搜索的数据并关键字涂色
    function addOption(value, keyw) {
        var v = value;
        return "<div style=\"padding-top:5px;padding-left:3px;width:395px;height:18px\" onmouseover=\"this.className='sman_selectedStyle'\" onmouseout=\"this.className=''\" onmousedown=\"document.getElementById('" + objInputId + "').value='" + value + "'\">" + v + "</div>";
    }
    String.prototype.Trim = function() {
        return this.replace(/(^\s*)|(\s*$)/g, "");
    }
    smanPromptList();
