/*	------------------------------------
 *	Leexoo web-JavaScript Leo 2009.09.11
 *	-----------------------------------*/ 
 		 index = -1;
         content = "";
         var inputField;
		 
         // 通过DWR调用业务层的方法。
         function findAll() {
             index = 0;
             // 清除层数据
             clearItems();
             // 设置层显示位置  
             setPopupOffset();
             var keyword = DWRUtil.getValue("login_search");
             if(keyword == ""){
              	clearItems(); 
              	return;
              }
             var reg = /^[\u4e00-\u9fa5]+$/i; 
             var flag = reg.test(keyword);
             DWREngine.setAsync(false);
             LeexooDwrManageImpl.getAssnRelatedKeywords(keyword,flag,function(codeCatas) { 
                  for(var i = 0; i < codeCatas.length; i++) { 
                    row = document.createElement("tr");
                    cell = document.createElement("td");
                    cell.onmouseout = function() {this.style.backgroundColor='';};
                    cell.onmouseover = function() {
                    	this.style.backgroundColor='gray';
                    	// inputField.value = this.firstChild.nodeValue;
                    	};
                    cell.onclick = function() { 
	                    inputField.value = this.firstChild.nodeValue;
	                    clearItems();
	                    formSearch();
                     };
                  // cell.onkeydown = function() {procTableKeyDown();};
                    cell.setAttribute("popup", "cell");  //cell.pop="cell";    // 自定义属性  
                    cell.setAttribute("height","20");                    
                    textNode = document.createTextNode(codeCatas[i].key); 
                    	
                    cell.appendChild(textNode);
                    row.appendChild(cell);
                    thePopupBody = document.getElementById("popupBody");
                    thePopupBody.appendChild(row);
                }
              });
              DWREngine.setAsync(true);
         }

		// 清除并隐藏层
         function clearItems() {
            thePopupBody = document.getElementById("popupBody");
            var numCell = thePopupBody.childNodes.length;            
            for(var i = numCell-1; i >= 0; i--) {
                thePopupBody.removeChild(thePopupBody.childNodes[i]);
            }
            thePopupDiv= document.getElementById("popupDiv");
            thePopupDiv.style.border = "none";            
        }

		// 设置下拉层显示位置
        function setPopupOffset() { 
             inputField = document.getElementById("login_search");
             var popupDivLeft = calculatePopupOffset(inputField,"offsetLeft");
             var popupDivTop = calculatePopupOffset(inputField,"offsetTop") + inputField.offsetHeight;
             thePopupDiv.style.width =  inputField.offsetWidth + "px";
             thePopupDiv.style.border = "black 1px solid";
             thePopupDiv.style.left = popupDivLeft+ "px";
             thePopupDiv.style.top = popupDivTop-11 + "px";
             thePopupTable = document.getElementById("popupTable");
             thePopupTable.style.width = thePopupDiv.style.width;
             thePopupTable.style.zIndex = "99999";
        }

		// 根据输入框的位置计算层的显示起始位置 (top and left)
        function calculatePopupOffset(element, attr) {
             var offset = 0;
             while(element) {
                  offset += element[attr];               
                  element = element.offsetParent;
             }
             return offset;
          }
	
		// 鼠标点击屏幕上其他地方清除显示层
        document.onmousedown = function(){
            if(!event.srcElement.popup){
                 clearItems();
            }
        }  

        // 将焦点定位在某个元素上。
        function focusOnElement(element) {
           setTimeout(function(){element.focus();}, 0);      
        }      

         // 实现用户按向上键、向下键和回车键时的事件处理
         // 按向上键导航到表中的最后一条记录。
         // 按向下键导航到表中的第一条记录。
         // 通过和函数procTableKeyDown()的配合，实现了在文本框和表中记录之间的循环导航。
         function procInputItemKeyDown() {  
              if (false){ // UP DOWN key           
                cellNum = thePopupBody.childNodes.length;
                if (cellNum <= 0 ) {
                    return;
                }

                if (event.keyCode == 38) {
                    index = cellNum - 1;  // 按向上箭头键使焦点跳到表中的最后一条记录。
                } else {
                    index = 0; // 按向下箭头键使焦点跳到表中的第一条记录。
                }
                focusOnElement(thePopupBody.childNodes[index].firstChild); // 获取焦点
                thePopupBody.childNodes[index].firstChild.style.backgroundColor='gray';
                content = inputField.value;
                inputField.value = thePopupBody.childNodes[index].firstChild.firstChild.nodeValue;
              } 
              // 数字键0到字母z 以及 BackSpace 键 event.keyCode > 47 && event.keyCode < 106 || event.keyCode == 8
              if(true){
              		findAll();
              } 
         }        

         // 实现用户按向上键、向下键和回车键时的事件处理
         // 通过按向上键、向下键实现在表中记录间的导航。如果导航到表中的第一条记录，则按向上键将切换到文本框；
         // 如果导航到表中的最后一条记录，则按向下键将切换到文本框。
         // 按回车键将切换到文本框，同时消除表。
         // 按向上键、向下键和回车键时，将选中的记录在文本框中显示。
         // 通过和函数procInputItemKeyDown()的配合，实现了在文本框和表中记录之间的循环导航。
         function procTableKeyDown() {
            if (thePopupBody.childNodes.length <= 0) {
               return;
            }

              if (event.keyCode == 38 || event.keyCode == 40) {    // UP DOWN
                  boundary = false;
                  thePopupBody.childNodes[index].firstChild.style.backgroundColor='';                 
	                  if (event.keyCode == 38){ //向上箭头键
	                       if (index <= 0 ) {
	                           boundary = true;
	                       } else {                       
	                           index--;
	                      }
	                  }else { 					//if (event.keyCode == 40) { // 向下箭头键
	                   if (index >= thePopupBody.childNodes.length - 1) {
	                        boundary = true;
	                   } else {
	                        index++;
	                     }                                    
	                  }
	                  if (boundary){ // 如果已经是表中的第一条记录或者最后一条记录，那么下次焦点跳到文本框。
	                      focusOnElement(inputField);
	                      inputField.value = content;
	                  }else { // 如果不是第一条记录或最后一条记录，那么光标向上或向下移动一条记录。
	                      thePopupBody.childNodes[index].firstChild.style.backgroundColor='gray';
	                      inputField.value = thePopupBody.childNodes[index].firstChild.firstChild.nodeValue;
	                  }
              }

             if (event.keyCode == 13) { // ENTER                  
               	  inputField.value = thePopupBody.childNodes[index].firstChild.firstChild.nodeValue;
                  clearItems();
                  focusOnElement(inputField);
             }
         }