// JavaScript Document
function ChangePessoa(tipoPessoa){
	if(tipoPessoa=='F'){
		document.getElementById('mostraCNPJ').style.display='none';				
		document.getElementById('mostraIE').style.display='none';
		document.getElementById('mostraR').style.display='none';
		document.getElementById('mostraCPF').style.display='';
		document.getElementById('mostraRG').style.display='';
		document.getElementById('mostraN').style.display='';
	}else{
		document.getElementById('mostraCNPJ').style.display='';
		document.getElementById('mostraIE').style.display='';
		document.getElementById('mostraR').style.display='';
		document.getElementById('mostraCPF').style.display='none';
		document.getElementById('mostraRG').style.display='none';
		document.getElementById('mostraN').style.display='none';
	}
};
// Objetivo principal: Verificar se email imputado pelo internauta em um capo qualquer eh um email valido.
function valMail(szVal){
 var chars    = "@#$&[]()/\\\{}!^:'\"";
 var pat      = /^(.+)@(.+)$/;
 var emaildiv = szVal.match(pat);
 if(emaildiv==null)
  return false;
 var login    = emaildiv[1];
 var dominio  = emaildiv[2];
 for(var intX0=0;intX0<chars.length;intX0++)
  if(login.indexOf(chars.substr(intX0,1))!=-1)
   return false;
 for(var intX0=0;intX0<chars.length;intX0++)
  if(dominio.indexOf(chars.substr(intX0,1))!=-1)
   return false;
 return true;
}
// Objetivo principal: Verificar se string imputada pelo internauta  em um capo qualquer eh uma string que faz algum sentido ou eh uma palavra qualquer.
function valString(szBuf){
 if(szBuf.length == 0)
  return(false);
 szBuf = szBuf.toUpperCase();
 if((szBuf == "ASDF")||(szBuf == "QWER")||(szBuf == "1234"))
  return(false);
 return(true);
}
function MascaraFone(e, o){
 var sBuf = new String();
 var iKey = getKeyPress(e);
 if (!isControlKey(iKey) && getSel()==''){
  if(!isNumberKey(iKey)||o.value.length>13) return(false);
  if(o.value.length==1 && o.value!="(") o.value="("+o.value;
  if(o.value.length==3 && o.value.substring(2,2)!=") ") o.value=o.value+") ";
  if(o.value.length==8 && o.value.substring(8,9)!="-") o.value=o.value + "-";
  if (o.value.length==13 && o.value.substring(9,10)!="-"){
   sBuf=sBuf.concat(o.value.substring(0,8),o.value.substring(9,10),"-",o.value.substring(10));
   o.value=sBuf;
  }
 }
}
function valForm() {
 var msg="Os seguintes campos estão incorretos\n\n";
 var err = false;
 var fmForm = window.document.install;

 msg += "-------------------------------------------------------------------";

 if(fmForm.nome.value==""){
  msg += "\n- Nome (Campo vazio).";
  err  = true;
 } else if(!valString(fmForm.nome.value)){
  msg += "\n- Nome (Formato de campo incorrreto).";
  err  = true;
 }

 iPessoa = -1;
 for (i=fmForm.tipo_pessoa.length-1; i > -1; i--) {
  if (fmForm.tipo_pessoa[i].checked) {
   iPessoa = i; i = -1;
  }
 }

 if(fmForm.tipo_pessoa[iPessoa].value=="F"){
	 if(fmForm.cpf.value==""){
	  msg += "\n- CPF (Campo vazio).";
	  err  = true;
	 } else if(!valString(fmForm.cpf.value)){
	  msg += "\n- CPF (Formato de campo incorrreto).";
	  err  = true;
	 }
 } else {
	 if(fmForm.cnpj.value==""){
	  msg += "\n- CNPJ (Campo vazio).";
	  err  = true;
	 } else if(!valString(fmForm.cnpj.value)){
	  msg += "\n- CNPJ (Formato de campo incorrreto).";
	  err  = true;
	 }
 }
 if(fmForm.endereco.value==""){
  msg += "\n- Endereço (Campo vazio).";
  err  = true;
 } else if(!valString(fmForm.endereco.value)){
  msg += "\n- Endereço (Formato de campo incorrreto).";
  err  = true;
 }
 if(fmForm.bairro.value==""){
  msg += "\n- Bairro (Campo vazio).";
  err  = true;
 } else if(!valString(fmForm.bairro.value)){
  msg += "\n- Bairro (Formato de campo incorrreto).";
  err  = true;
 }
 if(fmForm.estado.value==""){
	  msg += "\n- Estado(UF) (Campo vazio).";
	  err  = true;
	 }
 if(fmForm.cidade.value=="Selecione a cidade"){
  msg += "\n- Cidade (Campo vazio).";
  err  = true;
 }
 if(fmForm.cep.value==""){
  msg += "\n- CEP (Campo vazio).";
  err  = true;
 } else if(!valString(fmForm.cep.value)){
  msg += "\n- CEP (Formato de campo incorrreto).";
  err  = true;
 }
 if(fmForm.email.value==""){
	  msg += "\n- E-mail (Campo vazio).";
	  err  = true;
	 } else if(!valMail(fmForm.email.value)){
	  msg += "\n- E-mail (Formato de campo incorreto).";
	  err  = true;
	 }
 if(fmForm.tel.value==""){
  msg += "\n- Informe ao menos um telefone para contato (Campo vazio).";
  err  = true;
 }
 
 // Validacao do campo de cidade de atendimento
 if(fmForm.cid_atend.value==""){
	  msg += "\n- Informe ao menos uma cidade de atendimento (Campo vazio).";
	  err  = true;
	 }
 
 //var iInstall = 0;
 var iAceite  = 0;
 var listaMarcados = document.getElementsByTagName("INPUT");
 for (loop = 0; loop < listaMarcados.length; loop++) {
    var item = listaMarcados[loop];
    if (item.type == "checkbox" && item.checked) {
      if (item.id!="aceite") {
	iInstall = 1;
      } else {
	iAceite = 1;
      }

    }
 }  

/* if(iInstall==0){
  msg += "\n- Informe os tipos de montagem que realiza (Campo vazio).";
  err  = true;
 } */
 if(iAceite==0){
  msg += "\n- É obrigatorio o aceite do contrato";
  err  = true;
 }

 msg += "\n-----------------------------------------------------------------";
 if(err){alert(msg);}
 return(!err);
}
