 //Separateur pour les produits dans le cookie "mes favoris"
var _SEPARATEUR_FAVORIS = "___" ;

var currentPageFavoris = 1 ;
var nbProduitParPageFav = 5 ;
var limitProduitEnvoiAmi = 8 ;


/** #########################################
* Permet d'afficher le nombre de produits dans les favoris
*/
function updateSeparChar(_langue, _nom){
    var cookieValue = getCookie(_nom) ;
    
    //on va mettre a jour le separateur
    if(cookieValue && cookieValue.indexOf(_SEPARATEUR_FAVORIS) == -1){
        var dateDeLaListe = cookieValue.split("__date_liste=")[1] ;

        var replacements = new Array() ;
        replacements[0] = ";" ;
        replacements[1] = _SEPARATEUR_FAVORIS ;
        var cookieValUpdated = replaceAll(cookieValue, replacements) ;
        
        setCookie(_langue, _nom, cookieValUpdated, dateDeLaListe); 
    }
}

/** #########################################
* Permet d'ajouter une selection
*/
function ajoutSelection(langue, nom, url, reference, libelle, image,typo){
    cookieValue = getCookie(nom);

    //Calcule la date du jour
    var dateDuJour = new Date() ;
    month = dateDuJour.getMonth()+1 ;
    if(month < 10){
      month = "0" + month ;
    }
    
    day = dateDuJour.getDate() ;
    if(day < 10){
      day = "0" + day ;
    }
    
    var dateCourante = day+"/"+month+"/"+dateDuJour.getFullYear() ;
    if(langue == 'EN'){
      dateCourante = dateDuJour.getFullYear()+"-"+month+"-"+day ;
    } 
    
    var numPriorite = "0" ;
    var value = url+"__"+reference+"__"+libelle+"__"+image+"__"+dateCourante+"__"+typo ;    
    var replacementArray = new Array ;
    replacementArray[0] = "&quot;" ;
    replacementArray[1] = "''" ;
    value = replaceAll(value, replacementArray) ;

    // Le cookie n'existe pas
    if (cookieValue == null ){
        var expdate = new Date ();
        var expDateToGMT = "" ;
        
        var aujourdhui = new Date(0);
        var aujourdhuidate = aujourdhui.getTime();
        if (aujourdhuidate > 0){    
            expdate.setTime (expdate.getTime() - aujourdhuidate);
        }
        expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 7)); // expire dans 1 semaine
        expDateToGMT = expdate.toGMTString() ;
        
        value = value + "__date_liste=" + expDateToGMT ;
        
       setCookie(langue, nom, value, expDateToGMT);
       incrementeMaSelection() ;
    }
    //sinon, il existe et on va incrémenter la valeur du nombre de clic dans ce cookie
    else{
        cookieValue = value + _SEPARATEUR_FAVORIS + cookieValue  ;
        dateDeLaListe = cookieValue.split("__date_liste=")[1] ;
        setCookie(langue, nom, cookieValue, dateDeLaListe);
        incrementeMaSelection() ;
    }
    verifMaSelection(reference) ;
}

/** #########################################
* Permet de recuperer le nb de selections en cours
*/
function getNbMaSelection(){
    //On va mettre à jour le cookie pour remplacer tous les ";" par le nouveau séparateur "___"
    updateSeparChar(_language, "panier") ; //_language declare dans l'html
    
    var produits = getCookie('panier') ;
    if(produits){
        produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
        
        if(produitsTab.length>0){
            return produitsTab.length ;
        }
        else{
            return 0 ;
        }
    }
    else{
        return 0 ;
    }
}

/** #########################################
* Permet de selectionner le checkbox principal
*/
function checkCb(cbId){
    var mainCb = document.getElementById(cbId) ;
    if(mainCb){
        if(!mainCb.checked || mainCb.checked == ""){
            mainCb.checked = "true" ;
        }
        else{
            mainCb.checked = "" ;
        }
    }
    checkAll(mainCb) ;
}

