var ajax_ListeWindowDiv = new Array();
function ajax_AjouterWindowDiv(id, url, isDoRequestForEachRequest, isLoaded) {
	e = new Array();
	e['id'] = id; e['url'] = url; e['isLoading'] = false; e['isLoaded'] = isLoaded;
	e['isDoRequestForEachRequest'] = isDoRequestForEachRequest;
	ajax_ListeWindowDiv[id] = e;
}
function ajax_setContenuLoaded(id) {
	ajax_ListeWindowDiv[id]['isLoaded'] = true;
}
function ajax_setContenuUnLoaded(id) {
	ajax_ListeWindowDiv[id]['isLoaded'] = false;
}
function ajax_InsertBeforeTagBody(html) {
	bodies = document.getElementsByTagName('body');
	if (bodies != null && bodies.length > 0) {
		new Insertion.Top(bodies[0], html);
	}
}
function ajax_RemplirWindowDiv(id, events) {
	if (ajax_ListeWindowDiv[id]['isLoading']) return;
	if (!ajax_ListeWindowDiv[id] ['isDoRequestForEachRequest'] && ajax_ListeWindowDiv[id] ['isLoaded']) return;
	url_request = ajax_ListeWindowDiv[id]['url'];
	document.getElementById(id + '_chargement').style.display = 'none';
	document.getElementById(id + '_contenu').style.display = 'none';
	document.getElementById(id + '_erreur').style.display = 'none';
	/*
	** COMMENCEMENT AJAX
	*/
	ajax_ListeWindowDiv[id]['isLoading'] = true; document.getElementById(id + '_chargement').style.display = 'block';
	new Ajax.Request(
		url_request, {
			method: 'get',
			onException : function(request, exception) {
				ajax_ListeWindowDiv[id]['isLoading'] = false;
				document.getElementById(id + '_chargement').style.display = 'none';
				SporteoAjax.afficherErreur("Une erreur javascript s'est produite ("+exception.message+")", id + '_erreur');
				if (events != null && events['onError'] != null) { events.onError(); }
			},
			onFailure : function(obj) {
				ajax_ListeWindowDiv[id]['isLoading'] = false;
				document.getElementById(id + '_chargement').style.display = 'none';
				SporteoAjax.afficherErreur("Une erreur s'est produite ("+obj.status+")", id + '_erreur');
				if (events != null && events['onError'] != null) { events.onError(); }
			},
			onSuccess: function(obj) {
				if (SporteoAjax.isError(obj.responseText)) {
					SporteoAjax.afficherErreur(SporteoAjax.getHtmlError(obj.responseText), id + '_erreur');
					document.getElementById(id + '_chargement').style.display = 'none';
					if (events != null && events['onError'] != null) { events.onError(); }
				} else {
			    	document.getElementById(id + '_erreur').style.display = 'none';
			    	document.getElementById(id + '_chargement').style.display = 'none';
			    	document.getElementById(id + '_contenu').innerHTML = obj.responseText;
			    	try {
			    		obj.responseText.evalScripts();
			    	} catch (e) {
			    		js_executed = obj.responseText.extractScripts();
			    		str_js_executed = '';
			    		if (js_executed != null && js_executed.length > 0) {
			    			reg = new RegExp('\n');
			    			for(i=0;i<js_executed.length;i++) {
			    				str_js_executed = '<br /><pre>' + js_executed[i] + '</pre>';
			    			}
			    		}
			    		SporteoAjax.afficherErreur("Une erreur javascript s'est produite pouvant bloquer le bon fonctionnement de la page.<br />Description de l'erreur : <br />Message :  "+e.message+"<br />Fichier : " + e.fileName + "<br />Ligne : " + e.lineNumber + "<br />Caller : " + e.caller + "<br />Javascript failed : " + str_js_executed, id + '_erreur');
			    	}
			    	document.getElementById(id + '_contenu').style.display = 'block';
			    	if (events != null && events['onSuccess'] != null) { events.onSuccess(); }
				}
				ajax_ListeWindowDiv[id]['isLoading'] = false;
				ajax_ListeWindowDiv[id]['isLoaded'] = true;
			}
	    }
	);
}

HtmlAjax = Class.create();
HtmlAjax.prototype = {
    initialize: function(id) {
        this.id = id;
        this.contenu = document.getElementById(this.id + '_contenu').innerHTML;
    },
    unloaded: function() {
    	document.getElementById(this.id + '_contenu').style.display = 'none';
    	document.getElementById(this.id + '_contenu').innerHTML = '';
		ajax_setContenuUnLoaded(this.id);
	},
	changeUrlToCall: function(url) { ajax_ListeWindowDiv[this.id]['url'] = url; },
	load: function(events) {
		ajax_RemplirWindowDiv(this.id, events);
	},
	restoreContenu: function() {
		document.getElementById(this.id + '_contenu').style.display = 'block';
		document.getElementById(this.id + '_contenu').innerHTML = this.contenu;
	}
};