/*
	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;
	}*/
	
	if (document.replaceNode) // MSIE only it seems
	{
		newEl = destEl.cloneNode(false);
		destEl.replaceNode(newEl);
	}
	else
	{
		newEl = destEl.cloneNode(false);
		//alert("Got "+destEl);
		//alert("Got "+destEl.parentNode);
		//alert("Got "+newEl);
		//destEl.parentNode.replaceChild(destEl, newEl);
	}
	
	// Optimise for no query, just copy source
	if (filterString=="")
	{
		for(i=0; i<sourceEl.options.length; i++)
		{
			optionEl = sourceEl.options(i);
			newOptionEl = optionEl.cloneNode(true);
			newEl.insertBefore(newOptionEl);
		}
	}
	else
	{
		// Slow query
		for(i=0; i<sourceEl.options.length; i++)
		{
			optionEl = sourceEl.options(i);
			
			if (optionEl.text.toLowerCase().match(filterString.toLowerCase()))
			{
				newOptionEl = optionEl.cloneNode(true);
				newEl.insertBefore(newOptionEl);
			}
		}
	}
}

// Add all items to list
function taosAddAllToList(leftListName, rightListName, commaListName, seperatorString)
{
	// Add all items
	for(i=0; i<document.getElementById(leftListName).options.length; i++)
	{
		taosAddItemToList(i, leftListName, rightListName, commaListName, seperatorString);
	}
}

// Add currently selected item to list
function taosAddSelectedToList(leftListName, rightListName, commaListName, seperatorString)
{
	itemNo=document.getElementById(leftListName).selectedIndex;
	if (itemNo==-1) 
		return false;

	taosAddItemToList(itemNo, leftListName, rightListName, commaListName, seperatorString);
}

// Add specified item to list
function taosAddItemToList(itemNo, leftListName, rightListName, commaListName, seperatorString)
{
	oOpt=document.createElement('OPTION');
	oOpt.value=document.getElementById(leftListName).options(itemNo).value;
	
	if (document.getElementById(commaListName).value.indexOf(seperatorString+oOpt.value+seperatorString)!=-1) 
		return false;
	
	oOpt.style.color=document.getElementById(leftListName).options(itemNo).style.color;
	oOpt.text=document.getElementById(leftListName).options(itemNo).text;
			
	document.getElementById(rightListName).add(oOpt);
	document.getElementById(commaListName).value+=oOpt.value+seperatorString;
	document.getElementById(rightListName).selectedIndex=document.getElementById(rightListName).options.length-1;
}

// Remove all items from list
function taosRemoveAllFromList(leftListName, rightListName, commaListName, seperatorString)
{
	// Remove all items
	for(i=document.getElementById(rightListName).options.length-1; i>=0; i--)
	{
		document.getElementById(rightListName).options.remove(i);	
	}
	document.getElementById(commaListName).value=seperatorString;
}

// Remove currently selected item from list
function taosRemoveSelectedFromList(leftListName, rightListName, commaListName, seperatorString)
{
	itemNo=document.getElementById(rightListName).selectedIndex;
	if (itemNo==-1) 
		return false;
	
	taosRemoveItemFromList(itemNo, leftListName, rightListName, commaListName, seperatorString);
}

// Remove specified item from list
function taosRemoveItemFromList(itemNo, leftListName, rightListName, commaListName, seperatorString)
{
	document.getElementById(rightListName).selectedIndex--;
	tempValue=document.getElementById(rightListName).options(itemNo).value;
	tempRegEx=new RegExp(seperatorString+tempValue+seperatorString);
	document.getElementById(commaListName).value=document.getElementById(commaListName).value.replace(tempRegEx, seperatorString);
	document.getElementById(rightListName).options.remove(itemNo);
	
	if (document.getElementById(commaListName).value=="")
		document.getElementById(commaListName).value=seperatorString;
	
	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; 
  	}
}