/** #########################################
* Permet de selectionner tous les produits
* params : 
*     - mainCbValue : la valeur du checkbox principal 
*/
function checkAll(mainCb){
    var listeCheck = document.getElementsByName("produitCb") ;
    var cbValue = "true" ;
    if(mainCb){
        cbValue = mainCb.checked ;
    }
    for(var j=0 ; j<listeCheck.length ; j++){
        listeCheck[j].checked = cbValue ;        
    }
}

/** #########################################
* Permet d'incrementer le nb de selection
*/
function incrementeMaSelection(){
    parent.document.getElementById("nb-selections").innerHTML = getNbMaSelection() ;
}

/** #########################################
* Permet de verifier si le produit n'est pas deja dans ma selection
*/
function verifMaSelection(idModele){
    var produits = getCookie('panier') ;
    if(produits){
      produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
      if(produitsTab.length>0){
        for(i=0 ; i<produitsTab.length ; i++){
          var paramModele = produitsTab[i] ;
          var paramsTab = paramModele.split("__") ;
    
          var refProduit =  paramsTab[1] ;
          if(idModele == refProduit){
              document.getElementById("dejafavoris").style.display = "block" ;
              document.getElementById("ajouterfavoris").style.display = "none" ;              
          }
        }
      }
    }
}

/** #########################################
* Permet de generer la liste des modeles correspondant aux produit
*/
function getModelList(){    
    modelList = new ArrayList() ;
    
    var produits = getCookie('panier') ;
    if(produits){
        var produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
        if(produitsTab.length>0){
            for(i=0 ; i<produitsTab.length ; i++){
                var paramModele = produitsTab[i] ;
                var paramsTab = paramModele.split("__") ;
                                
                var refProduit =  paramsTab[1] ;
                modelList.add(refProduit) ;
            }
        }
    }
}

