function Suggestion(){this.inputFields=new Object();this.suggestions=new Array();this.activeTarget=null;this.selectedIndex=-1;this.insertAutomatically=false;this.forceSuggestion=true;this.ajaxRequest;this.source='index.php?page=UserSuggest'+SID_ARG_2ND;this.showIcon=false;this.multiple=true;this.separator=',';this.init=function(inputFieldID){if(!this.inputFields[inputFieldID]){this.inputFields[inputFieldID]=inputFieldID;var element=document.getElementById(inputFieldID);if(IS_IE||IS_SAFARI){element.style.display='block';}
element.form.setAttribute('autocomplete','off');element.form.onsubmit=function(){if(suggestion.selectedIndex!=-1)return false;};var newDiv=document.createElement('div');newDiv.id='option'+inputFieldID;newDiv.className='hidden';if(element.nextSibling){element.parentNode.insertBefore(newDiv,element.nextSibling);}
else{element.parentNode.appendChild(newDiv);}
element.onkeyup=function(e){return suggestion.handleInput(e);};element.onkeydown=function(e){return suggestion.handleBeforeInput(e);};element.onclick=function(e){return suggestion.handleClick(e);};element.onfocus=function(e){return suggestion.handleClick(e);};element.onblur=function(e){return suggestion.closeList();}}}
this.setForceSuggestion=function(forceSuggestion){this.forceSuggestion=forceSuggestion;}
this.closeAllLists=function(event){if(!event)event=window.event;var target=this.getEventTarget(event);if(target.type=='submit')return;for(var inputFieldID in this.inputFields){if(target.id.indexOf(inputFieldID)==-1){var activeTarget=document.getElementById(inputFieldID);this.closeList(activeTarget);}}}
this.getEventTarget=function(event){if(event.target)return event.target;else if(event.srcElement)return event.srcElement;}
this.handleBeforeInput=function(event){if(!event)event=window.event;var keyCode=0;if(event.which)keyCode=event.which;else if(event.keyCode)keyCode=event.keyCode;if(keyCode==40)return this.moveList('next');if(keyCode==38)return this.moveList('previous');if(keyCode==27)return this.closeList();if(IS_IE&&keyCode==13){if(this.selectedIndex!=-1){this.insertSelectedOption();return false;}}
return true;}
this.insertSelectedOption=function(index,target){if(index==undefined){index=this.selectedIndex;}
if(!target){target=this.activeTarget;}
if(this.suggestions.length>index&&this.suggestions[index]){var cursorStart=this.getCursorPosition(target);var start=0;var end=target.value.length;if(this.multiple){end=target.value.indexOf(this.separator,cursorStart);if(end==-1)end=target.value.length;for(var i=cursorStart;i>=0;i--){if(target.value.charAt(i)==this.separator){start=i;break;}}}
for(var i=start+1;i<target.value.length;i++){if(target.value.charAt(i)!=' '){break;}
start++;}
var newValue='';if(start>0)newValue+=target.value.substring(0,start+1);newValue+=this.suggestions[index]['name'];newValue+=target.value.substring(end);target.value=newValue;target.select();if(document.selection){var range=target.createTextRange();range.moveStart('character',cursorStart);range.moveEnd('character',(start>0?start+1:0)+this.suggestions[index]['name'].length);range.select();}
else if(window.getSelection){target.selectionStart=cursorStart;}}
this.closeList(target);return false;}
this.handleInput=function(event){if(!event)event=window.event;var keyCode=0;if(event.which)keyCode=event.which;else if(event.keyCode)keyCode=event.keyCode;var target=this.getEventTarget(event);if(this.activeTarget!=target)this.ajaxRequest=null;this.activeTarget=target;if(keyCode==40)return false;if(keyCode==38)return false;if(keyCode==27)return false;if(keyCode==8)return false;if(keyCode==13){if(this.selectedIndex!=-1){this.insertSelectedOption();return false;}
return true;}
if(keyCode==37||keyCode==39||!this.forceSuggestion){this.insertAutomatically=false;}
else{this.insertAutomatically=true;}
this.getSuggestList(target);}
this.setSelectedIndex=function(selectedIndex){if(this.selectedIndex!=-1){var oldElement=document.getElementById('optionList'+this.activeTarget.id+'Element'+this.selectedIndex);if(oldElement)oldElement.className="";}
this.selectedIndex=selectedIndex;var element=document.getElementById('optionList'+this.activeTarget.id+'Element'+this.selectedIndex);if(element)element.className="active";}
this.moveList=function(direction){if(direction=='next'){if(this.selectedIndex+1<this.suggestions.length){this.setSelectedIndex(this.selectedIndex+1);}}
else{if(this.selectedIndex>0){this.setSelectedIndex(this.selectedIndex-1);}}
return false;}
this.handleClick=function(event){this.closeAllLists(event);if(!event)event=window.event;var target=this.getEventTarget(event);if(this.activeTarget!=target)this.ajaxRequest=null;this.activeTarget=target;this.insertAutomatically=false;this.getSuggestList(target);}
this.getSuggestList=function(target){var string=this.getActiveString(target);if(string!=''){this.ajaxRequest=new AjaxRequest();this.ajaxRequest.openPost(this.source,'query='+encodeURIComponent(string),function(){suggestion.receiveResponse();});}
else{this.closeList();}}
this.receiveResponse=function(){if(this.ajaxRequest&&this.ajaxRequest.xmlHttpRequest.readyState==4&&this.ajaxRequest.xmlHttpRequest.status==200&&this.ajaxRequest.xmlHttpRequest.responseXML){this.suggestions=new Array();var suggestions=this.ajaxRequest.xmlHttpRequest.responseXML.getElementsByTagName('suggestions');if(suggestions.length>0){for(var i=0;i<suggestions[0].childNodes.length;i++){if(suggestions[0].childNodes[i].childNodes.length>0){var id=this.suggestions.length;this.suggestions[id]=new Object();this.suggestions[id]['name']=suggestions[0].childNodes[i].childNodes[0].nodeValue;this.suggestions[id]['type']=suggestions[0].childNodes[i].nodeName;}}}
this.showList();this.ajaxRequest.xmlHttpRequest.abort();}}
this.showList=function(){this.closeList();if(this.suggestions.length>0&&this.activeTarget){if(this.suggestions.length==1&&this.insertAutomatically){this.setSelectedIndex(0);this.insertSelectedOption();}
else{var optionDiv=document.getElementById('option'+this.activeTarget.id);if(optionDiv){optionDiv.className='pageMenu popupMenu';var optionList=document.createElement('ul');optionList.id='optionList'+this.activeTarget.id
optionDiv.appendChild(optionList);optionList=document.getElementById('optionList'+this.activeTarget.id);for(var i=0;i<this.suggestions.length;++i){var optionListElement=document.createElement('li');optionListElement.id='optionList'+this.activeTarget.id+'Element'+i;optionList.appendChild(optionListElement);var optionListLink=document.createElement('a');optionListLink.name=i;optionListLink.onmousedown=function(){suggestion.insertSelectedOption(this.name);};document.getElementById('optionList'+this.activeTarget.id+'Element'+i).appendChild(optionListLink);if(this.showIcon){var icon=document.createElement('img');icon.src=RELATIVE_WCF_DIR+'icon/'+this.suggestions[i]['type']+'S.png';optionListLink.appendChild(icon);}
var name=document.createTextNode((this.showIcon?' ':'')+this.suggestions[i]['name']);optionListLink.appendChild(name);}}
this.setSelectedIndex(0);}}}
this.closeList=function(target){this.selectedIndex=-1;if(!target){target=this.activeTarget;}
if(target){var optionDiv=document.getElementById('option'+target.id);if(optionDiv){var optionList=document.getElementById('optionList'+target.id);if(optionList){optionDiv.removeChild(optionList);}
optionDiv.className='hidden';}}}
this.getActiveString=function(target){var cursorPosition=this.getCursorPosition(target);var string=target.value.substring(0,cursorPosition);if(this.multiple){var parts=string.split(this.separator);if(parts.length>0){string=parts[parts.length-1];}
else string='';}
string=string.replace(/^\s+/,'');string=string.replace(/\s+$/,'');return string;}
this.getCursorPosition=function(target){var cursorPosition=target.value.length;if(typeof target.selectionStart=='number'){cursorPosition=target.selectionStart;}
else if(typeof document.selection.createRange()=='object'){var range=document.selection.createRange();range.moveEnd('textedit',1);var last=String(range.text);cursorPosition=cursorPosition-last.length;}
return cursorPosition;}
this.setSource=function(newSource){this.source=newSource;}
this.enableIcon=function(enable){this.showIcon=enable;}
this.enableMultiple=function(enable){this.multiple=enable;}
this.setSeparator=function(separator){this.separator=separator;}}
var suggestion=new Suggestion();
