if(typeof Util == "undefined"){ Util = {}; }

Util.HttpReader = function(options)
{	
	this.options = new Util.Options(options);
	
};

Util.HttpReader.prototype = {
	
	getXHR: function() {

	    if (window.XMLHttpRequest) { // Tous navigateurs, sauf IE ...
	        xhr = new XMLHttpRequest();
	    }
	    else if (window.ActiveXObject) { // Internet Explorer
	        try {
	            xhr = new ActiveXObject("Msxml2.XMLHTTP");
	        } catch(e) {
	            try {
	                xhr = new ActiveXObject("Microsoft.XMLHTTP");
	            } catch(e1) {
	                xhr = null;
	            }
	        }
	    }
	    else
	    { // XMLHttpRequest non supporté par le navigateur
	        alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest. Veuillez mettre à jour votre navigateur.");
	    }
		
		return xhr;
	},

	load: function()
	{
		var xhr = this.getXHR();
		
		//xhr.scope = this;
		var httpR = this;
		
		xhr.onreadystatechange = function() {
		    if(xhr.readyState == 4 && xhr.status == 200) {
		    	
		    	var onload = {};
				if(httpR.options.get("scope"))
				{
					var onload = httpR.options.get("scope");
				}
				if(httpR.options.get("handler"))
				{
					onload.handler = httpR.options.get("handler");
					onload.handler(xhr.responseText);	
				}

			}
		};
		
		var params = "d="+ (new Date()).getTime();
		for(var v in this.options.get("params"))
		{
			if(this.options.get("params")[v] instanceof Array)
			{
				var tab = this.options.get("params")[v];
				for(var i in tab)
				{
					params += "&"+v+"[]="+tab[i];
				}
			}
			else
			{
				params += "&"+v+"="+this.options.get("params")[v];
			}
		}
		var sync = this.options.get("sync");
		if(sync === "")
		{
			sync = true;
		}
		if(this.options.get("method") == "post")
		{
		    xhr.open("POST", this.options.get("url")+"?d="+ (new Date()).getTime(), sync);
		    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		    xhr.send(params);
		}

		if(this.options.get("method") == "get")
		{
			xhr.open("GET", this.options.get("url")+"?"+params, sync);
			xhr.send(null);
		}
		
		if(sync == false)
		{
			var onload = {};
			if(this.options.get("scope"))
			{
				var onload = this.options.get("scope");
			}
			if(this.options.get("handler"))
			{
				onload.handler = this.options.get("handler");
				onload.handler(xhr.responseText);	
			}

		}
		
	}

}
