/*ALL HAIL ABSANKTIE FOR THIS AJAX SCRIPT*/

/*VARIABLES*/
var req;
var activeRequest = false;
var method = false;
var file = false;
var format = false;
var destination = false;
/*END VARIABLES*/

/*INITIALIZE REQUEST*/
function loadFile(HTTPmethod, fileURL, varString, responseFormat, destID)
{
	document.body.style.cursor = "wait";
	if(activeRequest == false)
	{
		HTTPmethod=HTTPmethod.toUpperCase();

		if(HTTPmethod == 'GET' || HTTPmethod == 'POST' || HTTPmethod == 'HEAD')
		{
			if(fileURL.indexOf('.') != -1)
			{
				req = createObject();
				if(req != null)
				{
					if(!isSet(responseFormat))
					{
						responseFormat = false;
					}
					else
					{
						responseFormat = responseFormat.toLowerCase();
					}
					if(responseFormat == 'text' || responseFormat == 'xml' || responseFormat == 'json' || !responseFormat)
					{
						if(!isSet(varString))
						{
							varString = '';
						}
						if(!isSet(destID))
						{
							destID = false;
						}
						activeRequest = true;
						method = HTTPmethod;
						file = fileURL;
						format = responseFormat;
						destination = destID;
						if(method != 'GET')
						{
							req.open(method, file, true);
							req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
						}
						else
						{
							var rand = Math.floor(Math.random()*10000000);
							var url = file+'?cache='+rand;
							if(varString != '')
							{
								url += '&'+varString;
							}
							req.open(method, encodeURI(url), true);
						}
						req.onreadystatechange = processReqChange;
						switch(method)
						{
							case 'GET':
							case 'HEAD': req.send(null); break;
							case 'POST': req.send(varString); break;
						}
					}
					else
					{
						alert("'"+responseFormat+"' is not a valid response type.\n The request will be aborted.");
					}
				}
				else
				{
					alert("Your browser does not support AJAX.");
				}
			}
			else
			{
				alert("'"+fileURL+"' is not a valid file.\n The request will be aborted.");
			}
		}
		else
		{
			alert("'"+HTTPmethod+"' is not a valid method.\n The request will be aborted.");
		}
	}
	else
	{
		window.setTimeout(function () {loadFile(HTTPmethod, fileURL, varString, responseFormat, destID); }, 10);
	}
}
/*END INITIALIZE REQUEST*/



/*CREATE EMPTY REQUEST*/
function createObject()
{
	req = null;
	if(window.XMLHttpRequest)
	{
		try
		{
			req = new XMLHttpRequest();
		}
		catch(e)
		{
			req = null;
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				req = null;
			}
		}
	}
	if(req)
	{
		return req;
	}
}
/*END CREATE EMPTY REQUEST*/



/*ISSET CHECK*/
function isSet(variable)
{
	if(typeof(variable) == 'undefined' || variable == 'undefined' || variable == false || variable == null)
	{
		return false;
	}
	else
	{
		return true;
	}
}
/*END ISSET CHECK*/



/*FETCH DATA*/
function processReqChange()
{
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			var data=false;
			if(method == 'HEAD')
			{
				data = req.getAllResponseHeaders();
			}
			else
			{
				switch(format)
				{
					case 'json': data = eval('(' + req.responseText + ')'); break;
					case 'text': data = req.responseText; break;
					case 'xml':  data = req.responseXML; break;
					default:     data = req.responseText; break;
				}
			}
/*DATA HANDLING*/
			if(data)
			{
				handleData(data);
			}
/*DATA HANDLING*/
/*NO DATA HANDLING*/
			else
			{
				handleNoData();
			}
/*END NO DATA HANDLING*/
		}
/*STATUS ERROR*/
		else
		{
			alert("There was a problem retrieving the requested page:\n" + req.statusText);
			clearRequest();
		}
/*END STATUS ERROR*/
	}
}
/*END FETCH DATA*/



/*DATA HANDLING*/
function handleData(data)
{
	// User-defined callback.
	if(destination && Object.prototype.toString.call(destination) === '[object Function]')
	{
		destination(data);
		clearRequest();
	}
/*SPECIAL DATA ACTIONS*/
/***TYPE ONLY HERE***/
/***END TYPE ONLY HERE***/
/*END SPECIAL DATA ACTIONS*/
/*DEFAULT DATA HANDLER*/
	else
	{
		if(destination && isSet(document.getElementById(destination)))
		{
			placeData(data);
		}
		clearRequest();
	}
/*END DEFAULT DATA HANDLER*/
}
/*END DATA HANDLING*/



/*HANDLING EMPTY REQUEST*/
function handleNoData()
{
	if(!format)
	{
		if(destination)
		{
			if(destination.indexOf('.') != -1)
			{
				window.location.href = encodeURI(destination);
				clearRequest();
			}
			else if(destination.indexOf('reloadPage') != -1)
			{
				clearRequest();
				window.location.reload();
			}
/*SPECIAL NO DATA ACTIONS*/
/***TYPE ONLY HERE***/
/***END TYPE ONLY HERE***/
/*END SPECIAL NO DATA ACTIONS*/
			else
			{
				if(isSet(document.getElementById(destination)))
				{
					placeData();
				}
				else
				{
					alert('Invalid destination: '+destination);
				}
				clearRequest();
			}
		}
		else
		{
		clearRequest();
		}
	}
	else
	{
		if((destination && isSet(document.getElementById(destination))))
		{
			placeData();
		}
		clearRequest();
	}
}
/*END HANDLING EMPTY REQUEST*/



/*EMPTIES THE REQUEST*/
function clearRequest()
{
	activeRequest = false;
	method = false;
	file = false;
	format = false;
	destination = false;
	document.body.style.cursor = 'default';
}
/*END EMPTIES THE REQUEST*/



/*PLACING THE DATA*/
function placeData(data)
{
	if(!isSet(data))
	{
		data = '';
	}
	var node = document.getElementById(destination);
	if(node.value)
	{
		node.value = data;
	}
	else
	{
		node.innerHTML = data;
	}
}
/*END PLACING THE DATA*/