/* Paramtres globaux */
var isAllSwitchError = false ;

var SWITCH_ACTIVATION_DEEE = "DEEE" ;
var SWITCH_ACTIVATION_DEEE_KOODZA = "DEEE_KOODZA" ;
var SWITCH_ACTIVATION_MDR = "ACTIVATION_MDR" ;
var SWITCH_ACTIVATION_MDR_SAISIE_PREDICTIVE = "SAISIE_PREDICTIVE" ;
var SWITCH_ACTIVATION_DOCUMENTS_ASSOCIES = "DOCUMENTS_ASSOCIES" ;
var SWITCH_ACTIVATION_VOCAL = "ACTIVATION_VOCAL" ;
var SWITCH_ACTIVATION_KOODZA = "KOODZA" ;
var SWITCH_ACTIVATION_PRIX = "ACTIVATION_PRIX_CENTRAL" ;
var SWITCH_ACTIVATION_ARBO_MULTI = "ARBO_MULTICRITERES" ;
var SWITCH_ACTIVATION_OXYLANE_PROFIL = "OXYLANE" ;
var SWITCH_ACTIVATION_OXYLANE_AVIS = "AVIS" ;
var SWITCH_ACTIVATION_OXYLANE_PLUS_BT = "OFFRES" ;
var SWITCH_ACTIVATION_OXYLANE_PLUS_ONGLET = "OFFRES" ;
var SWITCH_ACTIVATION_LIAGE_DKT ="ACTIVATION_LIAGE_DKT";

var isErrorErepSwtich = false ;

var serviceIdSwitch = "" ;
/**
 * Permet de rcuprer le switch et de faire le traitement li  ce switch
 * @param :
 *    - httpStatus : le statut HTTP aprs l'appel ajax
 * @desc :
 *    - pour chaque switch, on va rcuprer l'objet et faire le traitement d'affichage ou d'activation li  ce switch
*/
var POST_LOAD_SWITCH_FUNCTION = function postSwitchService(httpStatus){
    if(httpStatus == "200"){
        var listeSwitch=getMySwitches(serviceIdSwitch)
        if(listeSwitch == null || listeSwitch.length == 0){
            isErrorErepSwtich = true ;
        }        
       
       /* if(SWITCH_ARRAY == null || SWITCH_ARRAY.length == 0){
            isErrorErepSwtich = true ;
        }*/

        if(!isErrorErepSwtich){
            var isMultiSwitchesChecked = false ;
            var isFromModelPage = false ;
            if(thisModelList != ""){
                isMultiSwitchesChecked = true ;
                
                if(thisModelList.length() == 1){
                    isFromModelPage = true ;
                }
            }
            
            for(var i=0 ; i<switchNameOrArray.length ; i++){
                var switchName = switchNameOrArray[i] ;
                
                //1- Traitement des switchs prix
                 //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(switchName, kind, number, subNumber, null, "bloc-decathlon"+model, true) ;               
                                
                                 if(switchName == SWITCH_ACTIVATION_DEEE)
                                 checkSwitch(switchName, kind, number, subNumber, _language, "zone-ecotaxe-decath"+model, false) ;                    
                                
                                 if(switchName == SWITCH_ACTIVATION_DEEE_KOODZA)
                                checkSwitch(switchName, kind, number, subNumber, _language, "zone-ecotaxe-koodza"+model, false) ;
                                
                                if(switchName == SWITCH_ACTIVATION_LIAGE_DKT)
                                checkSwitchLiage(switchName, kind, number, subNumber, _language, "bloc-decathlon"+model, false,model) ;
                                           
                                 if(switchName == SWITCH_ACTIVATION_KOODZA)
                                checkSwitch(switchName, kind, number, subNumber, _language, "bloc-koodza"+model, true) ;                
                                
                                if(switchName == SWITCH_ACTIVATION_OXYLANE_AVIS){
                                    checkSwitch(switchName, kind, number, subNumber, _language,"mdr-avis"+model, isFromModelPage) ;
                                    checkSwitch(switchName, kind, number, subNumber, _language,"oxylaneAvisPleinPage", isFromModelPage) ;        
                                }
                            }
                        }
                    }
                    else{//traitement d'un bloc
                         if(switchName == SWITCH_ACTIVATION_PRIX)
                        checkSwitch(switchName, kind, number, subNumber, null, "bloc-decathlon", true) ;                
                        
                         if(switchName == SWITCH_ACTIVATION_DEEE)
                        checkSwitch(switchName, kind, number, subNumber, _language, "zone-ecotaxe-decath", false) ;                    
                        
                         if(switchName == SWITCH_ACTIVATION_DEEE_KOODZA)
                        checkSwitch(switchName, kind, number, subNumber, _language, "zone-ecotaxe-koodza", false) ;
                                    
                         if(switchName == SWITCH_ACTIVATION_KOODZA)
                        checkSwitch(switchName, kind, number, subNumber, _language, "bloc-koodza", true) ;                
                        
                    }
                
                //2- Traitement des autres switchs
                 if(switchName == SWITCH_ACTIVATION_MDR)    
                checkSwitch(switchName, kind, number, subNumber, null, "searchForm", false) ;                    
                
                  if(switchName == SWITCH_ACTIVATION_MDR_SAISIE_PREDICTIVE)       
                 checkSwitchRecherchePredictive(switchName, kind, number, subNumber, null, "mdrech_form", false) ;                    
                
                 if(switchName == SWITCH_ACTIVATION_DOCUMENTS_ASSOCIES)      
                checkSwitch(switchName, kind, number, subNumber, null, "doc-associes", false) ;                    
                
                
                 if(switchName == SWITCH_ACTIVATION_ARBO_MULTI)      
                checkSwitch(switchName, kind, number, subNumber, null, "encartgaucheint-arbo", false) ;            
                    
                 if(switchName == SWITCH_ACTIVATION_OXYLANE_PROFIL)
                checkSwitchOxylane(switchName, kind, number, subNumber, _language, isFromModelPage) ;
               
                 if(switchName == SWITCH_ACTIVATION_OXYLANE_AVIS)
                checkSwitchOxylane(switchName, kind, number, subNumber, _language, isFromModelPage) ;
                
                 if(switchName == SWITCH_ACTIVATION_OXYLANE_PLUS_ONGLET)
                checkSwitchOxylane(switchName, kind, number, subNumber, _language, isFromModelPage) ;      
            }
        }
        else{
            isAllSwitchError = true ;    
            setSwitchesErrorStatus(switchNameOrArray) ;
        }
    }
    else{
        isAllSwitchError = true ;    
        setSwitchesErrorStatus(switchNameOrArray) ;
    }
    
    //en cas d'erreur sur le switch prix decath et prix koodza, on va cacher le bloc vente
    if(isAllSwitchError){
        if(parent.document.getElementById("bloc-vente")){
            parent.document.getElementById("bloc-vente").style.display = "none" ;
        }
    }
}

