var noticeMixte;
var noticeWithDelivery;

//AJAX pour raffraichissement du panier
function countCart(bt, inc)
{	 
	 var input = $('input', $(bt).parent());
	var qte = parseInt(input.val()) + inc;
	var id = input.attr('rel');
	
	 $('#zoneCart').load(
			 "/catalog/countCart",
			 {qte: qte, id: id}
	  );

}



function showNoticeMixedCart()
{
	noticeMixte.load();
}

function hideNoticeMixedCart()
{
	noticeMixte.close();
}

function showNoticeCartWithDelivery()
{
	noticeWithDelivery.load();
}

function hideNoticeCartWithDelivery()
{
	noticeWithDelivery.close();
}

function showHideElt(idElt, show)
{
	var elt = document.getElementById(idElt);
	if (elt)
	{
		elt.style.display = show ? '' : 'none';
	}
}

/**
 * Valide le panier.
 * @param formId : identifiant du formulaire.
 *
 */
function validateCart(formId)
{
	var cartForm = document.getElementById(formId);
	if (!cartForm) {
		return;
	}
	cartForm.submit();
}

/**
 * Affiche une notice à l'utilisateur, dans le cas d'un panier mixe.
 * 
 * @param formId : id du formulaire du panier
 * @param prefix : préfixe de nomage des checkbox de livraison
 * @param noticeId
 */
function checkMixedCart(formId, prefix, noticeId)
{
	var cartForm = document.getElementById(formId);
	if (!cartForm) {
		return;
	}
	
	
	noticeMixte = $("div#noticeCartMixte").overlay({ 
		expose: { 
		    /* expose configuration goes here */ 
		    maskId: 'mask',
		    // one configuration property 
		    color: '#666', 
		 
		    // another property 
		    opacity: 0.5
		 
		    // ... the rest of the configuration properties 
		},
		closeOnClick: false ,
	    api: true
	});
	
	
	var mem = "__vide__";
	for (var i = 0; i < cartForm.elements.length; i++)
	{
		var elt = cartForm.elements[i];
		if 
		(
			elt.type == 'checkbox' && elt.name.substring(0, prefix.length) == prefix
		)
		{
			if (mem == "__vide__") //la première fois, on initialise la valeur
			{
				mem = elt.checked;
				continue;
			}
			//vérification de chaque itération
			if (mem != elt.checked)
			{
				showNoticeMixedCart();
				return;
			}
		}
	}
	validateCart(formId);
}


/**
 * Affiche une notice à l'utilisateur, dans le cas d'un panier qui contient au moins un article à livrer.
 *
 * @param formId : id du formulaire du panier
 * @param prefix : préfixe de nomage des checkbox de livraison
 * @param noticeId
 */
function checkIfCartWithDelivery(formId, prefix, noticeId)
{

	var cartForm = document.getElementById(formId);
	if (!cartForm) {
		return;
	}


	noticeWithDelivery = $("div#noticeCartWithDelivery").overlay({
		expose: {
		    /* expose configuration goes here */
		    maskId: 'mask',
		    // one configuration property
		    color: '#666',

		    // another property
		    opacity: 0.5

		    // ... the rest of the configuration properties
		},
		closeOnClick: false ,
	    api: true
	});


	for (var i = 0; i < cartForm.elements.length; i++)
	{

		var elt = cartForm.elements[i];
		if ( elt.type == 'checkbox' && elt.name.substring(0, prefix.length) == prefix )
		{
			if (elt.checked)
			{
				showNoticeCartWithDelivery();
				return;
			}
                }
        }
	validateCart(formId);
}

