// ------------------------------------------------------------------------------------------ //
// Acrescenta algumas propriedades aos controles:
// .Indice			: indica o indice na tela para o controle
// .IndiceAnterior	: indica o indice do controle anterior
// .IndicePosterior	: indica o indice para o controle posterior
// .Tam 			: tamanho maximo para digitacao
// .AutoSkip		: indica se pula para o proximo campo apos completar o tamanho do campo
// .Funcao			: funcao javascript a ser utilizada quando for pressionada a tecla enter (13)
// .Tipo			: indica o tipo de dado
//						'D' -> (Digitos) so digitos de 0(zero) a 9(nove)
//						'N' -> (Numeros) digitos de 0(zero) a 9(nove), e ","(virgula)
//					    'N-' -> (Numeros tambem negativos) digitos de 0(zero) a 9(nove), "-" e ","(virgula)
//						'F' -> (Ponto Flutuante) digitos de 0(zero) a 9(nove) e "."(ponto)
//						'C' -> (Caracteres) caracteres de 'a' ate 'z' e de 'A' ate 'Z' e vogais acentuadas (????????????????)
//						'LN'-> (Letras e Numeros) caracteres de 'a' ate 'z' e de 'A' ate 'Z' e digitos de 0(zero) a 9(nove)
//						'CD'-> (Caracteres e Digitos) caracteres de 'a' ate 'z' e de 'A' ate 'Z' e vogais acentuadas (????????????????)
//													   e digitos de 0(zero) a 9(nove)
//						'T' -> (Telefone) so digitos de 0(zero) a 9(nove), "-", "(" e ")"
//						'E' -> (Email) caracteres de 'a' ate 'z' e de 'A' at? 'Z', "@", ".",
//									   "-", "_" e digitos de 0(zero) a 9(nove)
//						'U' -> (URL) caracteres de 'a' ate 'z' e de 'A' ate 'Z',
//									 digitos de 0(zero) a 9(nove), "-", "_", "/", ".", ":" e "\"
//						'URI' -> (URI) caracteres de 'a' ate 'z' e de 'A' ate 'Z',
//									 digitos de 0(zero) a 9(nove), "-", "_", "/", ".", ":" e "\" e "@"
//						'L' -> (Logradouro) caracteres de 'a' ate 'z' e de 'A' ate 'Z',
//											digitos de 0(zero) a 9(nove), "/", "-"
//											"." e ","
//						'DT' -> (Data) so digitos de 0(zero) a 9(nove) e "/"
//
//						'S' -> (Senha) caracteres de 'a' a 'z' e de 'A' ate 'Z', digitos de 0(zero)
//									   a 9(nove), "!", "@", "#", "$", "%", "?", "&", "*", "(", ")",
//				       "_", "-", "+", "=", "}", "]", "[", "{", "^", "~", "/", "<",
//									   ">", ",", ".", ":", "|", "\"
//						'CC'-> (Conselho Classe) caracteres de 'a' ate 'z' e de 'A' ate 'Z' e vogais acentuadas (??????????????),
//												 digitos de 0(zero) a 9(nove) e "-","+","|","," e "."
//			            'DS'-> (Descricao) Caracreres, Numeros, '/', '-', '.', ',',
//			                    '&', '_', '(', ')', ':', '?', '+', '=', '[', ']',
//			                    '!', '#', '\' , '$', '*', '%'
//						'CS'-> (Codigo SAP) Caracreres, Numeros, '-', '.', '_'
//
//						outro -> qualquer caracter entre ascii 32 e ascii 127
// .Saltar			: (reservado) indica o momento de saltar de campo
// ------------------------------------------------------------------------------------------ //


// Criando uma variavel global fFormEvento que e setada ao inicializar a pagina
var fFormEvento;

//-----------------------------------------------------------------------------
//- Seta o foco para o primeiro campo de um determinado formulario
//
//- par?metro form O formulario
//
//-----------------------------------------------------------------------------
function setaFocusPrimeiroCampo(formulario) 
{
    for (i = 0; i < formulario.elements.length; i++) 
    {
		campo = formulario.elements[i];
		tipo = "" + campo.type;
		if(campo.disabled==false && campo.readOnly==false) 
		{
		    if ( (tipo == "text") ||
			 (tipo == "select-one") ||
			 (tipo == "radio") ||
			 (tipo == "password") ) 
			 {
				campo.focus();
				break;
		     }
		}
    }

}



function inicializaForm(formulario) {
    fFormEvento = formulario;
    setaFocusPrimeiroCampo(formulario);
}

function inicializaFormSemFoco(formulario) {
    fFormEvento = formulario;
}

