﻿var jsHost = (('https:' == document.location.protocol) ? 'https://' : 'http://');
var trackURL = jsHost + "vm2.velocity-marketing.com/vm3/Activity.aspx";
var aid = '8752f1fe-d7a3-4bc5-b0eb-c18f3693a955';
var cpn = '9db7dc75-eab8-4dc3-995f-81a0cd57c7bb';
var url = document.URL;
var cookieName = "mid";
var path = "/";
var expDays = 365;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));

function fnWriteCookie() {
    var cDate = new Date();
    var cYear = cDate.getFullYear();
    var cMonth = cDate.getMonth();
    var cDay = cDate.getDate();
    var cHour = cDate.getHours();
    var cMin = cDate.getMinutes();
    var cSec = cDate.getSeconds();
    var cMil = cDate.getMilliseconds();
    var rNum = Math.random() * 100000000;
    var uni = cYear + cMonth + cDay + cHour + cMin + cSec + cMil + rNum;
    var que = createRandomWord(8);
    var id = uni.toString() + que;
    var protocoltxt = document.location.protocol.substring(0, document.location.protocol.length - 1);
    var isSecure = protocoltxt == 'https';

    fnSetCookie(cookieName, id, exp, path, null, isSecure);
    setTrackingPixel(id);
}
function fnSetCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}
function fnGetCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;

    return unescape(document.cookie.substring(offset, endstr));
}
function fnGetCookie(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 fnGetCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}
function checkCookie() {
    var test = fnGetCookie('mid');
    if (test != null)
        setTrackingPixel(test);
    else
        fnWriteCookie();
}
function setTrackingPixel(mid) {
    document.write('<img id=trkImg width=1 height=1 src=\"' + trackURL + '?a=' + aid + '&cpn=' + cpn + '&url=' + escape(url) + '&rurl=' + escape(document.referrer) + '&mid=' + mid + '&type=A\"><br>');
}
function createRandomWord(length) {
    var consonants = 'bcdfghjklmnpqrstvwxyz',
    vowels = 'aeiou',
    rand = function(limit) { return Math.floor(Math.random() * limit); }, i, word = '', length = parseInt(length, 10), consonants = consonants.split(''), vowels = vowels.split('');
    for (i = 0; i < length / 2; i++) {
        var randConsonant = consonants[rand(consonants.length)], randVowel = vowels[rand(vowels.length)];
        word += (i === 0) ? randConsonant.toUpperCase() : randConsonant;
        word += i * 2 < length - 1 ? randVowel : '';
    }
    return word;
}
