﻿//***************** exit strategy *******************************************//

window.onunload = function() {
    var currentDomain = getDomain(window.location.href);
    var elem = document.activeElement;
    if (elem) {
        if (elem.tagName == 'A') {
            if (elem.href && elem.href.indexOf("script:") === -1) {
                var targetDomain = getDomain(elem.href);

                if (targetDomain != currentDomain) {
                    removePages();
                }
            }
        }
        else {
            removePages();
        }
    }
    else {
        removePages();
    }
};

function writeExitPopupCookie() {

    document.cookie = 'popupshown=true;path=/';

}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function removePages() {
    url = window.location.href;
    doExitTraffic = true;

    //pages to be excluded from exit strategy, add the pages below;    
    var excludedPages = new Array();
    excludedPages[0] = "promotion-claim-page";
    excludedPages[1] = "thank-you";
    excludedPages[2] = "rules.aspx";
    excludedPages[3] = "help.aspx";
    excludedPages[4] = "casino-security.aspx";

    //check if exclude page was found, if found, set 'doExitTraffic' to false  
    for (p in excludedPages) {
        var page = RegExp(excludedPages[p]);

        if ((page.test(url))) {
            doExitTraffic = false;
            break;
        }
    }

    // if page was not found do the exit strategy, pop the page up!
    if (doExitTraffic)
        getExitTraffic();

}


//Exclude certain affiliates from exit strategy
function excludeExitPopUp() {
    var affID = document.getElementById("txtAffiliateID").value;
    var excludedSource = 'aff113458,aff113486,aff113487,aff113488,aff113489,aff113491,aff113492,aff113493,aff113975,aff113977,aff114205,aff114324,aff114513,aff114514,aff114521,aff114532,aff115563,aff115579,aff115580,aff115602,aff115737';
    var source = excludedSource.split(',');
    for (var i = 0; i < source.length; i++) {
        var sourceValue = source[i];
        if (sourceValue == affID)
            return true;
    }
    return false;
}
function setCookie(name, value, expires) {
    // no expiration date specified? use this date and it will just be deleted soon.
    if (!expires) expires = new Date();
    document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() + "; path=/";
}
function getCookie(name) {
    var cookies = document.cookie;
    if (cookies.indexOf(name) != -1) {
        var startpos = cookies.indexOf(name) + name.length + 1;
        var endpos = cookies.indexOf(";", startpos) - 1;
        if (endpos == -2) endpos = cookies.length;
        return unescape(cookies.substring(startpos, endpos));
    }
    else {
        return false; // the cookie couldn't be found! it was never set before, or it expired.
    }
}
function getExitTraffic() {
    if (!(getCookie("exitStratCookie"))) {

        // get the ul (set in codebehind onload for 'en')
        var lang = document.getElementById("txtLanguage");
        var ul = "";
        if (typeof (lang) != "undefined" && lang != null && lang.value.toLowerCase() != "") {
            // txtLanguage appears in EN masterpages
            ul = lang.value.toLowerCase();
        }
        else {
            // try get the ul from the URL
            var re = new RegExp(/\/([a-z]{2})\//);
            var m = re.exec(window.location.href);
            if (m != null) {
                ul = m[1];
            }
        }
        var languageHasExitStrat = false;
        // set defaults
        var exitPopUrl = "/exit-strat.aspx";
        var exitPopAttributes = "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=550";


        exitPopUrl = "/exit-strat.aspx";
        exitPopAttributes = "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=560,height=560";
        languageHasExitStrat = true;

        
        if (languageHasExitStrat == true) {
            try {
                var exitWindow = window.open(exitPopUrl
                                    , "Offer"
                                    , exitPopAttributes);
                exitWindow.focus();
            } catch (ex) {
                // Can't do anything, window is closing
            }
        }
        var expdate = new Date(); // pre-set to the current time and date
        expdate.setTime(expdate.getTime() + 1000);
        setCookie("exitStratCookie", true, expdate);
    }

}

function getDomain(url) {

    var domain = url.match(/:\/\/(www\.)?([^\/:]+)/);
    if (domain) {
        return domain[2];
    }
    else {
        return '';
    }
}

//***************** exit strategy ENDS *******************************************//
