
function XHRConnection() {
	var xhr = false;
	var data = new String();
	var elementIdCible = new String();

	var init = true;
	var etat;
	var reponse;

	var debug = false;

	try {
		xhr = new XMLHttpRequest();		
	}
	catch (e) {
		if (debug) { alert('Objet XMLHttpRequest non créé'); }
		try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			if (debug) { alert('Objet ActiveXObject("Microsoft.XMLHTTP") non créé'); }
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				if (debug) { alert('Objet ActiveXObject("Msxml2.XMLHTTP") non créé'); }
				xhr = false;
				xhr.setInit(false);
			}
		}
	}

	this.razData = function() {
		data = new String();
		data = '';
		if(typeof session_name=='string' && typeof session_id=='string')
			this.ajouteData(session_name,session_id);
	};
	
	
	this.ajouteData = function(param, valeur) {
		if(data.length == 0)
			data += param + '=' + escape(valeur);
		else
			data += '&' + param + '=' + escape(valeur);
	};
	
	this.setInit = function(val) {
		init = id;
	};
	
	this.getInit = function() {
		return init;
	};
	
	this.setCible = function(id) {
		elementIdCible = id;
	};
	
	this.getCible = function() {
		return elementIdCible;
	};
	
	this.sendAndLoad = function(URL, mode, callBack) {
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4 && xhr.status == 200) {
				if (typeof callBack == "function") {
						callBack(xhr);
						return;
				}
				else if (elementIdCible.length > 0){
					try {
						document.getElementById(elementIdCible).innerHTML = xhr.responseText;
					}
					catch(e) {
						if (debug) alert('onreadystatechange sans callback:' + e);
					}
					return;
				}
			}
		};
		switch(mode) {
			case "GET":
				try {
					if(data.length > 0) URL += "?" + data;
					xhr.open("GET", URL);
					xhr.send(null);
				}
				catch(e) {
					if(debug) alert('dans GET:' + e);
					return false;
				}
				break;
			case "POST":
				try {
					xhr.open("POST", URL); 
					xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					xhr.send(data);
				}
				catch(e) {
					if(debug) alert('dands POST:' + e);
					return false;
				}
				break;
			default :
				return false;
				break;
		}
		return true;
	};
	
	return this;
}

var affich = function(xhr) {
	alert('je suis dans mon callback');
	alert(xhr.responseText);	
}

function affiche_avatar(nom, val) {
	xhr=new XHRConnection();
	test=xhr.getInit();
	if(test) {
		with(xhr) {
			xhr.setCible('avatar');
			razData();
			ajouteData(nom, val);
			xhr.sendAndLoad('/comparateur/avatar/index.php', "GET");
		}
		return true;
	} else {
		return false;
	}
}