/**
 * @author João Vitor Paes de Barros do Carmo
 */

function moveUpList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("Não existem valores que podem ser movidos!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         //alert("You must select an entry to be moved!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            //alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               //alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
			   var moveId1 = listField[selected-1].id;
			   var moveId2 = listField[selected].id;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
			   listField[selected].id = moveId1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
			   listField[selected-1].id = moveId2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function moveDownList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("Não existem valores que podem ser movidos!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         //alert("You must select an entry to be moved!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            //alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == listField.length-1 ) {
               //alert("The last entry in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected+1].text;
               var moveText2 = listField[selected].text;
			   var moveId1 = listField[selected+1].id;
			   var moveId2 = listField[selected].id;
			   var moveName1 = listField[selected+1].name;
			   var moveName2 = listField[selected].name;
               var moveValue1 = listField[selected+1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
   			   listField[selected].id = moveId1;
               listField[selected+1].text = moveText2;
               listField[selected+1].value = moveValue2;
   			   listField[selected+1].id = moveId2;
               listField.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function dropdown_cima(objetoSelecao)
{
	try{
		if (objetoSelecao[objetoSelecao.selectedIndex - 1]!=null)
		{
			//objetoSelecao = new Select();
			opcaoAtual = objetoSelecao[objetoSelecao.selectedIndex];
	
			opcaoCima = objetoSelecao[objetoSelecao.selectedIndex - 1];
	
			valorAntigo = opcaoAtual.value;
			opcaoAtual.value = opcaoCima.value;
			opcaoCima.value = valorAntigo;
		
			moveUpList(objetoSelecao);
			//alert(objetoSelecao.innerHTML);
		}
	}catch(err){
		
	}
	
}

function dropdown_baixo(objetoSelecao)
{
	try{
		if (objetoSelecao[objetoSelecao.selectedIndex + 1]!=null)
		{
			//objetoSelecao = new Select();
			opcaoAtual = objetoSelecao[objetoSelecao.selectedIndex];
		
			opcaoBaixo = objetoSelecao[objetoSelecao.selectedIndex + 1];
		
			valorAntigo = opcaoAtual.value;
			opcaoAtual.value = opcaoBaixo.value;
			opcaoBaixo.value = valorAntigo;
		
			moveDownList(objetoSelecao);
			//alert(objetoSelecao.innerHTML);
		}
	}catch(err){
		
	}
	
}

function setOrdem(){
	objetoSelecao = document.getElementById('selecao');
	objetoSelecao.disabled = false;
	no = objetoSelecao;
	continua = true;
	while(continua)
	{
		no = no.parentNode;
		if (no.tagName == 'FORM')
		{
			form = objetoSelecao.parentNode;
			continua = false;
		}
	}

	opcaoAtual = objetoSelecao[objetoSelecao.selectedIndex];
	
	val = new Array();
	for (var i = 0; i < objetoSelecao.length; i++) {
		inputHidden = document.createElement("input");
		inputHidden.value = objetoSelecao[i].value;
		inputHidden.type = "hidden";
		inputHidden.id = "ordem[" + objetoSelecao[i].id + "]";
		inputHidden.name = "ordem[" + objetoSelecao[i].id + "]";
		val[i] = inputHidden;
		form.appendChild(inputHidden);
	}
}

function copiaNome(origem,destino)
{
	destino.innerHTML = "-> "+origem.value+" <-";
}

function isset(varname)
{
	return(typeof(window[varname])!='undefined');
}


function getFlashObj(movie){
   if (window.document[movie]) {
      return window.document[movie];
   }
   if (navigator.appName.indexOf("Microsoft Internet")==-1) {
      if (document.embeds && document.embeds[movie]) {
         return document.embeds[movie]; 
      }
   } else {
      return document.getElementById(movie);
   }
}


function getDimensions(movie) {
   var movieObj = getFlashObj(movie);
   var width = movieObj.TGetProperty("/", 8);
   var height= movieObj.TGetProperty("/", 9);
   alert("The current dimensions for the Flash Object ("+movie+") are "+width+" in width and "+height+" in height.");
}