// format valores no formulario cpf


function FormataCPF(pForm,pCampo,pTamMax,pPos1,pPos2,pPosTraco,pTeclaPres)
{
	var wTecla, wVr, wTam;	
	var nerros=true;
	
    wTecla = pTeclaPres.keyCode;
	wVr = pForm[pCampo].value;
	wVr = wVr.toString().replace( "-", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( "/", "" );
	wTam = wVr.length ;
	
	if (wTam < pTamMax && wTecla != 8) 
	{ 
	   wTam = wVr.length + 1 ; 
	}

	if (wTecla == 8 ) 
	{ 
	   wTam = wTam - 1 ; 
	}
			
	if ( wTecla == 8 || wTecla == 88 || (wTecla >= 48 && wTecla <= 57) || (wTecla >= 96 && wTecla <= 105) )
	{
		if ( wTam <= 2 )
		{
	 		pForm[pCampo].value = wVr ;
		}
		if (wTam > pPosTraco && wTam <= pTamMax) 
		{
	       wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
		}
		if ( wTam == pTamMax)
		{
		      wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3) + '.' + wVr.substr(wTam - pPos2, wTam);
		}
		pForm[pCampo].value = wVr;
		pForm[pCampo].value = toInteger(pForm[pCampo].value);
		nerros=true;
	}
	else
	{
		
		pForm[pCampo].value = toInteger(pForm[pCampo].value);
		nerros = false;
		
		
	}

	if (wTecla == 9) nerros = true;
	return nerros;
}

// format valores no formulario

