/**
 * Ajax interface
 *
 * @version 1.0.1
 */
function lib_Ajax()
{
	/**
	 * Class variables
	 */
	 this.objXmlHttp = getXmlHttpObject();




	/**
	 * Using Object Detection for a Cross-Browser Solution
	 */
	this.getXmlHttpObject = function()
	{
		var obj = null;

		try
		{
			obj = new XMLHttpRequest();
			// e.g. Firefox
		}
		catch(err1)
		{
			try
			{
				obj = new ActiveXObject("Msxml2.XMLHTTP");
				// some versions IE
			}
			catch(err2)
			{
				try
				{
					obj = new ActiveXObject("Microsoft.XMLHTTP");
					// some versions IE
				}
				catch(err3)
				{
				}
			}
		}

		return obj;
	}




	/**
	 * Send Ajax request using POST
	 *
	 * @param string url the URL to post the request to
	 * @param string parameters the parameters to send with the request
	 * @param string callback the name of the function to use for callback
	 */
	this.postAjax = function(url, parameters, callback)
	{
		if(this.objXmlHttp)
		{
			try
			{
				rnd = (new Date().getTime()) + parseInt(Math.random()*99999999);

				this.objXmlHttp.onreadystatechange = callback;
				this.objXmlHttp.open("POST", url, true);
				this.objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.objXmlHttp.send(parameters + '&random=' + rnd);
			}
			catch(e)
			{
			}
		}
	}



	/**
	 * Ajax callback function
	 *
	 * @param string element the name of the element to print the reponse in
	 * @param integer graphics the loading graphics to display
	 * @param integer type the type of loading graphics (1=graphics are loaded in same element the response is loaded, 2=full screen loading page)
	 *
	 * @return boolean status true if callback complete, false if not
	 */
	this.callbackAjax = function(element, graphics, type)
	{
		var status = false;

		//check if state is "loaded"
		if(this.objXmlHttp.readyState == 4)
		{
			//check if server HTTP response is "OK"
			if(this.objXmlHttp.status == 200)
			{
				//close loading popup (if applicable)
				if(type == 2)
				{
						 if(			  document.getElementById('popupWindow2'))	{			   document.getElementById('popupWindow2').innerHTML = '';}
					else if(window.parent.document.getElementById('popupWindow2'))	{window.parent.document.getElementById('popupWindow2').innerHTML = '';}
					obj_lib_popup = new lib_Popup();
					obj_lib_popup.closePopUp(2);
				}

				//print response
				document.getElementById(element).innerHTML = this.objXmlHttp.responseText;
				status = true;
			}
			else
			{
				//display error message
				document.getElementById(element).innerHTML = '';
			}
		}
		else
		{
			//while waiting for the page to load, display loading graphics(html)

			//get selected loading graphics
			var html = '';
				 if	(graphics == 1)	{html = '<img src="/users/templates/images/ajax-loader.gif" class="ajax"> Pagina wordt geladen...';}
			else if (graphics == 2)	{html = '<center><img src="/users/templates/images/ajax-loader.gif" class="ajax"> Pagina wordt geladen...</center>';}
			else if (graphics == 3)	{html = '';}
			else if (graphics == 4)	{html = '<img src="/users/templates/images/ajax-loader.gif" class="ajax">';}

			if(type == 1)
			{
				//display selected graphics in appropriate element
				document.getElementById(element).innerHTML = html;
			}
			else
			{
				//clear result element
				document.getElementById(element).innerHTML = '';

				//open popup
				obj_lib_popup = new lib_Popup();
				obj_lib_popup.openPopUp(2, 100, 100);

				//display selected graphics in popup
					 if(			  document.getElementById('popupWindow2'))	{			   document.getElementById('popupWindow2').innerHTML = html;}
				else if(window.parent.document.getElementById('popupWindow2'))	{window.parent.document.getElementById('popupWindow2').innerHTML = html;}
			}
		}

		return status;
	}
}










/****************************** NON-OBJECT ORIENTED SOLUTION: CURRENTLY STILL IN USE ******************************/

/**
 * Using Object Detection for a Cross-Browser Solution
 */
function getXmlHttpObject()
{
	var obj = null;

	try
	{
		obj = new XMLHttpRequest();
		// e.g. Firefox
	}
	catch(err1)
	{
		try
		{
			obj = new ActiveXObject("Msxml2.XMLHTTP");
			// some versions IE
		}
		catch(err2)
		{
			try
			{
				obj = new ActiveXObject("Microsoft.XMLHTTP");
				// some versions IE
			}
			catch(err3)
			{
			}
		}
	}

	return obj;
}



/**
 * XML HTTP object
 */
var xmlHttp;



/**
 * Send Ajax request using POST
 *
 * @param string url the URL to post the request to
 * @param string parameters the parameters to send with the request
 * @param string callback the name of the function to use for callback
 */
function postAjax(url, parameters, callback)
{
	xmlHttp = getXmlHttpObject();

	if(xmlHttp)
	{
		try
		{
			rnd = (new Date().getTime()) + parseInt(Math.random()*99999999);

			xmlHttp.onreadystatechange = callback;
			xmlHttp.open("POST", url, true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.send(parameters + '&random=' + rnd);
		}
		catch(e)
		{
		}
	}
}



/**
 * Ajax callback function
 *
 * @param string element the name of the element to print the reponse in
 * @param integer graphics the loading graphics to display
 * @param integer type the type of loading graphics (1=graphics are loaded in same element the response is loaded, 2=full screen loading page)
 *
 * @return boolean status true if callback complete, false if not
 */
function callbackAjax(element, graphics, type)
{
	var status = false;

	//check if state is "loaded"
	if(xmlHttp.readyState == 4)
	{
		//check if server HTTP response is "OK"
		if(xmlHttp.status == 200)
		{
			//close loading popup (if applicable)
			if(type == 2)
			{
					 if(			  document.getElementById('popupWindow2'))	{			   document.getElementById('popupWindow2').innerHTML = '';}
				else if(window.parent.document.getElementById('popupWindow2'))	{window.parent.document.getElementById('popupWindow2').innerHTML = '';}
				obj_lib_popup = new lib_Popup();
				obj_lib_popup.closePopUp(2);
			}

			//print response
			document.getElementById(element).innerHTML = xmlHttp.responseText;
			status = true;
		}
		else
		{
			//display error message
			document.getElementById(element).innerHTML = '';
		}
	}
	else
	{
		//while waiting for the page to load, display loading graphics(html)

		//get selected loading graphics
		var html = '';
		     if	(graphics == 1)	{html = '<img src="/users/templates/images/ajax-loader.gif"> Pagina wordt geladen...';}
		else if (graphics == 2)	{html = '<center><img src="/users/templates/images/ajax-loader.gif"> Pagina wordt geladen...</center>';}
		else if (graphics == 3)	{html = '';}
		else if (graphics == 4)	{html = '<img src="/users/templates/images/ajax-loader.gif">';}

		if(type == 1)
		{
			//display selected graphics in appropriate element
			document.getElementById(element).innerHTML = html;
		}
		else
		{
			//clear result element
			document.getElementById(element).innerHTML = '';

			//open popup
			obj_lib_popup = new lib_Popup();
			obj_lib_popup.openPopUp(2, 100, 100);

			//display selected graphics in popup
				 if(			  document.getElementById('popupWindow2'))	{			   document.getElementById('popupWindow2').innerHTML = html;}
			else if(window.parent.document.getElementById('popupWindow2'))	{window.parent.document.getElementById('popupWindow2').innerHTML = html;}
		}
	}

	return status;
}

