/**
*
* Module de gestion des requetes ajax
* @author Ludovic Hindryckx
* @version 0.1
*
**/

var nrj_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=''/>";
		else
			this.indicator = "<img src='"+obj.indicator+"' alt=''/>";

		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.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));

		if(obj.start != undefined) eval(obj.start);
		else this.start(obj.id);

	},
	handleSuccess:function(o){

		if(o.responseText != "" && this.id_dest){
			this.id_dest.innerHTML = o.responseText;
			var response = o.responseText;
			var tabJS;
			while((tabJS = /<script([^>]*)>([\s\S]*?)<\/script>/igm.exec(response)) != null){
				eval(tabJS[2]);
			}
			if(this.complete != null)
			eval(this.complete);

			if(this.id_dest.value != undefined) this.id_dest.value = o.responseText;
		}
	},

	handleFailure:function(o){
		alert(o.status + " : " + o.responseText);
	},
	start:function(){
		this.id_dest.innerHTML = this.indicator;
	},
	complete:""
};

var callback = {
	success:nrj_xmlRequest.handleSuccess,
	failure:nrj_xmlRequest.handleFailure,
	scope: nrj_xmlRequest
};


var callbackUpload = {
	upload:nrj_xmlRequest.handleSuccess,
	failure:nrj_xmlRequest.handleFailure,
	scope: nrj_xmlRequest
};
