var loading = "<img src='img/ajax-loader.gif' alt='loading...'>";
var requestCache = new Array();

/*
 * Ajax requests function
 * performs post and get asynchronous requests
 *
 * Arguments
 * method - (string) The method can be either post or get
 * url - (string) The requested url
 * data - (string) The datas of the request in the form 'var1=value1&var2=value2'
 * target - (mixed) The element DOM Object or the element id of the DOM element that have to be updated with the request response
 *
 * Options (object)
 * cache - (bool default to false) Whether to cache the request result or not
 * cacheTime - (int default 3600000 [1hr]) The time in milliseconds to keep the request in cache
 * load - (mixed default null) The element DOM Object or the element id of the DOM element to use to show the loading image
 * script - (bool default false) True if scripts have to be executed, false if not
 * callback - (function dafault null) The function to call after the request has been executed
 * callback_params - (string default null) The params passed to the callback function
 *
 * if the called method has to return an error, must return a string like:
 * request error:Error description
 * this way the method is not executed and an alert is displayed with the message "Error description"
 *
 */
function ajaxRequest(method, url, data, target, options) {
	var opt = {
		cache: false,
		cacheTime: 3600000,
		load: null,
		script: false,
		callback: null,
		callback_params: null
	};
	$extend(opt, options);
	target = $type(target)=='element'
		? target
		: $chk($(target))
			? $(target)
			: null;
	if(opt.cache && $defined(requestCache[url+data]) && ($time() - requestCache[url+data][0] < opt.cacheTime)) {
		target.set('html', requestCache[url+data][1]); 
		return true;
	}

	var opt_load = $chk(opt.load)? ($type(opt.load)=='element'?opt.load:$(opt.load)):null;
	var request = new Request.HTML({
		evalScripts: opt.script,
		url: url,
		method:	method,
		data: data,
		onRequest: function() {
			if(opt_load) opt_load.set('html', loading); 
		},
		onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			if(opt_load) opt_load.set('html', ''); 
			rexp = /request error:(.*)/;
			var err_match = rexp.exec(responseHTML);
			if($chk(err_match)) alert(err_match[1]);
			else {
				if(target) target.set('html', responseHTML);
				if(opt.cache) requestCache[url+data] = new Array($time(), responseHTML);
				if(opt.callback && opt.callback_params) opt.callback(opt.callback_params);
				else if($chk(opt.callback)) opt.callback(); 
			}
		}
	}).send();

}

