// This file is used for moving data from one multi-select box to another.
// Crystal Keen
// © 2001 Braun Consulting, Inc.

function selectAll(ctrl) {
	deleteDefault( ctrl );
	for (i=0; i<ctrl.length; i++) {
        ctrl.options[i].selected = true;
	}
}

function deleteDefault(ctrl) {
    if( ctrl.length > 0 && ctrl.options[0].value == "" ) {
        ctrl.options[0] = null;
	}
}

function itemExists( toCtrl, strSearch )
{
	var j, foundMatch;
	for( j=0, foundMatch=false; j<toCtrl.options.length && !foundMatch; j++ )
	{
		if (toCtrl.options[j].value == strSearch) {
			foundMatch = true;
		}
	}
	return foundMatch;
}

function addOption( toCtrl, optionText, optionVal )
{
	var nextitem;
	// search toCtrl to see if duplicate
	if( !itemExists( toCtrl, optionVal ))
	{
		deleteDefault( toCtrl );
		nextitem = toCtrl.options.length;
		toCtrl.options[nextitem] = new Option( optionText );
		toCtrl.options[nextitem].value = optionVal;
	}
}

function setOption( toCtrl, index, optionText, optionVal )
{
	toCtrl.options[index] = new Option( optionText );
	toCtrl.options[index].value = optionVal;
	toCtrl.options.selectedIndex = index;
}

function removeOption( fromCtrl, optionIndex )
{
	fromCtrl.options[optionIndex] = null;
}

function addSelected(fromCtrl, toCtrl) {
	var i, j, itemexists, nextitem;
	//step through all items in fromCtrl
	for (i = 0; i < fromCtrl.options.length; i++) {
		if (fromCtrl.options[i].selected && fromCtrl.options[i].value != "" ) {
			// add the item
			addOption( toCtrl, fromCtrl.options[i].text, fromCtrl.options[i].value );
		}
	}
}

function removeSelected( fromCtrl ) {
	var i;
	if (fromCtrl.selectedIndex > -1) {
		for (i=fromCtrl.options.length-1; i>=0; i--) {
			if (fromCtrl.options[i].selected)
				removeOption( fromCtrl, i );
		}
	}
}

// This function deletes the default value put in the multi select box,
// and ensures that it is not empty
function checkMultiValues( ctrl )
{
    deleteDefault( ctrl );
	return ( ctrl.length != 0 );
}

// Used for resetting an option to 0 and clearing out the remaining options
function resetNoneOption( ctrl )
{
	ctrl.options[0].selected = true;
	for( i=1; i<ctrl.length; i++ )
		ctrl.options[i] = null;
}

