
// I- Função para criação das máscaras de datas, CPFs, CNPJs e códigos dos campos de texto
// Ia- Campos só aceitam caracteres numéricos   
    function Mascaras(formato, objeto){ 
    if (window.event.keyCode >= 48 && window.event.keyCode <= 57){ 
    var campo = document.getElementById(objeto); 
// Ib- Campos que recebem datas
    if (formato == 'DATA'){ 
    separador = '/'; 
    conjunto1 = 2; 
    conjunto2 = 5; 
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto2) 
    campo.value = campo.value + separador; 
    } 
// Ic- Campos que recebem horários
    if (formato == 'HORA'){ 
    separador = ':'; 
    conjunto1 = 2; 
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador; 
    } 
// Id- Campos que recebem CEPs
    if (formato == 'CEP'){ 
    separador = '.'; 
    separador1 = '-'; 
    conjunto1 = 2;
    conjunto2 = 6;
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador;
    if (campo.value.length == conjunto2) 
    campo.value = campo.value + separador1;  
    } 
// Ie- Campos que recebem CPFs
    if (formato == 'CPF'){ 
    separador = '.'; 
    separador1 = '-'; 
    conjunto1 = 3; 
    conjunto2 = 7; 
    conjunto3 = 11; 
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto2) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto3) 
    campo.value = campo.value + separador1; 
    } 
// If- Campos que recebem CNPJs
    if (formato == 'CNPJ'){ 
    separador = '.'; 
    separador1 = '/';
    separador2 = '-';  
    conjunto1 = 2; 
    conjunto2 = 6; 
    conjunto3 = 10; 
    conjunto4 = 15;  
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto2) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto3) 
    campo.value = campo.value + separador1; 
    
    if (campo.value.length == conjunto4) 
    campo.value = campo.value + separador2; 
    }  
// Ig- Campos que recebem Códigos CETIP  
    if (formato == 'CETIP'){ 
    separador = '.'; 
    separador1 = '-'; 
    conjunto1 = 4; 
    conjunto2 = 6; 
    conjunto3 = 9; 
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto2) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto3) 
    campo.value = campo.value + separador1; 
    } 
// Ih- Campos que recebem Códigos SELIC  
    if (formato == 'SELIC'){ 
    separador = '.'; 
    separador1 = '-'; 
    conjunto1 = 4; 
    conjunto2 = 7; 
    conjunto3 = 10; 
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto2) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto3) 
    campo.value = campo.value + separador1; 
    }
// Ii- Campos que recebem números telefônicos
    if (formato == 'TEL'){ 
    separador = '('; 
    separador1 = ')'; 
    separador2 = ' '; 
    separador3 = '-'; 
    conjunto1 = 0; 
    conjunto2 = 3; 
    conjunto3 = 4; 
    conjunto4 = 9;
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador; 

    if (campo.value.length == conjunto2) 
    campo.value = campo.value + separador1; 

    if (campo.value.length == conjunto3) 
    campo.value = campo.value + separador2; 
    
    if (campo.value.length == conjunto4) 
    campo.value = campo.value + separador3; 
    }
// Ij- Campos que recebem Códigos MATRÍCULA  
    if (formato == 'MATRIC'){ 
    separador = '-'; 
    conjunto1 = 9; 
    if (campo.value.length == conjunto1) 
    campo.value = campo.value + separador; 
    }
    
    
    }else 
    window.event.keyCode = 0; 
    }
    
// II- Função para rollover de imagens    
    function MM_swapImgRestore() { //v3.0
      var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
    }
// III- Função para fazer o preload das imagens de rollover
    function MM_preloadImages() { //v3.0
      var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
        if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
    }
// IV- Função para trabalho com layers
    function MM_findObj(n, d) { //v4.01
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }
// V- Função auxiliar para fazer o rollover das imagens
    function MM_swapImage() { //v3.0
      var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
       if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
    }
// VI- Função para utilização de popup e janelas
    function MM_openBrWindow(theURL,winName,features) { //v2.0
      window.open(theURL,winName,features);
    }
