/*
  Portal Deuter
  Funções Javascript e Validações
  por Carlos Gutierre
  carlos@weborange.com.br
*/

/* ------------------------------------------------------------------ */
/*     NÚCLEO do Preenchimento de SELECTS                             */
/* ------------------------------------------------------------------ */

function create_opcao(id,opcao) {
   var new_opcao = document.createElement("option");
   var texto = document.createTextNode(opcao);
   new_opcao.setAttribute("value",id);
   new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
   return new_opcao; // Retorna a nova OPTION.
}

/* ------------------------------------------------------------------ */
/*     FIM DO NÚCLEO                                                  */
/* ------------------------------------------------------------------ */

function PegaProdutos(cat) {
 if(cat != "") {
    document.getElementById('equipamento').innerHTML= "";
    new_opcao = create_opcao(""," Carregando, aguarde! ");
    document.getElementById('equipamento').appendChild(new_opcao);
    jphp("POST","includes/jphp/pega_produtos.php","cat="+cat,"ShowProdutos");
 }
}

function ShowProdutos(html) {
  var ped1 = html.split(";");
  var tam = ped1.length;
 // var novo = "";
 document.getElementById('equipamento').innerHTML= "";

    new_opcao = create_opcao("","Selecione o Equipamento");
    document.getElementById('equipamento').appendChild(new_opcao);

  for(i=0; i<tam -1; i++) {
    var vem = ped1[i].split("|");
    new_opcao = create_opcao(vem[0],vem[1]);
    document.getElementById('equipamento').appendChild(new_opcao);

  }

}

function utf8_encode ( string ) {

    string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");

    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}

function VerificaEmail(email) {
     if(email.indexOf('@') == "-1" ||
		email.lastIndexOf('.') < email.indexOf('@') ||
		email.indexOf('@') == "0" ||
		email.indexOf('.') == (email.indexOf('@'))+1 ||
		email.lastIndexOf('.') == (email.length)-1 ||
		email.length < 5)
		{
			return false
		} else {
            return true
        }
}

function ValidaEnquete() {
 if(document.form_enquete.opcao[0].checked == false && document.form_enquete.opcao[1].checked == false && document.form_enquete.opcao[2].checked == false && document.form_enquete.opcao[3].checked == false && document.form_enquete.opcao[4].checked == false) { alert("Selecione uma resposta."); return false; }
}

function ValidaNewsletter() {
 if(document.form_newsletter.email.value != "") {
  if(VerificaEmail(document.form_newsletter.email.value) == false) {
   alert("E-mail invalido.");
   return false;
  }
 } else {
  alert("Preencha o campo e-mail.");
  return false;
 }
}

function ValidaPergunte() {
 if(document.perguntar.nome.value.length == 0) {
   alert("Digite seu nome.");
   document.perguntar.nome.focus();
   return false;
 }
 if(document.perguntar.email.value != "") {
  if(VerificaEmail(document.perguntar.email.value) == false) {
   alert("E-mail invalido.");
   document.perguntar.email.focus();
   return false;
  }
 } else {
  alert("Preencha o campo e-mail.");
  document.perguntar.email.focus();
  return false;
 }
 if(document.perguntar.pergunte.value.length == 0) {
   alert("Digite seu pergunta.");
   document.perguntar.pergunte.focus();
   return false;
 }
}

function ValidaAssistencia() {
 if(document.assistencia.nome.value.length == 0) {
   alert("Digite seu nome.");
   document.assistencia.nome.focus();
   return false;
 }
 if(document.assistencia.email.value != "") {
  if(VerificaEmail(document.assistencia.email.value) == false) {
   alert("E-mail invalido.");
   document.assistencia.email.focus();
   return false;
  }
 } else {
  alert("Preencha o campo e-mail.");
  document.assistencia.email.focus();
  return false;
 }
 if(document.assistencia.ddd.value.length == 0) {
   alert("Digite o DDD do seu telefone.");
   document.assistencia.ddd.focus();
   return false;
 }
 if(document.assistencia.telefone.value.length == 0) {
   alert("Digite seu telefone.");
   document.assistencia.telefone.focus();
   return false;
 }
 if(document.assistencia.categoria.value == "") {
   alert("Selecione uma categoria.");
   document.assistencia.categoria.focus();
   return false;
 }
 if(document.assistencia.equipamento.value == "") {
   alert("Selecione seu equipamento.");
   document.assistencia.equipamento.focus();
   return false;
 }
 if(document.assistencia.identificacao.value.length == 0) {
   alert("Digite o codigo de identicacao do seu produto.");
   document.assistencia.identificacao.focus();
   return false;
 }
 if(document.assistencia.sugestoes.value.length == 0) {
   alert("Digite sua sugestao.");
   document.assistencia.sugestoes.focus();
   return false;
 }
}

