var xmlhttp;	//XmlHttpRequest 对象
var count=0;	//记录总的符合项
var flag = 0;	//标记,用来判断是不是第一次按下键盘
//创建 XmllHttpRequest 对象
function createXmlHttpRequest(){
	// 判断游览器
	if (window.XmlHttpRequest ) {
		xmlhttp = new XmlHttpRequest() ; // FireFox 等浏览器的创建方式				
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	// IE 等浏览器的创建方式
	}	
}

//Ajax主调函数
function ajax(value){
	if(event.keyCode!=13&&event.keyCode!=40&&event.keyCode!=38)
	{
	    var redioobj=window.document.getElementsByName("radiobt");
	    dbids="";
	    if(redioobj.length>0)
	    {
	      if(!redioobj[0].checked) //自定义库检索
	      {
	        dbids=window.document.formtop.tempdbid.value;
	      }
	    }
	
		
		createXmlHttpRequest();
		value = encodeURI(encodeURI(value)); 
		xmlhttp.open("post","Autocomplete?key="+value+"&dbid="+dbids,false);
		xmlhttp.onreadystatechange=callback;
		xmlhttp.send(null);
	}
}

//Ajax回调函数  
function callback(){
	if(xmlhttp.readyState==4){

		if(xmlhttp.status==200){
			//alert(xmlhttp.responseXML.xml);
			parsexmlDOM(xmlhttp.responseXML);
		}else{
			alert("系统异常!");
		}
	}
}

//XMLDOM解析函数
function parsexmlDOM(xmlDOM){

	var titles = xmlDOM.getElementsByTagName("HotContent");
	var div = document.getElementById("show");
	var e = document.getElementById("searchkey");    // 获取searchkey的绝对位置
	var   top,   left;   
    left   =   e.offsetLeft;   
    top     =   e.offsetTop;   
    while(e   =   e.offsetParent)   {   
    left   +=   e.offsetLeft;   
    top     +=   e.offsetTop;   
    }   
    left   +=   document.body.scrollLeft;   
    top     +=   document.body.scrollTop;   
	
	
	

	div.innerHTML = "";
	count=0;
	flag=0;
	//alert(titles.length)
	div.style.display="block";
	div.style.left=left+1;
    div.style.top=top+15;
	var html = "<table width=\"221\">";
	if(titles.length>0){
		
		
		html = html+"<tr><td align=\"left\">关键词</td><td align=\"right\"></td></tr>"
		for(var i=0;i<titles.length;i++){
			count ++;
			html=html+"<tr>";
			html = html+"<td class=\"li\" align=\"left\"  id=\"li"+i+"\" myflag=\"0\" onmouseover=\"select(this);show();\" onmouseout=\"unselect(this);unshow();\" onclick=\"selected(this);\">"+titles[i].firstChild.nodeValue+"</td>";
            html = html+"<td align=\"right\" id=\"li1"+i+"\" myflag=\"0\" >"+"</td>";
            
            html=html+"</tr>";
           // alert(html)
		}
		
		//alert(html)
		
	}else{
		html = html+"<tr><td align=\"center\" colspan=\"2\">无查询结果</td></tr>"
	
		//div.style.display="none";
	}
	
	html = html+"</table>";
	div.innerHTML = html;
}

/*
function parsexmlDOM(xmlDOM){
	var titles = xmlDOM.getElementsByTagName("HotContent");
	var div = document.getElementById("show");
	div.innerHTML = "";
	count=0;
	flag=0;
	//alert(titles.length)
	if(titles.length>0){
		div.style.display="block";
		var html = "<ul>";
		for(var i=0;i<titles.length;i++){
			count ++;
			html = html+"<li  id=\"li"+i+"\" myflag=\"0\" onmouseover=\"select(this);show();\" onmouseout=\"unselect(this);unshow();\" onclick=\"selected(this);\">"+titles[i].firstChild.nodeValue+"111</li>";
           // alert(html)
		}
		html = html+"</ul>";
		//alert(html)
		div.innerHTML = html;
	}else{
		div.style.display="none";
	}
}
*/




//鼠标移出li后改变样式
function unselect(obj){
	obj.className="unselect";
}

//鼠标移入li后改变样式
function select(obj){
	obj.className="select";
}

//隐藏提示div
function unshow(){
	document.getElementById("show").style.display="none";
}

//显示提示div
function show(){
	document.getElementById("show").style.display="block";
}
//鼠标点击选中值
function selected(obj){
	document.getElementById("searchkey").value=obj.innerText;
	document.getElementById("show").style.display="none";
}
//跳到指定页面
function keyup(){
	//选择上一个
	if(event.keyCode==38){
		previous();
	}
	//选择下一个
	if(event.keyCode==40){
		next();
	}
	//选中
	if(event.keyCode==13){
		enter();
	}
	
}
function next(){
	if(count>0&&document.getElementById("show").style.display=="block"){
		if(flag==0){
			var temp = document.getElementById("li0");
			temp.className="select";
			temp.myflag="1";
			flag=1;
		}else{
			for(var i=0;i<count;i++){
				var temp = document.getElementById("li"+i);
				if(temp.myflag=="1"){
					temp.className="unselect";
					temp.myflag="0";
					id = temp.id.substr(2)-0+1;
					if(id==count){
						id=0;
					}
					var temp2 = document.getElementById("li"+id);
						document.getElementById("searchkey").value=temp2.innerText;
					temp2.className="select";
					temp2.myflag="1";
					break;
				}
			}

		}
	}
}

//当按键盘"上"时,选择上一个
function previous(){
	if(count>0&&document.getElementById("show").style.display=="block"){
		if(flag==0){
			var temp = document.getElementById("li0");
			temp.className="select";
			temp.myflag="1";
			flag=1;
		}else{
			for(var i=0;i<count;i++){
				var temp = document.getElementById("li"+i);
				if(temp.myflag=="1"){
					temp.className="unselect";
					temp.myflag="0";
					id = temp.id.substr(2)-1;
					if(id<0){
						id=count-1;
					}
					var temp2 = document.getElementById("li"+id);
						document.getElementById("searchkey").value=temp2.innerText;
					temp2.className="select";
					temp2.myflag="1";
					break;
				}
			}

		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//当按键盘"回车"时,把选中的值放入输入框
function enter(){
	if(count>0&&document.getElementById("show").style.display=="block"){
		if(flag==1){
			for(var i=0;i<count;i++){
				var temp = document.getElementById("li"+i);
				if(temp.myflag=="1"){
					document.getElementById("searchkey").value=temp.innerText;
					document.getElementById("show").style.display="none";
					break;
				}
			}
		}
	}
}