var busy=false;

function AddD2ShopCartWithConfirmation(form, n, catentryIdList, productId, nbrArtPan, destockCatgroupId, destockFfmcenterId){	
	
	var executeCmd=false;
	if (!busy) {
		busy=true;
		var storeId=form.storeId.value;
		var langId=form.langId.value;
		var catalogId=form.catalogId.value; 
		var justAddedItems = '-';
		var ahref='/webapp/wcs/stores/servlet/OrderItemAdd?calculateOrder=1&storeId='+storeId+'&langId='+langId+'&catalogId='+catalogId+'&URL=AddToBagView&productId='+productId+'&limitedCart=false';
		for(var i=0;i<n;i++) {		
			if(document.getElementById("quantity")){
				var quant=document.getElementById("quantity").value;
				if(quant > 0){
					ahref += '&catEntryId_' + i + '='+catentryIdList[i] + '&quantity_' + i + '=' + quant+ '&field2_' + i + '=' + destockCatgroupId;

					if(destockFfmcenterId) {
						ahref += '&field1_' + i + '=' + destockFfmcenterId ;
					}
					executeCmd=true;
					justAddedItems += catentryIdList[i]+'-';
				}
				else
					return;	
			} else if(document.getElementById('qty_'+i)) {
				var quant=document.getElementById('qty_'+i).value;
				if(quant > 0){
					ahref+='&catEntryId_'+i+'='+catentryIdList[i]+'&quantity_'+i+'='+quant+ '&field2_' + i + '=' + destockCatgroupId;
					
					if(destockFfmcenterId) {
						ahref += '&field1_' + i + '=' + destockFfmcenterId ;
					}
					executeCmd=true;
					justAddedItems += catentryIdList[i]+'-';
				}
			}
		}
		ahref += '&justAddedItems='+justAddedItems;

		var justAddedServices = '-';
		/*// [ATTENTION : BUG ?]
		for(var j=0;j<5;j++){
			for(var i=0;i<n;i++){
				if(document.getElementById('service_'+i+'_'+j) && document.getElementById('service_'+i+'_'+j).checked){
					var services=document.getElementById('service_'+i+'_'+j).value;
					ahref+='&services_'+i+'_'+j+'='+services;
					executeCmd=true;
					justAddedServices += services+'-';
				} 
			}
		}*/
		$("#product_form input.xSellServiceProposal:checked").each(function() {
			var service = $(this).val();
			ahref+='&'+ $(this).attr('id') + '='+ service;
			executeCmd=true;
			justAddedServices += service + '-';
		});
		
		
		if(!executeCmd){
			// ahref='ProductDisplayView?storeId='+storeId+'&langId='+langId+'&catalogId='+catalogId+'&catentryId='+productId;
			ahref='/webapp/wcs/stores/servlet/AddToBagView?storeId='+storeId+'&langId='+langId+'&catalogId='+catalogId+'&productId='+productId+'&limitedCart=false';
		}	  
		ahref += '&justAddedServices='+justAddedServices;
		
		if(destockFfmcenterId) {
			ahref += '&isDestock=1&destockFfmcenterId='+destockFfmcenterId;
		}
		//alert(ahref); return;
		
		masqueShow();
		initPopin();//dans scripts/popin.js
		showPopin();//dans scripts/popin.js

		var maCss="/webapp/wcs/stores/servlet/Conforama/css/general/neutre/color.css";
		var maLangCss="/webapp/wcs/stores/servlet/Conforama/css/fr_FR/neutre/color.css";
		$("#cssColorTemp").attr("href", maCss);
		$("#cssColorLangTemp").attr("href", maLangCss);
		scrollTo(0, 0);	
		$("#layerPopin").load(ahref, function(){$("#pleaseWaitPopinPanier").hide();$("#pleaseWaitLoadingPanier").hide();$("#pleaseWaitLoadingCancelPanier").hide();loadAll2();});

		/* cette section permet de center les 3 div qui composent la popin (sur la page d'ajout au panier d'un produit)
		 et de les centrer en fonction de la taille de l'écran de l'internaute  */
		
		//On récupère la taille de l'écran (div globale) 
		this.pageWidth = $("#contentAllWebsite").width();
		//On récupère les 3 div qui composent la popin de l'ajout au panier
		this.popinPanier = $("#pleaseWaitPopinPanier");
		this.popinLoadingPanier = $("#pleaseWaitLoadingPanier");
		this.popinCancelPanier = $("#pleaseWaitLoadingCancelPanier");
		
		//On récupère la taille de chaque div compose la popin de l'ajout au panier 
		var taillePopinPanier = this.popinPanier.width();
		var tailleLoadingPanier = this.popinLoadingPanier.width();
		var tailleLoadingCancel = this.popinCancelPanier.width();
		
		//On soustrait la taille de chaque div à la taille de la page et on divise par deux le résultat
		var popinLeft = (this.pageWidth-taillePopinPanier)/2;
		var popinLeftLoadingPanier = (this.pageWidth-tailleLoadingPanier)/2;
		var popinLeftLoadingCancel = (this.pageWidth-tailleLoadingCancel)/2;
		
		//Avec ce résultat, on affecte une nouvelle valeur à la propriété css "left" de chaque div
		this.popinPanier.css("left", popinLeft +"px");
		this.popinLoadingPanier.css("left", popinLeftLoadingPanier + "px");
		this.popinCancelPanier.css("left", popinLeftLoadingCancel + "px");
		//Ainsi, chaque div est centré sur le sens la largeur de la page quelque soit la taille de l'écran de l'internaute

		busy=false;
	} 
}

