// Seleciona os itens de acordo com os valores numa string
function List_SelectItensByValues (targetList, strValues)
{
	var aValues;
	
	aValues = strValues.split(";");
	
	for (i=0; i<targetList.length; ++i)
	{
		for (ct=0; ct<aValues.length; ++ct)
		{
			if (targetList.options[i].value == aValues[ct])
			{
				targetList.options[i].selected = true;
			}
		}
		return (true);
	}
}

// Adiciona um item em uma lista
function List_AddItem (targetList, lstValue, lstText)
{
	var newOption;

	newOption = new Option (lstText, lstValue);
	// option do fim é = a option
	targetList.options[targetList.length] = newOption;
	//targetList.add (newOption);

	//if (navigator.appName == "Microsoft Internet Explorer")
	//	return (true);

	//history.go(0);

//	return (true);
}

// Remove de uma lista um item pelo index
function List_RemoveItem (targetList, Index)
{
	//targetList.remove (Index);

	//if (navigator.family == "IE4")
	//{
		targetList.options[Index] = null;
	//}
	//else
	//{
	//	targetList.options[Index] = null;
	//	history.go(0);
	//}
	return (true);
}

// Tira um item de uma lista
function List_RemoveItemByValue (targetList, Value)
{
	var i;
		
	// Passa pelos itens até achar um com valor igual
	for (i=0; i<targetList.length; ++i)
	{
		if (targetList.options[i].value == Value)
		{
			List_RemoveItem (targetList, i);
			return (true);
		}
	}
	
	return (false);
}

// Retorna true ou falso caso encontre ou não um item na lista
function List_Exists (targetList, ItemValue)
{
	var i;

	for (i=0; i<targetList.length; ++i)
	{
		if (targetList.options[i].value == ItemValue)
			return (true);
	}
	
	return (false);
}

// Seleciona um item pelo valor parcial
function List_SelectItemByParcialValue (targetList, strValue)
{
	var i;
	
	for (i=0; i<targetList.length; ++i)
	{
		if (targetList.options[i].value.substring (0, strValue.length) == strValue)
		{
			targetList.selectedIndex = i;
			return (true);
		}
	}
	
	// Não achou nenhum ítem com este valor parcial
	targetList.selectedIndex = -1;
	return (false);
}

// Seleciona um item pelo valor
function List_SelectItemByValue (targetList, strValue)
{
	var i;
	
	for (i=0; i<targetList.length; ++i)
	{
		if (targetList.options[i].value == strValue)
		{
			targetList.selectedIndex = i;
			return (true);
		}
	}
	
	// Não achou nenhum ítem com este valor parcial
	targetList.selectedIndex = -1;
	return (false);
}

// Conta o número de ítens que tem um certo valor parcial
// Deve ser melhorada com regular expression para poder procurar uma string em qualquer posição, sem ter que
// começar da posição 0
function List_CountItemsByParcialValue (targetList, strValue)
{
	var i;
	var ct;
	
	for (i=ct=0; i<targetList.length; ++i)
	{
		if (targetList.options[i].value.substring (0, strValue.length) == strValue)
			++ct;
	}
	
	// Retorna o contador
	return (ct);
}

// Retorna o valor de um item selecionado
function List_GetSelectedValue (targetList)
{
//	var i;
	
//	for (i=0; i<targetList.length; ++i)
//	{
//		if (targetList.options[i].selected)
//		{
			// Retorna o nome do item que achou
		if (targetList.type =="hidden")
		{
			return (targetList.value);
		}
		else
		{
			if (targetList.selectedIndex != -1)
			{
				return (targetList.options[targetList.selectedIndex].value);
			}
			else
			{
				// Se não tem nada selecionado, se tiver pelomenos um item, retorna o primeiro valor
				if (targetList.length > 0)
				{
					return (targetList.options[0].value);
				}
				else
				{
					return ('');
				}
			}
		}	
}


function List_GetSelectedName (targetList)
{
	var i;

	if (targetList.type =="hidden")
	{
		return (targetList.value);
	}
	
	for (i=0; i<targetList.length; ++i)
	{
		if (targetList.options[i].selected)
		{
			// Retorna o nome do item que achou
			return (targetList.options[i].text);
		}
	}
	
	// Não achou
	return (false);
}

