/*global ActiveXObject */
/* Request and display a AJAX page */
function ASyncPage(requestFile, targetDiv, formId, method, loadingDiv)
{
	// Base vars
	this.loadingId = loadingDiv;
	this.formId = formId;
	this.targetdiv = targetDiv ? document.getElementById(targetDiv) : false;
	this.filename = requestFile;
	this.method = method.toLowerCase();
	this.baseurl = '';
	this.error = false;
	this.isworking = false;
	this.callback = null;
	this.callback_obj = null;
	this.callback_func = null;
	var self = this;
	var X_XDR = 4;
	var X_XHR = 3;
	var X_MSXML2 = 2;
	var X_MSXML = 1;
	var X_NONE = 0;
	this.xtype = X_NONE;
	this.oldRes = '';

	/* Request a page */
	this.requestPage = function()
	{
		this.getAjaxObject();
		this.form = this.formId ? document.getElementById(this.formId) : false;
		this.loadingdiv = (this.loadingId ? document.getElementById(this.loadingId) : false);

		if (this.xmlhttp && !this.isworking && this.checkErrors())
		{
			if (this.loadingdiv) { this.loadingdiv.style.display = 'block'; }
			this.isworking = true;
			var params = this.getQueryString();

			// Try open and send
			try
			{
				if (this.xtype == X_XDR)
					this.xmlhttp.onload = this.requestCallback;
				else
					this.xmlhttp.onreadystatechange = this.requestCallback;

				this.xmlhttp.open(this.method, this.baseurl + this.filename + (this.method == 'get' ? params : ''), true);

				// Set post headers
				if (this.method == 'post' && this.xtype != X_XDR)
				{
					this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					this.xmlhttp.setRequestHeader('Content-Length', params.length);
					this.xmlhttp.setRequestHeader('Connection', 'close');
				}

				// Set ajax request header
				this.xmlhttp.setRequestHeader('X_REQUESTED_WITH', 'XMLHttpRequest');

				this.xmlhttp.send(this.method == 'post' ? params : null);
			}
			catch (e)
			{
				alert(e.message);
				this.isworking = false;
			}
		}
		else
		{
			if (!this.isworking)
			{
				// Show error message
				if (this.loadingdiv) this.loadingdiv.style.display = 'block';
				alert('Deze pagina kon helaas niet worden opgehaald...\r\nFoutmelding: ' + this.error);
				this.isworking = false;
			}
		}
	};

	/* Callback function */
	this.requestCallback = function()
	{
		// Read status
		try
		{
			var state = parseInt(self.xmlhttp.status);
		}
		catch (e)
		{
			var state = 0;
		}

		try
		{
			if ((self.xmlhttp.readyState == 4 && state == 200) || (self.xmlhttp.readyState == 0 && state == 0))
			{
				// FF error on very fast refersh
				if (self.xmlhttp.readyState == 0 && state == 0)
				{
					if(self.targetdiv) { self.targetdiv.innerHTML = self.oldRes; }
				}
				else
				{
					if(self.targetdiv) { self.targetdiv.innerHTML = self.xmlhttp.responseText; }
					self.oldRes = self.xmlhttp.responseText;
				}

				if (self.loadingdiv) { self.loadingdiv.style.display = 'none'; }
				self.isworking = false;
				
				// Fire callback function if set
				//if(typeof self.callback == 'function')
				//{
				if(typeof self.callback_func == 'function')
				{
					if(typeof self.callback_obj == 'object')
					{
						setTimeout(self.delegate(self.callback_obj, self.callback_func, self.xmlhttp.responseText), 10);
					}
					else
					{
						self.callback_func(self.xmlhttp.responseText);
					}
				}
					//self.callback(self.xmlhttp.responseText);
					//self.callback(self.xmlhttp.responseText);
				//}
			}
			else if (state == 404) throw '404 Page not found';
		}
		
		// IE exception on very fast refersh
		catch (e)
		{
			alert('EXCEPTION\nNaam: ' + e.name + '\nBericht: ' + e.message);
			if(self.targetdiv) { self.targetdiv.innerHTML = self.oldRes; }
			self.isworking = false;
			if (self.loadingdiv) self.loadingdiv.style.display = 'none';
		}
	};

	/* Try create ajax object */
	this.getAjaxObject = function()
	{
		var ok = false, n = 3; // Op 4 zeten om XDomainRequest aan te zetten (nog niet geimplementeerd)
		do
		{
			try
			{
				switch (n)
				{
					case X_XDR: this.xmlhttp = new XDomainRequest(); break;
					case X_XHR: this.xmlhttp = new XMLHttpRequest(); break;
					case X_MSXML2: this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); break;
					case X_MSXML: this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); break;
				}
				this.xtype = n;
				ok = true;
			}
			catch (e)
			{
				this.xmlhttp = false;
				this.error = e;
			}
		}
		while ((--n > 0 && !ok) || !ok);
	};
	
	/* Creates query string from form fields */
	this.getQueryString = function()
	{
		var retval = '';
		var sep = '';

		if (this.form)
		{
			var retval = (this.method == 'get' ? '?' : '');

			for (var i = 0; i < this.form.elements.length; i++)
			{
				sep = (i + 1 == this.form.elements.length) ? '' : '&';
				switch (this.form.elements[i].type)
				{
					case 'checkbox':
						if (this.form.elements[i].checked)
						{
							retval += this.form.elements[i].name + "=on" + sep;
						}
						break;
					case 'radio':
						if (this.form.elements[i].checked)
						{
							retval += this.form.elements[i].name + "=" + this.form.elements[i].value + sep;
						}
						break;
					default:
						retval += this.form.elements[i].name + "=" + this.form.elements[i].value + sep;
				}
			}
		}
		return retval;
	};

	/* Check all required vars */
	this.checkErrors = function()
	{
		var err = false;
		if (!(this.method == 'get' || this.method == 'post')) { err = 'Invalid request method'; }
		else if (!this.targetdiv && this.targetdiv !== false) { err = 'Invalid target object'; }
		else if (!this.form && this.form !== false) { err = 'Invalid source form'; }
		else if (!this.loadingdiv && this.loadingdiv !== false) { err = 'Invalid loading object'; }
		else if (this.filename.length < 1) { err = 'Invalid request file'; }
		this.error = err;
		return !err;
	};
	
	/* Callback functie instellen */
	this.setCallback = function(func, obj)
	{
		this.callback_func = func;
		this.callback_obj = obj;
	};
	
	/* Returnt data naar object functie */
	this.delegate = function(o, func, data)
	{
		return function()
		{ 
			func.call(o, data);
		};
	};
}

/******************* Scripts voor het ophalen van ajax pagina's *******************/

// Bereken reis tab
function getUsersOnline()
{
	getUsersOnline.p = new ASyncPage('ajax.gebruikersonline.php', 'gebruikersonline', null, 'get', null);
	getUsersOnline.p.baseurl = 'admin/';
	getUsersOnline.p.requestPage();
	setTimeout('getUsersOnline()', 5000);
}