function ValidaSac() {
 if(document.formulario.nome.value.length == 0) {
   alert("Digite seu nome.");
   document.formulario.nome.focus();
   return false;
 }
 if(document.formulario.endereco.value.length == 0) {
   alert("Digite seu endereco.");
   document.formulario.endereco.focus();
   return false;
 }
 if(document.formulario.email.value != "") {
  if(VerificaEmail(document.formulario.email.value) == false) {
   alert("E-mail invalido.");
   document.formulario.email.focus();
   return false;
  }
 } else {
  alert("Preencha o campo e-mail.");
  document.formulario.email.focus();
  return false;
 }
 if(document.formulario.estado.value == "") {
   alert("Selecione seu estado.");
   document.formulario.estado.focus();
   return false;
 }
 if(document.formulario.cidade.value == "") {
   alert("Selecione sua cidade.");
   document.formulario.cidade.focus();
   return false;
 }
 if(document.formulario.mensagem.value.length == 0) {
   alert("Digite sua mensagem.");
   document.formulario.mensagem.focus();
   return false;
 }
}


function PegaCidade(estado) {
    document.formulario.cidade.innerHTML= "";
    new_opcao = create_opcao(""," Carregando, aguarde! ");
    document.formulario.cidade.appendChild(new_opcao);
 jphp("POST","includes/pega_cidade.php","estado="+estado,"mostra_cidades");
}

function mostra_cidades(html) {
  var ped1 = html.split(";");
  var tam = ped1.length;
 // var novo = "";
 document.formulario.cidade.innerHTML= "";

    new_opcao = create_opcao("","Selecione a Cidade");
    document.formulario.cidade.appendChild(new_opcao);

  for(i=0; i<tam -1; i++) {
    var vem = ped1[i].split("|");
    new_opcao = create_opcao(vem[0],vem[1]);
    document.formulario.cidade.appendChild(new_opcao);

  }

}

function PegaCidadeLojas(estado) {
    document.formulario.cidade.innerHTML= "";
    new_opcao = create_opcao(""," Carregando, aguarde! ");
    document.formulario.cidade.appendChild(new_opcao);
 jphp("POST","includes/pega_cidade_lojas.php","estado="+estado,"mostra_cidades_lojas");
}

function mostra_cidades_lojas(html) {
  var ped1 = html.split(";");
  var tam = ped1.length;
 // var novo = "";
 document.formulario.cidade.innerHTML= "";

    new_opcao = create_opcao("","Selecione a Cidade");
    document.formulario.cidade.appendChild(new_opcao);

  for(i=0; i<tam -1; i++) {
    var vem = ped1[i].split("|");
    new_opcao = create_opcao(vem[0],vem[1]);
    document.formulario.cidade.appendChild(new_opcao);

  }

}

function PegaLojas(cidade) {
    document.formulario.loja.innerHTML= "";
    new_opcao = create_opcao(""," Carregando, aguarde! ");
    document.formulario.loja.appendChild(new_opcao);
 jphp("POST","includes/pega_lojas.php","cidade="+cidade,"mostra_lojas");
}

function mostra_lojas(html) {
  var ped1 = html.split(";");
  var tam = ped1.length;
 // var novo = "";
 document.formulario.loja.innerHTML= "";

    new_opcao = create_opcao("","Selecione a Loja");
    document.formulario.loja.appendChild(new_opcao);

  for(i=0; i<tam -1; i++) {
    var vem = ped1[i].split("|");
    new_opcao = create_opcao(vem[0],vem[1]);
    document.formulario.loja.appendChild(new_opcao);

  }

}

