var c = '';
var depurar = 'off';
onload=onLoadMain;
function onLoadMain(){
  try{loadBeforeLoad();}catch(e){(depurar=="on"?showCatch(e):null);}
  try{loadAfterLoad();}catch(e){(depurar=="on"?showCatch(e):null);}
}

function getContent(Url, Param, Content){
  document.getElementById('theiframe').src='c.php?'+Url+'&o='+Content+'&'+Param;
}

function showCatch(e){
  var t='';
  for(i in e){
    t += i+' = '+e[i]+'\n';
  }
  alert(t);
}

function writeArray(ar){
  var t='';
  for(i in ar){
    t += i+' = '+ar[i]+'<br />';
  }
  document.body.innerHTML = t;
}

function getFloat(v){
  v = v.toString();
  var s = 0;
  s = parseFloat(getDecimal(v));
  if(isNaN(parseFloat(s))) s = 0;
  return s;
}

function getOnlyNumber(v){
  var s = '';
  for(i=0; i<v.length; i++){
    if(v.charAt(i)>=0 && v.charAt(i)<=9 || v.charAt(i)=="-"){
      s += v.charAt(i);
    }
  }
  return s;
}
//alert(getOnlyNumber('0a1b2c3m4k5j6l7i8u9'));

/**
@author Edivaldo Merlo Stens
@version 1.0
@date 19 jan 2007
@description Retorna data formatada. Deve entrar no formato dd/mm/aaaa ou dd/mm/aa. Ex.: formatDate('14/01/1981', 'BR'); ou formatDate('14/011981', 'BR');
*/
function formatDate(v, p) {
  if(!p)p='BR';
  p = p.toUpperCase();
  var s = '';
  v = getOnlyNumber(v);
  if(v.length==6 || v.length==8){
    if(p=='BR')
    s = v.substring(0, 2)+"/"+v.substring(2, 4)+"/"+v.substring(4, v.length);
    else if(p=='US')
    s = v.substring(2, 4)+"/"+v.substring(0, 2)+"/"+v.substring(4, v.length);
    else if(p=='SQL')
    s = v.substring(4, v.length)+"-"+v.substring(2, 4)+"-"+v.substring(0, 2);
  }
  return s;
}

/**
@author Edivaldo Merlo Stens
@version 1.0
@date 19 jan 2007
@description Valida data. Deve entrar no formato ddmmaaaa ou ddmmaa.
*/
function validDate(v) {
  var sit = true;
  dia = v.substring(0, 2);
  mes = v.substring(3, 5);
  ano = v.substring(6, v.length);

  // verifica o dia valido para cada mes
  if ((dia < 01) || (dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) {
    sit = false;
  }

  // verifica se o mes e valido
  if (mes < 01 || mes > 12 ) {
    sit = false;
  }

  // verifica se e ano bissexto
  if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) {
    sit =  false;
  }
  return sit;
}

function returnValidDate(v) {
  v.value=formatDate(v.value);
  if(!validDate(v.value)){
    alert('Data invalida!');
    v.select();
  }
}
//alert(formatDate('14/01/2006', 'sql'));

function formatMoney(v){
  v = getDecimal(v);
  v = v.toString().replace(/\$|\,/g,'');
  if(isNaN(v)) v = "0";
  cents = Math.floor((v*100+0.5)%100);
  v = Math.floor((v*100+0.5)/100).toString();
  if(cents < 10) cents = "0" + cents;
  for (var i = 0; i < Math.floor((v.length-(1+i))/3); i++)
  v =	v.substring(0,v.length-(4*i+3))+'.'+v.substring(v.length-(4*i+3));
  return (v + ',' + cents);
}

function getDecimal(v){
  var s = "";
  v = v.toString();
  var va = v.split(",");
  if(va.length>1)
  s = v.replace( ".", "" ).replace( ",", "." );
  else
  s = v;
  return parseFloat(s);
}

function isInt(str){
  var i = parseInt (str);

  if (isNaN (i))
  return false;

  i = i . toString ();
  if (i != str)
  return false;

  return true;
}

function reverse(v) {
  var s = '';
  for(j=v.length; j>=0; j--)
  s += v.charAt(j);
  return s;
}

function noNumbers(e){
  var keynum
  var keychar
  var numcheck

  // IE
  if(window.event){
    keynum = e.keyCode
  }
  // Netscape/Firefox/Opera
  else if(e.which){
    keynum = e.which
  }

  keychar = String.fromCharCode(keynum)
  numcheck = /\d/
  return !numcheck.test(keychar)
}
/*
function getAjaxSelect(url, obj){
//var ld = new showLoading(document.body);

var aj = new ajax();

aj.onLoading = function(req){
//ld.showCancel(req);
}
aj.onError = function(req){
alert('Erro: ['+req.status+'] '+ req.statusText +'\n\n'+unescape(req.responseText));
//ld.hide();
}
aj.onLoad = function(req){
obj.innerHTML='';
rn = unescape(req.responseText).split("\n");
for(i=1; i<(rn.length-1); i++){
var rt = rn[i].split("|");
var op = document.createElement("OPTION");
op.value=rt[0];
op.selected=(rn[0]==rt[0]);
op.innerText=(!rt[1]?rt[0]:rt[1]);
obj.appendChild(op);
}
}
aj.load(url, null, "GET", 'text/plain');
}
*/
function getAjaxSelect(urlcomplemento, obj){
  var ld = new showLoading(document.body);

  var aj = new ajax();

  aj.onLoading = function(req){
    //ld.showCancel(req);
  }
  aj.onError = function(req){
    alert('Erro: ['+req.status+'] '+ req.statusText +'\n\n'+unescape(req.responseText));
    ld.hide();
  }
  aj.onLoad = function(req){
    var txt = req.responseText.split("@@ERROR:");
    if(txt[1])
    alert('ERRO INTERNO!\n'+txt[1].replace("@@END", ""));
    else{
      var txt = req.responseText.split("@@END");
      if(txt[1]!=';')
      alert('ERRO INTERNO!\nNÃO FOI ENCONTRADO INSTRUÇÃO DE FINALIZAÇÃO.\nProvalvelmente seu comando não pode ser processado completamente.\n\nRETORNO DO ARQUIVO:\n'+req.responseText);
      else{
        rn = txt[0].split("\n");
        obj.innerHTML='';
        for(i=1; i<(rn.length-1); i++){
          var rt = rn[i].split("|");
          var op = document.createElement("OPTION");
          op.value=rt[0];
          op.selected=(rn[0]==rt[0]);
          op.innerText=(!rt[1]?rt[0]:rt[1]);
          obj.appendChild(op);
        }
      }
    }
    ld.hide();
  }
  aj.load('ajax.php?'+c+'&AJAX_CONTENT_TYPE=text/plain&AJAX_DEBUG=on&'+urlcomplemento, null, "GET", 'text/plain');
}

