//	add any event to any object in order to update any other item using AJAX
	function initializeAjax(arr,noajax){
		return ajax_initialize(null,arr,noajax);
	}

//----------------------------------------------------------------------------------------------------

	function in_array(stringToSearch, arrayToSearch) {
		for (s = 0; s < arrayToSearch.length; s++) {
			thisEntry = arrayToSearch[s].toString();
			if (thisEntry == stringToSearch) {
				return true;
			}
		}
		return false;
	}

//----------------------------------------------------------------------------------------------------

	function ajax_initialize(target, ajax_array, noajax)
	{
/*
	Incoming ajax_array must be defined as:

	var arr =	[
					["id","event",[["target id",display id],["target id 2",display id 2]]],
					["id_2","click",[["ul_province",14949],["ul_city",14950]]]
				];

	When making any changes properly test the following pages!
		http://green4ever.elicit.nl/kunstgras-bij-u-in-de-buurt/

	More info on properly updating elements and some IE issues can be found at
		http://www.mootools.net/docs
		http://mcarthurgfx.com/blog/article/parentnode-misbehavior-in-ie
		http://ramblings.gibberishcode.net/archives/cross-browser-ajax-updates-to-table-elements/9
*/
		var ajaxed = [];
	//	add ajax events for all array items
		if(typeof(ajax_array) != "undefined")
		{
			ajax_array.each(function(ele, i){
				ajax_initialize_element_event(ele[0], ele[1], ele[2]);
				ajaxed.push(ele[0]);
			});
		}
	//	add ajax events to all links of the target but only if the target is set as a result of a previous loaded ajax request
		if(target == '') target = 'window';
		if($(target))
		{
		//	add click event to link elements
			$(target).getElements('a').each(function(ele,i) {
				if(in_array(ele.get('id'),ajaxed) == false && in_array(ele.get('id'),noajax) == false) {
					ajax_initialize_element_event(ele, 'click', target);
				}
			});
		//	add submit event to form elements
			$(target).getElements('form').each(function(ele,i) {
				if(in_array(ele.get('id'),ajaxed) == false && in_array(ele.get('id'),noajax) == false) {
					ajax_initialize_element_event(ele, 'submit', target);
				}
			});
		}
		return true;
	}

//----------------------------------------------------------------------------------------------------

	function ajax_initialize_element_event_old(element, event, targets) {
	//  return if the element is not available
		if(!$(element)) return false;
		$(element).removeEvents(event);
		$(element).addEvent(event, function(e) {
			e.stop();
			targets.each(function(target,t) {
			//  prepare the request string
				if(event == 'change')
				{
					var req = '/ajax.php/?' + $(element).name + '=' + $(element).value + '&element=' + target[1];
				}
				else if(event=='submit'){
					var req = '/ajax.php/?element=' + target[1];
				}
				else
				{
					if(Browser.Engine.trident) { // IE requires a slightly different path ...
						var req =	$(element).protocol + '//' +
									$(element).hostname + '/' +
									$(element).pathname.substring(0,$(element).pathname.lastIndexOf('/')) +
									'/ajax.php' +
									$(element).search +
									($(element).search ? '&' : '?') +
									'element=' + target[1];
					} else {
						var req =	$(element).protocol + '//' +
									$(element).hostname +
									$(element).pathname.substring(0,$(element).pathname.lastIndexOf('/')) +
									'/ajax.php' +
									$(element).search +
									($(element).search ? '&' : '?') +
									'element=' + target[1];
					}
				}
			//	make the request
				var myHTMLRequest = new Request.HTML({
					method: (event=="submit"?"post":"get"),
					data: (event=="submit"?$(element):""),
					evalScripts: true,
					evalResponse: false,
					url: req,
					update: $(target[0]),
					onRequest:function(){
					//	prepare the target
						target[0] = ajax_initialize_target(target[0]);
					},
					onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
						//$(target).set("html",response);
						ajax_update_target(target[0], responseTree, responseElements, responseHTML);
						var newtarget = new Array(target);
						ajax_initialize(newtarget, newelements, newnoajax);
					}
				}).send();
			});
		});
	}
	function ajax_initialize_element_event(element, event, targets) {
	//	return if the element is not available
		if(!$(element)) return false;
		$(element).removeEvents(event);
		$(element).addEvent(event, function(e) {
			e.stop();
		//	prepare the request string
			if(event == 'change')
			{
				var req = '/ajax.php?' + $(element).name + '=' + $(element).value;
			}
			else if(event=='submit'){
				var req = '/ajax.php';
			}
			else
			{
				if(Browser.Engine.trident) { // IE requires a slightly different path ...
					var req =	$(element).protocol + '//' +
								$(element).hostname + '/' +
								$(element).pathname.substring(0,$(element).pathname.lastIndexOf('/')) +
								'/ajax.php' +
								$(element).search;
				} else {
					var req =	$(element).protocol + '//' +
								$(element).hostname +
								$(element).pathname.substring(0,$(element).pathname.lastIndexOf('/')) +
								'/ajax.php' +
								$(element).search;
				}
			}
		//	first request the action
			var myHTMLRequest = new Request.HTML({
				method: (event=="submit"?"post":"get"),
				data: (event=="submit"?$(element):""),
				async: false,	//	tell the browser to wait until request is completed
				evalScripts: true,
				evalResponse: false,
				url: req,
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
//alert('succes! .. blijkbaar :S');
					targets.each(function(target,t){
//alert(target);
					//	prepare the request string
						if(event == 'change')
						{
							var req = '/ajax.php?element=' + target[1];
						}
						else if(event=='submit'){
							var req = '/ajax.php?element=' + target[1];
						}
						else
						{
							if(Browser.Engine.trident) { // IE requires a slightly different path ...
								var req =	$(element).protocol + '//' +
											$(element).hostname + '/' +
											$(element).pathname.substring(0,$(element).pathname.lastIndexOf('/')) +
											'/ajax.php?element=' + target[1];
							} else {
								var req =	$(element).protocol + '//' +
											$(element).hostname +
											$(element).pathname.substring(0,$(element).pathname.lastIndexOf('/')) +
											'/ajax.php?element=' + target[1];
							}
						}
					//	make the request
						var myHTMLRequest = new Request.HTML({
							method: "get",
							data: "",
							evalScripts: true,
							evalResponse: false,
							url: req,
							update: $(target[0]),
							onRequest:function(){
							//	prepare the target
								target[0] = ajax_initialize_target(target[0]);
							},
							onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {

								ajax_update_target(target[0], responseTree, responseElements, responseHTML);
								var newtarget = new Array(target);
								ajax_initialize(newtarget, newelements, newnoajax);
							}
						}).send();
					});
				}
			}).send();
		});
	}

