// JScript source code
function numbersOnly(myfield, e){
    if (myfield.length ==0)
     myfield.value=0;  
   var key;
   var keychar;
    if (window.event)
     key = window.event.keyCode;
    else if (e)
     key = e.which;
    else
     return true;
   keychar = String.fromCharCode(key);
    if ((key==null) || (key==0) || (key==8) || 
        (key==9)|| (key==13)|| (key==27) )
     return true;
    else if ((("0123456789").indexOf(keychar) > -1))
      return true;
 else
   return false;
}

function FormataData( campo )
{    var tam = campo.value.length;
    if (((event.keyCode) >= 44 ) && ((event.keyCode) <= 57 )) 
    {    event.keyCode;
        if ( ( tam == 2 ) || ( tam == 5 ) )
        {    campo.value = campo.value + "/";    }
    }
    else
    {    event.keyCode = 0;    }
} 

function ValidaData( campo ){
	if ((campo.value.length > 1) && (campo.value.length < 10)) {
		alert("Formato deve ser dd/mm/aaaa");
		campo.focus();
	}
}

function validar(){
var check = true

//  if ((document.forms[0].txtNumInicial) && (document.forms[0].txtNumInicial.value.length != 15)) {
//     alert("Numero SISBOV Inicial Incorreto");
//     check = false;
//  }
//  else {
//    if ((document.forms[0].txtNumInicial.value.substr(0,3) != "105") && (check)){
//       alert("Numero SISBOV Inicial Incorreto");
//       check = false;
//    }
//  }

//  if ((document.forms[0].txtNumFinal) && (document.forms[0].txtNumFinal.value.length != 15)) {
//    if ((document.forms[0].txtNumInicial.value.substr(0,3) == "105") && (check)){
//       alert("Numero SISBOV Inicial Incorreto");
//     check = false;
//    }
//  }

  var s; 

  // inicio de verificacao de cnpj ou cpf 
  //if (document.forms[0].txtCNPJ.value.length == 0) { 
  //  alert("O CNPJ/CPF é um campo obrigatório !"); 
  //  document.forms[0].txtCNPJ.focus(); 
  //  return false; 
  //} 
if (document.forms[0].txtCNPJ.value.length != 0) { 
  s = limpa_string(document.forms[0].txtCNPJ.value); 

  // checa se é cpf 
  if (s.length == 11) { 
    if (valida_CPF(document.forms[0].txtCNPJ.value) == false ) { 
      alert("O CPF não é válido !"); 
      document.forms[0].txtCNPJ.focus(); 
      return false; 
    } 
  } 
  // checa se é cgc 
  else if (s.length == 14) { 
    if (valida_CGC(document.forms[0].txtCNPJ.value) == false ) { 
      alert("O CNPJ não é válido !"); 
      document.forms[0].txtCNPJ.focus(); 
      return false; 
    } 
  } 
  else { 
    alert("O CPF/CNPJ não é válido !"); 
    document.forms[0].txtCNPJ.focus(); 
    return false; 
  } 
  }
  // final da verificacao de cnpj ou cpf 


  if (check)
	return true;
  else
	return false;
	
}

  function limpa_string(S){ 
  // Deixa so' os digitos no campo 
    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 } 
    } //fim do for 
    return temp 
  }  // fim limpa_String 

  function valida_CPF(s) { 
    var i; 
    s = limpa_string(s); 
    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; 
  } // fim valida_CPF

  function valida_CGC(s) { 
    var i; 
    s = limpa_string(s); 
    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; 
  }  // fim valida_CGC

  function cobprop(){
    if (document.forms[0].chkEndCobranca.checked){
      document.forms[0].txtEndCobranca.value = document.forms[0].txtEndPropriedade.value;
      document.forms[0].txtCidadeCobranca.value = document.forms[0].txtCidadePropriedade.value;
      document.forms[0].drpUFCobranca.value = document.forms[0].drpUFPropriedade.value;
      document.forms[0].txtBairroCobranca.value = document.forms[0].txtBairroPropriedade.value;
      document.forms[0].txtCEPCobranca.value = document.forms[0].txtCEPPropriedade.value;
    }
    else {
      document.forms[0].txtEndCobranca.value = "";
      document.forms[0].txtCidadeCobranca.value = "";
      document.forms[0].drpUFCobranca.value = "";
      document.forms[0].txtBairroCobranca.value = "";
      document.forms[0].txtCEPCobranca.value = "";
    }
  }
  function entprop(){
    if (document.forms[0].chkEndEntregaPropriedade.checked){
      document.forms[0].chkEndEntregaCobranca.checked = false;
      document.forms[0].txtEndEntrega.value = document.forms[0].txtEndPropriedade.value;
      document.forms[0].txtCidadeEntrega.value = document.forms[0].txtCidadePropriedade.value;
      document.forms[0].drpUFEntrega.value = document.forms[0].drpUFPropriedade.value;
      document.forms[0].txtBairroEntrega.value = document.forms[0].txtBairroPropriedade.value;
      document.forms[0].txtCEPEntrega.value = document.forms[0].txtCEPPropriedade.value;
    }
    else {
      document.forms[0].txtEndEntrega.value = "";
      document.forms[0].txtCidadeEntrega.value = "";
      document.forms[0].drpUFEntrega.value = "";
      document.forms[0].txtBairroEntrega.value = "";
      document.forms[0].txtCEPEntrega.value = "";
    }
  }
  function entcob(){
    if (document.forms[0].chkEndEntregaCobranca.checked){
      document.forms[0].chkEndEntregaPropriedade.checked = false;
      document.forms[0].txtEndEntrega.value = document.forms[0].txtEndCobranca.value;
      document.forms[0].txtCidadeEntrega.value = document.forms[0].txtCidadeCobranca.value;
      document.forms[0].drpUFEntrega.value = document.forms[0].drpUFCobranca.value;
      document.forms[0].txtBairroEntrega.value = document.forms[0].txtBairroCobranca.value;
      document.forms[0].txtCEPEntrega.value = document.forms[0].txtCEPCobranca.value;
    }
    else {
      document.forms[0].txtEndEntrega.value = "";
      document.forms[0].txtCidadeEntrega.value = "";
      document.forms[0].drpUFEntrega.value = "";
      document.forms[0].txtBairroEntrega.value = "";
      document.forms[0].txtCEPEntrega.value = "";
    }
  }

  function sisbov(s){
    if (s.value.length > 0) {
      var num,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,d1;
      num = s.value.substr(0,14);
      s1 = num.substr(0,1);
      s2 = num.substr(1,1);
      s3 = num.substr(2,1);
      s4 = num.substr(3,1);
      s5 = num.substr(4,1);
      s6 = num.substr(5,1);
      s7 = num.substr(6,1);
      s8 = num.substr(7,1);
      s9 = num.substr(8,1);
      s10 = num.substr(9,1);
      s11 = num.substr(10,1);
      s12 = num.substr(11,1);
      s13 = num.substr(12,1);
      s14 = num.substr(13,1);
      d1 = s1*7+ s2*6+ s3*5+ s4*4+ s5*3+ s6*2+ s7*9+ s8*8+ s9*7+ s10*6+ s11*5+ s12*4+ s13*3+ s14*2;
      d1 = 11 - (d1 % 11);
	if ((d1 == 0) || (d1 > 9)){
		d1 = 0;
	}

      if (s.value.substr(0,3) != "105"){
        alert("Número SISBOV Incorreto.\nObs: Para solicitar apenas Aplicadores e Agulhas, entre em contato com um representante da ALLFLEX");
        s.focus();
        return;
      }

      if (d1 != s.value.substr(14,1)){
        alert("Número SISBOV Incorreto");
        s.focus();
        return;
      }
      if (document.forms[0].txtNumInicial.value != ""){
        if (document.forms[0].txtNumFinal.value != ""){
          document.forms[0].txtQdeBricos.value = document.forms[0].txtNumFinal.value.substr(0,14) - (document.forms[0].txtNumInicial.value.substr(0,14)-1);
          document.forms[0].txtQdeBricos.readOnly = true;
          document.forms[0].txtAplicadores.focus();
        }
      }
    }
    if (document.forms[0].txtNumInicial.value == ""){
      if (document.forms[0].txtNumFinal.value == ""){
          document.forms[0].txtQdeBricos.readOnly = false;
      }
    }
  }
  
function rastrear(x){
  document.rastreamento.p_codigo.value = x;
  document.rastreamento.submit();
}

function tracking(x){
  window.open('./alf1030.aspx?pedido=' + x + '&pop=1','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,menubar=no,width=630,height=450,top=50,left=50');
}

function abreTutorial(){
  window.open('./tutorial2.htm','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,menubar=no,width=630,height=450,top=100,left=100');
}

function caracterInvalido() {
if ((window.event.keyCode == 92) ||
    (window.event.keyCode == 34) ||
    (window.event.keyCode == 39) ||
    (window.event.keyCode == 42) ||
    (window.event.keyCode == 44) ||
    (window.event.keyCode == 46) ||
    (window.event.keyCode == 47) ||
    (window.event.keyCode == 58) ||
    (window.event.keyCode == 59) ||
    (window.event.keyCode == 60) ||
    (window.event.keyCode == 62) ||
    (window.event.keyCode == 63) ||
    (window.event.keyCode == 124)) {
	return false;
}
else
	return true;
}