// JavaScript functions to handle moving items from a one list on a form to another list on the same form

var sourceListName = 'sourceItems';
var destinationListName = 'selectedItems';

/*
	Set the name of the source list element that will hold the items that the user can select from 
	to add them to the destination list element.
*/
function setSourceListName(source) {
    if( source == null ) {
        sourceListName = 'sourceItems';
    }
    sourceListName = source;
}

/*
	Set the name of the destination list element that will hold the selected items from 
	the source list element.
*/
function setDestinationListName(destination) {
    if( destination == null ) {
        destinationListName = 'selectedItems';
    }
    destinationListName = destination;
}

/*
	Add the selected items from the left list(source) to the right list(destination)
*/
function toRightHandler(thisform) {
    var selectObjLeft=thisform.elements[sourceListName];
    var selectObjRight=thisform.elements[destinationListName];

    var rlength = selectObjRight.length;
    for(var i = 0; i < selectObjLeft.length; i++) {
        if ((selectObjLeft.options[i] != null) && (selectObjLeft.options[i].selected)) {
            // Exclude empty left list placeholder
            if( selectObjLeft.options[0].value == "" ) {
                continue;
            }
            // Remove empty list placeholder
            if( selectObjRight.options[0].value == "" ) {
                selectObjRight.options[0] = null;
                rlength--;
            }
            //Check if this value already exist in the right list or not
            //if not then add it otherwise do not add it.
            var found = false;
            var optionText = selectObjLeft.options[i].text;
            var optionValue = selectObjLeft.options[i].value;
            for(var count = 0; count < rlength; count++) {
                if (selectObjRight.options[count] != null) {
                    if (optionValue == selectObjRight.options[count].value) {
                        found = true;
                        break;
                    }
                }
            }
            if (found != true) {
                selectObjRight.options[rlength] = new Option(optionText, optionValue); 
                rlength++;
            }
        }
    }
}

/*
	Deletes all selected items from the right list(destination)
*/
function toLeftHandler(thisform) {
    var selectObjRight=thisform.elements[destinationListName];

    var rlength = selectObjRight.length;
    for(var i = (rlength - 1); i >= 0; i--) {
        if ((selectObjRight.options[i] != null) && (selectObjRight.options[i].selected)) {
            selectObjRight.options[i] = null;
        }
    }
    // The list is empty, add placeholder back in
    if ( selectObjRight.length == 0 ) {
        selectObjRight.options[0] = new Option("--------------------------------", "");
    }
}

/*
	Move all items from the left list(source) to the right list(destination)
*/
function allToRightHandler(thisform) {
    var selectObjLeft=thisform.elements[sourceListName];
    var selectObjRight=thisform.elements[destinationListName];

    var rlength = selectObjRight.length;
    for(var i = 0; i < selectObjLeft.length; i++) {
        if ((selectObjLeft.options[i] != null)) {
            // Exclude empty left list placeholder
            if( selectObjLeft.options[0].value == "" ) {
                continue;
            }
            // Remove empty list placeholder
            if( selectObjRight.options[0].value == "" ) {
                selectObjRight.options[0] = null;
                rlength--;
            }
            //Check if this value already exist in the right list or not
            //if not then add it otherwise do not add it.
            var found = false;
            var optionText = selectObjLeft.options[i].text;
            var optionValue = selectObjLeft.options[i].value;
            for(var count = 0; count < rlength; count++) {
                if (selectObjRight.options[count] != null) {
                    if (optionText == selectObjRight.options[count].text) {
                        found = true;
                        break;
                    }
                }
            }
            if (found != true) {
                selectObjRight.options[rlength] = new Option(optionText, optionValue); 
                rlength++;
            }
        }
    }
}

/*
	Deletes all items from the right list(destination)
*/
function allToLeftHandler(thisform) {
    var selectObjRight=thisform.elements[destinationListName];

    var rlength = selectObjRight.length;
    for(var i = (rlength - 1); i >= 0; i--) {
        if ((selectObjRight.options[i] != null)) {
            selectObjRight.options[i] = null;
        }
    }
    // The list is empty, add placeholder back in
    selectObjRight.options[0] = new Option("--------------------------------", "");
}

/*
	Selects or deselects all items in the right list(destination) depending upon the
	value the selectedFlag parameter
*/
function selectAll(thisform, selectedFlag) {
    var selectObjRight=thisform.elements[destinationListName];

    if( selectedFlag == null ) {
        selectedFlag = true;
    }
//var data = "Selected Patients\n";
    var rlength=selectObjRight.length;
    if (rlength > 0) {
        for(r = 0; r < rlength; r++) {
            if( selectObjRight.options[r].value == "") {
                selectObjRight.options[r].selected = false;
                continue;
            }
            selectObjRight.options[r].selected = selectedFlag;
//data += "[" + r + "]:" + selectObjRight.options[r].value;
//data += " length: " + selectObjRight.options[r].value.length + "\n";
        }
    }
//alert( data );
}

