/*
 * Consulta Remédios v2.0
 * por Junior Gobira
 * www.consultaremedios.com.br
 */

function ufcid(valor) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(ex) {
try {
ajax = new XMLHttpRequest();
}
catch(exc) {
alert("Esse browser não aceita scripts AJAX");
ajax = null;
}
}
}

if(ajax) {
document.cadastro.cidade.options.length = 1;
idOpcao = document.getElementById("opcoes");
ajax.open("POST", "cids.php", true);
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.onreadystatechange = function() {
if(ajax.readyState == 1) {
                   idOpcao.innerHTML = "Aguarde, carregando cidades...";
}
if(ajax.readyState == 4 ) {
if(ajax.responseXML) {
processXML(ajax.responseXML);
}
else {
idOpcao.innerHTML = "Primeiro selecione o estado";
}
}
}
var params = "estado="+valor;
ajax.send(params);
}
}

function processXML(obj){
var dataArray = obj.getElementsByTagName("cidade");
if(dataArray.length > 0) {
for(var i = 0 ; i < dataArray.length ; i++) {
var item = dataArray[i];
var codigo = item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
idOpcao.innerHTML = "Selecione um das cidades abaixo";
var novo = document.createElement("option");
    novo.setAttribute("id", "opcoes");
    novo.value = codigo;
    novo.text  = descricao;
    document.cadastro.cidade.options.add(novo);
}
}
else {
idOpcao.innerHTML = "Primeiro selecione o estado";
}
}

function vercad(theForm)
{
    var nome_site = "Consulta Remédios - Cadastro             \n\nPor favor";
    var flt = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

    if ( (theForm.pessoa[0].checked == false) && (theForm.pessoa[1].checked == false) ) {
       alert(nome_site+" informe qual é o tipo de pessoa.");
       return (false);
    }

    if (theForm.pessoa[0].checked == true) {
       z = vCPF(theForm.documento.value);
       if (z == 'err') {
       alert(nome_site+" informe o seu CPF corretamente.");
       return (false);
       }
    }

    if (theForm.pessoa[1].checked == true) {
       z = vCNPJ(theForm.documento.value);
       if (z == 'err') {
       alert(nome_site+" informe o CNPJ corretamente.");
       return (false);
       }
    }

    if (theForm.nome.value == "") {
       alert(nome_site+" informe o seu Nome/Razão Social.");
       theForm.nome.focus();
       return (false);
    }

    if (theForm.pessoa[1].checked == true) {
       if (theForm.nome_fantasia.value == "") {
       alert(nome_site+" informe o Nome Fantasia da Empresa.");
       theForm.nome_fantasia.focus();
       return (false);
       }
    }

    if(!flt.test(theForm.email.value)) {
       alert(nome_site+" informe o E-mail corretamente.");
       theForm.email.focus();
       return (false);
    }

    if ( (theForm.sexo[0].checked == false) && (theForm.sexo[1].checked == false) ) {
       alert(nome_site+" informe o seu Sexo.");
       return (false);
    }

    if (theForm.endereco.value == "") {
       alert(nome_site+" informe o seu Endereço.");
       theForm.endereco.focus();
       return (false);
    }

    if (theForm.bairro.value == "") {
       alert(nome_site+" informe o seu Bairro.");
       theForm.bairro.focus();
       return (false);
    }

    if (theForm.uf.value == 0) {
       alert(nome_site+" informe o seu Estado.");
       theForm.uf.focus();
       return (false);
    }

    if (theForm.cidade.value == 0) {
       alert(nome_site+" informe o sua Cidade.");
       theForm.cidade.focus();
       return (false);
    }

    if ( (theForm.telefone_res.value == "") && (theForm.telefone_com.value == "") && (theForm.celular.value == "") )  {
       alert(nome_site+" informe pelo menos 1(um) número de telefone.");
       theForm.telefone_res.focus();
       return (false);
    }

    if (theForm.senha.value == "") {
       alert(nome_site+" informe uma Senha.");
       theForm.senha.focus();
       return (false);
    }

    if ( (theForm.senha.value == "123456") || (theForm.senha.value == "12345678") ) {
       alert(nome_site+" informe uma Senha, esta senha não é aceitável.");
       theForm.senha.focus();
       return (false);
    }

    if (theForm.senha.value.length <= 5) {
       alert(nome_site+" informe a Nova Senha. (mínimo 6 caracteres)");
       theForm.senha.focus();
       return (false);
    }

    if (theForm.senha.value != theForm.csenha.value) {
       alert(nome_site+" confirme sua senha corretamente.");
       theForm.csenha.focus();
       return (false);
    }

    theForm.submit;
}

function vCPF(s){
    var i;
    var c = s.substr(0,9);
    var dv = s.substr(9,2);
    var d1 = 0;
    for (i = 0; i < 9; i++) {
    d1 += c.charAt(i)*(10-i);
    }
    if (d1 == 0){
    return "err";
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1) {
    return "err";
    }
    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 "err";
    }
    return "ok";
}

function vCNPJ(s)
{
      var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
      digitos_iguais = 1;
      if (s.length < 14 && s.length < 15)
            return "err";
      for (i = 0; i < s.length - 1; i++)
            if (s.charAt(i) != s.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            tamanho = s.length - 2
            numeros = s.substring(0,tamanho);
            digitos = s.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 "err";
            tamanho = tamanho + 1;
            numeros = s.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 "err";
            return "ok";
            }
      else
            return "err";
}
