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);
}

function moveIt(oFrom, oTo) {
    var iFirstSelect=99;    
    
    if (oFrom.selectedIndex == -1) {
        alert('No items selected!');
        return;
    }

    aRmv = new Array(oFrom.length);
    var sTitle,oNewOption;
    for (i = 0; i < oFrom.length; i++) {
        if (oFrom.options[i].selected) {
            if (oFrom.options[i].value!="") {
		if (oFrom.options[i].title) {
			sTitle=oFrom.options[i].title;
		} else { sTitle=null };
		oNewOption=new Option(oFrom.options[i].text, oFrom.options[i].value);
                oTo.options[oTo.options.length] = oNewOption;
		if (sTitle!=null) oNewOption.title=sTitle;
                aRmv[i] = 1;
            } else {
                oFrom.options[i].selected=false;
            }
        } else
            aRmv[i] = 0;
    }

    for (j = (oFrom.length - 1); j > -1; j--) {
        if (aRmv[j] == 1) {
            if (j < iFirstSelect)
                iFirstSelect=j;
            oFrom.options[j] = null;
        }
    }

    if (iFirstSelect >= oFrom.length)
        iFirstSelect=oFrom.length-1;
    if (oFrom.length > 0)
        oFrom.options[iFirstSelect].selected=true;
}

// -------------------------------------------------------------------
// 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;
}
