/*
FILE:		ajax.js
VERSION:	0.0.2
AUTHOR:		Andrew Real
COMPILED:	2011-04-27
COMMENTS:

	- Must be inserted within HEAD element
	- To initialize the ajax object, run ajax.boot()

*/
ajax = {

	isLoaded : false,
	isRunning : false,
	waitTime : 5000,
	waitTimer : null,
	boot : function() {

		Ajax = false;
		if (window.XMLHttpRequest) {

			Ajax = new XMLHttpRequest();
			ajax.isLoaded = true;

			}

		else if (window.ActiveXObject) {

			Ajax = new ActiveXObject("Microsoft.XMLHTTP");
			ajax.isLoaded = true;

			}

		},

	push : function(s) {

		source = s.source || null;
		param = s.param || null;
		onsuccess = s.onsuccess || null;
		onfail = s.onfail || null;
		if (!ajax.isLoaded) ajax.boot();
		ajax.waitTimer = setTimeout("ajax.deadRequest(onfail);",ajax.waitTime);
		ajax.prep({source:source,param:param,onSuccess:onsuccess,onFail:onfail});

		},

	deadRequest : function(f) {

		if (!ajax.isLoaded) ajax.boot();
		ajax.waitTimer = null;
		if (ajax.isRunning) {

			ajax.isRunning = false;
			f("The server has not responded to your request and has surpassed the specified time limit.");

			}

		},

	prep : function(str) {

		if (!Ajax) {

			alert("Unable to create AJAX request.");

			}

		else {

			success = str.onSuccess || null;
			fail = str.onFail || null;
			source = str.source || null;
			param = str.param || null;
			if (success == null) onSuccess = "void(0);";
			if (fail == null) onFail = "void(0);";
			if (source == null || param == null) return false;

			ajax.isRunning = true;
			Ajax.open('POST',source,true);
			Ajax.onreadystatechange = function() {

				if (Ajax.readyState == 4 && Ajax.status == 200) {

					if (ajax.isRunning) ajax.isRunning = false;
					if (ajax.waitTimer != null) ajax.waitTimer = null;
					success(Ajax.responseText);

					}

				else {

					try {

						resp = Ajax.status.substring(1,0);
						if (resp == "3" || resp == "4" || resp == "5") {

							if (ajax.isRunning) ajax.isRunning = false;
							if (ajax.waitTimer != null) ajax.waitTimer = null;
							fail(Ajax.responseText);
							return false;

							}

						}

					catch(e) {

						}

					}

				}

			Ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			Ajax.send(param);
			return true;

			}

		}

	}

