// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Title : Super simple javascript/ajax cart
// Author : Jerome Senaillat, AKA Remka
// URL : www.remka.net
//
// Description : A very simple javascript/ajax cart.
// You can modify, release, do whatever you want with this, and don't even need to link to my site.
// I wouldn't recommend you to use this in a production environement, since its not secure at all, but hey,
// you do as you want!
//
// Created : 2007/05/25/
// Modified : -
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function init() {
	var linksArray = document.getElementsByTagName('a');
	for (i=0; i<linksArray.length; i++) {
		// add to cart
		if (linksArray[i].className == "addToCart") {
			linksArray[i].onclick = function () {
				var itemId = this.getAttribute('id').substr(4);
				addToCart(itemId,'add');
				this.blur();
				return false;
			}
		}
		// delete from cart
		if (linksArray[i].className == "deleteFromCart") {
			linksArray[i].onclick = function () {
				var itemId = this.getAttribute('id').substr(4);
				addToCart(itemId,'del');
				this.blur();
				return false;
			}
		}
		// simple delete from cart
		if (linksArray[i].className == "simpleRemove") {
			linksArray[i].onclick = function () {
				var itemId = this.getAttribute('id').substr(6);
				addToCart(itemId,'del');
				this.blur();
				return false;
			}
		}
	}
}

function addToCart(itemId,action){
	var url = 'cart.php';
	var pars = 'action='+action+'&id='+itemId;
	var target = 'cartOutput';
	var myAjax = new Ajax.Updater(target, url, {
		method: 'get',
		parameters: pars,
		onComplete: function() {
			swapButton(itemId,action);
		}});
}

function swapButton(btnId,action) {
	if (document.getElementById('item'+btnId)) {
		var theItem = document.getElementById('item'+btnId);
		if (action == "add") {
			theItem.firstChild.setAttribute('src', "images/caddie/etoilepleine.gif");
			theItem.firstChild.setAttribute('title', "Retirer de la sélection");
			theItem.className = "deleteFromCart";
		} else if (action == "del") {
			theItem.firstChild.setAttribute('src', "images/caddie/etoilevide.gif");
			theItem.firstChild.setAttribute('title', "Ajouter à la selection");
			theItem.className = "addToCart";
		}
		init();
	}
}

window.onload = init;