function saveMover(oMover,oTo){
    // oMover is a SELECT, oTo is a text field.
    var sOutput='';
    var i;
    var s;
    
    for (i=0; i < oMover.length; i++)
    {
        s=oMover.options[i].value;
        if (s.length>0)
        {
            sOutput=sOutput+s+',';
        }
    }
    
    if (sOutput.length>0)
    {
        sOutput=sOutput.substring(0,sOutput.length-1);
    }
    oTo.value=sOutput;
    // if there's an onchange handler assigned to the field, fire it.
    // We have to do this because it won't automatically fire.
    if (typeof(oTo.onchange)=='function') oTo.onchange();
}

function moverEvent(lstAvailable, lstSelected, iDirection, iptSave) {
    if (iDirection > 0)
    {
        moveIt(lstAvailable,lstSelected);
    } else
    {
        moveIt(lstSelected,lstAvailable);
    }
    saveMover(lstSelected,iptSave);
}

/**
 * Known Issues: If you have a mover with an OPTGROUP with IE 9, 
 * it fails when you try to remove with a DOM Exception 
 * NOT_FOUND_ERR 8 
 */
function moveIt(oFrom, oTo) {
    try {

        if (oFrom.selectedIndex == -1)
        {
            alert('No items selected!');
            return;
        }
        var i, oNewOption, iSelect=0, aRmv = new Array();
        for (i = 0; i < oFrom.length; i++)
        {
            if (oFrom.options[i].selected)
            {
                if (oFrom.options[i].value!="")
                {
                    oNewOption=new Option(oFrom.options[i].text, oFrom.options[i].value);
                    if (oFrom.options[i].title)
                    {
                        oNewOption.title=oFrom.options[i].title;
                    }
                    oTo.options[oTo.options.length]=oNewOption;
                    aRmv.push(i);
                }
            }
        }

        while (aRmv.length>0)
        {
            iSelect=aRmv.pop();
            oFrom.remove(iSelect);
        }

        if (iSelect >= oFrom.length)
            iSelect=oFrom.length-1;
        if (oFrom.length > 0 && oFrom.options[iSelect]!=null)
            oFrom.options[iSelect].selected=true;
    } catch (err) {
        oFrom.selectedIndex=1;
        // alert('An error happened. The message was: '+err.message==null ? err : err.message);
    }
}



// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
    var o = obj.options;
    var i_selected = o[i].selected;
    var j_selected = o[j].selected;
    var i_title=null,j_title=null;
    if (o[i].title) i_title=o[i].title;
    if (o[j].title) j_title=o[j].title;
    var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
    var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
    if (i_title!=null) temp.title=i_title;
    if (j_title!=null) temp2.title=j_title;
    o[i] = temp2;
    o[j] = temp;
    o[i].selected = j_selected;
    o[j].selected = i_selected;
}
function moveSelectedSave(oControl, iDirection,oSaveTo) {
    moveSelected(oControl,iDirection);
    saveMover(oControl,oSaveTo);
}
/** 
 * Move selected items in a HTML SELECT element up or down.
 * 
 * @param oControl  Reference to the HTML SELECT element.
 * @param iDirection 1 for up, -1 for down.
 */
function moveSelected(oControl, iDirection) {
    var i;
    if (iDirection > 0)
    {

        // Move things up.
        for (i = 1; i < oControl.length; i++)
        {
            if (oControl.options[i].selected && !oControl.options[i-1].selected)
            {
                // This one is  selected, the one above is not.
                swapOptions(oControl,i,i-1);
                oControl.options[i-1].selected=true;
            }
        }
    } else
    {
        // Move Things down.
        for (i=oControl.length-2; i>=0; i--)
        {
            if (oControl.options[i].selected &&  !oControl.options[i+1].selected)
            {
                swapOptions(oControl,i,i+1);
                oControl.options[i+1].selected=true;
            }
        }
    }
}


function xferAllSelValues(oSelect) {
    if (oSelect!=null)
    {
        for (i=0; i < oSelect.options.length; i++)
        {
            if (!oSelect.options[i].disabled)
            {
                oSelect.options[i].selected=true;
            }
        }
    }
    return true;
}

function getSelectedOptionsAsCSVList(element) {
var sResult="",i,opt;
for (i=0; i < element.options.length; i++) {
	opt=element.options[i];
	if (opt.selected) {
	sResult+=(sResult.length>0 ? ',' : '')+opt.value;
	}
}	
return sResult;
}

