
function listbox_valueinstring(listID) {
    //listId is name of the listbox
    var listbox = document.getElementById(listID);
    var strString = "";

    for (var count = 0; count < listbox.options.length; count++) {

        if (count > 0) {
            strString = strString + ",";
        }
        strString = strString + listbox.options[count].value;
    }
    return strString;
}

function listbox_selectall(listID, isSelect) {
	//listId is name of the listbox
	//isSelect tells you to select all or deselect all
	var listbox = document.getElementById(listID);
	
	for(var count=0; count < listbox.options.length; count++) {
		listbox.options[count].selected = isSelect;
	}
}

function listbox_removeSelect(sourceID) {
    //Source is the id name of the listbox that you want to delete the selections
    var src = document.getElementById(sourceID);

    for (var count = 0; count < src.options.length; count++) {

        if (src.options[count].selected == true) {
            var option = src.options[count];

            var newOption = document.createElement("option");
            newOption.value = option.value;
            newOption.text = option.text;
            newOption.selected = true;
            try {
                src.remove(count, null); //Standard
            } catch (error) {
                src.remove(count); // IE only
            }
            count--;
        }
    }
}

function listbox_moveacross(sourceID, destID) {
    //Source is the id name of the listbox that you want to move over
    //Destination is the id name of the listbox that should be receiving the value
    var src = document.getElementById(sourceID);
    var dest = document.getElementById(destID);
    var exist = 0;

    for (var count = 0; count < src.options.length; count++) {
        if (src.options[count].selected == true) {
            var option = src.options[count];

            //Is this item already there?
            exist = 0;
            for (var destcount = 0; destcount < dest.options.length; destcount++) {
                if (option.value == dest.options[destcount].value) {
                    exist = 1;
                }
            }

            //If it did not exist, then add it
            if (exist == 0) {
                var newOption = document.createElement("option");
                newOption.value = option.value;
                newOption.text = option.text;
                newOption.selected = true;
                try {
                    dest.add(newOption, null); //Standard
                } catch (error) {
                    dest.add(newOption); // IE only
                }
            }
        }
    }
}


function listbox_move(listID, direction) {
    //listId is name of the listbox
    //direction is 'up' or 'down'

    var listbox = document.getElementById(listID);
    var selIndex = listbox.selectedIndex;

    if (-1 == selIndex) {
        alert("Please select an option to move.");
        return;
    }

    var increment = -1;
    if (direction == 'up')
        increment = -1;
    else
        increment = 1;

    if ((selIndex + increment) < 0 ||
				(selIndex + increment) > (listbox.options.length - 1)) {
        return;
    }

    var selValue = listbox.options[selIndex].value;
    var selText = listbox.options[selIndex].text;
    listbox.options[selIndex].value = listbox.options[selIndex + increment].value
    listbox.options[selIndex].text = listbox.options[selIndex + increment].text

    listbox.options[selIndex + increment].value = selValue;
    listbox.options[selIndex + increment].text = selText;

    listbox.selectedIndex = selIndex + increment;
}

function sortlistbox(listID) {
    var lb = document.getElementById(listID);
    arrTexts = new Array();
    arrValues = new Array();
    arrOldTexts = new Array();

    for (i = 0; i < lb.length; i++) {
        arrTexts[i] = lb.options[i].text;
        arrValues[i] = lb.options[i].value;

        arrOldTexts[i] = lb.options[i].text;
    }

    arrTexts.sort();

    for (i = 0; i < lb.length; i++) {
        lb.options[i].text = arrTexts[i];
        for (j = 0; j < lb.length; j++) {
            if (arrTexts[i] == arrOldTexts[j]) {
                lb.options[i].value = arrValues[j];
                j = lb.length;
            }
        }
    }
}

