function include(src){ 
var script=document.createElement('script');
script.type='text/javascript';
script.src=src;
document.getElementsByTagName('head')[0].appendChild(script);
}
var curentGloblaContext="notexist";
/*function getCurrentContext(){
    if(curentGloblaContext == "notexist"){
        DWRContextualizer.getCurrentContext({
            callback:function(data) {

                curentGloblaContext =data;
                return curentGloblaContext;
            }
        });
    }else{
        // alert("curentGloblaContext ok" )
        return curentGloblaContext;
    }

}*/
function getCurrentContext(){
    if(getCookie("anonymous_save")!=null){
        var contextParam = {
            'save':getCookie("anonymous_save"),
            'shop':getCookie("anonymous_shop")
        }
        //alert("save "+contextParam.save+" shop "+contextParam.shop);
        return contextParam;
    }else if(getCookie("ci_save")!=null) {
        var contextParam = {
            'save':getCookie("ci_save"),
            'shop':getCookie("ci_shop")
        }
        return contextParam;
    }
    else{
        return null;
    }

}
getCurrentContext();
//include('/javascript/shoppingcart/shoppingcart.js');
function fermerpopup() {
    if (!cache_popup) {
        obj_layer_popup.innerHTML = '';
        obj_layer_popup.visibility = "hidden";
        cache_popup = true;
    }
}
function removeContext() {
	var dateMag = new Date();
	dateMag.setTime(dateMag.getTime() + 365*24*60*60*1000);
	setCookie("anonymous_save", false, dateMag, "/", "." + window.location.hostname);
	location.reload();
}
function restoreContext() {
	var dateMag = new Date();
	dateMag.setTime(dateMag.getTime() + 365*24*60*60*1000);
	setCookie("anonymous_save", true, dateMag, "/", "." + window.location.hostname);
	location.reload();
}
function checkLists(action) {
    BasketMgt.isBasketEmpty(function(data) {
        var basketEmpty = data;
        ListCourseService.isListeEmpty(function(data) {
            var listeCourseEmpty = data;
            if (action == "remove"){
                if(!basketEmpty || !listeCourseEmpty){
                    openPopupQuitter(basketEmpty, listeCourseEmpty);
                }else{
                    var doSave = "false";
                    setSaveInSession(doSave);
                }
            }
            if (action == "restore"){
                if(!basketEmpty || !listeCourseEmpty){
                    openPopupAcceder(basketEmpty, listeCourseEmpty);
                }else{
                    var doSave = "true";
                    setSaveInSession(doSave);
                }
            }
            
        });
    });
}
function openPopupQuitter(basketEmpty, listeCourseEmpty) {
    init_layer_popup();
    
    if (obj_layer_popup) {	
        cache_popup = false;
        
        var ch_texte = '<div id="popup_gen">';
        ch_texte += '<div id="popup-conf">';
        ch_texte += '<div id="content">';
        ch_texte += '<img src="/images/pickup/avertissement.gif" alt="Avertissement" />';
        ch_texte += '<p class="texte">En quittant votre magasin, <br />';
        ch_texte += '<span>vous allez perdre le contenu ';
        if(!basketEmpty && !listeCourseEmpty){
            ch_texte += 'de votre liste et de votre panier. ';
        } else {
            if(!basketEmpty){    
                ch_texte += 'de votre panier. ';
            }
            if(!listeCourseEmpty){    
                ch_texte += 'de votre liste. ';
            }
        }
        ch_texte += '</span><br />';
        ch_texte += '<span class="conf">Souhaitez-vous continuer ?</span></p>';
        ch_texte += '<form>';
        ch_texte += '<div id="submit">';
        ch_texte += '<a href="#" onClick="setSaveInSession(';
        ch_texte += "'false'";
        ch_texte += ');"><img src="/images/pickup/btn-oui.gif" alt="OUI" /></a>';
        ch_texte += '</div>';
        ch_texte += '</form>';
        ch_texte += '<div id="retour"><a href="#" onclick="fermerpopup();"><img src="/images/pickup/btn-non.gif" alt="NON" /></a></div>';
        ch_texte += '</div>';
        ch_texte += '</div>';
        ch_texte += '</div>';
        
        contenu_layer_popup(ch_texte);
    }
}
function openPopupAcceder(basketEmpty, listeCourseEmpty) {
    init_layer_popup();
    
    if (obj_layer_popup) {	
        cache_popup = false;
        
        var ch_texte = '<div id="popup_gen">';
        ch_texte += '<div id="popup-conf">';
        ch_texte += '<div id="content">';
        ch_texte += '<img src="/images/pickup/avertissement.gif" alt="Avertissement" />';
        ch_texte += '<p class="texte">En accedant votre magasin, <br />';
        ch_texte += '<span>vous allez perdre le contenu ';
        if(!basketEmpty && !listeCourseEmpty){
            ch_texte += 'de votre liste et de votre panier. ';
        } else {
            if(!basketEmpty){    
                ch_texte += 'de votre panier. ';
            }
            if(!listeCourseEmpty){    
                ch_texte += 'de votre liste. ';
            }
        }
        ch_texte += '</span><br />';
        ch_texte += '<span class="conf">Souhaitez-vous continuer ?</span></p>';
        ch_texte += '<form>';
        ch_texte += '<div id="submit">';
        ch_texte += '<a href="#" onClick="setSaveInSession(';
        ch_texte += "'true'";
        ch_texte += ');"><img src="/images/pickup/btn-oui.gif" alt="OUI" /></a>';
        ch_texte += '</div>';
        ch_texte += '</form>';
        ch_texte += '<div id="retour"><a href="#" onclick="fermerpopup();"><img src="/images/pickup/btn-non.gif" alt="NON" /></a></div>';
        ch_texte += '</div>';
        ch_texte += '</div>';
        ch_texte += '</div>';
        
        contenu_layer_popup(ch_texte);
    }
}
function setSaveInSession(doSave) {
    DWRContextualizer.getContext("anonymous" ,function(data) {
        var currentShop = data;
        if (currentShop) {
	    var shopCode2 = ''+currentShop.shop;
	    while (shopCode2.length < 3)
	    {
	    shopCode2 = '0' + shopCode2;
	    }
	    //alert ('contextualize shopCode2 true ='+shopCode2);
	    var context = { shop:shopCode2, save:doSaveTrue };
            
            DWRContextualizer.registerContext("anonymous", context, "36000000", function(data) {
                DWRContextualizer.registerContextInSession("anonymous", function(data) {
                    BasketMgt.emptyBasket(function(data) {
                        ListCourseService.emptyList(function(data) {
                            displayHome();
                        });
                    });
                });
            });
        }
    });
}
function displayHome() {
    var sURL='/';
    window.location.href=sURL;
}
function refreshFromDeconnexionCI() {
   // DWRContextualizer.getCurrentContext(function(data) {
       var data=getCurrentContext();
        var oldContext = data;
        if(data) {
            DWRContextualizer.getContext("anonymous", function(data) {
                var newContext = data;
                if (newContext.shop == "null") {
                    DWRContextualizer.dropContextFromSession(function(data) {
                        clearUser();
                    });
                } else if (newContext.shop != oldContext.shop) {
                    BasketMgt.emptyBasket(function(data) {
                        ListCourseService.emptyList(function(data) {
                            DWRContextualizer.registerContextInSession(newContext, function(data) {
                                clearUser();
                            });
                        });
                    });
                } else {
                    DWRContextualizer.registerContextInSession(newContext, function(data) {
                        clearUser();
                    });
                }
            });
        } else {
            clearUser();
        }
 //  });
}
function accederMagsinCI() {
    DWRContextualizer.getContext("ci" ,function(data) {
        var ciShop = data;
        if (ciShop) {
            var doSave = "true";
	    var shopCode2 = ''+ciShop.shop;
	    while (shopCode2.length < 3)
	    {
	    shopCode2 = '0' + shopCode2;
	    }

            var context = { shop:shopCode2, save:doSave };
            
            DWRContextualizer.registerContext("ci", context, "36000000", function(data) {
                DWRContextualizer.registerContextInSession("ci", function(data) {
                    BasketMgt.emptyBasket(function(data) {
                        ListCourseService.emptyList(function(data) {
                            DWRShopContextualizer.getContextualizedShopEIIDByCode(ciShop.shop, function(data) {
                                var eiidmag = data;
                                var origin = { place:"tableaudebord", idlspub:eiidmag };
                                displayMag(origin);
                            });
                        });
                    });
                });
            });
        }
    });
}
function displayHomeCI() {
    var sURL='/mpng2-front/pre?zone=zoneespaceperso&srvname=srvuserlogin&useraccounttodo=home&pageid=23';
    window.location.href=sURL;
}
function setNonVendu() {
    //DWRContextualizer.getCurrentContext(function(data) {
        var data=getCurrentContext();
        var currentShop = data;
        if (currentShop) {
            if (!( currentShop.save =='false') ) {
                if(document.getElementById('nonvendu')) {
                    document.getElementById('nonvendu').style['display'] = 'block';
                }
            }
        } 
   //});
}
function setNonVenduMultiple(name) {
   // DWRContextualizer.getCurrentContext(function(data) {
    var data=getCurrentContext();
        var currentShop = data;
        if (currentShop) {
            if (!( currentShop.save =='false') ) {
                if(document.getElementById(name)) {
                    document.getElementById(name).style['display'] = 'block';
                }
            }
        } 
    //});
}
function isShopContextualized() {
  //  DWRContextualizer.getCurrentContext(function(data) {
   var data=getCurrentContext();
        var currentShop = data;
        if (currentShop) {
            if ( currentShop.save =='false' ) {
                document.getElementById('shopcontextualise').innerHTML = 'no';
            } else {
                document.getElementById('shopcontextualise').innerHTML = 'yes';
            }
        }
        else{
            document.getElementById('shopcontextualise').innerHTML = 'no';
        } 
   // });
}
/*Ouvre les popups html pickup lot 2*/
function popup(url, titre, largeur, hauteur, scroll) {
    window.open(url, titre, 'width=' + largeur + ', height=' + hauteur + ', scrollbars=' + scroll +',resizeable=no');
}
function getShopName(index) {
    if(index==undefined) index="";
    //DWRContextualizer.getCurrentContext(function(data) {
     var data=getCurrentContext();
        var currentShop = data;        
        if (currentShop && currentShop!= "notexist" && (currentShop.save == "true" || currentShop.save==true)) {
           // alert("getShopName "+currentShop.save);
	   var shopCode = currentShop.shop;
	   while (shopCode.indexOf('0') == 0)
	   {
	   shopCode = shopCode.substring(1);
	   }
           // alert("getShopName"+shopCode);
            DWRShopContextualizer.getContextualizedShopByCode(shopCode, function(datashop) {
                var contextualizedShop = datashop;
                if( document.getElementById('shopname'+index) ){
                    document.getElementById('shopname'+index).innerHTML = contextualizedShop.name;
                }
                if( document.getElementById('shopnamepickup'+index) ){
                    document.getElementById('shopnamepickup'+index).innerHTML = contextualizedShop.name;
                }
            });
        }
    //});
}
var requestShopNameMultiple= null;
var valideShopNameMultiple=0;
function getShopNameMultiple(countmax) {
if(valideShopNameMultiple ==0){    
    if(countmax==undefined) 
        var countmaxint = 0;
    else
        var countmaxint = countmax;//parseInt(countmax,10);
  //  DWRContextualizer.getCurrentContext({callback:function(data) {
             var data=getCurrentContext();
        var currentShop = data;        
        if (currentShop && currentShop!= "notexist" && (currentShop.save == "true" || currentShop.save==true) ) {
//           alert("getShopNameMultiple "+currentShop.shop);
	   var shopCode = currentShop.shop;
	   while (shopCode.indexOf('0') == 0)
	   {
	   shopCode = shopCode.substring(1);
	   }
            DWRShopContextualizer.getContextualizedShopByCode( shopCode, {callback:function(datashop) {
                var contextualizedShop = datashop;
                var count=0;
                while( document.getElementById('shopname'+count) ||  count < countmaxint ){
                    if( document.getElementById('shopname'+count) ){
                        document.getElementById('shopname'+count).innerHTML = contextualizedShop.name;
                    }
                    count++ ;
                }                
             valideShopNameMultiple=1;   
            }, 
    timeout:5000,       
    errorHandler:function(msg) {
         // alert("Une erreur s'est produite getContextualizedShopByCode : "+ msg);
    }
    
    });
        }
    /*},
    timeout:5000,       
    errorHandler:function(msg) {
           //alert("Une erreur s'est produite getCurrentContext : "+ msg);
    }
    
    });*/
    }
else{
    }
clearTimeout(requestShopNameMultiple);
}
function setNonVenduAll(countmax){
    var countmaxint = parseInt(countmax,10);
    //DWRContextualizer.getCurrentContext(function(data) {
         var data=getCurrentContext();
        var currentShop = data;
        if (currentShop) {
            if (!( currentShop.save =='false') ) {
                var count = 0;
                while(count < countmaxint ) {
                    if(document.getElementById('nonvendu'+count)){
                        document.getElementById('nonvendu'+count).style['display'] = 'block';
                    }
                    count ++;
                }
            }
        } 
   // });
}
function arrondi(chaine) {
    if (chaine.indexOf(".")>0) {
        if (chaine.length - chaine.indexOf(".")==2) {chaine = chaine + "0";}
        else if (chaine.length - chaine.indexOf(".")==1) {chaine = chaine + "00";}
    }
    if (chaine.indexOf(".")== -1) {
        chaine = chaine + ".00";
    }
    return chaine;
}
var pricepopup ="";
var nop = 1;
var totalprice=0;
function calculpopup(pos) {
    nop = document.getElementById('nopcommand').value ; 
    var varprix = pricepopup.replace(",",".");
    var somme = Math.round(nop * varprix * 100 ) / 100 ;
    somme = arrondi( somme.toString() );
    document.getElementById('calculprixpopup_'+pos).innerHTML= somme;
}
/*********************************************
 POPUP : ajout +/- 1 quantites
 *********************************************/
