﻿// JScript 文件

var array=new Array(); //定义一个全局变量数组，用来存放拆分字符串的值，后面具体介绍。
var zz=-1; //此为指针，后面用到
var jilu=0;

function beonblur()
{
    if(jilu==0)
    {
	    document.getElementById("search_suggest").style.display="none";
    }
}

function xmlHttpInitializtions()
{
    try 
    {
        xmlhttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            xmlhttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e2) 
        {
            xmlhttpRequest = false;
        }
    }
    if (!xmlhttpRequest && typeof XMLHttpRequest != 'undefined') 
    {
        xmlhttpRequest = new XMLHttpRequest();
    }
}

function beKeyUp()
{
    var key = document.getElementById("KProvince").value;
    if(key.length>0)//有值才POST，这里最好加个Trim()方法，但是JAVASCRIPT没有现成的方法，自己定义。
    {
        xmlHttpInitializtions();
        xmlhttpRequest.Open("POST","Default2.aspx?q=" + encodeURIComponent(key),true);//POST
        xmlhttpRequest.onreadystatechange=stateChange;//返回状态调用方法stateChange
        xmlhttpRequest.Send();
    }
	else
	{
		document.getElementById("search_suggest").innerHTML="";//每次清空DIV内容
		document.getElementById("search_suggest").style.display="none";
	}
}
function stateChange()
{
    if(xmlhttpRequest.readystate==4)
    {
        if(xmlhttpRequest.status==200)
        {
            var responseStr=xmlhttpRequest.responseText;//获取返回值
            if(responseStr.length>0)//返回值不为空才执行下面的代码
            {
                array=responseStr.split('|');//根据‘|’拆分，根据自己任意特殊字符，初始化数组
                positionDiv();//调用方法，定位DIV显示，具体见方法解释
                document.getElementById("search_suggest").innerHTML="";//每次清空DIV内容
                for(var i=0;i<array.length;i++)
                {
                    if(array[i]!="")//项值不为空，组合DIV，每个DIV有onmouseover、onmouseout、onclick对应事件
                    {
                        document.getElementById("search_suggest").innerHTML+="<div id='item" + i + "' class='itemBg' onmouseover='beMouseOver(" + i +")'  onmouseout='beMouseOut(" + i + ")' onclick='beClick(" + i + ")'>" + array[i] + "</div>";
                    }
                }

                document.getElementById("search_suggest").style.display="";
            }
            else
            {
                document.getElementById("search_suggest").style.display="none";
            }
        }
    }
}
//定位DIV显示
function positionDiv()
{
      var DivRef= document.getElementById("search_suggest");
      DivRef.style.position = "absolute";
      var t=document.getElementById("KProvince");
      DivRef.style.top= getAbsolutePos(t).y;//相对文本框的TOP高度，方法见下面
      DivRef.style.left= getAbsolutePos(t).x ;//相对文本框的LEFT宽度
      DivRef.style.height=array.length * 20;//DIV的高度等于每行20个象素乘行数（也就是数组的长度，体现全局数组的作用，不然要传入数组长度的参数）
}

//定位方法，不做解释
function getAbsolutePos(el)
{
    var SL = 0, ST = 0;
    var is_div = /^div$/i.test(el.tagName);
    
    var r = { x:el.offsetLeft - SL-0.8, y: 13.7 };
    if (el.offsetParent)
    {
        var tmp = this.getAbsolutePos(el.offsetParent);
        r.x += tmp.x;
        r.y += tmp.y;
    }
    return r;
}


//函数鼠标经过效果        
function beMouseOverEFF(i)
{
    if ((i>=0)&(i<=array.length-1))
    {
        document.getElementById("item" + i).className="item_high";
    }
}

//函数鼠标移开效果
function beMouseOutEFF(i)
{
    if ((i>=0)&(i<=array.length-1))
    {
        document.getElementById("item" + i).className="item_normal";
    }
}

//函数鼠标经过
function beMouseOver(i)
{
    document.getElementById("KProvince").focus();
	jilu=1;
    beMouseOutEFF(zz);
    zz=i;
    beMouseOverEFF(zz);
}

//函数鼠标移开
function beMouseOut(i)
{
	jilu=0;
    beMouseOutEFF(i);
}
//函数单击
function beClick(i)
{
    document.getElementById("KProvince").value=array[i];
    document.getElementById("search_suggest").style.display="none";
    document.getElementById("KProvince").focus();
}

