/* -------------------------------------------------------------------------------------
                               USAGE
   -------------------------------------------------------------------------------------
	ax = new AJAX();
	ax.OnComplete=ReportSuccess;
	ax.OnError=ReportError;
	ax.Send("<NameValuePairData/>", "<RemoteURL>", "string POST/GET", <boolean Async/Sync>, <boolean FormData/XML>);

	function ReportSuccess()
	{   
	   return ax.responseText;
	}
	
	function ReportError()
	{
	    alert(ax.responseText);
	}

*/

function AJAX()
{
  this.XmlHttp = this.initXMLHTTP();
}
 
AJAX.prototype.initXMLHTTP = function()
{ 
   var xmlHttp = false;
   try
    {
        if(window.ActiveXObject)
        {
            for( var i = 5; i; i--)
            {
                try
                {
                   if( i == 2 )
                    {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    
                    }
                    else
                    {
                        
                        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP." + i + ".0");
                    }
                    break;
                }
                catch(e)
                {                        
                    xmlHttp = false;
                }
            }
        }
        else if(window.XMLHttpRequest)
        {
            xmlHttp = new XMLHttpRequest();
        }
    }
    catch(e)
    {
        xmlHttp = false;
    }
    return xmlHttp ;
}
 
AJAX.prototype.Send = function(sendData, sendURL, sendMethod, isAsync, isFormData)
{
  if( this.XmlHttp )
  {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
      var oThis = this;
      this.XmlHttp.open(sendMethod, sendURL, isAsync);
      if(isAsync)
      {
        this.isAsync = true;
      	this.XmlHttp.onreadystatechange = function(){ oThis.ReadyStateChange(); };
      }
      if(isFormData)
      {
        this.isFormData = true;
      	this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      }
      this.XmlHttp.send(sendData);
      if(!isAsync)
      {
	    if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" ){
			this.status = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.status;
			this.statusText = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.statusText;
			this.responseText = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.responseText ;
			this.responseXML = typeof this.XmlHttp  == "undefined" ? null : this.XmlHttp.responseXML; 	    
	      return true;
	      }
	    else
	      return false; 
      }
    }
  }
}
 
AJAX.prototype.AbortCallBack = function()
{
  if( this.XmlHttp )
    this.XmlHttp.abort();
}
 
AJAX.prototype.OnLoading = function()
{
}
 
AJAX.prototype.OnLoaded = function()
{
}
 
AJAX.prototype.OnInteractive = function()
{
}
 
AJAX.prototype.OnComplete = function()
{
}
 
AJAX.prototype.OnAbort = function()
{
}
 
AJAX.prototype.OnError = function()
{
}

AJAX.prototype.status = "";

AJAX.prototype.statusText = "";

AJAX.prototype.responseText = "";

AJAX.prototype.responseXML = null;

AJAX.prototype.isFormData = false;

AJAX.prototype.isAsync = false;

AJAX.prototype.ReadyStateChange = function()
{
  if( this.XmlHttp.readyState == 1 )
  {
    this.OnLoading();
  }
  else if( this.XmlHttp.readyState == 2 )
  {
    this.OnLoaded();
  }
  else if( this.XmlHttp.readyState == 3 )
  {
    this.OnInteractive();
  }
  else if( this.XmlHttp.readyState == 4 )
  {
    if( this.XmlHttp.status == 0 )
      this.OnAbort();
    else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" )
      {
		  this.status = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.status;
		  this.statusText = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.statusText;
		  this.responseText = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.responseText ;
		  this.responseXML = typeof this.XmlHttp  == "undefined" ? null : this.XmlHttp.responseXML;
	      this.OnComplete();
      }
    else
      {
		  this.status = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.status;
		  this.statusText = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.statusText;
		  this.responseText = typeof this.XmlHttp  == "undefined" ? "" : this.XmlHttp.responseText ;
      	  this.OnError();
      }   
  }
}

