// JScript File
function Querystring() { // optionally pass a querystring to parse


    var qs = window.location.search.substring(1);

    var args = qs.split('&');

    for (var i = 0; i < args.length; i++) {

        var pair = args[i].split('=');
        if (pair[0] == "CID") {

            return pair[1];
        }
    }
    return "";



}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}


function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + "; path=/";
}

function removeCookie(name) {
    if (getCookie(name)) document.cookie = name + "=" +
";expires=Thu, 01-Jan-1970 00:00:01 GMT" + "; path=/";
}





function checkCookie(webid) {
    var cid = Querystring();

    BSDContact = getCookie('BSContactID');
    if (BSDContact == "undefined")
    { BSDContact = ""; }
    BSDUContact = getCookie('BSUContactID');
    if (BSDUContact == "undefined")
    { BSDUContact = ""; }
    VisitCode = getCookie('VisitCode');
    if (VisitCode == null || VisitCode == "") {
        var Code = new Date();
        VisitCode = Code.getTime();
        setCookie('VisitCode', VisitCode, null);
    }


    if (BSDContact != "" && BSDContact != null) {
        //logKnown Hit
        logHit(VisitCode, webid, BSDContact, "");
        moLog(VisitCode,webid,BSDContact,"","");
        //	removeCookie('BSUContactID');
    } else if (cid != "") {
        setCookie("BSContactID", cid, 365);
        BSDContact = cid;
        //log newly known hit
        logHit(VisitCode, webid, BSDContact, BSDUContact);
        moLog(VisitCode,webid,BSDContact,"", "");
        // removeCookie('BSUContactID');
    }
    if (BSDContact == "" || BSDContact == null) {
        if (BSDUContact == "" || BSDUContact == null) {
            try {
                var UCUD = getUCID();
                UCUD = UCUD + '-' + VisitCode;
                setCookie("BSUContactID", UCUD, 365);
                logHit(VisitCode, webid, "", UCUD);
                moLog(VisitCode,webid,"",UCUD, "");
            }
            catch (e) { }
        } else {
            //log unknown hit with UCID
            logHit(VisitCode, webid, "", BSDUContact);
            moLog(VisitCode,webid,"",BSDUContact, "");
        }
    }
    if (BSDContact != "" && BSDUContact != "") {
        if (BSDContact != null && BSDUContact != null) {
            removeCookie('BSUContactID');
        }
    }
}

function getUCID() {
    try {
        var UCID = randomString();
        return UCID;
    }
    catch (e) {    }
}

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 20;
    var x = 0;
    var GUCID = '';
    for (var i = 0; i < string_length; i++) {
        x = Math.round(Math.random() * chars.length);
        GUCID += chars.charAt(x);
        //GUCID +=chars[Math.round(Math.random() * chars.length)];
    }
    return GUCID;
}

function logHit(visitcode, webid, BSDContact, BSDUContact) {
    var dateTime = new Date();
    dateTime = dateTime.getTime();
    if (VisitCode != null && VisitCode != "") {
        var LinkSrc = "//app.beanstalkdata.com/includes/imagethumbnail.aspx?Type=LoadAsset&AssetCode=Common&File=Pixel.gif&WebId=" + webid + "&UserInfo=" + VisitCode + "&CID=" + BSDContact + "&UCID=" + BSDUContact + "&dts=" + dateTime;
        document.write("<img src='" + LinkSrc + "' ></img>");
    }
    else {
        var Code = new Date();
        VisitCode = Code.getTime();
        LinkSrc = "//app.beanstalkdata.com/includes/imagethumbnail.aspx?Type=LoadAsset&AssetCode=Common&File=Pixel.gif&WebId=" + webid + "&UserInfo=" + VisitCode + "&CID=" + BSDContact + "&UCID=" + BSDUContact + "&dts=" + dateTime;
        setCookie('VisitCode', VisitCode, null);
        document.write("<img src='" + LinkSrc + "' ></img>");
    }
}

function trackEvent(webid, action) {
    var cid = Querystring();
    BSDContact = getCookie('BSContactID');
    BSDUContact = getCookie('BSUContactID');
    VisitCode = getCookie('VisitCode');
    if (VisitCode == null || VisitCode == "") {
        var Code = new Date();
        VisitCode = Code.getTime();
        setCookie('VisitCode', VisitCode, null);
    }


    if (BSDContact != "" && BSDContact != null) {
        //logKnown Hit
        logEvent(VisitCode, webid, BSDContact, "", action);
	    moLog(VisitCode,webid,BSDContact,"", action);
        //	removeCookie('BSUContactID');
    }

    else if (cid != "") {
        setCookie("BSContactID", cid, 365);
        BSDContact = cid;
        //log newly known hit
        logEvent(VisitCode, webid, BSDContact, BSDUContact, action);
        moLog(VisitCode,webid,BSDContact, BSDUContact,action);
        // removeCookie('BSUContactID');
    }


    if (BSDContact == "" || BSDContact == null) {
        if (BSDUContact == "" || BSDUContact == null) {
            try {
                var UCUD = getUCID();
                UCUD = UCUD + '-' + VisitCode;
                setCookie("BSUContactID", UCUD, 365);
                logEvent(VisitCode, webid, "", UCUD, action);
                moLog(VisitCode,webid,"", UCUD,action);
            }
            catch (e) { }
        } else {
            //log unknown hit with UCID
            logEvent(VisitCode, webid, "", BSDUContact, action);
            moLog(VisitCode,webid, "", BSDUContact,action);
        }
    }
    if (BSDContact != "" && BSDUContact != "") {
        if (BSDContact != null && BSDUContact != null) {
            removeCookie('BSUContactID');
        }
    }

}

function logEvent(visitcode, webid, BSDContact, BSDUContact, action) {

    var dateTime = new Date();
    dateTime = dateTime.getTime();

    if (VisitCode != null && VisitCode != "") {
        var LinkSrc = "//app.beanstalkdata.com/includes/imagethumbnail.aspx?Type=LoadAsset&AssetCode=Common&File=Pixel.gif&WebId=" + webid + "&UserInfo=" + visitcode + "&CID=" + BSDContact + "&UCID=" + BSDUContact + "&dts=" + dateTime + "&ActionName=" + action;
        var newcontent = document.createElement('img');
        newcontent.setAttribute('src', LinkSrc);
    }
    else {
        var Code = new Date();
        VisitCode = Code.getTime();
        LinkSrc = "//app.beanstalkdata.com/includes/imagethumbnail.aspx?Type=LoadAsset&AssetCode=Common&File=Pixel.gif&WebId=" + webid + "&UserInfo=" + VisitCode + "&CID=" + BSDContact + "&UCID=" + BSDUContact + "&dts=" + dateTime + "&ActionName=" + action;
        setCookie('VisitCode', VisitCode, null);
        var newcontent = document.createElement('img');
        newcontent.setAttribute('src', LinkSrc);
    }
}

function moLog(visitcode, webid, BSDContact, BSDUContact, action) {
    var dateTime = new Date();
    dateTime = dateTime.getTime();
    var jsPage = window.location.pathname;
    if (!(VisitCode != null && VisitCode != "")) {
        var Code = new Date();
        VisitCode = Code.getTime();
        setCookie('VisitCode', VisitCode, null);
    }
    var LinkSrc = "//proc.beanstalkdata.com/mongo/track.php?WebId=" + webid + "&visit=" + visitcode + "&CID=" + BSDContact +"&UCID="+ BSDUContact +"&jsPage="+ jsPage +"&action="+ action +"&dts=" + dateTime;
    var newcontent = document.createElement('img');
    newcontent.setAttribute('src', LinkSrc);
}
