	var divBox =null;
	var txtInput =null;
	var txtValue = null;
	var url =null;
	var separator =null;
	var limCount =null;
	var bgColor=null;
	var selectColor=null;
	if (window.ActiveXObject) {
		bgColor = "rgb(255,255,255)";
		selectColor = "rgb(142,189,240)";
	}else{
		bgColor = "rgb(255, 255, 255)";
		selectColor = "rgb(142, 189, 240)";
	}
	function initial(txt,uri,separater,count){
		txtInput = txt; 
		url = uri;
		separator=separater;
		limCount = count;
		if(divBox==null){
			//divBox = document.getElementById("sugFromSta");
			divBox = document.createElement("DIV");
			divBox.style.position="absolute";
			divBox.style.visibility="hidden";
			divBox.style.backgroundColor="rgb(255, 255, 255)";
			divBox.style.border="1px groove pink";
			divBox.style.cursor="pointer";
			divBox.style.zIndex=201;
			divBox.id="sugFromSta";
			divBox.onblur=setDisplay;
			divBox.style.padding="5px";
	    }
		setPosition(txtInput,divBox);
	}

 //设置下拉表 相对于 文本输入框的位置
     function setPosition(txtInput,divBox){
         var width = txtInput.offsetWidth;         
         var left = getLength("offsetLeft",txtInput);         
         var top = getLength("offsetTop",txtInput) + txtInput.offsetHeight;         
         divBox.style.left = left + "px";
         divBox.style.top = top + "px"; 
         while(width<150){
         	width+=1;
         }
         divBox.style.width = width + "px";
     } 
        
    //获取对应属性的长度 
     function getLength(attr,txtInput)
     {
         var offset = 0;
         var item = txtInput;
         while (item)
         {
             offset += item[attr];
             item = item.offsetParent;
         } 
         return offset; 
     } 
 //选择下拉表中当前项的值 ,用于按回车或鼠标单击选中当前项的值
 function selValue(event){
// 	var e = event || window.event;
 	var e = getEvent();
 	var currentKey = e.charCode||e.keyCode;
     if (currentKey != 13){
     	var eventSource =e.srcElement||e.target;
     	var str = eventSource.innerHTML;
     	if(eventSource.tagName!="DIV")
     		str = eventSource.parentNode.innerHTML;
        txtInput.value = str.substring(str.lastIndexOf("　")+1, str.lastIndexOf("</")); 
     }
     setDisplay();
 }
//文本框失去焦点时 设置下拉表可见性 
        function setDisplay(){
        	divBox.style.visibility = "hidden";
        }
        
        //通过键盘选择下拉项 
        function selItemByKey(e){
//        	var e = event || window.event;
			var e=getEvent();
        	var currentKey = e.charCode||e.keyCode;
        	if(divBox.firstChild==null){
        		return;
        	} 
        	var children = divBox.childNodes;
            //向下 
            if (currentKey == 40 ){
            	var us = false;
                 for(i=0;i<children.length;i++){
                 	if(children[i].style.backgroundColor==selectColor ){
                 		children[i].style.backgroundColor=bgColor;
                 		if(children[i].nextSibling!=null){
                 			children[i].nextSibling.style.backgroundColor=selectColor;
							var str = children[i].nextSibling.innerHTML;
							txtInput.value = str.substring(str.lastIndexOf("　")+1, str.lastIndexOf("</")); 
                 			us = true; 
                 		}
                 		break;
                 	}
                 }
                 if(!us ){
                 	children[1].style.backgroundColor=selectColor;
					var str = children[1].innerHTML;
					txtInput.value = str.substring(str.lastIndexOf("　")+1, str.lastIndexOf("</")); 
                 }
            }
            //向上 
            else if (currentKey == 38 ){
         		var ds = false;
                 for(i=0;i<children.length;i++){
                 	if(children[i].style.backgroundColor==selectColor ){
                 		children[i].style.backgroundColor=bgColor;
                 		if(children[i].previousSibling!=null && i>1){
                 			children[i].previousSibling.style.backgroundColor=selectColor;
							var str = children[i].previousSibling.innerHTML;
							txtInput.value = str.substring(str.lastIndexOf("　")+1, str.lastIndexOf("</")); 
                 			ds = true; 
                 		}
                 		break;
                 	}
                 }
                 if(!ds ){ 
                 	divBox.lastChild.style.backgroundColor=selectColor; 
					var str = divBox.lastChild.innerHTML;
					txtInput.value = str.substring(str.lastIndexOf("　")+1, str.lastIndexOf("</")); 
                 }	
            }
            //回车 
            else if (currentKey == 13){
            	for(i=0;i<children.length;i++){
                 	if(children[i].style.backgroundColor==selectColor){
                 		children[i].style.backgroundColor=bgColor;
                 		var str = children[i].innerHTML;
        				txtInput.value = str.substring(str.lastIndexOf("　")+1, str.lastIndexOf("</")); 
                 		break;
                 	}
                 }
                setDisplay();
            } 
        } 
        function changet2Half(obj){
            var str=obj.value;
            var result="";
            for(var i=0; i < str.length; i++){
                if(str.charCodeAt(i)==12288){
                    result+= String.fromCharCode(str.charCodeAt(i)-12256);
                    continue;
                }
                if(str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375){
                    result+= String.fromCharCode(str.charCodeAt(i)-65248);
                }else{
                    result+= String.fromCharCode(str.charCodeAt(i));
                }
            }
            obj.value=result;
        }
        function autoComplete(txt,url,separator,limCount) {
//        	if (document.readyState!="complete")
//        		return false;
//        	var e = event || window.event ||arguments[0];
			var e=getEvent();
        	var currentKey = e.charCode || e.keyCode;
            if (currentKey == 38 || currentKey == 40 || currentKey == 13){                
                selItemByKey(e);
            } 
            else {
            	changet2Half(txt);
           	 	if(txt.value!=txtValue && txt.value.length>=limCount){
           	 	    for (var i=0;i<txt.value.length;i++){
						if (txt.value.charCodeAt(i)<0||txt.value.charCodeAt(i)>255)
							return false;
					}
	                initial(txt,url,separator,limCount);
	       	 		setDisplay();
           	 		startRequest();
           	 	}
           	 	txtValue = txt.value;
            } 
        }         
 /////////////远程处理
