function clearAll(fromCtrl)
{
	var i;
	var len = fromCtrl.options.length;
		for(i = 0; i < len; i++)
                fromCtrl.options[fromCtrl.options.length - 1] = null;

}

function insertItem(toCtrl,value,name)
{
	nextitem = toCtrl.options.length;
	toCtrl.options[nextitem] = new Option(name);
	toCtrl.options[nextitem].value = value;
}

function selectAll(fromCtrl)
{
	var i;

	for(i = 0; i < fromCtrl.options.length; i++)
	{
		fromCtrl.options[i].selected = true;
	}
}

function selectItem(fromCtrl, id)
{
	var i;

	for (i = 0; i < fromCtrl.options.length; i++)
	{
		if(fromCtrl.options[i].value == id) fromCtrl.options[i].selected = true;
	}

}

function findItem(fromCtrl, id)
{
	var i;

	for (i = 0; i < fromCtrl.options.length; i++)
	{
		if(fromCtrl.options[i].value == id) return fromCtrl.options[i];
	}
 return false;
}

function addItems(fromCtrl, toCtrl, pretag, preval, donotallow)
{

	var i;
	var j;
	var itemexists;
	var nextitem;

if(typeof(pretag) == 'undefined') pretag = '';
if(typeof(preval) == 'undefined') preval = '';

	// step through all items in fromCtrl
	for (i = 0; i < fromCtrl.options.length; i++)
	{
		if (fromCtrl.options[i].selected)
		{
			// search toCtrl to see if duplicate
			j = 0;
			itemexists = false;
			while ((j < toCtrl.options.length) && (!(itemexists)))
			{ 
				if (toCtrl.options[j].value == ((typeof(preval) != 'undefined')?(preval + fromCtrl.options[i].value):fromCtrl.options[i].value))
				{
					itemexists = true;
					//alert(fromCtrl.options[i].value + " found!");
				}
				j++;
			}
			if(typeof(donotallow) != 'undefined')
			{
			 if(typeof(donotallow) == 'object')
			 {
				var k;
				for(k=0; k < donotallow.length; k++)
				{
				 if(fromCtrl.options[i].value == donotallow[k]) itemexists = true;
				}
			 }
			 else if(fromCtrl.options[i].value == donotallow) itemexists = true;
			}

			if (!(itemexists))
			{
				// add the item
				nextitem = toCtrl.options.length;
				toCtrl.options[nextitem] = new Option(pretag + fromCtrl.options[i].text);
				toCtrl.options[nextitem].value = preval + fromCtrl.options[i].value;
			}
		}
	}

}

function removeItems(fromCtrl)
{

	var i = 0;
	var j;
	var k = 0;

	while (i < (fromCtrl.options.length - k))
	{
		if (fromCtrl.options[i].selected == true)
		{
			// remove the item
			for (j = i; j < (fromCtrl.options.length - 1); j++)
			{
				fromCtrl.options[j].text = fromCtrl.options[j+1].text;
				fromCtrl.options[j].value = fromCtrl.options[j+1].value;
				fromCtrl.options[j].selected = fromCtrl.options[j+1].selected;
			}
			k++;
		} else {
			i++;
		}
	}

	for (i = 0; i < k; i++)
	{
		fromCtrl.options[fromCtrl.options.length - 1] = null;
	}

}

