
function DSDropDownItem(elem,text,type){this.elem=elem;this.text=text||'';this.type=type||'';this.properties={};for(var i=3;i<arguments.length;++i){this.setProperty(arguments[i],arguments[++i]);}}
DSDropDownItem.prototype.getValue=function(){return this.text;}
DSDropDownItem.prototype.setProperty=function(key,value){return this.properties[key]=value;}
DSDropDownItem.prototype.getProperty=function(key){return this.properties[key];}
DSDropDownItem.prototype.isType=function(type){return(type==this.type);}
DSDropDownItem.prototype.addEventListener=function(type,func){switch(type){case'click':case'mouseout':case'mouseover':if(!this.events){this.events=new DSEvent();}
if(!this.events.getListeners(type)){var o=this;this.events.registerEvent(type);DSBrowserEvent.addListener(this.elem,type,function(){o.events.triggerEvent(type,null,o);});}
break;}
this.events.addListener(type,func);}
function DSDropDown(key,textbox){if(!key){throw('DSDropDown: you must specify a key');}
if(DSDropDown.lists[key]){throw('DSDropDown: duplicate key: "'+key+'"');}
if(!textbox){throw('DSDropDown: unable to find textbox: "'+textbox+'"');}
this.autocomp=true;this.key=key;this.textbox=textbox;this.parent=textbox.parentNode;this.listItems=[];this.pos=-1;this.createDropDown();this.textbox.setAttribute("autocomplete","off");DSBrowserEvent.bind(this.textbox,'click',this,DSDropDown.click);DSBrowserEvent.bind(this.textbox,'keydown',this,DSDropDown.typing);DSBrowserEvent.bind(this.textbox,'keypress',this,DSDropDown.typing);this.events=new DSEvent();this.events.registerEvent('typing');this.events.registerEvent('select');this.listXOffset=this.listYOffset=this.listExtraW=0;DSDropDown.lists[key]=this;if(!DSDropDown.inited){DSBrowserEvent.addListener(document,'click',DSDropDown.HideLists);DSDropDown.inited=true;}}
DSDropDown.lists=new Object();DSDropDown.inited=false;DSDropDown.HideLists=function(evt){for(var type in DSDropDown.lists){DSDropDown.lists[type].hide();}}
DSDropDown.click=function(e){this.toggleVisibility();e.stopPropagation();};DSDropDown.prototype.createDropDown=function(){if(navigator.userAgent.indexOf("MSIE")>=0){this.shim=DOMNode.create(this.parent,'','iframe','src','javascript:void(0)','class','DSDDList','style','filter:alpha(opacity=0);padding:0;margin:0;');}
this.list=DOMNode.create(this.parent,'','div','class','DSDDList');this.listElem=DOMNode.create(this.list,'','dl','id','dd_'+this.key);};DSDropDown.prototype.getLength=function(){return this.listItems.length;};DSDropDown.prototype.setAutoComplete=function(ac){this.autocomp=(ac?true:false);return this.autocomp;};DSDropDown.prototype.autoComplete=function(){return this.autocomp;};DSDropDown.prototype.updateShim=function(){if(this.shim){this.shim.style.top=this.list.style.top;this.shim.style.left=this.list.style.left;this.shim.style.width=this.list.style.width;this.shim.style.visibility=this.list.style.visibility;this.shim.style.height=this.list.clientHeight+'px';}};DSDropDown.prototype.positionList=function(){var top=this.textbox.offsetTop+this.textbox.offsetHeight;var left=this.textbox.offsetLeft;var width=this.textbox.offsetWidth-2;this.list.style.top=top+this.listYOffset+'px';this.list.style.left=left+this.listXOffset+'px';this.list.style.width=width+this.listExtraW+'px';this.updateShim();};DSDropDown.prototype.setListOffset=function(x,y,w){this.listXOffset=(typeof x!='undefined'?x:0);this.listYOffset=(typeof y!='undefined'?y:0);this.listExtraW=(typeof w!='undefined'?w:0);};DSDropDown.prototype.positionArrow=function(){var top=this.textbox.offsetTop+this.arrowYOffset;var left=this.textbox.offsetLeft+this.textbox.offsetWidth+this.arrowXOffset;this.arrow.style.top=top+'px';this.arrow.style.left=left+'px';};DSDropDown.prototype.addDropDownArrow=function(url,x,y){this.arrowXOffset=(typeof x!='undefined'?x:0);this.arrowYOffset=(typeof y!='undefined'?y:0);this.arrow=DOMNode.create(null,'','img','src',url,'class','DSDDArrow');DOMNode.appendAfter(this.arrow,this.textbox);DSBrowserEvent.bind(this.arrow,'click',this,DSDropDown.click);this.positionArrow();};DSDropDown.getByKey=function(key){return DSDropDown.lists[key];};DSDropDown.getKeys=function(){var a=[];for(var i in DSDropDown.lists){a.push(i);}
return a;};DSDropDown.prototype.addHeaderItem=function(text,cls,type){var d=DOMNode.create(this.listElem,text,'dt');DOMNode.addClass(d,'DSDDHeaderItem');if(cls)DOMNode.addClass(d,cls);var item=new DSDropDownItem(d,text,(type?type:text),'selectable',false,'removable',false);this.listItems.push(item);return item;}
DSDropDown.prototype.addListItem=function(elem,text,type){var d=DOMNode.create(this.listElem,'','dd');d.appendChild(elem);var item=new DSDropDownItem(d,text,type);for(var i=3;i<arguments.length;++i){item.setProperty(arguments[i],arguments[++i]);}
this.listItems.push(item);return item;}
DSDropDown.prototype.addSelectablePair=function(text1,text2,type,sticky){var i=this.getLength();var o=this;var a=DOMNode.create(null,text1,'a','href','javascript:void(0)');DOMNode.create(a,text2,'span');var item=this.addListItem(a,text1,type,'selectable',true,'removable',(!sticky));DOMNode.editStyleValue(item.elem,'cursor','pointer');DSBrowserEvent.bind(item,'click',this,this.useSelection);DSBrowserEvent.bind(item,'mouseover',this,this.eventHighlight,i);DSBrowserEvent.bind(item,'mouseout',this,this.eventDehighlight,i);return item;}
DSDropDown.prototype.addSelectableItem=function(text,type,sticky){var i=this.getLength();var o=this;var a=DOMNode.create(null,text,'a','href','javascript:void(0)');var item=this.addListItem(a,text,type,'selectable',true,'removable',(!sticky));DOMNode.editStyleValue(item.elem,'cursor','pointer');DSBrowserEvent.bind(item,'click',this,this.useSelection);DSBrowserEvent.bind(item,'mouseover',this,this.eventHighlight,i);DSBrowserEvent.bind(item,'mouseout',this,this.eventDehighlight,i);return item;}
DSDropDown.prototype.addLink=function(text,callback,type){var href=(typeof callback=='function'?'javascript:void(0)':callback);var a=DOMNode.create(null,text,'a','href',href,'class','link');var item=this.addListItem(a,text,type,'selectable',false);if(typeof callback=='function'){DSBrowserEvent.bind(item,'click',this,callback,type);}
return item;}
DSDropDown.prototype.addRemoveAllLink=function(text,callback,type){if(typeof type=='undefined'){type='';}
var o=this;var item=this.addLink(text,this.removeAll,type);item.setProperty('removable',true);if(callback){DSBrowserEvent.addListener(item,'click',callback);}
return item;}
DSDropDown.prototype.getItem=function(index){return this.listItems[index];}
DSDropDown.prototype.removeItem=function(index){var i=this.getItem(index);i.elem.parentNode.removeChild(i.elem);this.listItems.splice(index,1);}
DSDropDown.prototype.isRemovable=function(index){if(arguments.length<1){index=this.pos;}
if(index>=0&&index<this.getLength()){return this.getItem(index).getProperty('removable');}
return false;}
DSDropDown.prototype.removeAll=function(e,type){for(var i=0;i<this.getLength();++i){while(i<this.getLength()&&this.getItem(i).isType(type)&&this.isRemovable(i)){this.removeItem(i);}}}
DSDropDown.prototype.isShown=function(){return this.list.style.display=='block';}
DSDropDown.prototype.show=function(){if(this.isShown()){return;}
DSDropDown.HideLists();this.positionList();this.list.style.display='block';this.updateShim();}
DSDropDown.prototype.hide=function(){if(this.isShown()){this.dehighlight();this.list.style.display='none';this.updateShim();}}
DSDropDown.prototype.toggleVisibility=function(){if(this.isShown()){this.hide();}else{this.show();}}
DSDropDown.prototype.isSelectable=function(index){if(arguments.length<1){index=this.pos;}
if(index>=0&&index<this.getLength()){return this.listItems[index].getProperty('selectable');}
return false;}
DSDropDown.prototype.getValue=function(index){if(arguments.length<1){index=this.pos;}
if(this.isSelectable(index)){return this.listItems[index].getValue();}
return'';}
DSDropDown.prototype.useSelection=function(){this.setTextboxValue();this.hide();}
DSDropDown.prototype.setTextboxValue=function(val){this.clearHintText();if(val){this.textbox.value=val;}else if(this.isSelectable()){this.textbox.value=this.getValue();this.events.triggerEvent('select',this.listItems[this.pos],this);}}
DSDropDown.prototype.highlight=function(index){if(arguments.length<1){index=this.pos;}
if(this.isSelectable(index)){this.pos=index;DOMNode.addClass(this.listItems[index].elem,'selected');}}
DSDropDown.prototype.eventHighlight=function(e,index){this.highlight(index);}
DSDropDown.prototype.dehighlight=function(index){if(arguments.length<1){index=this.pos;}
if(this.isSelectable(index)){DOMNode.removeClass(this.listItems[index].elem,'selected');}
this.pos=-1;}
DSDropDown.prototype.eventDehighlight=function(e,index){this.dehighlight(index);}
DSDropDown.prototype.previous=function(){if(this.getLength()==0)return;var index=this.pos;if(index<0){index=0;}else{--index;}
while(index>0&&!this.isSelectable(index)){--index;}
if(this.isSelectable(index)){this.dehighlight();this.pos=index;this.highlight();}}
DSDropDown.prototype.next=function(){if(this.getLength()==0)return;var index=this.pos;if(index>=this.getLength()){index=this.getLength()-1;}else if(index<0){index=0;}else{++index;}
while(index<this.getLength()&&!this.isSelectable(index)){++index;}
if(this.isSelectable(index)){this.dehighlight();this.pos=index;this.highlight();}}
DSDropDown.prototype.last=function(){this.dehighlight();this.pos=this.getLength();this.previous();}
DSDropDown.prototype.first=function(){this.dehighlight();this.pos=-1;this.next();}
DSDropDown.KEY_BACKSPACE=8;DSDropDown.KEY_TAB=9;DSDropDown.KEY_ENTER=13;DSDropDown.KEY_ESC=27;DSDropDown.KEY_PGUP=33;DSDropDown.KEY_PGDN=34;DSDropDown.KEY_END=35;DSDropDown.KEY_HOME=36;DSDropDown.KEY_LEFT=37;DSDropDown.KEY_UP=38;DSDropDown.KEY_RIGHT=39;DSDropDown.KEY_DOWN=40;DSDropDown.KEY_DELETE=46;DSDropDown.canSelect=function(tb){return(tb.createTextRange||tb.setSelectionRange);}
DSDropDown.textboxSelect=function(tb,iStart,iEnd){if(tb.createTextRange){var oRange=tb.createTextRange();oRange.moveStart("character",iStart);oRange.moveEnd("character",-tb.value.length+iEnd);oRange.select();}else if(tb.setSelectionRange){tb.setSelectionRange(iStart,iEnd);}
tb.focus();}
DSDropDown.getTextboxValue=function(tb){var val;if(document.selection){var oRange=document.selection.createRange();val=tb.value.substring(0,tb.value.length-oRange.text.length);}else if(tb.setSelectionRange){val=tb.value.substring(0,tb.selectionStart);}else{val=tb.value;}
return val;}
DSDropDown.getCharCode=function(e){var k=e.getKeyCode();var c=e.getCharCode();if(e.getType()=='keydown'||k>60000||(typeof c!='undefined'&&c==0)||k==DSDropDown.KEY_ENTER||k==DSDropDown.KEY_TAB||k==DSDropDown.KEY_BACKSPACE){return null;}else{return(c||k);}}
DSDropDown.getKeyCode=function(e){var key=e.getKeyCode();if(key>63000){switch(key){case 63232:key=DSDropDown.KEY_UP;break;case 63273:key=DSDropDown.KEY_HOME;break;case 63233:key=DSDropDown.KEY_DOWN;break;case 63275:key=DSDropDown.KEY_END;break;case 63234:key=DSDropDown.KEY_LEFT;break;case 63276:key=DSDropDown.KEY_PGUP;break;case 63235:key=DSDropDown.KEY_UP;break;case 63277:key=DSDropDown.KEY_PGDN;break;}}
return key;}
DSDropDown.prototype.guess=function(inp){if(!inp)inp=DSDropDown.getTextboxValue(this.textbox);var t=new RegExp('^'+quotemeta(inp),'i');this.dehighlight();for(var i=0;i<this.getLength();++i){var val=this.getValue(i);if(this.isSelectable(i)&&t.test(val)){this.show();this.highlight(i);break;}}
if(DSDropDown.canSelect(this.textbox)&&this.isSelectable()){if(this.isSelectable()){this.textbox.value=inp+this.getValue().substring(inp.length);DSDropDown.textboxSelect(this.textbox,inp.length,this.textbox.value.length);}}else{return true;}
return false;}
DSDropDown.typing=function(e){if(e.getAltKey()||e.getCtrlKey()){return true;}
var tbVal=DSDropDown.getTextboxValue(this.textbox);var c=DSDropDown.getCharCode(e);var k=DSDropDown.getKeyCode(e);var afterVal;if(!c&&k==DSDropDown.KEY_BACKSPACE){afterVal=tbVal.substring(0,tbVal.length-1);}else if(!c&&k==DSDropDown.KEY_DELETE){afterVal=tbVal;}else{afterVal=tbVal+String.fromCharCode(DSDropDown.getCharCode(e));}
var c={'charCode':c,'keyCode':k,'beforeValue':tbVal,'afterValue':afterVal};if(!this.events.triggerEvent('typing',c,this)){return true;}
if(c.charCode){if(!this.autoComplete()){return true;}else if(this.guess(c.afterValue)){return true;}}else{switch(c.keyCode){case DSDropDown.KEY_TAB:if(this.isShown()){this.useSelection();}
this.hide();return true;break;case DSDropDown.KEY_UP:if(e.getType()=='keydown'){this.show();this.previous();this.setTextboxValue();return true;}
break;case DSDropDown.KEY_DOWN:if(e.getType()=='keydown'){this.show();this.next();this.setTextboxValue();return true;}
break;case DSDropDown.KEY_ENTER:if(e.getType()=='keydown'){return true;}
if(!this.isSelectable()||!DSDropDown.canSelect(this.textbox)){return true;}else{DSDropDown.textboxSelect(this.textbox,this.textbox.value.length,this.textbox.value.length);this.useSelection();this.hide();}
break;default:return true;}}
e.preventDefault();return false;};DSDropDown.prototype.addEventListener=function(type,func){this.events.addListener(type,func);};DSDropDown.prototype.setHintText=function(txt){this.hintText=txt;if(!this.hintTextInited){this.hintTextInited=true;var o=this;DSBrowserEvent.bind(this.textbox.form,'submit',this,this.clearHintText);this.showHintText();}};DSDropDown.prototype.showHintText=function(){if(this.hintTextInited&&(this.textbox.value==''||this.textbox.value==this.hintText)){DOMNode.addClass(this.textbox,'hintText');this.textbox.value=this.hintText;if(this.blur){DSBrowserEvent.remove(this.blur);}
this.focus=DSBrowserEvent.bind(this.textbox,'focus',this,this.clearHintText);}};DSDropDown.prototype.clearHintText=function(){if(DOMNode.isClass(this.textbox,'hintText')){this.textbox.value='';DOMNode.removeClass(this.textbox,'hintText');if(this.focus){DSBrowserEvent.remove(this.focus);}
this.blur=DSBrowserEvent.bind(this.textbox,'blur',this,this.showHintText);}};