var ie=(((navigator.userAgent.indexOf("MSIE")>-1)||(navigator.userAgent.indexOf("Mozilla/5.0")>-1))&&navigator.userAgent.indexOf("Opera")==-1?true:false);
var ns=(navigator.userAgent.indexOf("Netscape")>-1&&navigator.userAgent.indexOf("Opera")==-1?true:false);
//---------------

//#######################################################
// Caixa de confirmação                                //
//#######################################################
function ConfirmBox(msg,link) {
  if (confirm(msg)) {
    location.href=link;
  }
}

//#######################################################
// Caixa de confirmação                                //
//#######################################################
function ConfirmBox2(msg) {
  if (confirm(msg)) {
    return true;
  } else {
    return false;
  }
}

//#######################################################
// Valida Data                                         //
//#######################################################
function ValidarData(StrData,StrFormato) {
  var reDate1 = /^\d{1,2}\/\d{1,2}\/\d{1,4}$/;
  var reDate2 = /^[0-3]?\d\/[01]?\d\/(\d{2}|\d{4})$/;
  var reDate3 = /^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
  var reDate4 = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
  var reDate5 = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
  var reDate = reDate4;

  eval("reDate = reDate" + StrFormato);
  if (reDate.test(StrData.value)) {
    /*alert(pStr + " é uma data válida."); nao faz nada */
  } else if (StrData.value != null && StrData.value != "") {
    alert(StrData.value + " NÃO é uma data válida. Utilize o formato DD/MM/AAAA");
    StrData.value="";
    StrData.focus();
  }
}

//#######################################################
// Formata campos CPF ou CNPJ                          //
//#######################################################
function ajustar_cnpj_cpf(input) {
  var valor= input.value;
  var valorsemchar="";
  var valornovo="";
  //retira os pontos, traços e barras.
  for (var i=0;i<valor.length;i++) {
    if ((valor.charAt(i)!='.') && ( valor.charAt(i)!='-') && (valor.charAt(i)!='/')) {
      valorsemchar=valorsemchar+valor.charAt(i);
    }
  }
  //se for CPF...........
  if (valorsemchar.length==11) {
    for (var i=0;i<11;i++) {
      if ((i==3) || (i==6)) { valornovo=valornovo+'.'; }
      if (i==9) { valornovo=valornovo+'-'; }
      valornovo=valornovo+valorsemchar.charAt(i);
    }
    input.value=valornovo ;
  } else
    //se for CNPJ...........
    if (valorsemchar.length==14) {
      for (var i=0;i<14;i++) {
        if ((i==2) || (i==5)) { valornovo=valornovo+'.'; }
        if (i==8) { valornovo=valornovo+'/'; }
        if (i==12) { valornovo=valornovo+'-'; }
        valornovo=valornovo+valorsemchar.charAt(i);
      }
      input.value=valornovo;
    } else {
      //se não for nenhum dos dois..........
      input.value='';
      alert('CPF ou CNPJ inválido!');
    }
  return true;
}
																		
//#######################################################
// Formata campos CNPJ                                 //
//#######################################################
function formatar_cnpj_blur(input) {
  var valor=input.value;
  var valorsemchar="";
  var valornovo="";
  //retira os pontos, traços e barras.
  for (var i=0;i<valor.length;i++) {
    if ((valor.charAt(i)!='.') && ( valor.charAt(i)!='-') && (valor.charAt(i)!='/')) {
      valorsemchar=valorsemchar+valor.charAt(i);
    }
  }
  //se tiver 14 digitos...........
  if (valorsemchar.length==14) {
    for (var i=0;i<14;i++) {
      if ((i==2) || (i==5)) { valornovo=valornovo+'.'; }
      if (i==8) { valornovo=valornovo+'/'; }
      if (i==12) { valornovo=valornovo+'-'; }
      valornovo=valornovo+valorsemchar.charAt(i);
    }
    input.value=valornovo;
  } else {
    //se não tiver..........
    input.value='';
    alert('CNPJ inválido!');
  }
  return true;
}

