// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// 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,
        onSuccess: function(response){
            document.getElementById(target).innerHTML = response.responseText;
            swapButton(itemId,action);    
        }
    //        onComplete: function() {
    //            document.getElementById(target).innerHTML = 'test';
    //            alert (xhr.responseText);
    //            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/graphics/etoilepleine.gif");
            theItem.firstChild.setAttribute('title', "Retirer de la sélection");
            theItem.className = "deleteFromCart";
        } else if (action == "del") {
            theItem.firstChild.setAttribute('src', "images/graphics/etoilevide.gif");
            theItem.firstChild.setAttribute('title', "Ajouter à la sélection");
            theItem.className = "addToCart";
        }
        init();
    }
}

window.onload = init;
