function ajax(type)
{
   //---------------------
   // Private Declarations
   //---------------------
   var _request = null;
   var _this = null;
   var _type = 'xml';
   if( type == 'text' )
   {
   		_type = 'text';
   }

   //--------------------
   // Public Declarations
   //--------------------
   this.GetResponseXML = function()
   {
      return (_request) ? _request.responseXML : null;
   }

   this.GetResponseText = function()
   {
      return (_request) ? _request.responseText : null;
   }

   this.GetRequestObject = function()
   {
      return _request;
   }

   this.InitializeRequest = function(Uri, onSuccess, onFail, Method)
   {
		if(Method == null)
		{
			Method = 'POST';
		}
      _InitializeRequest();
      _this = this;

       _request.open(Method, Uri, true);

        this.OnSuccess = onSuccess;
        this.OnFail = onFail;

      this.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   }

   this.SetRequestHeader = function(Field, Value)
   {
      if (_request) _request.setRequestHeader(Field, Value);
   }

   this.Commit = function(Data)
   {
      if (_request) _request.send(Data);
   }

	this.Close = function()
   {
      if (_request) _request.abort();
   }

   //---------------------------
   // Public Event Declarations.
   //---------------------------
   this.OnUninitialize = function() { };
   this.OnLoading = function() { };
   this.OnLoaded = function() { };
   this.OnInteractive = function() { };
   this.OnSuccess = function() { };
   this.OnFail = function() { };

   //---------------------------
   // Private Event Declarations
   //---------------------------
   function _OnUninitialize() { _this.OnUninitialize(); };
   function _OnLoading() { _this.OnLoading(); };
   function _OnLoaded() { _this.OnLoaded(); };
   function _OnInteractive() { _this.OnInteractive(); };

   //------------------
   // Private Functions
   //------------------
   function _InitializeRequest()
   {
      _request = _GetRequest();
      _request.onreadystatechange = _StateHandler;
   }

   function _StateHandler()
   {
      switch (_request.readyState)
      {
         case 0:
            window.setTimeout("void(0)", 100);
            _OnUninitialize();
            break;

         case 1:
            window.setTimeout("void(0)", 100);
            _OnLoading();
            break;

         case 2:
            window.setTimeout("void(0)", 100);
            _OnLoaded();
            break;

         case 3:
            window.setTimeout("void(0)", 100);
            _OnInteractive();
            break;

         case 4:
         	var fail = false;
         	var fail_message = '';
            if (_request.status == 200)
            {
            	if( _type == 'xml' )
            	{
	            	var xmldoc = _request.responseXML;

	            	var success = xml_get_field(xmldoc, "success");
	            	if( success.toUpperCase() == "TRUE" )
			 		{
	               		_this.OnSuccess(xmldoc);
	              	}
	              	else
	              	{
	              		fail = true;
	              		fail_message = 'Success node failed';
	              	}
              	}
              	else
              	{
              		var doc = _request.responseText;
              		_this.OnSuccess(doc);
              	}
            }
            else
            {
            	fail = true;
            	fail_message = 'An unknown error occured';
            }

			if( fail==true )
			{
				_this.OnFail(xmldoc, fail_message);
			}
            return;
            break;
      }
   }

   function _GetRequest()
   {
      var obj;

      try
      {
         obj = new XMLHttpRequest();
      }
      catch (error)
      {
         try
         {
            obj = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (error)
         {
            return null;
         }
      }

      return obj;
   }
}