/**
 * Ajax interface
 *
 * @version 1.0.6
 *
 * Version control
 *
 * 1.0.6	Changed ajax loader icon location
 * 1.0.5	Removed non-oo code
 * 1.0.4	Minor bug fix to variable declaration
 */
function lib_Ajax()
{
	/**
	 * Class variables
	 */
	if(document.getElementById('package'))	{this.loaderimage = document.getElementById('package').value + '/templates/images/ajax-loader.gif';}
	else									{this.loaderimage = '/libraries/templates/images/ajax-loader.gif';}




	/**
	 * 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
			{
				var 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="'+this.loaderimage+'" class="ajax"> Pagina wordt geladen...';}
			else if (graphics == 2)	{html = '<center><img src="'+this.loaderimage+'" class="ajax"> Pagina wordt geladen...</center>';}
			else if (graphics == 3)	{html = '';}
			else if (graphics == 4)	{html = '<img src="'+this.loaderimage+'" 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;
	}



	/**
	 * Class variables
	 */
	 this.objXmlHttp = this.getXmlHttpObject();
}

