/*
================================================================================
FILENAME:	default.js
DESCRIPTION:	Default JavaScript classes and functions.
AUTHOR:		Erik Orbons
VERSION:	1.0
================================================================================
*/

function reportError (error)
{
	alert (error);
}

/*
================================================================================
Ajax:
================================================================================
*/
/**
 * Constructor.
 */
function AJAXRequest ()
{
	// Attempt to create the httpRequest object:
	if (window.XMLHttpRequest)
	{
		// Mozilla, safari and others:
		this.httpRequest = new XMLHttpRequest ();

		// Some implementations need the mime type to be set
		// to text/xml in order to work:
		if (this.httpRequest.overrideMimeType)
		{
			this.httpRequest.overrideMimeType ('text/xml');
		}
	}
	else if (window.ActiveXObject)
	{
		// IE:
		try
		{
			// Newer version:
			this.httpRequest = new ActiveXObject ("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			// Try the old version:
			try
			{
				this.httpRequest = new ActiveXObject ("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				throw (e);
			}
		}
	}
}

/**
 * Checks if the object's content is delivered.
 */
AJAXRequest.prototype.isComplete = function ()
{
	return this.httpRequest.readyState == 4;
}

/**
 * Checks if the object's content is successfully delivered.
 */
AJAXRequest.prototype.succeeded = function ()
{
	return this.httpRequest.status == 200;
}

/**
 * Checks if the object is working (sending a request).
 */
AJAXRequest.prototype.isWorking = function ()
{
	return this.httpRequest.readyState != 0;
}

/**
 * Performs a GET request.
 */
AJAXRequest.prototype.sendGET = function (url, asynchronous)
{
	if (this.isWorking ())
		return false;
		
	this.httpRequest.open ('GET', url, asynchronous);
	this.httpRequest.send (null);

	if (asynchronous)
		// Always return true:
		return true;
	else
		// Return the status:
		return this.isComplete () && this.succeeded ();
}

/**
 * Performs a POST request.
 */
AJAXRequest.prototype.sendPOST = function (url, data, asynchronous)
{
	if (this.isWorking ())
		return false;
	
	this.httpRequest.open ('POST', url, asynchronous);
	this.httpRequest.setRequestHeader ('Method', 
		"POST " + url + " HTTP/1.1");
	this.httpRequest.setRequestHeader ('Content-Type', 
		'application/x-www-form-urlencoded');
	this.httpRequest.send (data);

	if (asynchronous)
		// Always return true:
		return true;
	else
		// Return the status:
		return this.isComplete () && this.succeeded ();
}

/**
 * Retrieves the result from the request.
 */
AJAXRequest.prototype.getResultText = function ()
{
	return this.httpRequest.responseText;
}

/*
================================================================================
Dynamic module loader:
================================================================================
*/
/**
 * Class to dynamically load modules.
 */
function ModuleLoader ()
{
	this.loadedModules  = { };
	this.loadingModules = { };
}

/**
 * Checks if a given module is loaded.
 */
ModuleLoader.prototype.isLoaded = function (name)
{
	return typeof (this.loadedModules[name]) != "undefined";
}

/**
 * Checks if a given module is being loaded.
 */
ModuleLoader.prototype.isLoading = function (name)
{
	return typeof (this.loadingModules[name]) != "undefined";
}

/**
 * Directly loads a module.
 */
ModuleLoader.prototype.loadModule = function (name)
{
	if (this.isLoaded (name))
		return true;

	// Create an URL:
	var url = configuration.moduleLoaderURL + "?module=" +
		encodeURI (name);
		
	// Load it using AJAX:
	var loader = new AJAXRequest ();
	if (!loader.sendGET (url, false))
		return false;

	// Evaluate the result text:
	this.storingModule = name;
	//alert (loader.getResultText ());
	eval (loader.getResultText ());
	this.storingModule = null;


	// Check if the module was stored:
	return typeof (this[name]) != "undefined";
}

/**
 * Stores a module in the list.
 */
ModuleLoader.prototype.storeModule = function (module)
{
	if (typeof (this.storingModule) == "undefined" ||
		this.storingModule == null)
		// Not storing a module:
		return;

	this[this.storingModule] = module;
}

var modules = new ModuleLoader ();