function incrementPanierPopup(valeurAjout,btnQuantite) {
    if(Number(document.getElementById('nopcommand').value) + Number(valeurAjout) > 0){
        document.getElementById('nopcommand').value = Number(document.getElementById('nopcommand').value) + Number(valeurAjout);
    }
}
/*********************************************
 POPUP : verification quantites
 *********************************************/
function checkQtePopup(action, object, eiidtoadd, reflm, pos, stock, toppickup ) {
    
    var qte = 1;

    if(document.getElementById('nopcommand')){
        qte = parseInt(document.getElementById('nopcommand').value);
    }
    
    //if (isNaN(qte)){
    //    alert("La valeur saisie est incorrect");
    //    document.getElementById('nopcommand').value = 1;			
    //    calculpopup(pos);		
    //}else 
    if (qte < 0){
        alert("Vous ne pouvez pas saisir de quantit\u00e9 n\u00e9gative.");
        document.getElementById('nopcommand').value = 1;			
        calculpopup(pos);
    }else if (qte > 9999){
        alert("La quantit\u00e9 saisie ne doit pas d\u00e9passer 9999.");
        document.getElementById('nopcommand').value = 9999;			
        calculpopup(pos);
    }else if (qte == 0){
        alert("La quantit\u00e9 saisie doit etre sup\u00e9rieure a 0.");
        document.getElementById('nopcommand').value = 1;			
        calculpopup(pos);	
    }else{
        if (action == 'increment'){
            incrementPanierPopup(1, object);
            calculpopup(pos);
        }else if (action == 'decrement'){
            incrementPanierPopup(-1, object);
            calculpopup(pos);
        }else if (action == 'qte'){
            document.getElementById('nopcommand').value = qte;	
            calculpopup(pos);
        }else if (action == 'add'){
            //alert('add  BasketMgt=' +BasketMgt +'   eiidtoadd='+eiidtoadd + ' document.getElementByIdnopcommand).value='+document.getElementById('nopcommand').value );
            //BasketMgt.addProduct( eiidtoadd, document.getElementById('nopcommand').value, { callback:function(data) { refreshLightBasket(); showPopup('panier'); }});
            //alert('addShoppingCartProduct eiidtoadd='+eiidtoadd + ' document.getElementByIdnopcommand).value='+document.getElementById('nopcommand').value  +'  pos='+pos);
            //addShoppingCartProduct('1',parseInt(document.getElementById('nopcommand').value), pos);
            addProductWithStock('1',eiidtoadd, reflm, parseInt(stock),qte,pos,toppickup);
//            displayPanier();
	    setShopCode();
        }else if (action == 'pickup'){
            //alert('stock ('+stock+') >= qtecommande ('+ qtecommande +')  + nopcommand ('+ document.getElementById('nopcommand').value +')')
            //if( parseInt(stock,10) >= parseInt(qtecommande,10) + parseInt(document.getElementById('nopcommand').value,10) ){
            //if( parseInt(stock,10) >= parseInt(document.getElementById('nopcommand').value,10) ){
                //BasketMgt.addProduct( eiidtoadd , document.getElementById('nopcommand').value, { callback:function(data) {     refreshLightBasket();showPopup('panier');displayPanier();}});
                //alert('addShoppingCartProduct eiidtoadd='+eiidtoadd + ' document.getElementByIdnopcommand).value='+document.getElementById('nopcommand').value +'  pos='+pos);
                //addShoppingCartProduct('2',parseInt(document.getElementById('nopcommand').value), pos);
                addProductWithStock('2',eiidtoadd, reflm, parseInt(stock),qte,pos,toppickup);
                //displayPanier();
                setShopCode();
            //}
            //else{ //stock < qtecommande
                //popup('pre?zone=zonecatalogue&idEIPub='+eiidtoadd+'&idMDPPub=1228743587&mdpid=1227883446&eipublicationtype=VELOCITY&zonetmp=zoneimpressionsl&pre-pos='+pos,'','600','350');
            //    popup('pre?zone=zonecatalogue&idEIPub='+eiidtoadd+'&idMDPPub=1228743587&mdpid=1227883446&eipublicationtype=VELOCITY&zonetmp=zoneimpressionsl&pre-stockreel='+stock+'&pre-pos='+pos,'','600','350');
            //}
        }
    }
    
}
function displayPanier(bloc,qtecommande,jourplusun,delay,pos){
    i=0;
    stylePanier = "";
    while(document.getElementById('popPanier'+i)){
        document.getElementById('popPanier'+i).innerHTML = '<img src="/images/transparent.gif" alt="" />';
        i++;
    }
    i=1;
    while(document.getElementById('popPanier0'+i)){
        document.getElementById('popPanier0'+i).innerHTML = '<img src="/images/transparent.gif" alt="" />';
        i++;
    }
    
    if(bloc){
    var posp='';    
    //alert('pos ['+pos+'] qtecommande '+qtecommande+' jourplusun '+jourplusun+' delay '+delay);    
    if(pos==null || pos==''){pos='';}else{posp='_'+pos;}
    var price =document.getElementById("UnitPriceATA"+posp).innerHTML;
    if(document.getElementById("DiscountAmountATA"+posp))
        price =document.getElementById("DiscountAmountATA"+posp).innerHTML;
    var topPickup ='';
    if(document.getElementById("TopPickup"+posp)){topPickup = document.getElementById("TopPickup"+posp).innerHTML;}
    modevente = document.getElementById("ModeVente"+posp).innerHTML;
    var topRestock ='';
    if(document.getElementById("TopRestock"+posp)){topRestock = document.getElementById("TopRestock"+posp).innerHTML;}    
    var stock ='5';
    if(document.getElementById("Stock"+posp)){stock = document.getElementById("Stock"+posp).innerHTML;}    
    var delais =document.getElementById("DeliveryDelay"+posp).innerHTML;        
    var eiid = document.getElementById("eiid"+posp).innerHTML;
    var reflm = document.getElementById("reflm"+posp).innerHTML;
    var nopcommande = qtecommande;
    if(topPickup =='1' && modevente =="non"){
        stylePanier = "stylePanier1";
    }
    else if(topPickup != '1' && modevente =="oui"){		
        stylePanier = "stylePanier2";
    }
    else{
        stylePanier = "stylePanier3";
    }
    
    //alert(bloc+' - '+price+' - '+topPickup+' - '+modevente+' - '+topRestock+' - '+stock+' - '+qtecommande+' - '+delais+' - '+jourplusun+' - '+delay+' - '+eiid+' - '+nopcommande);
    
    pricepopup = price;
        html = '<div id="bloccommande" class="bloc-commander '+stylePanier+'">';
        html +='<input type="hidden" name="prix0" value="'+ price+'"> ';
        html +='      	<div class="close"><a href="javascript:displayPanier();">X</a></div>';
        html +='          <div class="titreBloc">Commander</div>';
        html +='          <div class="colonnes">';
        html +='          <div class="col-left">';
        html +='          	Quantit&eacute;<br />';
        html +='              <a href="javascript:void(0);"><img border="0" src="/images/common/bloc-commander-btn-moins.gif" onclick="checkQtePopup(';
        html +="'decrement'";
        html +=',this);" alt="Moins" /></a><input id="nopcommand" name="nopcommand" type="text" class="inputQuantite" value="'+qtecommande+'"  onblur="javascript:checkQtePopup(';
        html +="'qte'";
        html +=', this)" readonly /><a href="javascript:void(0);"><img border="0" src="/images/common/bloc-commander-btn-plus.gif" onclick="checkQtePopup(';
        html +="'increment'";
        html +=',this);" alt="Plus" /></a>';
        html +='          </div>';
        html +='          <div class="col-right">';
        html +='          	Prix Total<br /><strong><span id="calculprixpopup" style="color: #FFFFFF;"></span>&nbsp;EUR</strong>';
        html +='          </div>';
        html +='          </div>';
        if(topPickup =='1'){
            html +='          <div class="marginTopBtn"><a href="javascript:checkQtePopup(\'pickup\',this,\''+eiid+'\',\''+reflm+'\',\''+pos+'\',\''+stock+'\',\''+topPickup+'\');"><img border="0" src="/images/common/bloc-commander-btn-retrait.gif" alt="Retrait en magasin" /></a><br />';
            if(stock != 0){
                html +='&Agrave; partir du<br><span id="promessepickuppopup" style="color: #FFFFFF;font-weight: normal;"></span>&nbsp;&agrave;&nbsp;<span id="hours" style="color: #FFFFFF;font-weight: normal;"></span>h00';
            }
            else{
                if(topRestock =='1'){
                    html +='&Agrave; partir du<br>'+ jourplusun +'&nbsp;&agrave;&nbsp;<span id="hours" style="color: #FFFFFF;font-weight: normal;"></span>h00';
                }
                if(topRestock =='2'){
                    html +='sur commande dans un d&eacute;lai de '+ delay +' jours*';
                }
            }
            html +='</div>';
        }
        if(modevente =="oui"){
            html +='          <div class="marginTopBtn"><a href="javascript:checkQtePopup(';
            html +="'add'";
            html +=',this,\''+ eiid +'\',\''+reflm+'\',\''+pos+'\',\''+stock+'\',\''+topPickup+'\');displayPanier();';
            html +='"><img border="0" src="/images/common/bloc-commander-btn-livraison.gif" alt="Livraison &agrave; domicile" /></a><br />Sous ';
            html += delais;
            html +=' jours ouvr&eacute;s</div>';
        }
        if(topPickup =='1' && modevente !="oui"){
        html +='<div id="mag" class="marginTopBtn"><a href="javascript:popup(\'/mpng2-front/pre?zone=zonestatiqueonly&srvstatique-staticpagename=/html/pickup/popup-livraison-magasin.html\',\'\',\'715\',\'450\')" class="lienText">Livraison par mon<br>magasin</a></div>';
        }
        html +='<SCRIPT>calculpopup('+pos+');</SCRIPT>';
        html +='   </div>';
       // alert('bloc '+bloc)
        if(topPickup == '1' ){
        document.getElementById(bloc).innerHTML = html;
        
        //set nop from url 
        if(nopcommande && nopcommande != ''){
            nop = Number(nopcommande);
            document.getElementById('nopcommand').value = nopcommande;
        }
        if(topPickup =='1' && modevente !="oui"){
        //DWRContextualizer.getCurrentContext(function(data) {
         var data=getCurrentContext();
            var currentShop = data; 
            //alert("currentShop.shop "+currentShop.shop);
            
            if(currentShop.shop == larochelle) {
                if(document.getElementById('mag')){
                    document.getElementById('mag').style.display='block';
                }
            }
            else{
                if(document.getElementById('mag')){
                    document.getElementById('mag').style.display='none';
                }
            }
       // });//DWRContextualizer.getCurrentContext
        }

        calculpopup(pos);
        }else{
                checkQtePopup('add',this,""+eiid,""+reflm,""+pos,""+stock,""+topPickup);
        }
    }
    var StockDate =new Date();
    var StockDay =StockDate.getDate();   
     if(StockDay < 10){StockDay="0"+StockDay;}    
     var StockMonth =StockDate.getMonth()+1; 
     if(StockMonth < 10){StockMonth="0"+StockMonth; } 
     
    getNextShoppingDay(getTheDate(StockDay+"/"+StockMonth+"/"+StockDate.getFullYear()),"popup");
    getHours();
    
} 
function showProductDeco(productid,libelle,thismodevente,nouveaute,bloc,qtecommande,jourplusun,delay,pos) {
    _id_layer = "popup";    
     init_layer_popup();
    
    if (obj_layer_popup) {	
        cache_popup = false;
        startEffect2('masque');
        var ch_texte = '';
        if(libelle=='produit'){
            ch_texte +='<div id="contentPopup" class="pop_">';
            ch_texte +='<div class="fermer"><a href="javascript:fermerpopup()"><img src="/images/liste_produits/btn-fermer_.gif" alt="fermer" /></a></div><div class="clearBoth"></div>';
            ch_texte +='<div class="floatLeft width50pc">';
            ch_texte +='<div class="produit">';
            if(nouveaute == 'oui'){
                ch_texte +='<img src="/images/liste_produits/nouveau.gif">';
            }
            ch_texte +=  document.getElementById(productid).value ;   
             ch_texte +=  document.getElementById('disponibilite_'+pos).innerHTML ;
             var etopPickup = document.getElementById('TopPickup_'+pos).innerHTML ;
            if(thismodevente == 'oui' || etopPickup == "1"){
                ch_texte +='<a href="javascript:void(0);" onclick="fermerpopup();displayPanier(\'popPanier'+pos+'\',1,jourplusunarray['+pos+'],delayarray['+pos+'],\''+pos+'\');"><img src="/images/liste_produits/btn-panier_.gif"></a><br><img src="/images/liste_produits/shim.gif" height="5" /><br />';
               //ch_texte +='<a href="javascript:void(0);" onMouseDown="BasketMgt.addProduct( '+productid+', 1, { callback:function(data) { refreshLightBasket(); showProduct('+productid+',\'panier\',\''+modevente+'\',\''+nouveaute+'\')}});"><img src="/images/liste_produits/btn-panier_.gif"></a><br><img src="/images/liste_produits/shim.gif" height="5" /><br />';
            }
            ch_texte +='<a href="javascript:void(0);" onMouseDown="ListCourseService.addProduct( '+productid+', 1, { callback:function(data) { refreshLightBasket(); showProduct('+productid+',\'liste\',\''+thismodevente+'\',\''+ nouveaute +'\')}});"><img src="/images/liste_produits/btn-liste_.gif" border="0"></a></div>';	
            ch_texte +='<div class="clearBoth"></div>';
            
            ch_texte +=  '</div>';
        }
        else if(libelle=='panier'){
            ch_texte +='<div id="contentPopup" class="pop">';
            ch_texte +='<div class="titre"><img src="/images/liste_produits/titre-panier.gif" alt="Produit ajout&eacute; &agrave; votre panier" /></div>';
            ch_texte +='<div class="floatLeft width50pc">';
            ch_texte +='<div class="produit">';
            if(nouveaute == 'oui'){
                ch_texte +='<img src="/images/liste_produits/nouveau.gif">';
            }
            ch_texte +=  document.getElementById('ap-'+productid).value ;
            ch_texte +='<a href="javascript:fermerpopup();"><img src="/images/liste_produits/btn-poursuivre-achats.gif" alt="Poursuivre mes achats"></a><br><img src="/images/liste_produits/shim.gif" height="5" /><br />';
            ch_texte +='<a href="pre?zone=zoneprocessachat&todo=show&pageid=17"><img src="/images/liste_produits/btn-acceder-panier.gif" border="0"></a>';
            ch_texte +='</div>	';
            ch_texte +='<div class="clearBoth"></div>';        
            ch_texte +=  '</div>';
        }
        else if(libelle=='liste'){
            ch_texte +='<div id="contentPopup" class="pop">';
            ch_texte+='<div class="titre"><img src="/images/liste_produits/titre-liste.gif" alt="Produit ajout&eacute; &agrave; votre liste" /></div>';
            ch_texte +='<div class="floatLeft width50pc">';
            ch_texte +='<div class="produit">';
            if(nouveaute == 'oui'){
                ch_texte +='<img src="/images/liste_produits/nouveau.gif">';
            }
            ch_texte +=  document.getElementById(productid).value ;
            ch_texte +='<a href="javascript:fermer();"><img src="/images/liste_produits/btn-poursuivre-achats.gif" alt="Poursuivre mes achats"></a><br><img src="/images/liste_produits/shim.gif" height="5" /><br />';
            ch_texte +='<a href="pre?zone=zonelistecourse&srvname=srvlistecourse&action=showlistecourse&pageid=17"><img src="/images/liste_produits/btn-acceder-liste.gif" border="0"></a>';
            ch_texte +='</div>	';
            ch_texte +='<div class="clearBoth"></div>';        
            ch_texte +=  '</div>';
        }
        
        contenu_layer_popup(ch_texte);
        //refreshLightBasket();
    }
}
function setShopCode() {
   // DWRContextualizer.getCurrentContext({
var data=getCurrentContext();
         //callback:function(data) {
                if (data && data.save=='true') {setShopID(data.shop);}
                else{setShopID("0");;}
        /* },
        timeout:5000, 
        errorHandler:function(msg) {  //alert('getShopCode error=> '+data); 
        },
        exceptionHandler:function(msg) { //alert('getShopCode exception=> '+data); 
        } 		
    });*/
}
var globalcartID= null;
//Get ID of cart by XMLHttpRequest groovy 
function setShopID(shopid){
    
 var carturl="";
 if(document.location && document.location.protocol)
		{
		protocole = 'http'+((document.location.protocol=='https:')?'s':'')+':';
		}
 else
		{
		protocole = 'http:';
		}
	
 if (protocole == 'https:')
		{
                carturl="pre?zone=zoneimpressioncommande&todo=storeshop&shopid="+shopid;
		}
 else
		{
                carturl="pre?zone=zonetest2print&todo=storeshop&shopid="+shopid;
		}    
    
  var xhr=null;
   try
   {
     xhr = new XMLHttpRequest(); 
   } catch(e)
   { 
     try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } 
     catch (e2)
    { 
       try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } 
       catch (e) {alert('ERREUR');}
    }
  }