// Carrega ?ndices para o pr?ximo controle e controle anterior
function InicializarIndices()
{
    if (document.CargaInicial==null)
    {
	document.CargaInicial=false;		// Seta para s? fazer uma vez por documento
	var ctrlAnterior=null;
	var IndAnt=0;
	
	for ( var i=0; i<fFormEvento.elements.length;i++)
	{
	    var e=fFormEvento.elements[i];
	    if ( e.type!="hidden")  //&& e.type!="image" )
	    {
		if ( ctrlAnterior != null )
		    ctrlAnterior.IndicePosterior=i;
		ctrlAnterior=e;
		e.Indice=i;
		e.IndiceAnterior=IndAnt;
	    }
	}
	//if ( ctrlAnterior!=null )
	//{
	//	ctrlAnterior.IndicePosterior=i-1;
	//}
    }
}

// Colocar o foco em determinado campo
function SetarFoco ( ind )
    {
    InicializarIndices();

    //if ( isNaN(ind) && fFormEvento.elements[ind].type!="hidden")
    if ( isNaN(ind) && fFormEvento.elements[ind].type!="hidden" && !fFormEvento.elements[ind].disabled)
	    fFormEvento.elements[ind].focus();
    else
	for (;ind<fFormEvento.elements.length;ind++)
	    if ( fFormEvento.elements[ind].type!="hidden" && !fFormEvento.elements[ind].disabled)
		break;
	if ( ind<=fFormEvento.elements.length ){
		fFormEvento.elements[ind].focus();
	}
    }

// Retirar o foco em determinado campo
function SetarBlur ( ind )
    {

    InicializarIndices();
    //if ( isNaN(ind) && fFormEvento.elements[ind].type!="hidden") 
    if ( isNaN(ind) && fFormEvento.elements[ind].type!="hidden" && !fFormEvento.elements[ind].disabled)
	    fFormEvento.elements[ind].blur();
    else
	for (;ind<fFormEvento.elements.length;ind++)
	    if ( fFormEvento.elements[ind].type!="hidden" && !fFormEvento.elements[ind].disabled)
		break;
	if ( ind<=fFormEvento.elements.length ){
		fFormEvento.elements[ind].blur();
	}
    }

// Limpar o conte?do do(s) campo(s)
function LimparCampo ( ind )
    // Para -1, limpa todos os elementos
    {
    if (isNaN(ind))			// Limpa pelo nome
	fFormEvento.elements[ind].value="";
    else if (ind != -1 )	// Limpa o elemento "ind" ( s? considera "text" e "password" )
	for ( var i=ind; i < fFormEvento.elements.length;i++ )
	    if ( fFormEvento.elements[i].type=="text" || fFormEvento.elements[i].type=="password")		// S? limpa campo "text"
		{
		fFormEvento.elements[i].value="";
		break;
		}
    else					// Limpa todos os elementos "text" e "password"
	for ( var i=0; i < fFormEvento.elements.length; i++ )
	    if ( fFormEvento.elements[i].type=="text" || fFormEvento.elements[i].type=="password" )
		fFormEvento.elements[i].value="";

    }

// Verificar qual navegador
function QualNavegador()
{
    var s = navigator.appName;
    if ( s == "Microsoft Internet Explorer" )
	return "IE";
    else if ( s == "Netscape" )
	return "NE";
    else
	return "";
}

// Verificar qual a vers?o do navegador
function QualVersao()
{
    var s = navigator.appVersion;
    if ( QualNavegador() == "IE" )
    {
	var i = s.search("MSIE");
	s=s.substring(i+5);
	i=s.search(".");
	return parseInt(s.substring(0,i+1));
    }
    else if ( QualNavegador() == "NE" )
	return parseInt(s.substring(0,1));
    else
	return 0;
}


//nao deixa o cara copiar ou colar
function noCtrl(inpt){
	if (window.event.ctrlKey){
		alert("Voce nao pode usar ctrl");
	}
}

function SetarEvento(ctrl, Tam, Tipo, AutoSkip, Funcao)
{
//document.body.addKeyDown(67, function() {if (window.event.ctrlKey) alert("CTRL + C");});
    // Filtra navegadores conhecidos

    var s = QualNavegador();

    if ( s.length==0 )
	return;

    if ( s=="IE" && QualVersao()>6 )
	return;

    //Config pra netscape antigos
    //if ( s=="NE" && QualVersao()>4 )
    //config para o firefox (Netscape 5)
    if ( s=="NE" && QualVersao()!=5 )
	return;

   if (ctrl.onkeypress==null)
    {
		if (AutoSkip==null){
	    	AutoSkip=true;
		}
		if (Tipo!=null)
	    	Tipo.toUpperCase();

		ctrl.Tam=Tam;
		ctrl.Tipo=Tipo;
		ctrl.AutoSkip=true;
		ctrl.Saltar=false;
		ctrl.Funcao=Funcao;

		InicializarIndices();

		ctrl.onkeypress=ValidarTecla;		

		
		if (QualNavegador()=="IE" && (QualVersao()==5) || (QualVersao()==6)) {
	    	ctrl.onkeyup=SaltarCampo;

	    }

	}
}