function FormataValorDisplay(pForm, pCampo, pTamMax, pVirgula) 
{
	wVr = pForm[pCampo].value;
	wVr = wVr.toString().replace( "/", "" );
	wVr = wVr.toString().replace( "/", "" );
	wVr = wVr.toString().replace( ",", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( ".", "" );
	wTam = wVr.length;

      if (pVirgula == 1) {
		if ( wTam <= 2 ){ 
	 		pForm[pCampo].value = wVr ; }
	 	if ( (wTam > 2) && (wTam <= 5) ){
		   pForm[pCampo].value = wVr.substr( 0, wTam - 2 ) + ',' + wVr.substr( wTam - 2, wTam ) ; }
	 	if ( (wTam >= 6) && (wTam <= 8) ){
    	   pForm[pCampo].value = wVr.substr( 0, wTam - 5 ) + '.' + wVr.substr( wTam - 5, 3 ) + ',' + wVr.substr( wTam - 2, wTam ) ; }
	 	if ( (wTam >= 9) && (wTam <= 11) ) {
	 		pForm[pCampo].value = wVr.substr( 0, wTam - 8 ) + '.' + wVr.substr( wTam - 8, 3 ) + '.' + wVr.substr( wTam - 5, 3 ) + ',' + wVr.substr( wTam - 2, wTam ) ; }
	 	if ( (wTam >= 12) && (wTam <= 14) ){
	 		pForm[pCampo].value = wVr.substr( 0, wTam - 11 ) + '.' + wVr.substr( wTam - 11, 3 ) + '.' + wVr.substr( wTam - 8, 3 ) + '.' + wVr.substr( wTam - 5, 3 ) + ',' + wVr.substr( wTam - 2, wTam ) ; }
	 	if ( (wTam >= 15) && (wTam <= 17) ){
	 		pForm[pCampo].value = wVr.substr( 0, wTam - 14 ) + '.' + wVr.substr( wTam - 14, 3 ) + '.' + wVr.substr( wTam - 11, 3 ) + '.' + wVr.substr( wTam - 8, 3 ) + '.' + wVr.substr( wTam - 5, 3 ) + ',' + wVr.substr( wTam - 2, wTam ) ;}
     } else { 
		if ( wTam <= 3 ){ 
	 		pForm[pCampo].value = wVr ; }
	 	if ( (wTam > 3) && (wTam <= 6) ){
		   pForm[pCampo].value = wVr.substr( 0, wTam - 3 ) + '.' + wVr.substr( wTam - 3, wTam ) ; }
	 	if ( (wTam >= 7) && (wTam <= 9) ) {
    	   pForm[pCampo].value = wVr.substr( 0, wTam - 6 ) + '.' + wVr.substr( wTam - 6, 3 ) + '.' + wVr.substr( wTam - 3, wTam ) ; }
	 	if ( (wTam >= 10) && (wTam <= 12) ) {
	 		pForm[pCampo].value = wVr.substr( 0, wTam - 9 ) + '.' + wVr.substr( wTam - 9, 3 ) + '.' + wVr.substr( wTam - 6, 3 ) + '.' + wVr.substr( wTam - 3, wTam ) ; }
	 	if ( (wTam >= 13) && (wTam <= 15) ){
	 		pForm[pCampo].value = wVr.substr( 0, wTam - 12 ) + '.' + wVr.substr( wTam - 12, 3 ) + '.' + wVr.substr( wTam - 9, 3 ) + '.' + wVr.substr( wTam - 6, 3 ) + '.' + wVr.substr( wTam - 3, wTam ) ; }
	 	if ( (wTam >= 16) && (wTam <= 18) ){
	 		pForm[pCampo].value = wVr.substr( 0, wTam - 15 ) + '.' + wVr.substr( wTam - 15, 3 ) + '.' + wVr.substr( wTam - 12, 3 ) + '.' + wVr.substr( wTam - 9, 3 ) + '.' + wVr.substr( wTam - 6, 3 ) + '.' + wVr.substr( wTam - 3, wTam ) ;}
	 }
}

//verifica cpf
function VCPF(pForm, pCampo)
 {
	var wVr, wVrCPF, wVrSEQ, wTam, wSoma, wSoma2, i, j, wDig1, wDig2,aux,
	    wVETOR_CC = new Array(11),
	    wVETOR_PESO = new Array(11);
 	var Erro = false;
	wVrCPF = pForm[pCampo].value;
	aux = pForm[pCampo].value;
	wVr = aux;
  
	//jrrrr   wVr= aux.toString().substring(0,3) + aux.toString().substring(4,7) + aux.toString().substring(8,11) + aux.toString().substring(12,14) ;     

//	wVr = wVr.toString().replace(".","");
//	wVr = wVr.toString().replace(".","");
//	wVr = wVr.toString().replace(".","");
//	wVr = wVr.toString().replace("-","");
	wTam = wVr.length + 1;

    if (wTam < 11) {
	  //alert("Nº de dígitos do CPF menor que o normal. Redigite !!!");
	  pForm[pCampo].value = "";
	  pForm[pCampo].focus();
	  return false;
	}
    
	for (i = 0; i < wVr.length; i++) {
	   if (isNaN(parseInt(wVr.charAt(i))) ) {
  	     //alert("O CPF contém dígitos inválidos. Corrija-o !!!");
	     pForm[pCampo].value = "";
	     pForm[pCampo].focus();
	     return false;
	   }
	}
	
	//comentado abaixo por claudio para testes
	//if (wVr == '11111111111' || wVr == '22222222222' || wVr == '33333333333' || wVr == '44444444444' || wVr == '55555555555' || 
	//    wVr == '66666666666' || wVr == '77777777777' || wVr == '88888888888' || wVr == '99999999999') {
  	//     alert("CPF Inválido. Redigite-o !!!");
	//     pForm[pCampo].value = "";
	//     pForm[pCampo].focus();
	//     return false;
    //	}
    
    wSoma = 0;
	wSoma2 = 0;
    j = 2;
    for (i = 0; i < 11; i++) {
	   wVETOR_CC[i] = wVr.charAt(i);
	   wVETOR_PESO[i] = j;
	   j++; 
	} 
    i = 0;	  
    while (i < 9)  {
	   i++;
	   if (i < 10) {
          wSoma += wVETOR_CC[9 - i] * wVETOR_PESO[i - 1]; }
	   wSoma2 += wVETOR_CC[10 - i] * wVETOR_PESO[i - 1];
	}
	wDig1 = (wSoma * 10) % 11;
	wDig2 = (wSoma2 * 10) % 11;
	if (wDig1 == 10) { 
	    wDig1 = 0;
	}	
	if (wDig2 == 10) { 
	    wDig2 = 0;
	}		
    if (parseInt(wVr.charAt(9)) != wDig1 || parseInt(wVr.charAt(10)) != wDig2) {
  	     //alert("CPF com Dígito Verificador inválido. Redigite-o !!!");
//	     pForm[pCampo].value = "";
	     pForm[pCampo].focus();
	     return false;
    }	
    pForm[pCampo].value = wVrCPF;
//    pForm[pCampo + 1].value = wVrSEQ;

	return true;  // VerificaChaveAcesso(pForm, 1, 2, 3);
}



function FormataCGC(pForm,pCampo,pTeclaPres){
	var wTecla, wVr, wTam;	

	wTecla = pTeclaPres.keyCode;

	wVr = pForm[pCampo].value;
	wVr = wVr.toString().replace( "-", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( "/", "" );
	wTam = wVr.length ;

	if (wTam < 14 && wTecla != 8) { 
	   wTam = wVr.length + 1 ; 
	}

	if (wTecla == 8 ) { 
	   wTam = wTam - 1 ; 
	}

	if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
		if ( wTam <= 2 ){
	 		pForm[pCampo].value = wVr ;
		}
		if (wTam > 2 && wTam <= 14) {
		   wVr = wVr.substr(0, wTam - 2) + '-' + wVr.substr(wTam - 2, wTam);
		}
		if ( wTam == 14){
	       wVr = wVr.substr( 0, wTam - 12 ) + '.' + wVr.substr(wTam - 12, 3) + '.' + wVr.substr(wTam - 9, 3) + "/" + wVr.substr(wTam - 6, wTam);
		}
		pForm[pCampo].value = wVr;
	
	}

}


function VCGC(pForm, pCampo)
{ 
//= pForm[pCampo].value.toString().substring(0,15)
// pForm[pCampo].value.toString().substring(16, 2);
	var Numero; 
	var Digito; 
	var CNPJ ,aux = pForm[pCampo].value

	//Numero = Numero.replace(".","");
	//Numero = Numero.replace(".","");
	//Numero = Numero.replace("/","");
	//Numero = Numero.replace("-","");

	//Numero = aux.toString().substring(0,2) + aux.toString().substring(3,6) + aux.toString().substring(7,10) + aux.toString().substring(11,15);
	//CNPJ= aux.toString().substring(0,2) + aux.toString().substring(3,6) + aux.toString().substring(7,10) + aux.toString().substring(11,15) + aux.toString().substring(16,18);
	//Digito=aux.toString().substring(16,18);

	//troquei por este DWK
	Numero = aux.toString().substring(0,12);
	//alert(Numero);
	CNPJ = aux;
	//alert(CNPJ);
	Digito = aux.toString().substring(12,14);
	//alert(Digito);

//	CNPJ = CNPJ.replace(".","");
//	CNPJ = CNPJ.replace(".","");
//	CNPJ = CNPJ.replace("/","");
//	CNPJ = CNPJ.replace("-","");

	var CGC = Numero;
	var peso1 = '543298765432';
	var peso2 = '654329876543';
	var soma1 = 0;
	var soma2 = 0;
	var digito1 = 0;
	var digito2 = 0;
          
	 if (CNPJ == '00000000000000'){
	 	//alert("CNPJ Inválido. Redigite!");
		//pForm[pCampo].value = "";
	  	pForm[pCampo].focus();
	  	return false;
	 } 
	 
	 if ((Numero.length + Digito.length + 1 > 1) && (Numero.length + Digito.length + 1 < 15)) 
	 {
	  //alert("Nº de dígitos do CNPJ menor que o normal. Redigite !!!");
	  //pForm[pCampo].value = "";
	  pForm[pCampo].focus();
	  return false;
	 }	
 
 	if (Numero.length + Digito.length + 1 > 1){
 		 
		 for (i = 1; i < 12 - Numero.length + 1; i++) 
		 {
		  CGC = eval("'" + 0 + CGC + "'")
		 }
		    
		 for (i = 1; i < CGC.length+1; i++) 
		 {
		  soma1 += CGC.substring(i, i-1) * peso1.substring(i, i-1);
		 } 
		
		 soma1 %= 11;
		
		 if (soma1  < 2) 
		 {
		  digito1 = 0;
		 }
		 else 
		 {
		  digito1 = 11 - soma1; 
		 }
		
		 for (i = 1; i < CGC.length+1; i++) 
		 {
		  soma2 += CGC.substring(i, i-1) * peso2.substring(i, i-1);
		 } 
		 
		 soma2 += digito1 * 2 
		 soma2 %= 11;
		
		 if (soma2  < 2) 
		 {
		  digito2 = 0;
		 }
		 else 
		 {
		  digito2 = 11 - soma2; 
		 }
		
		 if (eval("'" + digito1 + digito2 + "'") != Digito)
		 {
		  //alert("CNPJ inválido. Redigite !!!");
//		  pForm[pCampo].value = "";
//		  pForm[pCampo].focus();
		  return false;
		 }
		 else 
		 {
		  return true;
		 }
	}
}

// Formata Data
function FormataData(pForm, pCampo,pTeclaPres) {
	var wTecla = pTeclaPres.keyCode;
	wVr = pForm[pCampo].value;
	wVr = wVr.replace( ".", "" );
	wVr = wVr.replace( "/", "" );
	wVr = wVr.replace( "/", "" );
	wTam = wVr.length + 1;

	if ( wTecla != 9 && wTecla != 8 ){
		if ( wTam > 2 && wTam < 5 )
			pForm[pCampo].value = wVr.substr( 0, wTam - 2  ) + '/' + wVr.substr( wTam - 2, wTam );
		if ( wTam >= 5 && wTam <= 10 )
			pForm[pCampo].value = wVr.substr( 0, 2 ) + '/' + wVr.substr( 2, 2 ) + '/' + wVr.substr( 4, 4 ); 
	}			
}


//Formatar Telefone
function FormataTEL(pForm, pCampo, pTeclaPres) {
	var wTecla = pTeclaPres.keyCode;
	var wTam;
	wVr = pForm[pCampo].value;
	wVr = wVr.replace( ".", "" );
//	wVr = wVr.replace( "(", "" );
//	wVr = wVr.replace( ")", "" );
	wVr = wVr.replace( "-", "" );
	wVr = wVr.replace( "/", "" );
	wTam = wVr.length + 1;

	if ( wTecla != 9 && wTecla != 8 ){
		if ( wTam > 0 && wTam < 2 ) 
			pForm[pCampo].value = '(' ;
		if ( wTam > 3 && wTam < 5 ) 
			pForm[pCampo].value = wVr.substr( 0, 8 ) + ')' + wVr.substr( 8, 4 );
		if ( wTam > 7 && wTam < 9 ) 
			pForm[pCampo].value = wVr.substr( 0, 8 ) + '-' + wVr.substr( 8, 4 );
		if ( wTam > 11)
			pForm[pCampo].value = wVr.substr( 0, 8 ) + '-' + wVr.substr( 8, 4 ); 
	}			
}

// 
function CampoUpcase (pForm, pCampo, pOperacao) {
   if (pOperacao == "+") {
     pForm[pCampo].value = pForm[pCampo].value.toUpperCase();
   } else {
     pForm[pCampo].value = pForm[pCampo].value.toLowerCase();
   }
}

//
function FormataCEP(pForm, pCampo, pTeclaPres) {
	var wTecla = pTeclaPres.keyCode;
	var wTam;
	wVr = pForm[pCampo].value;
	wVr = wVr.replace( ".", "" );
	wVr = wVr.replace( "-", "" );
	wVr = wVr.replace( "/", "" );
	wTam = wVr.length + 1;

	if ( wTecla != 9 && wTecla != 8 ){
		if ( wTam > 2 && wTam < 6 )
			pForm[pCampo].value = wVr.substr( 0, wTam - 2  ) + '.' + wVr.substr( wTam - 2, wTam );
		if ( wTam >= 6 && wTam <= 9 )
			pForm[pCampo].value = wVr.substr( 0, 2 ) + '.' + wVr.substr( 2, 3 ) + '-' + wVr.substr( 5, 3 ); 
	}			
}


//email
function VerificaEmail(pcampo)  { 

	  pcampo.value = pcampo.value.toLowerCase();					
      	  parametro = pcampo.value;								
	  teste_parametro = "false"; 
	  tamanho_parametro = parametro.length;
	  aposicao = 0; //posicao @
	  pposicao = 0; //poiscao ponto
	  narrobas = 0; // numero de @
	  for (i = 0; i < tamanho_parametro; i++) { /*verifica se existe espaco em branco */
		   if (parametro.charAt(i) == "@") {
		  	     narrobas = narrobas + 1
				 aposicao = i;
		   }
		   if (parametro.charAt(i) == ".") {
		  	     
				 pposicao = i;
		   }
		   
	   }
	   if (aposicao > 0 && aposicao+1 < pposicao && narrobas<2){
     	   teste_parametro = "true"; 
	   }	   
      	  
	  for (i = 0; i < tamanho_parametro; i++) { /*verifica se existe espaco em branco */
		   if (parametro.charAt(i) == " ") {
		  	     teste_parametro = "false"; 
		   }
	   }
		
	   if (tamanho_parametro < 5) {
	   	  teste_parametro = "false"; /*tamanho minimo*/
	   }
	  
			
	   if (parametro.charAt(0) == " " || parametro.charAt(1) == " ") { /*Primeiros chars em branco */
			  teste_parametro = "false"; 
	   }
				
		
		if (teste_parametro == "false" && tamanho_parametro != 0 ) { /*Existe algum problema no preenchiento do nome */
			alert("E-mail inválido!");
			pcampo.focus();
			return false;
		} else {
			return true;
		}
}



function LTrim(str)
/***
        PURPOSE: Remove leading blanks from our string.
        IN: str - the string we want to LTrim

        RETVAL: An LTrimmed string!
***/
{
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(0)) != -1) {
            // We have a string with leading blank(s)...

            var j=0, i = s.length;

            // Iterate from the far left of string until we
            // don't have any more whitespace...
            while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                j++;


            // Get the substring from the first non-whitespace
            // character to the end of the string...
            s = s.substring(j, i);
        }

        return s;
}


