/*升级 查找单位by z.f. begin*/
/*用法 (前提 使用JQ)
默认初始化实例名为：searchSelect
插入需要的文件 部分

开始：
var searchSelect;
$(document).ready(
	function(){ 
		 searchSelect=new SelectSearch("school2","schoolKey","searchSelect");
	}
); 
结束

事件触发的文本框。目前已动态加载
<input type="text" id="schoolKey"  value="请输入关键字查询"  onMouseDown="searchSelect.clearunInit()"  onKeyDown="searchSelect.getSelectValue()" style="color:#999999" onKeyUp="searchSelect.showSearch()" >
*/
function SelectSearch(searchId,keyId,InstantiatName,actionType){//需要两个id 第一个是 被查询的下拉框id ,第二个是关键字id（可以随便）,第3个该类的实例名
	this.searchId=searchId;
	this.InstantiatName=(InstantiatName==""||InstantiatName==null)?"searchSelect":InstantiatName;//该类实例化名称
	this.selectObj=$("#"+this.searchId);
	this.keyId=keyId;
	this.initValue="输入关键字后按回车查询";
	this.actionType=actionType;
	this.insertText(this.keyId,this.initValue);// 在searchId后插入文本框
	
	this.keyWordObj=$("#"+this.keyId);
	this.schoolAll=$("#"+this.searchId+" option");//初始化 用 还原
	this.selectionValue="";//文本选中区的value
	this.selectedValue=this.selectObj.val()||"";  //下拉框初始化选中value
}





SelectSearch.prototype.insertText=function (idStr,valueStr){
		//动态添加 关键字 文本框
		 //在school2后添加
		this.selectObj.after("&nbsp;&nbsp;<input type='text' id='"+idStr+"'  value='"+valueStr+"'  onMouseDown='"+this.InstantiatName+".clearunInit()'  onKeyDown='"+this.InstantiatName+".getSelectionValue()' style='color:#999999' onMouseMove='"+this.InstantiatName+".getSelectionValue()'  onKeyPress='"+this.InstantiatName+".showSearch(event);' >") ;

}



SelectSearch.prototype.getSelectionValue=function (){
   try{
		this.selectionValue="";
		var pos=document.selection.createRange();//获取选中区
		var posParent=pos.parentElement()?pos.parentElement():""; 
		if (posParent&&(posParent.tagName.toLowerCase()=="input")&&posParent.id==this.keyId){
			this.selectionValue=pos.text;
		}else{
			this.selectionValue="";
		}
	}catch(e){
		this.selectionValue="";
	}
}


SelectSearch.prototype.clearunInit=function clearunInit(){
   if (this.keyWordObj.val().trim()==this.initValue){
		this.keyWordObj.val("");
		this.keyWordObj.css("color","#000000");
   }
}

SelectSearch.prototype.clearInit=function (){
  if (this.keyWordObj.val().trim()==""){
		this.keyWordObj.val(this.initValue);
		this.keyWordObj.css("color","#999999");
  }
}

SelectSearch.prototype.batKey=function(){
		//分隔 关键字
		var word_key=this.keyWordObj.val().trim();
		var findValue="";
		if (word_key.indexOf(" ")>0){
			var word_key_part=word_key.split(" ");
			for (var i=0 ;i<word_key_part.length;i++){
				if (word_key_part[i].trim()!="")
					findValue=findValue+"[@"+this.actionType+"*='"+word_key_part[i].trim()+"']";//or 查询逗号结束
			}
		}else{
			findValue="[@"+this.actionType+"*='"+word_key+"']";//or查询逗号结束
		}
		return findValue;//.substr(0,findValue.length-1);
}


SelectSearch.prototype.showSearch=function (event){
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;  // 8退格键 46删除键  兼容ff
	var keyIdValue=this.keyWordObj.val().trim();
	
	//判断selection是否为但前的value
	if(keyCode!=13&&keyCode!=8&&keyCode!=46 ){return ;}
	if (keyIdValue==""||keyCode==8||keyCode==46||this.selectionValue!=""){
		this.selectObj.html(this.schoolAll);
	}
	
	if (keyIdValue!=""){
		this.selectObj.html(this.selectObj.find(this.batKey()));
	}
	//初始化选中项 待定
	//发现有 选中项的，为当前，没有 first
	if (this.selectObj.find("option[@"+this.actionType+"="+this.selectedValue+"]").val()!=""&&this.selectObj.find("option[@"+this.actionType+"="+this.selectedValue+"]").val()!=null){
		this.selectObj.val(this.selectedValue)
	}else{
		this.selectObj.val(this.selectObj.children().val());
		//document.getElementById(this.searchId).selectedIndex =0
	}
	document.getElementById(this.searchId).fireEvent("onchange")

}



String.prototype.trim = function()//相当于VB中的trim();
{
    return this.replace(/(^\s*)|(\s*$)/g,"");
}
/*升级 查找单位by z.f. end */
