/*
	taosMultiObjSelect.js
	===================================================================================
	TAOS
	Taos Multi-Object Selection JavaScript 1.2.0


	Updated: 05/03/2002 14:43
	Authors: Oliver Emberton (oliveremberton@silktide.com)
	===================================================================================
*/
// Populate one list with entries from another, filtered by specified search expression
function taosPopulateFilteredList(sourceEl, destEl, filterString)
{
	// Optimise for no query, just copy source
	/*if (filterString=="")
	{
		newEl = sourceEl.cloneNode(true);
		newEl.style.display='block';
		newEl.style.name=destEl.name;
		newEl.style.id=destEl.id;
		destEl.replaceNode(newEl);
		return;
	}*/

	var newEl = destEl.cloneNode(false);
	destEl.replaceNode(newEl);

	// Optimise for no query, just copy source
	if (filterString=="")
	{
		for(var i=0; i<sourceEl.options.length; i++)
		{
			var optionEl = sourceEl.options(i);
			var newOptionEl = optionEl.cloneNode(true);
			newEl.insertBefore(newOptionEl);
		}
	}
	else
	{
		// Slow query
		for(var i=0; i<sourceEl.options.length; i++)
		{
			var optionEl = sourceEl.options(i);

			if (optionEl.text.toLowerCase().match(filterString.toLowerCase()))
			{
				var newOptionEl = optionEl.cloneNode(true);
				newEl.insertBefore(newOptionEl);
			}
		}
	}
}

// Select all items in the list (added by James)
function taosSelectList(list, changeTo)
{
   for (var i=0;i<list.options.length;i++)
   {
     list.options[i].selected=changeTo;
   }
}

// Add all items to list
function taosAddAllToList(leftListName, rightListName, commaListName, seperator)
{
	// Mozilla Fix
	if(!document.all)
		taosSelectList(document.getElementById(leftListName), true);
	for(var i=0; i<document.getElementById(leftListName).options.length; i++)
	{
		taosAddItemToList(i, leftListName, rightListName, commaListName, seperator);
	}
	// Mozilla Fix
	if(!document.all)
		taosSelectList(document.getElementById(leftListName), false);
}

// Add currently selected item to list
function taosAddSelectedToList(leftListName, rightListName, commaListName, seperator)
{
	var leftList=document.getElementById(leftListName);
	var i=0;
	var sanity=10000;

	if(leftList.options)
	{
	        while(leftList.options[i])
		{
			sanity--;
			if(!sanity)
				break;

			if(leftList.options[i].selected)
			{
				taosAddItemToList(i, leftListName, rightListName, commaListName, seperator);
			}

			i++;
		}
	}
}

// Add selected items to list
function taosAddItemToList(itemNo, leftListName, rightListName, commaListName, seperator)
{
	var leftValue=document.getElementById(leftListName).options[itemNo].value;
	var rightList=document.getElementById(rightListName);
	if(rightList.options)
	{
		var i=0;
		while(rightList.options[i])
		{
			if(rightList.options[i].value==leftValue)
				return false;

			i++;
		}
	}

	var oOpt=document.createElement('OPTION');
	oOpt.value = document.getElementById(leftListName).options[itemNo].value;

	// Mozilla Fix
	if (document.getElementById(commaListName).value.indexOf(seperator+oOpt.value+seperator)==-1)
	{
		oOpt.style.color=document.getElementById(leftListName).options[itemNo].style.color;
		oOpt.text=document.getElementById(leftListName).options[itemNo].text;

		// Mozilla Fix
		if(document.all)
			document.getElementById(rightListName).add(oOpt);
		else
			document.getElementById(rightListName).appendChild(oOpt);

		if (document.getElementById(commaListName).value=="")
			document.getElementById(commaListName).value=seperator;

		document.getElementById(commaListName).value+=oOpt.value+seperator;
		document.getElementById(rightListName).selectedIndex=document.getElementById(rightListName).options.length-1;
	}
}

// Remove all items from list
function taosRemoveAllFromList(leftListName, rightListName, commaListName, seperator)
{
	// Mozilla Fix
	if(!document.all)
	{
		taosSelectList(document.getElementById(rightListName), true);
		var d = document.getElementById(rightListName);
		for(var i=d.options.length-1; i>=0; i--)
		{
			var r = document.getElementById(rightListName).options[i];
			d.removeChild(r);
		}
		taosSelectList(document.getElementById(rightListName), false);
	}
	else
	{
		// Remove all items
		for(var i=document.getElementById(rightListName).options.length-1; i>=0; i--)
		{
			document.getElementById(rightListName).options.remove(i);
		}
	}
	document.getElementById(commaListName).value=seperator;

}

// Remove currently selected items from list
function taosRemoveSelectedFromList(leftListName, rightListName, commaListName, seperator)
{
        var rightList=document.getElementById(rightListName);
	var i=0;
	var sanity=10000;
	var removeList=new Array();

	if(rightList.options)
	{
	        while(rightList.options[i])
		{
			sanity--;
			if(!sanity)
				break;

			if(rightList.options[i].selected)
			{
				removeList.push(i+1);
			}

			i++;
		}

		if(removeList.length)
		{
			var index;

			while(index=removeList.pop())
			{
                                taosRemoveItemFromList(index-1, leftListName, rightListName, commaListName, seperator);
			}
		}
	}
}

// Remove specified item from list
function taosRemoveItemFromList(itemNo, leftListName, rightListName, commaListName, seperator)
{
	var regexSeperator=seperator;
	if (regexSeperator=='\n')
		regexSeperator='\\s\n';

	document.getElementById(rightListName).selectedIndex--;
	var tempValue=document.getElementById(rightListName).options[itemNo].value;
	var tempRegEx=new RegExp(regexSeperator+tempValue+regexSeperator);
	document.getElementById(commaListName).value=document.getElementById(commaListName).value.replace(tempRegEx, seperator);

	// Mozilla Fix
	if(document.all)
		document.getElementById(rightListName).options.remove(itemNo);
	else
	{
		var d = document.getElementById(rightListName);
		var r = document.getElementById(rightListName).options[itemNo];
		d.removeChild(r);
	}

	if (document.getElementById(commaListName).value=="")
		document.getElementById(commaListName).value=seperator;

	if (document.getElementById(rightListName).selectedIndex==-1)
		document.getElementById(rightListName).selectedIndex=document.getElementById(rightListName).options.length-1;

}

function taosListboxSearch(leftHiddenListName, leftListName, searchTextboxName)
{
	taosPopulateFilteredList(document.getElementById(leftHiddenListName), document.getElementById(leftListName), document.getElementById(searchTextboxName).value);
}

// Event handler for keypress in text field where Enter should not
// submit the form
function taosHandleSearchKeypress(leftHiddenListName, leftListName, searchTextboxName)
{
	if(window.event && window.event.keyCode == 13)
	{
		taosListboxSearch(leftHiddenListName, leftListName, searchTextboxName);
  		return false;
  	}
}