function PickList(clientId, picklistName, storeAsText, multiSelect, mustExistInList, alphaSort, autoPostback)
{
        this.clientId = clientId;
        this.textId = clientId + "_Text";
        this.codeId = clientId + "_Code";
        this.listId = clientId + "_Items";
        this.listDivId = clientId + "_ListDIV";
        this.outerDivId = clientId + "_outerDiv";
        this.btnShowId = clientId + "_BtnShow";
        this.HyperTextId = clientId + "_HyperText";
        this.BtnDivId = clientId + "_ListBtnsDIV";
        this.isDroppedDown = false;
        this.storeValueAsText = storeAsText;
        this.multiSelect = multiSelect;
        this.mustExistInList = mustExistInList;
        this.AlphaSort = alphaSort;
        this.AutoPostBack = autoPostback;
        this.PickListName = picklistName;
        this.multiSelectValues = new Array();
        this.onChange = new YAHOO.util.CustomEvent("change", this);
}

function PickList_setVal(newVal) { 
    var text = document.getElementById(this.textId);
    var code = document.getElementById(this.codeId);
    var list = document.getElementById(this.listId);
    
        if (this.storeValueAsText == "True") {
           text.value = newVal;
           code.value = newVal; 
           if (this.multiSelect == "True")
           {
                this.multiSelectValues = new Array();
                var values = newVal.split(",");
                var listDiv = document.getElementById(this.listDivId);
                var items = listDiv.getElementsByTagName("input");
                var item;
                for (var i=0; i < items.length; i++)
                {
                    for (var j=0; j < values.length; j++)
                    {
                        if (items[i].type == "checkbox") 
                        {
                            if (items[i].value == values[j]) 
                            { 
                                items[i].checked = true; 
                                this.multiSelectValues[items[i].id] = items[i].value;
                                break;
                            }
                            else { items[i].checked = false; }
                        }
                    }
                }
           }
           else
           {
                // value is text, we need to set the selected item
                for (key=0; key<list.options.length; key++)
                {
                    if (list.options[key].text == newVal)
                    {
                        text.value = list.options[key].text;
                        code.value = newVal;
                        list.options[key].selected = true;
                        break;
                    }
                }
           }  
        } else {
            // value is an Id, we need to lookup the text
            for (key=0; key<list.options.length; key++)
            {
                if (list.options[key].value == newVal)
                {
                    text.value = list.options[key].text;
                    code.value = newVal;
                    list.options[key].selected = true;
                    break;
                }
            }
        }
    
}

function PickList_getVal() {
    var text = document.getElementById(this.textId);
    var code = document.getElementById(this.codeId);
    if (this.storeValueAsText == "True") {
        return text.value;
    } else {
        return code.value;
    }
}

function PickList_HandleFocusLeave()
{
    var text = document.getElementById(this.textId);
    this.SetVisibility(false);
}

function PickList_SetVisibility(visible)
{
    var listDiv = document.getElementById(this.listDivId);
    if( visible)
    {
        listDiv.style.display = "block";
    } else {
        listDiv.style.display = "none";
    }
}

function PickList_ButtonClick()
{
    var listDiv = document.getElementById(this.listDivId);
    var controlDiv = document.getElementById(this.clientId);
    var list = document.getElementById(this.listId);

    if( this.isDroppedDown ) {
        listDiv.style.display = "none";
        controlDiv.style.zIndex = 0;
        this.isDroppedDown = false;
    } else { 
        this.GetList();
        var ua = navigator.userAgent.toLowerCase();
        var textbox = $get(this.textId);
        var bounds = Sys.UI.DomElement.getBounds(textbox);
        if (bounds.width != 0)
        {
            listDiv.style.width = bounds.width + "px";
        }
        
        controlDiv.style.zIndex = 1;
        listDiv.style.display = "block";
        this.isDroppedDown = true;
        
        /* resize the iframe now that we've displayed the dropdown */
        if (this.multiSelect == "True")
        {
            var frame = $get(this.clientId + "_frame");
            var innerList = $get(this.clientId + "_InnerListDIV");
            
            if (innerList)
            {
                frame.style.width = innerList.offsetWidth + 'px';
                frame.style.height = innerList.offsetHeight + 'px';
            }
        }
        
        list.focus();
    }
}

