// Request an AJAX Connection
// 
// @param	string				The URL to call [Example: get_user.php, http://www.another-server.com/script.php]
// @param	string				Parameters to pass with the call [Example: name=value, firstname=john&lastname=doe]
// @param	string,function		The function to call upon completion [Example: "ajaxDone" - function ajaxDone(xml, text) { return text; } ]
// @param	string				The method of the request [Example: GET, POST] (Optional)
var xhr = false;
function AjaxConnection(url, params, callback, method) {
	method = (typeof(method) !== "undefined" && (method.toUpperCase() === "GET" || method.toUpperCase() === "POST")) ? method.toUpperCase() : "POST";
	
	// Stop any previous ajax call(s)
	if (xhr) {
		xhr.onreadystatechange = function() {};
		xhr.abort();
	}
	xhr = (function() {
		if (typeof(XMLHttpRequest) !== "undefined") {
			return new XMLHttpRequest();
		}
		
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
		
		// Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
		throw new Error("This browser does not support XMLHttpRequest.");
	})();
	
	xhr.open(method, url, true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState != 4) {
			return false;
		}
		
		if (typeof(callback) === "function") {
			callback(xhr.responseXML, xhr.responseText);
		}
		else if (typeof(window[callback]) !== "undefined") {
			window[callback](xhr.responseXML, xhr.responseText);
		}
	};
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(params);
	
	return xhr;
}
