  /*---------------------------------------------
        TiMiNG Comunicação & Tecnologia
        
     Autor: Adriano da Silva Araújo
     Criado em: <>
     Ultima alteração em: 19/09/2002 15:45
     Descrição: 
       Biblioteca de funções javascript úteis.
    ---------------------------------------------*/


	
function validarCPF2(){
   var cpf = document.cliente.cpf.value;
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  window.alert("CPF inválido. Tente novamente.");
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
    window.alert("CPF Válido.");
   return true;
 }
 
 function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }
function validarCPF(){
   var cpf = document.cliente.cpf.value;
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  window.alert("CPF inválido. Tente novamente.");
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
    window.alert("CPF Válido.");
   return true;
 }
 
 function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }

function habilitar(){
	
document.getElementById("logradouro").disabled=false;
document.getElementById("complemento").disabled=false;
document.getElementById("bairro").disabled=false;
document.getElementById("municipio").disabled=false;
document.getElementById("cep").disabled=false;
document.getElementById("telefone").disabled=false;
document.getElementById("alterar").disabled=true;
	
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}
function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function leech(v){
    v=v.replace(/o/gi,"0")
    v=v.replace(/i/gi,"1")
    v=v.replace(/z/gi,"2")
    v=v.replace(/e/gi,"3")
    v=v.replace(/a/gi,"4")
    v=v.replace(/s/gi,"5")
    v=v.replace(/t/gi,"7")
    return v
}

function soNumeros(v){
    return v.replace(/\D/g,"")
}
  
  function itelefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}
  
  function formatar_mascara(src, mascara) {
	var campo = src.value.length;
	var saida = mascara.substring(0,1);
	var texto = mascara.substring(campo);
	if(texto.substring(0,1) != saida) {
		src.value += texto.substring(0,1);
	}
}

  //mascara CPF
 function VerificaCPF () {
if (vercpf(document.cliente.CPF.value)) 
{
return true;
	}
	else 
{
errors="1";if (errors) alert('CPF NÃO VÁLIDO');
document.retorno = (errors == '')
}
}
function vercpf (cpf) 
{if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
return false;
add = 0;
for (i=0; i < 9; i ++)
add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
add = 0;
for (i = 0; i < 10; i ++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
alert('O CPF INFORMADO É VÁLIDO.');
return true;}

  //FUNCAO MASCARA CNPJ
function FormataCNPJ(Campo, teclapres){

   if(window.event){
    var tecla = teclapres.keyCode;
   }else  tecla = teclapres.which;

   var vr = new String(Campo.value);
   vr = vr.replace(".", "");
   vr = vr.replace(".", "");
   vr = vr.replace("/", "");
   vr = vr.replace("-", "");

   tam = vr.length + 1;

   
   if (tecla != 9 && tecla != 8){
      if (tam > 2 && tam < 6)
         Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
      if (tam >= 6 && tam < 9)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
      if (tam >= 9 && tam < 13)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
      if (tam >= 13 && tam < 15)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
      }
}
//FUNCAO VALIDA CNPJ
function validaCNPJ() {
CNPJ = document.cliente.CNPJ.value;
erro = new String;
if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
}
//substituir os caracteres que nao sao numeros
if(document.layers && parseInt(navigator.appVersion) == 4){
x = CNPJ.substring(0,2);
x += CNPJ.substring(3,6);
x += CNPJ.substring(7,10);
x += CNPJ.substring(11,15);
x += CNPJ.substring(16,18);
CNPJ = x; 
} else {
CNPJ = CNPJ.replace(".","");
CNPJ = CNPJ.replace(".","");
CNPJ = CNPJ.replace("-","");
CNPJ = CNPJ.replace("/","");
}
var nonNumbers = /\D/;
if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n"; 
var a = [];
var b = new Number;
var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
for (i=0; i<12; i++){
a[i] = CNPJ.charAt(i);
b += a[i] * c[i+1];
}
if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
b = 0;
for (y=0; y<13; y++) {
b += (a[y] * c[y]); 
}
if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
erro +="Digito verificador com problema!";
}
if (erro.length > 0){
alert(erro);
return false;
} else {
alert("CNPJ valido!");
}
return true;
}

  // FUNÇÕES EMAIL
