function MoveOptionUp(list, hiddenField) 
{
    if(list.selectedIndex > 0)
    {
	    var selectedCount = CountSelectedOptions(list);
	    
	    if(selectedCount == 1) 
	    {
		    SwapOptions(list, list.selectedIndex, list.selectedIndex - 1);	        
        }
        
        PersistList(list);
    }
}

function MoveOptionDown(list, hiddenField)
{
    if(list.selectedIndex >= 0 && list.selectedIndex < list.options.length - 1)
    {
	    var selectedCount = CountSelectedOptions(list);
	    
	    if(selectedCount == 1) 
	    {
		    SwapOptions(list, list.selectedIndex, list.selectedIndex + 1);	        
        }
        
        PersistList(list);
    }    
}

function AppendToList(list, text, value)
{
    var opt = new Option(text, value, true, true);
    list.options[list.options.length] = opt;
    
    PersistList(list);
}

function InsertOption(list, option, index)
{
    var newPosition = list.options.length;
    list.options[newPosition] = option;
    
    for(var i = newPosition; i > index; i--)
    {
        SwapOptions(list, i, i - 1);
    }
    
    PersistList(list);
}


function SwapOptions(list, i, j) 
{
    var o = list.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp_i = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp_j= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	
	o[i] = temp_j;
	o[j] = temp_i;
	
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

function FindOptionByValue(list, value)
{
    var index = -1;
    
    for(var i = 0; i < list.options.length; i++)
    {
        if(list.options[i].value == value)
        {
            index = i;
            break;
        }
    }
    
    return index;
}

function FindOptionByText(list, text)
{
    var index = -1;
    
    for(var i = 0; i < list.options.length; i++)
    {
        if(list.options[i].text == text)
        {
            index = i;
            break;
        }
    }
    
    return index;
}

function GetSelectedValue(list)
{
    var opt = GetSelectedOption(list);
    var val = "";
    
    if(opt != null)
    {
        val = opt.value;
    }
    
    return val;
}

function GetSelectedText(list)
{
    var opt = GetSelectedOption(list);
    var text = "";
    
    if(opt != null)
    {
        text = opt.text;
    }
    
    return text;
}

function GetSelectedOption(list)
{
    var opt = null;
    
    if(list.selectedIndex != -1)
    {
        opt = list.options[list.selectedIndex];
    }
    
    return opt;
}

function MoveOptionsToList(fromList, toList)
{
    var copiedIndexes = new Array();
    var option;
    var from = fromList.options;
    
    for(var i = 0; i < from.length; i++)
    {
        if(from[i].selected)
        {
            option = new Option(from[i].text, from[i].value, from[i].defaultSelected, from[i].selected);
            toList.options[toList.options.length] = option;
            copiedIndexes[copiedIndexes.length] = i;                    
        }
    }
    
    for(var i = 0; i < copiedIndexes.length; i++)
    {
        fromList.options[copiedIndexes[i] - i] = null;
    }
    
    PersistList(fromList);
    PersistList(toList);
}

function CountSelectedOptions(list)
{
    var count = 0;
    
    for(var i = 0; i < list.options.length; i++)
    {
        if(list.options[i].selected)
        {
            count++;
        }
    }
    
    return count;
}

function SortList(list)
{
    list.options.sort(OptionTextSort);
}

function OptionTextSort(a, b)
{
    if((a.text + "") < (b.text + ""))
    {
        return -1;
    }
    else if((a.text + "") > (b.text + ""))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
	
function SelectAll(list)
{
    SetSelections(list, true);
}

function ClearAll(list)
{
    SetSelections(list, false);
}

function RemoveAll(list)
{
    list.options.length = 0;
    PersistList(list);
}

function RemoveSelected(list)
{
    for(var i = list.options.length - 1; i >= 0; i--)
    {
        if(list.options[i].selected)
        {
            list.options[i] = null;
        }        
    }    
    
    PersistList(list);
}

function SetSelections(list, value)
{
    for(var i = 0; i < list.options.length; i++)
    {
        list.options[i].selected = value;
    }
}

function PersistAllLists()
{
    var selects = document.getElementsByTagName("select");
    
    for(var i = 0; i < selects.length; i++)
    {
        PersistList(selects[i]);
    }
}

function PersistList(list, hiddenField)
{

    if(hiddenField == null)
    {
        hiddenField = GetAssociatedHiddenField(list);
    }
    
    if(hiddenField != null)
    {
        hiddenField.value = "";
        
        if(list.disabled)
        {
            hiddenField.value += "0";
        }
        else
        {
            hiddenField.value += "1";
        }
        
        hiddenField.value += "::" + list.selectedIndex + ";;";
        
        for(var i = 0; i < list.options.length; i++)
        {
            hiddenField.value += list.options[i].value + "::";
            hiddenField.value += list.options[i].text + "::";
            
            if(list.options[i].selected)
            {
                hiddenField.value += "1";
            }
            else
            {
                hiddenField.value += "0";
            }
            
            if(list.disabled)
            {
                hiddenField.value += "0";
            }
            else
            {
                hiddenField.value += "1";
            }
                        
            hiddenField.value += ";;";
        }
        
        var s = hiddenField.value;
        hiddenField.value = s.substring(0, s.length - 2);
    }
}

function PersistElement(element, hiddenField)
{
    if(hiddenField == null)
    {
        hiddenField = GetAssociatedHiddenField(element);
    }
    
    if(hiddenField != null)
    {    
        hiddenField.value = element.value;
        hiddenField.value += "::";
        
        if(element.disabled)
        {
            hiddenField.value += "0";
        }
        else
        {
            hiddenField.value += "1";
        }        
        
        if(typeof(element.selectedIndex) != "undefined")
        {
            hiddenField.value += "::" + element.selectedIndex;
        }
    }    
}

function PersistValue(element, hiddenField)
{
    if(hiddenField == null)
    {
        hiddenField = GetAssociatedHiddenField(element);
    }
    
    if(!(IsNull(hiddenField) || IsNull(element)))
    {
        if(typeof(element.selectedIndex) != "undefined")
        {
            var opt = element.options[element.selectedIndex];
            
            if(opt.value.length > 0)
            {
                hiddenField.value = opt.value;
            }
        }
        else if(typeof(element.checked) != "undefined")
        {
            hiddenField.value = element.checked;            
        }
        else if(typeof(element.value) != "undefined")        
        {
            hiddenField.value = element.value;
        }
        else if(typeof(element.innerText) != "undefined")
        {
            hiddenField.value = element.innerText;
        }
    }
}

function GetAssociatedHiddenField(element)
{
    var name = "";
    
    if(typeof(element.hiddenfield) != "undefined")
    {
        name = element.hiddenfield;
    }
    else
    {
        var index = element.id.indexOf("_");
        
        if(index == -1)
        {
            name = "_" + element.id;
        }
        else
        {
            name = element.id.substr(0, index) + "_" + element.id.substr(index);
        }
    }    
    
    return _frm.elements[name];
}

function EnforceMaxLength(element)
{
    var belowMax = false;
    
    if(typeof(element.maxLength) == "string")
    {
        var maxLength = parseInt(element.maxLength);
        
        if(element.value.length < maxLength)
        {
            belowMax = true;
        }        
    }
    else
    {
        belowMax = true;
    }
 
    if(window.event)
    {
        window.event.returnValue = belowMax;
    }
    
    return belowMax;
}

function CountRemainingCharacters(element)
{
    if(typeof(element._MaxLength) == "string")
    {
        var maxLength = parseInt(element._MaxLength);
        var remaining = maxLength - element.value.length;
        var remainingElementID = "lbl" + element.id + "Remaining";
        var remainingElement = document.all[remainingElementID];
        
        if(remainingElement != null)
        {
            remainingElement.innerText = remaining;
            
            if(remaining < 0)
            {
                remainingElement.className = "error";
            }
            else
            {
                remainingElement.className = "";
            }
        }                
    }   
    
    return false; 
}
