/* Parametres globaux */
var isAllSwitchError = false ;

var SWITCH_ACTIVATION_PRIX = "ACTIVATION_PRIX_CENTRAL" ;
var SWITCH_ACTIVATION_DEEE = "DEEE" ;
var SWITCH_ACTIVATION_LIAGE_DKT = "ACTIVATION_LIAGE_DKT" ;

var SWITCH_ACTIVATION_DOC_ASSOCIES = "DOCUMENTS_ASSOCIES" ;

var SWITCH_ACTIVATION_MDR = "ACTIVATION_MDR" ;
var SWITCH_ACTIVATION_MDR_SAISIE_PREDICTIVE = "SAISIE_PREDICTIVE" ;

var SWITCH_ACTIVATION_OXYLANE_AVIS = "AVIS" ;

var isErrorErepSwtich = false ;

var isDeactivationOxylaneAvis = false ;

var isDeactivationPrixMag = false ;

/**
 * Permet de recuperer le switch et de faire le traitement lie a ce switch
 * @param :
 *    - httpStatus : le statut HTTP apres l'appel ajax
 * @desc :
 *    - pour chaque switch, on va recuperer l'objet et faire le traitement d'affichage ou d'activation lie a ce switch
*/
var POST_LOAD_SWITCH_FUNCTION = function postSwitchService(httpStatus){
    if(httpStatus == "200"){
        //L'id du service
        var currentServiceId = mapServiceId.get(_SERVICE_SWITCH) ;
        
        //on va verifier qu'erep a bien repondu en testant la reponse retourne qui est la liste des switchs
        var switches = getSwitches(currentServiceId) ;
        if(switches == null || switches.length == 0){
            isErrorErepSwtich = true ;
        }
        
        //Si pas d'erreur erep concernant la reponse
        if(!isErrorErepSwtich){
            var isMultiSwitchesChecked = false ;
            var isFromModelPage = false ;
            if(thisModelList != ""){
                isMultiSwitchesChecked = true ;
                
                if(thisModelList.length() == 1){
                    isFromModelPage = true ;
                }            
            }
            var isFromOtherCounrty = false ;
            var valueCookieCountry = getCookie("country") ;
            if(valueCookieCountry){
                if(valueCookieCountry != "FR" && valueCookieCountry != "EN" && valueCookieCountry != "ES" && valueCookieCountry != "IT"){
                    isFromOtherCounrty = true ;
                }
            }
            for(var i=0 ; i<switchNameOrArray.length ; i++){
                var switchName = switchNameOrArray[i] ;
                
                //1- Traitement des switchs prix
                if(switchName == SWITCH_ACTIVATION_PRIX || switchName == SWITCH_ACTIVATION_DEEE ||  switchName == SWITCH_ACTIVATION_LIAGE_DKT){            
                    //traitement de plusieurs blocs
                    if(isMultiSwitchesChecked){
                        for(var k=0 ; k<thisModelList.length() ; k++){
                            var model = thisModelList.get(k) ;
                            
                            if(model){
                                if(switchName == SWITCH_ACTIVATION_PRIX){
                                    checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, null, "bloc-decathlon"+model, true, isFromOtherCounrty) ;
                                }
                                if(switchName == SWITCH_ACTIVATION_DEEE){
                                    checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, _language, "zone-ecotaxe-decath"+model, false, isFromOtherCounrty) ;      
                                }
                                if(switchName == SWITCH_ACTIVATION_LIAGE_DKT){
                                    checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, _language, "dispo_txt", false, isFromOtherCounrty) ;
                                }
                            }
                        }
                    }
                    else{//traitement d'un bloc
                        if(switchName == SWITCH_ACTIVATION_PRIX){
                            checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, null, "bloc-decathlon", true, isFromOtherCounrty) ;                
                        }
                        if(switchName == SWITCH_ACTIVATION_DEEE){
                            checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, number, subNumber, _language, "zone-ecotaxe-decath"+model, false, isFromOtherCounrty) ;                    
                        }
                        if(switchName == SWITCH_ACTIVATION_LIAGE_DKT){
                            checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, _language, "dispo_txt", false, isFromOtherCounrty) ;      
                        }
                    }
                }
                
                //2- Traitement des autres switchs
                if(switchName == SWITCH_ACTIVATION_MDR){
                    checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, null, "header-mdr", false, isFromOtherCounrty) ;
                }
                 if(switchName == SWITCH_ACTIVATION_MDR_SAISIE_PREDICTIVE){
                    checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, null, "mdr-text", false, isFromOtherCounrty) ;
	 }
                if(switchName == SWITCH_ACTIVATION_DOC_ASSOCIES){
                    checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, null, "supportClient", false, isFromOtherCounrty) ;
                }
                
                //3 - Traitement des switchs Oxylane
                //Si le client a choisit un pays autre que ceux definit pour le site (FR, EN, ES et IT), on ne va pas activer les switchs Oxylane
                //Pour cela, on va tester l'existence du cookie
                if(!isFromOtherCounrty){
                    if(switchName == SWITCH_ACTIVATION_OXYLANE_AVIS){
                        checkSwitch(currentServiceId, switchName, THIRD_KIND_SITE, idSite, _language, "oxylaneClub", false, isFromOtherCounrty) ;
                    }
                }
                else{
                    isDeactivationOxylaneAvis = true ;
                }
            }
            
            //on verifie si tous les switchs oxylane ont ete desactive pour desactive le bloc oxylane club de la fiche produit
            if(isDeactivationOxylaneAvis){
                var divOxyClubElement = getElement("oxylaneClub", true, "") ;
                if(divOxyClubElement){            
                    hideBloc(divOxyClubElement) ;
                }
            }
            
            //on verifie si tous les switchs prix ont ete desactive pour desactive le bloc prix de la fiche produit
            if(isDeactivationPrixMag){
                var divOuAcheterElement = getElement("ouAcheter", true, "") ;
                if(divOuAcheterElement){            
                    hideBloc(divOuAcheterElement) ;
                }
            }
        }
        else{//Sinon, on desactive tous les principaux switchs
            isAllSwitchError = true ;    
            setSwitchesErrorStatus(switchNameOrArray) ;
        }
    }
    else{
        isAllSwitchError = true ;    
        setSwitchesErrorStatus(switchNameOrArray) ;
    }
    
    //en cas d'erreur sur le switch prix decath, on va cacher le bloc vente
    if(isAllSwitchError){
        if(parent.document.getElementById("bloc-vente")){
            parent.document.getElementById("bloc-vente").style.display = "none" ;
        }
    }
}

