var Jx = {};

Jx.HttpRequest = function(options)
{
	this.options = options;
}

Jx.HttpRequest.prototype = {
	
	getXhr: function() {
	    var r = false;
		if (window.XMLHttpRequest) {
	        r = new XMLHttpRequest();
	    }
	    else if (window.ActiveXObject) {
	        try {
				r = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) {
				r = new ActiveXObject("Microsoft.XMLHTTP");
			}
	    } else {
			alert("Erreur XML");
		}
		return r;
	},
	
	load: function(options)
	{
		var xhr = this.getXhr();
		xhr.scope = this.options.scope;
		xhr.scope.handler = this.options.handler;
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4 && xhr.status == 200) {
				var on = this.scope;
				on.handler(xhr.responseText);
			}
		}
		
		var tmp = new Array();
		if(options.params)
		{
			for(var v in options.params)
			{
				tmp.push(v+"="+options.params[v]);
			}
		}
		
		sync = true;
		
		if(this.options.sync)
		{
			sync = this.options.sync;
		}
		
		xhr.open("GET", this.options.url+"?"+tmp.join("&")+"&"+ (new Date()).getTime(), sync);
	  	xhr.send(null);	
	}
		
}

Jx.Prices = function(options)
{
	this.options = options;
	this.init();
}

Jx.Prices.prototype = {
		
	init: function()
	{
		this.element = document.getElementById(this.options.id);
		this.panier = document.getElementById(this.options.panierId);
	},
		
	load: function()
	{
		var http = new Jx.HttpRequest({
			url: "/ajax/prix.php",
			handler: function(txt)
			{
				this.element.innerHTML = txt;
			},
			scope: this
		});
		http.load({
			params: {hetId: this.options.hetId}
		});
	},
	
	add: function(tarifId)
	{
		var http = new Jx.HttpRequest({
			url: "/ajax/panier.php",
			handler: function(txt)
			{
				this.get();
			},
			scope: this
		});
		http.load({
			params: {add: tarifId}
		});
	},
	
	del: function(tarifId)
	{
		var http = new Jx.HttpRequest({
			url: "/ajax/panier.php",
			handler: function(txt)
			{				
				this.get();
			},
			scope: this
		});
		http.load({
			params: {del: tarifId}
		});
	},
	
	get: function()
	{
		this.panier.innerHTML = "Veuillez patienter<br />Nous chargeons le panier...";
		var http = new Jx.HttpRequest({
			url: "/ajax/panier.php",
			handler: function(txt)
			{
				this.panier.innerHTML = txt;
				this.load();
			},
			scope: this
		});
		
		http.load({
			params: {get: 1}
		});
	}
		
}

Jx.track = function()
{
	var http = new Jx.HttpRequest({
		url: "/ajax/trackin.php",
		handler: function(txt)
		{
			
		},
		scope: this,
		sync: false
	});
	http.load({
		params: {}
	})
}

Jx.untrack = function()
{
	var http = new Jx.HttpRequest({
		url: "/ajax/trackin.php",
		handler: function(txt)
		{
			
		},
		scope: this
	});
	http.load({
		params: {untrack: true}
	})
}
