
function AC(id,handler){this.type=this.id;this.obj=document.getElementById(id);this.url='/auto/ac.php';this.obj.completehandler=handler
this.submit_callback=(arguments.length>2)?arguments[2]:null;this.div=document.createElement('DIV');this.div.className='ac_menu';this.div.style.visibility='hidden';this.div.style.position='absolute';this.div.style.zIndex=10;this.div.style.width=this.obj.offsetWidth-2+"px";this.div.style.left=this.total_offset(this.obj,'offsetLeft')+"px";this.div.style.top=this.total_offset(this.obj,'offsetTop')+this.obj.offsetHeight-1+"px";this.obj.parentNode.insertBefore(this.div,this.obj.nextSibling);this.iframe=null;this.obj.onkeydown=this.onkeydown;this.obj.onkeyup=this.onkeyup;this.obj.onkeypress=this.onkeypress;this.obj.onblur=function(){this.AC.close_popup();}
this.obj.AC=this;this.selected_option=null;this.request=null;this.cache=new Array();this.typing=false;this.typing_timeout=10;this.sending_timeout=10;this.search_term=null;this.previous_term=null;this.searched_term=null;this.last_input=null;this.poll_input=false;_ac_map_add(this);}
AC.prototype.enable_unicode=function(){this.poll_input=true;_ac_key_check(this,this.typing_timeout);}
AC.prototype.total_offset=function(element,property){var total=0;while(element){total+=element[property];element=element.offsetParent;}
return total;}
AC.prototype.close_popup=function(){this.div.style.visibility='hidden';this.selected_option=null;this.typing=false;this.search_term=null;this.previous_term=null;}
AC.prototype.XMLHttpRequest=function(){var request=null;if(typeof XMLHttpRequest!='undefined'){request=new XMLHttpRequest();}else{try{request=new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{request=new ActiveXObject('Microsoft.XMLHTTP')}catch(e){request=null}}}
return request;}
var _ac_key_thunk=new Array();function _ac_key_thunk_call(id){if(_ac_key_thunk[id]){var ac=_ac_key_thunk[id][1];var unselected=ac.obj.value;if(document.selection){var range=document.selection.createRange();if(range){var length=unselected.length-range.text.length;if(length>0){unselected=unselected.substring(0,length);}}}else if(ac.obj.setSelectionRange){var length=ac.obj.selectionEnd-ac.obj.selectionStart;if(length>0)
unselected=unselected.substring(0,ac.obj.selectionStart);}
if(unselected!=ac.last_input){if(unselected.length>0){ac.searched_term=unselected;ac.suggest(ac.searched_term);}else{_ac_cancel(ac);ac.close_popup();}
ac.last_input=unselected;}
if(ac.poll_input){_ac_key_thunk[id][2]=setTimeout("_ac_key_thunk_call("+id+")",ac.typing_timeout);}else{_ac_key_thunk[id]=null;for(i=_ac_key_thunk.length;i>0;i--)
if(_ac_key_thunk[i]==null)
_ac_key_thunk.length--;}}}
function _ac_key_check(ac,timeout){for(i=_ac_key_thunk.length-1;i>=0;i--)
if(_ac_key_thunk[i]!=null&&_ac_key_thunk[i][0]==ac.obj.id){clearTimeout(_ac_key_thunk[i][2]);_ac_key_thunk[i]=null;}
var i=_ac_key_thunk.length;var handle=setTimeout("_ac_key_thunk_call("+i+")",timeout);_ac_key_thunk[i]=new Array(ac.obj.id,ac,handle);}
var _ac_thunk=new Array();function _ac_thunk_call(id){if(_ac_thunk[id]){var ac=_ac_thunk[id][1];ac.typing=false;ac.send(_ac_thunk[id][2]);_ac_thunk[id]=null;for(i=_ac_thunk.length;i>0;i--)
if(_ac_thunk[i]==null)
_ac_thunk.length--;}}
function _ac_cancel(ac){for(i=_ac_thunk.length-1;i>=0;i--)
if(_ac_thunk[i]!=null&&_ac_thunk[i][0]==ac.obj.id){clearTimeout(_ac_thunk[i][3]);_ac_thunk[i]=null;}}
function _ac_add(ac,query,timeout){var i=_ac_thunk.length;var handle=setTimeout("_ac_thunk_call("+i+")",timeout);_ac_thunk[i]=new Array(ac.obj.id,ac,query,handle);}
var _ac_map=new Array();function _ac_map_add(ac){_ac_map[ac.obj.id]=ac;}
AC.prototype.suggest=function(query){if(query==this.search_term)
return;_ac_cancel(this);if(this.request&&this.request.readyState!=0){this.request.abort();}
var lc=query.toLowerCase();for(i=0;i<this.cache.length;i++)
if(this.cache[i][0]==lc){var results=this.cache[i][1];this.search_term=query;this.update_popup(results);return;}
this.typing=true;this.send(query);}
AC.prototype.send=function(query){if(this.typing){_ac_add(this,query,this.sending_timeout);return;}
this.search_term=query;if(this.iframe==null){this.request=this.XMLHttpRequest();if(this.request==null){var iframe=document.createElement('IFRAME');iframe.src=this.url+'?i=1&id='+encodeURIComponent(this.obj.id)+'&t='+encodeURIComponent(this.type)+'&s='+encodeURIComponent(query);iframe.style.width='0px';iframe.style.height='0px';this.iframe=this.obj.appendChild(iframe);this.obj.focus();}else{var AC=this;this.request.onreadystatechange=function(){if(AC.request.readyState==4){try{if(AC.request.status==200)
{var xmlDoc=AC.request.responseXML;if(xmlDoc.documentElement)
{var results=new Array();var searchTerm=xmlDoc.documentElement.getAttribute("s");if(searchTerm!=null)
{if(searchTerm!=query)
{return;}}
var locationElts=xmlDoc.documentElement.getElementsByTagName("l");for(var i=0;i<locationElts.length;i++)
{var name=locationElts[i].getAttribute("n");var html="<span class='a'>"+name+"</span>";results[i]=new Array(name,i,html);}
AC.update_popup(results);}}}catch(e){}}}
this.request.open("GET",this.url+"?id="+encodeURIComponent(this.obj.id)+"&t="+encodeURIComponent(this.type)+"&s="+encodeURIComponent(query));this.request.send(null);}}else{this.iframe.src=this.url+'?i=1&id='+encodeURIComponent(this.obj.id)+'&t='+encodeURIComponent(this.type)+'&s='+encodeURIComponent(query);this.obj.focus();}}
AC.prototype.update_popup=function(results){if(this.search_term!=null&&results!=null&&results.length>0){while(this.div.hasChildNodes())
this.div.removeChild(this.div.firstChild);if(this.previous_term==null||this.search_term.length>=this.previous_term.length){this.selected_option=0;}else{this.selected_option=null;}
this.previous_term=this.search_term;for(i=0;i<results.length;i++){var div=document.createElement('DIV');div.divid=results[i][1];div.AC=this;if(this.selected_option==div.divid)
div.className='ac_highlight';else
div.className='ac_normal';div.name=results[i][0];div.value=div.name;div.innerHTML=results[i][2];div.onmousedown=function()
{this.AC.onselected();var savedValue=this.AC.obj.value
this.AC.obj.value='';this.AC.obj.value=savedValue;if(this.completehandler)
{this.completehandler();}}
div.onmouseover=function()
{if(this.AC.selected_option!=null)
this.AC.div.childNodes[this.AC.selected_option].className='ac_normal';this.AC.selected_option=this.divid;this.AC.cabbage=this.AC.selected_option;this.className='ac_highlight';}
div.onmouseout=function()
{this.className='ac_normal';}
this.div.appendChild(div);}
this.div.style.visibility='visible';if(this.selected_option==0&&(this.obj.createTextRange||this.obj.setSelectionRange)&&this.obj.value!=results[0][0]&&results[0][0].substring(0,this.search_term.length).toLowerCase()==this.search_term.toLowerCase())
{if(this.obj.value!=this.search_term)
{return;}
this.obj.value=results[0][0];if(this.obj.createTextRange){var range=this.obj.createTextRange();range.moveStart('character',this.search_term.length);range.select();}else{this.obj.setSelectionRange(this.search_term.length,this.obj.value.length);}}}else{this.close_popup();}
var found=false;var lc=this.search_term.toLowerCase();for(i=0;i<this.cache.length;i++)
if(this.cache[i][0]==lc){found=true;break;}
if(!found){this.cache[this.cache.length]=new Array(lc,results);}}
AC.prototype.update_input=function(){if(this.div.childNodes[this.selected_option].name)
{this.obj.value=this.div.childNodes[this.selected_option].name;}}
AC.prototype.onselected=function(){if(this.selected_option==null)
if(this.cabbage==null)
return;else
this.selected_option=this.cabbage;this.update_input();this.close_popup();if(this.submit_callback)
this.submit_callback();var btnSearch=document.getElementById('btnSearch');if(btnSearch)
{btnSearch.focus();}
var txtSearch=document.getElementById('txtSearch');if(txtSearch)
{txtSearch.focus();}}
AC.prototype.onkeypress=function(e){if(!e)e=window.event;var c=e.keyCode;if(c==0)c=e.charCode;switch(c){case 13:e.returnValue=false;if(e.preventDefault)e.preventDefault();break;case 38:case 40:e.returnValue=false;if(e.preventDefault)e.preventDefault();break;default:break;}}
AC.prototype.onkeydown=function(e){if(!e)e=window.event;var c=e.keyCode;if(c==0)c=e.charCode;var i=this.AC.selected_option==null?-1:this.AC.selected_option;switch(c){case 38:i--;e.returnValue=false;if(e.preventDefault)e.preventDefault();break;case 40:i++;e.returnValue=false;if(e.preventDefault)e.preventDefault();break;case 13:e.returnValue=false;if(e.preventDefault)e.preventDefault();e.cancelBubble=true;if(this.completehandler)
{this.completehandler();}
var savedValue=this.AC.obj.value
this.AC.obj.value='';this.AC.obj.value=savedValue;var btn=document.getElementById('btnSearch');fnTrapKD(btn,e);break;default:_ac_key_check(this.AC,this.AC.typing_timeout);break;}
if(c==38||c==40){var length=this.AC.div.childNodes.length;if(i<0)i=0;if(i>=length)i=length-1;if(i!=this.AC.selected_option){for(j=0;j<length;j++){if(j==i){this.AC.obj.value=this.AC.div.childNodes[j].value;this.AC.selected_option=this.AC.div.childNodes[j].divid;this.AC.div.childNodes[j].className='ac_highlight';}else{this.AC.div.childNodes[j].className='ac_normal';}}
this.AC.search_term=this.AC.div.childNodes[this.AC.selected_option].value;if(this.AC.div.style.visibility=='hidden'){this.AC.suggest(this.AC.searched_term);}}}}
AC.prototype.onkeyup=function(e){if(!e)e=window.event;var c=e.keyCode;if(c==0)c=e.charCode;switch(c){case 38:case 40:e.returnValue=false;if(e.preventDefault)e.preventDefault();break;case 13:this.AC.onselected();e.returnValue=false;if(e.preventDefault)e.preventDefault();break;case 27:this.AC.close_popup();e.returnValue=false;if(e.preventDefault)e.preventDefault();break;default:break;}}
function _ac_rpc(){var id=arguments[0];if(_ac_map[id]){_ac_map[id].process_reply.apply(_ac_map[id],arguments);}}