xhr.open('GET', carturl , false); 
xhr.send(null); 
if(xhr.readyState == 4){
    var response = xhr.responseText;
    //alert(response);
 }
}
function checkListsSC(action) {
    
    var cartid = getCartID(); 
    var ALL = 'ALL';
    ListCourseService.isListeEmpty(function(data) {
        var listeCourseEmpty = data;                
        
        DWRLMShoppingCart.getShoppingCartInfo(cartid,ALL, {
            callback:function(msg) { 
                //var listeCourseEmpty = true;
                var basketEmpty = true;
                tabInfos = msg;
                var basketnbr = tabInfos.cartItemCount;
                //alert('basketnbr='+basketnbr);
                if(basketnbr > 0){ basketEmpty=false; }
                
                if (action == "remove"){
                    if(!basketEmpty || basketEmpty == "false"  || !listeCourseEmpty|| listeCourseEmpty =="false"){
                        openPopupQuitterSC(basketEmpty, listeCourseEmpty);
                    }else{
                        var doSave = "false";
                        setSaveInSessionSC(doSave);
                    }
                }
                if (action == "restore"){
                    if(!basketEmpty || !listeCourseEmpty){
                        openPopupAccederSC(basketEmpty, listeCourseEmpty);
                    }else{
                        var doSave = "true";
                        setSaveInSessionSC(doSave);
                    }
                }
                
            },			
            timeout:5000, 
            errorHandler:function(msg) { /* alert('checkListsSC error=> '+msg);*/ },
            exceptionHandler:function(msg) { /*alert('checkListsSC exception=> '+msg); */} 		
        });
    });
}
function openPopupAccederSC(basketEmpty, listeCourseEmpty) {
    init_layer_popup();
    
    if (obj_layer_popup) {	
        cache_popup = false;
        
        var ch_texte = '<div id="popup_gen">';
        ch_texte += '<div id="popup-conf">';
        ch_texte += '<div id="content">';
        ch_texte += '<img src="/images/pickup/avertissement.gif" alt="Avertissement" />';
        ch_texte += '<p class="texte">En accedant votre magasin, <br />';
        ch_texte += '<span>vous allez perdre le contenu ';
        if(!basketEmpty && !listeCourseEmpty){
            ch_texte += 'de votre liste et de votre panier. ';
        } else {
            if(!basketEmpty){    
                ch_texte += 'de votre panier. ';
            }
            if(!listeCourseEmpty){    
                ch_texte += 'de votre liste. ';
            }
        }
        ch_texte += '</span><br />';
        ch_texte += '<span class="conf">Souhaitez-vous continuer ?</span></p>';
        ch_texte += '<form>';
        ch_texte += '<div id="submit">';
        ch_texte += '<a href="#" onClick="setSaveInSessionSC(';
        ch_texte += "'true'";
        ch_texte += ');"><img src="/images/pickup/btn-oui.gif" alt="OUI" /></a>';
        ch_texte += '</div>';
        ch_texte += '</form>';
        ch_texte += '<div id="retour"><a href="#" onclick="fermerpopup();"><img src="/images/pickup/btn-non.gif" alt="NON" /></a></div>';
        ch_texte += '</div>';
        ch_texte += '</div>';
        ch_texte += '</div>';
        
        contenu_layer_popup(ch_texte);
    }
}
function openPopupQuitterSC(basketEmpty, listeCourseEmpty) {
    init_layer_popup();
    
    if (obj_layer_popup) {	
        cache_popup = false;
        
        var ch_texte = '<div id="popup_gen">';
        ch_texte += '<div id="popup-conf">';
        ch_texte += '<div id="content">';
        ch_texte += '<img src="/images/pickup/avertissement.gif" alt="Avertissement" />';
        ch_texte += '<p class="texte">En quittant votre magasin, <br />';
        ch_texte += '<span>vous allez perdre le contenu ';
        if(!basketEmpty && !listeCourseEmpty){
            ch_texte += 'de votre liste et de votre panier. ';
        } else {
            if(!basketEmpty){    
                ch_texte += 'de votre panier. ';
            }
            if(!listeCourseEmpty){    
                ch_texte += 'de votre liste. ';
            }
        }
        ch_texte += '</span><br />';
        ch_texte += '<span class="conf">Souhaitez-vous continuer ?</span></p>';
        ch_texte += '<form>';
        ch_texte += '<div id="submit">';
        ch_texte += '<a href="#" onClick="setSaveInSessionSC(';
        ch_texte += "'false'";
        ch_texte += ');"><img src="/images/pickup/btn-oui.gif" alt="OUI" /></a>';
        ch_texte += '</div>';
        ch_texte += '</form>';
        ch_texte += '<div id="retour"><a href="#" onclick="fermerpopup();"><img src="/images/pickup/btn-non.gif" alt="NON" /></a></div>';
        ch_texte += '</div>';
        ch_texte += '</div>';
        ch_texte += '</div>';
        
        contenu_layer_popup(ch_texte);
    }
}
function setSaveInSessionSC(doSave) {
	
	
   /* DWRContextualizer.getContext("anonymous" ,function(data) {
        var currentShop = data;
        if (currentShop) {
            
	    var shopCode2 = ''+currentShop.shop;
	    while (shopCode2.length < 3)
	    {
	    shopCode2 = '0' + shopCode2;
	    }

            var context = { shop:shopCode2, save:doSave };
            DWRContextualizer.registerContext("anonymous", context, "36000000", function(data) {
                DWRContextualizer.registerContextInSession("anonymous", function(data) {
                    //compute contextualized shopid 
                    var shopid ="0"; 
                    if (currentShop.save=='false') { shopid = currentShop.shop;}
                    removeCart(shopid);
                        ListCourseService.emptyList(function(data) {
                            displayHome();
                        });
                });
            });
        }
    });*/
}
function checkAllRef(origin, shopCode, doSave) {
    if (doSave == "on" || doSave == true) {
        doSave = "true";
    } else {
        doSave = "false";
    }
    var orig = "ci";
    var doSaveTrue = 'true';
    var context = { shop:shopCode, save:doSave };
    var contexttrue = { shop:shopCode, save:doSaveTrue };

    var shopCode2 = ''+shopCode;
    while (shopCode2.length < 3)
    {
      shopCode2 = '0' + shopCode2;
    }
    var contexttrue2 = { shop:shopCode2, save:doSaveTrue };
            
    DWRContextualizer.registerContext(orig, contexttrue2, "36000000", function(data) {
        DWRContextualizer.registerContextInSession(orig, function(data) {
            DWRContextualizer.registerContext(orig, context, "36000000", function(data) {
                //compute contextualized shopid 
                var shopid ="0"; 
                removeCart(shopCode);
                //remove list
                ListCourseService.emptyList(function(data) {
                    document.formmagasinvalide.submit();
                });
            });
        });
    });
    
}
function loadRefContextualisationPopup(origin, contextualizedShop, currentShop, ciShop){
    init_layer_popup();
    if (obj_layer_popup) {	
        cache_popup = false;
        
        if(origin.place == "tableaudebord") {
            //context depuis changement mag de ref
            var ch_texte = '<div id="popup_gen" >';
            ch_texte += '<div class="clearBoth"></div>';
            ch_texte += '<div id="content">';
            ch_texte += '<img src="/images/pickup/ico-nouveau.jpg" alt="NOUVEAU" />';
            ch_texte += '<p class="texte">Choisissez le magasin de ' +contextualizedShop.name+ '<br />';
            ch_texte += 'et <span>consultez le prix et la disponibilit&eacute; des articles</span><br />de ce magasin directement sur le site Leroy Merlin !</p>';
            if(contextualizedShop.pickup == true){
                ch_texte += '<p class="texte">D&eacute;couvrez aussi notre nouveau service exclusif : le <a href="';
                ch_texte += "javascript:fermerpopup();popup('pre?zone=zonestatiqueonly&srvstatique-staticpagename=/html/pickup/popup-retrait-magasin.html','','810','570');";
                ch_texte += '">retrait en magasin</a>.</p>';
            }
            ch_texte += '<form name="context">';
            if(!ciShop || !ciShop.shop){
                ch_texte += '<input onclick="xt_med(\'C\',\'38\',\'pop_up_choix_magasin::prochaines_visites\',\'N\');" type="checkbox" class="checkbox" checked name="register" id="check" />';
                ch_texte += '<label for="check">Je souhaite acc&eacute;der directement au magasin de '+ contextualizedShop.name +'<br />lors de mes prochaines visites.</label>';
                ch_texte += '<div id="submit"><a href="#" onClick="checkAllRef({ place:\''+ origin.place +'\', idlspub:'+origin.idlspub+' }, '+contextualizedShop.id+', dwr.util.getValue(\'register\'));"><img src="/images/pickup/btn-acces-magasin.gif" alt="Acceder au magasin" /></a>';
            }else{
                if(currentShop){
                    ch_texte += '<div id="submit"><a href="#" onClick="checkAllRef({ place:\''+ origin.place +'\', idlspub:'+origin.idlspub+' }, '+contextualizedShop.id+', '+ciShop.save+');"><img src="/images/pickup/btn-acces-magasin.gif" alt="Acceder au magasin" /></a>';
                }else{
                    ch_texte += '<div id="submit"><a href="#" onClick="checkAllRef({ place:\''+ origin.place +'\', idlspub:'+origin.idlspub+' }, '+contextualizedShop.id+', '+ciShop.save+');"><img src="/images/pickup/btn-acces-services.gif" alt="Acceder aux services de ce magasin" /></a>';
                }
            }
            ch_texte += '<div id="submit"><a href="pre?zone=zoneespaceperso&amp;srvname=srvuserregister&amp;useraccounttodo=shop&action=savePseudoForumAction&myshop.shop='+contextualizedShop.id+'&myshop.shop.name='+contextualizedShop.name+'&amp;pageid=23"><img src="/images/pickup/btn-retour-accueil.gif" alt="Retour a l accueil de votre compte" /></a>';
            ch_texte += '</div>';
            ch_texte += '</form>';
            ch_texte += '</div>';
            ch_texte += '</div>';
        }
        
        contenu_layer_popup(ch_texte);
    }
    
    
    
}
function changeShopReference(){
    
    //alert('changeShopReference =>');
    var codemagasincb = "";
    for (counter = 0; counter < document.formmagasinvalide.magasin.length; counter++){
        if (document.formmagasinvalide.magasin[counter].checked)
            codemagasincb = document.formmagasinvalide.magasin[counter].value;
    }   
    //alert('changeShopReference codemagasincb =>'+codemagasincb);
    DWRShopContextualizer.getContextualizedShopByCode(codemagasincb, { callback:function(datashop) {
        var contextualizedShop = datashop;    
        if (contextualizedShop && contextualizedShop.availability){ 
            
            DWRShopContextualizer.getContextualizedShopEIIDByCode(codemagasincb,{ 
                callback:function(datashopeiid) {
                var localeiid = datashopeiid;
                //alert('localeiid=>'+localeiid);
                var origin = { place:"tableaudebord", idlspub:localeiid };    
        
               // DWRContextualizer.getCurrentContext({
                    //callback:function(data) {
                     var data=getCurrentContext();
                    var currentShop = data;
                    //if(currentShop != null){ alert('0 currentShop.shop='+currentShop.shop); }
                    DWRContextualizer.getContext("ci" , { callback:function(data) {
                        var ciShop = data;
                        //alert("ciShop " +ciShop);
                        if (!ciShop || !ciShop.shop) {
                            //alert('changeShopReference Nouveau magasin context CM=>');
                            loadRefContextualisationPopup(origin, contextualizedShop, currentShop, ciShop);
                        }
                        else{
                            //le magasin doit etre different du magasin en cours
                            //alert('changeShopReference Remplacement magasin Context CM=>');
                            loadRefContextualisationPopup(origin, contextualizedShop, currentShop, ciShop);
                        }
                    }});
               // }});
            }});
            
        }else{
            //selection magasin non contextualisable
            //alert('changeShopReference selection magasin non contextualisable');
            document.formmagasinvalide.submit();
        }
    }});
        
}

