﻿    var req;
    function TypeAhead(txt){
     var url="returntips.aspx?key="+escape(txt);
      if(window.XMLHttpRequest){
		req= new XMLHttpRequest();
     }
     else
     {
        req=new ActiveXObject("Microsoft.XMLHTTP");
     }
     if(txt && req){
     req.open("GET",url,true);
     req.onreadystatechange=callback;
     req.send(null);
      }
	autoComplete.currentListId = 0;
    }
    
    function callback(){
    if(req.readyState==4)
    {
       if(req.status==200)
       {
        parseMessage();
        }
     }
    }
    function parseMessage(){
    var tipDiv=document.getElementById("suggestBox");
    tipDiv.innerHTML=req.responseText;
	autoComplete.listNum = autoComplete.suggestLists.length - 1;
	if(autoComplete.listNum == 0){
		autoComplete.hideBox();
	}
	else
		autoComplete.showBox();
    }

var AutoComplete = function() {
	this.Key = {
		UP: 38,     //提示框向上，同时搜索框中关键字随之改变
		DOWN: 40,   //提示框向下，同时搜索框中关键字随之改变
		BKSPACE: 8,	//关键字退一格
		DEL: 46,	//关键字删除一字符
		ESC: 27,    //隐藏提示框，搜索框复原
		ENTER: 13   //隐藏提示框
	};
    this.listMaxNum = 10;   //最多提示条数
    this.inputBox = document.getElementById('keyword');
    this.suggestBox = document.getElementById('suggestBox');
    this.searchForm = document.getElementById('searchForm');
    this.suggestLists = this.suggestBox.getElementsByTagName('div');
    this.suggestListClass = 'AClist';
    this.suggestListClassCur = 'AClist ACcurrent';
    this.cache = new Array();		//缓存
    this.inputBox.onkeyup = this.keydown;
    this.suggestBox.onmouseover = this.mouseover;
    this.suggestBox.onclick = this.mouseclick;
    this.setDefault();
}
AutoComplete.prototype.setDefault = function(){
    this.display = false;
    this.currentListId = 0; //focus列表当前索引跟踪
    this.listNum = 0;      //实际返回的提示条数（可能超过10，超过10的已经被隐藏掉）
    this.initKeyword = "";  //用户手动输入的原始关键字
    this.hasItemFocus = false;
}
AutoComplete.prototype.blurItem = function(listId){     //listId以0~9表示列表项
    this.suggestLists[listId].className = this.suggestListClass;
}
AutoComplete.prototype.focusItem = function(listId){
    this.blurItem(this.currentListId);
    this.currentListId = listId;
    this.suggestLists[listId].className = this.suggestListClassCur;
    this.hasItemFocus = true;
}
AutoComplete.prototype.updateValue = function(listId){
    this.inputBox.value = this.suggestLists[listId].firstChild.innerHTML;
}
AutoComplete.prototype.keydown = function(e){
    e = e ? e : event;
    rootThis = autoComplete;
	var maxDispId = rootThis.listMaxNum > rootThis.listNum ? rootThis.listNum - 1 : rootThis.listMaxNum - 1;
    switch(e.keyCode) {
        case rootThis.Key.UP:
        	if(!rootThis.hasItemFocus)
        		var toUpId = maxDispId;
        	else if(!rootThis.display){
        		rootThis.showBox();
        		var toUpId = 0;
        	}
			else if(rootThis.hasItemFocus){
				var toUpId = rootThis.currentListId == 0 ? maxDispId : rootThis.currentListId - 1;
	    	}
        	if(rootThis.inputBox.value){
	            rootThis.focusItem(toUpId);
	            rootThis.updateValue(toUpId);
            }
            break;
        case rootThis.Key.DOWN:
        	if(!rootThis.hasItemFocus)
        		var toDownId = 0;
        	else if(!rootThis.display){
        		rootThis.showBox();
        		var toDownId = 0;
        	}
			else{
				var toDownId = rootThis.currentListId == maxDispId ? 0 : rootThis.currentListId + 1;
        	}
        	if(rootThis.inputBox.value){
	       	    rootThis.focusItem(toDownId);
	           	rootThis.updateValue(toDownId);
           	}
            break;
        //case rootThis.Key.ENTER:
        //    rootThis.hideBox();
        //    break;  
        case rootThis.Key.ESC:
            rootThis.inputBox.value = rootThis.initKeyword;
            rootThis.hideBox();
            break;
        case rootThis.Key.BKSPACE:
        	if(rootThis.inputBox.value)
        		TypeAhead(rootThis.inputBox.value);
        	else
        		rootThis.hideBox();
        	break;
        case rootThis.Key.DEL:
        	if(rootThis.inputBox.value)
        		TypeAhead(rootThis.inputBox.value);
        	else
        		rootThis.hideBox();
        	break;
        default:
            if(e.keyCode != 32 && e.keyCode != 13 && e.keyCode != 229 && (e.keyCode< 48 || e.keyCode > 111) ){                    //48~111:英文、数字、标点 229：中文 32:空格
            	break;
            }
            TypeAhead(rootThis.inputBox.value);
            //rootThis.showBox();
            rootThis.initKeyword = rootThis.inputBox.value;
            break;
    }
}
AutoComplete.prototype.mouseover = function(e){
    e = e ? e : event;
    target = e.target ? e.target : e.srcElement;
    rootThis = autoComplete;
    while ( (target!=rootThis.suggestBox) && (target.className.indexOf(rootThis.suggestListClass)==-1) ){
        target = target.parentNode;
    }
    if(target == rootThis.suggestBox)
        return false;
    else{
        var listId = parseInt(target.attributes["index"].value);
        rootThis.focusItem(listId);
    }
}
AutoComplete.prototype.mouseclick = function(e){
    e = e ? e : event;
    target = e.target ? e.target : e.srcElement;
    rootThis = autoComplete;
    while ( (target!=rootThis.suggestBox) && (target.className.indexOf(rootThis.suggestListClass)==-1) ){
        target = target.parentNode;
    }
    if(target == rootThis.suggestBox)
        return false;
    else{
        var listId = parseInt(target.attributes["index"].value);
        rootThis.updateValue(listId);
		document.getElementById('searchForm').submit();
        rootThis.hideBox();
    }
}
AutoComplete.prototype.showBox = function(){
   	this.suggestBox.style.visibility = "visible";
   	this.display = true;
   	if(document.getElementById('iframeSuggest'))document.getElementById('iframeSuggest').style.visibility = 'hidden';
}
AutoComplete.prototype.hideBox = function(){
    this.suggestBox.style.visibility = "hidden";
    this.display = false;
    this.hasItemFocus = false;
	if(document.getElementById('iframeSuggest'))document.getElementById('iframeSuggest').style.visibility = 'visible';//document.getElementById('iframeSuggest').style.display = 'visible';
}
//document.onclick = function(e){
//    e = e ? e : event;
//    target = e.target ? e.target : e.srcElement;
//    while(target != document && target != autoComplete.searchForm)
//        target = target.parentNode;
//    if(target == document)
//        return autoComplete.hideBox();
//    else
//        return false;
//}
var autoComplete = new AutoComplete();
