function amplia(img, imprimir){
  var ptr = "";
  if(imprimir!=null && imprimir!='undefined' && imprimir)
    ptr += "&print=true";
  window.open("ampliaImg.php?foto="+img+ptr,"","width=1,height=1,top=0,left=0")
  void(0);
}

//usada em todas as páginas
//tirando a barra de rolagem do iframe a adicionando ao body da pagina pai
function ajustarAltura(){
  var obj;
  var obj2;
  var altura;
  obj = parent.document.getElementById("centro");
  if(obj == null){ //testa se a página visitada está no iframe
    window.location = 'index.php';
  } else {
    obj2 = document.getElementById("TabelaPrincipal");
    altura = 300; //altura minima para o iframe
    if(obj2.scrollHeight < altura){
     obj.height = altura;
    } else {
      obj.height = obj2.scrollHeight;
    }
  }
  //parent.window.scrollTo(0, 0);
  window.status = "OAB RP | Ordem dos Advogados do Brasil | Secção de Ribeirão Preto";
  mudaFoco();
}

function mudaFoco(){
  parent.focus();
}

function tecla(e){
  var code;

  // Obtém o evento. No caso do Firefox, este
  // evento é passado como argumento, e no caso do IE,
  // deve ser obtido através do objeto window.
  if (!e)
    var e = window.event; 

  // Detecta o target da tecla
  var targ;
  if (e.target)
    targ = e.target;
  else if (e.srcElement)
    targ = e.srcElement;

  // Este código previne um erro do navegador Safari:
  // Se o usuario clica num DIV com texto, os outros browsers
  // retornam o DIV como sendo o target. Safari retorna  o nó contendo
  // o texto (nodeType 3). Nesse caso, o target que nos interessa é o pai.
  if (targ.nodeType == 3) // defeat Safari bug
     targ = targ.parentNode;

  // Obtém o nome da TAG HTML do target do evento
  tag = targ.tagName.toUpperCase();

  // Verifica se o evento não esta sendo acionado em nenhum
  // campo como campo de texto e combobox.
  // Esta verificação é importante, pois o handler pode bloquear
  // o funcionamento adqueado desses campos (por exemplo, em vez de escrever
  // a letra no campo, executa uma função).
  if (tag == "INPUT" || tag == "SELECT" || tag == "TEXTAREA")
    return;

  // Detecta o codigo da tecla
  if (e.keyCode)
    code = e.keyCode;
  else if (e.which)
    code = e.which;

  var character = String.fromCharCode(code);

  //Seta para cima ou para baixo ou pgup ou pgdown
  if(code == 38 || code == 40 || code == 33 || code == 34)
    mudaFoco();
  //alert(code);
}

window.onload = ajustarAltura;
document.onkeydown = tecla;

