if(typeof(_sysDom)=="undefined")alert("_sysDom is not defined!");
function ETSOODropdown(sName,oContainer,sXml,iWidth,sDefault,iListWidth)
{
    if(oContainer&&oContainer.nodeName=="INPUT")
    {
        this.textField=oContainer;
        oContainer=oContainer.parentNode;
    }
    if(iWidth==null)iWidth=150;
    var o=this;
    ETSOODropdown.all[sName]=o;
    this.fieldName=null;
    this.name=sName;
    this.container=oContainer;
    this.xml=sXml;
    this.listWidth=iListWidth;
    this.width=iWidth;
    this.height=null;
    this.multiple=false;
    this.disabled=false;
    this.options=[];
    this.showText=true;
    this.defaultValue=sDefault;
    this.selectedIndex=-1;
    this.value=null;
    this.update=true;
    this.focused=false;
    this._zIndex=10;
    this._intervalObject=null;
    this._interval=200;
    this._baseText=null;
    this._readFlag=-2;
    this._ie5=(_sysDom.browser.name=="msie"&&_sysDom.browser.subVersion<5.5);
    this.add=function(oOption)
    {
        if(this.indexOf(oOption)!=-1)return;
        var iLen=this.options.length;
        oOption.index=iLen;
        this.options[iLen]=oOption;
        if(oOption.selected)
        {
            this._setValue(oOption);
        }
        this._addOption(oOption);
    }
    this.removeAll=function()
    {
        var iLen=this.options.length;
        if(iLen>0)
        {
            if(this._ie5)this._createTable();
            else
            {
                for(var i=0;i<iLen;i++)
                {
                    this.remove(this.options[i]);
                }
            }
        }
        this._currentTR=null;
        this.selectedIndex=-1;
        this.options=[];
    }
    this.indexOf=function(oOption)
    {
        var iLen=this.options.length;
        for(var i=0;i<iLen;i++)
        {
            if(this.options[i].value==oOption.value)return i;
        }
        return -1;
    }
    this.remove=function(oOption)
    {
        if(oOption)
        {
            if(oOption._tr)
            {
                oOption._tr.parentNode.removeChild(oOption._tr);
            }
            this.options[oOption.index]=null;
        }
    }
    if(oContainer)
    {
        oContainer.innerHTML="";
        oContainer.style.cursor="default";
            
        var oTextSpan=document.createElement("table");
	    oTextSpan.setAttribute("border",0);
	    oTextSpan.setAttribute("cellPadding",0);
	    oTextSpan.setAttribute("cellSpacing",0);
        oTextSpan.style.width=this.width+"px";
        oContainer.appendChild(oTextSpan);
        this._Table=oTextSpan;
        
	    var oTBody=document.createElement("tbody");
	    oTextSpan.appendChild(oTBody);
	    var oTR=document.createElement("tr");
	    oTBody.appendChild(oTR);
	
	    var oFTD=document.createElement("td");
	    oFTD.style.paddingLeft="2px";
	    oFTD.setAttribute("align","left");
	    oTR.appendChild(oFTD);
	    
	    this._FTD=oFTD;
	    
	    var oSTD=document.createElement("td");
	    oSTD.setAttribute("width",15);
	    oSTD.setAttribute("align","right");
	    oSTD.style.padding="1px";
	    oTR.appendChild(oSTD);
	    
        var oImage=document.createElement("img");
        if(_sysDom.browser.name!="msie")oImage.setAttribute("align","left");
        oSTD.appendChild(oImage);
        this._icon=oImage;
        oImage.src="/ETSOO/Eajax/Icon/downB.gif";
      
        oTR.onmouseover=function()
        {
            oImage.src="/ETSOO/Eajax/Icon/downL.gif";
        }
        oTR.onmouseout=function()
        {
            oImage.src="/ETSOO/Eajax/Icon/downB.gif";
        }
        _sysDom.attachEvent(oTR,"onclick",function(oEvent){o._autoShow(oEvent)});

        var oDiv=document.createElement("div");
        with(oDiv.style)
        {
            position="absolute";
            zIndex=this._zIndex;
            border="1px solid #7f9db9";
            padding="2px";
            overflow="auto";
            backgroundColor="#ffffff";
            display="none";
        }
        oContainer.appendChild(oDiv);
        this._listDiv=oDiv;
        
        this._createTable();
        
        if(!_sysDom.browser.isCover)
        {
            var objIFrame=document.createElement("iframe");
            objIFrame.setAttribute("border",0);
            objIFrame.setAttribute("frameBorder",0);
            oContainer.appendChild(objIFrame);
            with(objIFrame.style)
            {
                position="absolute";
                zIndex=this._zIndex-1;
                display="none";
            }
            this._iframe=objIFrame;
        }
	    _sysDom.attachEvent(document,"onmousedown",function(oEvent){o.hide(oEvent)});
	    _sysDom.attachEvent(window,"onblur",function(oEvent){o.hide(oEvent)});
	    this.setReadOnly();
		if(sDefault!=null)this.setText(sDefault);
    }
}
ETSOODropdown.all={};
ETSOODropdown.onKeyDown=function(oEvent)
{
    if(oEvent)
    {
        var aAll=ETSOODropdown.all;
        var oCurrent;
        var iCode=oEvent.keyCode;
        for(var sName in aAll)
        {
            var oOne=aAll[sName];
            if(oOne.focused)
            {
                oCurrent=oOne;
                break;
            }
        }
        if(oCurrent)
        {
            var oTR=oCurrent._currentTR;
            if(iCode==13)
            {
                if(oTR==null&&oCurrent.options.length==1)oTR=oCurrent.options[0]._tr;
                if(oTR)oTR.onclick();
                _sysDom.cancelEvent(oEvent);
            }
            else
            {
                oCurrent._autoShow(oEvent);
                var oNext;
                if(iCode==38)
                {
                    if(oTR==null)oNext=oCurrent.options[oCurrent.options.length-1]._tr;
                    else oNext=oTR.previousSibling;
                    if(oNext)
                    {
                        if(oTR)oTR.onmouseout();
                        oNext.onmouseover();
                    }
                }
                else if(iCode==40)
                {
                    if(oTR==null)oNext=oCurrent.options[0]._tr;
                    else oNext=oTR.nextSibling;
                    if(oNext)
                    {
                        if(oTR)oTR.onmouseout();
                        oNext.onmouseover();
                    }
                }
                if(oNext)oCurrent._currentTR=oNext;
            }
        }
    }
}
_sysDom.attachEvent(document,"onkeydown",ETSOODropdown.onKeyDown);
ETSOODropdown.prototype._createTable=function()
{
    if(this._listTBody==null||this._ie5)
    {
        if(this._listTBody)
        {
            this._listTBody.parentNode.style.display="none";
        }
        var objListTable=document.createElement("table");
	    objListTable.setAttribute("width",(this.listWidth==null)?"0":this.listWidth+"px");
	    objListTable.setAttribute("border",0);
	    objListTable.setAttribute("cellPadding",0);
	    objListTable.setAttribute("cellSpacing",0);
        
        this._listDiv.appendChild(objListTable);
        
	    var oTBody=document.createElement("tbody");
	    objListTable.appendChild(oTBody);
	    this._listTBody=oTBody;
    }
}
ETSOODropdown.prototype.hide=function(oEvent)
{
    if(this._listDiv.style.display=="none")return;
    var blnH=false;
    if(oEvent==null)blnH=true;
    else
    {
        var objE=_sysDom.getEventTarget(oEvent);
        if(objE==null||objE.nodeName=="#document"||!_sysDom.contains(this._listDiv,objE))
        {
            blnH=true;
        }
    }
    if(blnH)
    {
        this._listDiv.style.display="none";
        if(this._iframe)this._iframe.style.display="none";
        _sysDom._ie5Hide(self,false);
    }
}
ETSOODropdown.prototype._autoShow=function(oEvent)
{
    if(this.disabled)return false;
    if(this.xml==null&&this.options.length==0)return false;
    var oDiv=this._listDiv;
    var aPos=_sysDom.getPos(this.textField);
    var iLeft=aPos[0]-3;
    var iTop=aPos[1]+17;
    with(oDiv.style)
    {
        if(display=="none")display="block";
        left=iLeft+"px";
        top=iTop+"px";
    }
    if(this._iframe)
    {
        with(this._iframe.style)
        {
            if(display=="none")display="block";
            left=iLeft+"px";
            top=iTop+"px";
            width=(oDiv.clientWidth+2)+"px";
            height=(oDiv.clientHeight+2)+"px";
        }
    }
    _sysDom._ie5Hide(self,true);
    if(this._currentTR)this._currentTR.onmouseover();
}
ETSOODropdown.prototype.readXml=function()
{
    var sXml=this.xml;
    if(sXml==null||sXml=="")return;
	if(this._readFlag!=0&&this.options.length>0)return;
    var sText=this.getText();
    if(sText=="")
    {
        this.value="";
        if(this.defaultValue!=null)sText=this.defaultValue;
    }
    if(this._readFlag==-1||(this._readFlag==0&&this._baseText!=sText)||this._readFlag==1)
    {
        this._baseText=sText;
        this.setValue(null);
        if(sXml.indexOf("?")==-1)sXml+="?";
        else sXml+="&";
        sXml+="_SF="+this.name;
        if(this._readFlag<=0)
        {
            sXml+="&_SC="+_sysDom.escape(sText);
            if(this.fieldName&&this.value)
            {
                sXml+="&"+this.fieldName+"="+this.value;
            }
        }
        this._readFlag=0;
        var o=this;
        this.removeAll();
        _sysAjax.importResources("dd_"+this.name,sXml,function(oR,oReq){o.doReadXml(oR,oReq)},null,null,null,null,true,true);
    }
}
ETSOODropdown.prototype.doReadXml=function(oR,oReq)
{
    var oDoc=oReq.getDocument();
    if(oDoc==null)return;
    var sNameField=oDoc.getAttribute("NameField");
    var sAddField=oDoc.getAttribute("AddField");
    var aData=oDoc.getElementsByTagName("Data");
    var iLen=aData.length;
    for(var i=0;i<iLen;i++)
    {
        var oData=aData[i];
        var sId=oData.getAttribute("id");
        var aInfo=_sysXml.getListText(oData);
        var sText=aInfo[sNameField];
        var sAddText=aInfo[sAddField];
        if(sAddText)sText+="("+sAddText+")";
        this.add(new ETSOODDOption(sText,sId,false,aInfo));
    }
    if(this.focused)this._autoShow();
}
ETSOODropdown.prototype._addOption=function(oOption)
{
    var o=this;
    
    var oTR=document.createElement("tr");
    oOption._tr=oTR;
    this._listTBody.appendChild(oTR);
    oTR.onmouseover=function()
    {
        if(o._currentTR)o._currentTR.onmouseout();
        o._currentTR=this;
        this.style.backgroundColor="#dcecfd";
    }
    oTR.onmouseout=function()
    {
        this.style.backgroundColor="";
    }
    oTR.onclick=function()
    {
        o._setValue(oOption);
        if(o.showText||o._baseText!=oOption.value)
        {
            o.removeAll();
            if(o.onChange)o.onChange(oOption);
        }
        o.hide();
    }
    if(oOption.value==o.defaultValue)
    {
        oTR.onclick();
        o.defaultValue=null;
    }
    if(oOption.index==o.selectedIndex)oTR.onmouseover();
    
    var oTD=document.createElement("td");
    oTD.setAttribute("noWrap","true");
    oTD.style.paddingTop="2px";
    oTD.style.paddingBottom="2px";
    if(this.height)oTD.style.height=this.height+"px";
    oTR.appendChild(oTD);
    
    oTD.innerHTML=oOption.text;
    if(this.height)
    {
        if(this.oOptions.length*this.height>400)
        {
            this._listDiv.style.height=(this.height*Math.round(400/this.height))+"px";
        }
    }
}
ETSOODropdown.prototype._setValue=function(oOption)
{
	this.container.title=oOption.text;
    this.setText(oOption.value);
    this.selectedIndex=oOption.index;
    this.setValue(oOption.value);
}
ETSOODropdown.prototype.setValue=function(strValue)
{
    if(strValue!=null&&this.value!=strValue)
    {
        this._readFlag=0;
        this.value=strValue;
        _sysDom.updateStatus(this.textField);
    }
}
ETSOODropdown.prototype.setText=function(sText)
{
    this.textField.value=sText;
}
ETSOODropdown.prototype.getText=function()
{
    return this.textField.value;
}
ETSOODropdown.prototype.setReadOnly=function()
{
    if(this._readFlag>=-1)return;
    this._readFlag=-1;
    var numBorder=0;
    if(_sysDom.browser.subName!="safari")numBorder=1;
    this._Table.style.border=numBorder+"px solid #7f9db9";

    var iWidth=this.width;
    var oTextField=this.textField;
    if(oTextField==null)
    {
        oTextField=_sysDom.createInput(document,"text",this.name);
        this._FTD.appendChild(oTextField);
        this.textField=oTextField;
    }
    with(oTextField.style)
    {
        border="0px solid #ffffff";
        padding="0px";
        margin="0px";
        fontSize="12px";
        width=iWidth+"px";
    }
    var o=this;
    _sysDom.attachEvent(oTextField,"onpropertychange",function(oEvent){o.setValue(_sysDom.getEventTarget(oEvent).value);});
    if(this.xml)
    {
        _sysDom.attachEvent(oTextField,"ondblclick",function(oEvent){o.ondblclick(oEvent)});
        _sysDom.attachEvent(oTextField,"onfocus",function(oEvent){o.onfocus(oEvent)});
        _sysDom.attachEvent(oTextField,"onblur",function(oEvent){o.onblur(oEvent)});
    }
    this.setSelected(this.selectedIndex);
    this.readXml();
}
ETSOODropdown.prototype.ondblclick=function(oEvent)
{
    this._readFlag=1;
    this.readXml();
}
ETSOODropdown.prototype.onfocus=function(oEvent)
{
    if(oEvent)this.focused=true;
	this._readFlag=-1;
    if(this.update)this._intervalObject=window.setInterval("ETSOODropdown.all['"+this.name+"'].readXml()",this._interval);
    else window.setTimeout("ETSOODropdown.all['"+this.name+"'].readXml()",this._interval);
}
ETSOODropdown.prototype.onblur=function(oEvent)
{
    if(oEvent)this.focused=false;
    if(this.update)
    {
        window.clearInterval(this._intervalObject);
        this._intervalObject=null;
    }
}
ETSOODropdown.prototype.setSelected=function(value,numType)
{
    if(numType==null)numType=0;
    var oOption=null;
    if(numType==2)
    {
        oOption=this.options[value];
    }
    else
    {
        var iLen=this.options.length;
        for(var i=0;i<iLen;i++)
        {
            var objOne=this.options[i];
            if((numType==0&&objOne.value==value)||(numType==1&&objOne.text==value))
            {
                oOption=objOne;
                break;
            }
        }
    }
    if(oOption)
    {
        this._setValue(oOption);
        return true;
    }else return false;
}
ETSOODropdown.prototype.setDisabled=function(blnDisabled)
{
    this.disabled=blnDisabled;
    this.textField.style.color=blnDisabled?"#aca899":"";
    if(this.textField.nodeName=="INPUT")
    {
        this.textField.readOnly=blnDisabled;
    }
    this._icon.style.filter=blnDisabled?"gray":"";
}
function ETSOODDOption(text,value,selected,data)
{
    if(value==null)value=text;
    if(selected==null)selected=false;
    this.text=text;
    this.value=value;
    this.selected=selected;
    this.index=-1;
	this.data=data;
}