/*----------------------------------------------------------------------------+
| Class: objAjaxReq                                                           |
| -----------------                                                           |
| 13/07/2008 - modified how referenced from window scope to allow use of      |
|              multiple instances (caused all sorts of bother before!)        |
+----------------------------------------------------------------------------*/


function objAjaxReq() {
	this.http_request = false;
	this.err = "";
	this.id = "oar" + Math.random();
	this.method = "GET";
    this.paramString = null;
    this.mimeType = "text/plain";
    this.headers = new Object();
	
	this.handleStateChange = function() {
        if (this.http_request.readyState == 4) {
            if (this.http_request.status == 200) {
                this.handleLoad();
            } else {
                alert('Problem loading external data.');
            }
        }
	}

    // Method to override ...
	this.handleLoad = function() {
		alert(this.http_request.responseText);
	}
	
	this.open = function(url) {
        try {
    		window[this.id] = this; // botch - to make sure 
        } catch(e) {
            alert("couldn't reference object");
        }
	    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    	    this.http_request = new XMLHttpRequest();
	        if (this.http_request.overrideMimeType && this.mimeType != "") {
            	this.http_request.overrideMimeType(this.mimeType);
            	// See note below about this line
        	}
    	} else if (window.ActiveXObject) { // IE
        	try {
            	this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
        	} catch (e) {
            	try {
                	this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
            	} catch (e) {}
        	}
    	}
	    if (!this.http_request) {
    	    this.err = 'Giving up :( Cannot create an XMLHTTP instance';
        	return false;
    	}
        // onreadystatechange handler not called in our scope, so ...
    	eval("this.http_request.onreadystatechange = function() { window['" + this.id + "'].handleStateChange(); };");
    	this.http_request.open(this.method, url, true);
    	for(var hdr in this.headers) {
            this.http_request.setRequestHeader(hdr, this.headers[hdr]);
    	}
    	this.http_request.send(this.paramString);
    	return true;
	}
	
	this.setHeader = function(strHeader, strValue) {
        this.headers[strHeader] = strValue;
	}
	
}