/** ( vim: set ts=4 sw=4 fenc=utf8: )
 * AJAX function file
 * by: Klaas Sangers + wjd
 * created: 20070815
 * last edit: 20071009
 */

/***********************************************************************

UNIVERSAL DOCUMENTATION

* AJAX readyState Information
    State	Description
 	0		The request is not initialized
 	1		The request has been set up
 	2		The request has been sent
 	3		The request is in process
 	4		The request is complete

* AJAX method - open( String request, String url, Boolean asynchronous )
    @param String request - GET or POST like with regular forms
    @param String url - absolute or relative path of the server-side script
    @param Boolean asynchronous - specifies that the request should be handled asynchronously

***********************************************************************/


/**
 * getAjaxObject() returns an object which allows the use of AJAX functions
 * @return xmlHttp Object
 */
function getAjaxObject() {
	var xmlHttp = null;
	if (typeof ActiveXObject != "undefined") {
		try { xmlHttp = xmlHttp || new ActiveXObject("MSXML2.XmlHttp.6.0"); } catch(e) {}
		try { xmlHttp = xmlHttp || new ActiveXObject("MSXML2.XmlHttp.3.0"); } catch(e) {}
		try { xmlHttp = xmlHttp || new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
		if (!xmlHttp) alert('AJAX is broken. No valid ActiveXObject created.');
	} else if (typeof XMLHttpRequest != "undefined") {
		try { xmlHttp = xmlHttp || new XMLHttpRequest(); } catch(e) { alert('AJAX is broken: ' + e); }
	}
	return xmlHttp;
}

/**
 * ajaxRequest( String url, [ function fnReady, [ function fnNotReady ] ] )
 * @param String url - absolute or relative path of the server-side script
 * @param function fnReady (Optional) - function which is performed when ready
 * @param function fnNotReady (Optional) - function which is perform while not yet ready
 * @return Boolean success
 */
function ajaxRequest( url, fnReady, fnNotReady ) {
	if (typeof ajaxRequest_hasReportedError != 'undefined')
		return true;

	var xmlHttp = getAjaxObject();
	fnReady = fnReady || function() {};
	fnNotReady = fnNotReady || function() {};

	if (typeof url != 'string' || url == '') {
		alert('kajax: ajaxRequest: No url specified');
		return false;
	}
	if (!xmlHttp) {
		ajaxRequest_hasReportedError = true;
		alert("Your browser doesn't support AJAX.\nUw browser ondersteunt geen AJAX.\nJo ynternetbrowser kinne AJAX net oan.\n\n"
				+ "-Klaas Sangers");
		return false;
	}
		
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState > 0 && xmlHttp.readyState < 4) {
			fnNotReady();
		} else if (xmlHttp.readyState == 4) {
			fnReady(xmlHttp.responseText);
		}
	}
		
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return true;
}

/**
 * ajahRequest( String url, [ Object dest, String param, [ Boolean announce ] ] )
 * @param String url - absolute or relative path of the server-side script
 * @param Object dest (Optional) - destination Object for the returned value
 * @param String param (Optional) - destination parameter for the returned value (e.g. "value", "InnerHTML", "src")
 * @param Boolean announce (Optional) - Return "Loading..." in the destination, or not - not used for <img>
 * @return Boolean success
 */
function ajahRequest( url, dest, param, announce ) {
	dest = dest || null;
	param = param || null;
	announce = announce || false;
	
	if (dest != null && param != null) {
		if (announce) { // not for <img>
			return ajaxRequest(url, function(readyText) { dest[param] = readyText; }, function(notReadyText) { dest[param] = "Loading..."; });
		} else {
			return ajaxRequest(url, function(readyText) { dest[param] = readyText; });
		}
	} else {
		return ajaxRequest(url);
	}
}

/**
 * ajaxResultset( String url, String dest, String fn )
 * @param String url - absolute or relative path of the server-side script
 * @param String dest - String for destination for the returned values (e.g. if the variable is named x the String would be 'x')
 * @param String fn - function that has to be run to parse the returned values (e.g. "parseJSON")
 * @return Boolean success
 */
function ajaxResultset( url, dest, fn ) {
	var xmlHttp = getAjaxObject();
	
	if (xmlHttp != false) { // browser supports AJAX
		
		xmlHttp.onreadystatechange=function() {
			if (xmlHttp.readyState==4) {
				if (dest != null) {
					eval(dest+' = '+xmlHttp.responseText);
					eval( fn );
				}
			}
		}
		
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		return true;
	} else if (supportsAJAX && url != null && dest != null && fn != null) { // browser doesn't support AJAX, first attempt at using AJAX which produces an alert()
		supportsAJAX = false;
		alert("Your browser doesn't support AJAX.\nUw browser ondersteunt geen AJAX.\nJo ynternetbrowser kinne AJAX net oan.\n\n-Klaas Sangers");
		return false;
	} else { // browser doesn't support AJAX, second or more attempts at using AJAX which doesn't produce alert()'s to avoid being spammed
		return false;
	}
}

