/**
 * This file contains AJAX functions used throughout the entire application. 
 * Compatible with Safari 3+, Firefox 1.5..xx and IE 6 >.
 *
 * @author Herman Bredewoud
 * @version 1.00
 * @name Script AJAX
 */
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Array with XMLHttpRequests.
var m_oAjax_http = new Array();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
*	This function will search on an event for an object that has AJAX attributes (URL, Destination, Mode).
*	If those attributes have been found the getXML function will be called.
*
*	@param object a_oEvent
*/
function AJAX_search(a_oEvent) {
	// Arrays with the object attributes values.
	var aURL;
	var aDest;
	var aMode;
	
	// Boolean to stop the parent walkover loop.
	var bAJAXObjectFound = false;
	
	// Target object.
	var oHTML = OBJ_getTarget(a_oEvent);
	var oParent = oHTML;
	
	// Loop through the parentNodes to check if there are AJAX attributes;
	for (var i=0; i<CONF_iAJAXParentCheck; i++) {
		if (OBJ_hasAttributes(oHTML)) {
			for (var j=0; j<oHTML.attributes.length; j++) {
				if (oHTML.attributes[j].nodeName.toLowerCase() == 'url') {
					aURL = oHTML.attributes[j].nodeValue.split(',');
				}
				if (oHTML.attributes[j].nodeName.toLowerCase() == 'destination') {
					aDest = oHTML.attributes[j].nodeValue.split(',');
				}
				if (oHTML.attributes[j].nodeName.toLowerCase() == 'mode') {
					aMode = oHTML.attributes[j].nodeValue.split(',');
				}
				
				// If the URL attribute has been found stop the loop.
				if (OBJ_isArray(aURL)) {
					bAJAXObjectFound = true;
					break;
				}
			}
			
			// If no AJAX attributes are found set oHTML to the parent node.
			if (bAJAXObjectFound) {
				break;
			} else {
				oHTML = oParent.parentNode;
			}
		}
	}
	
	
	// If AJAX attributes are found get the XML.
	if (bAJAXObjectFound) {
		if (aURL.length == aDest.length) {
			for (var i=0; i<aURL.length; i++) {
				if (aMode.length == 1) {
					AJAX_getXML(aURL[i], aDest[i], aMode.toString());
				} else {
					AJAX_getXML(aURL[i], aDest[i], aMode[i]);
				}
			}
		} else {
			alert("ERROR: URL and DESTINATION do not match.");
		}
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*	This function can be called to make an XMLHttp request and get content from the server.
*	The object given needs to contain 3 specific attributes url, destination and mode.
*
*	@param string a_sURL
*	@param string a_sDest
*	@param string a_sMode
*/
function AJAX_getXML(a_sURL, a_sDest, a_sMode) {
	a_sCallback		= '';
//	if (oHtml.attributes['callback']) {
//		a_sCallback		= oHtml.attributes['callback'].nodeValue;
//	}
	
	//Show the loader.
	AJAX_loader(true);
	
	if (a_sMode=="download") {
		AJAX_createIframe(a_sURL);
		AJAX_runOnComplete();
	} else {		
		AJAX_OnReadystateChange(AJAX_getXMLHttpRequest(), a_sURL, a_sDest, a_sMode, a_sCallback);
	}
}

/**
*	This function can be called to make an XMLHttp request and get content from the server.
*	The diverence between AJAX_getXML and AJAX_getXMLmanual is that AJAX_getXMLmanual creates a new HTML object and
*	creates the attributes url, destination and mode. The new created object will be send to the AJAX_getXML to get the request
*	from the server.
*
*	@param string a_sURL
*	@param string a_sDest
*	@param string a_sMode
*/
function AJAX_getXMLmanual(a_sURL, a_sDest, a_sMode) {
	AJAX_getXML(a_sURL, a_sDest, a_sMode);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*	This function will set the content of the destination with the content returned from the XMLHttp request.
*
*	@param string sDestination
*	@param string sContent
*/
function AJAX_setXML(sDestination, sContent, sMode) {
	if (oDestination=document.getElementById(sDestination)) {
		if (sMode == "append") {
			oDestination.innerHTML += sContent;
		} else if (sMode == "replace") {
			oDestination = OBJ_getParent(oDestination);
			
			oDestination.innerHTML = sContent;
//			oDestination.outerHTML = sContent;
		} else if (sMode == "replacechildren") {
						
			oDestination.innerHTML='';
			
			var a_oDiv = document.createElement('div');
			a_oDiv.innerHTML = sContent;

			oDestination.appendChild(a_oDiv);
			
			// Check the source if there are flash items
			if (CONF_bSWF) OBJ_flash(oDestination);
			
		} else if (sMode == 'javascript') {
			eval(sContent);
		}
	} else {
		if (sMode == 'javascript') {
			eval(sContent);
		} else {
			alert ("Destination: "+sDestination+" not found.");
		}
	}
}

/**
*	This function will create a XMLHttp request and return the XMLHttp object.
*	Also this function will search the m_oAjax_http for XMLHttp objects that have finished and deletes them from the array.
*/
function AJAX_getXMLHttpRequest() {
	var m_oAjax_http_length = m_oAjax_http.length;
	
	// Check if an AJAX object is ready and can be removed from the array to save memory.
	if (m_oAjax_http_length == 0) {
		m_oAjax_http[m_oAjax_http_length] = '';
	} else {
		for (var i=0; i< m_oAjax_http.length; i++) {
			if (m_oAjax_http[i]) {
				if (m_oAjax_http[i].readyState == 0 || m_oAjax_http[i].readyState == 4) {
					delete m_oAjax_http[i];
					m_oAjax_http[i] = null;
					m_oAjax_http.splice(i,1);
					i--;
				}
			}
		}
		m_oAjax_http_length = m_oAjax_http.length;
	}
	
	// ActiveXObject IE en XMLHttpRequest firefox/safari
	if (window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlhttp = false;
			}
		}
		
		return m_oAjax_http[m_oAjax_http_length] = xmlhttp;
	} else if (window.XMLHttpRequest) {
		return m_oAjax_http[m_oAjax_http_length] = new XMLHttpRequest();
	}
}

/**
*	This function will create an onreadystatechange function for the given XMLHttp object.
*	If the readystate of the XMLHttp object is 4 the function AJAX_setXML will be called.
*	
*	@param object oXml
*	@param string sUrl
*	@param string sDestination
*/
function AJAX_OnReadystateChange(oXml, sUrl, sDestination, sMode, sCallback) {
	try {
		oXml.onreadystatechange = function() {
			if (oXml.readyState == 4) {
				
				AJAX_setXML(sDestination, oXml.responseText, sMode);
				if (sCallback!='') {
					eval(sCallback);
				}
				
				AJAX_runOnComplete();
			}
		}
		
		AJAX_send(oXml, sUrl);
	} catch(e) {
		alert(e);		
	}
}

/**
*	This function will send header information.
*
*	@param object oXml
*	@param string sUrl
*/
function AJAX_send(oXml, sUrl) {
	var queryStr = sUrl.split("?");
	if (queryStr[0] == '') {
		queryStr[0] = sUrl;
	}

	oXml.open('POST', queryStr[0], true);
	
	oXml.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXml.send(queryStr[1]);
}

/**
*	These functions need to be called if readyState == 4.
*/
function AJAX_runOnComplete() {
	//Hide the loader.
	AJAX_loader(false);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*	This function will create and show or hide the loader screen. Use css to create the style for the 2 id elements.
*
*	@param boolean a_bShow
**/
function AJAX_loader(a_bShow) {
	if (CONF_bLoader) {
		//Show the loading background. Used for disabling user controls.
		if (!(a_oLoaderBackground = document.getElementById('AJAX_loader_background'))) {
			var element_loader_background = document.createElement('div');
			element_loader_background.setAttribute('id', 'AJAX_loader_background');
			document.body.appendChild(element_loader_background);
	
			a_oLoaderBackground = document.getElementById('AJAX_loader_background');
			a_oLoaderBackground.style.display = 'block';
		} else {
			if (a_oLoaderBackground.style.display=='none' && a_bShow) {
				a_oLoaderBackground.style.display = 'block';
			} else {
				a_oLoaderBackground.style.display = 'none';
			}
		}
		
		//Show the loading screen.
		if (!(a_oLoader = document.getElementById('AJAX_loader'))) {
			var element = document.createElement('div');
			element.setAttribute('id', 'AJAX_loader');
			document.body.appendChild(element);
	
			a_oLoader = document.getElementById('AJAX_loader');
			a_oLoader.innerHTML = '<img src="/app_images/loader/loader.gif" />';
			a_oLoader.style.display = 'block';
		} else {
			if (a_oLoader.style.display=='none' && a_bShow) {
				a_oLoader.style.display = 'block';
			} else {
				a_oLoader.style.display = 'none';
			}
		}
		
		//Center the loading screen.
		OBJ_center(a_oLoader);
	}
}

function AJAX_createIframe(a_sUrl) {
	if (!(element_Header = document.getElementById('frame_header'))) {
		var element_Header = document.createElement('iframe');
		element_Header.setAttribute('id', 'frame_header');
		element_Header.setAttribute('src', a_sUrl);
		element_Header.style.backgroundColor = 'rgb(252,252,252)';
		element_Header.style.display = 'block';
		element_Header.style.width = '100%';
		element_Header.style.height = '400px';
		document.body.appendChild(element_Header);
	} else {
		element_Header.src = a_sUrl;
	}
}


