﻿	var arrOptions=new Array();//引用一个保存从服务查询中获得的所有可用选项的数组
	var strLastValue="";//保存文本框中包含的最后的字符串
	var bMadeRequest;//请求是否已经发送
	var theTextBox;//保存对拥有焦点的文本框的引用
	var objLastActive;//保存对最后激活的文本框的引用
	var currentValueSelected=-1;//与selectedIndex类似，如果是-1,表示没有选项选中
	var bNoResults=false;//是否没有结果
	var isTiming=false;//确定页面上是否运行了一个定时器
	var baseURL="";
	var isOpera=(navigator.userAgent.toLowerCase().indexOf("opera")!=-1);
	
	//使用正则表达式限制结果数量changeIMG
	//避免每次按键要要请求服务器
	function SetText(xVal){
		theTextBox.value=arrOptions[xVal][0];
		theTextBox.obj.hidden.value=arrOptions[xVal][1];
		document.getElementById("spanOutput").style.display="none";
		currentValueSelected=-1;
		theTextBox.form.submit();//提交表单
	}
	function SetMoveText(xVal){
		theTextBox.value=arrOptions[xVal][0];
		theTextBox.obj.hidden.value=arrOptions[xVal][1];
	}
	var countForId=0;
	function MakeMatches(xCompareStr){
		countForId=0;
		var matchArray=new Array();
		var regExp=new RegExp(theTextBox.obj.regExAny+xCompareStr,theTextBox.obj.regExFlags);
		for(i=0;i<arrOptions.length;i++){
			var theMatch=arrOptions[i][0].match(regExp);

			///*以下 是自己加的
			if(getClassIDType()){//如果删除 或 修改 ，同时必须修改第***208***行
				theMatch = true;//匹配显示的结果，现在这样，就是不用匹配，有什么显示什么（zxl修改）
			}
			//以上 是自己加的*/
			
			if(theMatch){
				matchArray[matchArray.length]=CreateUnderline(arrOptions[i][0],arrOptions[i][1],xCompareStr,i);
			}
		}
		return matchArray;
	}
	function StartTimeout(){
		isTiming=setTimeout("HideTheBox()",theTextBox.obj.theVisibleTime);
	}
	//执行字符串操作
	var undeStart="<span class='spanMatchText'>";
	var undeEnd="</span>";
	var selectSpanStart="<span style='width:100%;display:block;' class='spanNormalElement' onmouseover='SetHighColor(this)'";
	var selectSpanEnd="</span>";
	var resultStart="<span class='resultData'";
	var resultEnd="</span>";
	function CreateUnderline(xStr,xResult,xTextMatch,xVal){
		selectSpanMid=" onclick='SetText("+xVal+")'"+" id='OptionsList_"+countForId+"' theArrayNumber='"+xVal+"'>";
		resultSpanid=" id='ResultList_"+countForId+"'>";
		var regExp=new RegExp(theTextBox.obj.regExAny+xTextMatch,theTextBox.obj.regExFlags);
		var aStart=xStr.search(regExp);
		var matchedText=xStr.substring(aStart,aStart+xTextMatch.length);
		countForId++;
		return selectSpanStart+selectSpanMid+resultStart+resultSpanid+xResult+resultEnd+xStr.replace(regExp,undeStart+matchedText+undeEnd)+selectSpanEnd;
	}
	//动态设定元素的位置
	function SetElementPosition(theTextBoxInt){
		var selectedPosX=0;
		var selectedPosY=0;
		var theElement= theTextBoxInt;
		if(!theElement)return;
		var theElemHeight= theElement.offsetHeight;
		var theElemWidth= theElement.offsetWidth;
		while(theElement!=null){
			selectedPosX+=theElement.offsetLeft;
			selectedPosY+=theElement.offsetTop;
			theElement=theElement.offsetParent;
		}
		xPosElement=document.getElementById("spanOutput");
		xPosElement.style.left=selectedPosX;
		if(theTextBoxInt.obj.matchTextBoxWidth)
		xPosElement.style.width=theElemWidth;
		xPosElement.style.top=selectedPosY+theElemHeight;
		xPosElement.style.display="block";
		if(theTextBoxInt.obj.userTimeOut){
			xPosElement.onmouseout=StartTimeout;
			xPosElement.onmouseover=EraseTimeout;
		}else{
			xPosElement.onmouseout=function(){//
				if(document.getElementById("spanOutput").style.display=="block"){//防止在隐藏菜单时触发事件
					theTextBox.value=strLastValue;
				}
				currentValueSelected=-1;
				for(i=0;i<countForId;i++){
					document.getElementById('OptionsList_'+i).className='spanNormalElement';
					document.getElementById('ResultList_'+i).className='resultData';
				}
			};
			xPosElement.onmouseover=null;
		}
	}
	//ajax
	function SuggestAjax(url,func,param,str){
		var request;
		var netie = null;
		if(window.XMLHttpRequest){
			request=new XMLHttpRequest();
			netie = "fox";
		}else if(window.ActiveXObject){
			netie = "ie";
			try{
				request=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
			}
			try{
				request=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
			}
		}
		request.onreadystatechange=function(){func(request,str)};
		request.open("POST",url+"?netie="+netie,true);
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		request.send(param);
	}
	function EraseTimeout(){
		clearTimeout(isTiming);
		isTiming=false;
	}
	function HideTheBox(){
		document.getElementById("spanOutput").style.display="none";
		currentValueSelected=-1;
		EraseTimeout();
		//changeIMG(document.getElementById('img2'));
	}
	function GrabHighlighted(){
		if(currentValueSelected>=0){
			xVal=document.getElementById("OptionsList_"+currentValueSelected).getAttribute("theArrayNumber");
			SetText(xVal);
			HideTheBox();
		}
	}
	//添加上下箭头键选择的功能
	//xDir--突出显示应该移动的方向
	function MoveHighlight(xDir){
		//if(currentValueSelected>=0){
			newValue=parseInt(currentValueSelected)+parseInt(xDir);
			if(newValue>-1&&newValue<countForId){
				currentValueSelected=newValue;
				SetHighColor(null);
			}else{
				if(xDir==1){
					currentValueSelected=-1;
				}
				if(xDir==-1){
					currentValueSelected=countForId;
				}
				SetHighColor(null);
			}
			//if(newValue<-1&&xDir=-1){
				//currentValueSelected=countForId;
				//SetHighColor(null);
			//}
			//if(newValue>countForId&&xDir=1){
				//currentValueSelected=countForId;
				//SetHighColor(null);
			//}
			//xVal=document.getElementById("OptionsList_"+currentValueSelected).getAttribute("theArrayNumber");
			//SetMoveText(xVal);
		//}
	}
	//设置高亮
	function SetHighColor(theTextBoxParam){
		if(theTextBoxParam){
			currentValueSelected=theTextBoxParam.id.slice(theTextBoxParam.id.indexOf("_")+1,theTextBoxParam.id.length);
		}
		for(i=0;i<countForId;i++){
			document.getElementById('OptionsList_'+i).className='spanNormalElement';
			document.getElementById('ResultList_'+i).className='resultData';
		}
		if(currentValueSelected>-1&&currentValueSelected<countForId){
			var selectItemObj=document.getElementById('OptionsList_'+currentValueSelected);
			selectItemObj.className='spanHighElement';
			xVal=selectItemObj.getAttribute("theArrayNumber");
			document.getElementById('ResultList_'+currentValueSelected).className='resultHighData';
			SetMoveText(xVal);
		}else{
			theTextBox.value=strLastValue;
		}
	}
	function BuildList(theText){
		SetElementPosition(theTextBox);//设置元素位置
		var theMatches=MakeMatches(theText);//格式化匹配的文本
		theMatches=theMatches.join().replace(/\,/gi,"");
		
		//changeIMG(document.getElementById('img1'));
		
		//显示结果
		if(theMatches.length>0){
			document.getElementById("spanOutput").innerHTML=theMatches;
			//document.getElementById("OptionsList_0").className="spanHighElement";
			//document.getElementById("ResultList_0").className="resultHighData";
			currentValueSelected=-1;
			bNoResults=false;
		}else{//显示没有匹配
			currentValueSelected=-1;
			bNoResults=true;
			if(theTextBox.obj.showNoMatchMessage)										// 现在调用自己的方法，  下面注释的是原先的方法
				document.getElementById("spanOutput").innerHTML="<span class='noMatchData'>"+getOutMess()/*theTextBox.obj.noMatchingDataMessage*/+"</span>";
			else
				HideTheBox(); 
		}
	}
	function TypeAhead(xStrText){
		var strParams="query="+encodeURI(xStrText)+"&where="+theTextBox.obj.matchAnywhere;
		var loader1=new SuggestAjax(theTextBox.obj.serverCode,BuildChoices,strParams,xStrText);
	} 
	//将rewsponseText转换为数组
	function BuildChoices(request,str){
		if(request.readyState==4){
			if(request.status==200){
				var strText=request.responseText;
				eval(strText);
				BuildList(str);
				bMadeRequest=false;
			}
		}
	}
	//检测用户按键的函数
	function GiveOptions(e){
		
		/**************************/
		if(!getClassIDType()){
			//return;
		}
		/**************************/
		
		var intKey=-1;
		//检测用户按键
		if(window.event){
			intKey=event.keyCode;
			theTextBox=event.srcElement;
		}else{
			intKey=e.which;
			theTextBox=e.target;
		}

		//*************************************
		//虚拟设置另一个对象
		if(theTextBox.id!='query'){
			theTextBox = document.getElementById('query');
            
		}
		//*************************************
		
		//重置定时器
		if(theTextBox.obj.useTimeout){
			if(isTiming)EraseTimeout();
			StartTimeout();
		}
		
		//确定是否存在文本
		if(theTextBox.value.length==0&&!isOpera){
			arrOptions=new Array();
			HideTheBox();
			strLastValue="";
			return false;
		}
		//确定功能键
		if(objLastActive==theTextBox){
			if(intKey==13){
				GrabHighlighted();
				theTextBox.blur();
				return false;
			}else if(intKey==38){
				MoveHighlight(-1);
				return false;
			}else if(intKey==40){
				MoveHighlight(1);
				return false;
			}
		}
		//处理按键操作
		if(objLastActive!=theTextBox||theTextBox.value.indexOf(strLastValue)!=0||(arrOptions.length==0||arrOptions.length==15)||(theTextBox.value.length<=strLastValue.length)){
			objLastActive=theTextBox;
			bMadeRequest=true;
			TypeAhead(theTextBox.value.Trim())//ajax处理
		}else if(!bMadeRequest){
			BuildList(theTextBox.value.Trim());
		}
		strLastValue=theTextBox.value;//保存用户输入
		
		
		//changeIMG(document.getElementById('img1'));
	}
	function AddHandler(objText){
		objText.onkeyup=GiveOptions;
		objText.onmouseup=GiveOptions;
		//objText.onmouseover=GiveOptions;
		//document.getElementById('img1').onclick=GiveOptions;
		/**
		objText.onblur=function(){
			if(this.obj.userTimeout)StartTimeout();
			if(currentValueSelected<0)HideTheBox();
		}
		**/
		if(isOpera)objText.onkeypress=GiveOptions;
	}
	//为文本框添加提示功能（属性）函数,事件处理(AddHandler)
	//xElem---文本框
	//xHidden--用来保存值的hidden元素
	//xserverCode--服务器url
	//xignoreCase--是否在搜索过程中忽略大小
	//xmatchAnywhere--是否在字符串中匹配任意位置的文本
	//xmatchTextBoxWidth--是否匹配文本框宽度
	//xshowNoMatchMessage--是否显示无匹配消息
	//xnoMatchingDataMessage--用于显示的消息
	//xuseTimeout--确定选项显示一段时间后是否隐藏
	//xtheVisibleTime--span保持打开的时间
	function SetProperties(xElem,xHidden,xserverCode,xignoreCase,xmatchAnywhere,xmatchTextBoxWidth,xshowNoMatchMessage,xnoMatchingDataMessage,xuseTimeout,xtheVisibleTime){
		var props={
			elem:xElem,
			hidden:xHidden,
			serverCode:xserverCode,
			regExFlags:((xignoreCase)?"i":""),
			regExAny:((xmatchAnywhere)?"^":""),
			matchAnywhere:xmatchAnywhere,
			matchTextBoxWidth:xmatchTextBoxWidth,
			theVisibleTime:xtheVisibleTime,
			showNoMatchMessage:xshowNoMatchMessage,
			noMatchingDataMessage:xnoMatchingDataMessage,
			useTimeout:xuseTimeout
		};
		AddHandler(xElem);
		return props;
	}
	//window.onload=function(){
	////var url = "http://192.168.0.27:8080/standardSearcher/FormMapServlet"
	//	var elemSpan=document.createElement("span");
	//	elemSpan.id="spanOutput";
	//	elemSpan.className="spanTextDropdown";
	//	document.body.appendChild(elemSpan);
	//	alert('zxl');
	//	document.search.query.obj=SetProperties(document.search.query,document.search.txtUserValue,baseURL,true,true,true,true,"",false,null);
	//}
	
	function winOnLoad(){
		//var url = "http://192.168.0.27:8080/standardSearcher/FormMapServlet"
		var elemSpan=document.createElement("span");
		elemSpan.id="spanOutput";
		elemSpan.className="spanTextDropdown";
		document.body.appendChild(elemSpan);
		document.search.query.obj=SetProperties(document.search.query,document.search.txtUserValue,baseURL,true,false,true,false,"该关键词已经没有查询范围，请更换查询条件",false,null);
	}
	function doSearch(){
		search.submit();
	}
	
	//*************************以下 是自己加的 ,得到类别，并判断是否是 专词提示 的类别
	function getClassIDType(classID){
		//var classID = document.search.classID.value;
		if(classID==null){
			classID = document.search.classID.value;
		}
		if(classID=="c00000012"){//如果是 专词提示
			return true;
		}else{
			return false;
		}
	}
	function getOutMess(){//得到输出信息
		if(getClassIDType()){//如果是专词提示
			outMess= "专词搜索，可以用国家（中文形式的）开头，然后通过提示搜索";
		}else{
			outMess= "";
		}
		return outMess;
	}
	function getClassValue(){//得到类别 看是否需要加上 专词的 字段
		var classID = document.search.classID.value;
		if(getClassIDType(classID)){
			classID = classID+"&types=zw";
		}
		return classID;
	}
	String.prototype.Trim = function(){return this.replace(/^\s+|\s+$/g,"");}
	//*************************以上 是自己加的
	
	
	
	
	function TypeAhead_search_bottom(xStrText){
		var strParams="query="+xStrText+"&classID=c00000012&types=zw";
		var loader1=new SuggestAjax_search_bottom(baseURL,BuildChoices_search_bottom,strParams,xStrText);
	}
	function SuggestAjax_search_bottom(url,func,param,str){
		var request;
		if(window.XMLHttpRequest){
			request=new XMLHttpRequest();
		}else if(window.ActiveXObject){
			try{
				request=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
			}
			try{
				request=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
			}
		}
		request.onreadystatechange=function(){func(request,str)};
		request.open("POST",url+"?date="+new Date().getTime(),true);
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		request.send(param);
	}
	function BuildChoices_search_bottom(request,str){
		if(request.readyState==4){
			if(request.status==200){
				var strText=request.responseText;
				eval(strText);//arrOptions
				buildBottomSearch();
			}
		}
	}
	function buildBottomSearch(){
		var htmltable = "<TABLE border=0 id='ajaxTable'>\n<TR>\n";
		for(var i=0;i<arrOptions.length;i++){
			//alert(arrOptions[i][0]);
			htmltable=htmltable+"<TD><A class='p' href='"+baseURLHome+"/talents/searchfd.jsp?classID="+getClassValue()+"&query="+encodeURI(arrOptions[i][0])+"'>"+arrOptions[i][0].replace(/ +/g,"")+"</A></TD><TD width='35'>&nbsp;</TD>\n";
			if(i!=0&&(i+1)%4==0){
				htmltable = htmltable + "</TR>\n<TR>\n";
			}
		}
		htmltable = htmltable + "</TR>\n</TABLE>\n";
		document.getElementById('searchAboutWord').innerHTML=htmltable;
	}