/*
Name:       AJAX
Version:    0.0.3 (25. October 2010)
Author:     Finn Rudolph
Support:    rudolph@stadtwerk.org

License:    This code is licensed under a Creative Commons 
            Attribution-Noncommercial 3.0 Unported License 
            (http://creativecommons.org/licenses/by-nc/3.0/).

            You are free:
                + to Share - to copy, distribute and transmit the work
                + to Remix - to adapt the work

            Under the following conditions:
                + Attribution. You must attribute the work in the manner specified by the author or licensor 
                  (but not in any way that suggests that they endorse you or your use of the work). 
                + Noncommercial. You may not use this work for commercial purposes. 

            + For any reuse or distribution, you must make clear to others the license terms of this work.
            + Any of the above conditions can be waived if you get permission from the copyright holder.
            + Nothing in this license impairs or restricts the author's moral rights.
*/

/* AJAX constructor */
function AJAX()
{
	/* Setting option defaults */
	this.defaults =
	{
		mode:               'GET',
		url:                '',
		asynchronus:        true,
		contentType:        'application/x-www-form-urlencoded',
		returnFunction:     function(response,source){console.log(response+source)},
		data:               null,
		responseType:       'TEXT',
		callSource:         null
	};
	
	/* Closure for this */
	var my = this;

	/* Make the call */
	this.call = function(options)
	{
		/* Evaluate options */
		for(var name in my.defaults) 
		{
			this[name] = (options !== undefined && options[name] !== undefined) ? options[name] : my.defaults[name];
		}
		
		/* Create global request object */
		my.createRequestObject();

		/* Request url asynchronus */
		my.request.open(my.mode, my.url, my.asynchronus);

		/* Handle the started request */		
		my.request.onreadystatechange = my.getReadyStateHandler();
		my.request.setRequestHeader("Content-Type", my.contentType);
		my.request.send(my.data);
	}
	
	/* Create an XML HTTP request object */
	this.createRequestObject = function()
	{
		var request = null;
		try
		{
			request = new XMLHttpRequest();
		}
		catch(isIE)
		{
			try
			{
				request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(isNotIE)
			{
				try
				{
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (failed)
				{
					request = null;
				}
			}
		}
		if (request == null)
		{
			console.log('AJAX: createRequestObject » Failed to create an request object');
		}
		
		/* Make the request object global */
		this.request = request;
	}
	
	
	/* Handle the active request */
	this.getReadyStateHandler = function()
	{
		/* Return an anonymous function that listens to the XMLHttpRequest instance */
		return function()
		{
			switch(my.request.readyState)
			{
				/* Proceed if readyState is DONE - http://www.w3.org/TR/XMLHttpRequest/#states */
				case 4:
					statusCode = my.request.status;
					if(statusCode != 200)
					{
						console.log('AJAX: handleRequest » HTTP Error: '+statusCode);
					}
					/* Proceed if HTTP Status Code is OK - http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1 */
					else
					{
						/* Pass the payload of the response to the return function */
						var content = (my.responseType.toLowerCase() == 'xml') ? my.request.responseXml :  my.request.responseText;
						my.returnFunction(content, my.callSource);
					}
					break;

				default:
					return false;
					break;     
			}
		}
	}
}
