/***********************************************************************************/
/**********************               POP UP UNDER        **************************/
/***********************************************************************************/
/******These methods enable to affect a popup to the current window which***********/
/**************************redirect the guest to a survey***************************/
/***********************************************************************************/
var divId = "";
var isPopunderIncluded = true;

function popupNova(genericTest,currentTest){

    var testPagePopunder = "";
    var divImageBackground = "";
    var myStyle = "";
    var noThankYou = "";
     var html= "";
   
    if (genericTest == "myDisneyland") {
        if (currentTest == "joinNow") {
            myStyle =  "width : 180px; height: 30px;  border: 0px; float: left;";
            testPagePopunder = '\n<a href='+urlMyDisneyland+'  alt="Disneyland Paris" title="Disneyland Paris" style="'+myStyle+'"></a>\n';
            divImageBackground = '\n<div id="pop_upRedirect">\n';
            noThankYou = "width : 60px; height: 15px;  border: 0px; float: left;";
            divId= "pop_upRedirect";
        }else if (currentTest == "joinAtEnd"){
            myStyle =  "width : 265px; height: 30px;  border: 0px; float: left;";
            testPagePopunder = '\n<a href="#" onclick= popCenter("'+urlMyDisneyland+'");setCapNova(); alt="Disneyland Paris" title="Disneyland Paris" style="'+myStyle+'"></a>\n';
            divImageBackground = '\n<div id="pop_upUnder">\n';
            noThankYou = "width : 80px; height: 15px;  border: 0px; float: left;";
            divId= "pop_upUnder";
        }
        if (document.getElementById('divPopUnder')) {
           
            //html +="<div id='bodyMask'></div>";
            html += "\n";
            // iframe is constructed in the code for ie6
            html += '<iframe id="iframe" width="330px" height="330px" style="position:absolute;top:30%;left:30%;visibility:hidden;z-index:987;"></iframe>\n';
            html += divImageBackground;
            html += '<div id="closeWindowCross">\n';
            html += '<a href="#" onclick="fermerPopupNova();" alt="Close Window" title="Disneyland Paris" style="border: 0px; float: right; width: 19px; height: 19px;"></a>\n';
            html += '</div>\n';
            html += '<div id="LienCliquezIci">\n';
            html += testPagePopunder;
            html += '</div>\n';
            html += '<div id="closeWindowArea">\n';
            html += '<a href="#" onclick="fermerPopupNova();" alt="Close Window" title="Disneyland Paris" style ="'+noThankYou+'"></a>\n';
            html += '</div>\n';
            html += '</div>\n';
            document.getElementById('divPopUnder').innerHTML = html;
            document.getElementById('maskBody').style.display = "block";
        }
        
    } else if (genericTest == "qualificator") {
        if (currentTest == "joinNow") {
            myStyle =  "width : 110px; height: 30px;  border: 0px; float: left;";
            testPagePopunder = '\n<a href='+urlQualificator+'  alt="Disneyland Paris" title="Disneyland Paris" style="'+myStyle+'"></a>\n';
            divImageBackground = '\n<div id="qualificatorBuzz">\n';            
            divId= "qualificatorBuzz";
        }else if (currentTest == "joinAtEnd"){
            myStyle =  "width : 118px; height: 20px;  border: 0px; float: left;";
            testPagePopunder = '\n<a href="#" onclick= popCenter("'+urlQualificator+'");setCapNova(); alt="Disneyland Paris" title="Disneyland Paris" style="'+myStyle+'"></a>\n';
            divImageBackground = '\n<div id="qualificatorSully">\n';            
            divId= "qualificatorSully";
        }
        if (document.getElementById('divPopUnder')) {
            html += "\n";
            // iframe is constructed in the code for ie6
            html += '<iframe id="iframe" width="330px" height="330px" style="position:absolute;top:30%;left:30%;visibility:hidden;z-index:987;"></iframe>\n';
            html += divImageBackground;
            html += '<div id="closeWindowQualif">\n';
            html += '<a href="#" onclick="fermerPopupNova();" alt="Close Window" title="Disneyland Paris" style="border: 0px; float: right; width: 12px; height: 12px;"></a>\n';
            html += '</div>\n';
            html += '<div id="LienCliquezIci">\n';
            html += testPagePopunder;
            html += '</div>\n';           
            html += '</div>\n';
            document.getElementById('divPopUnder').innerHTML = html;
            document.getElementById('maskBody').style.display = "block";
        }
    }    
}