// VII- Função para habilitação de mostrar e esconder layers
    function MM_shlVis() { //v6.0
      var i,p,v,obj,args=MM_shlVis.arguments;
      for (i=0; i<(args.length-2); i+=3) if
    ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
        if (obj.style) { obj=obj.style; (obj.visibility=='visible')?(obj.visibility='hidden'):(obj.visibility='visible')}
       }
    }
// VIIa- Mesma acima, sem o clique de apagar - botao só ida
	function MM_showHideLayers() { //v6.0
	  var i,p,v,obj,args=MM_showHideLayers.arguments;
	  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
		if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
		obj.visibility=v; }
	}
// VIII- Função específica para iFrame
    function IFRAME1_onclick() {

}
// IX- Função para escolha do estado no mapa em Flash
    function selecionaEstado(uf){
    document.getElementById('txtUfRes').value = uf;
}

// X- Função para caracteres de números para os campos de valores de moedas
    function isNotNumber(entrada){
    var retorno = true;
    var lixo = entrada.value;
    var iTemp = 0;

	for( k=0; k<lixo.length; k++) {
		if ( (lixo.charAt(k)!= "0") && (lixo.charAt(k)!= "1") && (lixo.charAt(k)!= "2") && (lixo.charAt(k)!= "3") && (lixo.charAt(k)!= "4") && (lixo.charAt(k)!= "5") && (lixo.charAt(k)!= "6") && (lixo.charAt(k)!= ",") && (lixo.charAt(k)!= ".") && (lixo.charAt(k)!= "7")&& (lixo.charAt(k)!= "8") && (lixo.charAt(k)!= "9")){
		      retorno = retorno && false; 
	    }
		iTemp++;
	}//for
	if (iTemp <= 0) { retorno = false };
	retorno = !retorno;
	return retorno;
}

//** INICIO IDENTIFICAÇÃO DO NAVEGADOR **
    var isNav4, isNav, isIE;
    if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
      isNav = (navigator.appName=="Netscape") ? true : false;
      isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
    }
    if (navigator.appName=="Netscape") {
	    isNav4 = (parseInt(navigator.appVersion.charAt(0))==4);
    }
//** FIM IDENTIFICAÇÃO DO NAVEGADOR **

// XI- Funções de formatação de valores
    function tirarZerosEsquerda(STR){

      var sAux='';
      var i=0;
      STR=new String(STR);

      while (i < STR.length ){
        if ((STR.charAt(i)!='.') && (STR.charAt(i)!=',')){
	      sAux += STR.charAt(i);
        }
	    i++
      }


      STR = new String(sAux);
      sAux = '';
      i=0;

      while (i < STR.length ){
        if (STR.charAt(i) != '0'){
          sAux = STR.substring(i,STR.length)
	      i = STR.length;
	    }
        i++;
      }

      return  sAux;
    }

//XII- Bloquear caracteres nao numericos - onKeyPress=onlynum()
function onlynum(e) {
	var keyNumber = (isIE) ? event.keyCode : e.which;
	if (((keyNumber<48)||(keyNumber>57)) && (keyNumber!=13) && (keyNumber!="0") && (keyNumber!=8)) {
		if (isIE) event.keyCode=0;
		return false;
	}
}

// XIII- Bloquear caracteres nao numericos e permitir mais casas decimeis- onKeyPress=onlynum()
function onlynumPlus(e) {
	var keyNumber = (isIE) ? event.keyCode : e.which;
	if (((keyNumber<48)||(keyNumber>57)) && (keyNumber!=13) && (keyNumber!="0") && (keyNumber!=8)) {
		if (isIE) event.keyCode=0;
		return false;
	}
}

function formatarvalor(STR){
  isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
  var decimal,inteiro;
  var i,count;
  STR = new String(STR);
  STR = tirarZerosEsquerda(STR);
  inteiro='';
  if (STR.length == 1){
	  inteiro  = '0';
	  decimal = '0' + STR;
	}
	else {
	  if (STR.length == 2){
		  inteiro  = '0';
		  decimal = STR;
		}
		else{
		  decimal = STR.substring(STR.length-2,STR.length);
		  i=3;
		  count=0;
		  while (i<=STR.length){
		 		if (count==3) {
				  inteiro = '.' + inteiro;
				  count = 0;
				}
		    inteiro = STR.charAt(STR.length-i) + inteiro;
				count++;
				i++;
		  }
		}
	}
  if (inteiro == '') {
    inteiro = '0';
  }
  if (decimal == '') {
    decimal = '00';
  }
  return inteiro+','+decimal;
}

