//得到四个对象
		function initVars() {
		  //输入文本框
		  inputField = document.getElementById("uname");
		  //下拉框
		  selectId = document.getElementById("topLevelType");
		  //层中的表格
		  nameTable = document.getElementById("name_table");
		  //下拉的层
		  completeDiv = document.getElementById("popup");
		  //表格中的表主体
		  nameTableBody = document.getElementById("name_table_body");
		  //隐藏文本域
		  inputHidden = document.getElementById("unamehid");
		  //简繁查询条件
		  inputSimple = document.getElementById("simple");
		  //剪切数据
		  clip = document.getElementById("clip");
		  
		}
		var child = null;
    	function findNames(){
    		initVars();
    		var key = window.event.keyCode;
    		if(inputField.value == inputField.defaultValue){
				inputField.style.color="#d0d0b4";
			}else{
    		//alert(key);
	    		if(key != 40 && key != 38){
	    			child = null;
	    			
	    			var uname = inputField.value.Trim();
	    			
		    		if(uname.length > 0){
		    			inputField.style.color="#000000";
		    			uname = T2S(uname);
		    			inputHidden.value=uname;
		    			
		    			autocomplete.queryCommodityByAutoComplete(uname,callback);
		    		}else{
		    			clearNames();
		    		}
	    		}
    		}
    	}
    	
    	function paste(){
    		initVars();
    		var condition = "";
    		var uname = inputField.value;
    		
    		var clipboardContent = clipboardData.getData("Text");
    		var range = document.selection.createRange();
            range.moveStart("character",-inputField.value.length);
            var cursorPosition=range.text.length;
            var selection = document.selection.createRange().duplicate().text;
            var uname = inputField.value;
            
            //if(cursorPosition <= uname.length){
            	
           	condition = uname.substring(0,cursorPosition-selection.length) + clipboardContent + uname.substring(cursorPosition);
            //}else{
            //	condition = uname + clipboardContent;
            //}
            inputField.value = condition;
            inputSimple.value = condition;
           // alert(cursorPosition+" , "+uname + " , "+condition+" , "+clipboardContent);
            findNames();
            
           
            
    	}
    	
    	function cut(){
    		initVars();
    		var range = document.selection.createRange();
            range.moveStart("character",-inputField.value.length);
            var cursorPosition=range.text.length;
            var selection = document.selection.createRange().duplicate().text;
            var uname = inputField.value;
            var condition = uname.substring(0,cursorPosition-selection.length)+uname.substring(cursorPosition);
            inputField.value = condition;
            inputSimple.value = condition;
            clip.value = selection;
    		//alert(cursorPosition +" , "+range.text+" , "+selection);
    		findNames();
    	}
    	
    	
    	function findTopNames(){
    		var key = window.event.keyCode;
    		if(key != 40 && key != 38){
    			child = null;
    			initVars();
    			
	    		if(inputField.value.length > 0){
	    			DWRService.queryCommodityByTopAutoComplete(inputField.value,selectId.value,callback);
	    		}else{
	    			clearNames();
	    		}
    		}
    	}
    	// 回调函数
    	function callback(the_names){
    		  if(inputSimple.value!=""){	
    		  	inputField.value = inputSimple.value;
    		  	inputSimple.value = "";
    		  }
    		  if(clip.value != ""){
    		  	clipboardData.setData("text",clip.value);
    		  	clip.value = "";
    		  }
    		 //清除表格原有的内容
			  clearNames();
			  var size = the_names.length;
			  
			  if(size>0){
			  	//设置表格的位置
			 	 setOffsets();
			  }
			  //单元格的行，列，文本节点
			  var row, cell, txtNode;
			  for (var i = 0; i < size; i++) {
			    //名字的内容
			    var nextNode = the_names[i];
			    //建立一行
			    row = document.createElement("tr");
			    cell = document.createElement("td");
			    //匿名函数
			    cell.onmouseout = function() {if(child!=null){child.style.backgroundColor = "";child=row;}this.className = 'mouseOverQ';} ;
			    cell.onmouseover = function() {if(child!=null){child.style.backgroundColor = "";child=row;}this.className = 'mouseOutQ';} ;
			    //设置单元格的属性
			    cell.setAttribute("background", "#FFFFFF");
			    cell.setAttribute("border", "0");
			    //点击，选到文本框中
			    cell.onclick = function() {populateName(this);};
				
			    txtNode = document.createTextNode(nextNode);
			    //文本加到单元格
			    cell.appendChild(txtNode);
			    //单元格加到表格行
			    row.appendChild(cell);
			    //行加到表格
			    nameTableBody.appendChild(row);
			    
			  }
			 
			  if(inputField.value.length <= 0){
			  	 clearNames();
			  }
			  
			  
    	}
    	
    	function setOffsets() {
		  //文本框对象的可见宽度
		  var end = inputField.offsetWidth;
		  //文本框
		  var left = calculateOffsetLeft(inputField);
		  //层的顶部位置
		  var top = calculateOffsetTop(inputField) + inputField.offsetHeight;
		  //设置层的位置
		  completeDiv.style.border = "black 1px solid";
		  completeDiv.style.left = left + "px";
		  completeDiv.style.top = top + "px";
		  //表格的宽度
		  nameTable.style.width = end + "px";
		}
		
		function calculateOffsetLeft(field) {
		  return calculateOffset(field, "offsetLeft");
		}
		
		function calculateOffsetTop(field) {
		  return calculateOffset(field, "offsetTop");
		} 
		
		//计算位置的函数：元素，属性
		function calculateOffset(field, attr) {
		  var offset = 0;
		  while (field) {
		    //文本框[属性]，这种写法得到当前元素相对于父元素的偏移值
		    offset += field[attr];
		    field = field.offsetParent;
		  }
		  return offset;
		}
		  
		function populateName(cell) {
		  //选中的单元格的值放到文本框中
		  inputField.value = cell.firstChild.nodeValue;
		  inputHidden.value = cell.firstChild.nodeValue;
		  clearNames();
		  document.forms[0].submit();
		}
		  
		function clearNames() {
			initVars();
		  //有多少行
		  var ind = nameTableBody.childNodes.length;
		  for (var i = ind - 1; i >= 0; i--) {
		    //删除每一行
		    nameTableBody.removeChild(nameTableBody.childNodes[i]);
		  }
		  //层的外框
		  completeDiv.style.border = "none";
		}
		//当文本框失去焦点时，隐藏提示层
		function showUserNone(){
			initVars();
			
			var offsetParent=nameTableBody,x,y;
			leftx=0;
			topy=0;
			while(offsetParent!=null && offsetParent.tagName.toUpperCase()!="BODY") {
				leftx+=offsetParent.offsetLeft;
				topy+=offsetParent.offsetTop;
				offsetParent=offsetParent.offsetParent;
			}
			var x = window.event.x;
			var y = window.event.y;
			var width = nameTableBody.offsetWidth;
			var height = nameTableBody.offsetHeight;
			if(((leftx+width)<x || x<leftx) && ((topy-height)<y || y<topy)){
				clearNames();
			}
			if(inputField.value == ""){
				inputField.style.color="#d0d0b4";
				inputField.value = inputField.defaultValue;
				inputHidden.value = "";
			}
			
		}
		//上下键选择
		window.document.onkeydown = function () {
   				initVars();
				var key = window.event.keyCode;
				
				//向下
				if(key == 40){
					if(child == null){
						var nextNode  = nameTableBody.firstChild;
						if(nextNode != null){
							child = nextNode;
							child.style.backgroundColor = "#1A66B3";
							inputField.value = nextNode.firstChild.innerHTML;
							inputHidden.value = nextNode.firstChild.innerHTML;
						}
					}else{
						nextNode = child.nextSibling;
						if (nextNode != null) {
							child.style.backgroundColor = "#FFFFFF";
							child = child.nextSibling;
							child.style.backgroundColor = "#1A66B3";
							inputField.value = nextNode.firstChild.innerHTML;
							inputHidden.value = nextNode.firstChild.innerHTML;
						}
					}
				}else if(key == 38){//向上
				
					if(child != null){
						var previousNode = child.previousSibling;
						if(previousNode!=null){
							child.style.backgroundColor = "#FFFFFF";
							child = child.previousSibling;
							child.style.backgroundColor = "#1A66B3";
							inputField.value = previousNode.firstChild.innerHTML;
							inputHidden.value = previousNode.firstChild.innerHTML;
						}
					}
				}
				
			}
			//空值验证
			function checkuname(){
				initVars();
				var unameValue = inputField.value.Trim();
				//if(!/\d*|\w*/.test(unameValue)){unameValue=""}
				//unameValue = unameValue.replace(/\\/g,"").replace(/'/g,"");
				//var a = "\"\"\"";
				//alert(unameValue);
				if(unameValue.length<=0 || unameValue == inputField.defaultValue){
					inputField.style.color="#d0d0b4";
					inputField.value = "";
					inputField.focus();
					return false;
				}
				
				inputField.value = unameValue;
				inputHidden.value = unameValue
				return true;
			}
			
			String.prototype.Trim = function()    
			{    
				return this.replace(/(^\s*)|(\s*$)/g, "");    
			}
			
			function onFocusClear(){
				initVars();
				var unameValue = inputField.value;
				if(unameValue=="" || unameValue == inputField.defaultValue){
					inputField.value = "";
					inputField.style.color="#d0d0b4";
					inputHidden.value="";
				}
				
			}
			
