Ajax = {};

// Array of potential XHMLHttpRequest-creation factory functions
Ajax.factories = [
	function () { return new XMLHttpRequest(); },
	function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
	function () { return new ActiveXObject('Microsoft.XMLHTTP'); }
];

// Successful XMLHttpRequest-creation factory function (when found)
Ajax.factory = null;

// Create and return a new XMLHttpRequest object
Ajax.newRequest = function () {

	// If a successful XMLHttpRequest-creation function has been decided, return it
	if (Ajax.factory) return Ajax.factory();

	// Try all XMLHttpRequest-creation functions until one is successful
	for (var i = 0; i < Ajax.factories.length; i++) {
		try {
			var factory = Ajax.factories[i]
			var request = factory();

			// If factory function is successful, store it as single method and return the request
			if (request) {
				Ajax.factory = factory;
				return request;
			}
		}

		// Current iteration of XMLHttpRequest-creation function is unsuccessful; try another
		catch(e) {
			continue;
		}
	}

	// No XMLHttpRequest-creation functions unsuccessful; throw error
	Ajax.factory = function () {
		throw new Error('XMLHttpRequest not supported');
	};
	Ajax.factory();
};

// Fetch the contents of the specified URL using an Ajax GET request
Ajax.getText = function(url, callback) {
	var request = Ajax.newRequest();
	request.onreadystatechange = function() {
		if (request.readyState === 4 && request.status === 200) {
			callback(request.responseText);
		}
	}
	request.open('GET', url);
	request.send(null);
};