/** simple functions for ajax magic */


// constants
var MAX_ENTITY_SIZE = 800;  // the number of characters to display on failed ajax functions


/**
* @brief retrieve a javascript object (json-format) from the specified URL with GET method
* @param url - string - the url to request
* @param callback - function - a function that is given the http status code and the http response json
*/
function AjaxGet(url, callback)
{
	var xhr = new XMLHttpRequest();
	xhr.open("GET", url, true);
	xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
		{
			if(callback)
			{
				if(xhr.responseText)
				{
					if(xhr.getResponseHeader("Content-Type") == "application/json")
					{
						var json = JSON.parse(xhr.responseText);
					}
					else
					{
						var json = {"ENTITY":xhr.responseText};
					}
				}
				else
				{
					var json = {};
				}
				json.ACTION = url;
				callback(xhr.status, json);
			}
			else
			{
				if((xhr.status < 200) || (xhr.status > 299))
				{
					alert("HTTP GET Status "+xhr.status);
					if(xhr.responseText && xhr.getResponseHeader("Content-Type").match(/text|xml/))
					{
						alert(xhr.responseText.substr(0, MAX_ENTITY_SIZE));
					}
				}
			}
		}
	};
	xhr.send(null);
	return true;
}  // function AjaxGet()


/**
* @brief posts data to a script using ajax and the HTTP POST method
* @param url - string - the url to request
* @param postdata - string - the data that is to be sent via http post (must be urlencoded!)
* @param callback - function - a function that is given the http status code and the http response json
*/
function AjaxPost(url, postdata, callback)
{
	var xhr = new XMLHttpRequest();
	xhr.open("POST", url, true);
	xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
		{
			if(callback)
			{
				if(xhr.responseText)
				{
					if(xhr.getResponseHeader("Content-Type") == "application/json")
					{
						var json = JSON.parse(xhr.responseText);
					}
					else
					{
						var json = {"ENTITY":xhr.responseText};
					}
				}
				else
				{
					var json = {};
				}
				json.ACTION = url;
				callback(xhr.status, json);
			}
			else
			{
				if((xhr.status < 200) || (xhr.status > 299))
				{
					alert("HTTP POST Status "+xhr.status);
					if(xhr.responseText && xhr.getResponseHeader("Content-Type").match(/text|xml/))
					{
						alert(xhr.responseText.substr(0, MAX_ENTITY_SIZE));
					}
				}
			}
		}
	};
	xhr.send(postdata);
	return true;
}  // function AjaxPost()


function FormElement(root_node, name)
{
	var all = root_node.getElementsByTagName("*");
	var matches = [];
	for(var i = 0; i < all.length; i++)
	{
		if(all[i].nodeName.match(/^(input|select|textarea|button)$/i) && all[i].name && all[i].name == name)
		{
			matches.push(all[i]);
		}
	}
	return matches;
}  // function FormElement()


/**
* @brief returns a throbber image object
* @param id - string - an optional id to associate with the image
* @return image object
*/
function Throbber(id)
{
	var img = document.createElement("img");
	img.src = "img/throbber.gif";
	img.setAttribute("alt", "[working]");
	img.setAttribute("title", "please wait...");
	if(id)
	{
		img.setAttribute("id", id);
	}
	return img;
}  // function Throbber()
