/* ----- */

$ = function (id) {
  return document.getElementById(id);
}

jQuery(document).ready(function($) {
    $('.formato_telefone').mask('(99)9999-9999');
});

/* ----- */

function div(_id) {
  if ($(_id).style.display == "none") {
    $(_id).style.display = '';
  }
  else {
    $(_id).style.display = 'none';
  }
}

/* ----- */

// Formatar Telefone
function formatarTelefone (Campo,e) { 
  var key = '';
  var len = 0;
  var strCheck = '0123456789';
  var aux = '';
  var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13 || whichCode == 8 || whichCode == 0) { return true; /*Enter backspace ou FN qualquer um que no seja alfa numerico*/ }
  key = String.fromCharCode(whichCode);
  if (strCheck.indexOf(key) == -1) { return false; /*no  vlido*/ }
  aux =  formatarTelefoneRemove(Campo.value);
  len = aux.length;
  if (len >= 10) { return false; /*impede de digitar um telefone maior que 10*/ }
  aux += key;
  Campo.value = formatarTelefoneMont(aux);
  return false;
}

function formatarTelefoneMont (Telefone) {
  var aux = len = '';
  len = Telefone.length;
  if (len <= 9) { tmp = 5; }
  else { tmp = 6; }
  aux = '';
  for(i = 0; i < len; i++) {
    if (i == 0) { aux = '('; }
    aux += Telefone.charAt(i);
    if (i+1 == 2) { aux += ')'; }
    if (i+1 == tmp) { aux += '-'; }
  }
  return aux;
}

function formatarTelefoneRemove (Telefone) {
  var strCheck = '0123456789';
  var len = i = aux = '';
  len = Telefone.length;
  for (i = 0; i < len; i++) {
    if (strCheck.indexOf(Telefone.charAt(i))!=-1) {
      aux += Telefone.charAt(i);
    }
  }
  return aux;
}
// -----

// Formatao para qualquer mscara 
function Formatar (src,mask) {
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
  if (texto.substring(0,1) != saida) {
    src.value += texto.substring(0,1);
  }
}
// -----

// Formatar Data
function formatarData(campo,teclapress) {
  if (somenteNumero.apply(campo, [teclapress]) == false) return false;
  var tecla = teclapress.keyCode;  
  vr = campo.value;
  vr = vr.replace(".","");
  vr = vr.replace("/","");
  vr = vr.replace("/","");
  tam = vr.length + 1;
  if (tecla != 9 && tecla != 8) {
    if (tam > 2 && tam < 5) {
      campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
    }
    if (tam >= 5 && tam <= 10) {
      campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
    }
  }
}
// -----

// Funo que Formata Valor
// Modificado por Anderson Böger Roveda - Testando no IE e FF
// Uso: onkeydown="formatarValor(this,20,event);"
function formatarValor(valor,tammax,event) {
  var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  vr = valor.value;
  vr = vr.replace("/","");
  vr = vr.replace("/","");
  vr = vr.replace(",","");
  vr = vr.replace(".","");
  vr = vr.replace(".","");
  vr = vr.replace(".","");
  vr = vr.replace(".","");
  if (vr.length > 0) { vr = parseInt(vr,10); }
  vr = '' + vr
  tam = vr.length;
  if (tam < tammax && tecla != 8) { tam = vr.length + 1 ; }
  if (tecla == 8 ) { tam = tam - 1; }
  if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) {
    if (tam <= 2 ) {
      if (tam == 1) { valor.value = "0,0" + vr ; }
      else if (tam == 2) { valor.value = "0," + vr ; }
      else { valor.value = "" ; }
    }
    if ((tam > 2) && (tam <= 5)) { valor.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
    if ((tam >= 6) && (tam <= 8)) { valor.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
    if ((tam >= 9) && (tam <= 11)) { valor.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
    if ((tam >= 12) && (tam <= 14)) { valor.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
    if ((tam >= 15) && (tam <= 17)) { valor.value = vr.substr( 0, tam - 15 ) + '.' + vr.substr( tam - 15, 3 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + ',' + vr.substr( tam - 8, 0 ) ; }
  }
}
// -----

// Formatar CEP
function formatarCEP (objeto) {
  if (objeto.value.indexOf("-") == -1 && objeto.value.length > 5) objeto.value = "";
  if (objeto.value.length == 5) objeto.value += "-";
}
// -----

// Somente Numero
function somenteNumero(e, dec) {
    /*
       versão antiga e bugada
  var key = '';
  var strCheck = '0123456789';
  var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
  key = String.fromCharCode(whichCode);
  if (strCheck.indexOf(key) == -1) return false;
  */
    // nova versão (caiomoritz@gmail.com) em 16/09/2009
    // fonte: http://www.htmlcodetutorial.com/forms/index_famsupp_158.html
    var myfield = this;
    var key;
    var keychar;

    if (window.event)
       key = window.event.keyCode;
    else if (e)
       key = e.which;
    else
       return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key==null) || (key==0) || (key==8) || 
        (key==9) || (key==13) || (key==27) )
       return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
       return true;

    // decimal point jump
    else if (dec && (keychar == "."))
       {
       myfield.form.elements[dec].focus();
       return false;
       }
    else
       return false;
}

/* ----- */

function curriculumDisplay (id) {  
  $('curriculum1').style.display = 'none';
  $('curriculum2').style.display = 'none';
  $('curriculum3').style.display = 'none';
  $('curriculum4').style.display = 'none';
  $('curriculum5').style.display = 'none';
  $('curriculum'+id).style.display = 'block';

  $('curriculumLink1').className = '';
  $('curriculumLink2').className = '';
  $('curriculumLink3').className = '';
  $('curriculumLink4').className = '';
  $('curriculumLink5').className = '';
  $('curriculumLink'+id).className = 'selecionado';
}
/* ----- */

function imprimir (_w, _h, _src) {
  if (_w == 0) {
    _w = screen.width;
  }
  if (_h == 0) {
    _h = screen.height;
  }
  var _left = (screen.width - _w) / 2;
  altura = ((screen.height - _h) / 2) - 50;
  var _top = (altura > 0 ? altura : 0);
  win = window.open(_src, "Imprimir", 'width='+_w+', height='+_h+', top='+_top+', left='+_left+', toolbar=yes, location=no, status=no, menubar=yes, scrollbars=yes, resizable=no');
  if (parseInt(navigator.appVersion) >= 4) {
    win.window.focus();
  }
}

/* Open Window */
function OpenWindow (URL,Name,W,H,Scroll) {
  var WinLeft = (screen.width - W) / 2;
  var WinTop = (screen.height - H) / 2;
  WinProps = 'width='+W+',height='+H+',top='+WinTop+',left='+WinLeft+',scrollbars='+Scroll+',toolbar=0,location=0,directories=0,status=0,menuBar=0,resizable=0';
  Win = window.open(URL,Name,WinProps); 
  if (parseInt(navigator.appVersion) >= 4) {
    Win.window.focus();
  }
}
/* ----- */

function noticias_indicar (_id) {
  OpenWindow('?pg=noticias_indicar&id='+_id,'',350,380,0);
}
