/**
 * AjaxAPI 1.0
 * 
 * This function library is written to make working with Ajax requests more easy.
 * For every function I wrote a small sample.
 * 
 * Overview of functions:
 * AjaxAPI.getAjaxRequestObject()
 * AjaxAPI.getContents(url, responsefunction)
 * AjaxAPI.getText(url, responsefunction)
 * AjaxAPI.getXML(url, responsefunction)
 * AjaxAPI.fillContainer(url, container)
 * 
 * @author Stefan Thoolen <stefan@netvlies.nl>
 * @version 1.0
 * @package Ajax
 */

// Starts the Ajax API Tree
AjaxAPI=new Object();

/**
 * Builds the XML HTTP Request Object in all browsers possible.
 * 
 * @access	private
 * @return	object	The XML HTTP Request Object
 */ 
AjaxAPI.getAjaxRequestObject=function () {
	var req = null;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest ();
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject ("Microsoft.XMLHTTP");
		} catch (e) {
			try { req = new ActiveXObject ("Msxml2.XMLHTTP"); }
			catch (e) {}
		}
	}
	return req;
}

/**
 * Gets the contents from an URL and returns the XML Request Object to the response function.
 * 
 * Sample:
 * <code>
 *   AjaxAPI.getText('http://www.netvlies-demo.nl/ajax.txt',function (requestObj) {
 *       alert(requestObj.responseText);
 *   });
 * </code>
 * 
 * @param	string		url					The URL that should be requested
 * @param	function	responsefunction	The function that should be executed when all data is received
 * @return	boolean							If the request succeeded or not
 */ 
AjaxAPI.getContents=function (url, responsefunction) {
	try {
		var ajaxRequest=AjaxAPI.getAjaxRequestObject();
		ajaxRequest.onreadystatechange = function () {
			if(ajaxRequest.readyState==4 && ajaxRequest.status==200) responsefunction(ajaxRequest);
		}
		ajaxRequest.open ("GET", url, true);
		ajaxRequest.send (null);
		return true;
	} catch (e) { return false; }
}

/**
 * Gets the contents from an URL and returns as text to the response function.
 * 
 * Sample:
 * <code>
 *   AjaxAPI.getText('http://www.netvlies-demo.nl/ajax.txt',function (responseText) {
 *       alert(responseText);
 *   });
 * </code>
 * 
 * @param	string		url					The URL that should be requested
 * @param	function	responsefunction	The function that should be executed when all data is received
 * @return	boolean							If the request succeeded or not
 */ 
AjaxAPI.getText=function (url, responsefunction) {
	try {
		AjaxAPI.getContents(url, function (req) { responsefunction(req.responseText); });
		return true;
	} catch (e) { return false; }
}

/**
 * Gets the contents from an URL and returns as XML to the response function
 * 
 * Sample:
 * <code>
 *   AjaxAPI.getText('http://www.netvlies-demo.nl/ajax.txt',function (responseXML) {
 *       alert(typeof responseXML);
 *   });
 * </code>
 * 
 * @param	string		url					The URL that should be requested
 * @param	function	responsefunction	The function that should be executed when all data is received
 * @return	boolean							If the request succeeded or not
 */ 
AjaxAPI.getXML=function (url, responsefunction) {
	try {
		AjaxAPI.getContents(url, function (req) { responsefunction(req.responseXML); });
		return true;
	} catch (e) { return false; }
}

/**
 * Fills an object with the HTML content from an URL
 * 
 * Sample 1:
 * <code>
 *   AjaxAPI.fillContainer('http://www.netvlies-demo.nl/ajax.txt', document.body);
 * </code>
 * Sample 2:
 * <code>
 *   AjaxAPI.fillContainer('http://www.netvlies-demo.nl/ajax.txt', 'objectid');
 * </code>
 * 
 * @param	string		url			The URL that should be requested
 * @param	variant		container	Can be an object or id. The object.innerHTML will be the contents of the URL
 * @return	boolean					If the request succeeded or not
 */ 
AjaxAPI.fillContainer=function (url, container) {
	try {
		if(typeof container == 'string') container=document.getElementById(container);
		AjaxAPI.getText(url, function (responseText) {
			container.innerHTML=responseText;
		});
		return true;
	} catch (e) { return false; }
}
