﻿// JScript 文件

var globalDiv;
var divName="globalDiv";
//function InitQueryCode(queryFieldName)
//{
 //queryField= document.getElementById(queryFieldName);
 
 
 //queryField.onkeyup=keypressHandler;
 // queryField.onclick=keypressHandler;
 
//}
function keypressHandler(obj)
{
var text=obj.value;
//Employee_Index为需要用到Ajax的页面
var rArray= Employee_Index.GetSearchItems(text).value;

showQueryDiv(rArray);
}
//创建父div
function getDiv(divID)
{
///如果父DIV不在则创建,并将父DIV定位在文本框下面
  if(!globalDiv)
  {
  var queryField= document.getElementById("Keywords");
    if(!document.getElementById(divID))
    {
    var newNode=document.createElement("div");
    newNode.setAttribute("id",divID);
    document.body.appendChild(newNode);
    
    }
    globalDiv=document.getElementById(divID);
    var x=queryField.offsetLeft;
    var y=queryField.offsetTop+queryField.offsetHeight;
    var parent=queryField;
    while(parent.offsetParent)
    {
    parent=parent.offsetParent;
    x+=parent.offsetLeft;
    y+=parent.offsetTop;
    }
    globalDiv.style.border="1px solid gray";
    globalDiv.style.position="absolute";
    globalDiv.style.left=x+"px";
    globalDiv.style.top=y+"px";
    globalDiv.style.width="303px";
    globalDiv.style.zIndex=100;
    globalDiv.style.backgroundColor="#ffffff";
  }
 return globalDiv;
}

//将查询出来的子DIV创建,并显示子DIV
//参数是用来传递查询结果的,结果是一个数组
function showQueryDiv(resultArray)
{
  var parentDiv=getDiv(divName);
  parentDiv.style.display="block";
  //删除父DIV
  while(parentDiv.childNodes.length>0)
  {
   parentDiv.removeChild(parentDiv.childNodes[0]);
  }
  //用for循环创建子DIV
  for(var i=0;i<resultArray.length;i++)
  {
    sonDiv=document.createElement("div");
    sonDiv.innerHTML=resultArray[i];
    sonDiv.onmousedown=selectItem;
    sonDiv.onmousemove=divMove;
    sonDiv.onmouseout=divOut;
    parentDiv.appendChild(sonDiv);
  }
} 
 function selectItem()
  {
   var sonDiv=this;
   var textObj=document.getElementById("Keywords");
   textObj.value=sonDiv.innerHTML;
   var parentDiv=getDiv(divName);
   globalDiv.style.display="none";
  }
  function divMove()
  {
    var sonDiv=this;
    sonDiv.style.backgroundColor="#3366cc";
  }
    function divOut()
  {
    var sonDiv=this;
    sonDiv.style.backgroundColor="#ffffff";
  }