﻿/**********************************************************
 * sortList
 *
 * Sorts the list of elements in the specified control by
 * ascending order.
 *
 * TODO: Add parallel sorting of lists with values
 * different from text displayed.
 *********************************************************/
function sortList(ctrlID)
{
    //var lstBox = document.all(ctrlID);   
    var lstBox = document.forms[0][ctrlID];
    var arrLstTexts = new Array();
    //var arrLstValues = new Array();
    
    for(i=0; i<lstBox.length; i++)
    {
        arrLstTexts[i] = lstBox.options[i].text;
        //arrLstValues[i] =  lstBox.options[i].value;
    }
    
    arrLstTexts.sort();
    
    for(i=0; i<lstBox.length; i++)
    {
        lstBox.options[i].text = arrLstTexts[i];
        lstBox.options[i].value = arrLstTexts[i];
    }    
}

/**********************************************************
 * selectAllListItems
 *
 * Selects all items in the specified control.
 *********************************************************/
function selectAllListItems(ctrlID)
{
    var lstBox = document.all(ctrlID);
    
    for(i=0; i<lstBox.length; i++)
    {
        lstBox.options[i].selected=true;
    }
}

/**********************************************************
 * moveListItem
 *
 * Moves the selected list items from the source control 
 * to the destination control specified.
 *********************************************************/
function moveListItem(lstSource, lstDest)
{
    //var m_lstSource = document.all(lstSource);
    //var m_lstDest = document.all(lstDest);
    var m_lstSource = document.forms[0][lstSource];
    var m_lstDest = document.forms[0][lstDest];

    if(m_lstSource.length < 1)
    {
        alert('No items to move.');
    }
    else
    {
        if(m_lstSource.options.selectedIndex == -1)
        {
            alert('Please select an item to move.');
        }
        else
        {
            while(m_lstSource.options.selectedIndex >= 0)
            {
                var m_lstItem = new Option() // Create new ListItem instance
                
                m_lstItem.text = m_lstSource.options[m_lstSource.options.selectedIndex].text;
                m_lstItem.value = m_lstSource.options[m_lstSource.options.selectedIndex].value;
                m_lstDest.options[m_lstDest.length] = m_lstItem;
                
                m_lstSource.remove(m_lstSource.options.selectedIndex);
            }
            
            sortList(lstSource);
            sortList(lstDest);
        }
    }
    
    return false;
}

function serializeList(ctrlID_ListBox, ctrlID_Store)
{
    var lstBox = document.forms[0][ctrlID_ListBox];
    var hidElement = document.forms[0][ctrlID_Store];
 
    hidElement.value = "";
   
    for(i=0; i<lstBox.length; i++)
    {
        hidElement.value += lstBox.options[i].value+";";              
    }
    
    return false;
} 