//RTrim(string) : Returns a copy of a string without trailing spaces.
//==================================================================
function RTrim(str)
/***
        PURPOSE: Remove trailing blanks from our string.
        IN: str - the string we want to RTrim

        RETVAL: An RTrimmed string!
***/
{
        // We don't want to trip JUST spaces, but also tabs,
        // line feeds, etc.  Add anything else you want to
        // "trim" here in Whitespace
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            // We have a string with trailing blank(s)...

            var i = s.length - 1;       // Get length of string

            // Iterate from the far right of string until we
            // don't have any more whitespace...
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                i--;


            // Get the substring from the front of the string to
            // where the last non-whitespace character is...
            s = s.substring(0, i+1);
        }

        return s;
}




//Trim(string) : Returns a copy of a string without leading or
//               trailing spaces
//=============================================================
function Trim(str)
/***
        PURPOSE: Remove trailing and leading blanks from our string.
        IN: str - the string we want to Trim

        RETVAL: A Trimmed string!
***/
{
        return RTrim(LTrim(str));
}


function toInteger(checkString)
{
 var newString = "";    
 var count = 0;         
    for (i = 0; i < checkString.length; i++) {
        ch = checkString.substring(i, i+1);
        if (ch >= "0" && ch <= "9") 
        {
            newString += ch;
        }
        else
        {
         if (ch == "-") newString += ch;
         if (ch == ".") newString += ch;
         if (ch == "/") newString += ch;
        }
    }
    
    return newString; //checkString;
}


