/* =================================================================================================== *
 *                                                                                                     *
 * Ajax class                                                                                          *
 * by Johannes Jensen, 2009                                                                            *
 *                                                                                                     *
 * USAGE:                                                                                              *
 * new Ajax().formatParameters( params:Object ) - formats an Object of params into a PHP url string.   *
 * new Ajax().createRequest() - returns a fresh XMLHttpRequest object. Returns null if it fails.       *
 * new Ajax().load( file:String, params:Object = null, onComplete:Function ) - performs a              *
 * -> XMLHttpRequest and runs onComplete() function with parameter: data:String which is the data      *
 * -> loaded from file:String.                                                                         *
 *                                                                                                     *
 * =================================================================================================== */

// Ajax class.
function Ajax() {
	// Create the XMLHttpRequest object.
	this.createRequest = function() {
		// Allocating xmlHttp variable
		var xmlHttp = null;

		try {
			// In modern browsers. (Such as Firefox, Safari, etc. -- not IE though. Silly IE.)
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			try {
				// IE.
				xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
			} catch (e) {
				// If the first IE fails, let's just try this IE.
				xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			}
		}
		
		// Did we fail?
		if(xmlHttp == null) {
			// No XMLHttpRequest obtained. We failed.
			window.alert("Cannot load data. Please download a new browser. I suggest Firefox or Safari.");
		}
		
		// Return our beloved XMLHttpRequest. Returns null if the XMLHttpRequest was unobtainable.
		return xmlHttp;
	};
	
	// Format parameters.
	this.formatParameters = function(params) {
		// Array of params.
		var p = [];
		
		// Are the params an object and are they not null?
		if(typeof(params) == 'object' && params != null) {
			// Loop through params and append encodeURIComponent() parsed values to old values.
			for(i in params) {
				// Add parsed string to p array.
				p.push(i + '=' + encodeURIComponent(params[i]));
			}
			
			// Return the final parsed string.
			return '&' + p.join('&');
		} else {
			// Return nothing, if the params are set to null or params aren't an object.
			return '';
		}
	};
	
	// Load a file from the website, once loaded it executes complete() with the params: 
	// data, which contains a string with data from the website that was just loaded.
	this.load = function(file, params, onComplete) {
		// Attempt to create a new XMLHttpRequest.
		var request = this.createRequest();
		
		// Did it work? 
		if(request != null) {
			// Run this function each time the request updates. readyState is 4 or 'complete' once it's done loading.
			request.onreadystatechange = function() {
				// Is it done?
				if(request.readyState == 4 || request.readyState == 'complete') {
					// Pass responseText on to the onComplete() function and execute the onComplete() function.
					onComplete(request.responseText);
				}
			};
			
			// Load from the file and parse the params using formatParameters().
			request.open('GET', file + (file.indexOf('?') == -1 ? '?' : '&') + 'sid=' + new Date().getTime() + this.formatParameters(params));
			request.send(null);
		}
	};
}