/**
  * Classe JavaScript para manipulação de requisições assíncronas
  * @author    Danilo Akamine <danilowz@gmail.com>
  * @see	   danilow.wordpress.com
  * @version   1.0
*/

Ajax = function() { }

Ajax.instance = null;
Ajax.processo = [];
Ajax.contador = 0;

Ajax.getInstance = function()
{
  if (this.instance == null)
  {
    try {
      this.instance = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
      try {
        this.instance = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (ex) {
        try {
          this.instance = new XMLHttpRequest();
        }
        catch (exc) {
          alert('Erro ao instanciar objeto Ajax.');
          this.instance = null;
        }
      }
    }
  }
  return this.instance;
}

Ajax.doPost = function(url, parametros, callback)
{
  this.processo[this.processo.length] = [url, parametros, callback];
  if ( (this.contador + 1) == this.processo.length)
  {
    Ajax.execute();
  }
}

Ajax.execute = function()
{
  this.getInstance().open("POST", this.processo[this.contador][0], true);
  this.getInstance().setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  this.getInstance().setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  this.getInstance().setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
  this.getInstance().setRequestHeader("Pragma", "no-cache");
  this.getInstance().send(this.processo[this.contador][1]);
  this.getInstance().onreadystatechange = this.processo[this.contador][2];
}

Ajax.next = function()
{
  this.contador++;
  if (this.contador < this.processo.length)
  {
    setTimeout("Ajax.execute()", 20);
  }
}

