function _validaCPF(cpfFormatado){ //valida cpf
    
    if ( Procura_Str(__limpa(cpfFormatado)) > 0 )     
		return (0);      
    
    var c = cpfFormatado.substring(0,9);
    var dv = cpfFormatado.substring(9,11);
    var d1 = 0;
    for (i = 0; i < 9; i++)
       d1 += c.charAt(i)*(10-i);

    if (d1 == 0)
        return (0);

    d1 = 11 - (d1 % 11);

    if (d1 > 9) d1 = 0;

    if (dv.charAt(0) != d1)
        return (0);

    d1 *= 2;

    for (i = 0; i < 9; i++)
        d1 += c.charAt(i)*(11-i);

    d1 = 11 - (d1 % 11);

    if (d1 > 9) d1 = 0;

    if (dv.charAt(1) != d1)
       return (0);

    return (1);
	
}


function __validaCGC(CNPJ){ //valida cnpj

    var g = CNPJ.length-2;
    if(CNPJ == 0 )
        return (0);
    if(__RealTestaCNPJ(CNPJ,g) == 1){
        g=CNPJ.length-1;
        if(__RealTestaCNPJ(CNPJ,g) == 1)
            return (1);
        else
            return (0);

    }else
        return (0);

}

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 textCounter(field, maxlimit){
	if (field.value.length > maxlimit) {
 		field.value = field.value.substring(0, maxlimit);
 		alert("Você passou o limite de " + maxlimit + " carateres.");
 	} 
}



var validacoes={
  'simples':/[^\w\s\xC0-\xFF]/g,
  'carro':/[^\w\s\xC0-\xFF\.]/g,
  'texto':/[^\w\s\xC0-\xFF\,\.\?\!\:\-\(\)\$\%\@]/g,
  'mail':/[^\w\\@\.\-]/g,
  'numeros':/[^\d]/g,
  'alfa':/[^\w\.]/g,
  'contrato':/[^\d\w\/]/g,
  'identidade': /[^\d\.\-\/]/g,
  'fone':/[^\d\(\)\-\s\_]/g
}

function bvf(campo,tipo){
	var re = new RegExp(validacoes[tipo]);
	if (campo.value.match (re))
		campo.value = campo.value.replace(re,'');		  
	if (Trim(campo.value) == '')
		campo.value = '';	 
}

function Trim(str){
	return str.replace(/^\s+|\s+$/g,'');
}

function Procura_Str(param) {
	invalidos = '00000000000,11111111111,22222222222,33333333333,44444444444,55555555555,66666666666,77777777777,88888888888,99999999999,00000000191,19100000000';
	return invalidos.indexOf(param);
}


function __formataCNPJ(texto){
	if (__validaCGC(texto)!=1){
		texto = '';
        } else{
	        if(texto.charAt(2) != ".")
			texto = texto.substring(0,2)+"."+texto.substring(2,15);
	
		if(texto.charAt(6) != ".")
			texto = texto.substring(0,6)+"."+texto.substring(6,16);
	
		if(texto.charAt(10) != "/")
			texto = texto.substring(0,10)+"/"+texto.substring(10,17);
	
		if(texto.charAt(15) != "-")
			texto = texto.substring(0,15)+"-"+texto.substring(15,18);
	}
	return (texto);
} 

function __formataCpf(texto){
	if ( _validaCPF(texto)!=1){
		texto = '';
	} else {
		if(texto.charAt(3) != "." )
			texto = texto.substring(0,3) +"."+ texto.substring(3,12);
	
		if(texto.charAt(7)!= "." )
			texto = texto.substring(0,7) +"."+ texto.substring(7,13);
	
		if(texto.charAt(11) != "-")
			texto = texto.substring(0,11) +"-"+ texto.substring(11,14);
	}
	return (texto);
} 

function __limpa(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);
	}
	return(c);
} 


function validarCpfCnpj(objeto){
	texto = __limpa(objeto.value);
	
	if (texto.length == 14) {
		objeto.value=__formataCNPJ(texto);		 
	} else if (texto.length == 11){ 
		objeto.value=__formataCpf(texto);
	} else {
		objeto.value='';
	}
}