var xmlHttp; 
//先新建一XHR对象 
function createXMLHttpRequest() { 
	//如果是IE，用activexobject 
	if (window.ActiveXObject) { 
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	}  
	//如果其它浏览器就用XMLHttpRequest 
	else if (window.XMLHttpRequest) { 
		xmlHttp = new XMLHttpRequest(); 
	} 
} 
function getUrl(){
	var rs = "";
	if(url.indexOf("?")>0)
	   rs = (url + "&zpy=" + txtInput.value);
	else
	   rs = (url + "?zpy=" + txtInput.value);
	if(separator!=null){
		rs += "&separate="+separator;
	}
	return rs + "&rn="+Math.random();
}
//开始函数 
function startRequest() { 
	createXMLHttpRequest(); 
	//指定当readyState属性改变时执行的函数 
	xmlHttp.onreadystatechange = handleStateChange; 
	　//创建一个新的http请求，并指定此请求的方法、URL以及验证信息 
	xmlHttp.open("GET", getUrl(), true); 
	//发送请求到http服务器并接收回应 
	xmlHttp.send(null); 
} 

function handleStateChange() { 
　//4数据接收完毕 
	if(xmlHttp.readyState == 4) { 
	//200返回请求状态为OK 
		if(xmlHttp.status == 200) { 
			//弹出对话框，并输入simpleResponse.xml文件的文本内容   
			showSugBox(xmlHttp.responseText); 
		} 
	} 
}

function showSugBox(rsTxt){
	var stas = rsTxt.split("@"); 
	divBox.innerHTML = "";
	var div = document.createElement("DIV");
	div.innerHTML="输入中文/拼音或↑↓选择.";
//	div.setAttribute("style","border-bottom:1px dotted red;padding:5px 0 5px 0;");
	div.style.borderBottom="1px dotted red";
	div.style.padding="5px 0 5px 0";
	divBox.appendChild(div);
	for(i=0; i<stas.length ;i++){
		if(stas[i]!=null && stas[i].length>1){
			div = document.createElement("DIV");
			div.setAttribute("id","sub_div");
			if (window.ActiveXObject) {
				div.setAttribute("style","background-color:rgb(255,255,255);");
			}else{
				div.setAttribute("style","background-color:rgb(255, 255, 255);");
			}
			var ld = document.createElement("SPAN");
			ld.innerHTML = stas[i].substring(0,stas[i].indexOf("|")); //ld.style.styleFloat = "left";
			var rd = document.createElement("SPAN");
			rd.innerHTML = "　"+stas[i].substring(stas[i].indexOf("|")+1); //rd.style.styleFloat = "right";
			if (window.ActiveXObject) {
				ld.style.styleFloat = 'left';
				rd.style.styleFloat = 'right';
			}
			else {
				ld.style.cssFloat = 'left';
				rd.style.cssFloat = 'right';
			}
			div.appendChild(ld);div.appendChild(rd);div.appendChild(document.createElement("BR"));
			div.onclick = selValue;
			div.onmouseover = function(){this.style.backgroundColor=selectColor;};
			div.onmouseout = function(){this.style.backgroundColor=bgColor;};
			div.style.padding="2px";
			divBox.appendChild(div);
		}
	}
	if(divBox.childNodes.length>0 ){
		divBox.style.visibility = "visible";
		if(document.getElementById("sugFromSta")==null)
			document.body.appendChild(divBox);
	}
}

function getEvent() //同时兼容ie和ff的写法
{  
        if(document.all)   return window.event;   
        func=getEvent.caller;        
        while(func!=null){  
            var arg0=func.arguments[0];
            if(arg0)
            {
              if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))
              {  
              return arg0;
              }
            }
            func=func.caller;
        }
        return null;
} 

function yyc(){
	if(divBox != null)
		divBox.style.visibility = "hidden";
}
if(window.ActiveXObject)
	document.attachEvent("onclick",yyc);
else
	document.addEventListener("click",yyc,false);