/** #########################################
* Permet de générer la liste des sélections
*/
function genereMaSelection(langue, produits, libelleMaSelection, libelleAcces, libelleSupprimer, libelleValidite, libellePrixJur, refLibelle, articleLibelle, prioTitre, prioLib0, prioLib1, prioLib2, urlSite, nomSite, sujet, introduction, corps1, corps2, signature, refLabel,enSavoirPlus) {
    if(produits){
        produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
        if(produitsTab.length>0){
            for(var i=0 ; i<produitsTab.length ; i++){
                var paramModele = produitsTab[i] ;
                var paramsTab = paramModele.split("__") ;

                var urlProduit = paramsTab[0] ;
                var refProduit =  paramsTab[1] ;
                var libelleProduit =  paramsTab[2] ;
                var imageProduit =  paramsTab[3] ;
                var dateSelection = paramsTab[4] ;     
                var typo = paramsTab[5] ;
                var regImg=new RegExp("my-", "g");
                imageProduit=imageProduit.replace(regImg,"mh-");                
                if(imageProduit.indexOf("mh-")==-1){
                    var regImg=new RegExp("80", "g");
                    imageProduit=imageProduit.replace(regImg,"photo-non-dispo");
                }
  
                var funcPicture = setLinkTag(tagRubrique+"ma-selection_product_img","");
                var funcLink = setLinkTag(tagRubrique+"ma-selection_product_link","");

                if(urlProduit && refProduit && libelleProduit && imageProduit && dateSelection ){
                    if(urlProduit != "undefined" && refProduit != "undefined" && libelleProduit != "undefined" && imageProduit != "undefined" && dateSelection!= "undefined" ){
                        contenuHTML = contenuHTML + 
                            '<div class="bloc" id="maSelection'+refProduit+'">'+
                                '<div class="img">'+
                                    '<img  id="picto-new-'+refProduit+'" style="display:none;" src="/'+langue+'/images/static/label-new.gif" class="label-new" />'+
                                    '<p class="thumb">'+
                                        '<img src="'+imageProduit + '" alt="'+libelleProduit+'" class="thumb" />'+
                                        '<a class="blue bold right" href="'+urlProduit+'" onClick="'+funcLink+'">'+enSavoirPlus+'</a>'+
                                    '</p>'+
                                    '<p class="shadow">'+
                                        '<span>'+
                                        '<img height="1" width="100%" style="display: none;" src="/FR/images/static/1ptrans.gif" />'+
                                        '</span>'+
                                    '</p>'+                     
                                    '<h3 class="s14">'+libelleProduit+'</h3>'+                                    
                                    '<p class="center grey s10">'+typo+'  | '+refLibelle+refProduit+'</p>'+                                    
                                    '<div class="link grey">'+
                                        '<a href="'+urlProduit+'" onClick="'+funcPicture+'">'+
                                        '<img align="absmiddle" src="/'+langue+'/images/static/bt-grey-left-conseil.gif" />'+
                                        '<span>'+libelleAcces.toUpperCase()+'</span>'+
                                        '<img align="absmiddle" src="/'+langue+'/images/static/bt-grey-right-conseil.gif" /></a>'+
                                    '</div>'+   
                                    '<div class="ou-acheter">'+
                                        '<div id="bloc-prix' +refProduit+'" class="bloc-prix" style="display:none;">'+
                                            '<div id="bloc-decathlon'+refProduit+'" style="display:none;" class="bloc-decathlon"></div>'+
                                            '<p class="grey s10 libJurPrix" id="libJurPrix'+refProduit+'">'+libellePrixJur+'</p>'+
                                        '</div>'+                                        
                                    '</div>'+                                     
                                    '<p class="grey s10">'+
                                    '</p><form action="" method="post">'+
                                        '<input name="produitCb" class="checkDel" type="checkbox" value="'+refProduit+'" id="delete' + refProduit + '" /> <label for="delete' + refProduit + '">'+libelleSupprimer+'</label>'+
                                    '</form>'+
                                    '<p/>'+
                                '</div>'+
                            '</div>';
                   }
                }
            }
        }
        
        var propertiesDivObjSr = document.getElementById("contentMaSelection");
        propertiesDivObjSr.innerHTML = contenuHTML ;
 
        displayElementsPage() ;
    }            
}

/** #########################################
* Permet d'afficher la page numPage
*/
function showPageFavoris(numPage){
    hideAll("contentMaSelection", "pageFavoris") ;
    
    var pageObj = document.getElementById("pageFavoris"+numPage) ;
    if(pageObj){
        pageObj.style.display = "block" ;
    }
    var paginationBlocObj = document.getElementById("paginationFavoris") ;
    if(paginationBlocObj){
        paginationBlocObj.style.display = "block" ;
    }
    
    resetClass("paginationFavoris", "paginationPage", "span") ;
    
    var toutAfficherObj = document.getElementById("toutafficher") ;
    if(toutAfficherObj && toutAfficherObj.className == "toutafficherselected"){
        toutAfficherObj.className = "toutafficher" ;
    }
        
    var pageSelected = document.getElementById("paginationPage"+numPage) ;
    if(pageSelected){
        pageSelected.className = "paginationPageSelected" ;
    }
}

/** #########################################
* Permet d'afficher toutes les pages
*/
function displayAll(thisObj, idPage, totalPage){
    if(thisObj.className.indexOf('selected') == -1){
        resetClass("paginationFavoris", "paginationPage", "span") ;
            
        for(var n=1 ; n<totalPage; n++){
            var pageToDisplayObj = document.getElementById(idPage+n) ;
            if(pageToDisplayObj){
                pageToDisplayObj.style.display = "block" ;
            }
        }
        thisObj.className = thisObj.className+"selected" ;
    }   
}