/**
 * Permet de dsactiver toutes les zones associes  tous les switches en cas d'erreur, par ex: http status = 404
 * @param :
 *    - switchNameArray : le tableau contenant tous les switches  traiter
 * @desc :
 *    - va servir  dsactiver toutes les zones associes aux switches, cd DEEE, DEEE koodza, PRIX Decathlon, PRIX Koodza, LIAGE
*/
function setSwitchesErrorStatus(switchNameArray){
     for(var i=0 ; i<switchNameArray.length ; i++){
        var switchName = switchNameArray[i] ;
        setSwitchErrorStatus(switchName) ;
    }
}

/**
 * Permet de dsactiver les zones associes au switch en cas d'erreur
 * @param :
 *    - switchName : le nom du switch
 * @desc :
 *    - va servir  dsactiver les zones DEEE, DEEE koodza, PRIX Decathlon, PRIX Koodza, LIAGE si un pb est dtect sur le switch associ
*/
function setSwitchErrorStatus(switchName){
    if(switchName == SWITCH_ACTIVATION_PRIX){
        disableElementSwitch("bloc-decathlon") ;
    }
    if(switchName == SWITCH_ACTIVATION_DEEE){
        disableElementSwitch("zone-ecotaxe-decath") ;
    }
    if(switchName == SWITCH_ACTIVATION_DEEE_KOODZA){
        disableElementSwitch("zone-ecotaxe-koodza") ;
    }
    if(switchName == SWITCH_ACTIVATION_KOODZA){
        disableElementSwitch("bloc-koodza") ;
    }
    if(switchName == SWITCH_ACTIVATION_VOCAL){
        disableElementSwitch("bloc-vocalisation") ;
    }
    if(switchName == SWITCH_ACTIVATION_MDR){
        disableElementSwitch("searchForm") ;
    }
    if(switchName == SWITCH_ACTIVATION_ARBO_MULTI){
        disableElementSwitch("encartgaucheint-arbo") ;
    }
    if(switchName == SWITCH_ACTIVATION_DOCUMENTS_ASSOCIES){
        disableElementSwitch("doc-associes") ;
    }
    if(switchName == SWITCH_ACTIVATION_ARBO_MULTI){
        disableElementSwitch("encartgaucheint-arbo") ;
    }
}

