<!--

function item (key)
{
	key = key.split ('_');

	this.tipo = key [0];
	this.id   = parseInt (key [1], 10);
	this.qta  = parseInt (key [2], 10);

	this.equals = function (id) {  return  (this.tipo + '_' + this.id) == id;  }
	this.compact = function () {  return  this.tipo + '_' + this.id + '_' + this.qta;  }
}

function carrello (cookieName)
{
	this.items = new Array ();
	this.cookieName = cookieName;

	this.popola = function (biscotto)
	{
		if (!biscotto) return;

		biscotto = biscotto.split ('|');
		for (i = 0; i < biscotto.length; ++ i) this.items [this.items.length] = new item (biscotto [i]);
	}

	this.popola (getCookie (this.cookieName));

	this.getItem = function (id)
	{
		for (i = 0; i < this.items.length; ++ i) if (this.items [i].equals (id)) return i;
		return false;
	}

	this.get = function (id)
	{
		return ((indice = this.getItem (id)) === false) ? 0 : this.items [indice].qta;
	}

	this.set = function (id, qta)
	{
		if ((indice = this.getItem (id)) === false) this.items [indice = this.items.length] = new item (id + '_0');

		var old_qta = this.items [indice].qta;
		this.items [indice].qta = qta;

		return old_qta;
	}

	this.salva = function ()
	{
		var temp = new Array ();
		for (i = 0; i < this.items.length; ++ i) if (this.items [i].qta > 0) temp [temp.length] = this.items [i].compact ();
		setCookie (this.cookieName, temp.join ('|'), 10, '/');
	}

	this.empty = function ()
	{
		setCookie (this.cookieName, '', -10, '/');
	}
}

function card_a_canestro (cesta)
{
	this.cesta = cesta;

	this.obj_totale = document.getElementById ('totale');
	if (this.obj_totale) this.val_totale = parseInt (this.obj_totale.innerHTML);

	this.resetta = function (msg)
	{
		if (document.form1  &&  (acq_obj = document.form1 ['acq_' + this.id])) acq_obj.value = this.old_acq;
		else if (acq_obj = document.getElementById ('acq_' + this.id)) acq_obj.innerHTML = this.old_acq;

		window.alert (msg);
		return false;
	}

	this.aggiorna = function (handle, msg)
	{
		this.cesta.set (this.id, this.new_acq);
		this.cesta.salva ();

		if (!handle)
		{
			if (acq_obj = document.getElementById ('acq_' + this.id)) acq_obj.innerHTML = this.new_acq;
			else if (acq_obj = document.form1 ['acq_' + this.id]) acq_obj.value = this.new_acq;

			var differenza = this.new_acq - this.old_acq;

			this.val_qta -= differenza;
			this.obj_qta.innerHTML = this.val_qta;

			if (this.obj_totale)
			{
				this.val_totale += differenza;
				this.obj_totale.innerHTML = this.val_totale;
			}
			window.alert (msg);
		}
		return handle;
	}

	this.compra = function (id, quante)
	{
		if (quante == 0) if (!window.confirm ('Con questa operazione toglierai l\'articolo dal carrello.' + "\n" + 'Vuoi progeguire ?')) return false;

		this.id = id;
		this.old_acq = this.cesta.get (this.id);

		this.obj_qta = document.getElementById ('qta_' + this.id);
		this.val_qta = parseInt (this.obj_qta.innerHTML, 10);

		var massimo = this.val_qta + this.old_acq;

		if (quante != null)
		{
			if (isNaN (quante)  ||  ((quante = parseInt (quante, 10)) < 0))
				return this.resetta ('Attento: devi inserire un numero di articoli valido' + "\n" + '(sono validi i numeri interi, maggiori o uguali a zero)');

			if (quante > massimo)
				return this.resetta ('Attento: sono disponibili al massimo ' + massimo + ' copi' + (massimo == 1 ? 'a' : 'e') + ' di questo articolo.');

			this.new_acq = quante;
			return this.aggiorna (true);
		}
		else
		{
			if (this.val_qta == 0)
				return this.resetta ('Attento: non ci sono altre copie disponibili di questo articolo.');

			this.new_acq = this.old_acq + 1;
			return this.aggiorna (false, 'L\'articolo è stato aggiunto al carrello.');
		}
	}

	this.svuota = function ()
	{
		if (!window.confirm ('Vuoi davvero svuotare il tuo carrello ?')) return false;

		loading ();
		this.cesta.empty ();
		setTimeout ('document.location.reload ();', 10);

		return false;
	}
}

// -->