function formatarOnKeyUp(OBJ){
  var decimal,inteiro;
  var i,count;
  STR = new String(OBJ.value);
  STR = tirarZerosEsquerda(STR);
  inteiro='';
  if (isIE) {
		if (STR.length == 1){
			  inteiro  = '0';
			  decimal = '0' + STR;
			}
			else {
			  if (STR.length == 2){
				  inteiro  = '0';
				  decimal = STR;
				}
				else{
				  decimal = STR.substring(STR.length-2,STR.length);
				  i=3;
				  count=0;
				  while (i<=STR.length){
			 		if (count==3) {
					  inteiro = '.' + inteiro;
					  count = 0;
					}
				    inteiro = STR.charAt(STR.length-i) + inteiro;
					count++;
					i++;
				  }
				}
			}


		if (inteiro == '') {
		  inteiro = '0';
		}

		if (decimal == '') {
		  decimal = '00';
		}
		OBJ.value = inteiro+','+decimal;
  }
  else
  {

	if (navigator.appName=="Netscape")
	{
		var versao = navigator.userAgent;
		versao=versao.toString();
		menorVersao = versao.search("/6.0");
		if (menorVersao>-1)
		{
			var isNS6="sim";
		}
	}

	if(isNS6=="sim")
		OBJ.style.textAlign	= "left";


	if(STR=="")
		OBJ.value = "0,00";
	if((STR.length==1 || STR.length==2 || STR.length==3) && OBJ.value.substr(0,1)=='0')
		OBJ.value = OBJ.value.substr(1,OBJ.value.length-1);
	if((STR.length==2) && OBJ.value.substr(0,1)!='0')
		OBJ.value = "0," + OBJ.value;
	if((STR.length==1) && OBJ.value.substr(0,1)!='0')
		OBJ.value = "0,0" + OBJ.value;


	if(isNS6=="sim")
		OBJ.style.textAlign	= "right";

  }

}
function formatarOnKeyDown(OBJ,tammax,teclapres){
  var decimal,inteiro;
  var i,count;
  STR = new String(OBJ.value);

  inteiro='';

  if (isIE) {
	if (OBJ.maxLength > STR.length){
	  STR = tirarZerosEsquerda(STR); //ESTA FUNCAO TIRA TAMBEM PONTO E VIRGULA

	  if ( ((event.keyCode > 47) && (event.keyCode < 59)) || ((event.keyCode > 95) && (event.keyCode < 106))   ){

			if (STR.length == 0){
			  inteiro  = '0';
			  decimal = '0' + STR;
			}
			else {
			  if (STR.length == 1){
			    inteiro  = '0';
			    decimal = STR;
			  }
			  else{
			    decimal = STR.substring(STR.length-1,STR.length);
			    i=2;
			    count=0;
			    while (i<=STR.length){
			 		if (count==3) {
			  	  inteiro = '.' + inteiro;
			  	  count = 0;
			  	}
			      inteiro = STR.charAt(STR.length-i) + inteiro;
			  	count++;
			  	i++;
			    }
			  }
			}

	  }
	  else{
	    if (event.keyCode == 8){

	      if (STR.length == 0){
			  inteiro  = '0';
			  decimal = '00';
			}
			else {
			  if (STR.length == 1){
			    inteiro  = '0';
			    decimal = '00' + STR;
			  }
			  else {
			    if (STR.length == 2){
		          inteiro  = '0';
			      decimal = '0' + STR;
			     }
		         else{
	 			   decimal = STR.substring(STR.length-3,STR.length);
				   i=4;
				   count=0;
				   while (i<=STR.length){
			 		 if (count==3) {
					   inteiro = '.' + inteiro;
					   count = 0;
					  }
				      inteiro = STR.charAt(STR.length-i) + inteiro;
					  count++;
					  i++;
				    }
			     }
			  }
	      }
	    }
	    else {

	      if (STR.length == 1){
		 	  inteiro  = '0';
		      decimal = '0' + STR;
			}
			else {
			  if (STR.length == 2){
				  inteiro  = '0';
				  decimal = STR;
				}
				else{
				  decimal = STR.substring(STR.length-2,STR.length);
				  i=3;
				  count=0;
				  while (i<=STR.length){
			 		if (count==3) {
					  inteiro = '.' + inteiro;
					  count = 0;
					}
				    inteiro = STR.charAt(STR.length-i) + inteiro;
					count++;
					i++;
				  }
				}
			}
	    }
	  }

	  if (inteiro == '') {
	    inteiro = '0';
	  }

	  if (decimal == '') {
	    decimal = '00';
	  }
	  OBJ.value = inteiro+','+decimal;
	}
  }
  else
  {
	var tecla = teclapres.keyCode;
	valorSemFormato = OBJ.value;
	valorSemFormato = valorSemFormato.replace( ",", "" );
	valorSemFormato = valorSemFormato.replace( ".", "" );
	valorSemFormato = valorSemFormato.replace( ".", "" );
	valorSemFormato = valorSemFormato.replace( ".", "" );
	valorSemFormato = valorSemFormato.replace( ".", "" );
	tamanho = valorSemFormato.length;

	if (tecla == 46) {
	     OBJ.value = OBJ.value;

	}if (tamanho < tammax && tecla != 8) {
	    tamanho = valorSemFormato.length + 1;

	}if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
	     if (tecla == 8) {
	         tamanho = tamanho - 1;
	     }

	     if (tamanho <= 2) {
	 		 OBJ.value = valorSemFormato;

	 	 }else if ((tamanho > 2) && (tamanho <= 5)) {
	 		 OBJ.value = valorSemFormato.substr(0, tamanho - 2) + ',' +
	 		               valorSemFormato.substr(tamanho - 2, tamanho);

	 	 }else if ((tamanho >= 6) && (tamanho <= 8)) {
	 	 	 OBJ.value = valorSemFormato.substr(0, tamanho - 5) + '.' +
	 	 	               valorSemFormato.substr(tamanho - 5, 3) + ',' +
	 		               valorSemFormato.substr(tamanho - 2, tamanho);

	 	 }else if ((tamanho >= 9) && (tamanho <= 11)) {
	 		 OBJ.value = valorSemFormato.substr(0, tamanho - 8) + '.' +
	 		               valorSemFormato.substr(tamanho - 8, 3) + '.' +
	 		               valorSemFormato.substr(tamanho - 5, 3) + ',' +
	 		               valorSemFormato.substr(tamanho - 2, tamanho);

	 	 }else if ((tamanho >= 12) && (tamanho <= 14)) {
	 	 	 OBJ.value = valorSemFormato.substr(0, tamanho - 11) + '.' +
	 		               valorSemFormato.substr(tamanho - 11, 3) + '.' +
	 		               valorSemFormato.substr(tamanho - 8, 3)  + '.' +
	 		               valorSemFormato.substr(tamanho - 5, 3)  + ',' +
	 		               valorSemFormato.substr(tamanho - 2, tamanho);

	 	 }else if ((tamanho >= 15) && (tamanho <= 17)) {
	 	 	 OBJ.value = valorSemFormato.substr(0, tamanho - 14) + '.' +
	 		               valorSemFormato.substr(tamanho - 14, 3) + '.' +
	 		               valorSemFormato.substr(tamanho - 11, 3) + '.' +
	 		               valorSemFormato.substr(tamanho - 8, 3)  + '.' +
	 		               valorSemFormato.substr(tamanho - 5, 3)  + ',' +
	 		               valorSemFormato.substr(tamanho - 2, tamanho);
	    }
	}
  }
}

function refreshCampo(obj){
     isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
	if (isIE) {
		obj.value = obj.value;
	}
}
