// 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');
    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
        logHit(VisitCode, webid, BSDContact, "");
        //	removeCookie('BSUContactID');
    }

    if (cid != "") {
        setCookie("BSContactID", cid, 365);
        BSDContact = cid;
        //log newly known hit
        logHit(VisitCode, webid, BSDContact, BSDUContact);
        // 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);

            }
            catch (e) { }
        } else {
            //log unknown hit with UCID
            logHit(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) {
    if (VisitCode != null && VisitCode != "") {
        var LinkSrc = "http://app.beanstalkdata.com/includes/imagethumbnail.aspx?Type=LoadAsset&AssetCode=Common&File=Pixel.gif&WebId=" + webid + "&UserInfo=" + VisitCode + "&CID=" + BSDContact + "&UCID=" + BSDUContact;
        document.write("<img src='" + LinkSrc + "' ></img>");
    }
    else {
        var Code = new Date();
        VisitCode = Code.getTime();
        LinkSrc = "http://app.beanstalkdata.com/includes/imagethumbnail.aspx?Type=LoadAsset&AssetCode=Common&File=Pixel.gif&WebId=" + webid + "&UserInfo=" + VisitCode + "&CID=" + BSDContact + "&UCID=" + BSDUContact;
        setCookie('VisitCode', VisitCode, null);
        document.write("<img src='" + LinkSrc + "' ></img>");
    }
}
