/**
 ** Ajax library	
 **	Copyright 2007 
 **	Daniel Oosterhuis for Bean IT
 **
 **/

var typing_delay = 500;
window.__pending = new Array();

/*
 * Implements a typing delay to limit the amount of requests
 */
function remote_lookup(action)
{
	if (window.__pending[action]) clearTimeout(window.__pending[action]); // if other key event is pending, cancel it
	//not needed here
	//show_progress(action); // animatie
	var args = remote_lookup.arguments;
	var pass=null;
	
	if (args.length>1) {
		pass = new Array();		
		
		for (var i=1;i<args.length;i++)	pass.push(args[i]!=''?args[i]:'null');
	}
	
	window.__pending[action] = setTimeout(function() { _remote_lookup(action,pass); }, typing_delay);
}

/*
 * Execute a lookup on the server and attach callback
 * @param action 	string 	action 
 * @param value 	mixed 	input
 * @returns			boolean
 */
function _remote_lookup(action)
{
	var args='';
	if (_remote_lookup.arguments[1]) while (el=_remote_lookup.arguments[1].shift()) args += '&args[]='+ urlencode(el);
	
		// user callback functie
	eval('var callback = '+action+'_cb');
	//alert(callback);
	if (!callback) callback = function(){ alert('No response  defined for handler action '+ action);};
		
	var url = __url_ajax_handler + '?action='+urlencode(action)+args;

	//alert(url);

	// systeem callback functie
	var cb = 
	function(response){ 
		//alert(response);
		try { 
			if (!response) response = "''";
			eval('var data = '+response+';'); 
		} catch (e) { 
			//alert('Exception occured in data lookup' + response); 
			alert('Exception occured in data lookup. Please contact the system administrator if the problem persists.'); 
			hide_progress(action); 
			return; 
		} 
		callback(data); 
		hide_progress(action); 
	}
	
	ajaxSend(url,cb);
}

// threadsafe asynchronous XMLHTTPRequest code
function ajaxSend(url, callback, returnFormat){


	// we use a javascript feature here called "inner functions"
	// using these means the local variables retain their values after the outer function
	// has returned. this is useful for thread safety, so 
	// reassigning the onreadystatechange function doesn't stomp over earlier requests.
	function ajaxBindCallback(){
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				if (ajaxCallback){
					returnFormat == "XML" ? ajaxCallback(ajaxRequest.responseXML) : ajaxCallback(ajaxRequest.responseText);
				} else {
					alert('no callback defined');
				}
			} else {
				alert("Er is een fout opgetreden bij het ophalen van de XML data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
			}
		}
	}
	
	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	var ajaxCallback = callback;
	
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}

		
function hide_progress(action)
{
	var progress = document.getElementById("_progress_anim_"+action);
	if (progress) progress.style.display = 'none';	
}

function show_progress(action)
{
	var progress = document.getElementById("_progress_anim_"+action);
	if (progress) progress.style.display = '';	
}
