
var AjaxCommunicatorConstants = {
                                  /* httpRequest.readyState == ?? */
                                  READY_STATE_UNINITIALISED : 0,
                                  READY_STATE_LOADING : 1,
                                  READY_STATE_LOADED : 2,
                                  READY_STATE_INTERACTIVE : 3,
                                  READY_STATE_COMPLETED : 4
                                };

function AjaxCommunicator()
{
  this.DEBUG = false;
  this.httpRequest = this.getHttpRequest();
  this.setAsynchronous(true);
  this.setMethod("POST");
  this.username = "";
  this.password = "";
  this.clearParameters();
}

AjaxCommunicator.prototype.isSupportedByBrowser = function()
{
  if (this.httpRequest && this.httpRequest != null)
  {
    return true;
  }
  return false;
}

AjaxCommunicator.prototype.getHttpRequest = function()
{
  if (window.XMLHttpRequest) 
  {
    return new XMLHttpRequest();
  } 
  else if (window.ActiveXObject) 
  {
    var versions = ["Msxml2.XMLHTTP.7.0", 
                    "Msxml2.XMLHTTP.6.0", 
                    "Msxml2.XMLHTTP.5.0", 
                    "Msxml2.XMLHTTP.4.0", 
                    "MSXML2.XMLHTTP.3.0", 
                    "MSXML2.XMLHTTP",
                    "Microsoft.XMLHTTP"];
    for (var i=0; i<versions.length; i++)
    {
      try 
      {
        var request = new ActiveXObject(versions[i]);
        if (request) 
        {
          return request;
        }
      }
      catch (activeException) 
      {
        // alert(activeException + "=" + versions[i]);
      } 
    }
  }
  return null;
}

AjaxCommunicator.prototype.invoke = function(parameters,onSuccess,onError)
{
  var ajaxCommunicatorGlobal = this;
  var parametersGlobal = parameters;
  var onSuccessGlobal = onSuccess;
  var onErrorGlobal = onError;
  
  // create an anonymous function to log state changes
  this.httpRequest.onreadystatechange = function() 
  {
    var httpRequest = ajaxCommunicatorGlobal.httpRequest;
    if (httpRequest.readyState == AjaxCommunicatorConstants.READY_STATE_UNINITIALISED)
    {
      if (this.DEBUG == true)
      {
        alert("Uninitialised - 0: The request is uninitialized (before you've called open()).");
      }
    }
    else if (httpRequest.readyState == AjaxCommunicatorConstants.READY_STATE_LOADING)
    {
      if (this.DEBUG == true)
      {
        alert("Loading - 1: The request is set up, but hasn't been sent (before you've called send()).");
      }
    }
    else if (httpRequest.readyState == AjaxCommunicatorConstants.READY_STATE_LOADED)
    {
      if (this.DEBUG == true)
      {
        alert("Loaded - 2: The request was sent and is being processed (you can usually get content headers from the response at this point).");
      }
    }
    else if (httpRequest.readyState == AjaxCommunicatorConstants.READY_STATE_INTERACTIVE)
    {
      if (this.DEBUG == true)
      {
        alert("Interactive - 3: The request is being processed; often some partial data is available from the response, but the server hasn't finished with its response.");
      }
    }
    else if (httpRequest.readyState == AjaxCommunicatorConstants.READY_STATE_COMPLETED)
    {
      if (this.DEBUG == true)
      {
        alert("Completed - 4: The response is complete; you can get the server's response and use it.");
      }
      // OK status
      if (httpRequest.status == 200)
      {
        if (onSuccessGlobal &&
            onSuccessGlobal != null &&
            onSuccessGlobal != "")
        {
          eval(onSuccessGlobal + "(parametersGlobal,ajaxCommunicatorGlobal,httpRequest.responseText,httpRequest.responseXML);");
        }
      }
      // Error status
      else
      {
        if (httpRequest.status == 401)
        {
          alert("Ajax - Unauthorized request, please check username and password: " + ajaxCommunicatorGlobal.getUrl());
        }
        else if (httpRequest.status == 403)
        {
          alert("Ajax - Forbidden request: " + ajaxCommunicatorGlobal.getUrl());
        }
        else if (httpRequest.status == 404)
        {
          alert("Ajax - Request URL does not exist: " + ajaxCommunicatorGlobal.getUrl());
        }
        if (onErrorGlobal &&
            onErrorGlobal != null &&
            onErrorGlobal != "")
        {
          eval(onErrorGlobal + "(parametersGlobal,ajaxCommunicatorGlobal,httpRequest.responseText,httpRequest.responseXML);");
        }
      }
    }
  }

  var content = "";
  if (this.getMethod() == "POST")
  {
    this.httpRequest.open("POST",
                          this.getUrl(),
                          this.getAsynchronous(),
                          this.getUsername(),
                          this.getPassword());
    // this.httpRequest.setRequestHeader("Connection", "close");
    this.httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var index = 0;
    for (var tag in this.parameters)
    {
      if (index == 0)
      {
        content = tag + "=" + this.encode(this.parameters[tag]);
      }
      else
      {
        content += "&" + tag + "=" + this.encode(this.parameters[tag]);
      }
      index++;
    }
    this.httpRequest.send(content);
  }
  else
  {
    var index = 0;
    for (var tag in this.parameters)
    {
      if (index == 0)
      {
        content = tag + "=" + this.encode(this.parameters[tag]);
      }
      else
      {
        content += "&" + tag + "=" + this.encode(this.parameters[tag]);
      }
      index++;
    }
    this.httpRequest.open("GET",
                          this.getUrl() + "?" + content,
                          this.getAsynchronous(),
                          this.getUsername(),
                          this.getPassword());
    this.httpRequest.setRequestHeader("Connection", "close");
    this.httpRequest.send(null);
  }
  // but you can also send HEAD requests. 
  // alert(request.getAllResponseHeaders());
}

