/* 2010-01-21 01:51 ----------------------- Certain functions are based on querystring values ---------------------------*/

var VT; /* NB lower case "vt" variable is set in websys/download.asp */
var Affiliatecheck=-1;
var strSource="";
var SiteRedirectorTag="";
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;
     }
}

//------------------------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 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 (x = 0; x < 6; x++) {
        if (strSource.toUpperCase() == noPop_array[x]) {
            noXpop = true;
        }
    }
}

//************* SourceSites Checks Ends Here *******************************

//------------ Site redirector exclusions 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 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 {
        getExitTraffic();
    }
}

//Sart of the main function

function getExitTraffic() {

    BotsExclusion();
    SiteRedirectorExclusion();

	// certain affids do not get the exit strat?? 
    // if (typeof (Affiliatecheck) == "undefined" || Affiliatecheck == 0) { alert("Breaking on Affiliatecheck"); return; }
    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="";}
	

    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) {
        // can't do much here, window is closing 
    }
}

/************** Execution of the exit strategy Starts here *****************************************************************

----------------------------------------------------------------------------------------------------------------------------*/



function getDomain(url) {

    var domain = url.match(/:\/\/(www\.)?([^\/:]+)/);
    if (domain) {
        return domain[2];
    } else {
        return '';
    }
}

	 	 