var popUpJDialog = true;

function AjouterProduitPanier(reference, libelle, quantite, e ,popup, colisage) 
{	
	if(libelle != "pdt_lies")	
	{
		($('#id_sous_produit').val() != null) ? reference = $('#id_sous_produit').val() : reference = reference;
	}
	
	($('#quantiteProduit').val() != null) ? quantite = $('#quantiteProduit').val() : quantite = quantite;

	if(popup == true)
	{
		panierPopUp("ajouter", "panierPopUp", x, y, "show", reference, quantite);	
	}
	else
	{
		PanierActions('ajouter', reference, quantite, e);
	}
}

function SupprimerProduitPanier(reference)
{
	$("#content_prd_"+ reference).addClass("modification");
	// action : desactiver bton valider et afficher waiting
	var type = $("#btValiderPanier").get(0).tagName.toLowerCase()
	if(type == "a")
	{
		$("#btValiderPanier").attr('href', "javascript:void(0);");
	}
	else if(type == "img")
	{
		$("#btValiderPanier").removeAttr('onclick');
	}
	$("#content_prd_"+ reference +' .col6').html($("#waiting").html());
	
	PanierActions('supprimer', reference, 0, null);
}

function ModifierProduitPanier(reference, quantite, oldQuantite, colisage)
{
	var newQuantite = quantite;
	var update = true;
	//alert("test " + oldQuantite + " - " + colisage);
	
	// si il y a une ancienne quantite on verifie si 
	if(oldQuantite != null && quantite == oldQuantite)
	{
		update = false;
	}
	if(colisage != null && colisage > 1 && update)
	{
		if(quantite%colisage == 0)
		{
			newQuantite = quantite;
		}
		else
		{
			newQuantite = colisage;
			if(quantite > colisage){
				newQuantite = parseInt(colisage * (parseInt(quantite/colisage) + 1));
			}
			
		}
	}
	
	// action : desactiver bton valider et afficher waiting
	var type = $("#btValiderPanier").get(0).tagName.toLowerCase()
	if(type == "a")
	{
		$("#btValiderPanier").attr('href', "javascript:void(0);");
	}
	else if(type == "img")
	{
		$("#btValiderPanier").removeAttr('onclick');
	}
	$("#content_prd_"+ reference).addClass("modification");
	
	//$(".waiting").show();
	$("#content_prd_"+ reference +' .col6').html($("#waiting").html());
	// on passe au modification des champs en front :
	if(quantite == 0) // on retire le conteneur
	{
		PanierActions('supprimer', reference, 0, null);
	}
	else
	{
		$("#contentNbProduits").html(parseInt(parseInt($("#contentNbProduits").html() - oldQuantite) + newQuantite)); 
		$("#quantite_" + reference).val(newQuantite);
	}
	
	// on procede a la mise a jour en ajax
	if(update)
	{
		PanierActions('modifier', reference, newQuantite, null);
	}
}

function confirmeCookie()
{
	PanierActions('cookie','0',0);
}

function PanierActions(fonction, reference, quantite, e, callback)
{
	if(fonction == 'ajouter')
	{
		sourisxy(e);
		$.post(
		"/panier/ajouter",{ "reference" : escape(reference), "quantite" : quantite },
			function(data) { 
				handleResponse(fonction, data, null, reference, quantite);
				if(callback != null)
					callback();
			},
			"html"
		);
	}
	else
	{
		$.post(
		"/panier-content/"+ escape(fonction), { "reference" : escape(reference), "quantite" : quantite },
			function(data) { 
				$("#commandeContenu").html(data);
				
				if($("#nbArticlesPanierTop").size() == 1)
				$("#nbArticlesPanierTop").html($("#contentNbProduits").html());
				
				if($("#totalPanierTop").size() == 1)
					$("#totalPanierTop").html($("#contentTotalPanier").html())
				//handleResponse(fonction, data, null, reference, quantite);
				//if(callback != null)
				//	callback();
			},
			"html"
		);
	}	
}

