/*
 * functions to deal with moving things around in select lists
 *
 * @version 2005.01.31
 */

function listDeselect(theList)
{
    if (theList.length == 0)
    {
        return;
    }
    for (var loop = 0; loop < theList.length; loop++)
    {
        theList.options[loop].selected = false;
    }
}

function listToArray(theList, onlySelected)
{
    var theArray = new Array();
    for (var loop = 0; loop < theList.length; loop++)
    {
        if (onlySelected == true && theList.options[loop].selected == false)
        {
            continue;
        }

        if (theList.options[loop].value == "")
        {
            continue;
        }

        var no = new Option();
        no.value = theList.options[loop].value;
        no.text = theList.options[loop].text;
        theArray.push(no);
    }
    return theArray;
}

function arrayToList(theArray, theList)
{
    for (var loop = theList.length - 1; loop >= 0; loop--)
    {
        theList.options[loop] = null;
    }

    for (var loop = 0; loop < theArray.length; loop++)
    {
        theList.options[theList.options.length] = theArray[loop];
    }

    if (theArray.length == 0)
    {
        theList.options[0] = new Option(window.noneText ? window.noneText : "(no columns)", "");
    }
}

function mergeArray(fromArray, toArray, whereToInsert)
{
    for (var loop = 0; loop < fromArray.length; loop++)
    {
        if (whereToInsert >= 0)
        {
            toArray.splice(++whereToInsert, 0, fromArray[loop]);
        }
        else
        {
            toArray.push(fromArray[loop]);
        }
    }
}

function arrayContains(theArray, theValue)
{
    for (var loop = 0; loop < theArray.length; loop++)
    {
        if (theArray[loop].value == theValue)
        {
            return true;
        }
    }
    return false;
}

function listHighlight(theList, selectArray)
{
    for (var loop = 0; loop < theList.length; loop++)
    {
        if (arrayContains(selectArray, theList.options[loop].value))
        {
            theList.options[loop].selected = true;
        }
        else
        {
            theList.options[loop].selected = false;
        }
    }
}

function arrayPurge(theArray, selectArray)
{
    for (var loop = theArray.length - 1; loop >= 0; loop--)
    {
        if (arrayContains(selectArray, theArray[loop].value))
        {
            theArray.splice(loop, 1);
        }
    }
}

function listMove(fromListId, toListId)
{
	var fromList = document.getElementById(fromListId);
	var toList = document.getElementById(toListId);

    if (fromList.length == 0 || (fromList.length == 1 && fromList.options[0].value == ""))
    {
        alert("You can't move an empty list!");
        return;
    }

    var selectArray = listToArray(fromList, true);
    if (selectArray.count == 0)
    {
        alert("Select the columns to move.\n\nUse shift-click or control-click to select multiple columns");
    }

    var toArray = listToArray(toList, false);
    var whereToInsert = toList.selectedIndex;

    mergeArray(selectArray, toArray, whereToInsert);
    arrayToList(toArray, toList);
    listHighlight(toList, selectArray);

    listPurge(fromList, selectArray);
}

function listPurge(fromList, selectArray)
{
    var curTop = fromList.scrollTop;
    for (var loop = fromList.length - 1; loop >= 0; loop--)
    {
        if (arrayContains(selectArray, fromList.options[loop].value))
        {
            fromList.options[loop] = null;
        }
    }

    fromList.scrollTop = curTop;
    if (fromList.length == 0)
    {
        fromList.options[0] = new Option(window.noneText ? window.noneText : "(no columns)", "");
    }
}

function listFirstHighlight(theList)
{
    for (var loop = 0; loop < theList.length; loop++)
    {
        if (theList.options[loop].selected == true)
        {
            return loop;
        }
    }
    return -1;
}

function listLastHighlight(theList)
{
    for (var loop = theList.length - 1; loop >= 0; loop--)
    {
        if (theList.options[loop].selected == true)
        {
            return loop;
        }
    }
    return -1;
}

function listSwap(theList, from, to)
{
    var text = theList.options[to].text;
    var value = theList.options[to].value;
    var selected = theList.options[to].selected;

    theList.options[to].text = theList.options[from].text;
    theList.options[to].value = theList.options[from].value;
    theList.options[to].selected = theList.options[from].selected;

    theList.options[from].text = text;
    theList.options[from].value = value;
    theList.options[from].selected = selected;
}

function listToText(theList, theInput)
{
    var text = "";
    var theArray = listToArray(theList, false);
    for (var loop = 0; loop < theArray.length; loop++)
    {
        text = text + theArray[loop].value + ",";
    }
    theInput.value = text;
}

function listReorder(theList, amount)
{
    var selectArray = listToArray(theList, true);
    if (selectArray.length == 0)
    {
        alert("Select the columns to move.\n\nUse shift-click or control-click to select multiple columns");
        return;
    }

    if (amount > 0)
    {
        for (var multi = 0; multi < amount; multi++)
        {
            if (theList.options[theList.length - 1].selected == true)
            {
                break;
            }

            for (var loop = theList.length - 1; loop >= 1; loop--)
            {
                if (theList.options[loop - 1].selected == true)
                {
                    listSwap(theList, loop - 1, loop);
                }
            }
        }
    }
    else
    {
        amount = -1 * amount;
        for (var multi = 0; multi < amount; multi++)
        {
            if (theList.options[0].selected == true)
            {
                break;
            }
            for (var loop = 0; loop < theList.length - 1; loop++)
            {
                //alert("testing " + (loop - amount) + " with " + loop );
                if (theList.options[loop + 1].selected == true)
                {
                    //alert("swapping " + (loop - amount) + " with " + loop );
                    listSwap(theList, loop + 1, loop);
                }
            }
        }
    }
}
