/*****************************************/
/* Leroy-Merlin - popender management v2 */
/* Author: JGR (Argia) Nov-2005          */
/* Update Mar-2006 -> block the e-letter */
/*****************************************/



/********************/
/* common functions */
/********************/



/**
 random function
 */
function Alea(repetition) {

  var x = -1;
  while (x < 0) { x = Math.ceil(Math.random() * repetition); }
  return x;

}



/**
 get the value of a cookie
 */
function getCookieVal(offset) {

    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }

    return unescape(document.cookie.substring(offset, endstr));

}



/**
 set the value of a cookie [not in use]
 */
function setCookieVal() {

  var sCookieValue = '1';
  var myCookieVal = getCookie(sCookieName);
  if (myCookieVal == null) {
    setCookie(sCookieName, sCookieValue, exp);
  } else {
    myCookieVal++;
    setCookie(sCookieName, myCookieVal, exp);
  }
  
}



/**
 set a cookie
 */
function setCookie(name, value) {

    var argv = setCookie.arguments;
    var argc = setCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : "/";
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;

    document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");

}



/** 
 delete a acookie
 */
function deleteCookie(name) {

    var expdate = new Date();
    expdate.setTime (expdate.getTime() - 1);
    document.cookie = name + "=" + null + "; expires=" + expdate.toGMTString();

}



/**
 get a cookie
 */
function getCookie(name) {

    var arg = name + "=";
    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;
}





/********************/
/* purchase inquire */
/********************/



/**
 get the QAchat cookie
 */
function getQAchatCookie() {  

    var arg = "NovatrisQAchatCookie=";
    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 "y";
        }
        i = document.cookie.indexOf(" ", i) + 1; 
        if (i == 0) {
            break;
        }
    }  

    return null;
}





/**************************/
/* satisfaction barometer */
/**************************/



var expQuestDays = 365; // number of days the cookie should last

var exp = new Date(); 
exp.setTime(exp.getTime() + (expQuestDays*24*60*60*1000));



/**
 set the barometer cookie
 */
function setQuestCookieVal() {
  var sCookieValue = '1';
  var myCookieVal = getCookie(sCookieName);
  if (myCookieVal == null) {
    setCookie(sCookieName, sCookieValue, exp);
  } else {
    myCookieVal++;
    setCookie(sCookieName, myCookieVal, exp);
  }
  
}

/**
 set the barometer cookie, then send this one to the user
 */
function DoTheQuestCookieStuff(name,url,width,height) {
  setQuestCookieVal();
  var myWindow = window.open(url,'Novatris','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,resizeable=yes,width='+width+',height='+height+',left=1,top=1');
}





/************/
/* e-letter */
/************/

var PopupWindow = null;
var PopupWindowCloser = "n";

var expDays = 365; // number of days the cookie should last
var nbMonthes = 3;
var nbTries = 3;

var sELCookieName = "elettre-inscription-v2";
var iMainCounter = 0;
var iMonthlyCounter = 0;
var iMainExpirationDate = 0;
var iLastAskDate = 0;
var iLastVisitDate = 0;
var iPreviousMainCounter = 0;
var iPreviousMonthlyCounter = 0;

var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
var dtToday = new Date();



/**
 create the E-Letter cookie
 */
function createELCookie() {

    iMainCounter = 1;
    iMainCounter = iMainCounter * 1; // to be sure that this whill be a numeric data...
    iMonthlyCounter = 1;
    iMonthlyCounter = iMonthlyCounter * 1;
    iMainExpirationDate = exp.getTime();
    iMainExpirationDate = iMainExpirationDate * 1;
    iLastAskDate = dtToday.getTime();
    iLastAskDate = iLastAskDate * 1;
    iLastVisitDate = dtToday.getTime();
    iLastVisitDate = iLastVisitDate * 1;
    iPreviousMainCounter = 1;
    iPreviousMainCounter = iPreviousMainCounter * 1;
    iPreviousMonthlyCounter = 1;
    iPreviousMonthlyCounter = iPreviousMonthlyCounter * 1;
}



/**
 read the values of the E-Letter cookie
 */
