/*
   ===============================================================================================
   AjaxWrapper.js: A "wrapper" class that establishes an HTTP connections and interacts with a web
                   server using Ajax (i.e., the XMLHTTPRequest object). 
   ===============================================================================================
   
   NOTES: 
   
   The constructor for the AjaxWrapper class takes the following form: 
      AjaxWrapper(sHttpMethod, sLocation)
      
      sHttpMethod - How to send data to the web server. Possible values are 'GET' and 'POST'. 
      sLocation   - The URL to be requested from the web server. 
      
   The member data of instances of the AjaxWrapper class are: 
      httpMethod  - How to send data to the web server. Possible values are 'GET' and 'POST'. 
      httpRequest - The XMLHTTPRequest object used to connect to the web server. 
      location    - The URL to be requested from the web server.
	  onError     - The method to be executed if there is an HTTP error .
	  onLoad      - The method to be executed if the HTTP request was successful.
      
   The member functions of instances of the AjaxWrapper class are: 
      send        - Sends the HTTP request to the web server using the specified properties.
 */
function AjaxWrapper(sHttpMethod, sLocation) {
	var oGenericWrapper = new Object();
   
    this.httpMethod = sHttpMethod;
    this.location   = sLocation;
    this.onLoad     = function() {};
    this.onError    = function() {};
    
    if (window.XMLHttpRequest) {
        this.httpRequest = new XMLHttpRequest();
        
        if(this.httpRequest.overrideMimeType) {
            this.httpRequest.overrideMimeType('text/xml');
        }
    }
    else {
        if (window.ActiveXObject) {
            try {
                this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                    this.httpRequest = null;
                }
            }
        }
    }
    	
    if(this.httpRequest != null) {
    	oGenericWrapper.httpRequest = this.httpRequest;
    	oGenericWrapper.ajaxWrapper = this;
    
        this.httpRequest.onreadystatechange = function() {
		    if(oGenericWrapper.httpRequest.readyState == 4) {
		        if(oGenericWrapper.httpRequest.status == 200) {
		            oGenericWrapper.ajaxWrapper.onLoad();
		        }
		        else {
		            oGenericWrapper.ajaxWrapper.onError();
		        }
		    }
		};
        
        if(typeof(_ajaxwrapper_prototype_called) == 'undefined') {
            _ajaxwrapper_prototype_called = true;
            AjaxWrapper.prototype.send = SendRequest;
        }
    }
}

function SendRequest() {
    this.httpRequest.open(this.httpMethod, this.location, true);
    this.httpRequest.send(null);
}