//#######################################################
// Formata campos CNPJ                                 //
//#######################################################
function ajustar_cnpj(input, evento) {
  var BACKSPACE=8;
  var TAB=9;
  var DEL=46;
  var FRENTE=39;
  var TRAS=37;
  var HOME=36;
  var END=35;
  var tecla=(evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
  if ((tecla == BACKSPACE )||
      (tecla == DEL)||
      (tecla == FRENTE)||
      (tecla == TRAS)||
      (tecla == TAB)||
      (tecla == HOME)||
      (tecla == END)) { return true; }
  if (tecla == 13 ) { return false; }
  if ((tecla<48)||(tecla>57)) {
    evento.returnValue =false;
    return false;
  } else {
    if ((input.value.length==2)||(input.value.length==6)) { input.value=input.value + "." ; }
    else
    if (input.value.length==10) { input.value=input.value + "/" ; }
    else
    if (input.value.length==15) { input.value=input.value + "-" ; }
  }
  return true;
}

//#######################################################
// Formata campos CPF                                  //
//#######################################################
function formatar_cpf_blur(input) {
  var valor=input.value;
  var valorsemchar="";
  var valornovo="";
  //retira os pontos, traços e barras.
  for (var i=0;i<valor.length;i++) {
    if ((valor.charAt(i)!='.') && ( valor.charAt(i)!='-') && (valor.charAt(i)!='/')) {
      valorsemchar=valorsemchar+valor.charAt(i);
    }
  }
  //se tiver 11 digitos...........
  if (valorsemchar.length==11) {
    for (var i=0;i<11;i++) {
      if ((i==3) || (i==6)) { valornovo=valornovo+'.'; }
      if (i==9) { valornovo=valornovo+'-'; }
      valornovo=valornovo+valorsemchar.charAt(i);
    }
    input.value=valornovo ;
  } else {
    //se não tiver..........
    input.value='';
    alert('CPF inválido!');
  }
  return true;
}

//#######################################################
// Formata campos CPF                                  //
//#######################################################
function ajustar_cpf(input, evento) {
  var BACKSPACE=8;
  var TAB=9;
  var DEL=46;
  var FRENTE=39;
  var TRAS=37;
  var HOME=36;
  var END=35;
  var tecla=(evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
  if ((tecla == BACKSPACE )||
      (tecla == DEL)||
      (tecla == FRENTE)||
      (tecla == TRAS)||
      (tecla == TAB)||
      (tecla == HOME)||
      (tecla == END)) { return true; }
  if (tecla == 13 ) { return false; }
  if ((tecla<48)||(tecla>57)) {
    evento.returnValue =false;
    return false;
  } else {
    if((input.value.length==3)||(input.value.length==7)) {
      input.value=input.value + "." ;
    } else {
      if(input.value.length==11) {
        input.value=input.value + "-" ;
      }
    }
  }
  return true;
}
													      
//#######################################################
// Formata campos DATA                                 //
//#######################################################
function ajustar_data(input, evento) {
  var BACKSPACE=8;
  var TAB=9;
  var DEL=46;
  var FRENTE=39;
  var TRAS=37;
  var HOME=36;
  var END=35;
  var tecla=(evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
  if ((tecla == BACKSPACE )||
      (tecla == DEL)||
      (tecla == FRENTE)||
      (tecla == TRAS)||
      (tecla == TAB)||
      (tecla == HOME)||
      (tecla == END)) { return true; }
  if (tecla == 13) { return false; }
  if ((tecla<48)||(tecla>57)) {
    evento.returnValue =false;
    return false;
  } else {
    if ((input.value.length==2)||(input.value.length==5)) {
      input.value=input.value+"/"; }
    }
  return true;
}

//#######################################################
// Formata campos FONE                                 //
//#######################################################
function ajustar_fone(input, evento) {
  var BACKSPACE=8;
  var TAB=9;
  var DEL=46;
  var FRENTE=39;
  var TRAS=37;
  var HOME=36;
  var END=35;
  var tecla=(evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
  if ((tecla == BACKSPACE )||
      (tecla == DEL)||
      (tecla == FRENTE)||
      (tecla == TRAS)||
      (tecla == TAB)||
      (tecla == HOME)||
      (tecla == END)) { return true; }
  if (tecla == 13 ) { return false; }
  if ((tecla<48)||(tecla>57)) {
    evento.returnValue=false;
    return false;
  } else {
    if (input.value.length==0) { input.value="(" + input.value; }
    if (input.value.length==4) { input.value=input.value+")"; }
    if (input.value.length==5) { input.value=input.value+" "; }
    if (input.value.length==10){ input.value=input.value+"-"; }
  }
  return true;
}

//#######################################################
// Formata campos CEP                                  //
//#######################################################
function formatar_cep_blur(input) {
  var valor=input.value;
  var valorsemchar="";
  var valornovo="";
  //retira os pontos, traços e barras.
  for (var i=0;i<valor.length;i++) {
    if ((valor.charAt(i)!='.') && ( valor.charAt(i)!='-') && (valor.charAt(i)!='/')) {
      valorsemchar=valorsemchar+valor.charAt(i);
    }
  }
  //se tiver 8 digitos...........
  if (valorsemchar.length==8) {
    for (var i=0;i<8;i++) {
      if (i==5) { valornovo=valornovo+'-'; }
      valornovo=valornovo+valorsemchar.charAt(i);
    }
    input.value=valornovo ;
  } else {
  //se não tiver..........
    input.value='';
    alert('CEP inválido!');
  }
  return true;
}


//#######################################################
// Formata campos CEP                                  //
//#######################################################
function ajustar_cep(input, evento){
  var BACKSPACE=8;
  var TAB=9;
  var DEL=46;
  var FRENTE=39;
  var TRAS=37;
  var HOME=36;
  var END=35;
  var tecla=(evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
  if ((tecla == BACKSPACE )||
      (tecla == DEL)||
      (tecla == FRENTE)||
      (tecla == TRAS)||
      (tecla == TAB)||
      (tecla == HOME)||
      (tecla == END)) { return true; }
  if (tecla == 13) { return false; }
  if ((tecla<48)||(tecla>57)) {
    evento.returnValue=false;
    return false;
  } else {
//    if (input.value.length==2) { input.value=input.value+"."; }
    if (input.value.length==5) { input.value=input.value+"-"; }
  }
  return true;
}
									    
//#######################################################
// Aceita somente numeros                              //
//#######################################################
function somente_numeros(input, evento) {
  var tecla=(evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
  if ((tecla > 32 && tecla < 48) || tecla > 57) { return false; } else { return true; }
}
    
//#######################################################
// RETIRA O PONTO EM CAMPOS MONETARIOS                 //
//#######################################################
function Money_Focus(input) {
  stt=input.value;
  if (stt!='') {
    i=0;
    valor='';
    while(i < stt.length) {
      s=stt.charAt(i)
      if (s != '.') { valor+=s; }
      i++;
    }
    input.value=valor;
  }
}

//#######################################################
// COLOCA O PONTO EM CAMPOS MONETARIOS                 //
//#######################################################
function Money_Blur(input) {
  stt=input.value;
  if (stt=='') { return false; }
  if (stt.length==1) { input.value='0.0'+stt; }
  if (stt.length==2) { input.value='0.'+stt; }
  if (stt.length>2) {
    inteiros=stt.slice(0,stt.length-2); //tudo menos os 2 ultimos numeros.
    centavos=stt.slice(stt.length-2); //soh os 2 ultimos numeros
    valor=inteiros+'.'+centavos;
    input.value=valor;
  }
}									

//#######################################################
// Para campos monetarios                              //
//#######################################################
function somente_money(input, evento) {
  var BACKSPACE=8;
  var TAB=9;
  var DEL=46;
  var FRENTE=39;
  var TRAS=37;
  var HOME=36;
  var END=35;
  var PONTO=190;
  var tecla=(evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
  if ((tecla == BACKSPACE )||
      (tecla == DEL)||
      (tecla == TAB)||
      (tecla == FRENTE)||
      (tecla == TRAS)||
      (tecla == HOME)||
      (tecla == END)||
      (tecla == PONTO)) { return true; }
  if ( tecla == 13 ) return false;
  //verifica qualquer outra tecla que nao seja numero.
  if ((tecla<48)||(tecla>57)) {
    evento.returnValue=false;
    return false;
  }
  return true;
}


//#######################################################
// Zoomifyer                                           //
//#######################################################
function Zoomify(picture, title) {
  Zoom = window.open("jaragua/zoomify.php?picture="+picture+"&title="+title, "Zoom", "width=764,height=520,resizable=no,toolbar=no,menubar=no,scrollbars=no");
}

//#######################################################
// Funcão para abrir popup com fotos.                  //
//#######################################################
function AbrirAlbum(album) {
  Album = window.open("Fotos/album.php?album="+album, "Album", "width=650,height=657,resizable=no,toolbar=no,menubar=no,scrollbars=no");
}

//#######################################################
// Funcão para abrir popup com fotos.                  //
//#######################################################
function AbrirAlbum2(album) {
  Album = window.open("Fotos/album2.php?album="+album, "Album", "width=710,height=601,resizable=no,toolbar=no,menubar=no,scrollbars=no");
}

//#######################################################
// Funcão para abrir popup com foto do piloto.         //
//#######################################################
function FotoPiloto(codigo) {
  Piloto = window.open("fotopiloto.php?codigo="+codigo, "Piloto", "width=160,height=207,resizable=no,toolbar=no,menubar=no,scrollbars=no");
}

function ValidateFormLogin() {
  campo=document.formLogin.login.value;
  if (campo=='') {
    alert('Informe seu Login.');
    return false;
  }
  campo=document.formLogin.senha.value;
  if (campo=='') {
    alert('Informe sua senha.');
    return false;
  }
  document.formLogin.submit();
}

function SubmitFormLogin() {
  ValidateFormLogin();
}

function ValidateFormLogout() {
  document.formLogout.submit();
}

function SubmitFormLogout() {
  ValidateFormLogout();
}

function ValidateFormEnquete() {
  document.formEnquete.submit();
}

function SubmitFormEnquete() {
  ValidateFormEnquete();
}

//#######################################################
// VALIDA CPF E CNPJ                                   //
//#######################################################
function ValidarCPF(cpf) {
  var numeros, digitos, soma, i, resultado, digitos_iguais;
  digitos_iguais = 1;
  if (cpf.value.length < 11) {
    alert('CPF Inválido.');
    cpf.value='';
    return false;
  }
  for (i = 0; i < cpf.value.length - 1; i++)
    if (cpf.value.charAt(i) != cpf.value.charAt(i + 1)) {
       digitos_iguais = 0;
       break;
    }
  if (!digitos_iguais) {
    numeros = cpf.value.substring(0,9);
    digitos = cpf.value.substring(9);
    soma = 0;
    for (i = 10; i > 1; i--)
      soma += numeros.charAt(10 - i) * i;
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(0)) {
      alert('CPF inválido.');
      cpf.value='';
      return false;
    }
    numeros = cpf.value.substring(0,10);
    soma = 0;
    for (i = 11; i > 1; i--)
      soma += numeros.charAt(11 - i) * i;
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(1)) {
      alert('CPF inválido.');
      cpf.value='';
      return false;
    }
    return true;
  } else {
    alert('CPF inválido');
    cpf.value='';
    return false;
  }
}

function ValidarCPF2(cpf) {
  var numeros, digitos, soma, i, resultado, digitos_iguais;
  digitos_iguais = 1;
  if (cpf.length < 11) return false;
  for (i = 0; i < cpf.length - 1; i++)
    if (cpf.charAt(i) != cpf.charAt(i + 1)) {
       digitos_iguais = 0;
       break;
    }
  if (!digitos_iguais) {
    numeros = cpf.substring(0,9);
    digitos = cpf.substring(9);
    soma = 0;
    for (i = 10; i > 1; i--)
      soma += numeros.charAt(10 - i) * i;
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(0)) return false;
    numeros = cpf.substring(0,10);
    soma = 0;
    for (i = 11; i > 1; i--)
      soma += numeros.charAt(11 - i) * i;
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(1)) return false;
    return true;
  } else return false;
}

function ValidarCNPJ(cnpj) {
  var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
  digitos_iguais = 1;
  if (cnpj.length < 14 && cnpj.length < 15) return false;
  for (i = 0; i < cnpj.length - 1; i++)
    if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
      digitos_iguais = 0;
      break;
    }
  if (!digitos_iguais) {
    tamanho = cnpj.length - 2
    numeros = cnpj.substring(0,tamanho);
    digitos = cnpj.substring(tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2) pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(0)) return false;
    tamanho = tamanho + 1;
    numeros = cnpj.substring(0,tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2) pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(1)) return false;
    return true;
  } else return false;
}