/**
 * Permet de desactiver toutes les zones associees a tous les switches en cas d'erreur, par ex: http status = 404
 * @param :
 *    - switchNameArray : le tableau contenant tous les switches a traiter
 * @desc :
 *    - va servir a desactiver toutes les zones associees aux switches, cad DEEE, PRIX Decathlon, LIAGE
*/
function setSwitchesErrorStatus(switchNameArray){
     for(var i=0 ; i<switchNameArray.length ; i++){
        var switchName = switchNameArray[i] ;
        setSwitchErrorStatus(switchName) ;
    }
}

/**
 * Permet de desactiver les zones associees au switch en cas d'erreur
 * @param :
 *    - switchName : le nom du switch
 * @desc :
 *    - va servir a desactiver les zones DEEE, PRIX Decathlon, LIAGE si un pb est detecte sur le switch associe
*/
function setSwitchErrorStatus(switchName, divObjName){
    if(divObjName){
        disableElementSwitch(divObjName) ;
    }
    else{
        if(switchName == SWITCH_ACTIVATION_PRIX){
            disableElementSwitch("bloc-decathlon") ;
        }
        if(switchName == SWITCH_ACTIVATION_DEEE){
            disableElementSwitch("zone-ecotaxe-decath") ;
        }
        if(switchName == SWITCH_ACTIVATION_MDR){
            disableElementSwitch("header-mdr") ;
        }
        if(switchName == SWITCH_ACTIVATION_DOC_ASSOCIES){
            disableElementSwitch("supportClient") ;
        }
        if(switchName == SWITCH_ACTIVATION_OXYLANE_AVIS){
            isDeactivationOxylaneAvis = true ;
            disableElementSwitch("oxylaneClub") ;
        }    
        if(switchName == SWITCH_ACTIVATION_MDR_SAISIE_PREDICTIVE){
            if(getElement('mdr-text')){
                getElement('mdr-text').onKeyUp = "" ;
            }
        }
    }
}


