// + --------------------------------------------------------------------------------------
// + XHRConnection
// + V1.2
// + Thanh Nguyen, http://www.sutekidane.net
// + 14.06.2005
// + http://creativecommons.org/licenses/by-nc-sa/2.0/fr/deed.fr
// adaptée par marko_
// + --------------------------------------------------------------------------------------
function XHRConnection() {
	
	var conn = false;
	var datas = new String();

	try {
		conn = new XMLHttpRequest();		
	}
	catch (error) {
		try {
			conn = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (error) {
			try {
				conn = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (error) {
				conn = false;
			}
		}
	}

	this.resetData = function() {
		datas = new String();
		datas = '';
	};

	this.appendData = function(pfield, pvalue) {
		datas += (datas.length == 0) ? pfield+ "=" + encodeURIComponent(pvalue) : "&" + pfield + "=" + encodeURIComponent(pvalue);
	};
 

	this.sendAndLoad = function(Url, httpMode, callBack) {
		httpMode = httpMode.toUpperCase();
		conn.onreadystatechange = function() {
			if (conn.readyState == 4 && conn.status == 200) {
				// Si une fonction de callBack a été définie
				
				if (typeof callBack == "function") {
					callBack(conn);
					return;
				}
			}
		};
		
		try {
			conn.open("POST", Url); 
			conn.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestHeader("User-Agent", "XHRConnection v1.2");
			datas = datas.length == 0 ? '.' : datas; //__ il faut des données pour IE sinon il est incapable de gérer la connexion HTTP
			conn.send(datas);
		}
		catch(error) {
			return false;
		}

		return true;
	};
	return this;
}