function PickList_GetList()
{
    var list = document.getElementById(this.listId);
    
    if ((this.multiSelect == "True") || (list.options.length == 0))
    {
        if (typeof(xmlhttp) == "undefined") {
            xmlhttp = YAHOO.util.Connect.createXhrObject().conn;
        }
        var text = document.getElementById(this.textId);
                       
        var vURL = "SLXPickListCache.aspx?picklist=" + this.PickListName + "&multiselect=" + this.multiSelect + "&alphasort=" + this.AlphaSort + "&clientid=" + this.clientId + "&current=" + text.value;
        xmlhttp.open("GET", vURL, false);
        xmlhttp.send(null);
        this.HandleHttpResponse(xmlhttp.responseText);
    }
}

function PickList_HandleHttpResponse(results)
{
    if (results == "NOTAUTHENTICATED")
    {
        window.location.reload(true);
        return;
    }
    if (this.multiSelect == "True")
    {
        document.getElementById(this.listId).innerHTML = results;
        var listDiv = document.getElementById(this.listDivId);
        var items = listDiv.getElementsByTagName("input");
        for (var i=0; i < items.length; i++)
        {
            if (items[i].checked)
            {
                this.multiSelectValues[items[i].id] = items[i].value;
            }
            else
            {
                this.multiSelectValues[items[i].id] = undefined;
            }
        }
    }
    else
    {
        var list = document.getElementById(this.listId);
        var items = results.split("|");
        for (var i=0; i<items.length; i++)
        {
            if (items[i] == "") continue;
            var parts = items[i].split(",");
            var oOption = document.createElement("OPTION");
            list.options.add(oOption);
            if (parts[0].charAt(0) == '@')
            {
                parts[0] = parts[0].substr(1);
                oOption.selected = true;
            }
            oOption.innerHTML = parts[1];
            oOption.value = parts[0];
        }
        //this.isDroppedDown = true;
        //list.focus();
    }
}

function PickList_SelectionChanged()
{
    var list = document.getElementById(this.listId);
    if(list.options.length > 0)
    {
        var text = document.getElementById(this.textId);
        var code = document.getElementById(this.codeId);
        var hyperText = document.getElementById(this.HyperTextId);
        var oldText = text.value;
        if (list.selectedIndex != -1)
        {
            var newText;
            if (list.options[list.selectedIndex].innerText)
            {
                newText = list.options[list.selectedIndex].innerText;
            }
            else
            {
                newText = list.options[list.selectedIndex].textContent; 
            }
            var newValue = list.options[list.selectedIndex].value;
            if(oldText != newText)
            {
                text.value = newText;
                if (hyperText != undefined)
                {
                    hyperText.innerText = newText;
                }
                code.value = newValue;
            }
        }
        this.SetVisibility(false);
        this.isDroppedDown = false;
        if (this.AutoPostBack)
        {
            if (Sys)
            {
                Sys.WebForms.PageRequestManager.getInstance()._doPostBack(this.clientId, null);
            }
            else
            {
                document.forms(0).submit();
            }
        }
        this.onChange.fire(this);
        this.InvokeChangeEvent(text);
    }
}

function PickList_MultiSelect()
{
    var text = document.getElementById(this.textId);
    var hasMultiple = false;
    var listDiv = document.getElementById(this.listDivId);
    var items = listDiv.getElementsByTagName("input");
    var hyperText = document.getElementById(this.HyperTextId);
    text.value = "";
    for (var i=0; i < items.length; i++)
    {
        if (this.multiSelectValues[items[i].id] != undefined)
        {
            if (hasMultiple) { text.value = text.value + "," + this.multiSelectValues[items[i].id]; }
            else
            {
                text.value = this.multiSelectValues[items[i].id];
                hasMultiple = true;
            }
        }
    }
    if (hyperText != undefined)
    {
        hyperText.innerText = text.value;
    }
    this.SetVisibility(false);
    this.isDroppedDown = false;
    this.onChange.fire(this);
    this.InvokeChangeEvent(text);
}

