function abre_janela_boleto(width, height, nome, scrollbar){
  var top; var left;
  top = ( (screen.height/2) - (height/2) )
  left = ( (screen.width/2) - (width/2) )
  window.open('',nome,'width='+width+',height='+height+',scrollbars='+scrollbar+',toolbar=yes,location=no,status=no,menubar=no,resizable=no,left='+left+',top='+top);
}
function abre_janela(width, height, nome, scrollbar){
  var top; var left;
  top = ( (screen.height/2) - (height/2) )
  left = ( (screen.width/2) - (width/2) )
  window.open('',nome,'width='+width+',height='+height+',scrollbars='+scrollbar+',toolbar=no,location=no,status=no,menubar=no,resizable=no,left='+left+',top='+top);
}
function mygetElement(id) {
  if (document.getElementById) {
    return document.getElementById(id);
  }
  else if (document.all) {
    return document.all[id];
  }
  else if(document.layers) {
    return document[id];
  }
}
(function(){
//Evita o refresh dos backgrounds no IE
var  m = document.uniqueID
&& document.compatMode
&& !window.XMLHttpRequest
&& document.execCommand ;
try{if(!!m){m("BackgroundImageCache", false, true)}}catch(oh){};
})();
function fieldFocus(obj){
  obj.className='fieldFocus';
}
function fieldBlur(obj){
  obj.className='field';
}
function FormataValor(objeto,teclapres,tammax,decimais)  {
  if(window.event) {
    var tecla = teclapres.keyCode;
  }
  else if(teclapres.which) {
    var tecla = teclapres.which;
  }
  var tamanhoObjeto = objeto.value.length;
  if ((tecla == 8) && (tamanhoObjeto == tammax)) {
    tamanhoObjeto = tamanhoObjeto - 1 ;
  }
  if (( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) && ((tamanhoObjeto+1) <= tammax)) {
    vr = objeto.value;
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( ",", "" );
    vr = vr.replace( ".", "" );
    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 > 1)) {
      tam = tam - 1 ;
      vr = objeto.value;
      vr = vr.replace( "/", "" );
      vr = vr.replace( "/", "" );
      vr = vr.replace( ",", "" );
      vr = vr.replace( ".", "" );
      vr = vr.replace( ".", "" );
      vr = vr.replace( ".", "" );
      vr = vr.replace( ".", "" );
    }
    if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
      if (decimais > 0) {
        if ( (tam <= decimais) ) objeto.value = ("0," + vr) ;
        if( (tam == (decimais + 1)) && (tecla == 8)) objeto.value = vr.substr( 0, (tam - decimais)) + ',' + vr.substr( tam - (decimais), tam ) ;
        if ( (tam > (decimais + 1)) && (tam <= (decimais + 3)) &&  ((vr.substr(0,1)) == "0")) objeto.value = vr.substr( 1, (tam - (decimais+1))) + ',' + vr.substr( tam - (decimais), tam ) ;
        if ( (tam > (decimais + 1)) && (tam <= (decimais + 3)) &&  ((vr.substr(0,1)) != "0")) objeto.value = vr.substr( 0, tam - decimais ) + ',' + vr.substr( tam - decimais, tam ) ;
        if ( (tam >= (decimais + 4)) && (tam <= (decimais + 6)) ) objeto.value = vr.substr( 0, tam - (decimais + 3) ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
        if ( (tam >= (decimais + 7)) && (tam <= (decimais + 9)) ) objeto.value = vr.substr( 0, tam - (decimais + 6) ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
        if ( (tam >= (decimais + 10)) && (tam <= (decimais + 12)) ) objeto.value = vr.substr( 0, tam - (decimais + 9) ) + '.' + vr.substr( tam - (decimais + 9), 3 ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
        if ( (tam >= (decimais + 13)) && (tam <= (decimais + 15)) ) objeto.value = vr.substr( 0, tam - (decimais + 12) ) + '.' + vr.substr( tam - (decimais + 12), 3 ) + '.' + vr.substr( tam - (decimais + 9), 3 ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
      }
      else if(decimais == 0) {
        if ( tam <= 3 ) objeto.value = vr ;
        if ( (tam >= 4) && (tam <= 6) ) {
          if(tecla == 8) {
            objeto.value = vr.substr(0, tam);
            teclapres.cancelBubble = true;
            teclapres.returnValue = false;
          }
          objeto.value = vr.substr(0, tam - 3) + '.' + vr.substr( tam - 3, 3 ); 
        }
        if ( (tam >= 7) && (tam <= 9) ) {
          if(tecla == 8) {
            objeto.value = vr.substr(0, tam);
            teclapres.cancelBubble = true;
            teclapres.returnValue = false;
          }
          objeto.value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ); 
        }
        if ( (tam >= 10) && (tam <= 12) ) {
          if(tecla == 8) {
            objeto.value = vr.substr(0, tam);
            teclapres.cancelBubble = true;
            teclapres.returnValue = false;
          }
          objeto.value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ); 
        }
        if ( (tam >= 13) && (tam <= 15) ) {
          if(tecla == 8) {
            objeto.value = vr.substr(0, tam);
            teclapres.cancelBubble = true;
            teclapres.returnValue = false;
          }
          objeto.value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ) ;
        }			
      }
    }
  }
  else if((teclapres.keyCode != 8) && (teclapres.keyCode != 9) && (teclapres.keyCode != 13) && (teclapres.keyCode != 35) && (teclapres.keyCode != 36) && (teclapres.keyCode != 46)) {
    return false;
  }
}
function onlyNumbers(e) {
  if(window.event) {
    key = e.keyCode;
  }
  else if(e.which) {
    key = e.which;
  }
  if (key!=8 || key < 48 || key > 57) return (((key > 47) && (key < 58)) || (key==8));
  {
    return true;
  }
}
function formatar(src, mask, ev, tipo) {
  var result = false;
  if (tipo == 'numero'){
    result = onlyNumbers(ev);
  }
  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);
  }
  return result;
}
function checa_cpf(numero) {
  var erro = 0;
  var msg = 1;
  var campo = numero;
  while(InStr(campo, '-') > 0){ campo = campo.replace("-", "") };
  while(InStr(campo, '.') > 0){ campo = campo.replace(".", "") };
  if (campo.length != 11 || campo == "00000000000" || campo == "11111111111" ||
    campo == "22222222222" ||	campo == "33333333333" || campo == "44444444444" ||
    campo == "55555555555" || campo == "66666666666" || campo == "77777777777" ||
    campo == "88888888888" || campo == "99999999999")
    msg = erro;
  soma = 0;
  for (y=0; y < 9; y ++)
    soma += parseInt(campo.charAt(y)) * (10 - y);
  resto = 11 - (soma % 11);
  if (resto == 10 || resto == 11)resto = 0;
  if (resto != parseInt(campo.charAt(9)))
    msg = erro; soma = 0;
  for (y = 0; y < 10; y ++)
    soma += parseInt(campo.charAt(y)) * (11 - y);
  resto = 11 - (soma % 11);
  if (resto == 10 || resto == 11) resto = 0;
  if (resto != parseInt(campo.charAt(10)))
    msg = erro;
  return msg;
}
function formata_data(evt, campo){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
var mData =campo.value
mData = mData.replace("/","")
if (mData.length == 2){
  var mDay = mData.substr(0,2);
  campo.value = mDay+"/";
}
else if (mData.length == 4){
  var mDay = mData.substr(0,2);
  var mMonth = mData.substr(2,2);
  campo.value = mDay+"/"+mMonth+"/";
}
return true;
}
function verifica_data(campo) {
var v_campo = campo.value;
while(InStr(v_campo, '_') > 0){ v_campo = v_campo.replace("_", "") };
while(InStr(v_campo, '/') > 0){ v_campo = v_campo.replace("/", "") };
if(v_campo==''){
  return false;
}
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = campo.value;
matchArray = campo.value.match(datePat);
if (matchArray == null) {
  alert("Informe o dia, mês e ano como abaixo:\nEx: 01/01/1999");
  campo.select();
  return false;
}
month = matchArray[3];
day = matchArray[1];
year = matchArray[5];
if (month < 1 || month > 12) {
  alert("Mês "+month+" não existe.");
  campo.select();
  return false;
}
if (day < 1 || day > 31) {
  alert("Dia "+day+" não existe.");
  campo.select();
  return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
  alert("O mês "+month+" não possui 31 dias.")
  campo.select();
  return false;
}
if (month == 2) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
  alert("Fevereiro de " + year + " não possui " + day + " dias.");
  campo.select();
  return false;
}
}
return true;
}
function checa_cnpj(numero) {
  var erro = 0;
  var msg = 1;
  var campo = numero;
  while(InStr(campo, '.') > 0){ campo = campo.replace(".", "") };
  while(InStr(campo, '/') > 0){ campo = campo.replace("/", "") };
  while(InStr(campo, '-') > 0){ campo = campo.replace("-", "") };
  var y;
  var c = campo.substr(0,12);
  var dv = campo.substr(12,2);
  var d1 = 0;
  for (y = 0; y < 12; y++) {
    d1 += c.charAt(11-y)*(2+(y % 8));
  }
  if (d1 == 0) msg = erro;
  d1 = 11 - (d1 % 11);
  if (d1 > 9) d1 = 0;
  if (dv.charAt(0) != d1)msg = erro;
  d1 *= 2;
  for (y = 0; y < 12; y++) {
    d1 += c.charAt(11-y)*(2+((y+1) % 8));
  }
  d1 = 11 - (d1 % 11);
  if (d1 > 9) d1 = 0;
  if (dv.charAt(1) != d1) msg = erro;
  return msg;
}
function checa_cep(numero){
  var msg = 0;
  var campo = numero;
  while(InStr(campo, ' ') > 0){ campo = campo.replace(" ", "") };
  while(InStr(campo, '-') > 0){ campo = campo.replace("-", "") };
  campo = parseFloat(campo);
  if (campo >= 01000000 && campo <= 99999000) msg = 1;
  return msg;
}
function InStr(n, s1, s2){
var numargs=InStr.arguments.length;	
if(numargs<3)
  return n.indexOf(s1)+1;
else
  return s1.indexOf(s2, n)+1;
}
