﻿function AAJAX(instanceName)
{
   // *** private data members

   var _oXmlHttpRequester = null;

  var Header = 
	{
	   Date: "Date",
	   ContentType: "Content-Type",
	   ContentLength: "Content-Length",
	   AcceptRanges: "Accept-Ranges",
	   LastModified: "Last-Modified",
	   ContentMD5: "Content-MD5",
	   ETag: "ETag"
	};

   var Method =
	{
	   Get: "GET",
	   Post: "POST",
	   Head: "HEAD",
	   Soap: "SOAP",
	   Put: "PUT",
	   Delete: "DELETE",
	   Options: "OPTIONS",
	   Trace: "TRACE",
	   Connect: "CONNECT"
	};

   var callbackMode = 
	{
	   Sync: false,
	   Async: true
	};

   var ReadyState = 
	{
	   Uninitialized: 0,
	   Loading: 1,
	   Loaded: 2,
	   Interactive: 3,
	   Complete: 4
	};

   var Status = 
	{
	   OK: 200,
	   Created: 201,
	   Accepted: 202,
	   NoContent: 204,
	   BadRequest: 400,
	   Unauthorized: 401,
	   Forbidden: 403,
	   NotFound: 404,
	   MethodNotAllowed: 405,
	   RequestTimeout: 408,
	   Gone: 410,
	   ServerError: 500
	};

   var Exception = 
	{
	   XmlHttpNotSupported: "XMLHTTPRequest is not supported by the browser."
	}

   // *** public properties

   this.Method = Method.Get; // default
   this.requestStatus = Status.OK; // default


   // *** public methods

   this.getXmlHttpRequester = getXmlHttpRequester;
   this.abortRequest = abortRequest;
   this.makeRequest = makeRequest;


   function getXmlHttpRequester()
   {
	_oXmlHttpRequester = null;

	if(window.ActiveXObject) // check IE first
	{
	   _oXmlHttpRequester = new ActiveXObject("Msxml2.XMLHTTP"); // IE 6.0+
	   if(!_oXmlHttpRequester) {_oXmlHttpRequester = new ActiveXObject("Microsoft.XMLHTTP");} // IE 5.0, 5.5
	}
	if(window.XMLHttpRequest) // FF, NS, Safari etc.. next
	{
	   _oXmlHttpRequester = new XMLHttpRequest();
	   makeXmlRequest();
	}

	if(!_oXmlHttpRequester) // neither IE nor Mozilla worked out 
	{
	   alert(Exception.XmlHttpNotSupported);
	}
   }

   function abortRequest()
   {
	_oXmlHttpRequester.abort();
   }
   
   function makeRequest(url, callback) {

        if (!_oXmlHttpRequester) {
            getXmlHttpRequester();
        }
        
        _oXmlHttpRequester.onreadystatechange = function()
		{
		   if(_oXmlHttpRequester.readyState == ReadyState.Complete)
		   {
			  this.requestStatus = _oXmlHttpRequester.status;

			  if(_oXmlHttpRequester.status == Status.OK)
			  {
			    callback(_oXmlHttpRequester.responseText);
              } else {
                alert('There was a problem with the request. Current status is : ' + _oXmlHttpRequester.status);
              }
           }
		}
        _oXmlHttpRequester.open(this.Method , url, true);
        _oXmlHttpRequester.send(null);
    }
    
    function makeXmlRequest()
   {
	// Overriding the header sent by the server, just in case if it's not text/xml:
	//    Some old versions of Mozilla browsers work properly only if server  
	//    response has the XML mime-type header.  

	if(_oXmlHttpRequester.overrideMimeType) {_oXmlHttpRequester.overrideMimeType('text/xml');}
   }

   
} // end of AAJAX() class
