/*// 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]);
		}
	});*/
	
	// 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)
{
	new Ajax.Request("server.php?action=addToCart", {
		parameters: "id=" + id,
		onSuccess: function(resp) {
			var cartUpdate = eval('(' + resp.responseText + ')');
			var isNew = cartUpdate['cartItemDetails'][0].isNew;
			var newQty = cartUpdate['cartItemDetails'][0].newQty;
			var stock = cartUpdate['cartItemDetails'][0].stock;
			var newStock=(stock-newQty);
			// if this item is new in the cart, inject it inside, otherwise update the qty and subtotal
			if (isNew == 1)
			{
				var newItem = '<div id="cartItem_' + id + '" style="display:none" class="row">';
				newItem += '<div class="cell5"><img src="images/products/' + cartUpdate['cartItemDetails'][0].img + '" width="40" height="40" border="0" /></div>';
				newItem += '<div class="cell1" id="cartItemQty_' + id + '" valign="top">1</div>';
				newItem += '<div class="cell2">' + cartUpdate['cartItemDetails'][0].title + '</div>';
				newItem += '<div class="cell3" id="cartItemPrice_' + id + '">' + cartUpdate['cartItemDetails'][0].newPrice + ' €</div>';
				newItem += '<div class="cell4"><a href="server.php?action=removeFromCart&id=' + id + '" onclick="return removeFromCart(' + id + ')">';
				newItem += 'x</a></div><div class="clear"></div></div>';
				
			
				
				if ($("cartIsEmpty"))
					Element.hide($("cartIsEmpty"));

				new Insertion.Bottom("cartItems", newItem);
				
				
				Effect.Appear("cartItem_" + id, { duration: 0.5 });
				//Effect.Appear("cartItem1_" + id, { duration: 0.5 });
				Element.update($("cartTotalAmount"), "Totale: " + cartUpdate['cartItemDetails'][0].total + " €");
			
				new Effect.Highlight("cartTotalAmount", {startcolor:'#999999', endcolor:'#777777', restorecolor:'#777777'});
				
				Element.update($("checkout"), '<a href="checkout1.php">Checkout</a>');
			
				Element.update($("frm_container_"+ id), '<button type="submit"  name="submit' + id + '" value="Add" onClick="return addToCart(' + id + ');" class="no_submit" disabled>Selezionato</button>');
				//Element.hide($("add_"+ id));
			}
			// so, the item already existed in the cart, therefore update its quantity and subtotal
			else
			{
				Element.update($("cartItemQty_" + id), cartUpdate['cartItemDetails'][0].newQty);
				Element.update($("cartItemPrice_" + id), cartUpdate['cartItemDetails'][0].newPrice + " €");
				Element.update($("cartTotalAmount"), "Totale: " + cartUpdate['cartItemDetails'][0].total + " €");
				Element.update($("checkout"), '<a href="checkout1.php">Checkout</a>');
				new Effect.Highlight("cartTotalAmount", {startcolor:'#999999', endcolor:'#777777', restorecolor:'#777777'});
				new Effect.Highlight("cartItemPrice_" + id, {startcolor:'#999999', endcolor:'#777777', restorecolor:'#777777'});
				new Effect.Highlight("cartItemQty_" + id, {startcolor:'#999999', endcolor:'#777777', restorecolor:'#777777'});
				
				////////////////
			
				Element.update($("frm_container_"+ id), '<button type="submit"  name="submit' + id + '" value="Add" onClick="return addToCart(' + id + ');" class="no_submit" disabled>Selezionato</button>');
				
				//Element.update($("cartItemQty1_" + id), cartUpdate['cartItemDetails'][0].newQty);
				//new Effect.Highlight("cartItemQty1_" + id, {startcolor:'#999999', endcolor:'#777777', restorecolor:'#777777'});
				
			}
		}
	});
	return false;
}

// remove an item from the cart
function removeFromCart(id)
{
	Effect.Fade("cartItem_" + id);
	new Ajax.Request("server.php?action=removeFromCart", {
		parameters: "id=" + id,
		onSuccess: function(resp) {
			var total = resp.responseText;

	
			

			if (total == 0)
			{
				// update the cart's total amount and contents
				Element.update($("cartItems"), '<div id="cartIsEmpty">Il carrello è vuoto.</div>');
				Element.update($("cartTotalAmount"), "Total: 0.00 €");

				// update the cart's total amount
				Element.update($("cartTotalAmount"), "Totale: " + total + " €");
				
				Element.update($("checkout"), "");
				
				var newQty = "0";//Update['cartItemDetails'][0].newQty;
				var stock = "1";//Update['cartItemDetails'][0].stock;	

			Element.update($("frm_container_"+ id), '<button type="submit"  name="submit' + id + '" value="Add" onClick="return addToCart(' + id + ');" class="submit">Aggiungi</button>');
			}
			else
			{
						
				// update the cart's total amount
				Element.update($("cartTotalAmount"), "Totale: " + total + " €");
				Element.update($("checkout"), '<a href="checkout1.php">Checkout</a>');
				
				var newQty = "1";//Update['cartItemDetails'][0].newQty;
			var stock = "1";//Update['cartItemDetails'][0].stock;

		Element.update($("frm_container_"+ id), '<button type="submit"  name="submit' + id + '" value="Add" onClick="return addToCart(' + id + ');" class="submit">Aggiungi</button>');
			
				
			}
			new Effect.Highlight("cartTotalAmount", {startcolor:'#999999', endcolor:'#777777', restorecolor:'#777777'});
		
			
		}
	});
	return false;
}

// empty the cart
function emptyCart()
{
	new Ajax.Request("server.php?action=emptyCart", {
		onSuccess: function(resp) {
			if (resp.responseText == 1)
			{
				// update the cart's total amount and contents
				Element.update($("cartItems"), '<div id="cartIsEmpty">Il tuo carrello è vuoto.</div>');
				Element.update($("cartTotalAmount"), "Totale: 0.00 €");
				//$$('.submit').invoke('setStyle', {background: '#f00'});
				//$$('.submit').invoke('disabled', {'true':'false'});
				//$$('.submit').invoke('setStyle', {display: 'block'}) 
				//$$('.submit').invoke('enable' : 'disable'); //aggiunto
				//$$('.submit').invoke('enable' : 'disable'); //aggiunto
				$$('.submit').invoke('enable'); //aggiunto
				//Form.Element.enable('.submit');
				//$$('submit').enable();
				//getElements('.submit').invoke('enable'); //aggiunto
				//getElements(form).invoke('disable');
				new Effect.Highlight("cartIsEmpty", {startcolor:'#999999', endcolor:'#777777', restorecolor:'#777777'});
				new Effect.Highlight("cartTotalAmount", {startcolor:'#999999', endcolor:'#777777', restorecolor:'#777777'});
			}
		}
	});
	return false;
}
