var http_request = false;
var data_received=new Array();
var ajax_func;
var output_type;


// escape() encodes most of the stuff we need to encode.
// it misses single (%27), double quotes (%22), the plus sign (%2B) and slash (%2F)
// so we replace those manually...

function URLencode(param) {
    return escape(param).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

function URLdecode(param) {
    return unescape(param).replace(/\%2B/g, '+').replace(/\%22/g, '"').replace(/\%27/g, "'").replace(/\%2F/g, '/');
}

function ajaxReq() {
	var url=ajaxReq.arguments[0];
	var method=ajaxReq.arguments[1];
	output_type=ajaxReq.arguments[2].toLowerCase(); // supported : XML, JSON, HTML
	ajax_func=ajaxReq.arguments[3];

	var content_type = '';
	if(output_type == 'json'){
		content_type = 'text/plain';
	}
	else if(output_type == 'xml' || output_type == 'html'){ 
		content_type = 'text/' + output_type;
	}
	else {
		alert("Missing Argument : Supported Output Type.");
		return false;
	}
	
	var param='';
	if(method=="GET"){
		param = param + '?';
	}

	for(var i=4; i<ajaxReq.arguments.length; i++){
		if(eval(i%2)!=0){
			param = param + URLencode(ajaxReq.arguments[ i ]);
		}
		else{
			if(param!='?' && param!=''){
				param = param + '&';
			}
			param = param + ajaxReq.arguments[ i ] + '=';
		}
	}

	http_request = false;
	if(window.XMLHttpRequest){ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType){
			// Note: The line http_request.overrideMimeType('text/xml'); 
			// Will trigger an error in FF if the page retrieve by XMLHttpRequest is not valid XML
			// See https://bugzilla.mozilla.org/show_bug.cgi?id=311724
			http_request.overrideMimeType(content_type);
			
		}
	}
	else if(window.ActiveXObject){ // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if(!http_request){
		alert("Giving up! :( Impossible de créer une instance XMLHTTP");
		return false;
	}

	if(method=="GET"){
		url = url + param;
	}
	http_request.open(method, url, true);

	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	http_request.onreadystatechange = dispatchContents;

	if(method=="POST"){
		http_request.send(param);
	}
	else{
		http_request.send(null);
	}
}

// Appel la fonction demandee par le client

function dispatchContents(){
	if(http_request.readyState == 4){
		if(http_request.status == 200){
			var param_data;
			if(output_type == 'xml'){
				param_data = http_request.responseXML;
			}
			else if (output_type == 'html'){
				param_data = http_request.responseText;
			}
			else if (output_type == 'json'){
				param_data = new Object();
				param_data = eval("(" + http_request.responseText + ")");
			}
			eval(ajax_func + "(param_data)");
		}
		else{
			alert('Unknown script error. Returned status is ' + http_request.status + '.');
		}
	}
}