function handleResponse(fonction, data, e, ref ,quantite) 
{
	var docXML = clean(data);
	
	var errLength = docXML.getElementsByTagName("errstock");
	
	if(errLength.length > 0)
	{
		var erreur = docXML.getElementsByTagName("errstock")[0].firstChild.nodeValue;
		if(erreur != null)
		{
			
			$("#disponibilite").hide();
			$("#erreurStock").show();
			return false;
		}
	}
	
	var nbTotal= docXML.getElementsByTagName("nbTotal")[0].firstChild.nodeValue;
	var prixTotal= docXML.getElementsByTagName("TOTAL")[0].firstChild.nodeValue;
	
	if($("#nbArticlesPanierTop").size() == 1)
		RemoveChildNodes(document.getElementById('nbArticlesPanierTop')).appendChild(document.createTextNode(nbTotal));
		
	if($("#totalPanierTop").size() == 1)
		RemoveChildNodes(document.getElementById('totalPanierTop')).appendChild(document.createTextNode(prixTotal));
		
	if(fonction == 'ajouter')
	{
		panierPopUp("panierPopUp", x, y, "show", ref, quantite);
	}

	if(document.getElementById('totalPanierRecap') != null)
		RemoveChildNodes(document.getElementById('totalPanierRecap')).appendChild(document.createTextNode(prixTotal));
		
	
}


////// DEBUT des fonctions de gestion de la confirmation d'ajout au panier d'un article //////
var x;
var y;

function panierPopUp(fonction, element, x, y, action, ref, quantite) {
	
	$.post(
		"/panier-popup/"+ escape(fonction), { "reference" : ref, "quantite" : quantite },
		function(data) { 
			
			if(popUpJDialog){ /** Si on utilise la popup JDialog **/
				var name 	= navigator.appName
			
				/** Params affichage **/
				var height 	= 'auto';
				var width	= 228;
				
				
				var sTop	= document.documentElement.scrollTop;
				var sLeft	= document.documentElement.scrollLeft;
				
				if (name != "Microsoft Internet Explorer") 
				{
					var posY	= (y)-(140);
					var posX	= (x)+(width - 350 );
				}
				else 
				{
					var posY	= ((y)- 160) + sTop;
					var posX	= (x)-(width - (width/2)) + sLeft;
				}
				
				
				//$("#" + element).css	= ("left", posX + "px");
				
				/* Desactivation de la popUp */
				
				$(data).dialog({
					autoOpen : true,
					height : height,
					width : width,
					modal : false,
					resizable : false,
					zIndex : 9999
					/*buttons : {						
						'Continuer vos achats': function() { $("#" + element).dialog("close"); 
												$("#" + element).dialog("destroy"); 
												$("#" + element).remove();
								   },
						'Valider ma commande': function() { $("#" + element).dialog("close"); 
												$("#" + element).dialog("destroy");
												$("#" + element).remove();
												window.location.href='/commande-panier';
								   }
					}*/
				});
				
				$(".ui-dialog-titlebar-close").click(function() {
					$("#" + element).dialog("close"); 
					$("#" + element).dialog("destroy"); 
					$("#" + element).remove();
				});
				
				$(".ui-dialog").css("top", posY + "px");
				$(".ui-dialog").css("left", posX + "px");
				
			}
			else if(!popUpJDialog){
				$("#"+element).html("");
				$("#"+element).html(data);
				$("#"+element).css("display", '');
				$("#articlesPanier").css("display", 'none');
			}
			
			
			if($("#nbArticlesPanierTop").size() == 1)
				$("#nbArticlesPanierTop").html($("#popup_quantite").val());
				
			if($("#totalPanierTop").size() == 1)
				$("#totalPanierTop").html($("#popup_prix_panier").val())
	
			
		},
		"html"
	)
	
}

function sourisxy(e)
{
  x = (navigator.appName=="Netscape") ? e.pageX : event.x + document.body.scrollLeft;
  y = (navigator.appName=="Netscape") ? e.pageY : event.y + document.body.scrollTop;
	
	if(navigator.appName.substring(0,3) == "Net")
	document.captureEvents(Event.mousemove);
	
}
////// FIN des fonctions de gestion de la confirmation d'ajout au panier d'un article //////


function SetCodePromo(code) 
{
	if(code != "" && code != "aucune")
	{
		$.post(
			"/panier-content/addcodepromo",{ "reference" : escape(code) },
			function(data) {
				//alert(data);
				$("#commandeContenu").html(data);
				
			
			/*	if(data.getElementsByTagName('messCP')[0].textContent == 'Le code que vous avez saisi n\'est pas valide.')
				{
					
				}*/
			},
			"html"
		);
	}
}

//Cette fonction ne doit normalement plus être utilisée pour Neoshop
/*
function UpdateProduitsRecap(produits) 
{
	for(var i = 0; i < produits.length; i++) 
	{
		var idproduit = produits[i].childNodes[0].firstChild.nodeValue;
		var prixTotal = produits[i].childNodes[4].firstChild.nodeValue;
		
		if($('#totalProduitRecap_' + idproduit).size() == 1)
			RemoveChildNodes($('#totalProduitRecap_' + idproduit)).appendChild(document.createTextNode(prixTotal));
	}
}
*/

