/*##############################################################################################
############# JS DES FONCTIONS D APPEL DE CHARGEMENT AJAX DES XML ET DE POST CHARGEMENT ####### 
#################################### POUR LA PARTIE SWITCH ##################################### 
/*############################################################################################## */
var loadedSwitches = false; 
var switchStatusError = false;
/*############################################################################################## */
/* Met le flag switchStatusError a true si probleme lors de la reponse http de chargement des switch */
var POST_LOAD_SWITCH_SERVICE_FUNCTION = function postSwitchService(httpStatus, _joService) {
            // Si la reponse du service switch n est pas correct : pb erep --> mode degrade   
            if(httpStatus != null && httpStatus==200 && _validateSwitchServiceReponse(_serviceSwitchContextId)) {
        	        loadedSwitches = true;		
            }
            else {
                       loadedSwitches = true;
        	        switchStatusError = true;
        	        hideBloc('blocPrixTpsReel');
        	        _urlErep = null; // si _urlErep vaut null, aucun service ne fonctionnera -> mode degrade
            }        
}
/*############################################################################################## */
/* Determine les parametres pour la chargement des switchs en fonction de la presence du cookie : switch site OU switch site et magasin*/
function loadSwitchesConnectOrNot() {
                var numMagCookie = getEltFromUrl(getCookie('decathlon'), 'idMagasin');
                if(numMagCookie!=undefined){	
                        // cas magasin 
                        loadSwitches (_client, SWITCH_KIND_SITE, _siteNumber, _siteNumber, SWITCH_KIND_STORE, numMagCookie, numMagCookie);
                }
                else {
                        // cas centrale 
                        loadSwitches (_client, SWITCH_KIND_SITE, _siteNumber, _siteNumber, null, null, null);
                }
}
/*############################################################################################## */
/* Charge tous les switchs (site OU switch site et magasin) */
function loadSwitches(_client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2) {	
               _urlErep = getUrlErep(_SERVICE_SWITCH, _client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2, POST_LOAD_SWITCH_SERVICE_FUNCTION) ;                
               if(_urlErep != "" && _urlErep!=undefined){             
                    _serviceSwitchContextId = _initAndExecuteSwitchService(_urlErep, getErepLanguageCode( _language), _number1, null, _number2,_kind1,_number1,_subNumber1,_kind2,_number2,_subNumber2,POST_LOAD_SWITCH_SERVICE_FUNCTION); 
                }
}
/*############################################################################################## */
/* Liste des actions a executer en fonction de l etat des switch et du cookie magasin et de la page sur laquelle on se trouve */
function postSwitchAction() {
            if (loadedSwitches == null || ! loadedSwitches) {
                    window.setTimeout("postSwitchAction()", 10);
            } 
            else {
                    // COMMUN A TOUTES PAGES 
                    // Test si connexion valide
                     cookieMagasin = isConnexionEnable();
                     
                   // COMMUN A TOUTES PAGES SAUF MAGASIN - GDBC, VENTE COMPLEMENTAIRE, EBOOKING-STEPS ET STOCKS MULTI-MAGS CAR IFRAME
                   if (typePage!='magasin' && typePage!='famille-gdbc' && typePage!='modele-vc' && typePage!='ebooking-steps' && typePage!='psd' && typePage!='oxylane-comments') {
                           if(switchStatusError){
                                // Affichage de la popup d'infos
                                var iframe = createElement_iframe("popup-hp-0", "", "", "", "", null, document.body);
                                iframe.style.display = 'none';
                                openNewWindowScrollEffect(0,window.parent.urlPopupBugErep,'popup-bug-service',null);
                            }
                            // Header                        
                           showBlocIfCookieHeader(cookieMagasin);
                           // Chargement des parametres du header univers
                           chargementUnivers();
                           // Création de la liste des marques dans le mega-bouton
                           importNaviMbBrands();
                           // Moteur de recherche                           
                            if (!getSwitchSite('ACTIVATION_MDR')) {                                 
                                hideBloc('bandeaurecherche');
                            }
                             showBloc('lien_dkttv');
                             // Lien changement de langue a cote des guides conseils
                            showBloc('affichlienlangue');      
                            // Skin de Noel
                            changeClassIfXmas();
                            // Skin pour image d ambiance
                            changeClassIfOpeco();
                            // Soldes
                            changeClassIfSold(cookieMagasin);
                            // Affichage popup "comment ca marche"
                            if(window.location.search == "?popupInfo"){
                                show_filter('filter');show_popup('popup-commentcamarche');
                            }
                            // Ajout d un gestionnaire sur l evenement onclick du doucment
                            window.document.onclick = documentOnclickEventHandler;
                            // Modification du lien contact-us dans le bottom-link selon magasin
                            /*if(document.getElementById('contactLink') && cookieMagasin)
                                getStoreInfos(getEltFromUrl(cookieMagasin, "urlMag"),POST_LOAD_CONTACT_STOREINFOS_FUNCTION);*/

                            // Gestion du mega-bouton shop
                            megaBoutonShopHandler(cookieMagasin);
                    }
                   
                   // SPECIFIQUE PAGE                      
                   // Homepage
                    if (typePage=='homepage') {
                        showBlocIfCookieHome(cookieMagasin,rootPath);
                        // Creation du panneau lateral de navigation
                        importNaviPanel();
                        // TG Homepage   
                        if (PSI_QMODELS_ARRAY.length>0) {
                           loadModelsConnectOrNot(cookieMagasin);       
                        }
                    }
                    // Rayon classic
                    if (typePage=='rayon') {                            
                        showBlocIfCookieRayon(cookieMagasin);   
                         // TG Rayon                            
                         if (typeof PSI_QMODELS_ARRAY != 'undefined' && PSI_QMODELS_ARRAY.length>0) {
                            loadModelsConnectOrNot(cookieMagasin);   
                         }
                         else{
                             if (tabTypeBoutique.length > 0 && getSwitchSite('THEMATIC_SHOPS')) loadCardinaliteBoutiqueConnectOrNot(cookieMagasin);
                             else if(!conseilOuVideo) hideBloc('zoneOnglets'); // Pas de TG ni de MB, si pas de fiche conseil ou video, on cache la zone TG
                         }
                    }
                      // Famille classic
                    if (typePage=='famille') {
                        // Produit hero Famille
                        if (PSI_QMODELS_ARRAY.length>0) {
                            typePage='famille-hero';
                            loadModelsConnectOrNot(cookieMagasin);
                        }
                        // Depth famille
                        else loadFamilyConnectOrNot(cookieMagasin);
                        showBlocIfCookieFamilleClassic(cookieMagasin);
                    }    
                    // Famille gdbc
                     if (typePage=='famille-gdbc') {
                          loadFamilyConnectOrNot(cookieMagasin);   
                          showBlocIfCookieFamilleComparatif();
                     }
                    // Modele classic
                     if (typePage=='modele') {           
                        try {
                             // Oxylane - Avis
                             if (getSwitchSite('AVIS')) loadAvis(getSwitchSite('AVIS'));
                        }
                        catch (err) {}                         
                        // Prix stock modele
                        loadModelConnectOrNot(cookieMagasin, _language);
                        // Lien .com
                        if (getSwitchSite('KOODZA')) {
                            if (boolKoodza=='true' || urlBuyOnline != '') {
                                showBloc('lienKoodza');
                                if (urlBuyOnline != '') showBloc("infoLegalesBuyOnline"); 
                                else showBloc("infoLegalesKoodzaDispo");
                            }
                            else showBloc("infoLegalesKoodzaIndispo");
                        }                         
                        //Gestion SlideShow
                        if (cookieMagasin !=null) {	
		myIdMagasin = getEltFromUrl(cookieMagasin, 'idMagasin');
		loadFamily(_client, _siteNumber, _centralNumber, myIdMagasin, _familyId);
	         }
	         else loadFamily(_client, _siteNumber, _centralNumber, null, _familyId);
                    }                                
                    // Modele vente complementaire                         
                     if (typePage=='modele-vc') {                                 
                         if (PSI_QMODELS_ARRAY.length>0) {
                            loadModelsConnectOrNot(cookieMagasin);   
                         }
                      }                         
                    // HP Marque
                     if (typePage=='HPMarque') {  
                         // TG HP Marque    
                         if (typeof PSI_QMODELS_ARRAY != "undefined" && PSI_QMODELS_ARRAY.length>0) {
                            loadModelsConnectOrNot(cookieMagasin);   
                         }
                    }   
                     // Modele magasin
                     if (typePage=='magasin') {               
                         loadThirdParty(_client, THIRD_KIND_STORE, idMagasinEm, idMagasinEm);	
                    }
                    // Mini boutique
                      if (typePage=='miniBoutique') {                           
                          loadBoutiqueConnectOrNot(cookieMagasin);   
                      }
                    // Regions
                    if (typePage=='region') {
                        // TMP DEBUT - ouverture popup region NORD
                        if (typeof regionIdCategorie != 'undefined') {
                            if (_siteNumber == '55813' && regionIdCategorie == '10193106') openNewWindow('/FR/popup-mail-client-vendeur-98326657/index.html', 'mail-client-vendeur', 'width=550,height=400, ,menubar=no,toolbar=no, scrollbars=no, resizable=no', false);
                        }
                        // TMP FIN - ouverture popup ouverture nouveau magasin
                    }
                    // iFrame ebooking-steps
                    if (typePage=='ebooking-steps') {

                        if (cookieMagasin!=null) {
                            idMagasin = getEltFromUrl(cookieMagasin, 'idMagasin');
                            // Affichage bouton resa dans la div d explication du service eresa    
                            if(getSwitchSite('BOOKING') && getSwitchMagasin('FLAG_BOOKABLE')) {
                                showBloc('boutonStartResa'); 
                            }
                        }
                        else {
                            // Affichage bouton connection magasin dans la div d explication du service eresa            
                            if (getSwitchSite('LOCAL_STOCK')) {
                                showBloc('boutonConnectStore');
                            }
                        }
                        // Affichage des blocs adequats en fonction du parametre passe dans l url                     
                        var bmask = window.location.search;
                        if (bmask != "") {
                            var bmaskIndex = bmask.indexOf("=");
                            if (bmaskIndex != 0) {
                                bmask = bmask.substring(bmaskIndex + 1);

                                if (bmask == '1') {
                                    // Si on passe dans l url la parametre bmask = 1, on masque certains blocs de la popup
                                    hideBloc('presentationSteps');
                                    hideBloc('descriptionSteps');
                                    showBloc('generalConditions');
                                }
                            }
                        }
                    }                    

                    // eBooking
                    if (typePage=='eBooking') {
                        // Remplit le bloc aide eresa         
                        extractEbookingSteps(urlEbookingSteps, 'steps-ebooking-help', POST_EXTRACT_EBOOKING_STEPS_LOCAL_PRICE_FUNCTION);
                        // Passage de parametre a la popup de connexion magasin
                        urlConnexionMagasin = urlConnexionMagasin + '?from=eBooking';
                        // On masque le choix des langues
                        hideBloc('affichlienlangue');
                    }

                     // Modele magasin
                     if (typePage=='psd' && window.parent.srcDone) {
                         initDisplayPsd();
                         if(listeMagProximity.length == 0) loadStoreList(getEltFromUrl(cookieMagasin, 'idMagasin'));
                         else loadProductStoresDisponibility(_siteNumber, _language,_centralNumber, listeArticles.value, listeMagProximity);
                    }

                   // Boutique (shop)
                    if (typePage=='shop') {
                        loadShopConnectOrNot(cookieMagasin);
                    }

                   // Avis Oxylane
                    if (typePage=='oxylane-comments') {
                        loadAvisOxylane();
                    }

            }           
}
/*############################################################################################## */
/* Retourne l etat du switch (false si probleme lors de la reponse http de chargement des switch)  */
function getSwitchSite(switchName) {                
	if(switchStatusError) {
	        return false;
	}
	else {
                    var oService = getService(_serviceSwitchContextId); 
                    if (oService ==null) {return null;} 
                    var oContent = oService.getContent(); 
                    if (oContent ==null) {return null;}
                    try {
                        return oContent.getSwitch(switchName, SWITCH_KIND_SITE, _siteNumber, _siteNumber, null).isEnabled();
                    }
                    catch (e) {
                        return false;
                    }
             }
}
/*############################################################################################## */
/* Retourne l etat du switch du magasin du cookie (false si probleme lors de la reponse http de chargement des switch)  */
function getSwitchMagasin(switchName) {                
	if(switchStatusError) {
	        return false;
	}
	else {	        
                    var oService = getService(_serviceSwitchContextId); 
                    if (oService ==null) {return null;} 
                    var oContent = oService.getContent(); 
                    if (oContent ==null) {return null;}
                    try {
                        return oContent.getSwitch(switchName, SWITCH_KIND_STORE, idMagasin, idMagasin, null).isEnabled();
                    }
                    catch (e) {
                        return false;
                    }
             }
}

