function trim(sString){
	while ((sString.substring(0,1) == ' ') || (sString.substring(0,1) == '\r') || (sString.substring(0,1) == '\t') || (sString.substring(0,1) == '\n') || (sString.substring(0,1) == '\f')){
		sString = sString.substring(1, sString.length);
	}
	while ((sString.substring(sString.length-1, sString.length) == ' ') || (sString.substring(sString.length-1, sString.length) == '\r') || (sString.substring(sString.length-1, sString.length) == '\t') || (sString.substring(sString.length-1, sString.length) == '\n') || (sString.substring(sString.length-1, sString.length) == '\f')){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function checkNum(evt){
	// Next line is for cross-browser functionality
	var eTarget = evt.target || evt.srcElement;
	if (isNaN(eTarget.value)){
		alert("Este campo deve ser numérico!");
		eTarget.value = eTarget.value.substr(0,eTarget.value.length-1);
		eTarget.focus();
	}
}

function moveItem(fromList, toList, fromArr, toArr, moveAll){
	var auxFromList = document.getElementById(fromList);
	var auxToList = document.getElementById(toList);
	var arrRmv = Array();
	for (var f=0;f<auxFromList.options.length;f++){
		if ((auxFromList.options[f].selected == true) || (moveAll)){
			// If destination list is empty, simply adds the element, else inserts it at the 
			// correct position in the sort order
			oOpt = document.createElement("OPTION");
			insertPos = -1;
			if (auxToList.options.length == 0) {
				// Creates new element
				auxToList.options.add(oOpt);
			}else{
				// Finds out the correct position on the list to insert the item
				for (var k=0;k<auxToList.options.length;k++){
					if (auxToList.options[k].innerHTML > auxFromList.options[f].innerHTML){
						auxToList.insertBefore(oOpt, auxToList.options[k]);
						insertPos = k;
						break;
					}
				}
				// if no element greater than this was found, inserts at the bottom of the list
				if (insertPos == -1){
					auxToList.options.add(oOpt);
				}
			}	
			//(ATTENTION! The two attributes MUST be set after the add method, or it will fail)
			oOpt.innerHTML = auxFromList.options[f].innerHTML;
			oOpt.value = auxFromList.options[f].value;
			// Marks the item for removal
			arrRmv.push(auxFromList.options[f].innerHTML);
			// Adds the item to the associate array
			if (insertPos == -1){
				toArr.push(fromArr[f]);
			}else{
				toArr.splice(insertPos,0,fromArr[f]);
			}
		}	
	}
	// Remove moved items
	for (var i=0;i<arrRmv.length;i++){
		for (var g=0;g<auxFromList.options.length;g++){
			if (auxFromList.options[g].innerHTML == arrRmv[i]){
				auxFromList.removeChild(auxFromList.options[g]);
				// Removes it from the associate array
				fromArr.splice(g,1);
			}
		}		
	}		
}

function addToList(auxFromList, auxToList, newIndex, newElement){
	// Checks the two lists to see if the item is a duplicate
	isDuplicate = false;
	for (var k=0;k<auxFromList.options.length;k++){
		if ((auxFromList.options[k].innerHTML == newElement) || (auxFromList.options[k].value == newIndex)){
			isDuplicate = true;
		}
	}
	// ... and find out the correct position on the list to insert the item
	insertPos = -1;
	for (var k=0;k<auxToList.options.length;k++){
		if ((auxToList.options[k].innerHTML == newElement) || (auxToList.options[k].value == newIndex)){
			isDuplicate = true;
		}else if (auxToList.options[k].innerHTML > newElement){
			insertPos = k;
		}
	}
	if (!isDuplicate){
		// If list is empty or if there is no item greater than the new one, 
		// simply adds the element, else inserts it at the correct position in the sort order
		oOpt = document.createElement("OPTION");
		if ((auxToList.options.length == 0) || (insertPos == -1)){
			// Creates new element
			auxToList.options.add(oOpt);
		}else{
			auxToList.insertBefore(oOpt, auxToList.options[insertPos]);
		}	
		//(ATTENTION! The two attributes MUST be set after the add method, or it will fail)
		oOpt.innerHTML = newElement;
		oOpt.value = newIndex;
		return true;
	}else{
		alert("O item que você está tentando adicionar é duplicado, por favor verifique e tente novamente!");
		return false;
	}
}

function toggleBranch(objList){
	// Checks the browser type
	var isIE = (window.navigator.appName == "Microsoft Internet Explorer");
	var myId = objList.id;
	var idSize = myId.length;
	var listItems = document.getElementById("tblTree").rows; 
	for (i=0;i<listItems.length;i++){
		if ((listItems[i].cells[0].id != null) && (listItems[i].cells[0].id != undefined)){
			if ((listItems[i].cells[0].id != myId) && (listItems[i].cells[0].id.substr(0,idSize) == myId)){
				if ((listItems[i].cells[0].style.display == '') || (listItems[i].cells[0].style.display == 'none')){
					if (listItems[i].cells[0].id.length == (idSize + 1)){
						if (isIE){
							listItems[i].cells[0].style.display = 'block';
						}else{	
							listItems[i].cells[0].style.display = 'table-cell';
						}
					}
				}else{
					listItems[i].cells[0].style.display = 'none';
				}
			}
		}
	}
}