function fermerPopupNova() {
    document.getElementById(divId).style.display="none";
    // Iframe is for ie6
    document.getElementById("iframe").style.display="none";
    if (document.getElementById('maskBody')) {
        document.getElementById("maskBody").style.display="none";
    }
   

}

// This method constructs a popup whishes displayed behind the main window
function popCenter(page) {
    if (document.getElementById('maskBody')) {
        document.getElementById("maskBody").style.display="none";
    }
    var options = "'status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes'";
    var width = "1000";
    var height = "740";
    var top=(screen.height-height)/2;
    var left=(screen.width-width)/2;
    window.open(page,"","top="+top+",scrollbars=yes"+",left="+left+",width="+width+",height="+height+","+options);
    
    if ((window.parent && navigator.userAgent.indexOf('Chrome/') > 0)
        || navigator.userAgent.indexOf('Opera/') > 0) {
        window.parent.blur();
    } else {
        window.focus();
    }



}



// Public function called in Home Page. It gives rules to display or not the popunder (random function)

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

function getCookiePopUp(NameOfCookie){
    if (document.cookie.length > 0) {
        begin = document.cookie.indexOf(NameOfCookie+"=");
        if (begin != -1) {
            begin += NameOfCookie.length+1;
            end = document.cookie.indexOf(";", begin);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;
}

function setCookiePopUp(NameOfCookie, value, expiredays) {
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    var path = "/";    
    var splitMyDomain = new Array();
    var splitMyDomainLenght = 0;
    var myDomainToCheck = window.location.host;
    splitMyDomain = myDomainToCheck.split('.')
    splitMyDomainLenght = splitMyDomain.length;
    var tld = splitMyDomain[splitMyDomainLenght-1];
    var domain = ".";
    var end = 0;
    (tld == "uk") ?  end = 4 : end = 3;
    for (var i =0; i<end; i++) {
        if (splitMyDomain[splitMyDomainLenght-i]) {
            domain +=splitMyDomain[splitMyDomainLenght-end+i]+".";
        }
    }
    domain = domain.substring(0,domain.length-1);
    document.cookie = NameOfCookie + "=" + escape(value) +"; path="+path+
    "; domain="+domain+ ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}




function DoTheCookieStuff(name,capping,genericTest,currentTest) {
    if (isPopUpDisplayed()) {
        times_visited=eval(getCookiePopUp(name))+1;
        setCookiePopUp(name,times_visited,15);
        if (times_visited<=capping)    {
            popupNova(genericTest,currentTest);
        }
    }
}

function setCapNova() {
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (15 * 24 * 3600 * 1000));
    document.cookie = "apb176dpg" +  "=" + escape(3) + "; expires=" + ExpireDate.toGMTString();
    document.getElementById(divId).style.display="none";
    document.getElementById("iframe").style.display="none";
}





function isPopUpDisplayed()  {
    var MyDisneyland = window.location.host;  
    var returnMyString = MyDisneyland.indexOf("my-disneyland");
    var displayPopPup = true;
    if (returnMyString != -1) {
        displayPopPup = false;
    } 
    return  displayPopPup;
}


function displayPopUpUnder(displayNb,random,testRef,genericTest) {
    if (Aleatoire(random ) == 1){
        var cakeName = "";
        (genericTest == "myDisneyland") ?  cakeName = "apb176dpg" : cakeName = "000XFR529";
        DoTheCookieStuff(cakeName,displayNb,genericTest,testRef);

    }
}
/***********************************************************************************/
/**********************               END POP UP UNDER        **************************/