function txtBoxFormat(objeto, sMask, evtKeyPress) {
     var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

     if(document.all) { // Internet Explorer
       nTecla = evtKeyPress.keyCode; }
     else if(document.layers) { // Nestcape
       nTecla = evtKeyPress.which;
     }

     sValue = objeto.value;

     // Limpa todos os caracteres de formatação que
     // já estiverem no campo.
     sValue = sValue.toString().replace( "-", "" );
     sValue = sValue.toString().replace( "-", "" );
     sValue = sValue.toString().replace( ".", "" );
     sValue = sValue.toString().replace( ".", "" );
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( ":", "" );
     sValue = sValue.toString().replace( ":", "" );
     sValue = sValue.toString().replace( "(", "" );
     sValue = sValue.toString().replace( "(", "" );
     sValue = sValue.toString().replace( ")", "" );
     sValue = sValue.toString().replace( ")", "" );
     sValue = sValue.toString().replace( " ", "" );
     sValue = sValue.toString().replace( " ", "" );
     fldLen = sValue.length;
     mskLen = sMask.length;

     i = 0;
     nCount = 0;
     sCod = "";
     mskLen = fldLen;

     while (i <= mskLen) {
       bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
       bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

       if (bolMask) {
         sCod += sMask.charAt(i);
         mskLen++; }
       else {
         sCod += sValue.charAt(nCount);
         nCount++;
       }

       i++;
     }

     objeto.value = sCod;

     if (nTecla != 8) { // backspace
       if (sMask.charAt(i-1) == "9") { // apenas números...
         return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
       else { // qualquer caracter...
         return true;
       } }
     else {
       return true;
     }
}

function mudaFocoBySize(objOrigem, objDestino, intSize, e){
  //se a tecla for tab ou shift faz nada
  if(e!=null && document.all && (e.keyCode == 9 || e.keyCode == 16)) // Internet Explorer
    return true;
  else if(e!=null && document.layers && (e.keyCode == 9 || e.keyCode == 16)) // Nestcape
    return true;

  if(objOrigem.value.length == intSize)
    objDestino.focus();
  else if(objOrigem.value.length > intSize){
    objOrigem.value = objOrigem.value.substring(0, intSize);
    objDestino.focus();
  }
  return true;
}

function number_format(obj, onlyInteger){
  var newValue = '';
  var curChar;
  var valid = /^[0-9]$/;
  var isDotted = false;
  var oi = onlyInteger == null ? false : onlyInteger;
  for(var i = 0; i < obj.value.length; i++){
    curChar = obj.value.substring(i, i+1);
    if((curChar == ',' || curChar == '.') && !isDotted && !oi){
      newValue += '.';
      isDotted = true;
    } else if(valid.test(curChar))
      newValue += curChar;
  }
  if(!isDotted && newValue.length > 0 && !oi)
    newValue += ".00";
  obj.value = newValue;
}

function printPage(){
    window.print();
}

function mailPage(id, tipo, others){
  var janLarg = 310;
  var janAlt = 285;
  var largura = screen.width;
  var altura = screen.height;
  var xPos = (largura - janLarg) / 2;
  var yPos = (altura - janAlt) / 2;
  var c = "";
  if(others!='undefined' && others!=null)
    c = "&" + others;

  self.focus();


  window.open("mail.this.php?id="+id+"&tipo="+tipo+c,"mailpage","width="+janLarg+",height="+janAlt+",top="+yPos+",left="+xPos);
  void(0);
}

function LTrim(value) {  
  var re = /\s*((\S+\s*)*)/;
  return value.replace(re, "$1");
}

function RTrim(value) {
  var re = /((\s*\S+)*)\s*/;
  return value.replace(re, "$1");
}

function trim(value) {
  return LTrim(RTrim(value));
}

//funcao de formatacao monetaria (correios)
function mformat(obj, tammax, e){
  if (!e)
    var e = window.event; 

  var tecla;

  if (e.keyCode)
    tecla = e.keyCode;
  else if (e.which)
    tecla = e.which;

  if((tecla < 96 || tecla > 105) && tecla!=8 && tecla!=9 && tecla!=16 && tecla!=35 && tecla!=36 && tecla!=37 && tecla!=39){
    return false;
  }

  vr = obj.value;
  vr = vr.replace( "/", "" );
  vr = vr.replace( "/", "" );
  vr = vr.replace( ",", "" );
  vr = vr.replace( ",", "" );
  vr = vr.replace( ".", "" );
  vr = vr.replace( ".", "" );
  vr = vr.replace( ".", "" );
  vr = vr.replace( ".", "" );
  //Replaces adicionais
  //vr = vr.replace( "-", "" );
  //vr = vr.replace( "+", "" );
  //vr = vr.replace( "*", "" );
  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 ){
      obj.value = vr;
    }

    if ( (tam > 2) && (tam <= 5) ){
      obj.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam );
    }

    if ( (tam >= 6) && (tam <= 8) ){
      obj.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
    }

    if ( (tam >= 9) && (tam <= 11) ){
      obj.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) ){
      obj.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) ){
      obj.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
    }
  }
  return true;
}

function mformatBlur(obj){
  var newValue = '';
  var curChar;
  var valid = /^[0-9]$/;
  var isDotted = false;
  for(var i = 0; i < obj.value.length; i++){
    curChar = obj.value.substring(i, i+1);
    if((curChar == ',') && !isDotted){
      newValue += '.';
      isDotted = true;
    } else if(valid.test(curChar))
      newValue += curChar;
  }
  obj.value = newValue;
}

function mformatFocus(obj){
  var obj2 = new Object();
  obj2.value = obj.value;
  number_format(obj2, true);


  var vr = obj2.value;
  tam = vr.length;
  
  if ( tam <= 2 ){
    obj.value = vr;
  }

  if ( (tam > 2) && (tam <= 5) ){
    obj.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam );
  }

  if ( (tam >= 6) && (tam <= 8) ){
    obj.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
  }

  if ( (tam >= 9) && (tam <= 11) ){
    obj.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) ){
    obj.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) ){
    obj.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
  }

  /*if(obj.value.length > 0)
    obj.select();*/
}
