/**************************************************
Generalised AJAX Functions
-------------------------------------------------
initXMLHTTPObj()
AJAX(actionURL, strMethod, containerObj, formObj, strFnName)


Tested against:
MSIE 7.0.5730
Mozilla FF 3.0.4
Safari 3.1.1
Opera 9.27

Rene Modesto, January 2008
**************************************************/

function initXMLHTTPObj()
{
	/*************************************************
	* Creates an instance of the XMLHttpRequest Object, and returns it
	**************************************************/
	var xmlHttp = false;
	if (window.XMLHttpRequest)
	{   
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{    // Internet Explorer
	    try
	    {
	    	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch (e)
	    {
	    	try
	        {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	        }
	      	catch (e)
	        {
	        	xmlHttp = false;
	        	alert('AJAX object init failed');
	        }
	     }
	}
	return xmlHttp;
}

function AJAX(actionURL, strMethod, containerObj, formObj, strFnName)
{
	//alert('starting AJAX');
	/*************************************************
	* Retrieves an output based on a form data
	**************************************************/
	var xmlHttp = initXMLHTTPObj();
	var formData = "";
	var origHTML;
	
	if (formObj != null)
	{
		// grab and URLencode the form data
		for (i = 0; i < formObj.elements.length; i++)
		{
			//data validation
			if (formObj.elements[i].type == 'checkbox' || formObj.elements[i].type == 'radio' )
			{	
				if (formObj.elements[i].checked)
				{
					if (i != 0)
						formData = formData + '&'; 
					formData = formData + formObj.elements[i].name + '=' + encodeURIComponent(formObj.elements[i].value);
				}
			}
			else
			{
				if (i != 0)
						formData = formData + '&'; 
				formData = formData + formObj.elements[i].name + '=' + encodeURIComponent(formObj.elements[i].value);
			}
		}
	}
		
	// if the XMLHTTPrequest obj exists, send off the data
	if (xmlHttp)
	{
		origHTML = containerObj.innerHTML;
		containerObj.innerHTML = "<div style='text-align:center;'><img src='/images/gfx/icons/ajax-kit.gif' alt='loading...'></div>";
		if(strMethod.toUpperCase() == "POST")
		{
			xmlHttp.open("POST", actionURL, true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", formData.length);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(formData); 
		}
		else if (strMethod.toUpperCase() == "GET")
		{
			xmlHttp.open("GET", actionURL, true);
			xmlHttp.send(null); 	
		}
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if (xmlHttp.status == 200 || xmlHttp.status == 304)
				{
					//alert(strFnName + "(containerObj, xmlHttp.responseText);");
					if (strFnName != null)
						eval(strFnName + "(containerObj, xmlHttp.responseText);");
					else
						containerObj.innerHTML = xmlHttp.responseText;
				}
				else
				{
					alert("Error " + xmlHttp.status);
					containerObj.innerHTML = origHTML;
				}
			}
		}
	}
}
