var XmlHttp = Class.create();
XmlHttp.prototype = {
	initialize : function(sUrl, sMethod, encoding, asXml) {
		this.url = sUrl;
		this.method = sMethod || "GET";
		this.encoding = encoding || "ISO-8859-1";
		this.asXml = ("undefined" != typeof(asXml) ? asXml : true);

		this.m_oObj;
		this.m_aParam = new Array();

		this.onError = false;
		this.status = null;
		this.responseText = null;
		this.resonseXml = null;

		this.contentFinished = function() {
		};
	},

	onReadyStateChange : function() {

		if (this.m_oObj.readyState == 4) {

			this.m_oObj.onreadystatechange = function() {
			};

			try {
				this.onError = (this.m_oObj.status < 200 || this.m_oObj.status >= 400 || (this.asXml && this.m_oObj.responseXML.documentElement.nodeName == "parsererror"));
			} catch (e) {
				this.onError = true;
			}

			try {
				this.responseText = this.m_oObj.responseText;
				this.responseXml = this.asXml ? new XmlDom(this.m_oObj.responseXML) : null;
				this.status = this.m_oObj.status;
			} catch (e) {
			}

			this.contentFinished(this);
		}
	},

	addParam : function(sKey, vVal) {
		if (typeof(vVal) == 'array') {
			for (var i = 0; i < vVal.length; i++) {
				this.m_aParam.push(new Array(sKey + '[]', vVal[i]));
			}
		} else {
			this.m_aParam.push(new Array(sKey, vVal));
		}
	},

	call : function(f) {
		if (typeof(f) != "undefined") {
			this.contentFinished = f;
		}

		// Création de l'objet XmlHttpRequest
		if (typeof(XMLHttpRequest) != "undefined") {
			this.m_oObj = new XMLHttpRequest();
			if (this.m_oObj.overrideMimeType) {
				this.m_oObj.overrideMimeType('text/xml');
			}
		} else {
			this.m_oObj = Try.these(function() {
						return new ActiveXObject('Msxml2.XMLHTTP.3.0')
					}, function() {
						return new ActiveXObject('Msxml2.XMLHTTP')
					}, function() {
						return new ActiveXObject('Microsoft.XMLHTTP')
					}) || false;
		}

		if (!this.m_oObj) {
			throw ("Unable to create XMLHttpRequest object");
		}

		// Récupération des param dans l'URL
		var aURL = this.url.split('&');
		this.url = aURL[0];
		for (var i = 1; i < aURL.length; i++) {
			var a = aURL[i].split('=');
			this.addParam(a[0], a[1]);
		}

		// Traitement des paramètres à joindre à la requete
		var sParam = $A(this.m_aParam).collect(function(param) {
					return param[0] + "=" + encodeURIComponent(param[1]);
				}).join('&');

		if (this.method != 'POST' && sParam.length > 0) {
			this.url += '?' + sParam;
			sParam = '';
		}

		this.m_oObj.open(this.method, this.url, true);
		this.m_oObj.onreadystatechange = this.onReadyStateChange.bind(this);
		this.m_oObj.setRequestHeader("Encoding", this.encoding);
		this.m_oObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset="
						+ this.encoding);
		/*
		 * Force "Connection: close" for older Mozilla browsers to work around a bug where XMLHttpRequest sends an
		 * incorrect Content-length header. See Mozilla Bugzilla #246651.
		 */
		if (this.m_oObj.overrideMimeType && (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0, 2005])[1] < 2005) {
			this.m_oObj.setRequestHeader("Connection", "close");
		}
		this.m_oObj.send(sParam);
	}

};
