//Methods from Ajax Patterns and Best Practices - by Christian Gross (Apress, 2006)
function FactoryXMLHttpRequest() {
	if(window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		var msxmls = new Array(
			'Msxml2.XMLHTTP.5.0',
			'Msxml2.XMLHTTP.4.0',
			'Msxml2.XMLHTTP.3.0',
			'Msxml2.XMLHTTP',
			'Microsoft.XMLHTTP');
		for (var i = 0; i < msxmls.length; i++) {
			try {
				return new ActiveXObject(msxmls[i]);
			} catch (e) {
			}
		}
	}
	throw new Error("Could not instantiate XMLHttpRequest");
}


//Making Multiple Asynchronous Requests
function Asynchronous( ) {
	this._xmlhttp = new FactoryXMLHttpRequest();
}

function Asynchronous_call(method,url,parameters) {
	var instance = this;
	this._xmlhttp.open(method, url, true); //method == 'POST' or 'GET'
	if (method=='GET'){
		this._xmlhttp.send(null);
	} else if (method=='POST'){
		this._xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		this._xmlhttp.setRequestHeader("Content-length", parameters.length);
		this._xmlhttp.setRequestHeader("Connection", "close");
		this._xmlhttp.send(parameters);
	}
	this._xmlhttp.onreadystatechange = function() {
		switch(instance._xmlhttp.readyState) {
		case 1:
			instance.loading();
			break;
		case 2:
			instance.loaded();
			break;
		case 3:
			instance.interactive();
			break;
		case 4:
			instance.complete(instance._xmlhttp.status,
			instance._xmlhttp.statusText,
			instance._xmlhttp.responseText, instance._xmlhttp.responseXML);
			break;
		}
	}
}

function Asynchronous_loading() {
}

function Asynchronous_loaded() {
}

function Asynchronous_interactive() {
}

function Asynchronous_complete(status, statusText, responseText, responseHTML) {
}

Asynchronous.prototype.loading = Asynchronous_loading;
Asynchronous.prototype.loaded = Asynchronous_loaded;
Asynchronous.prototype.interactive = Asynchronous_interactive;
Asynchronous.prototype.complete = Asynchronous_complete;
Asynchronous.prototype.call = Asynchronous_call;
