﻿/***
* Descrição.: formata um campo do formulário de
* acordo com a máscara informada...
* Parâmetros: - objForm (o Objeto Form)
* - strField (string contendo o nome
* do textbox)
* - sMask (mascara que define o
* formato que o dado será apresentado,
* usando o algarismo "9" para
* definir números e o símbolo "!" para
* qualquer caracter...
* - evtKeyPress (evento)
* Uso.......: <input type="textbox"
* name="xxx".....
* onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);">
* Observação: As máscaras podem ser representadas como os exemplos abaixo:
* CEP -> 99.999-999
* CPF -> 999.999.999-99
* CNPJ -> 99.999.999/9999-99
* Data -> 99/99/9999
* Tel Resid -> (99) 999-9999
* Tel Cel -> (99) 9999-9999
* Processo -> 99.999999999/999-99
* C/C -> 999999-!
* E por aí vai...
***/

function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

if(window.event) { // Internet Explorer
nTecla = evtKeyPress.keyCode; }
else if(evtKeyPress.which) { // Netscape/Firefox/Opera
nTecla = evtKeyPress.which;
}

sValue = objForm[strField].value;

// Limpa todos os caracteres de formatação que
// já estiverem no campo.
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( " ", "" );
sValue = sValue.toString().replace( " ", "" );
fldLen = sValue.length;
mskLen = sMask.length;

i = 0;
nCount = 0;
sCod = "";
mskLen = fldLen;

while (i <= mskLen) {
bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

if (bolMask) {
sCod += sMask.charAt(i);
mskLen++; }
else {
sCod += sValue.charAt(nCount);
nCount++;
}

i++;
}

objForm[strField].value = sCod;

if (nTecla != 8) { // backspace
if (sMask.charAt(i-1) == "9") { // apenas números...
return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
else { // qualquer caracter...
return true;
} }
else {
return true;
}
}


/***
*
* Validação CNPJ
*
***/
function isNUMB(c)
 { if((cx=c.indexOf(","))!=-1) {c = c.substring(0,cx)+"."+c.substring(cx+1); }
 if((parseFloat(c) / c != 1)){ if(parseFloat(c) * c == 0){return(1);}else{return(0);}}else{return(1);}}

function LIMP(c)
 { while((cx=c.indexOf("-"))!=-1){c = c.substring(0,cx)+c.substring(cx+1);}
 while((cx=c.indexOf("/"))!=-1){c = c.substring(0,cx)+c.substring(cx+1);}
 while((cx=c.indexOf(","))!=-1)
  {c = c.substring(0,cx)+c.substring(cx+1);}
 while((cx=c.indexOf("."))!=-1)
  {c = c.substring(0,cx)+c.substring(cx+1);}
 while((cx=c.indexOf("("))!=-1)
  {c = c.substring(0,cx)+c.substring(cx+1);}
 while((cx=c.indexOf(")"))!=-1)
  {c = c.substring(0,cx)+c.substring(cx+1);}
 while((cx=c.indexOf(" "))!=-1)
  {c = c.substring(0,cx)+c.substring(cx+1);}
 return(c);
 }


function RealTestaCNPJ(CNPJ,g)
 {var VerCNPJ=0;var ind=2;var tam;
 for(f=g;f>0;f--){VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
  if(ind>8)
   {ind=2;}else{ind++;}}
  VerCNPJ%=11;
  if(VerCNPJ==0 || VerCNPJ==1){VerCNPJ=0;}else{VerCNPJ=11-VerCNPJ;}
  if(VerCNPJ!=parseInt(CNPJ.charAt(g))){return(0);}else{return(1);}}

function VerifyCNPJ(CNPJ)
 {CNPJ = LIMP(CNPJ);
 if(isNUMB(CNPJ) != 1)
  {return(0);}else{if(CNPJ == 0){return(0);}else{g=CNPJ.length-2;
  if(RealTestaCNPJ(CNPJ,g) == 1)
    {g=CNPJ.length-1;
    if(RealTestaCNPJ(CNPJ,g) == 1){return(1);}else{return(0);}}else{return(0);}}}}


/***
*
* Validação CPF
*
***/
	function validaCNPJ(CNPJ) {
		erro = new String;
		if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
	 	if (erro.length == 0) return false;
		 }
	 	//substituir os caracteres que nao sao numeros
		 if(document.layers && parseInt(navigator.appVersion) == 4){
	 	x = CNPJ.substring(0,2);
		 x += CNPJ.substring(3,6);
	 	x += CNPJ.substring(7,10);
		 x += CNPJ.substring(11,15);
	 	x += CNPJ.substring(16,18);
		 CNPJ = x;
	 	} else {
		 CNPJ = CNPJ.replace(".","");
	 	CNPJ = CNPJ.replace(".","");
		 CNPJ = CNPJ.replace("-","");
	 	CNPJ = CNPJ.replace("/","");
		 }
		 	var nonNumbers = /\D/;
	 	if (nonNumbers.test(CNPJ)) return false;
	 	var a = [];
		var b = new Number;
	 	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
		 for (i=0; i<12; i++){
	 	a[i] = CNPJ.charAt(i);
		 b += a[i] * c[i+1];
	 	}
		 if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	 	b = 0;
		 for (y=0; y<13; y++) {
	 	b += (a[y] * c[y]);
		 }
	 	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
		 if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
	 		return false;
		 }
	 	if (erro.length > 0){
		 alert(erro);
	 	return false;
		 } else {
		   return false;
		 }
		   return true;
	 }



 function validarCPF(cpf){
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
	 return false;
   }

   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");

   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
	 return false;
   }
   return true;
 }

 function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }
