//==============================================================================
// LIST RELATED FUNCTIONS
//==============================================================================

//=======================
// Purpose : Moves a item from one list to the other
// Parameters :
//				oSource - Source list
//				oDestination - Destination list
//=======================
function GetItem(oSource,oDestination){
	var sourceList, destinationList, exists;
	sourceList = oSource;
	destinationList = oDestination;

    if(sourceList.options.length > 0){
		if (sourceList.options.selectedIndex != -1){
			for (i=0;i<destinationList.options.length;i++)
				if (destinationList.options[i].value == sourceList.options[sourceList.options.selectedIndex].value) exists = true;

			if (!exists){
				for (i=0;i<sourceList.options.length;i++){
					if (sourceList.options[i].selected){
			  			destinationList.options[destinationList.options.length] = new Option(sourceList.options[i].text,sourceList.options[i].value);
					}
				}
				for (i=sourceList.options.length-1;i>=0;i--){
					if (sourceList.options[i].selected){
						sourceList.options[i] = null;
					}
				}
				SortD(oDestination);
			}
			else {
				alert('Este valor já foi adicionado à lista.');
			}
		}
	}
}

//=======================
// Purpose : Moves a item from one list to the other and updates the repository
// Parameters :
//				oLeft - Left Waste list
//				oRight - Right Check list
//				iDir - Direction to move the item (0 - Left->Right; 1 - Right->Left)
//				oRepository - Repository to update with check list
//=======================
function MoveItem(oLeft,oRight,iDir,oRepository) {
	if (iDir == 0) {
		GetItem(oLeft,oRight);
	} else {
		GetItem(oRight,oLeft);
	}

	reWriteIDs(oRight,oRepository);
}

//=======================
// Purpose : Removes a item from the list
// Parameters :
//				oSource - Source list
//				oRepository - Repository to update with check list
//=======================
function DelItem(oSource, oRepository){
	var sourceList
	sourceList = oSource;

	if(sourceList.options.length > 0){
		if (sourceList.options.selectedIndex != -1){
			for (i = sourceList.options.length-1; i >= 0; i--){
				if (sourceList.options[i].selected) sourceList.options[i] = null;
			}
		}
	}
	reWriteIDs(sourceList, oRepository);
}

//=======================
// Purpose : Verifies if a Option allready exists in the list
// Parameters :
//				oOption - Source Selected Option
//				oRepository - Repository Object
//=======================
function CheckIfExists(oOption, oRepository) {
	//Verifica se a opção já foi adicionada à lista
	for (i = 0; i < oRepository.options.length; i++) {
		if (oRepository.options[i].value == oOption) {
			alert('Este valor já foi adicionado à lista.');
			return true;
		}
	}
	return false;
}

//=======================
// Purpose : Moves a item Up in the list
// Parameters :
//				oSource - Source list
//				oRepository - Repository to update with check list
//=======================
function MoveUp(oSource, oRepository){
	var sourceList;
	var tempOption;

	sourceList = oSource;

	if (sourceList.options.selectedIndex != 0) {
		tempOption = new Option(sourceList.options[sourceList.options.selectedIndex].text,sourceList.options[sourceList.options.selectedIndex].value);
		sourceList.options[sourceList.options.selectedIndex].value = sourceList.options[sourceList.options.selectedIndex-1].value;
		sourceList.options[sourceList.options.selectedIndex].text = sourceList.options[sourceList.options.selectedIndex-1].text;

		sourceList.options[sourceList.options.selectedIndex-1].value = tempOption.value;
		sourceList.options[sourceList.options.selectedIndex-1].text = tempOption.text;
		sourceList.options.selectedIndex = sourceList.options.selectedIndex - 1;
	}
	reWriteIDs(sourceList, oRepository);
}

//=======================
// Purpose : Moves a item Down in the list
// Parameters :
//				oSource - Source list
//				oRepository - Repository to update with check list
//=======================
function MoveDown(oSource,oRepository){
	var sourceList;
	var tempOption;

	sourceList = oSource;

	if (sourceList.options.selectedIndex != sourceList.options.length-1) {
		tempOption = new Option(sourceList.options[sourceList.options.selectedIndex].text,sourceList.options[sourceList.options.selectedIndex].value);
		sourceList.options[sourceList.options.selectedIndex].value = sourceList.options[sourceList.options.selectedIndex+1].value;
		sourceList.options[sourceList.options.selectedIndex].text = sourceList.options[sourceList.options.selectedIndex+1].text;

		sourceList.options[sourceList.options.selectedIndex+1].value = tempOption.value;
		sourceList.options[sourceList.options.selectedIndex+1].text = tempOption.text;
		sourceList.options.selectedIndex = sourceList.options.selectedIndex + 1;
	}
	reWriteIDs(sourceList, oRepository);
}

//=======================
// Purpose : ReWrites IDs in the repository
// Parameters :
//				oSource - Source list
//				oRepository - Repository to update with check list
//=======================
function reWriteIDs(oSource,oRepository) {
	var sourceList;
	sourceList = oSource;

	//Clear the repository
	oRepository.value = '';

	//Re-Write IDs
	if(sourceList.options.length > 0){
		for (i=0;i<sourceList.options.length;i++) {
			oRepository.value = oRepository.value + sourceList.options[i].value;
			if (i < sourceList.options.length-1) oRepository.value = oRepository.value + '|';
		}
	}
}

function SortD(box){
	var temp_opts = new Array();
	var temp = new Object();

	for(var i=0; i<box.options.length; i++){
		temp_opts[i] = box.options[i];
	}

	for(var x=0; x<temp_opts.length-1; x++){
		for(var y=(x+1); y<temp_opts.length; y++){
			if(temp_opts[x].text > temp_opts[y].text){
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
			}
		}
	}

	for(var i=0; i<box.options.length; i++){
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
	}
}

function addOption(campo, valor, texto){
	var obj = eval(campo);
	var n = obj.options.length;

	obj.options[n] = new Option();
	obj.options[n].value = valor;
	obj.options[n].text = texto;
}

function clearOptions(campo) {
	var obj = eval(campo);
	obj.length = 0;
}