<!--
if(typeof(CQL)=='undefined')CQL={};if(typeof(CQL.AC)=='undefined')CQL.AC={};var
ARROW_L=37,ARROW_U=38,ARROW_R=39,ARROW_D=40,SPACE=32,BKSPACE=8;CQL.AC.AutoCompleteBox=function(textbox,acDataParser,acUIProvider,urlFormatString){this.DEFAULT_CALLBACK_DELAY_MS=300;this.DEFAULT_BLUR_TIMEOUT_MS=200;this.DEFAULT_LONG_BLUR_TIMEOUT_MS=1200;this.CallbackDelayMS=this.DEFAULT_CALLBACK_DELAY_MS;this.BlurTimeoutMS=this.DEFAULT_BLUR_TIMEOUT_MS;this.LongBlurTimeoutMS=this.DEFAULT_LONG_BLUR_TIMEOUT_MS;if(acDataParser==null)acDataParser=new CQL.AC.DefaultACDataParser();this.ACDataParser=acDataParser;if(acUIProvider==null)acUIProvider=new CQL.AC.DefaultACUIProvider();this.ACUIProvider=acUIProvider;this.OnSelectAndContinue=function(){alert("You must handle the OnSelectAndContinue event manually. For example:\n\nmyAutoCompleteBox.OnSelectAndContinue = function()\n{\n\talert('You selected something');\n}");};this.Div=null;this.Textbox=textbox;this.m_ElementList=null;this.URLFormatString=urlFormatString;this.UID=null;this.LastDataSourceRequestMS=null;this.DataRequestTimer=null;this.SelectedIndex=-1;this.Items=null;this.LastEnteredKeyCode=null;this.Textbox.AutoCompleteBox=this;this.UID=CQL.GenerateUniqueObjectID();CQL.OC[this.UID]=this;this.RPC=new CQL.RPC(this);this.IsUseCQLRPC=CQL.GetXMLHttpRequest()!=null;this.Init=function(){this.Div=document.createElement("DIV");document.body.appendChild(this.Div);this.Div.className="AutoCompleteBox";this.Div.style.position="absolute";this.SetDivPosition();this.SetItems([]);};this.SetDivPosition=function(){var txtTop=CQL.GetRealTop(this.Textbox);var txtLeft=CQL.GetRealLeft(this.Textbox);this.Div.style.top=(txtTop+this.Textbox.offsetHeight)+"px";this.Div.style.left=txtLeft+"px";};this.ClearContents=function(){this.Items=null;this.SetSelectedIndex(-1);this.RenderList();};this.RenderList=function(){this.m_ElementList=new Array();if(this.Textbox.value==""){this.Items=null;this.SetSelectedIndex(-1);}if(this.Items!=null){this.Div.style.display="block";this.Div.innerHTML="";for(i=0;i<this.Items.length;i++){var nodeDiv=this.ACUIProvider.GenerateLineNode(this,this.Items[i],i);this.Div.appendChild(nodeDiv);this.m_ElementList[i]=nodeDiv;}CQL.ToggleDDLsOverlappingDIV(this.Div,false);}else{this.Div.style.display="none";CQL.ToggleDDLsOverlappingDIV(this.Div,true);}};this.SetSelectedIndex=function(index){if(this.Items==null){this.SelectedIndex=-1;return;}var newIndex=index;if(index<0)newIndex=-1;else if(index>=this.Items.length)newIndex=this.Items.length-1;this.SelectedIndex=newIndex;this.RefreshSelection();};this.SetItems=function(items){if(items&&items.length)this.Items=items;else this.Items=null;this.RenderList();};this.RefreshSelection=function(){if(this.Items==null||!this.Items.length)return;for(i=0;i<this.Items.length;i++){var row=this.m_ElementList[i];if(i==this.SelectedIndex){if(row.className!="selected")row.className="selected";}else{if(row.className!="normal")row.className="normal";}}};this.SelectByEnteredText=function(isExpandText){if(this.m_ElementList==null)return;var text=this.Textbox.value;var maxMatchStrength=0;var maxMatchIndex=-1;for(var i=0;i<this.m_ElementList.length;i++){var tmpMatchStrength=this.GetMatchStrength(text.toLowerCase(),this.m_ElementList[i].AutoCompleteText.toLowerCase());if(tmpMatchStrength>maxMatchStrength){maxMatchStrength=tmpMatchStrength;maxMatchIndex=i;}}this.SetSelectedIndex(maxMatchIndex);if(isExpandText&&maxMatchIndex>=0)this.ExpandTextToMatchSelection();};this.GetMatchStrength=function(str1,str2){if(str1==null||str2==null)return 0;var numMatching=0;for(var i=0;i<str1.length;i++){if(str2.length<=i||str1.charAt(i)!=str2.charAt(i)){numMatching=0;break;}else if(str1.charAt(i)==str2.charAt(i)){numMatching++;}}return numMatching;};this.ExpandTextToMatchSelection=function(){if(navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().indexOf('safari')>=0)return;if(this.SelectedIndex<0)return;var full=this.m_ElementList[this.SelectedIndex].AutoCompleteText.toLowerCase();var txt=this.Textbox.value.toLowerCase();if(full.indexOf(txt.substring(0,this.Textbox.GetCursorLocation()))==0){var pasted=full.substring(this.Textbox.GetCursorLocation(),full.length);if(pasted){this.Textbox.AppendText(pasted,true);}}};this.SetTextFromSelection=function(){if(this.SelectedIndex>=0){this.Textbox.value=this.m_ElementList[this.SelectedIndex].AutoCompleteText;var eol=this.Textbox.value.length;if(this.Textbox.createTextRange){var oRange=this.Textbox.createTextRange();oRange.moveStart("character",eol);oRange.moveEnd("character",eol);oRange.select();}else if(this.setSelectionRange){this.setSelectionRange(eol,eol);}this.Textbox.focus();}};this.SelectAndContinue=function(){if(typeof(this.OnSelectAndContinue)=='function')this.OnSelectAndContinue(this);};this.BeginDataSourceRequest=function(str){if(typeof(this.ACDataParser.Parse)=='undefined'){alert('The data parser does not have a Parse() method');return;}if(new Date().getTime()-this.LastDataSourceRequestMS<this.CallbackDelayMS){if(this.DataRequestTimer!=null)window.clearTimeout(this.DataRequestTimer);this.DataRequestTimer=window.setTimeout("CQL.OC['"+this.UID+"'].BeginDataSourceRequest('"+CQL.MakeStringJSSafe(str)+"')",this.CallbackDelayMS);return;}var url=this.URLFormatString.replace("{0}",escape(str));window.status="Retrieving Data...";var fncSuccess=new Function("CQL.OC['"+this.UID+"'].OnSuccess(arguments[0])");var fncError=new Function("CQL.OC['"+this.UID+"'].OnError(arguments[0])");if(this.IsUseCQLRPC){this.RPC.OnSuccess=fncSuccess;this.RPC.OnError=fncError;this.RPC.BeginRequest(url);}else{RS.Execute(url,"WriteOutput",fncSuccess,fncError);}};this.OnSuccess=function(result){window.status="";this.SetItems(this.ACDataParser.Parse(result));if(this.LastEnteredKeyCode==BKSPACE)this.SelectByEnteredText(false);else this.SelectByEnteredText(true);};this.OnError=function(result){window.status="Error: "+result;this.ClearContents();};this.Textbox.onkeydown=function(evt){var keyCode=document.layers?evt.which:document.all?event.keyCode:document.getElementById?evt.keyCode:0;if(keyCode==ARROW_U){newIndex=this.AutoCompleteBox.SelectedIndex-1;this.AutoCompleteBox.SetSelectedIndex(newIndex);this.AutoCompleteBox.SetTextFromSelection();}else if(keyCode==ARROW_D){newIndex=this.AutoCompleteBox.SelectedIndex+1;this.AutoCompleteBox.SetSelectedIndex(newIndex);this.AutoCompleteBox.SetTextFromSelection();}else if(CQL.IsEnter(keyCode)){this.AutoCompleteBox.SelectAndContinue();return false;}return true;};this.Textbox.onkeyup=function(evt){var keyCode=document.layers?evt.which:document.all?event.keyCode:document.getElementById?evt.keyCode:0;var chr=String.fromCharCode(keyCode).toLowerCase().charCodeAt(0);if(this.value==""){this.AutoCompleteBox.ClearContents();}else if(((chr>='0'.charCodeAt(0)&&chr<='9'.charCodeAt(0))||(chr>='a'.charCodeAt(0)&&chr<='z'.charCodeAt(0)))||chr==SPACE||chr==BKSPACE){this.AutoCompleteBox.LastEnteredKeyCode=chr;if(this.value.length){this.AutoCompleteBox.LastDataSourceRequestMS=new Date().getTime();this.AutoCompleteBox.BeginDataSourceRequest(this.value);}if(this.AutoCompleteBox.LastEnteredKeyCode==BKSPACE)this.AutoCompleteBox.SelectByEnteredText(false);else this.AutoCompleteBox.SelectByEnteredText(true);}return true;};this.Textbox.GetNumCharsSelected=function(){var len=-1;if(this.createTextRange){var rng=document.selection.createRange().duplicate();len=rng.text.length;}else if(this.setSelectionRange){len=this.selectionEnd-this.selectionStart;}return len;};this.Textbox.GetCursorLocation=function(){var loc=0;if(this.createTextRange){var rng=document.selection.createRange().duplicate();rng.moveEnd("textedit",1);loc=this.value.length-rng.text.length}else if(this.setSelectionRange){loc=this.selectionStart;}else{loc=-1;}return loc;};this.Textbox.AppendText=function(str,isHighlight){var cursor=this.GetCursorLocation();if(this.GetNumCharsSelected()>0)this.value=this.value.substring(0,cursor)+str;else this.value=this.value+str;if(isHighlight)this.SelectRange(cursor,this.value.length+1);};this.Textbox.SelectRange=function(iStart,iLength){if(this.createTextRange){var oRange=this.createTextRange();oRange.moveStart("character",iStart);oRange.moveEnd("character",iLength-this.value.length);oRange.select();}else if(this.setSelectionRange){this.setSelectionRange(iStart,iLength);}this.focus();};this.Textbox.onblur=function(){window.setTimeout("CQL.OC['"+this.AutoCompleteBox.UID+"'].ClearContents()",this.AutoCompleteBox.BlurTimeoutMS);window.setTimeout("CQL.OC['"+this.AutoCompleteBox.UID+"'].ClearContents()",this.AutoCompleteBox.LongBlurTimeoutMS);};this.Init();return this;};CQL.AC.ACDataParser=function(){this.Parse=function(result){alert('You must override the Parse() method in the ACDataParser subclass');};return this;};CQL.AC.DefaultACDataParser=function(){this.Parse=function(result){return result.split('\n');};return this;};CQL.AC.DefaultACDataParser.prototype=new CQL.AC.ACDataParser();CQL.AC.ACUIProvider=function(){this.GenerateLineNode=function(){alert('You must override the GenerateLineNode() method in the ACUIProvider subclass');};return this;};CQL.AC.DefaultACUIProvider=function(){this.GenerateLineNode=function(autoCompleteBox,item,lineIndex){var nodeDiv=document.createElement("DIV");nodeDiv.innerHTML=item;nodeDiv.AutoCompleteBox=autoCompleteBox;nodeDiv.LineIndex=lineIndex;nodeDiv.AutoCompleteText=item;nodeDiv.onmouseover=function(){this.AutoCompleteBox.SetSelectedIndex(this.LineIndex);};nodeDiv.onmouseout=function(){this.className="normal";};nodeDiv.onclick=function(){this.AutoCompleteBox.Textbox.value=this.AutoCompleteText;this.AutoCompleteBox.ClearContents();this.AutoCompleteBox.SelectAndContinue();};return nodeDiv;};return this;};CQL.AC.DefaultACUIProvider.prototype=new CQL.AC.ACUIProvider();