/* ----------------------- Certain functions are based on querystring values ---------------------------*/

var VT; /* NB lower case "vt" variable is set in websys/download.asp */
var Affiliatecheck = 0;
var strSource = "";
var SiteRedirectorTag = "";
var SiteRedirectorTag1 = "";
var cuba = "";
var modeTag = "";
var noXpop = false;

var query = window.location.search.substring(1);
var vars = query.split("&");

for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    switch (pair[0]) {
        case "VT": VT = pair[1]; break;
        case "sourcesite":
        case "s":
            strSource = pair[1].toLowerCase();
            Affiliatecheck = strSource.indexOf("aff"); break;
        case "MU": SiteRedirectorTag = pair[1]; break;
        case "trd": SiteRedirectorTag1 = pair[1]; break;
        case "cuba": cuba = pair[1]; break;
        case "mode": cuba = modeTag[1]; break;
    }
}

/* ----------------------- END certain functions are based on querystring values ---------------------------*/



//------------------------Cookie State Management Starts Here---------------------------------------------



var expDays = -1; // number of days the cookie should last



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;
}


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] : null;
    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" : "");
}


function DeleteCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = GetCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


var exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));

function amt() {
    var count = GetCookie('mycount')
    if (count == null) {

        SetCookie('mycount', '1')

        return 1

    }

    else {

        var newcount = parseInt(count) + 1;

        DeleteCookie('mycount')

        SetCookie('mycount', newcount, exp)

        return count

    }

}

function getCookieVal(offset) {

    var endstr = document.cookie.indexOf(";", offset);

    if (endstr == -1)

        endstr = document.cookie.length;

    return unescape(document.cookie.substring(offset, endstr));

}



//------------------------Cookie State Management Ends Here---------------------------------------------



//************* SourceSites Checks Starts Here *******************************

function BotsExclusion() {
    /* prevents exit strat for certain affiliates */
    if (typeof (strSource) == "undefined" || strSource == null) { return; }

    var noPop_array = new Array(6);
    noPop_array[0] = "AFF81035"
    noPop_array[1] = "AFF57222"
    noPop_array[2] = "AFF25486"
    noPop_array[3] = "AFF16205"
    noPop_array[4] = "AFF75501"
    noPop_array[5] = "AFF72573"

    for (var x = 0; x < 6; x++) {
        if (strSource.toUpperCase() == noPop_array[x]) {
            noXpop = true;
        }
    }
}

//************* SourceSites Checks Ends Here *******************************



//------------ Site redirector exlcusions Starts Here ----------------------

function SiteRedirectorExclusion() {

    if (typeof (SiteRedirectorTag) == "undefined" || SiteRedirectorTag == null) { return; }
    if (SiteRedirectorTag.toUpperCase() == "XP") {
        noXpop = true;
    }
}

//------------ Site redirector exlcusions Ends Here ------------------------



/*---------------------------------------------------------------------------

!!!!!! Collection of geopraphical location info Starts Here !!!!!!!!!!!!!!!!*/

function GetCountry() {

    if (document.cookie.length > 0) {

        var AllCookies = document.cookie;

        var IndividualCookie = AllCookies.split(";");



        if (AllCookies.indexOf("&cont=") != -1)

            var AllCookiesIndex = AllCookies.indexOf("cont=")

        var AllCookiesLastIndex = AllCookies.lastIndexOf("&start")

        var FullCont = AllCookies.substring(AllCookiesIndex, AllCookiesLastIndex)

        Cont = FullCont.replace("cont=", "")

        //alert(AllCookiesIndex); 

        //alert(AllCookiesLastIndex);

        //alert(Cont);

    }

}

/*!!!! Collection of geopraphical location info Ends Here !!!!!!!!!!!!!!!!

-----------------------------------------------------------------------------*/



function RegionalExitExecution() {

    GetCountry();

    var ContValue = Cont.toLowerCase();

    var result = null;



    /*switch (ContValue)

             {

               case "de": window.open('/popups/german/index.asp?a=71590&VT='+VT+'&nopop=true','GermanExit',''); break;

               case "jp": window.open('/popups/gamingclub-japanese.asp?VT='+VT+'&nopop=true','JapeneseExit','width=352,height=351'); break;

               //case "us": window.open('http://www.universalnutrition.com','EnglishExit',''); break;

               default: result = 'unknown';

             }*/

}



/*----------------------------------------------------------------------------------------------------------------------------

**************** Execution of the exit strategy Starts here ******************************************************************/

window.onunload = function() {
    var count = GetCookie('mycount');
    var currentDomain = getDomain(window.location.href);
    var elem = document.activeElement;

    if (elem) {
        if (elem.tagName == 'A') {
            if (elem.href) {
                var linkDomain = getDomain(elem.href);
                if (linkDomain != currentDomain && linkDomain.indexOf("javascript") == -1)
                    getExitTraffic();
            }
        }
    }
    else {
        // We fire exit strat just once on reload/refresh/move to another page in teh site
        if (count == null) {
            count = 1;
            SetCookie('mycount', count, exp);
            getExitTraffic();
        } else {
            count++;
            SetCookie('mycount', count, exp);
        }
    }
}
//------------------------ExitTraffic Executes Ends Here---------------------------------------------



//Sart of the main function

function getExitTraffic() {

    BotsExclusion();
    SiteRedirectorExclusion();

    if (typeof (noXpop) == "undefined" || noXpop == true) { return; }
    if (typeof (vp) == "undefined" || vp == null) { vp = ""; }
    if (typeof (ul) == "undefined" || ul == null) { ul = ""; }
    if (typeof (cur) == "undefined" || cur == null) { cur = ""; }

    if (typeof (cuba) != "undefined" && cuba == "true") {
        try {
            var exitWindow = window.open('http://www.cubalaya.com/?inId=1');
            exitWindow.focus();
        } catch (ex) {
            // window closing, can't do anything.
        }
    } else {
        try {
            var popUp = window.open("http://www.bellerockentertainment.com/exit-strat/general.asp?vp=" + vp + "&ul=" + ul + "&cur=" + cur + "&"
                        , "Offer"
                        , 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=500');
            popUp.focus();
        } catch (ex) {
            // window closing, can't do anything.
        }
    }
}
/************** Execution of the exit strategy Starts here *****************************************************************

----------------------------------------------------------------------------------------------------------------------------*/



function getDomain(url) {

    var domain = url.match(/:\/\/(www\.)?([^\/:]+)/);



    if (domain) {

        return domain[2];

    }

    else {

        return '';

    }

}

	 