// Copia os itens selecionados de uma lista para outra
function List_CopyToFrom (fromList, toList)
{
	var i;
	
	for (i=0; i<fromList.length; ++i)
	{
		if (fromList.options[i].selected)
		{
			// Primeiro verifica se já está na lista
			if (List_Exists (toList, fromList.options[i].value) == false)
				List_AddItem (toList, fromList.options[i].value, fromList.options[i].text);
		}
	}
	
	return (true);
}

// Move os itens selecionados de uma lista para outra
function List_MoveToFrom (fromList, toList)
{
	var i;
	
	for (i=0; i<fromList.length; ++i)
	{
		if (fromList.options[i].selected)
		{
			// Primeiro verifica se já está na lista
			if (List_Exists (toList, fromList.options[i].value) == false)
			{
				List_AddItem (toList, fromList.options[i].value, fromList.options[i].text);
				List_RemoveItem (fromList, i);
				--i;
			}
		}
	}
	
	return (true);
}

// Remove de uma lista todos os itens selecionados
function List_RemoveSelectedItens (targetList)
{
	var i;
		
	// Monta um vetor com os índices selecionados
	for (i=0; i<targetList.length; ++i)
	{
		if (targetList.options[i].selected)
		{
			List_RemoveItem (targetList, i);
			--i;
		}
	}
	
	return (true);
}

// Remove todos os itens de uma lista
function List_RemoveAllItens (targetList)
{
	var i;
	
	for (i=0; i<targetList.length; ++i)
	{
		List_RemoveItem (targetList, i);
		--i;
	}
	
	return (true);
}

// Pega todos os valores selecionados de uma lista e concatena separado por ';'
function List_GetAllSelectedItens (targetList)
{
	var i;
	var Ret='';
	var fPrimeiro = true;
		
	for (i=0; i<targetList.length; ++i)
	{
		if (targetList.options[i].selected == true)
		{
			// Se for o último ítem da lista, não põe o ';'
			if (fPrimeiro)
			{
				Ret += targetList.options[i].value;
				fPrimeiro = false;
			}
			else
			{
				Ret += ';' + targetList.options[i].value;
			}
		}
	}
	return (Ret);
}

// Pega todos os valores dos itens de uma lista e concatena separado por ';'
function List_GetAllItens (targetList)
{
	var i;
	var Ret='';
	var fPrimeiro = true;
	
	for (i=0; i<targetList.length; ++i)
	{
		// Se for o último ítem da lista, não põe o ';'
		if (fPrimeiro)
		{
			Ret += targetList.options[i].value;
			fPrimeiro = false;
		}
		else
		{
			Ret += ';' + targetList.options[i].value;
		}
	}
	return (Ret);
}

// Seleciona todos os itens de uma listbox
function List_SelectAllItens (targetList)
{
	var i;
	
	for (i=0; i<targetList.length; i++)
	{
		// Seleciona todos os itens
		targetList.options[i].selected = true;
	}
	
	return (true);
}

// Preenche uma listview com itens de um "array" montado numa string
function List_InsertFromStringArray (targetList, strArray)
{
	// ;_; - separa itens de uma mesma posição do array, como variáveis de uma struct
	// -|- - separa cada posição do array
	
	var ArrayDeItens;
	var ArrayDeSubItens;
	var ct;
	
	if (strArray != '')
	{
		ArrayDeItens = strArray.split ("-|-");
	
		// Para cada posição do array, separa cada parte e monta na listview
		for (ct=0; ct<ArrayDeItens.length; ++ct)
		{
			ArrayDeSubItens = ArrayDeItens[ct].split (";_;");
			List_AddItem (targetList, ArrayDeSubItens[0], ArrayDeSubItens[1]);
		}
	}
	
	return (true);
}

// Retorna se tem ou não algum valor selecionado
function List_HasSelectedValue (targetList)
{
	if (targetList.selectedIndex != -1)
	{
		return (true);
	}
	else
	{
		return (false);
	}
}