AjaxCommunicator.prototype.abortInvoke = function()
{
  this.httpRequest.abort();
}

AjaxCommunicator.prototype.getMethod = function()
{
  return this.method;
}
AjaxCommunicator.prototype.setMethod = function(value)
{
  this.method = value;
}

AjaxCommunicator.prototype.getAsynchronous = function()
{
  return this.asynchronous;
}
AjaxCommunicator.prototype.isAsynchronous = function()
{
  return this.asynchronous;
}
AjaxCommunicator.prototype.setAsynchronous = function(value)
{
  this.asynchronous = value;
}

AjaxCommunicator.prototype.getParameters = function()
{
  return this.parameters;
}
AjaxCommunicator.prototype.getParameter = function(name)
{
  for (var tag in this.parameters)
  {
    if (tag == name)
    {
      return this.parameters[tag];
    }
  }
  return "";
}
AjaxCommunicator.prototype.isParameterExists = function(name)
{
  for (var tag in this.parameters)
  {
    if (tag == name)
    {
      return true;
    }
  }
  return false;
}
AjaxCommunicator.prototype.setParameter = function(name,value)
{
  this.parameters[name] = value;
}
AjaxCommunicator.prototype.clearParameters = function(value)
{
  this.parameters = new Array();
}

AjaxCommunicator.prototype.encode = function(value)
{
  if (encodeURIComponent)
  {
    return encodeURIComponent(value);
  }
  else if (escape)
  {
    return escape(value);
  }
  return value;
}
AjaxCommunicator.prototype.decode = function(value)
{
  value = value.replace(/\+/g,' ');
  if (decodeURIComponent)
  {
    return decodeURIComponent(value);
  }
  if (unescape)
  {
    return unescape(value);
  }
  return value;
}

AjaxCommunicator.prototype.getUsername = function()
{
  return this.username;
}
AjaxCommunicator.prototype.setUsername = function(value)
{
  this.username = value;
}

AjaxCommunicator.prototype.getPassword = function()
{
  return this.password;
}
AjaxCommunicator.prototype.setPassword = function(value)
{
  this.password = value;
}

AjaxCommunicator.prototype.getUrl = function()
{
  return this.url;
}
AjaxCommunicator.prototype.setUrl = function(value)
{
  this.url = value;
}

  var clsAjaxCommunicator = new AjaxCommunicator();