//function so_numero(pForm,pCampo,pTeclaPres)
//{
// var wTecla 
//  var nerro = true;
//    wTecla =  pTeclaPres.keyCode;
//	if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105)
//	{
//		pForm[pCampo].value = toInteger(pForm[pCampo].value);
//		nerro = true;
//	}
//	else
//	{
//		pForm[pCampo].value = toInteger(pForm[pCampo].value);
//		nerro = false;
//	}
// return nerro;	
//}


var keyCodigo = 0;

function soNumeroPress(evento)
{

	if (keyCodigo == 0)
	{	
		keyCodigo = evento.keyCode;	
	}
	
	if ((keyCodigo == 8 || keyCodigo == 13 || keyCodigo == 9 || keyCodigo == 71 || keyCodigo == 46 || keyCodigo  == 37  || keyCodigo  == 39) || (keyCodigo >= 48 && keyCodigo <= 57) || (keyCodigo >= 96 && keyCodigo <= 105)) 
	{
		VerifiqueTAB=true; 
		return true;
	} 
	else
	{
		return false;
	}
}


function soNumeroDown(evento){


	keyCodigo = evento.keyCode;

	if ((keyCodigo == 8 || keyCodigo == 13 || keyCodigo == 9 || keyCodigo == 46) || (keyCodigo >= 48 && keyCodigo <= 57) || (keyCodigo >= 96 && keyCodigo <= 105)){
		VerifiqueTAB=true; 

		return true;

	} else {

		return false;

	}

}




