function validarCPF(cpf){
   var cpf;
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
     window.alert("CPF inválido. Tente novamente.");
	 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"){
	  window.alert("CPF inválido. Tente novamente.");
	  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))){
	 window.alert("CPF inválido. Tente novamente.");
	 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))){
     window.alert("CPF inválido. Tente novamente.");
	 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;
 }

function validaCNPJ(cnpj){
   var cnpj;
   var filtro = /^\d{2}.\d{3}.\d{3}\/\d{4}-\d{2}$/i;
   var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
   if(!filtro.test(cnpj)){
     window.alert("CNPJ inválido. Tente novamente.");
	 return false;
   }
   
   cnpj = remove(cnpj, ".");
   cnpj = remove(cnpj, "/");
   cnpj = remove(cnpj, "-");
   
   if(cnpj.length != 14 || cnpj == "000000000000000" || cnpj == "111111111111111" ||
	  cnpj == "222222222222222" || cnpj == "333333333333333" || cnpj == "444444444444444" ||
	  cnpj == "555555555555555" || cnpj == "666666666666666" || cnpj == "777777777777777" ||
	  cnpj == "888888888888888" || cnpj == "999999999999999"){
	  window.alert("CNPJ inválido. Tente novamente.");
	  return false;
   }
    
	tamanho = cnpj.length - 2
	numeros = cnpj.substring(0,tamanho);
	digitos = cnpj.substring(tamanho);
	soma = 0;
	pos = tamanho - 7;
	
		for (i = tamanho; i >= 1; i--){
			  soma += numeros.charAt(tamanho - i) * pos--;
			  if (pos < 2)
					pos = 9;
		}
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(0)){
          window.alert("CNPJ inválido. Tente novamente.")
		}
		
	tamanho = tamanho + 1;
	numeros = cnpj.substring(0,tamanho);
	soma = 0;
	pos = tamanho - 7;
	
        for (i = tamanho; i >= 1; i--){
              soma += numeros.charAt(tamanho - i) * pos--;
              if (pos < 2)
                    pos = 9;
         }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
		if (resultado != digitos.charAt(1)){
           window.alert("CNPJ inválido. Tente novamente.");
		}
}