/** #########################################
* Permet de sauvegarder sa liste
*/
function saveChange(reference, langue, urlSite, nomSite, sujet, introduction, corps1, corps2, signature, refLabel, prioLib0, prioLib1, prioLib2){
    var produits = getCookie('panier') ;
    if(produits){
        var prio = document.getElementById("prio"+reference) ;
        dateDeLaListe = produits.split("__date_liste=")[1] ;
        produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
        if(produitsTab.length>0){
            var nouvelleValeur = "" ;
            for(t=0 ; t<produitsTab.length ; t++){
                var paramModele = produitsTab[t] ;
                var paramsTab = paramModele.split("__") ;
                
                var refProduit =  paramsTab[1] ;
                if(refProduit != reference){
                    if(nouvelleValeur.length == 0){
                        nouvelleValeur = paramModele ;
                    }
                    else{
                        nouvelleValeur = nouvelleValeur +_SEPARATEUR_FAVORIS+ paramModele ;
                    }
                }
                else{
                    var urlProduit = paramsTab[0] ;                    
                    var libelleProduit =  paramsTab[2] ;
                    var imageProduit =  paramsTab[3] ;
                    var dateSelection = paramsTab[4] ;
                    var numPriorite = prio.value ;
                    paramModele = urlProduit+"__"+refProduit+"__"+libelleProduit+"__"+imageProduit+"__"+dateSelection+"__"+numPriorite ;
                    
                    if(nouvelleValeur.length == 0){
                        nouvelleValeur = paramModele ;
                    }
                    else{
                        nouvelleValeur = nouvelleValeur +_SEPARATEUR_FAVORIS+ paramModele ;
                    }
                }
            }            
            nouvelleValeur = nouvelleValeur + "__date_liste="+dateDeLaListe ;
            setCookie(langue, "panier", nouvelleValeur, dateDeLaListe) ;
            
            var nouveauLienMailTo = getLienMailTo(urlSite, nomSite, sujet, introduction, corps1, corps2, signature, refLabel, prioLib0, prioLib1, prioLib2) ;
            document.getElementById("lien-mailto").href = nouveauLienMailTo ;
        }
    }
}

/** #########################################
* Permet de supprimer une selection
*/
function supprimerSelection(langue, reference){
    var produits = getCookie('panier') ;
    if(produits){    
        dateDeLaListe = produits.split("__date_liste=")[1] ;
        produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
        if(produitsTab.length == 1){        
            deleteCookie(langue, 'panier') ;
            window.location.reload() ;
        }
        else if(produitsTab.length>1){
            var nouvelleValeur = "" ;
            for(t=0 ; t<produitsTab.length ; t++){
                var paramModele = produitsTab[t] ;
                var paramsTab = paramModele.split("__") ;
                
                var refProduit =  paramsTab[1] ;
                if(refProduit != reference){
                    if(nouvelleValeur.length == 0){
                        nouvelleValeur = paramModele ;
                    }
                    else{
                        nouvelleValeur = nouvelleValeur +_SEPARATEUR_FAVORIS+ paramModele ;
                    }
                }
            }            
            nouvelleValeur = nouvelleValeur + "__date_liste="+dateDeLaListe ;
            setCookie(langue, "panier", nouvelleValeur, dateDeLaListe) ;
            
            window.location.reload() ;
        }
    }
}

/** #########################################
* Permet de supprimer les selections choisies
*/
function supprimerSelectionChoisie(langue, warningSupprime){    
    var listeCheck = document.getElementsByName("produitCb") ;
    
    if(listeCheck.length == 0){
        alert(warningSupprime) ;
    }
    else{
        var isArticleSupprime = false ;
        for(var j=0 ; j<listeCheck.length ; j++){
            if(listeCheck[j].checked){
                var reference = listeCheck[j].value ;
                supprimerSelection(langue, reference) ;
                isArticleSupprime = true ;
            }
        }
        if(isArticleSupprime){
            window.location.reload() ;
        }
        else {
            alert(warningSupprime) ;
        }
    }
}

