window.onload = function() {
	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, calea )
{
	if(navigator.appName== "Microsoft Internet Explorer")
	{
	}else{
	new Ajax.Request(calea + "server.php?action=addToCart", {
		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 doc_cantitate and subtotal
			if (isNew == 1)
			{
				var newItem = '<div id="cartItem_' + id + '" style="display:none" class="row">';
				newItem += '<div class="cell1" id="cartItemQty_' + id + '" valign="top">1</div>';
				newItem += '<div class="cell2">' + cartUpdate['cartItemDetails'][0].doc_titlu + '</div>';
				newItem += '<div class="cell3" id="cartItemPrice_' + id + '">' + cartUpdate['cartItemDetails'][0].newPrice + ' RON</div>';
				newItem += '<div class="cell4"><a href="'+calea+'server.php?action=removeFromCartJustOne&id=' + id + '" onclick="return removeFromCartJustOne(' + id + ', ' + calea + ')">';
				newItem += '-</a></div>';
				newItem += '<div class="cell5"><a href="'+calea+'server.php?action=removeFromCart&id=' + id + '" onclick="return removeFromCart(' + id + ', ' + calea + ')">';
				newItem += 'x</a></div><div class="clear"></div></div>';
				
				if ($("cartIsEmpty"))
					Element.hide($("cartIsEmpty"));

				new Insertion.Bottom("cartdocumente", newItem);	
				
				Effect.Appear("cartItem_" + id, { duration: 0.5 });
				Element.update($("cartTotalAmount"), "Total: " + cartUpdate['cartItemDetails'][0].total + " RON");
				new Effect.Highlight("cartTotalAmount", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
			}
			// 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 + " RON");
				Element.update($("cartTotalAmount"), "Total: " + cartUpdate['cartItemDetails'][0].total + " RON");
				new Effect.Highlight("cartTotalAmount", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
				new Effect.Highlight("cartItemPrice_" + id, {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
				new Effect.Highlight("cartItemQty_" + id, {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
			}
		}
	});
	return false;
}
}

// remove items from the cart one by one
function removeFromCartJustOne(id, calea )
{
	if(navigator.appName== "Microsoft Internet Explorer")
	{
	}else{
	new Ajax.Request(calea + "server.php?action=removeFromCartJustOne", {
		parameters: "id=" + id,
		onSuccess: function(resp) {
			var cartUpdate = eval('(' + resp.responseText + ')');
			//  otherwise update the doc_cantitate and subtotal
			if(cartUpdate['cartItemDetails'][0].newQty == 0)
			{
				Effect.Fade("cartItem_" + id);
				if (cartUpdate['cartItemDetails'][0].total == 0)
				{
					// update the cart's total amount and contents
					Element.update($("cartdocumente"), '<div id="cartIsEmpty">Cosul de cumparaturi este gol.</div>');
					Element.update($("cartTotalAmount"), "Total: 0.00 RON");
					new Effect.Highlight("cartIsEmpty", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
				}
				else
				{
					// update the cart's total amount
					Element.update($("cartTotalAmount"), "Total: " + cartUpdate['cartItemDetails'][0].total + " RON");
				}
				//new Effect.Highlight("cartTotalAmount", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});				
			}else{
				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");
				new Effect.Highlight("cartTotalAmount", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
				new Effect.Highlight("cartItemPrice_" + id, {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
				new Effect.Highlight("cartItemQty_" + id, {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
			}
		}
	});
	return false;
	}
}

// remove an item from the cart
function removeFromCart(id, calea )
{
	if(navigator.appName== "Microsoft Internet Explorer")
	{
	}else{
	Effect.Fade("cartItem_" + id);
	new Ajax.Request(calea + "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($("cartdocumente"), '<div id="cartIsEmpty">Cosul de cumparaturi este gol.</div>');
				Element.update($("cartTotalAmount"), "Total: 0.00 RON");
				new Effect.Highlight("cartIsEmpty", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
			}
			else
			{
				// update the cart's total amount
				Element.update($("cartTotalAmount"), "Total: " + total + " RON");
			}
			new Effect.Highlight("cartTotalAmount", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
		}
	});
	return false;
	}
}

// empty the cart
function emptyCart( calea )
{
	if(navigator.appName== "Microsoft Internet Explorer")
	{
	}else{
	new Ajax.Request(calea + "server.php?action=emptyCart", {
		onSuccess: function(resp) {
			if (resp.responseText == 1)
			{
				// update the cart's total amount and contents
				Element.update($("cartdocumente"), '<div id="cartIsEmpty">Cosul de cumparaturi este gol.</div>');
				Element.update($("cartTotalAmount"), "Total: 0.00 RON");
				new Effect.Highlight("cartIsEmpty", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
				new Effect.Highlight("cartTotalAmount", {startcolor:'#CF0808', endcolor:'#ffffff', restorecolor:'#ffffff'});
			}
		}
	});
	return false;
	}
}