//function Mostra(quem, tammax) {
//   if (quem.value.length == tammax) { 
//     var i=0,j=0, indice=-1;
//     for (i=0; i<document.forms.length; i++) { 
//       for (j=0; j<document.forms[i].elements.length; j++) { 
//          if (document.forms[i].elements[j].name == quem.name) { 
//            indice=i;
//            break;
//          } 
//       } 
//       if (indice != -1) break; 
//     } 
//     for (i=0; i<=document.forms[indice].elements.length; i++) { 
//       if (document.forms[indice].elements[i].name == quem.name) { 
//          while ( (document.forms[indice].elements[(i+1)].type == "hidden") &&
//                  (i < document.forms[indice].elements.length) ) { 
//             i++;
//          } 
//          document.forms[indice].elements[(i+1)].focus();
//          break;
//       } 
//    } 
//   } 
//} 


VerifiqueTAB=true;
function Mostra(quem, tammax) {
	if ( (quem.value.length == tammax) && (VerifiqueTAB) ) {
		var i=0,j=0, indice=-1;
		for (i=0; i<document.forms.length; i++) {
			for (j=0; j<document.forms[i].elements.length; j++) {
				if (document.forms[i].elements[j].name == quem.name) {
					indice=i;
					break;
				}
			}
			if (indice != -1)
		         break;
		}
		for (i=0; i<=document.forms[indice].elements.length; i++) {
			if (document.forms[indice].elements[i].name == quem.name) {
				while ( (document.forms[indice].elements[(i+1)].type == "hidden") &&
						(i < document.forms[indice].elements.length) ) {
							i++;
				}
				document.forms[indice].elements[(i+1)].focus();
				VerifiqueTAB=false;
				break;
			}
		}
	}
}

//<!-- -->

function PararTAB() 
{ 
   VerifiqueTAB=false; 
} 

//<!-- -->

function ChecarTAB(evento) 
{ 

   VerifiqueTAB=true; 
   		
   keyCodigo = evento.keyCode;

   if ((keyCodigo == 8 || keyCodigo == 13 || keyCodigo == 9 || keyCodigo == 71) || (keyCodigo >= 48 && keyCodigo <= 57) || (keyCodigo >= 96 && keyCodigo <= 105))
   {
	return true;
    } else 
	
    {
	return false;
	}

} 
