﻿/**Agradecimento: http://www.pcforum.com.br/cgi/yabb/YaBB.cgi?num=1090001360 **/
function validaCpf(cpf)
{
      var numeros, digitos, soma, i, resultado, digitos_iguais;
      digitos_iguais = 1;
      if (cpf.length < 11)
            return false;
      for (i = 0; i < cpf.length - 1; i++)
            if (cpf.charAt(i) != cpf.charAt(i + 1))
            {
                  digitos_iguais = 0;
                  break;
            }
      if (!digitos_iguais)
            {
            numeros = cpf.substring(0,9);
            digitos = cpf.substring(9);
            soma = 0;
            for (i = 10; i > 1; i--)
                  soma += numeros.charAt(10 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            numeros = cpf.substring(0,10);
            soma = 0;
            for (i = 11; i > 1; i--)
                  soma += numeros.charAt(11 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
      }
      else
            return false;
}

function validaCnpj(cnpj)
{
      var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
      digitos_iguais = 1;
      if (cnpj.length < 14 && cnpj.length < 15)
            return false;
      for (i = 0; i < cnpj.length - 1; i++)
            if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            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))
                  return false;
            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))
                  return false;
            return true;
            }
      else
            return false;
} 

function mostraResultado(tipo,ok)
{
    if(ok)
    {
        $("#result"+tipo+"ok").show();
        $("#result"+tipo+"er").hide();
    }else{
        $("#result"+tipo+"ok").hide();
        $("#result"+tipo+"er").show();    
    }
}

function execCpf()
{
    var t = $("#TxtCpf").val().length;
    if(t<11)
    {
        alert("O CPF deve ter 11 dígitos e você digitou apenas "+t+".");
        return false;
    }
    mostraResultado("cpf", validaCpf($("#TxtCpf").val()));
    return false;
}

function execCnpj()
{
    var t = $("#TxtCnpj").val().length;
    if(t<11)
    {
        alert("O CNPJ deve ter 14 dígitos e você digitou apenas "+t+".");
        return false;
    }
    mostraResultado("cnpj", validaCnpj($("#TxtCnpj").val()));
    return false;
}