function PickList_HandleEnterKey(e)
{
    if (!e) var e = window.event;
    if (e.keyCode == 13) //Enter
    {
        //alert("enter pressed");
        e.returnValue = false;
        e.cancelBubble = true;
        this.SelectionChanged();
    }
}

function PickList_TextChange(e)
{
    if (!e) var e = window.event;
    if (e.keyCode == 13) //Enter
    {
        return;
    }
    
        var text = document.getElementById(this.textId);
        var list = document.getElementById(this.listId);
        var items = list.options;
        if (items.length == 0) { this.GetList(); }
        if( text.value == "" ) 
        {
            if ((this.isDroppedDown) && (!this.mustExistInList)) 
            { 
                this.SetVisibility(false);
                this.isDroppedDown = false; 
            }
            return;
        }
        if (this.isDroppedDown)
        {
            if (e.keyCode == 40) //Down
            {
                list.selectedIndex = list.selectedIndex + 1;
                return;
            }
            if (e.keyCode == 38) //Up
            {
                list.selectedIndex = list.selectedIndex - 1;
                return;
            }
        }
        if (this.mustExistInList)
        {
            this.SetVisibility(true);
            this.isDroppedDown = true;
        }
        else { list.selectedIndex = -1; }
        var matchFound = false;
        for( var i = 0; i < items.length; i++ ) {
            var item = items[i];
            if( item.text.toLowerCase().indexOf( text.value.toLowerCase() ) == 0 ) {
                list.selectedIndex = i;
                matchFound = true;
                if (!this.isDroppedDown)
                {
                    this.SetVisibility(true);
                    this.isDroppedDown = true;
                }
                break;
            }
        }
        if ((this.mustExistInList) && (!matchFound))
        {
            text.value = text.value.substr(0,text.value.length -1);
        }
        if (list.selectedIndex == -1)
        {
            this.SetVisibility(false);
            this.isDroppedDown = false;
        }
}

function PickList_SetCheckedState(itemId, index, value)
{
    var checkbox = document.getElementById(itemId);
    if (checkbox.checked)
    {
        checkbox.checked = false;
        this.multiSelectValues[checkbox.id] = undefined;
    }
    else
    {
        checkbox.checked = true;
        this.multiSelectValues[checkbox.id] = value;
    }
}

function PickList_InvokeChangeEvent(cntrl)
{
    if (document.createEvent)
    {
        //FireFox
        var evObj = document.createEvent('HTMLEvents'); 
        evObj.initEvent ('change', true, true); 
        cntrl.dispatchEvent(evObj);
    }
    else
    {
        //IE
        cntrl.fireEvent('onchange');
    }
}

PickList.prototype.setVal = PickList_setVal;
PickList.prototype.getVal = PickList_getVal;
PickList.prototype.SetVisibility = PickList_SetVisibility;
PickList.prototype.ButtonClick = PickList_ButtonClick;
PickList.prototype.SelectionChanged = PickList_SelectionChanged;
PickList.prototype.MultiSelect = PickList_MultiSelect;
PickList.prototype.TextChange = PickList_TextChange;
PickList.prototype.SetCheckedState = PickList_SetCheckedState;
PickList.prototype.HandleFocusLeave = PickList_HandleFocusLeave;
PickList.prototype.HandleEnterKey = PickList_HandleEnterKey;
PickList.prototype.GetList = PickList_GetList;
PickList.prototype.HandleHttpResponse = PickList_HandleHttpResponse;
PickList.prototype.InvokeChangeEvent = PickList_InvokeChangeEvent;


if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 