﻿/*!
 * 搜索自动提示功能
 * 
 * http://www.sobnb.com
 *
 * Copyright 2010, Sobnb
 *
 * Date:2010-3-23
 */
 

var searchObject=null;  //声明一个XMLHttpRequest对象
var inputField=null;
var selectvalue=null;   //用来存放选中的结果
var flag=false;         //标志是否点击选中结果并放入结果框
var str=null; 
var keyRows=null;       //指针，选中第几行

//开始搜索，调用Ajax程序
function searchSuggest()
{
    var search=document.getElementById(inputField);
    if(search.value=="中文/拼音")
    {
        search.value="";
    }
    if(search.value=="")
    {
        createShowTable("");
    }
    else
    {
        var str=escape(search.value);
        var url="/tools/SearchSuggest.aspx?search="+str;
        makeAjax(url);
    }     
}
    
//实例化XMLHttpRequest对象
if(window.XMLHttpRequest)
{
    searchObject=new XMLHttpRequest();
}
else if(window.ActiveXOjbect)
{
    try
    {
        searchObject=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            searchObject=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {}
    }
}
var http_request = false;
function makeAjax(url) 
{
    http_request = false;
    if (window.ActiveXObject)  // IE
    {
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try 
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) 
            {
                try 
                {
                    http_request = new ActiveXObject("Msxml2.XMLHTTP.4.0");
                } 
                catch (e) 
                {
                    alert('该浏览器不支持AJAX，请使用普通IE登录，谢谢！');
                }
            }
        }
    }
    else if (window.XMLHttpRequest)  // Mozilla, Safari,...
    {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) 
        {
                http_request.overrideMimeType('text/xml');
        }
    } 

    if (!http_request) 
    {
        alert('Giving up :( Cannot create an XMLHTTP instance)');
        return false;
    } 
    http_request.onreadystatechange = handleSearchSuggest;
    http_request.open('GET', url, true);
    http_request.send(null);
}

//获得查询结果    
function handleSearchSuggest()
{
    if(http_request.readyState==4)
    {
        if(http_request.status==200)
        {
            str=http_request.responseText;
            if(str!="")
            {
                createShowTable(str);
            }
            else
            {
                removeShowTable();
            }
        }
        else
        {
            alert("错误：请求页面出错");
        }
    }
}

//创建提示窗口
function getDisplayArea(search)
{
    inputField=search;
    Area=document.getElementById("search_suggest");
    if(!Area)
    {  
        var tempDiv=document.createElement("div");  
        tempDiv.setAttribute("id","search_suggest");  

        //设置div的css  
        tempDiv.style.position="absolute";   
        tempDiv.style.overflowY = "hidden";  
        tempDiv.style.overflowX = "hidden";  
        tempDiv.style.overflow="scroll";  
        tempDiv.style.display="none";
        document.body.appendChild(tempDiv);
    } 
}

//创建内容
function createShowTable(dataSource)
{
    var search=document.getElementById(inputField);
    var displayArea=document.getElementById("search_suggest");
    displayArea.innerHTML="";  
    displayArea.style.display="block";  
    var data=dataSource;
    if(typeof(data) == "undefined" || data=="")
    {
        data="Beijing||北京||110220$$Shanghai||上海||110220$$Chongqing||重庆||110220$$Hangzhou||杭州||110220$$Sanya||三亚||110220$$Nanjing||南京||110220$$Shenzhen||深圳||110220$$Chengdu||成都||110220$$Dalian||大连||110220$$Xiamen||厦门||110220"; 
    }
    var tb=document.createElement("TABLE");  
    tb.setAttribute("cellspacing","0");
    tb.setAttribute("cellpadding","0"); 
   
    tb.style.width="100%";  
   
    var recordsAry = data.split("$$");  
   
    var tr,td1,td2,fot;  
    var record,td1V,td2V,hint;  
   
    for(var i=0;i<recordsAry.length;i++)
    {  
        record=recordsAry[i].split("||");  
        tr = document.createElement("tr");         
        tr.setAttribute("id",""+recordsAry[i]+i); 
        tr.setAttribute("bgColor","ffffff"); 
        tr.setAttribute("onMouseOver","suggestOver(this,'"+record[1]+"');this.bgColor='e7f1fd'");
        tr.setAttribute("onMouseOut","suggestOut(this)");

        tr.style.cursor = "pointer";  
        //可对tr作css修饰   
        td1 = document.createElement("td");  
        td2 = document.createElement("td");  
        //可对td作css修饰  
        td1.setAttribute("align","left");
        td1.setAttribute("class","suggest_left");  
        td2.setAttribute("align","right");
        td2.setAttribute("class","suggest_right");      
   
        td1V=document.createTextNode(record[0]);
        td2V=document.createTextNode(record[1]);
        //可对文本作css修饰  
   
        //组装  
        td1.appendChild(td1V);  
        td2.appendChild(td2V);  
   
        tr.appendChild(td1);  
        tr.appendChild(td2);  
   
        tb.appendChild(tr);    
   
    }  
   
    tr = document.createElement("tr"); 
    tr.setAttribute("onClick","removeShowTable()"); 
   
    td1 = document.createElement("td");  
    td1.setAttribute("align","right"); 
    td1.setAttribute("colspan","2");
   
    fot = document.createElement("font");
    fot.setAttribute("color","red");
    fot.setAttribute("onClick","removeShowTable()"); 
    fot.setAttribute("id","search_close");
    fot.appendChild(document.createTextNode("关闭"));
   
    td1.appendChild(fot);  
    tr.appendChild(td1);  
    tb.appendChild(tr);  
   
    displayArea.appendChild(tb);  
    displayArea.innerHTML=displayArea.innerHTML;  
    selectvalue=null;
    var eT=0,eL=0;
    while(search=search.offsetParent)
    {
        eT+=search.offsetTop;
        eL+=search.offsetLeft;
    } 
    displayArea.style.top=eT+32;
    displayArea.style.left=eL;
 }
 