function validaemail(){
	if (document.cliente.email.value.indexOf('@')==-1||document.cliente.email.value.indexOf('.')==-1){
	alert('Digite um Email válido.');
	}
	else{  return true; 
	}
}
//FUNÇÃO DATA
  function validaData(data) {
    if(data.length < 6 || data.length > 10) return false;
    pos0 = data.indexOf("/");
    if(pos0 == -1) return false;
    pos1 = data.indexOf("/", pos0 + 1);
    if(pos1 == -1) return false;
    if(data.indexOf("/", pos1 + 1) != -1) return false;
    dia = data.substring(0,pos0);
    dia = (dia.charAt(0) == "0") ? dia.charAt(dia.length - 1) : dia
    mes = data.substring(pos0 + 1, pos1);
    mes = (mes.charAt(0) == "0") ? mes.charAt(mes.length - 1) : mes
    ano = data.substring(pos1 + 1, data.length);
    ano = (ano.charAt(0) == "0") ? ano.charAt(ano.length - 1) : ano
    if(isNaN(dia) || isNaN(mes) || isNaN(ano)) return false;
    if(parseInt(ano) >= 0 && parseInt(ano) < 1900) return false;
    if(parseInt(ano) > 2100 || parseInt(ano) < 0 || parseInt(mes) > 12 || parseInt(mes) < 1) return false;
    numero = ((parseInt(ano) - 1884) / 4)
    if(numero == Math.floor(numero))
    { dias = "312931303130313130313031"; }
    else
    { dias = "312831303130313130313031"; }
    diamax = parseInt(dias.substring((mes-1)*2,((mes-1)*2)+2));
    if(parseInt(dia) < 1 || parseInt(dia) > diamax) return false;
    return true;
  }
  
  // Usar as duas próximas funções devem ser utilizadas em conjunto.
  var flag = 0;
  function mascaraKeyPress(edt) {
    if (self.event.keyCode < 48 || self.event.keyCode > 57) return false;
    flag = (edt.value.length == 1 || edt.value.length == 4) ? 1 : 0;
    return true;
  } 
  function mascaraKeyUp(edt) {
    if (edt.value.length == 2 || edt.value.length == 5) {
       if (flag == 1) { 
          edt.value += "/"; flag = 2; }
       else if (flag == 2) {        
          edt.value = edt.value.substring(0, edt.value.length-1); }
    }
  } 
  
  // FUNÇÕES DE MANIPULAÇÃO NUMÉRICA
  function isDigit (c){ return ((c >= "0") && (c <= "9"))  }  
  
  function isInteger (s) {
    var i;
    if (isEmpty(s)) if (isInteger.arguments.length == 1) return false;
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
  }
  
  // FUNÇÕES DIVERSAS
  function criaArray(n) { this.length = n; }
  
  function setFocus(obj, str) {
    if (! str=="") alert(str);
    obj.focus(); obj.select();
  } 
  
 
          function mascara_data(data){ 
              var mydata = ''; 
              mydata = mydata + data; 
              if (mydata.length == 2){ 
                  mydata = mydata + '/'; 
                  document.forms[0].data.value = mydata; 
              } 
              if (mydata.length == 5){ 
                  mydata = mydata + '/'; 
                  document.forms[0].data.value = mydata; 
              } 
              if (mydata.length == 10){ 
                  verifica_data(); 
              } 
          } 
           
          function verifica_data () { 

            dia = (document.forms[0].data.value.substring(0,2)); 
            mes = (document.forms[0].data.value.substring(3,5)); 
            ano = (document.forms[0].data.value.substring(6,10)); 

            situacao = ""; 
            // verifica o dia valido para cada mes 
            if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
                situacao = "falsa"; 
            } 

            // verifica se o mes e valido 
            if (mes < 01 || mes > 12 ) { 
                situacao = "falsa"; 
            } 

            // verifica se e ano bissexto 
            if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
                situacao = "falsa"; 
            } 
    
            if (document.forms[0].data.value == "") { 
                situacao = "falsa"; 
            } 
    
            if (situacao == "falsa") { 
                alert("Data inválida!"); 
                document.forms[0].data.focus(); 
            } 
          } 

          function mascara_hora(hora){ 
              var myhora = ''; 
              myhora = myhora + hora; 
              if (myhora.length == 2){ 
                  myhora = myhora + ':'; 
                  document.forms[0].hora.value = myhora; 
              } 
              if (myhora.length == 5){ 
                  verifica_hora(); 
              } 
          } 
           
          function verifica_hora(){ 
              hrs = (document.forms[0].hora.value.substring(0,2)); 
              min = (document.forms[0].hora.value.substring(3,5)); 
               
              alert('hrs '+ hrs); 
              alert('min '+ min); 
               
              situacao = ""; 
              // verifica data e hora 
              if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){ 
                  situacao = "falsa"; 
              } 
               
              if (document.forms[0].hora.value == "") { 
                  situacao = "falsa"; 
              } 

              if (situacao == "falsa") { 
                  alert("Hora inválida!"); 
                  document.forms[0].hora.focus(); 
              } 
          } 
