
function el(id) {
  return document.getElementById(id);
}


function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft;
    curtop  = obj.offsetTop;
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
  }
  return [curleft,curtop];
}


function tirarenters(vary) {
  var re1 = /[\r\n]/g;
  var re2 = /'/g;
  var varx = vary.replace(re1,'');
  //varx = varx.replace(re2,'\\\'');
  return varx;
}

function titar_aspas(strx) {
  try {
    if (strx != '') {
      var re1 = /[\']/g;
      var varx = strx.replace(re1,'\\\'');
      return varx;
    }
  }
  catch(e) {}
  return '';
}

function ajax_get_into(urlX, where, postprocess, orientacao, noblock, div, loading, loadtype) {
  var aj_xmlhttp  = false;
  var aj_resposta = '';
  var aj_nomefunc = '';
  var destination = '';
  var veil = false;
  var noblock = ((typeof(noblock) == "undefined") || (noblock === "") ) ? false : true;
  var div = ((typeof(div) == "undefined") || (div === "") ) ? 'loading_top' : div;
  var loading = ((typeof(loading) == "undefined") || (loading === "") ) ? true : loading;
  var loadtype = ((typeof(loadtype) == "undefined") || (loadtype === "") ) ? "" : loadtype;

  function drawit(ajX) {
    try {
      if (ajX == '0') {
        window.location = '/?sair=1';
      }
      else {
        var destx = document.getElementById(destination);
        if (testaRetorno(ajX)==false) return false;
        if (destx) destx.innerHTML = ajX;
        if (aj_nomefunc && (aj_nomefunc != '')) eval(aj_nomefunc + '();');
      }
    }
    catch (e) { alert(e); }
  }

  function ajax_resposta() {
    try {
      if ( aj_xmlhttp.readyState == 4 ) {
        aj_resposta = aj_xmlhttp.responseText;
        
        loading_stop(noblock, div,loading);
        
        try {
          temp = tirarenters(aj_resposta);
          drawit(temp);
          //loginScreeSaver();
        }
        catch(exc) { alert(exc); }
      }
    }
    catch(e) { alert('ERRO 2: ' + e); }
  }

  try {
    if ( !aj_xmlhttp ) {
      //clearInterval(intervalLogin);
      destination = where;
      aj_nomefunc = postprocess;
      loading_start(loadtype,"", noblock, false, div, loading);

      /*@cc_on @*/
      // MICROSOFT EXPLORER
      /*@if (@_jscript_version >= 5)
      // JScript gives us Conditional compilation, we can cope with old IE versions.
      // and security blocked creation of the objects.
      try {
        aj_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
        try {
          aj_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(1)');
        }
      }
      @end @*/

      if ( typeof XMLHttpRequest != 'undefined' ) {
        // MOZILLA
        try {
          aj_xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(2)');
        }
      }
      
      if ( window.createRequest ) {
        // ZAFARI
        try {
          aj_xmlhttp = window.createRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(3)');
        }
      }

      if ( aj_xmlhttp ) {
        aj_resposta = '';
        try {
          var dateObj = new Date();
          var curtime = dateObj.getTime();
          urlX += '&tstamprandajx91=' + curtime;
          aj_xmlhttp.onreadystatechange = ajax_resposta;
          aj_xmlhttp.open("GET",urlX,true);
          aj_xmlhttp.send(null);
        }
        catch(e) {
          alert(e.description + '\n' + e.message);
        }
      }
    }
    // end IF global
  }
  catch(e) {
    alert(e.description + '\n' + e.message);
  }
}


function ajax_get(urlX, where, postprocess, noblock, div, loading, loadtype) {
  var aj_xmlhttp  = false;
  var aj_resposta = '';
  var aj_nomefunc = '';
  var destination = '';
  var veil = false;
  var noblock = ((typeof(noblock) == "undefined") || (noblock === "") ) ? false : true;
  var div = ((typeof(div) == "undefined") || (div === "") ) ? 'loading_top' : div;
  var loading = ((typeof(loading) == "undefined") || (loading === "") ) ? true : loading;
  var loadtype = ((typeof(loadtype) == "undefined") || (loadtype === "") ) ? "processing" : loadtype;

  
  function drawit(ajX) {
    try {
      if (ajX == '0') {
        window.location = '/?sair=1';
      }
      else {
        var destx = document.getElementById(destination);
        if (testaRetorno(ajX)==false) return false;
        if (destx) {
          if (is_ie()) {
            var aux = document.createElement('div');
            aux.innerHTML = ajX;
            if (destx.lastChild) {
              destx.removeChild(destx.lastChild);
            }
            destx.appendChild(aux);
          } else {
            destx.innerHTML = ajX;
          }
        }
        try{
    		  var chamada = aj_nomefunc + '(\''+ ajX + '\');';
          if (aj_nomefunc && (aj_nomefunc != '')) eval( chamada );
        }catch(e){
          if (aj_nomefunc && (aj_nomefunc != '')) eval(aj_nomefunc + '();');
        }
      }
      try { 
        loading_stop(noblock, div,loading);
      }catch(ee) { 
        alert(ee); 
      }
    }
    catch (e) { alert(e); }
  }

  function debug() {
    try {
      if ( aj_xmlhttp.readyState == 4 ) {
        aj_resposta = aj_xmlhttp.responseText;
        try {
          temp = tirarenters(aj_resposta);
          drawit(temp);
          //eval( aj_nomefunc + '(\'' + temp + '\')');
        }
        catch(exc) { alert(exc); }
      }
    }
    catch(e) { alert('ERRO: ' + e); }
  }

  function ajax_resposta() {
    try {
      if ( aj_xmlhttp.readyState == 4 ) {
        aj_resposta = aj_xmlhttp.responseText;
        loading_stop(noblock, div,loading);
        try {
          temp = tirarenters(aj_resposta);
          //loginScreeSaver();
          drawit(temp);
          //eval( aj_nomefunc + '(\'' + temp + '\')');
        }
        catch(exc) { alert(exc); }
      }
    }
    catch(e) { alert('ERRO 2: ' + e); }
  }

  try {
    if ( !aj_xmlhttp ) {
      //clearInterval(intervalLogin);
      destination = where;
      aj_nomefunc = postprocess;
      loading_start(loadtype,"",noblock,"",div,loading);

      if ( typeof XMLHttpRequest != 'undefined' ) {
        // MOZILLA
        try {
          aj_xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(2)');
        }
      }
      
      if ( window.createRequest ) {
        // ZAFARI
        try {
          aj_xmlhttp = window.createRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(3)');
        }
      }

      if ( aj_xmlhttp ) {
        aj_resposta = '';
        try {
          var dateObj = new Date();
          var curtime = dateObj.getTime();
          urlX += '&tstamprandajx91=' + curtime;
          aj_xmlhttp.onreadystatechange = ajax_resposta;
          aj_xmlhttp.open("GET",urlX,true);
          aj_xmlhttp.send(null);
        }
        catch(e) {
          alert(e.description + '\n' + e.message);
        }
      }
    } // end IF global
  }
  catch(e) {
    alert(e.description + '\n' + e.message);
  }
}


function ajax_post(urlX, where, formX, postprocess, noblock, div, loading, loadtype) {
  var aj_xmlhttp  = false;
  var aj_resposta = '';
  var aj_nomefunc = '';
  var destination = '';
  var veil = false;
  var noblock = ((typeof(noblock) == "undefined") || (noblock === "") ) ? false : true;
  var div = ((typeof(div) == "undefined") || (div === "") ) ? 'loading_top' : div;
  var loading = ((typeof(loading) == "undefined") || (loading === "") ) ? true : loading;
  var loadtype = ((typeof(loadtype) == "undefined") || (loadtype === "") ) ? "" : loadtype;


  function drawit(ajX) {
    try {
      try { loading_stop(noblock, div,loading); }
      catch(ee) { alert(ee); }
      if (ajX == '0') {
        window.location = '/?sair=1';
      }
      else {
        var destx = document.getElementById(destination);
        if (testaRetorno(ajX)==false) {
          return false;
        }
        if (destx) {
          if (is_ie()) {
            var aux = document.createElement('div');
            aux.innerHTML = ajX;
            while (destx.lastChild) {
              destx.removeChild(destx.lastChild);
            }
            destx.appendChild(aux);
          } else {
            destx.innerHTML = ajX;
          }
        }
        if (aj_nomefunc && (aj_nomefunc != '')) {
          if (aj_nomefunc.substr(aj_nomefunc.length-1,1)==";") {
            eval(aj_nomefunc);
          } else {
	        var ajX2 = titar_aspas(ajX);
	        eval(aj_nomefunc + '(\'' + ajX2 + '\');');
	      }
        }
      }
    }
    catch (e) { alert(e); }
  }

  function debug() {
    try {
      if ( aj_xmlhttp.readyState == 4 ) {
        aj_resposta = aj_xmlhttp.responseText;
        try {
          temp = tirarenters(aj_resposta);
          drawit(temp);
          //eval( aj_nomefunc + '(\'' + temp + '\')');
        }
        catch(exc) { alert(exc); }
      }
    }
    catch(e) { alert('ERRO: ' + e); }
  }

  function ajax_resposta() {
    try {
      if ( aj_xmlhttp.readyState == 4 ) {
        aj_resposta = aj_xmlhttp.responseText;
		    loading_stop(noblock, div,loading);
        try {
          temp = tirarenters(aj_resposta);
          //loginScreeSaver();
          drawit(temp);
          //eval( aj_nomefunc + '(\'' + temp + '\')');
        }
        catch(exc) { alert(exc); }
      }
    }
    catch(e) { alert('ERRO 2: ' + e); }
  }

  try {
    if ( !aj_xmlhttp ) {
      //clearInterval(intervalLogin);
      aj_nomefunc = postprocess;
      destination = where;
      //loading_start(status, msg, noblock, stop, div, loading)
      loading_start("","",noblock,"",div, loading);
			
      if ( typeof XMLHttpRequest != 'undefined' ) {
        // MOZILLA
        try {
          aj_xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(2)');
        }
      }
      
      if ( window.createRequest ) {
        // ZAFARI
        try {
          aj_xmlhttp = window.createRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(3)');
        }
      }

      if ( aj_xmlhttp ) {
        aj_resposta = '';
        var parameters = '';
        var theForm = document.getElementById(formX);
        var valx = '';
        if (theForm) {
          for(i=0; i < theForm.elements.length; i++) {
            if ( (theForm.elements[i].type == 'radio') || (theForm.elements[i].type == 'checkbox') ) {
              valx = getCheckedValue(theForm.elements[i]);
              if (valx != '') parameters = parameters + '&' + theForm.elements[i].name + '=' + escape(valx);
            }
            else {
              valx = theForm.elements[i].value;
              parameters = parameters + '&' + theForm.elements[i].name + '=' + escape(valx);
            }
          }
        }
        try {
          var dateObj = new Date();
          var curtime = dateObj.getTime();
          urlX += '&tstamprandajx91=' + curtime;

          aj_xmlhttp.onreadystatechange = ajax_resposta;
          aj_xmlhttp.open("POST",urlX,true);
          aj_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          aj_xmlhttp.setRequestHeader("Content-length", parameters.length);
          aj_xmlhttp.setRequestHeader("Connection", "close");
          aj_xmlhttp.send(parameters);
        }
        catch(e) {
          alert(e.description + '\n' + e.message);
        }
      }
    } // end IF global
  }
  catch(e) {
    alert(e.description + '\n' + e.message);
  }
}


// SYNCHRONOUS AJAX ou SJAX

function simple_sjax_post(urlX, where, formX, noblock, div, loading, loadtype) {
  var aj_xmlhttp  = false;
  var aj_resposta = '';
  var aj_nomefunc = '';
  var destination = '';
  var veil = false;
  var retorno = false;
  var noblock = ((typeof(noblock) == "undefined") || (noblock === "") ) ? false : true;
  var div = ((typeof(div) == "undefined") || (div === "") ) ? 'loading_top' : div;
  var loading = ((typeof(loading) == "undefined") || (loading === "") ) ? true : loading;
  var loadtype = ((typeof(loadtype) == "undefined") || (loadtype === "") ) ? "processing" : loadtype;

    
  function ajax_resposta(aj_resposta) {
    var ret = false;
    try {
      temp = tirarenters(aj_resposta);
      //loginScreeSaver();
      ret = isNaN(temp) || (temp < 1) ? false : temp;
    }
    catch(e) { alert('ERRO 2: ' + e); }
    return ret;
  }

  try {
    if ( !aj_xmlhttp ) {
      //clearInterval(intervalLogin);
      destination = where;
      loading_start(loadtype,"",noblock, "", div, loading);
//      loading_start("","",noblock);
      
      if ( typeof XMLHttpRequest != 'undefined' ) {
        // MOZILLA
        try {
          aj_xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(2)');
        }
      }
      
      if ( window.createRequest ) {
        // ZAFARI
        try {
          aj_xmlhttp = window.createRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(3)');
        }
      }

      if ( aj_xmlhttp ) {
        aj_resposta = '';
        var parameters = '';
        var theForm = document.getElementById(formX);
        var valx = '';
        if (theForm) {
          for(i=0; i < theForm.elements.length; i++) {
            if ( (theForm.elements[i].type == 'radio') || (theForm.elements[i].type == 'checkbox') ) {
              valx = getCheckedValue(theForm.elements[i]);
              if (valx != '') parameters = parameters + '&' + theForm.elements[i].name + '=' + escape(valx);
            }
            else {
              valx = theForm.elements[i].value;
              parameters = parameters + '&' + theForm.elements[i].name + '=' + escape(valx);
            }
          }
        }
        try {
          var dateObj = new Date();
          var curtime = dateObj.getTime();
          urlX += '&tstamprandajx91=' + curtime;
          //urlX = trim(urlX);
          //urlX = urlX.replace(/'/g,'');
          //alert(urlX);
          aj_xmlhttp.open("POST",urlX,false);
          aj_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          aj_xmlhttp.setRequestHeader("Content-length", parameters.length);
          aj_xmlhttp.setRequestHeader("Connection", "close");
          aj_xmlhttp.send(parameters);
//          retorno = ajax_resposta(aj_xmlhttp.responseText);
          retorno = aj_xmlhttp.responseText;
        }
        catch(e) {
          alert(e.description + '\n' + e.message);
        }
      }
      loading_stop(noblock,div,loading);
    } // end IF global
  }
  catch(e) {
    alert(e.description + '\n' + e.message);
  }

  return retorno;
}


function sjax_post(urlX, where, parX, noblock, div, loading, loadtype) {
  var aj_xmlhttp  = false;
  var aj_resposta = '';
  var aj_nomefunc = '';
  var destination = '';
  var veil = false;
  var retorno = false;
  var noblock = ((typeof(noblock) == "undefined") || (noblock === "") ) ? false : true;
  var div = ((typeof(div) == "undefined") || (div === "") ) ? 'loading_top' : div;
  var loading = ((typeof(loading) == "undefined") || (loading === "") ) ? true : loading;
  var loadtype = ((typeof(loadtype) == "undefined") || (loadtype === "") ) ? "processing" : loadtype;

  function ajax_resposta(aj_resposta) {
    var ret = false;
    try {
      temp = tirarenters(aj_resposta);
      //loginScreeSaver();
      ret = isNaN(temp) || (temp < 1) ? false : temp;
    }
    catch(e) { alert('ERRO 2: ' + e); }
    return ret;
  }

  try {
    if ( !aj_xmlhttp ) {
      //clearInterval(intervalLogin);
      destination = where;
      loading_start(loadtype,"",noblock, "", div, loading);

      if ( typeof XMLHttpRequest != 'undefined' ) {
        // MOZILLA
        try {
          aj_xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(2)');
        }
      }
      
      if ( window.createRequest ) {
        // ZAFARI
        try {
          aj_xmlhttp = window.createRequest();
        }
        catch (e) {
          aj_xmlhttp = false;
          alert('Erro ao conectar-se(3)');
        }
      }

      if ( aj_xmlhttp ) {
        aj_resposta = '';
        try {
          var dateObj = new Date();
          var curtime = dateObj.getTime();
          urlX += '&tstamprandajx91=' + curtime;
          aj_xmlhttp.open("POST",urlX,false);
          aj_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          aj_xmlhttp.setRequestHeader("Content-length",parX.length);
          aj_xmlhttp.setRequestHeader("Connection", "close");
          if (parX==""||parX==undefined) parX=null;
          aj_xmlhttp.send(parX);
          retorno = aj_xmlhttp.responseText;
        }
        catch(e) {
          alert(e.description + '\n' + e.message);
        }
      }
      loading_stop(noblock,div,loading);
    } // end IF global
  }
  catch(e) {
    alert(e.description + '\n' + e.message);
  }

  return retorno;
}




//************* FIM AJAX **************