/*############################################################################################## */
/* Execute le service switch et retourne l ID du contexte*/
function _initAndExecuteSwitchService(_theRoot, _theLanguage, _theSiteId, _theCentralId, _theStoreId,_theThirdKind, _theThirdNumber, _theThirdSubNumber,_theThirdKind2, _theThirdNumber2, _theThirdSubNumber2,_theLocalFunc) {

    var oContext = new JoServiceContext(_theRoot,getErepLanguageCode( _language), _theSiteId, _theCentralId, _theStoreId);
    oContext.setSwitches(_theThirdKind,_theThirdNumber,_theThirdSubNumber,_theThirdKind2,_theThirdNumber2,_theThirdSubNumber2);
    var oService = new JoService(oContext);
    oService.execute(_theLocalFunc);
    
    return oService.getId();
}
/*############################################################################################## */
/* valide la reponse du service switch (erreur - vide ou autres)*/
function _validateSwitchServiceReponse(_serviceId) {
    try {
       var oService = getService(_serviceId) ;
       var oContent = oService.getContent();
       if (oContent != null) {
           var switchesList = oContent.getSwitchesList() ;
            if (switchesList != null) {
               if (switchesList.length > 0) return true;
            }
        }
    
        return false
    }
    catch (e) {
        return false;
    }
}

/*############################################################################################## */
/* Liste des actions a executer en fonction de l etat des switch */
function postSwitchActionDktTv() {
            if (loadedSwitches == null || ! loadedSwitches) {
                    window.setTimeout("postSwitchActionDktTv()", 10);
            }
            else {
               if(switchStatusError){
                    // Affichage de la popup d'infos
                    var iframe = createElement_iframe("popup-hp-0", "", "", "", "", null, document.body);
                    iframe.style.display = 'none';
                    openNewWindowScrollEffect(0,window.parent.urlPopupBugErep,'popup-bug-service',null);
                }
                // Test si connexion valide
                cookieMagasin = isConnexionEnable();
                /* Recuperation du prix erep du produit */
                loadModelDktTvConnectOrNot(cookieMagasin,_language);
                /* Chargement des produits affiches sous le player */
                loadModelsDktTv(_client, _siteNumber, _centralNumber, null);
            }
}