function getELCookieValues() {

    var sString = "x";
    sString = getCookie(sELCookieName);

    if(sString == null) {
        return null;
    } else {
        var myTab = sString.split("Z");
        var i = 0;
        var flag = true;
        for(i = 0; i < 7; i ++) {
            if(myTab[i] == null || myTab[i] == NaN || myTab[i] == undefined) {
                flag = false;
            }
        }

        if(flag == false) {
            return null;
        }

        iMainCounter = myTab[0];
        iMainCounter = iMainCounter * 1;
        iMonthlyCounter = myTab[1];
        iMonthlyCounter = iMonthlyCounter * 1;
        iMainExpirationDate = myTab[2];
        iMainExpirationDate = iMainExpirationDate * 1;
        iLastAskDate = myTab[3];
        iLastAskDate = iLastAskDate * 1;
        iLastVisitDate = myTab[4];
        iLastVisitDate = iLastVisitDate * 1;
        iPreviousMainCounter = myTab[5];
        iPreviousMainCounter = iPreviousMainCounter * 1;
        iPreviousMonthlyCounter = myTab[6];
        iPreviousMonthlyCounter = iPreviousMonthlyCounter * 1;
    }

    if(iMainCounter <= nbMonthes + 1) {
        if(iMainExpirationDate < dtToday.getTime()) {
            return null;
        }
    }

    return "ok";

}



function incrementELMonthlyCounter() {

    iPreviousMonthlyCounter = iMonthlyCounter;
    iMonthlyCounter ++;

}



function incrementELMainCounter() {

    iPreviousMainCounter = iMainCounter;
    iPreviousMonthlyCounter = iMonthlyCounter;
    iMainCounter ++;
    iMonthlyCounter = 1;

}



function setELLastAskDate() {

    iLastAskDate = dtToday.getTime();

}



function storeELCookie() {

    var sString = iMainCounter + "Z"
        + iMonthlyCounter + "Z"
        + iMainExpirationDate + "Z"
        + iLastAskDate + "Z"
        + iLastVisitDate + "Z"
        + iPreviousMainCounter + "Z"
        + iPreviousMonthlyCounter;

    setCookie(sELCookieName, sString, exp, "/");

}



/**
 function used by "elettre-inscription02" to definitely block the mechanism
 (the user has accepted the e-letter)
 */

function blockELMainCounter() {

    getELCookieValues();
    iPreviousMainCounter = iMainCounter;
    iMainCounter = 999;
    storeELCookie();

}



/**
 function used by "elettre-inscription02.tpl" to unblock the mechanism
 (an error occured in the validation of the user's parameters)
 */
function resetELPreviousCounterState() {

    getELCookieValues();
    iMainCounter = iPreviousMainCounter;
    storeELCookie();

}



/**
 open "elettre-inscription01.htm"
 */
function askForELetter() {

    setELLastAskDate();
    var sName;
    sName = dtToday.getTime();
    var popupHeight = 350;
    var popupWidth = 420;
    var x = 200;
    var y = 200;
    if(ns) {
      try {
        x = (screen.width - popupWidth) / 2;
        y = (screen.height - popupHeight) / 2;
      } catch(e) {
      }
    } else {
      x = (screen.width - popupWidth) / 2;
      y = (screen.height - popupHeight) / 2;
    }
    x = Math.ceil(x);
    y = Math.ceil(y);
    var sParam = "location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,resizeable=yes,dependant=yes,width=" + popupWidth + ",height=" + popupHeight + ",status=no,screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y;

   // PopupWindow = window.open('/html/homepage/fr/elettre-inscription01.htm', sName, sParam);

}



/**
 main function to manage the cookie
 */
function manageELCookiePopup() {

    // block the e-letter
    return;

    // get the cookie
    var result = getELCookieValues();
    // manage the apparition of the question
    if(result == null) {
        // no cookie present, or not in valid format, or expired.
        // reset it, then ask for the E-Letter question
        createELCookie();
        askForELetter();
    } else {
        // cookie is already set, manage it
        if(iMainCounter > nbMonthes + 1) {
            // the e-letter is already active, nothing to do
        } else {
            // the e-letter has been previously refused
            if(iMainCounter <= nbMonthes) {
                if(iMonthlyCounter < nbTries) {
                    // less than three questions since the previous set of the main counter
                    // ask again
                    incrementELMonthlyCounter();
                    askForELetter();
                } else {
                    // the monthly counter equals nbTries
                    // more than three questions since the previous set of the main counter
                    // but if main counters equals nbMonthes, then do nothing
                    if(iMainCounter < nbMonthes) {
                        // Compute the number of days since the previous ask
                        var iDelta = 0;
                        if(iLastAskDate <= 0) {
                            // abnormal case, force the question to appear
                            iDelta = 31;
                        } else {
                            iDelta = dtToday.getTime() - iLastAskDate;
                            iDelta = iDelta * 1;
                            iDelta = iDelta / (24*60*60*1000);
                        }
                        if(iDelta >= 30) {
                            // more then 30 days: ask again
                            incrementELMainCounter();
                            askForELetter();
                        }
                    }
                }
            } else {
                // iMainCounter equals (nbMonthes + 1)
                // do nothing
            }
        }
    }

    storeELCookie();

}