function TextToLowerCase(campo) {
	campo.value = campo.value.toLowerCase();
}

function TextToUpperCase(campo) {
	campo.value = campo.value.toUpperCase();
}

function SaltarCampo(ctrl)
{
    
    if (ctrl==null)
	ctrl=this;
    if ( ctrl.AutoSkip && ctrl.Saltar)
	if (ctrl.Saltar)
	{
	    ctrl.Saltar=false;
	    if ( ctrl.IndicePosterior != null )
		SetarFoco(ctrl.IndicePosterior);
	    else SetarBlur(ctrl.Indice);
	}
}



// Fazer o salto de campo
function ValidarTecla (evnt)
{

    var tk;
    var c;
    var ctrlPressed = ( (QualNavegador()=="IE") ? event.ctrlKey : evnt.ctrlKey);
/*
	if(this.value.length >= this.Tam){
		alert("Atingido o limite de " + this.Tam + " caracteres do campo!" );
		return false;
	}
*/
    // Recebe a tecla pressionada
    tk = ( (QualNavegador()=="IE") ? event.keyCode : evnt.which);
    c=String.fromCharCode(tk);
    c=c.toUpperCase();

    // -- Este trecho faz com que o <Enter> tenha a fun??o de <Tab>, mas acho invi?vel, pois n?o ? poss?vel
    //	     colocar o foco em campos do Tipo "image", e, neste caso, nunca seria poss?vel fazer a submiss?o
    //	     do formul?rio
    // if ( tk == 13 )
    // {
    //	this.Saltar=true;
    //	SaltarCampo(this);
    //	return false;
    // }
//alert(evnt.ctrlKey);
// if (evnt.ctrlKey) alert("ok");

    if (tk == 13) {
    	if (this.Funcao != null) {
    		eval(this.Funcao);
    		return true;
    	}
    }


	//permite as teclas "backspace", "setas" e "delete"
	if ((tk == 8) || (tk == 0)) return true;

	
    // S? aceita teclas alfanum?ricas. N?o aceita teclas de controle
    //if ( tk < 32 )
	//return true;
//	if ( tk > 127 )
//		return false;
   //faz aceitar control + v no Mozilla Firefox
   if(tk==118 && ctrlPressed)
      return true;	

    if (this.Tipo == "D") {
    	if ( c<"0" || c>"9" )
//		if (( c<"0" || c>"9" ) && (c!="V"))
		    return false;
    }
	
	
    if (this.Tipo == "N") {
	  if ( (c<"0" || c>"9") && (c!=",") )
	    return false;
	  if ( (c==",") && ((this.value.search(",")>-1) || (this.value.length==0)) )
	    return false;
	  if ( (c==".") && (this.value.length==0) )
	    return false;
    }


    if (this.Tipo == "N-") {
	  if ( (c<"0" || c>"9") && (c!=",") && (c!="-"))
	    return false;
	 if ( (c=="-") && ((this.value.search("-")>-1)) )
	    return false;
	  if ( (c==",") && ((this.value.search(",")>-1) || (this.value.length==0)) )
	    return false;
	  if ( (c==".") && (this.value.length==0) )
	    return false;
	
	  if (c=="-") {
	    this.value = c + this.value;
	    return false;
	  }
    }
    
    if (this.Tipo == "F") {
	if ( (c<"0" || c>"9") && c!="," )
	    return false;
	if ( (c==",") && (this.value.length==0) )
	    return false;
    }

    if (this.Tipo == "C") {
	if ( (c<"A" || c>"Z") && (c!=" ") &&  (c!="?") &&  (c!="?") &&	(c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&	(c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") &&  (c!="?") && (c!="?"))
	    return false;
    }

    if (this.Tipo == "CD") {
	if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!=" ") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&	(c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?")  && (c!="?"))
	    return false;
    }

    if (this.Tipo == "T") {
	if ( (c<"0" || c>"9") && (c!="-" && c!="(" && c!=")") )
	    return false;
    }

    if (this.Tipo == "DT") {
	if ( (c<"0" || c>"9") && (c!="/") )
	    return false;
    }
    if (this.Tipo == "HR") {
	if ( (c<"0" || c>"9") && (c!=":") )
	    return false;
    }

    if (this.Tipo == "E") {
	  if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!="@" && c!="." && c!="-" && c!="_"))
	    return false;
    }

    if (this.Tipo == "LN") {
	if ( (c<"A" || c>"Z") && (c<"0" || c>"9") )
	    return false;
    }

    if (this.Tipo == "U") {
	if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!="-" && c!="_" && c!=":" && c!="." && c!="/" && c!="\\") )
	    return false;
    }
    
    if (this.Tipo == "URI") {
	if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!="-" && c!="_" && c!=":" && c!="." && c!="/" && c!="\\" && c!="@") )
	    return false;
    }
    if (this.Tipo == "L") {
    //if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!="/" && c!="-" && c!="." && c!="," && c!=" ") && (c!=" ") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&	(c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") &&	(c!="?") &&  (c!="-") &&  (c!="+") &&  (c!="|") &&  (c!=",") &&  (c!="."))

    if ( (c!="0") && (c!="1") && (c!="2") && (c!="3") && (c!="4") && (c!="5") && (c!="6") && (c!="7") && (c!="8") && (c!="9") && 
         (c!="A") && (c!="B") && (c!="C") && (c!="D") && (c!="E") && (c!="F") && (c!="G") && (c!="H") && (c!="I") && 
         (c!="J") && (c!="K") && (c!="L") && (c!="M") && (c!="N") && (c!="O") && (c!="P") && (c!="Q") && (c!="R") && 
         (c!="S") && (c!="T") && (c!="U") && (c!="V") && (c!="W") && (c!="X") && (c!="Y") && (c!="Z") && (c!="?") &&
         (c!="/" && c!="-" && c!="." && c!="," && c!=" ") && (c!=" ") && (c!="?") && (c!="?") && (c!="?") && 
         (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="-") &&  
         (c!="+") &&  (c!="|") &&  (c!=",") &&  (c!=".") && (c!="?") && (c!="?") && (c!="?") )

	//if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!="/" && c!="-" && c!="." && c!="," && c!=" "))
	    return false;
    }

    if (this.Tipo == "S") {
	if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!="!" && c!="@" && c!="#" && c!="$" && c!="%" && c!="?" && c!="&" && c!="*" && c!="(" && c!=")" && c!="_" && c!="-" && c!="+" && c!="=" && c!="}" && c!="]" && c!="[" && c!="{" && c!="^" && c!="~" && c!="/" && c!="<" && c!="?" && c!=">" && c!="," && c!="." && c!=":" && c!="|" && c!="\\"))
	    return false;
    }

    if (this.Tipo == "CC") {
	if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!=" ") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&	(c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?") &&	(c!="?") &&  (c!="-") &&  (c!="+") &&  (c!="|") &&  (c!=",") &&  (c!="."))
	    return false;
    }

    if (this.Tipo == "DS") {
	if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!=" ") &&  (c!="?") && (c!="?")
	     &&  (c!="?") &&  (c!="?") &&  (c!="?") &&	(c!="?") && (c!="?") && (c!="?")
	     && (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?") &&  (c!="?")
	     &&  (c!="?") &&  (c!="?") &&  (c!="?") && (c!="?") && (c!="?")
	     && (c!="?") && (c!="?") && (c!="?") && (c!="?") && (c!="?")
	     && (c!="?") && (c != "/") && (c!="-") && (c != ".") && (c != ",")
	     && (c!="&") && (c != "_") && (c!="(") && (c != ")") && (c != ":")
	     && (c!="?") && (c != "+") && (c!="=") && (c != "[") && (c != "]")
	     && (c!="!") && (c != "#") && (c != "\\") && (c != ";")
		 && (c != "$") && (c != "*") && (c != "%")
		 )
	    return false;
    }

	if (this.Tipo == "CS") {
	  if ( (c<"A" || c>"Z") && (c<"0" || c>"9") && (c!="-")
	      && (c != ".") && (c != "_")
		 )
	    return false;
    }

    this.Saltar=(this.value.length==this.Tam-1);
    if ( ((QualNavegador()=="IE") && QualVersao()<5) || (QualNavegador()!="IE") )
	SaltarCampo(this);


    return true;
}

function ExecutarTeclaEnter (funcao,evnt)
{
    var tk;

    // Recebe a tecla pressionada
    tk = ( (QualNavegador()=="IE") ? event.keyCode : evnt.which);

	
	
    // -- Este trecho faz com que o <Enter> chame a fun??o passada
    if ( tk == 13 ) {
		eval(funcao);
    	return true;
    }
}
