Var $ = function (a_sID) {return document. getElementById (a_sID );}
Var $ = function (a_sName) {return document. getElementsByTagName (a_sName );}
Var $ F = function (a_sID ){
Var oObj = $ (a_sID );
If (! OObj) oObj = document. forms [a_sID];
Return oObj;
}
Function TSuggestionBox (){
// Private attributes
This. _ boxes = {};
This. _ position = 0;
This. _ suggestion = "";
This. _ object = this;
// Private Method
This. _ setStyle = TSuggestionBox_SetStyle;
This. _ getSearchResult = TSuggestionBox_GetSearchResult;
This. _ listenItem = TSuggestionBox_ListenItem;
// Public Method
This. listen = TSuggestionBox_Listen;
This. clear = TSuggestionBox_Clear;
}
Function TSuggestionBox_Listen (a_sSearchBoxId, a_sSuggestionBoxId, a_sURL, a_sFormId) {// listen to the search box and search suggestion box
Var oSearchBox =$ (a_sSearchBoxId );
If (! OSearchBox) return;
This. _ boxes [a_sSearchBoxId] = [a_sSuggestionBoxId, a_sURL, a_sFormId];
Var oSuggestionBox = this. _ object;
OSearchBox. onkeydown = function (e ){
Var oObj = $ (a_sSuggestionBoxId );
If (! OObj) return;
Var oItems = oObj. getElementsByTagName ("li ");
If (! OItems) return;
Var iKeyCode, iPos = oSuggestionBox. _ position;
If (window. event) {iKeyCode = window. event. keyCode ;}
Else if (e. which) {iKeyCode = e. which ;}
If (iKeyCode! = 40 & iKeyCode! = 38) return; // when the direction key is not up or down
// Alert ('up or up direction key? ');
If (iKeyCode = 40) {// downward arrow key
IPos ++;
If (iPos >= oItems. length) {iPos = 0 ;}
}
If (iKeyCode = 38) {// The up arrow key
IPos --;
If (iPos <0) {iPos = oItems. length-1 ;}
}
OSuggestionBox. _ position = iPos;
OSuggestionBox. _ setStyle ();
If (iPos> = 0 & iPos <oItems. length) {oSearchBox. value = oItems [iPos]. childNodes [0]. nodeValue ;}
Else {$ (a_sSearchBoxId). value = oSuggestionBox. _ suggestion ;}
};
OSearchBox. onkeyup = function (e ){
Var iKeyCode;
If (window. event) {iKeyCode = window. event. keyCode ;}
Else if (e. which) {iKeyCode = e. which ;}
If (iKeyCode! = 40 & iKeyCode! = 38) {// non-up or down key
OSuggestionBox. _ getSearchResult (a_sSearchBoxId );
OSuggestionBox. _ suggestion = oSearchBox. value;
}
};
OSearchBox. onclick = function (e ){
Var iKeyCode;
If (window. event) {iKeyCode = window. event. keyCode ;}
Else if (e. which) {iKeyCode = e. which ;}
If (iKeyCode! = 40 & iKeyCode! = 38 ){
OSuggestionBox. _ getSearchResult (a_sSearchBoxId );
OSuggestionBox. _ suggestion = oSearchBox. value;
}
};
}
Function TSuggestionBox_GetSearchResult (a_sSearchBoxId) {// obtain the search result
Var oBox = this. _ boxes [a_sSearchBoxId], oSuggestionBox = this. _ object, oHttpRequest;
Var iPos = this. _ position;
If (! OBox) return;
Try {oHttpRequest = new XMLHttpRequest ();}
Catch (e) {oHttpRequest = new ActiveXObject ("Microsoft. XMLHTTP ");}
OHttpRequest. onreadystatechange = function (){
If (oHttpRequest. readyState = 4 ){
If (oHttpRequest. status = 200 ){
Var sResult = oHttpRequest. responseText;
$ (OBox [0]). innerHTML = sResult;
If (sResult ){
OSuggestionBox. _ position = 0;
OSuggestionBox. _ listenItem (a_sSearchBoxId); // listen to the options in suggestionBox
}
}
}
}
OHttpRequest. open ("post", oBox [1], true );
OHttpRequest. setRequestHeader ('content-type', 'application/x-www-form-urlencoded ');
OHttpRequest. send ("keyword =" + escape ($ (a_sSearchBoxId). value ));
}
Function TSuggestionBox_SetStyle () {// set the style of the suggested prompt box
Var oNodes = $ ("li"), oNode;
Var iPos = this. _ position;
For (var I = 0; I <oNodes. length; I ++ ){
ONode = oNodes [I];
ONode. className = "";
}
If (iPos> = 0 & iPos <oNodes. length ){
ONode = oNodes [iPos];
ONodes [iPos]. className = "select ";
}
}
// Hide the suggestion box
Function TSuggestionBox_Clear (a_sID) {this. _ boxes [a_sID]. innerHTML = "";}
Function TSuggestionBox_ListenItem (a_sSearchBoxId) {// listen to SuggestionBox Item
Var oBox = this. _ boxes [a_sSearchBoxId];
Var oSuggestionBox = this. _ object;
Var iPos = this. _ position;
Var sFormId = "";
If (! OBox) return;
Var oObj = $ (oBox [0]);
If (! OObj) return;
If (oBox. length> 2) sFormId = oBox [2];
Var oItems = oObj. getElementsByTagName ("li ");
If (! OItems) return;
Var oItem;
For (var I = 0; I <oItems. length; I ++ ){
OItem = oItems [I];
If (! OItem) continue;
OItem. onclick = function () {// submit a search form
Var iPos = oSuggestionBox. _ position;
Var oForm = $ F (sFormId );
If (! OForm) return;
OForm. submit ();
};
OItem. onmouseover = function (){
OSuggestionBox. _ position = this. value;
OSuggestionBox. _ setStyle ();
$ (A_sSearchBoxId). value = this. childNodes [0]. nodeValue;
};
}
If (oItems. length> 0) oItems [0]. className = "select ";
}