// when the page loads, transform all product images into draggable items // and allow them to be dropped on the cart window.onload = function() { // retrieve all product images var draggables = $$("div.productImg"); // make them all draggable draggables.each(function(currentDraggable) { new Draggable(currentDraggable, { revert: true, ghosting: true }); }); // define the cart as a droppable Droppables.add("cart", { hoverclass: "cartOnHover", onDrop: function(element) { // each product image has an id like 'product_x', where 'x' is the database id, // so we extract it next var itemId = element.id.split("_"); addToCart(itemId[1], "msg_"+itemId[1]); } }); // bind a progress indicator to all Ajax calls startAjax(); } function startAjax() { Ajax.Responders.register({ // when an Ajax request is started, show the indicator onCreate: function() { if (Ajax.activeRequestCount > 0) Element.show($("indicator")); }, // when an Ajax request is finished, hide the indicator onComplete: function() { if (Ajax.activeRequestCount == 0) Element.hide($("indicator")); } }); } // add item to cart function addToCart(id, msg_id) { new Ajax.Request("/server.php?action=addToCart&"+Math.random(9999), { parameters: "id=" + id, onSuccess: function(resp) { var cartUpdate = eval('(' + resp.responseText + ')'); var isNew = cartUpdate['cartItemDetails'][0].isNew; // if this item is new in the cart, inject it inside, otherwise update the qty and subtotal if (isNew == 1) { $(msg_id).show(); Effect.Fade(msg_id); var newItem = ''; if ($("cartIsEmpty")) Element.hide($("cartIsEmpty")); new Insertion.Bottom("cartItems", newItem); Element.update($('cartItemQty_' + id), cartUpdate['cartItemDetails'][0].newQty); Element.update($('cartItemPrice_' + id), cartUpdate['cartItemDetails'][0].newPrice + ' RON'); if(cartUpdate['cartItemDetails'][0].discount!="") { Element.update($("cartTotalDiscount"), "Reducere: -" + cartUpdate['cartItemDetails'][0].discount + " RON"); new Effect.Highlight("cartTotalDiscount", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); } Effect.Appear("cartItem_" + id, { duration: 0.5 }); Element.update($("cartTotalAmount"), "Total: " + cartUpdate['cartItemDetails'][0].total + " RON"); new Effect.Highlight("cartTotalAmount", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); } // so, the item already existed in the cart, therefore update its quantity and subtotal else { $(msg_id).show(); Effect.Fade(msg_id); Element.update($("cartItemQty_" + id), cartUpdate['cartItemDetails'][0].newQty); Element.update($("cartItemPrice_" + id), cartUpdate['cartItemDetails'][0].newPrice + " RON"); Element.update($("cartTotalAmount"), "Total: " + cartUpdate['cartItemDetails'][0].total + " RON"); if(cartUpdate['cartItemDetails'][0].discount!="") { Element.update($("cartTotalDiscount"), "Reducere: -" + cartUpdate['cartItemDetails'][0].discount + " RON"); new Effect.Highlight("cartTotalDiscount", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); } new Effect.Highlight("cartTotalAmount", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); new Effect.Highlight("cartItemPrice_" + id, {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); new Effect.Highlight("cartItemQty_" + id, {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); } } }); return false; } // remove an item from the cart function removeFromCart(id) { Effect.Fade("cartItem_" + id); new Ajax.Request("/server.php?action=removeFromCart&"+Math.random(9999), { parameters: "id=" + id, onSuccess: function(resp) { var cartRemove = eval('(' + resp.responseText + ')'); var total = cartRemove['cartDetails'][0].total; var discount = cartRemove['cartDetails'][0].discount; if (total == 0) { // update the cart's total amount and contents Element.update($("cartItems"), '
Cosul este gol.
'); Element.update($("cartTotalAmount"), "Total: 0.00 RON"); new Effect.Highlight("cartIsEmpty", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); } else { if(discount=="") Element.update($("cartTotalDiscount"), ""); else { Element.update($("cartTotalDiscount"), "Reducere: -" + discount + " RON"); new Effect.Highlight("cartTotalDiscount", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); } // update the cart's total amount Element.update($("cartTotalAmount"), "Total: " + total + " RON"); } new Effect.Highlight("cartTotalAmount", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); } }); return false; } // empty the cart function emptyCart() { new Ajax.Request("/server.php?action=emptyCart&"+Math.random(9999), { onSuccess: function(resp) { if (resp.responseText == 1) { // update the cart's total amount and contents Element.update($("cartItems"), '
Cosul este gol.
'); Element.update($("cartTotalDiscount"), ""); Element.update($("cartTotalAmount"), "Total: 0.00 RON"); new Effect.Highlight("cartIsEmpty", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); new Effect.Highlight("cartTotalAmount", {startcolor:'#CCCCCC', endcolor:'#F7F7F7', restorecolor:'#F7F7F7'}); } } }); return false; }