/*
	Classe d'abstraction de l'objet XMLHttpRequest pour Internet Explorer et Mozilla Firefox
	(c) 01.2005, Robloche & poof65
*/

function CreateXMLHTTPRequestObject() {
	
	this.xhr_object    = null;
	this.response      = null;
	this.ready         = true;
	this.asynchronous  = true;

	
	
	if(window.XMLHttpRequest) { 
		this.xhr_object = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			this.xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
			
		} catch (Msxml2E) {
			try {
				this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (MicrosoftE) {
				this.xhr_object = null;
			}
        }
	}
	
	if(!this.xhr_object){
		alert("KO");
	}
	
	this.indicatorFunction = null;
	

	this.setIndicatorFunction = function(func) {
		if(typeof(func) == "function") this.indicatorFunction = func;
	}
	
	this.setSynchronous = function() {
		this.asynchronous = false;
	}


	this.setAsynchronous = function() {
		this.asynchronous = true;
	}


	this.getFileGet = function(url, data) {
		return this.doRequest(url, "GET", data);
	}


	this.getFile = this.getFileGet;
	
	this.getFilePost = function(url, data) {
		return this.doRequest(url, "POST", data);
	}

	this.getFileHeader = function(url, header) {
		return this.doRequest(url, "HEAD", header);
	}

	this.doRequest = function(url, method, data) {
		if(!this.ready || !this.xhr_object) return false;

		function _getResponseHeader(headers, header_name) {
			var tmp = headers.split("\n");
			for(var i=0, n=tmp.length, t=[]; i<n-1; ++i) {
				t = tmp[i].split(": ");
				if(t[0].toLowerCase() == header_name.toLowerCase()) return t[1];
			}
			return "Header inconnu...";
		}

		if(this.indicatorFunction) this.indicatorFunction(true);
		this.ready = false;

		var obj = this;
		function onreadystatechangeFunction() {
			if(obj.xhr_object.readyState != 4) return;
			
			if(obj.indicatorFunction) obj.indicatorFunction(false);

			var all_headers = obj.xhr_object.getAllResponseHeaders();
			if(method == "HEAD") {
				obj.response = data ? _getResponseHeader(all_headers, data) : all_headers;
			}
			else {
				var content_type = _getResponseHeader(all_headers, "Content-Type");
				if (content_type != "Header inconnu..." && (new RegExp("^text/xml.*$", "gi")).test(content_type))
					obj.response = obj.xhr_object.responseXML;
				else
					obj.response = obj.xhr_object.responseText;
			}
		}

		if(method == "GET" && typeof(data) != "undefined" && data != "") url += "?"+data;
		this.xhr_object.open(method, url, this.asynchronous);

		if(this.asynchronous)
			this.xhr_object.onreadystatechange = onreadystatechangeFunction;
		
		if(data) this.xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		else     data = null;
		this.xhr_object.send(data);

		if(!this.asynchronous)
			onreadystatechangeFunction();

		return true;
	}

	this.hasResponse = function() {
		return this.response != null;
	}

	this.getResponse = function() {
		return this.response;
	}

	this.validateRequest = function() {
		this.ready    = true;
		this.response = null;
	}

	this.cancelRequest = function() {
		this.xhr_object.abort();
		if(this.indicatorFunction) this.indicatorFunction(false);
		this.validateRequest();
	}
}

