
/*****************************************************************/
function setCentralCountry(pays,lang){
    //on en cree un nouveau avec une date d'expiration das 3 mois
    setCookie(lang, "country", pays) ;
    setCookie("", "redirect", lang+"_"+pays) ;
    window.location.href = "./"+lang+"/";
}

/** #########################################
* Permet de récupérer les paramètres passés à l'url
*/
function getParameters() {
     urlGet = document.location.search;
     params = urlGet.split("&");
     param = new Array();
     for(j=0;j<params.length; j++){
     param.push(params[j].split("=")) ;
     }
     return param;
}

/*****************************************************************/
function redirectSiteCountry(){
    var countryCookie = getCookie("redirect") ;

    if(countryCookie && countryCookie != ""){
        //recuperation de la langue et du pays
        var langue = countryCookie.split("_")[0] ;
        var country = countryCookie.split("_")[1] ;
        
        var currentUrlSite = document.location.href ;
        var urlSite = currentUrlSite.split("/")[2] ;
        
        setCentralCountry(country, langue) ;
    }
}

/** #########################################
* Permet de créer le cookie
*/
function setCookie(langue, name, value) {      
    //modif 090615 : gestion cookie 3 mois
    var expdate = new Date ();
    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 * 30 * 3)); // expire dans 3 mois
    expDateToGMT = expdate.toGMTString() ;
    
    var argv = setCookie.arguments;
    var argc = setCookie.arguments.length;
    var path = langue != "" ? "/"+langue+"/" : "/" ;
    var exp = (argc > 3) ? argv[3] : expDateToGMT ;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    
    document.cookie=name+"="+escape(value)+
        ((exp==null) ? "" : ("; expires="+exp))+
        ((path==null) ? "" : ("; path="+path))+
        ((domain==null) ? "" : ("; domain="+domain))+
        ((secure==true) ? "; secure" : "");
}

/** #########################################
* Permet de récupérer les informations d'un cookie
*/
function getCookieVal(offset)
{
 var endstr=document.cookie.indexOf (";", offset);
 if (endstr==-1) endstr=document.cookie.length;
 return unescape(document.cookie.substring(offset, endstr));
}

function getCookie(_namecookie) {
 var arg=_namecookie+"=";
 var alen=arg.length;
 var clen=document.cookie.length;
 var i=0;
 while (i<clen) {
 var j=i+alen;
 if (document.cookie.substring(i, j)==arg)
 return getCookieVal (j);
 i=document.cookie.indexOf(" ",i)+1;
 if (i==0)
 break;
 }
 return null;
}

/** #########################################
* Permet de supprimer un cookie
*/
function deleteCookie(_languecookie, _namecookie) {
 var exp=new Date();
 exp.setTime (exp.getTime() - 100000);
 var cval=getCookie(_namecookie);
 if(cval){
 setCookie(_languecookie, _namecookie, cval, exp.toGMTString());
 }
}

/*
 * Permet de retourner l'url root du site
 * @param : urlHref doit etre du type http://www.aptonia.com ou http://aptonia.preview.dktetrix.net ou http://www.decathlon.fr ou http://fr.preview.dktetrix.net
 * @return : www.aptonia.com ou aptonia.preview.dktetrix.net ou decathlon.fr ou fr.preview.dktetrix.net
*/
function getRootUrl(urlHref){
    if(urlHref){
        var rootUrl = "" ;
        var urlSplit = urlHref.split("/") ;
        if(urlSplit.length > 1 && urlSplit[2]){
            rootUrl = urlSplit[2] ;
        }
        return rootUrl ;
    }
    else{
        return "" ;
    }
}
/*fonction  de vérification de l'url pour outils de stat*/
function getOnlineState(){
  var siteUrl = getRootUrl(document.location.href) ; //retourne www.aptonia.com par ex.
  if(siteUrl.indexOf(".com") > -1){
    return true ;
  }
  else{
    return false ;
  }
}