/**
 * Permet de traiter les elements associer au switchs
 * @param :
 *    - serviceId : L'identifiant du service
 *    - switchName : le nom du switch
 *    - kind : le type de tiers a traiter : THIRD_KIND_SITE
 *    - idSite : l'identifiant du site
 *    - language : la langue du switch (pe vide)
 *    - divObjName : l'identifiant du bloc associe au switch
 *    - checkErrorStatus : true/false : permet de gerer ou non les erreurs
*/
function checkSwitch(serviceId, switchName, kind, idSite, language, divObjName, checkErrorStatus, isFromOtherCounrty) {
    var divElement = getElement(divObjName, true, "") ;
    
    if(divElement){
        //Recuperation de l'objet JoSwitch
        var switchObj = getMySwitch(serviceId, switchName, kind, idSite, language) ;
        
        if(switchObj){
            var isEnable = switchObj.isEnabled() ;
                        
            if(checkErrorStatus){
                isAllSwitchError = false ;
            }
            
            if(!isEnable){
                if(switchName == SWITCH_ACTIVATION_PRIX){
                    isDeactivationPrixMag = true ;
                }
                if(switchName == SWITCH_ACTIVATION_OXYLANE_AVIS){
                    isDeactivationOxylaneAvis = true ;
                }
                divElement.style.display = "none" ;

                //Traitement special pour le liage, on cache le bloc "Voir la dispo" et on remplace le lien pas du texte
                if(switchName == SWITCH_ACTIVATION_LIAGE_DKT){
                    disableDktLink() ;
                }
            }
            else{
                if(switchName == SWITCH_ACTIVATION_MDR_SAISIE_PREDICTIVE){
                    //si le switch est actif on autorise l'appele  la fonction de la recherche predictive
                    divElement.onkeyup = function() {
                        searchSuggest(document.getElementById('searchForm' )); 
                    }
                }
                else if(switchName == SWITCH_ACTIVATION_OXYLANE_AVIS){
                    //on va activer l'affichage des avis oxylane
                    loadAvis(isEnable);

                    if (isEnable) {
                        if (contientParam('afficheavis', '1')) {
                            document.getElementById('onglet-avis').onclick();
                            window.location.href = "#ongletAfficheOxylaneAvis";
                        }
                    }
                }                
                else if(switchName == SWITCH_ACTIVATION_LIAGE_DKT){
                    //on cache le texte du prix
                    hideBlocId("dispo_txt") ;
                }
                else { //Pour tous les autres switchs
                    divElement.style.display = "block" ;
                }
            }
        }
        else{
            if(checkErrorStatus){
                isAllSwitchError = true ;
            }
            setSwitchErrorStatus(switchName, divObjName) ;
        }
    }
    else{        
        if(checkErrorStatus){
            isAllSwitchError = true ;
        }
        setSwitchErrorStatus(switchName) ;
    }
}


/**
 * Permet de cacher les blocs correspondant a un switch
 * @param :
 *    - divElement : l'id de l'element a cacher
*/
function disableElementSwitch(divElement){
    var divElementObj = getElement(divElement, true, "") ;
    if(divElementObj){
        hideBloc(divElementObj) ;
    }
}


/**
 * Permet de desactiver le liage vers Decath
*/
function disableDktLink(){
    var idAElt = "dispo" ;
    var idTxtElt = "dispo_txt" ;
    var idBtnElt = "dispo_btn" ;
    
    var divElementBtnObj = getElement(idBtnElt) ;
    if(divElementBtnObj){        
        //On cache le bouton "Voir la dispo"
        hideBloc(divElementBtnObj) ;
    }
    
    //on cache le lien
    hideBlocId(idAElt) ;
    
    //on affiche le texte qui remplace le lien
    displayBlocId(idTxtElt) ;
}


/**
 * Permet de verifier les switches utiliser pour la vente (prix decath)
 * @param :
 *    - _switchNameArray : le tableau contenant les switches a checker
 *    - _client : ITOOL
 *    - _siteNumber : l'identifiant du site
 *    - _theModelList : la liste des modeles
 * @desc :
 *    - appelle la methode loadSwitches
*/
function checkSwitches(_switchNameArray, _client, _siteNumber, _theModelList){
    loadSwitches(_switchNameArray, _client, _siteNumber, POST_LOAD_SWITCH_FUNCTION, _theModelList) ;
}


/**
 * Permet de charger tous les switchs et de lancer le tratement post appel Ajax
 * @param :
 *    - _switchNameArray : le tableau contenant les switches a checker
 *    - _client : ITOOL
 *    - _idSite : l'identifiant du site
 *    - _localfunc : la fontion post ajax a appeler : par ex, POST_LOAD_SWITCH_VENTE_FUNCTION
 *    - _modelList : la liste des codes modèle
 * @desc :
 *    - recupere le serveur erep, initialise ce serveur et charge tous les switches
*/
function loadSwitches(_switchNameOrArray, _client, _siteNumber, _localfunc, _theModelList) {
    switchNameOrArray = _switchNameOrArray ; //va servir a la fonction post ajax
    idSite = _siteNumber ; //va servir a la fonction post ajax
    thisModelList = _theModelList //va servir a la fonction post ajax

    _urlErep = getUrlErep(_SERVICE_SWITCH, _client, THIRD_KIND_SITE, _siteNumber, _siteNumber, _localfunc) ;
    
    if(_urlErep != "" && typeof _urlErep != "undefined"){
        var isActivePriceFilter = false ;
        var isActiveAssFilter = false ;
        
        serviceId = initExecuteService(_urlErep, _language, _siteNumber, _centralNumber, CONTEXT_MODE_SWITCH, "", _localfunc, isActivePriceFilter, isActiveAssFilter) ;
        
        mapServiceId.put(_SERVICE_SWITCH, serviceId) ;
    }
    else{
        setSwitchesErrorStatus(_switchNameOrArray) ;
    }
}