  // #############################################################

  function ismail( oMail ) {
    // Valida oMail como um e-mail, retorna true/false

    MyRegExp = new RegExp("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
    return MyRegExp.test(oMail)

  }

  // #############################################################

  function boleto(id) {
    window.open ("index.php?acao=showboleto&id="+id, "_blank");
  }

  // #############################################################

  function round(number,X) {
    // arredonda um numero para as casas decimais especificadas
    X = (!X ? 2 : X);
    return Math.floor(number*Math.pow(10,X))/Math.pow(10,X);
  }

  // #############################################################


  function validamail ( oform) {
    oMail = oform.mail.value;
    if ( ismail ( oMail ) ) {
       return true;
    } else {
       alert("Endereço de E-mail inválido!");
       return false;
    }
  }

	 // #############################################################


  function validamail2 ( oform) {
    oMail = oform.mail.value;
		document.getElementById('oTd').innerHTML = "<div style=\"border-width=thin;border-style= solid;border-color= #000000;\"><font size=4 >Aguarde...</font></div>";
    if ( ismail ( oMail ) ) {
		    oform.submit();
		} else {
       alert("Endereço de E-mail inválido!");
			 document.getElementById('oTd').innerHTML = "<input name=\"submit\" id=oButton type=submit class=campos value=\" Enviar \" OnClick=\"javaScript:return validamail2(document.frmcontato);\">";
       return false;
    }
  }

 // #############################################################

  function radioSelect (campo,qual) {
    if (campo) {
      for (cont=0;cont<campo.length;cont++) {
        if (campo[cont].value == qual) campo[cont].checked = true;
      }
    }
  }

  // #############################################################

  function printver(oque) {
    window.open (oque, "_new","menubar=yes, width=500,height=400, scrollbars=yes");
  }

  // #############################################################

  function popmeup(oque,w,h,comsb) {
    if (w==0) w = 515;
    if (h==0) h = 400;
    window.open (oque, "_new","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+(comsb?"yes":"no")+",resizable=no,menubar=no,width="+w+",height="+h);
  }

  // #############################################################

  function validacadastro(requerlogin) {
     frm = document.frmcad;
     resposta = "Os seguintes campos contém problemas:\n-------------------------------------\n";
     ok = true;
     // login
     MyRegExp = new RegExp("^([0-9a-zA-ZáàãâÀÁÃÂéèêÉÈÊíìîÌÍÎóòôõÓÒÔÕúùûÚÛÙçÇ\_]{2,15})$");
     if (requerlogin && !MyRegExp.test(frm.login.value)) { // se nao requer login, ele testa E-mail
       // login invalido
       resposta += "Login inválido ou com caracteres inválidos. O Login deve ter de 2 a 15 caracteres.\n";
       ok = false;
     }
     // senha
     MyRegExp = new RegExp("^([0-9a-zA-ZáàãâÀÁÃÂéèêÉÈÊíìîÌÍÎóòôõÓÒÔÕúùûÚÛÙçÇ\_]{5,10})$");
     if (!MyRegExp.test(frm.senha.value) && (frm.senha.value != "")) { // com erro
       resposta += "Senha inválida ou com caracteres inválidos. A Senha deve ter de 5 a 10 caracteres.\n";
       ok = false;
     } else if (frm.senha.value != frm.senha_c.value) { // senha ok, testa se sao iguais
       resposta += "O valor da senha e da confirmação da senha diferem. Devem ser iguais.\n";
       // ja preencheu usenha, remove!
       frm.usenha.value = "";
       ok = false;
     } else if (frm.senha.value != "") { // senha ok e igual ... md5 it
       md5login(); // preenche usenha com a senha em md5
     }
     // mail
     if ( !ismail (frm.mail.value) ) {
       resposta += "O E-mail especificado não é um E-mail válido\n";
       ok = false;
     }
     // a partir de agora testa os obrigatórios
     // nome
     if ( frm.nome.value.length == 0 ) {
       resposta += "Campo 'Nome' é obrigatório\n";
       ok = false;
     }
     // endereco
     if ( frm.endereco.value.length == 0 ) {
       resposta += "Campo 'Endereco' é obrigatório\n";
       ok = false;
     }
     // bairro
     if ( frm.bairro.value.length == 0 ) {
       resposta += "Campo 'Bairro' é obrigatório\n";
       ok = false;
     }
     // cidade
     if ( frm.cidade.value.length == 0 ) {
       resposta += "Campo 'Cidade' é obrigatório\n";
       ok = false;
     }
     // estado
     if ( frm.estado.value.length == 0 ) {
       resposta += "Campo 'Estado' é obrigatório\n";
       ok = false;
     }
     // pais
     if ( frm.pais.value.length == 0 ) {
       resposta += "Campo 'País' é obrigatório\n";
       ok = false;
     }
     // cep
     if ( frm.cep.value.length != 8 ) {
       resposta += "Campo 'Cep' é obrigatório\n";
       ok = false;
     }
     // cpf/cnpj
     if (frm.pjuridica[1].checked) { // CNPJ / CGC
       if (!validaCGC(frm.cpf_cnpj.value)) {
         resposta += "O CNPJ/CGC digitado não é válido (ou é um CPF), e este campo é obrigatório para Pessoa Jurídica\n";
         ok = false;
       }
     } else { // CPF
       if (!validaCPF(frm.cpf_cnpj.value)) {
         resposta += "O CPF digitado não é válido (ou é um CNPJ), e este campo é obrigatório para Pessoa Física\n";
         ok = false;
       }
     }
     if (!ok) {
        alert (resposta);
     }
     return ok;
  }

  // #############################################################

  function isnumber( oValor ) {
    MyRegExp = new RegExp("^(\-)?([0-9]+)(([\,\.]{1}[0-9]{0,2})?)$");
    return MyRegExp.test(oValor);
  }

  // #############################################################

  function selall(ofrm,modo) {
    for (var i=0;i<ofrm.elements.length;i++){
	  ofrm.elements[i].checked = modo;
	}
  }

  // #############################################################

  function togglediv(divname, para) {
    if (document.getElementById) {
      document.getElementById(divname).style.display  = (para) ? '' : 'none';
    } else if (document.all) {
      document.all[divname].style.display = (para) ? '' : 'none';
    } else if(document.layers) {
      document[divname].style.display = (para) ? '' : 'none';
    }
  }

  // #############################################################
  function menudiv(divname) {
    if (document.getElementById) {
      document.getElementById(divname).style.display  = (document.getElementById(divname).style.display == 'none') ? 'block' : 'none';
    } else if (document.all) {
      document.all[divname].style.display = (document.all[divname].style.display == 'none') ? 'block' : 'none';
    } else if(document.layers) {
      document[divname].style.display = (document[divname].style.display == 'none') ? 'block' : 'none';
    }
  }

  // #############################################################
  function mygetElement(id) {

    if (document.getElementById) {
      return document.getElementById(id);
    } else if (document.all) {
      return document.all[id];
    } else if(document.layers) {
      return document[id];
    }
  }

  // #############################################################
  function lostpass () {
    document.location = "index.php?acao=lostpass&login=" + document.frmlogin.ulogin.value;
  }

  // #############################################################
  
  function apenasNumeros(k) { 
    if (k == 0 || k == 8) return true; // firefox LIXO
    if(!(k >= 48 && k <= 57)) { 
        return false;
    }
  }
  
  // #############################################################
  function ereg(arquivo,mascara) {
    MyRegExp = new RegExp(mascara);
    return MyRegExp.test(arquivo);
  }
  
  // #############################################################

  function limpaString(S){
	// Deixa so' os digitos no numero
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";

	for (var i=0; i<S.length; i++)	{
		digito = S.charAt(i);
		if (Digitos.indexOf(digito) >= 0)	{
			temp=temp+digito
        }
	} //for

	return temp
  }
  
  // #############################################################
  
  function validaCGC(s) {
  var i;
  s = limpaString(s);
  if (s.length < 14) return false;
  var c = s.substr(0,12);
  var dv = s.substr(12,2);
  var d1 = 0;
  for (i = 0; i < 12; i++)
  {
    d1 += c.charAt(11-i)*(2+(i % 8));
  }
        if (d1 == 0) return false;
        d1 = 11 - (d1 % 11);
  if (d1 > 9) d1 = 0;
  if (dv.charAt(0) != d1)
  {
    return false;
  }
  
  d1 *= 2;
  for (i = 0; i < 12; i++)
  {
    d1 += c.charAt(11-i)*(2+((i+1) % 8));
  }
  d1 = 11 - (d1 % 11);
  if (d1 > 9) d1 = 0;
  if (dv.charAt(1) != d1)
  {
    return false;
  }
  return true;
}

// #############################################################
function validaCPF(cpf) {
  var i; 
  s = cpf; 
  if (s.length < 11) return false;
  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 false; 
  } 
    
  d1 = 11 - (d1 % 11); 
  if (d1 > 9) d1 = 0; 
  if (dv.charAt(0) != d1) { 
      return false; 
  } 
  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 false; 
  } 
    
  return true; 
} 
