/**
 * This library contains objects to perform AJAX-like functionalities.
 *
 * @author  Christophe Lauret (Allette Systems)
 * @version 29 October 2006
 */

/**
 * Retrieves the data as HTML at the specified URL and stores it in the container with the 
 * specified ID. This method will invoke the <code>parseResponseAsHTML</code> function upon
 * reception of the data.
 * 
 * @param url The URL to fetch the requested data.
 * @param id  The id of the container.
 */
function retrieveDataAsHTML(url, id) {
	retrieveData(url, id, parseResponseAsHTML);
	return false;
}

// perform the XMLHttpRequest();
function retrieveInfoBox(url, parentid) {
	retrieveData(url, parentid, parseInfoBoxResponse);
}

// perform the XMLHttpRequest(); and hide the resulting info box
function retrieveInfoBoxAndHide(url, parentid) {
	retrieveData(url, parentid, preloadInfoBoxResponse);
}

// perform the XMLHttpRequest();
function retrievePopup(url, parentid) {
	retrieveData(url, parentid, parsePopupResponse);
}

// Parse Requests ---------------------------------------------------------------------------------

/**
 * Parses the HTTP response and stores the content directly as HTML in the container which id is
 * specified.
 *
 * @param response    The HTTP response.
 * @param containerId The ID of the container receiving the content.
 */
function parseResponseAsHTML(response, containerId) {
	var container = document.getElementById(containerId);
	if (container)
		container.innerHTML = response;
}

/**
 * Parse the content returned for Info boxes
 * 
 * @param response  The HTTP response.
 * @param recipient The ID of the parent DIV container.(TODO: check)
 */
function parseInfoBoxResponse(response, recipient) {
  if (ib_recipient != recipient)
    return false;
	var inHTML = response; //response.substring(response.indexOf('<body>') + 6, response.lastIndexOf('</body>'));
	var div = document.getElementById(recipient);
	div.innerHTML = inHTML;
	div.style.display = 'block';
}

/**
 * Parse the content returned for Info boxes, create the info box and hides it.
 * 
 * @param response  The HTTP response.
 * @param recipient The ID of the parent DIV container.(TODO: check)
 */
function preloadInfoBoxResponse(response, recipient) {
	var inHTML = response;
	var div = document.getElementById(recipient);
	div.innerHTML = inHTML;
	div.style.display = 'none';
}

function parsePopupResponse(response, parentId) {
	var inHTML = response.substring(response.indexOf('<div id="html-content">') + 23, response.lastIndexOf('</div>'));
	document.getElementById(parentId + '-contents').innerHTML = inHTML;
	document.getElementById(parentId).style.display = 'block';
}

// Core XML HTTP Requests -------------------------------------------------------------------------

/**
 * Performs the XMLHttpRequest();
 *
 * @param url  The url to invoke.
 * @param obj  An object to pass to the function that will parse the content.
 * @param func The fonction to execute once the request if received.
 */
function retrieveData(url, objects, func) {
	try {
    // this should work for most modern browsers excluding: IE Mac
		var req = ( window.XMLHttpRequest ) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ;
			req.onreadystatechange = function() {
			switch(req.readyState){
				case 1: break;
				case 2: break;
				case 3: break;
				case 4:
					if ( req.status == 200 ){// only if "OK"
						func(req.responseText, objects);
					} else {
						onError("The following error was returned:\n" + req.statusText);
					}
					break;
				}
			}
			req.open('get', noCache(url), true);
			req.send('');
	} catch(e) {
    // a browser not equiped to handle XMLHttp
		onError("This JavaScript function is not supported by your web browser.\n" + e);
	}
}

/**
 * Adds a timestamp at the end of the URL to prevent some browsers from caching.
 *
 * @param url The url to modify if required.
 */
function noCache(url){
	var qs = new Array();
	var arr = url.split('?');
	var scr = arr[0];
	if(arr[1]) qs = arr[1].split('&');
	qs[qs.length]='nocache='+new Date().getTime();
	return scr+'?'+qs.join('&');
}

/**
 * What happens when an error occurs.
 *
 * @param message The error message.
 */
function onError(message){
	alert(message);
}