function ValidaBusca() {
 if(document.busca.buscar.value.length == 0 && document.busca.tipo.value == "" && document.busca.utilizacao.value == "") {
   alert("Escolha um tipo, uma utilizacao ou uma palavra para buscar.");
   return false;
 }
}

function ValidaBuscaLoja() {
 if(document.formulario.estado.value == "" && document.formulario.tipo.value == "") {
   alert("Utilize o filtro de estado, cidade e tipo.");
   return false;
 }
}

function ValidaRegistro() {
 if(document.formulario.nome.value.length == 0) {
   alert("Digite seu nome.");
   document.formulario.nome.focus();
   return false;
 }
 if(document.formulario.email.value != "") {
  if(VerificaEmail(document.formulario.email.value) == false) {
   alert("E-mail invalido.");
   document.formulario.email.focus();
   return false;
  }
 } else {
  alert("Preencha o campo e-mail.");
  document.formulario.email.focus();
  return false;
 }
 if(document.formulario.endereco.value.length == 0) {
   alert("Digite seu endereco.");
   document.formulario.endereco.focus();
   return false;
 }
 if(document.formulario.estados.value == "") {
   alert("Selecione seu estado.");
   document.formulario.estados.focus();
   return false;
 }
 if(document.formulario.cidade2.value == "") {
   alert("Selecione sua cidade.");
   document.formulario.cidade2.focus();
   return false;
 }
 if(document.formulario.telefone.value.length == 0) {
   alert("Digite seu telefone.");
   document.formulario.telefone.focus();
   return false;
 }
 if(document.formulario.cep.value.length == 0) {
   alert("Digite seu CEP.");
   document.formulario.cep.focus();
   return false;
 }
 if(document.formulario.dia.value == "") {
   alert("Selecione o dia em que nasceu.");
   document.formulario.dia.focus();
   return false;
 }
 if(document.formulario.mes.value == "") {
   alert("Selecione o mes em que nasceu.");
   document.formulario.mes.focus();
   return false;
 }
 if(document.formulario.ano.value == "") {
   alert("Selecione o ano em que nasceu.");
   document.formulario.ano.focus();
   return false;
 }
 if(document.formulario.categoria.value == "") {
   alert("Selecione uma categoria.");
   document.formulario.categoria.focus();
   return false;
 }
 if(document.formulario.equipamento.value == "") {
   alert("Selecione um equipamento.");
   document.formulario.equipamento.focus();
   return false;
 }
 if(document.formulario.estado.value == "") {
   alert("Selecione o estado onde comprou seu equipamento.");
   document.formulario.estado.focus();
   return false;
 }
 if(document.formulario.cidade.value == "") {
   alert("Selecione a cidade onde comprou seu equipamento.");
   document.formulario.cidade.focus();
   return false;
 }
 if(document.formulario.loja.value == "") {
   alert("Selecione a loja onde comprou seu equipamento.");
   document.formulario.loja.focus();
   return false;
 }

 document.getElementById('prods').disabled='';
}

function ValidaComentario() {
 if(document.form_coment.nome.value.length == 0) {
   alert("Digite seu nome.");
   document.form_coment.nome.focus();
   return false;
 }
 if(document.form_coment.email.value != "") {
  if(VerificaEmail(document.form_coment.email.value) == false) {
   alert("E-mail invalido.");
   document.form_coment.email.focus();
   return false;
  }
 } else {
  alert("Preencha o campo e-mail.");
  document.form_coment.email.focus();
  return false;
 }
 if(document.form_coment.comentario.value.length == 0) {
   alert("Digite seu comentario.");
   document.form_coment.comentario.focus();
   return false;
 }
}

function AddProd() {
 if(document.getElementById('codigo').value.length > 0) {
  txt = document.getElementById('prods').innerHTML;
  document.getElementById('prods').innerHTML = txt + document.getElementById('codigo').value + ', ' ;
  document.getElementById('codigo').value = "";
  document.getElementById('codigo').focus();
 }
}

function CleanProd() {
 document.getElementById('prods').innerHTML = "";
}
