// Ajouter un article au panier
function ajouter_panier(id){
	var _url = document.location; // url de la page courante
	var title = $("#panier_titre_offre").html();
	var ifError = $("#add_panier").html();
	$.ajax
	({
		type: "GET",
		url: '/include/gestionPanier.asp',
		data: "addToPanier=1&title="+escape(title)+"&_url="+_url+"&id="+id,
		dataType: "html",
		timeout: 20000,
		beforeSend: function ()
		{
			// Ajout en cours
			$("#add_panier").html("<span class='deja_panier'>Ajout en cours...</span>");
		},
		complete: function () {},
		error: function(xhr, ajaxOptions, thrownError)
		{
			$("#add_panier").html(ifError);
			alert ("Une erreur est survenue");
		},
		success: function(msg)
		{
			if ((msg) == "add_panier_ok")
			{
				actualiserPanier();
				if ($("#montrer_panier").is(':hidden'))
				{
					show_panier();
				}
				var odd_even_panier = $("#odd_even_panier").html();
				// Mise ŕ jour Bouton
				$("#add_panier").html("<span class='deja_panier'>Ajouté au panier</span>");
			}
			else
			{
				$("#add_panier").html(ifError);
				alert ("Une erreur est survenue");
			}
		}
	});
}

function actualiserPanier(){
	var idPanier = $("#montrer_panier");
	$.ajax
	({
		type: "GET",
		url: '/include/gestionPanier.asp',
		data: "showPanier=1",
		dataType: "html",
		timeout: 20000,
		beforeSend: function ()
		{
			idPanier.html("Chargement de votre panier en cours...");
		},
		complete: function () {},
		error: function(xhr, ajaxOptions, thrownError)
		{
			idPanier.html("Une erreur est survenue");
		},
		success: function(msg)
		{
			idPanier.html(msg);
		}
	});
}

//AfficherCacher panier
function show_panier(){
	$("#panier").show();
	$("#acces_panier").show();
	$("#montrer_panier").toggle('fast');
}

// Pair Impair
function Odd(value) { 
    return (value & 1)==1; 
}

// Supprimer un article du panier
function removeItem(id){
	var ifError;
	var odd_even_panier = $("#odd_even_panier").html();
	$.ajax
	({
		type: "GET",
		url: '/include/gestionPanier.asp',
		data: "removeItem="+id,
		dataType: "html",
		timeout: 20000,
		beforeSend: function ()
		{
			ifError = $("#item_"+id).html();
			$("#item_"+id).html("Suppression en cours...");
		},
		complete: function () {},
		error: function(xhr, ajaxOptions, thrownError)
		{
			$("#item_"+id).html(ifError);
		},
		success: function(msg)
		{
			if (msg == "removeItemOk"){ // Suppression OK
				actualiserPanier();
				if (odd_even_panier < 1)
				{ // Plus aucun articles dans le panier : On cache le panier
					$("#panier").hide();
				}
				var offID = $("#offID").html();
				$("#add_panier").html('<a href="" onclick="ajouter_panier('+offID+');return false;" class="lien_act_offre"><span class="act_offre">Ajouter au panier</span></a>');
			}
			else // Erreur lors de la suppression
			{
				$("#item_"+id).html(ifError);
			}
		}
	});
}
