var sDomainName = document.domain;
if (sDomainName.indexOf('.vesicare.com') != -1) {
    dropDomainName = "domain=.vesicare.com;";
}
else {
    dropDomainName = "";
}

function addNewCookie(sCookieName) {
    var newExpDate = new Date();
    newExpDate.setDate(newExpDate.getDate() + 90);
    document.cookie = sCookieName + "=1; path=/ ;" + dropDomainName + "expires=" + newExpDate.toGMTString();
}

function ShowInvitation(_loadScript) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(script);
    script.src = _loadScript;
}

function getRandom(num) {
    var n = 1000000000; function ugen(old, a, q, r, m) {
        var t = Math.floor(old / q);
        t = a * (old - (t * q)) - (t * r); return Math.round((t < 0) ? (t + m) : t);
    } var m1 = 2147483563, m2 = 2147483399, a1 = 40014, a2 = 40692, q1 = 53668, q2 = 52774, r1 = 12211, r2 = 3791, x = 67108862;
    var g2 = (Math.round(((new Date()).getTime() % 100000)) & 2147483647), g1 = g2; var shuffle = [32], i = 0;
    for (; i < 19; i++) { g1 = ugen(g1, a1, q1, r1, m1); } for (i = 0; i < 32; i++) {
        g1 = ugen(g1, a1, q1, r1, m1);
        shuffle[31 - i] = g1;
    } g1 = ugen(g1, a1, q1, r1, m1); g2 = ugen(g2, a2, q2, r2, m2); var s = Math.round((shuffle[Math.floor(shuffle[0] / x)] + g2) % m1);
    var rand = Math.floor(s / (m1 / (n + 1))) / n; if (typeof (num) == "undefined") { return rand; } else {
        return Math.floor(rand * (num + 1));
    } 
}
var siterecruitUrl = "http://siterecruit.comscore.com/sr/vesicare/broker.js";
var foreseeUrl = "http://www.vesicare.com/foresee/foresee-trigger.js";
if (window.location.protocol == 'https:') {
    siterecruitUrl = "https://siterecruit.comscore.com/sr/vesicare/broker.js";
    foreseeUrl = "https://www.vesicare.com/foresee/foresee-trigger.js";
}

if (document.cookie.indexOf("srloaded") != -1) {
    //alert("srloaded temp cookie detected...\ncontinue to load SR scripts...");
    ShowInvitation(siterecruitUrl);
} else if (document.cookie.indexOf("forseeloaded") != -1) {
    //If foreseeloaded temp cookie exist continue loading Foresee scripts
    //alert("foreseeloaded temp cookie detected...\ncontinue to load Foresee scripts...");
    ShowInvitation(foreseeUrl);
} else {
    var ran1 = getRandom();
    var weight = 0.5; // 50-50 weight distribution
    if (ran1 < weight) {
        //Load Foresee
        //alert("loading Foresee scripts...weight=50");
        ShowInvitation(foreseeUrl);
        addNewCookie("forseeloaded");
    }
    else {
        //Load SR
        //alert("loading SR scripts...weight=50");
        ShowInvitation(siterecruitUrl);
        addNewCookie("srloaded");
    }
}
