/*  Adaptable ajax library for making custom calls on the fly.
 *  Allows for declaration of ajax parameters and postprocess methods at runtime
 *  Created by Pablo de Benito. Give credit, you thieving scumbags! xD
 *  The only requisite is that the AjaxHandler object is named 'ajax' 
 */

//Constructor
function AjaxHandler()
{
  this.ajaxObject = null;
  try
  {
    this.ajaxObject = new XMLHttpRequest();
  }
  catch(e)
  {
    try
    {
      this.ajaxObject = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      this.ajaxObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  this.method = "standard";
  this.response = "";
  this.parametersToSend = new Array();
  this.methods = new Array();
  
  //declaration of a postprocess method.
  this.methods["standard"] = function()
  {
    //do stuff on sucessful call
  }
}
//various variables
AjaxHandler.prototype.request = '';
AjaxHandler.prototype.status = 'idle';
AjaxHandler.prototype.ajaxObject = null;
AjaxHandler.prototype.responseText;
AjaxHandler.prototype.responseXML;
AjaxHandler.prototype.method;
AjaxHandler.prototype.technique = 'asynchronous';
//dynamic parameter list
AjaxHandler.prototype.parametersToSend = null;

//launch the call on the specified url
AjaxHandler.prototype.makeStandardRequest = function(url)
{
  var async = false;
  url += "&u=" + new Date().getTime();
  
  if (this.technique == 'asynchronous')
    async = true;
  
  this.ajaxObject.open("POST", url, async);
  
  try
  {
    this.ajaxObject.send(null);
  }
  catch(e)
  {
    
  }
  if (async==true)
  {
    this.ajaxObject.onreadystatechange = function()
    {
      var state = ajax.ajaxObject.readyState;
      if (state == 4)
      {
        try
        {
          ajax.responseText = ajax.ajaxObject.responseText;
          ajax.responseXML = ajax.ajaxObject.responseXML;
          ajax.processResponse();
        }
        catch(e)
        {
          
        }
        return true;
      }
    }
  }
  else
  {
    this.responseText = this.ajaxObject.responseText;
    this.responseXML = this.ajaxObject.responseXML;
    this.processResponse();
  }
  this.clearParameters();
}

//we call the function that corresponds to 'method'
AjaxHandler.prototype.processResponse = function()
{
  this.methods[this.method]();
}

//launch ajax call using a custom GET url with all parameteres appended
AjaxHandler.prototype.makeCustomRequest = function(url)
{
  var parameters = "";
  for (var i=0; i<this.parametersToSend.length; i++)
  {
    v = this.parametersToSend[i];
    if (parameters == "")
      parameters += "?"+v[0]+"="+v[1];
    else
      parameters += "&"+v[0]+"="+v[1];
  }
  this.makeStandardRequest(url+parameters);
}

//set the postprocess method
AjaxHandler.prototype.setProcessMethod = function(m)
{
  this.method = m;
}

//adds a parameter p with value v to the parameter list
AjaxHandler.prototype.setParameter = function(p,v)
{
  var temp = new Array(p,v);
  this.parametersToSend.push(temp);
}

//clear parameter list
AjaxHandler.prototype.clearParameters = function()
{
  for (var v in this.parametersToSend)
  {
    delete v;
    v = undefined;
  }
  this.parametersToSend = new Array();
}
//use this variable in your script, like ajax.setParameter(whatever) and stuff
var ajax = new AjaxHandler();