/**
 * Permet de traiter les lements associer au switchs
 * @param :
 *    - switchName : le nom du switch
 *    - kind : le type de tiers  traiter : THIRD_KIND_SITE
 *    - number : l'identifiant du site
 *    - subNumber : l'identifiant du site
 *    - language : la langue du switch (p vide)
 *    - divObjName : l'identifiant du bloc associ au switch
 *    - checkErrorStatus : true/false : permet de grer ou non les erreurs
*/
function checkSwitch(switchName, kind, number, subNumber, language, divObjName, checkErrorStatus){    
    var divElement = document.getElementById(divObjName) ;

    if(divElement){
        var oSwitch = getMySwitch(serviceIdSwitch,switchName,kind, number, subNumber,language);
        var activationSwitch = false;
       if(oSwitch) activationSwitch = oSwitch.isEnabled() ;

        if(checkErrorStatus){
            isAllSwitchError = false ;
        }
        if(!activationSwitch){
            divElement.style.display = "none" ;
        }
        else{
            divElement.style.display = "block" ;
        }
    }
    else{
        if(checkErrorStatus){
            isAllSwitchError = true ;
        }
        setSwitchErrorStatus(switchName) ;
    }
}

/**
 * Permet de traiter les elements associer au switchs Oxylane
 * @param :
 *    - switchName : le nom du switch
 *    - kind : le type de tiers a traiter : THIRD_KIND_SITE
 *    - number : l'identifiant du site
 *    - subNumber : l'identifiant du site
 *    - language : la langue du switch (pe vide)
*/
function checkSwitchOxylane(switchName, kind, number, subNumber, language, isFromModelPage){
    var oSwitch = getMySwitch(serviceIdSwitch,switchName,kind, number, subNumber,language);
      var activationSwitch = false;
      if(oSwitch) activationSwitch = oSwitch.isEnabled() ;
      
    if(activationSwitch){
        if(switchName == SWITCH_ACTIVATION_OXYLANE_PROFIL){
            if(isFromModelPage){
                loadProfil(activationSwitch) ;
            }
        }
        else if(switchName == SWITCH_ACTIVATION_OXYLANE_AVIS){
            if(isFromModelPage){
                loadAvis(activationSwitch);
            }
        }        
        else if(switchName == SWITCH_ACTIVATION_OXYLANE_PLUS_ONGLET){
            if(isFromModelPage){
                loadOxylaneServices(activationSwitch);
            }
            /*else{
                loadBoutonOxylane(activationSwitch);
            }*/
        }
    }
}

/**
 * Permet de cacher les blocs correspondant  un switch
 * @param :
 *    - divElement : l'id de l'lment  cacher
*/
function disableElementSwitch(divElement){
    var divElementObj = document.getElementById(divElement) ;
    if(divElementObj){
        divElementObj.style.display = "none" ;
    }
}

/**
 * Permet de vrifier les switches utiliser pour la vente (prix decath+prix koodza)
 * @param :
 *    - _switchNameArray : le tableau contenant les switches  checker
 *    - _client : ITOOL
 *    - _kind1 : le type de tiers  traiter : THIRD_KIND_SITE
 *    - _number1 : l'identifiant du site
 *    - _subNumber1 : l'identifiant du site
 *    - _kind2 : ""
 *    - _number2 : ""
 *    - _subNumber2 : ""
 * @desc :
 *    - appelle la mthode loadSwitches
*/
function checkSwitches(_switchNameArray, _client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2, _modelList){
    loadSwitches(_switchNameArray, _client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2, POST_LOAD_SWITCH_FUNCTION, _modelList) ;
}

