/**
*
* Module de gestion des requetes ajax
* @author Ludovic Hindryckx
* @version 0.1
*
**/

var mmp_xmlRequest = {
	id_dest:"",
	indicator: "",
	ajaxRequest:function(obj){
		this.id_dest = (document.getElementById(obj.id_dest) ? document.getElementById(obj.id_dest) : obj.id_dest);
		this.id_dest = (eval(this.id_dest) ? eval(this.id_dest) : this.id_dest);

		if(obj.indicator == undefined)
		this.indicator = "<img src='../images/ajax-loader.gif' alt='loading'/>";
		else this.indicator = "<img src='"+obj.indicator+"' alt='loading'/>";

		if(obj.complete != undefined)
		this.complete = obj.complete;

		switch (obj.type){
			case "FORM" :
				obj.type = "POST";
				YAHOO.util.Connect.setForm(document.getElementById(obj.formId), false);
			default:
				obj.type = "POST";
				obj.callback = (obj.callback == undefined ? callback : obj.callback);
			break;
			case "UPLOAD" :
				obj.callback = (obj.callback == undefined ? callbackUpload : obj.callback);
				obj.type = "POST";
				YAHOO.util.Connect.setForm(document.getElementById(obj.formId), true);
			break;
		}
		YAHOO.util.Connect.asyncRequest(obj.type, obj.url, obj.callback, (obj.flux == undefined ? "" : obj.flux));
	},
	handleSuccess:function(o){
		if(o.responseText != "" && this.id_dest){
			this.id_dest.innerHTML = o.responseText;
			var response = o.responseText;
			var tabJS;
				var myRegExp = /<script([^>]*)>([\s\S]*?)<\/script>/igm;
				while((tabJS = myRegExp.exec(response)) != null){
					eval(tabJS[2]);
				}
			if(this.complete != null)
			eval(this.complete);
		}
		
	},
	handleAbort:function(eventType, args) {
		alert(eventType+" :> "+args)
	},
	handleStart:function(eventType, args) {
	},
	handleFailure:function(o){
		alert(o.status + " : " + o.responseText);
	},
	start:function(){
		this.id_dest.innerHTML = this.indicator;
	},
	complete:""
};

var callback = {
	success:mmp_xmlRequest.handleSuccess,
	failure:mmp_xmlRequest.handleFailure,
	scope: mmp_xmlRequest
};


var callbackUpload = {
	onStart:mmp_xmlRequest.start, 
	onAbort:mmp_xmlRequest.handleAbort,
	upload:mmp_xmlRequest.handleSuccess,
	failure:mmp_xmlRequest.handleFailure,
	scope: mmp_xmlRequest
};