function recoverPassword() {
  var dados;

  // Cria uma variável dados em formato JSON, com 1 chave e 1 valor
  dados = {
    'email':$('#id_email').val()
  };

  $('#statusSenha').html('Aguarde...');
  $('#btOk').attr('disabled','disabled');
  $.ajax({
      type: "POST",
      url: '/cliente/ajax/recoverpass/',
      dataType: "json",
      data: dados,
      success: function(retorno){
          $('#statusSenha').empty();
          $.each(retorno, function(i, item) {
            if (item.msgtype == 'erro')
              $('#statusSenha').css("color","red");
            else {
              $('#id_email').val('');
              $('#statusSenha').css("color","green");
            }

            $('#statusSenha').html(item.msgtext);
            $('#btOk').attr('disabled','');
          });
      },
      error: function(erro) {
        $('#statusSenha').css("color","red");
        $('#statusSenha').html(erro.statusText);
        $('#btOk').attr('disabled','');
      }
  });
}

function validaCPF(campo) {
    var CPF = campo.value; // Recebe o valor digitado no campo
   // Aqui começa a checagem do CPF
   var POSICAO, I, SOMA, DV, DV_INFORMADO;
   var DIGITO = new Array(10);
   DV_INFORMADO = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado

   // Desemembra o número do CPF na array DIGITO
   for (I=0; I<=8; I++)
     DIGITO[I] = CPF.substr( I, 1);

   // Calcula o valor do 10º dígito da verificação
   POSICAO = 10;
   SOMA = 0;
   for (I=0; I<=8; I++) {
      SOMA = SOMA + DIGITO[I] * POSICAO;
      POSICAO = POSICAO - 1;
   }
   DIGITO[9] = SOMA % 11;
   if (DIGITO[9] < 2)
        DIGITO[9] = 0;
   else
       DIGITO[9] = 11 - DIGITO[9];

   // Calcula o valor do 11º dígito da verificação
   POSICAO = 11;
   SOMA = 0;
   for (I=0; I<=9; I++) {
      SOMA = SOMA + DIGITO[I] * POSICAO;
      POSICAO = POSICAO - 1;
   }
   DIGITO[10] = SOMA % 11;
   if (DIGITO[10] < 2)
        DIGITO[10] = 0;
   else
        DIGITO[10] = 11 - DIGITO[10];

   // Verifica se os valores dos dígitos verificadores conferem
   DV = DIGITO[9] * 10 + DIGITO[10];
   if (DV != DV_INFORMADO) {
      alert('CPF inválido');
      //campo.value = '';
      campo.focus();
      return false;
   }
}

function validaCNPJ(campo) {
  var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj;
  cnpj = campo.value.replace(/\D+/g, '');
  digitos_iguais = 1;
  if (cnpj.length != 14) {
    alert('CNPJ inválido');
    campo.focus();
    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)) {
      alert('CNPJ inválido');
      campo.focus();
      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)) {
      alert('CNPJ inválido');
      campo.focus();
      return false;
    } else
      return true;
  } else {
    alert('CNPJ inválido');
    campo.focus();
    return false;
  }
}

function validaCPF_CNPJ(campo) {
  // Verifica o tipo de cliente
  var result;
  tipo = $('#id_tipo').val();
  switch (tipo) {
    case 'F': result = validaCPF(campo); break;
    case 'J': result = validaCNPJ(campo); break;
    default: result = true;
  }

  if (result == true)
    $('#'+campo.id).css({'border':'1px solid #d1d1d1'});
  else
    $('#'+campo.id).css({'border':'1px solid red'});

  return result;

}