//----------------------------------------------------------------------------------------------------

	function ajax_initialize_target(target)
	{
		if(!target || target == 'window' || !$(target)) {
		//	make sure the dimmer window is available
			if(!$('window')) {
			//	create new elements
				var p = document.getElementById('page');
				var d = document.createElement('div');
				var c = document.createElement('div');
				var w = document.createElement('div');
			//	insert the new elements into the DOM
				document.body.appendChild(d);
				d.appendChild(c);
				d.appendChild(w);
			//	define some attributes
				d.setAttribute('id', 'dimmer');
				c.setAttribute('id', 'closeWindow');
				c.innerHTML = '<a href="javascript:null();" onclick="javascript: closeWindow();"><img src="/css/img/close.png" /></a>';
				w.setAttribute('id', 'window');
			}
			else
			{
				var w = document.getElementById('window');
			}

			if(!$('windowTarget'))
			{
				var t = document.createElement('div');
				$('window').empty();
				w.appendChild(t);
				t.setAttribute('id', 'windowTarget');
			}
			target = 'windowTarget';
		}

	//	replace the current contents with a loading image
		var l = new Element('div', {'class': 'loading'});
		var i = new Element('img', {'src': '/css/img/ajax-loader.gif'});
		l.appendChild(i);
		$(target).empty();
		$(target).appendChild(l);

	//	update the target
		return target;
	}

//----------------------------------------------------------------------------------------------------

	function ajax_update_target(target, responseTree, responseElements, responseHTML) {
	//	replace the target with the new html
		var newHTML = new Element('div', {html: responseHTML});
	//	exception for FireFox 2.0.0.2 in Windows
		if(Browser.Engine.gecko && Browser.Engine.version == 18) {
			$(target).empty();
			$(target).adopt(newHTML.getFirst()); // created for telos.elicit.nl popup forms (beheer/kapitalen/bewerk)
		} else if(Browser.Engine.trident){
	//	exception for IE ...
			$(target).empty();
			$(target).adopt(newHTML.getFirst());
		} else {
			newHTML.getFirst().replaces($(target));
		}
	}

//----------------------------------------------------------------------------------------------------

	var closeWindow = function(){if($('dimmer')){$('dimmer').destroy();}}

//----------------------------------------------------------------------------------------------------

