  function verificaEnter(strProximoComando){
    if(event.keyCode == 13){
      eval(strProximoComando);
    }
  }

  function formatCNPJ(textField){
    if(trim(textField.value) == "") return;
    if(isNaN(textField.value)) return;
    if(textField.value.length < 14) return;

    valor = textField.value;

    textField.value = valor.substring(0, 2)  + "." 
                    + valor.substring(2, 5)  + "."
                    + valor.substring(5, 8)  + "/"
                    + valor.substring(8, 12) + "-"
                    + valor.substring(12, 14);
  }

  function unformatCNPJ(textField){
    valor = textField.value;

    valor = removeChar(valor, ".");
    valor = removeChar(valor, "/");
    valor = removeChar(valor, "-");

    textField.value = valor;
    textField.select();
  }

  function formatCPFCNPJ(textField){
    if(trim(textField.value) == "") return;
    if(isNaN(textField.value)) return;
    if( textField.value.length != 11
     && textField.value.length != 14) return;

    valor = textField.value;


    if(textField.value.length == 11){
      textField.value = valor.substring(0, 3) + "."
                      + valor.substring(3, 6) + "."
                      + valor.substring(6, 9) + "-"
                      + valor.substring(9, 11);
    }else{
      textField.value = valor.substring(0, 2)  + "." 
                      + valor.substring(2, 5)  + "."
                      + valor.substring(5, 8)  + "/"
                      + valor.substring(8, 12) + "-"
                      + valor.substring(12, 14);
    }
  }

  function unformatCPFCNPJ(textField){
    valor = textField.value;

    valor = removeChar(valor, ".");
    valor = removeChar(valor, "/");
    valor = removeChar(valor, "-");

    textField.value = valor;
    textField.select();
  }

  function formatCEP(textField){
    if(trim(textField.value) == "") return;
    if(isNaN(textField.value)) return;
    if(textField.value.length < 8) return;
    
    valor = textField.value;
    
    textField.value = valor.substring(0, 5) + "-"
                    + valor.substring(5, 8);
  
  }
  
  function unformatCEP(textField){
    textField.value = removeChar(textField.value, "-");
    textField.select();
  }
  
  function formatValue(textField){
    if(trim(textField.value) == "") return false;
    if(isNaN(textField.value)) return false;
  
    valor = parseFloat(textField.value).toString();
    if(valor.length < 3){
      valor = repeatChar("0", 3 - valor.length) + valor;
    }
    
    textField.value = valor.substring(0, valor.length - 2) + ","
                    + valor.substring(valor.length - 2, valor.length);
                    
    return true;
  }
  
  function unformatValue(textField){
    if(textField.value == "") return;
  
    textField.value = parseFloat(removeChar(textField.value, ","));
    textField.select();
  }
  
  function twoDecimals(textField){
    if(textField.value == "") return "0";
    arrValor = textField.value.split(".");
    if(arrValor.length == 1){
      strReturn = textField.value + "00";
    }else{       //Já tem decimal
      if(arrValor[1].length == 1){
        strReturn = replaceChar(textField.value, ".", "") + "0";
      }else{
        strReturn = arrValor[0] + arrValor[1].substr(0,2);
      }
    }

    textField.value = strReturn;
  }
  
  
  function onlyNumbers(e){
	var teclaValor;
	if(e.which){
		teclaValor = e.which;
	}else{
		teclaValor = event.keyCode;
	}
	if( teclaValor < 48 || (teclaValor > 57 && teclaValor < 96) || teclaValor > 105){ //Nao Numericos
		if(teclaValor != 8  && teclaValor != 46 && teclaValor != 37 && teclaValor != 39 &&
		   teclaValor != 16 && teclaValor != 9  && teclaValor != 35 && teclaValor != 36){ //Teclas Especiais
			return false;
		}
	}
  }

  function onlyNumbersAndComma(e){
	var teclaValor;
	if(e.which){
		teclaValor = e.which;
	}else{
		teclaValor = event.keycode;
	}
	if((teclaValor < 48 || (teclaValor > 57 && teclaValor < 96) || teclaValor > 105) &&
	   (teclaValor != 188 && teclaValor != 110)){ //Nao Numericos e não virgula
	   	if(teclaValor != 8  && teclaValor != 46 && teclaValor != 37 && teclaValor != 39 &&
	   	   teclaValor != 16 && teclaValor != 9  && teclaValor != 35 && teclaValor != 36){ //Teclas Especiais
	   	   return false;
	   	}
	}  
  }


  function checkedValue(objOption){
    if(objOption.length == null){ //Não array
      return ((objOption.checked)?0:-1);
    }
  
    for(i=0;i<objOption.length;i++){
      if(objOption[i].checked){
        return i;
      }
    }
    
    return -1;
  }
  
  function openWindow(theURL, winName, width, height){
    var winl = (screen.width - width) / 2;
    var wint = (screen.height - height) / 2;

    features = "scrollbars=yes,width=" + width + ",height=" + height + ",top=" + wint + ",left=" + winl;
    return window.open(theURL, winName, features);
  }

  function openScrollWindow(theURL, winName, width, height){
    var winl = (screen.width - width) / 2;
    var wint = (screen.height - height) / 2;

    features = "scrollbars=yes,width=" + width + ",height=" + height + ",top=" + wint + ",left=" + winl;
    return window.open(theURL, winName, features);
  }

  function ordenarRegistroForm(lineAtual){ 
  		//ordernar uma seguencia de linhas de registros em um formulario
  		//recebe como parametro a linha sendo alterada
		//os campos devem ser do tipo 'text' e possuir o 'id' no formato 'ord_xx',
        	//onde 'xx' e' a sequencia original do campo, comecando em 1.
		var lineNow;		
		var oldValue = 0;
		var newValue;
		var nowValue;
		var errorGeral = 0;
		var totalValue = 0; //para encontrar o valor faltante/alterado
		
		var vlrObjId = document.getElementById("ord_"+lineAtual);
		newValue = vlrObjId.value;
		
		if(isNaN(newValue)){
			errorGeral = 1;
		}else if(parseInt(newValue) <1 ){
			errorGeral = 1;
		}

		lastLine  = 1; //varre desde o primeiro objeto
		vlrObjId=document.getElementById("ord_"+lastLine);
		while(vlrObjId){
			nowValue = vlrObjId.value;
			if(lastLine != lineAtual){
				nowValue = parseInt(nowValue);
				totalValue += nowValue; 
			}
			oldValue += lastLine;  //soma ate chegar ao ultimo objeto para encontrar o valor que deveria ser
			lastLine++;
			vlrObjId=document.getElementById("ord_"+lastLine);
		}
		lastLine--;
		oldValue -= totalValue; //acha o antigo valor do objeto atual
		vlrObjId = document.getElementById("ord_"+lineAtual);
		if(errorGeral == 1){
			vlrObjId.value = oldValue;
			alert("O valor da Ordem deve ser numérico, positivo e maior que 0");
			return;
		}if(newValue > lastLine){  //valor fora do range
			vlrObjId.value = oldValue;
			alert("O valor informado e maior que a quantidade de registros. Último valor possível="+lastLine);
			return;
		}

		for(lineNow = 1; lineNow <= lastLine; lineNow++){
			if(lineNow == lineAtual){  //nao altera a linha atual (sendo alterada)
				continue;
			}
			vlrObjId = document.getElementById("ord_"+lineNow);
			nowValue = vlrObjId.value;
			nowValue = parseInt(nowValue);
			if(nowValue > oldValue){ //tudo que estava acima do antigo valor tem que ser decrementado
				nowValue--;
				vlrObjId.value  = nowValue;
			}
			if(nowValue >= newValue){ //tudo que estava acima do novo valor tem que ser incrementado
				vlrObjId.value  = nowValue + 1;
			}
		}
  }



  function elId(id){
  	//fornece o objeto conforme o id do mesmo
  	if (document.getElementById) {
  		return document.getElementById(id);
  	} else if (window[id]) {
  		return window[id];
  	}
  	return null;
  }

/**********************
   Funcoes Auxiliares
 **********************/
 
  function trim(str){
    posIni = 0;
    posFim = str.length;

    for(i=0;i<str.length;i++){
      if(str.charAt(i) != " "){
        posIni = i;
        break;
      }
    }

    if(i == str.length) return "";   //String vazia

    for(i=str.length-1;i>=0;i--){
      if(str.charAt(i) != " "){
        posFim = i + 1;
        break;
      }
    }

    return str.substring(posIni, posFim);
  }


  function removeChar(str, strOld){
    return replaceChar(str, strOld, "");
  }

  function replaceChar(str, strOld, strNew){

    newStr = "";

    for(i=0;i<str.length;i++){
      if(str.charAt(i) == strOld){
        newStr += strNew;
      }else{
        newStr += str.charAt(i);
      }
    }

    return newStr;
  }
  
  function repeatChar(charac, times){
    if(times <= 0) return "";
    
    newStr = "";
    
    for(i=0;i<times;i++){
      newStr += charac;   
    }
    
    return newStr;
  }