function popupWindow(url) {
	newwindow=window.open(url,'name','height=600,width=800,resizable=yes,scrollbars=yes');
	if (window.focus)
		newwindow.focus();
}

function goToShop(ahref){
	busy=false;	
	var maCss="/webapp/wcs/stores/servlet/Conforama/css/general/neutre/color.css";
	var maLangCss="/webapp/wcs/stores/servlet/Conforama/css/fr_FR/neutre/color.css";
	$("#cssColorTemp").attr("href", maCss);
	$("#cssColorLangTemp").attr("href", maLangCss);		
	$("#contentPopin").hide();
	$("#loading").show();
	busy=false;
}

function showLoadingAndSubmitForm(){ 
   busy=true;
   masqueShow();
   initPopin();//dans scripts/popin.js
   showPopin();//dans scripts/popin.js
   var maCss="/webapp/wcs/stores/servlet/Conforama/css/general/neutre/color.css";
   var maLangCss="/webapp/wcs/stores/servlet/Conforama/css/fr_FR/neutre/color.css";
   $("#cssColorTemp").attr("href", maCss);
   $("#cssColorLangTemp").attr("href", maLangCss);
   scrollTo(0, 0);	
   $("#loading").show();
   busy=false;
}

function submitForm(formName) {
	document.forms[formName].submit();
}

var busyPopin=false;
function AddD2ShopCartWithConfirmationPopin(storeId, langId, catalogId, n, catentryIdList, productId, nbrArtPan, destockCatgroupId, destockFfmcenterId){
	
	var executeCmd=false;
	if (!busyPopin) {
		busyPopin=true;
		var ahref='/webapp/wcs/stores/servlet/OrderItemAdd?calculateOrder=1&storeId='+storeId+'&langId='+langId+'&catalogId='+catalogId+'&URL=AddToBagView&productId='+productId+'&limitedCart=false';
		for(var i=0;i<n;i++) {		
			if(document.getElementById("quantity")){
				var quant=document.getElementById("quantity").value;
				if(quant > 0){
					ahref+='&catEntryId_'+i+'='+catentryIdList[i]+'&quantity_'+i+'='+quant+ '&field2_' + i + '=' + destockCatgroupId;
					
					if(destockFfmcenterId) {
						ahref += '&field1_' + i + '=' + destockFfmcenterId ;
					}
					
					executeCmd=true;
				}
				else
					return;	
			} else if(document.getElementById('qty_'+i)) {
				var quant=document.getElementById('qty_'+i).value;
				if(quant > 0){
					ahref+='&catEntryId_'+i+'='+catentryIdList[i]+'&quantity_'+i+'='+quant+ '&field2_' + i + '=' + destockCatgroupId;
					
					if(destockFfmcenterId) {
						ahref += '&field1_' + i + '=' + destockFfmcenterId ;
					}
					
					executeCmd=true;
				}
			} 
		}

		var justAddedServices = '-';
		// [ATTENTION : BUG ?]
		for(var j=0;j<5;j++){
			for(var i=0;i<n;i++){
				if(document.getElementById('popinservice_'+i+'_'+j) && document.getElementById('popinservice_'+i+'_'+j).checked){
					var services=document.getElementById('popinservice_'+i+'_'+j).value;
					ahref+='&services_'+i+'_'+j+'='+services;
					executeCmd=true;
					justAddedServices += services+'-';
				} 
			}
		}
		if(!executeCmd){
			ahref='/webapp/wcs/stores/servlet/AddToBagView?storeId='+storeId+'&langId='+langId+'&catalogId='+catalogId+'&productId='+productId+'&limitedCart=false';
		}	  
		ahref += '&justAddedServices='+justAddedServices;
		
		if(destockFfmcenterId) {
			ahref += '&isDestock=1&destockFfmcenterId='+destockFfmcenterId;
		}
		
		var maCss="/webapp/wcs/stores/servlet/Conforama/css/general/neutre/color.css";
		var maLangCss="/webapp/wcs/stores/servlet/Conforama/css/fr_FR/neutre/color.css";
		$("#cssColorTemp").attr("href", maCss);
		$("#cssColorLangTemp").attr("href", maLangCss);
		scrollTo(0, 0);	
		$("div.popin").hide();
		$("#layerPopin").load(ahref, function(){$("div.popin").show();$("#pleaseWaitPopinPanier").hide();$("#pleaseWaitLoadingPanier").hide();$("#pleaseWaitLoadingCancelPanier").hide();loadAll2();});
		busyPopin=false;
	} 
	//}
}