function setAjaxVoid(urlcomplemento){
  var ld = new showLoading(document.body);

  var aj = new ajax();
  aj.onLoading = function(req){
    //ld.showCancel(req);
  }
  aj.onError = function(req){
    alert('Erro: ['+req.status+'] '+ req.statusText +'\n\n'+unescape(req.responseText));
    ld.hide();
  }
  aj.onLoad = function(req){
    var txt = req.responseText.split("@@ERROR:");
    if(txt[1])
    alert('ERRO INTERNO!\n'+txt[1].replace("@@END", ""));
    else{
      var txt = req.responseText.split("@@END");
      if(txt[1]!=';')
      alert('ERRO INTERNO!\nNÃO FOI ENCONTRADO INSTRUÇÃO DE FINALIZAÇÃO.\nProvalvelmente seu comando não pode ser processado completamente.\n\nRETORNO DO ARQUIVO:\n'+req.responseText);
    }
    ld.hide();
  }
  aj.load('ajax.php?'+c+'&AJAX_CONTENT_TYPE=text/plain&AJAX_DEBUG=on&'+urlcomplemento, null, "GET", 'text/plain');
}

function isKeySearch(e){
  //event.keyCode==13 || event.keyCode==32 || event.keyCode==8 || event.keyCode==46 || (event.keyCode>=65 && event.keyCode<=90) || (event.keyCode>=48 && event.keyCode<=57)
  var k = e.keyCode;
  return (k!=192 && k!=93 && k!=91 && k!=45 && k!=44 && k!=19 && k!=27 && k!=20 && k!=18 && k!=17 && k!=16 && k!=9 && !(k>=33 && k<=40) && !(k>=112 && k<=123));
}

function aumentaH(idObj, hmax, execBefore, execAfter){
  if(execBefore)
  eval(execBefore);

  var obj = document.getElementById(idObj);
  hmax = parseFloat(hmax);
  var hfalta = (hmax - parseFloat(obj.style.height));
  var hatual = (parseFloat(obj.style.height))+Math.ceil(hfalta/4);
  //alert('aumentaH: '+obj.style.height);
  if(hatual<hmax){
    obj.style.height = hatual+'px';
    document.body.scrollTop = document.body.scrollHeight;
    setTimeout("aumentaH('"+idObj+"', "+hmax+", null, \""+execAfter+"\")", 18);
  } else{
    obj.style.height = hmax+'px';
    if(execAfter)
    eval(execAfter);
  }
}

function diminuiH(idObj, hmin, execBefore, execAfter){
  if(execBefore)
  eval(execBefore);

  var obj = document.getElementById(idObj);
  hmin = parseFloat(hmin);
  //var hfalta = (obj.offsetHeight-hmin);
  var hatual = ((parseFloat(obj.style.height)>0?parseFloat(obj.style.height):obj.offsetHeight) - hmin);
  //alert('diminuiH: '+obj.style.height);
  hatual = (hatual)-Math.ceil(hatual/4);
  if(hatual>hmin){
    obj.style.height = hatual+'px';
    document.body.scrollTop = document.body.scrollHeight;
    setTimeout("diminuiH('"+idObj+"', "+hmin+", null, \""+execAfter+"\")", 18);
  } else{
    obj.style.height = hmin+'px';
    if(execAfter)
    eval(execAfter);
  }
}

function setStyleDisplay(o,v){
  if(!v)v='';
  document.getElementById(o).style.display=v;
}

function setStyleOverflow(oId, v){
  if(!v)v='';
  document.getElementById(oId).style.overflow=v+'';
}

/*
function diminuiH(idObj, hmin, execAfter){
var obj = document.getElementById(idObj);
hmin = parseFloat(hmin);
var hfalta = (hmin - obj.offsetHeight);
alert(hfalta);
var hatual = (obj.offsetHeight)-Math.ceil(hfalta/4);
if(hatual<=0 || isNaN(hatual))hatual = 1;
alert(hatual);
if(hatual>hmin){
obj.style.height = hatual+'px';
document.body.scrollTop = document.body.scrollHeight;
setTimeout("diminuiH('"+idObj+"', "+hmin+", \""+execAfter+"\")", 18);
} else{
obj.style.display='none';
if(execAfter)
setTimeout(execAfter, 0);
}
}
*/