/** #########################################
* Permet de recuperer la date du jour
*/
function getDateDuJour(){
    var dateDuJour = new Date() ;
    month = dateDuJour.getMonth()+1 ;
    if(month<10){
        month = "0" + month ;
    }
    var day = dateDuJour.getDate() ;
    if(day<10){
        day = "0" + day ;
    }    
    var dateCourante = day+"/"+month+"/"+dateDuJour.getFullYear() ;
    if(_language == 'EN'){
        dateCourante = dateDuJour.getFullYear()+"-"+month+"-"+dateDuJour.getDate() ;
    }
    document.getElementById("dateImpression").innerHTML = dateCourante ;
}
                 
/** #########################################
* Permet de recuperer le lien mailTo
*/
function getLienMailTo(urlSite, nomSite, sujet, introduction, corps1, corps2, signature, refLabel, prio0, prio1, prio2){
    //Generation du lien mailTo
    var produits = getCookie('panier') ;
    var bodyEmail = decodeURI(introduction) ;
    
    if(produits){
        produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
        if(produitsTab.length>0){
        for(i=0 ; i<produitsTab.length ; i++){
            var paramModele = produitsTab[i] ;
            var paramsTab = paramModele.split("__") ;
            
            var urlProduit = paramsTab[0] ;
            var refProduit =  paramsTab[1] ;
            var libelleProduit =  paramsTab[2] ;
            var dateSelection = paramsTab[4] ;            
            
            bodyEmail = bodyEmail + "\n" + " - " + libelleProduit + " - " + decodeURI(refLabel) + refProduit + " >> " + urlProduit ;
            }
        }
    }
    var lienMailTo = "mailto:?subject=" +
        escape(decodeURI(sujet)+" "+nomSite) +
        "&body=" + escape(bodyEmail+"\n"+decodeURI(signature)) ;
    return lienMailTo ;
}

/** #########################################
* Permet de verifier le nombre de produit pour envoi a un ami
*/
function verifProduit(avertissementMessage){
    var produits = getCookie('panier') ;
    if(produits){
        produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
        if(produitsTab.length > limitProduitEnvoiAmi){
            alert(avertissementMessage) ;
            return false ;
        }
        else{
            return true ;
        }
    }
    else{
        return false;
    }
}

/** #########################################
* Permet de récupérer le lien mailTo
*/
function displayInformationPage(langue, urlSite, maSelectionValidite, liageDecath, libelleAcheterDecath, libellePrixJur, libelleValidite){
    var produits = getCookie('panier') ;

    if(produits){
        produitsTab = produits.split(_SEPARATEUR_FAVORIS) ;
        if(produitsTab.length>0){
            var propertiesDivObjSr = document.getElementById("contentMaSelection");
            for(i=0 ; i<produitsTab.length ; i++){
                var paramModele = produitsTab[i] ;
                var paramsTab = paramModele.split("__") ;
        
                var urlProduit = paramsTab[0] ;
                var refProduit =  paramsTab[1] ;
                
                if(paramsTab.length > 6){ //on va recuperer la date de creation de cette liste
                    dateCreationListe = paramsTab[paramsTab.length-1].split("=")[1] ;
                    dateObj = new Date(dateCreationListe) ;
                    var monthCrea = dateObj.getMonth()+1 ;
                    if(monthCrea<10){
                        monthCrea = "0" + monthCrea ;
                    }
                    var dayCrea = dateObj.getDate() ;
                    if(dayCrea<10){
                        dayCrea = "0" + dayCrea ;
                    }
                    var dateCrea = dayCrea+"/"+monthCrea+"/"+dateObj.getFullYear() ;
                    if(langue == 'EN'){
                        dateCrea = dateObj.getFullYear()+"-"+monthCrea+"-"+dayCrea ;
                    }
                    libelleValidite = libelleValidite.replace("DATE_CREATION", dateCrea) ;
	     theInnerHTML = propertiesDivObjSr.innerHTML ;
	     propertiesDivObjSr.innerHTML = theInnerHTML ;
                }
            }
           propertiesDivObjSr.innerHTML = theInnerHTML ; 
            
            var infosLegalesBloc = getElement("infoslegales", true, "") ;
            if(infosLegalesBloc){
                infosLegalesBloc.innerHTML = libellePrixJur+"<br/>"+libelleValidite ; 
            }
        }
    }    
    
    document.getElementById("waitingMessage").style.display = "none" ;
    document.getElementById("produits").style.display = "block" ;        
}