/**
 * Permet de charger tous les switchs et de lancer le tratement post appel Ajax
 * @param :
 *    - _switchNameArray : le tableau contenant les switches  checker
 *    - _client : ITOOL
 *    - _kind1 : le type de tiers  traiter : THIRD_KIND_SITE
 *    - _number1 : l'identifiant du site
 *    - _subNumber1 : l'identifiant du site
 *    - _kind2 : ""
 *    - _number2 : ""
 *    - _subNumber2 : ""
 *    - _localfunc : la fontion post ajax  appeler : par ex, POST_LOAD_SWITCH_VENTE_FUNCTION
 * @desc :
 *    - rcupre le serveur erep, initialise ce serveur et charge tous les switches
*/
function loadSwitches(_switchNameOrArray, _client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2, _localfunc, _modelList) {
    switchNameOrArray = _switchNameOrArray ; //va servir  la fonction post ajax    
    kind = _kind1 ; //va servir  la fonction post ajax
    number = _number1 ; //va servir  la fonction post ajax
    subNumber = _subNumber1 ; //va servir  la fonction post ajax    
    thisModelList = _modelList //va servir  la fonction post ajax

    _urlErep = getUrlErep(_SERVICE_SWITCH, _client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2, _localfunc) ; //recupere le serveur eRep
    var oContext = new JoServiceContext(_urlErep, _language, _siteNumber, _centralNumber, _storeNumber);
    
    if(_urlErep != ""){
        setModeContextSwitch(oContext, CONTEXT_MODE_SWITCH,  _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2);
        serviceIdSwitch = executeService(oContext, _localfunc);
    }else{
        setSwitchesErrorStatus(switchNameOrArray) ;     
        if(parent.document.getElementById("bloc-vente")){
            parent.document.getElementById("bloc-vente").style.display = "none" ;
        }
    }
}


/**
 * Permet de traiter les elements associer au switch de la recherche predictive
 * @param :
 *    - switchName : le nom du switch
 *    - kind : le type de tiers a traiter : THIRD_KIND_SITE
 *    - number : l'identifiant du site
 *    - subNumber : 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 checkSwitchRecherchePredictive(switchName, kind, number, subNumber, language, divObjName, checkErrorStatus){    
    var divElement = getElement(divObjName, true, "") ;
    var oSwitch = getMySwitch(serviceIdSwitch,switchName,kind, number, subNumber,language);
    
    if(divElement){
        var activationSwitch = false ;
        if(oSwitch.isEnabled()){
            activationSwitch = oSwitch.isEnabled() ;
        }
        else{
            activationSwitch = false ;
        }
        
        if(checkErrorStatus){
            isAllSwitchError = false ;
        }
        
        /*si le switch est actif on autorise l'appele  la fonction de la recherche predictive*/
        if(activationSwitch){
            divElement.onkeyup = function() {
                searchSuggest(document.getElementById('searchForm' )); 
             }
        }
    }
}

/**
 * Permet de traiter les elements associer au switch du liage dkt
 * @param :
 *    - switchName : le nom du switch
 *    - kind : le type de tiers a traiter : THIRD_KIND_SITE
 *    - number : l'identifiant du site
 *    - subNumber : 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 checkSwitchLiage(switchName, kind, number, subNumber, language, divObjName, checkErrorStatus,refModel){    
    var divElement = getElement(divObjName, true, "") ;
    var oSwitch = getMySwitch(serviceIdSwitch,switchName,kind, number, subNumber,language);
    
    
    if(divElement){
        var activationSwitch = false ;
        if(oSwitch && oSwitch.isEnabled()){
            activationSwitch = oSwitch.isEnabled() ;
        }
        else{
            activationSwitch = false ;
        }
        
        if(checkErrorStatus){
            isAllSwitchError = false ;
        }
        
        /*si le switch est actif on autorise l'appele  la fonction de la recherche predictive*/
        if(!activationSwitch && language=="FR" ){
            var elts = divElement.getElementsByTagName("A");
            for(var i=0 ; i<elts.length ; i++){
                var elt = elts[i];
                if(elt && elt.href && elt.href != ''){
                    elt.href = "" ;
                    elt.target = "_self" ;
                }
            }
        }
    }
}