// 清除提示层
function removeShowTable()
{
    Area=document.getElementById("search_suggest");
    Area.innerHTML="";    
    Area.style.display="none"; 
    var search=document.getElementById(inputField);
    if(flag)
    {
        search.value=selectvalue;
    }
    if(search.value=="")
    {
        search.value="中文/拼音";
    }
 } 
 
 //鼠标划过时，背景变色，并把当前值保存下来，并把标志位置为真
function suggestOver(object,txt)
{
    object.style.background="#3366cc";
    object.style.color="#fff";
    selectvalue=txt;
    flag=true;
    //document.getElementById("ctl00_Top1_tbxSearchName")=txt;
}
//划出时，背景变色，并把当前值置空，并把标志位置为假
function suggestOut(object)
{
    object.style.background="#ffffff";
    object.style.color="#0055aa";
    selectvalue=null;
    flag=false;
}

 //搜索框输入内容时  
function keyUpEvent(object)
{
    var key=(window.event)? event.keyCode : object.which;  
    // 不是上下两个方向键  
    if(key != "40" && key != "38")
    {
        searchSuggest();   
    }
    else
    {
        selectvalue=null;               // 将选择关键字的指针变量清空；  
        upOrDown(key);
    }  
}

function upOrDown(key)
{  
    var trCount;
    Area=document.getElementById("search_suggest");  
    var tb=Area.firstChild;  
    if(tb)
    {  
        trCount = tb.firstChild.childNodes.length - 2;  
        //向下 
        if(key == "40")
        {    
            if(keyRows == null)
            {  
                keyRows=0;  
            }
            else if(keyRows >= trCount)
            {  
                tb.firstChild.childNodes[keyRows].bgColor = "#99ffff";
                keyRows = 0;  
            }
            else
            {  
                tb.firstChild.childNodes[keyRows].bgColor = "#ffffff"; 
                keyRows++;  
            }  
              
            tb.firstChild.childNodes[keyRows].bgColor = "#99ffff";
              
            tb.firstChild.childNodes[keyRows].focus();  
             
            var record=tb.firstChild.childNodes[keyRows].id.split("||");  
   
            selectvalue=record[1]; 
            flag=true; 
         }
         else if(key == "38")
         {                          //向上  
            if(keyRows == null)
            {  
                 keyRows=0;  
            }
            else if(keyRows==0)
            {  
                 tb.firstChild.childNodes[keyRows].bgColor = "#99ffff";
                 keyRows = trCount;  
            }
            else
            {  
                 tb.firstChild.childNodes[keyRows].bgColor = "#ffffff"; 
                 keyRows--;  
            }  
            tb.firstChild.childNodes[keyRows].bgColor = "#99ffff";  
            tb.firstChild.childNodes[keyRows].focus();              
            var record=tb.firstChild.childNodes[keyRows].id.split("||");               
            selectvalue=record[1];
            flag=true;     
         }
         else if(key == "13")
         {      //Enter
            var record=tb.firstChild.childNodes[keyRows].id.split("||");
            alert(record);
            selectvalue=record[1];   
            removeShowTable();  
            document.getElementById(inputField).blur();  
         }
         if(Area && keyRows != null && keyRows >= 0)
         {         
             Area.scrollTop = keyRows * tb.firstChild.childNodes[keyRows].scrollHeight;  
         }  
     }
 }  