/** #########################################
* Permet d'afficher les resultats
*/
function displayElementsPage(){
    hideBlocId("waitingMessage") ;
    hideBlocId("emptyMessage") ;
    
    displayBlocId("outils") ;
    displayBlocId("info") ;
    
    //Traitement pour afficher la validite des favoris
    var produits = getCookie('panier') ;
    setValiditeListe(produits) ;

    //Traitement de l'envoi a un ami
    setLienMailTo(produits) ;
    
    displayBlocId("info") ;
    displayBlocId("produits") ;
}

/** #########################################
* Permet d'afficher la date de validite de la liste
*/
function setValiditeListe(_produits){
    if(_produits){
        var produitsTab = _produits.split(_SEPARATEUR_FAVORIS) ;

        if(produitsTab.length > 0){
            nbProduit = produitsTab.length ;
            var propertiesDivObjSr = getElement("contentMaSelection", true, "");
            for(i=0 ; i<produitsTab.length ; i++){
                var paramModele = produitsTab[i] ;
                var paramsTab = paramModele.split("__") ;

                var urlProduit = paramsTab[0] ;
                var refProduit =  paramsTab[1] ;

                if(paramsTab.length > 7){ //on va recuperer la date de creation de cette liste
                    var dateCreationListe = paramsTab[paramsTab.length-1].split("=")[1] ;
                    
                    dateObj = new Date(dateCreationListe) ;
                    var monthCrea = dateObj.getMonth()+1 ;
                    if(monthCrea<10){
                        monthCrea = "0" + monthCrea ;
                    }
                    var dayCrea = dateObj.getDate() ;
                    if(dayCrea<10){
                        dayCrea = "0" + dayCrea ;
                    }
                    var dateCrea = dayCrea+"/"+monthCrea+"/"+dateObj.getFullYear() ;
                    if(_language == 'EN'){
                        dateCrea = dateObj.getFullYear()+"-"+monthCrea+"-"+dayCrea ;
                    }
                    if(getElement("validiteFavoris")){
                        var libelleValidite = getElement("validiteFavoris").innerHTML ; 
                        libelleValidite = libelleValidite.replace("DATE_CREATION", dateCrea) ;
                        getElement("validiteFavoris", true, "").innerHTML = libelleValidite ;
                    }
                }
            }
        }
    }
}

/** #########################################
* Permet de récupérer le lien mailTo
*/
function setLienMailTo(_produits){
    //Génération du lien mailTo
    var bodyEmail = "" ;
    
    if(_produits){
        produitsTab = _produits.split(_SEPARATEUR_FAVORIS) ;
        if(produitsTab.length>0){
            var nbProduits = produitsTab.length ;
            for(var i=0 ; i < nbProduits ; i++){
                var paramModele = produitsTab[i] ;
                var paramsTab = paramModele.split("__") ;
                
                var urlProduit = paramsTab[0] ;
                var refProduit =  paramsTab[1] ;
                var libelleProduit =  paramsTab[2] ;
                var dateSelection = paramsTab[4] ;
                
                bodyEmail = bodyEmail + "%0A" +
                    "- " + libelleProduit + " - " + refProduit + " > " + urlProduit ;
            }
        }
        
        var mailToObj = getElement("envoyer-ami", true, "") ;
        var mailToHref = mailToObj.href  ;
        mailToHref = mailToHref.replace("__LIST__", bodyEmail) ;
        mailToObj.href = mailToHref ;
    }
}