function ScanCookie(variable)
	{
	cook = document.cookie;
	variable += "=";
	place = cook.indexOf(variable,0);
	if (place <= -1)
		return("0");
	else
		{
		end = cook.indexOf(";",place)
		if (end <= -1)
			return(unescape(cook.substring(place+variable.length,cook.length)));
		else
			return(unescape(cook.substring(place+variable.length,end)));
		}
	}

function CreationCookie(nom,valeur,permanent)
	{
	if(permanent)
		{
		dateExp = new Date(2020,11,11);
		dateExp = dateExp.toGMTString();
		ifpermanent = '; expires=' + dateExp + ';';
		}
	else
		ifpermanent = '';
	document.cookie = nom + '=' + escape(valeur) + ifpermanent;
	}

/*****************/
/* main function */
/*****************/

var myAlea=1;
var myAleaQ=0;
function openpp(blocker) {
    
    /* the value "blocker=1" is used to prevent the opening of the window */
    blocker = 0;//maj SJ 21/06/2010 bloquage ouverture popup T21673
    if(blocker != 0) {
        return;
    }
    
    
    var resultAlea=Alea(myAlea);

     var nbvisite = parseInt(ScanCookie("nbvisite"));
   // if(ScanCookie("dejapopup")==0  ){
        CreationCookie("dejapopup","oui",false);
       
       // alert("nbvisite "+nbvisite);
       var modulo = nbvisite % 3;
        if( modulo == 0 || nbvisite == 0){
            //window.open('http://www.leroymerlin.fr/html/popup/_popup.html', 'Invitation','height=550, width=650, top=100, left=100, toolbar=no, menubar=yes, location=no, resizable=yes, scrollbars=no, status=no');
            //    window.open('/html/popup/_popup.html', 'Invitation','height=550, width=650, top=100, left=100, toolbar=no, menubar=yes, location=no, resizable=yes, scrollbars=no, status=no'); old link
        }
        nbvisite = nbvisite+1;
        CreationCookie("nbvisite",""+nbvisite,true);
   // }

    
    
}


/*****************/
/* function for survey*/
/*****************/


function PopupUnder() {
    var pu=window.open("http://ns25526.ovh.net/enquetes/lmsatisv6/introduction/intro-leroymerlin.asp","popunder","width=1200,height=750");
    pu.document.close;
    pu.blur();
}


function addDaysToDate(old_date, delta_days)
{
// Les mois vont de 0 a 11 donc on enleve 1, cast avec *1
var new_date = new Date(old_date.getFullYear() , old_date.getMonth(), old_date.getDate() + delta_days);
return new_date;
}


function CreationCookie30jours(nom,valeur)
{
var now = new Date();
var dt30 = addDaysToDate(now, 30);
dt30 = dt30.toGMTString();
coptions = '; expires=' + dt30 + ';' + " path=/ ;";
document.cookie = nom + '=' + escape(valeur) + coptions;
}

function loadSatisfaction(){
  var SatisfactionAlea=45;
 //check if sort is already done for this person
 var resultAlea = parseInt(ScanCookie("SatisfactionAlea"));
 if(resultAlea == 0){
  resultAlea = Alea(SatisfactionAlea);
 }
 CreationCookie30jours("SatisfactionAlea",resultAlea);
 var nbrefus = parseInt(ScanCookie("nbrefus"));
 var repondu = parseInt(ScanCookie("repondu"));
 if(resultAlea==1 && nbrefus < 2 && repondu==0 ){
  //jQuery('#popup-satisfaction').show();  
  loadpopupmodal('609','409');
 }
}


function setSatisfactionRefus(){
var nbrefus = parseInt(ScanCookie("nbrefus"));
nbrefus = nbrefus+1;
CreationCookie30jours("nbrefus",""+nbrefus);
fermersatisfaction();
}

function setSatisfactionRepondu(){
CreationCookie30jours("repondu","1");
//jQuery('#popup-satisfaction').hide();
fermersatisfaction();
PopupUnder();
}

function loadpopupmodal(width,height) {
var popfichier = '/javascript/common/popin-satisfaction.html';
var popwidth = width;
var popheight = height;
jQuery.get(popfichier, function(data)
{
jQuery(data).modal(
{
autoPosition : true,
overlayClose : true,
closeClass :"close",
overlayCss: {backgroundColor:"#000000"},
minHeight : popheight,
minWidth  : popwidth,
autoResize : false
});
});
}

function fermersatisfaction () {
//jQuery.modal.close();
jQuery('#popup-satisfaction').hide();
jQuery('#simplemodal-container').hide();
jQuery('#simplemodal-overlay').hide();
}

