Cart = {};
Cart.htmlContainerElement = null;

Cart.addItem = function(csId, popup, pid, csPidSrc)
{
	var targetId = 0;
	
	if(!Object.isUndefined(pid))
		targetId = pid;
	else
	{
		if(product_mgr.lastSize != 0)
			targetId = product_mgr.product_id;
		else
			alert("Vous devez d'abord choisir une taille");
	}
	
	var parameters = {};
	parameters.product_id = targetId;
	parameters.cs_id = csId;
	parameters.ms = new Date();
	
	if(csPidSrc !== null)
		parameters.csPidSrc = csPidSrc;
		
	if (targetId != 0)
	{
		//We check the availability of the products
		new Ajax.Request(
				"frontoffice/ajax/cart/addItem.php",
				{
					method : "post",
					asynchronous : false,
					parameters : parameters,
					onSuccess : function(xhr){
						if(xhr.responseJSON.success == true){
							new Ajax.Request("frontoffice/ajax/cart/checkProducts.php");
						}
					}
				}
			);
		if(popup)
			showCartPopup.show();
	}
	Cart.getQty();
}

Cart.addCrossSelling = function(csId)
{
	Cart.addItem(csId,0,product_mgr.product_id,cross_mgr.product_id);
	Cart.addItem(csId,1,cross_mgr.product_id,product_mgr.product_id);
}

Cart.removeItem = function(item)
{
	if (confirm("Retirer l'article du panier ?"))
	{
		new Ajax.Request(
			"frontoffice/ajax/cart/removeItem.php",
			{
				method : "post",
				parameters : {
					ms : new Date(),
					index : item
				},
				onSuccess : function(xhr) {Cart.renderItems(); Cart.getQty();}.bind(this)
			}
		);
	}
}

Cart.renderItems = function()
{
	command_mgr.update(1);
//	front_mgr.update("frontoffice/ajax/command/renderWorkflow.php",{"state" : 1});
}

Cart.updateQty = function(idx)
{
	if ($("qty_"+idx) === null)
		return;
		
	var newQty = $("qty_"+idx).value;
	
	if (Object.isNumber(window.parseInt(newQty)))
	{
		var qtyAsInteger = window.parseInt(newQty);

		if (qtyAsInteger < 1)
		{
			alert("La quantité du produit doit être positif");
			return;
		}
		new Ajax.Request("frontoffice/ajax/cart/updateQty.php",
						{
							method : "post",
							parameters: {
								ms : new Date(),
								idx : idx,
								qty : qtyAsInteger
							},
							onSuccess: function(xhr) {
								if (window.showCartPopup.isDisplayed)
									window.showCartPopup.show();
								else
									Cart.renderItems();
								Cart.getQty();
							}.bind(this)
						});
	}
}

Cart.getQty = function()
{
	new Ajax.Updater(
			$("cartQt"),
			"frontoffice/ajax/cart/getQty.php"
			);
}

Cart.addCode = function(triggerElement)
{
	if (triggerElement.previous() === null)
		return;
	var pattern = triggerElement.previous().value;
	new Ajax.Request(
			"frontoffice/ajax/cart/addCode.php",
			{
				method : "post",
				parameters : {
					ms : new Date(),
					code_pattern : pattern
				},
				onSuccess: function(xhr) {
								if (window.showCartPopup.isDisplayed)
									window.showCartPopup.show();
								else
									Cart.renderItems();}
			}
		);
}

Cart.removeCode = function(code)
{
	if (confirm("supprimer ce code ?"))
	{
		new Ajax.Request(
				"frontoffice/ajax/cart/removeCode.php",
				{
					method : "post",
					parameters : {
						ms : new Date(),
						code_id : code
					},
					onSuccess: function(xhr) {
									if (window.showCartPopup.isDisplayed)
										window.showCartPopup.show();
									else
										Cart.renderItems();}
				}
			);
	}
}
