﻿var OmniObj = function(actionName, theDate, arrHistory) {
    this.action_name = actionName;
    this.sess_date = theDate;
    var month = theDate.getMonth() + 1;
    var dateval = theDate.getDate();
    this.sess_date_str = String.format('{0}{1}{2}', theDate.getFullYear(), month < 10 ? '0' + month : month, dateval < 10 ? '0' + dateval : dateval);
    if (arrHistory) {
        this.history = arrHistory;
    } else {
        this.resetHistory();
    }
};

OmniObj.prototype.resetHistory = function() {
    this.history = [];
    for (var i = 0; i < 30; i++) {
        this.history.push(0);
    }
}

OmniObj.prototype.setDate = function(theDate) {
    var month = theDate.getMonth() + 1;
    var dateval = theDate.getDate();
    this.sess_date_str = String.format('{0}{1}{2}', theDate.getFullYear(), month < 10 ? '0' + month : month, dateval < 10 ? '0' + dateval : dateval);
}

OmniObj.prototype.toString = function() {
    this.history = this.history.slice(0, 30);
    var s = String.format('{0}?{1}:{2}', this.action_name, this.sess_date_str, this.history.join('|'));
    return s;
}

OmniObj.prototype.sessionCount = function() {
    if (this.history.length > 0) {
        return this.history[0];
    }
}

OmniObj.prototype.count = function() {
    var count = 0;
    // count the value
    for (var j = 0; j < this.history.length; j++) {
        count += new Number(this.history[j]);
    }
    return count;
}

OmniObj.prototype.incrementSession = function() {
    if (this.history.length > 0) {
        var prev = new Number(this.history[0]);
        if (prev != NaN) {
            this.history[0] = ++prev;
        }
    }
}

/****** Cookie Manager **************/

OmniCookieManager = function(cookieName) {
    this.cookieName = cookieName;
    this.objs = [];
};

OmniCookieManager.TAG_CREATE = 'TagCreate';
OmniCookieManager.REPORT_RENDER = 'Reports';
OmniCookieManager.BLOG_READ = 'ArticleRead';
OmniCookieManager.NO_COOKIE = 'No Cookie Present';

OmniCookieManager.GetSessionCount = function(o_obj) {
    if (null == o_obj) {
        return NO_COOKIE;
    }

    return o_obj.sessionCount();
}

OmniCookieManager.GetCount = function(o_obj) {
    if (null == o_obj) {
        return OmniCookieManager.NO_COOKIE;
    }

    return o_obj.count();
}

OmniCookieManager.prototype.writeCookie = function(name, value, expires, path, domain, secure) {
    path = "/";

    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires for 30 days
    */
    if (expires) {
        expires = 30 * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + expires);

    document.cookie = name + "=" + value +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ((secure) ? ";secure" : "");
}

OmniCookieManager.prototype.readCookie = function(name) {
    var cookiename = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length, c.length);
    }

    return null;
}




OmniCookieManager.prototype.loadCookie = function() {
    var value = this.readCookie(this.cookieName);

    if (null == value) {
        // write cookie
        var o_obj = new OmniObj(OmniCookieManager.TAG_CREATE, new Date());
        this.objs.push(o_obj);
        o_obj = new OmniObj(OmniCookieManager.REPORT_RENDER, new Date());
        this.objs.push(o_obj);
        o_obj = new OmniObj(OmniCookieManager.BLOG_READ, new Date());
        this.objs.push(o_obj);
        
        this.writeObjs();
    } else {
        //read cookie
        var actions = value.split('!');

        for (var i = 0; i < actions.length; i++) {
            var pos = actions[i].indexOf('?');
            if (pos > -1) {
                var key = actions[i].substring(0, pos);
                var value = actions[i].substring(pos + 1);

                if (null != value) {
                    var last = value.split(':');
                    var d = new Date(last[0].substr(0, 4) + '/' + last[0].substr(4, 2) + '/' + last[0].substr(6, 2));
                    var arr = last[1].split('|');

                    var o_obj = new OmniObj(key, d, arr);
                    this.makeCurrent(o_obj);
                    Array.add(this.objs, o_obj);
                }
            }
        }

        this.writeObjs();
    }
}

OmniCookieManager.prototype.getOmniObj = function(name) {
    var o = null;
    for (var i = 0; i < this.objs.length; i++) {
        if (this.objs[i].action_name == name) {
            o = this.objs[i];
            break;
        }
    }
    return o;
}

OmniCookieManager.prototype.setOmniObj = function(obj) {
    for (var i = 0; i < this.objs.length; i++) {
        if (this.objs[i].action_name == obj.action_name) {
            this.objs[i] = obj
            break;
        }
    }
    this.writeObjs();
}

OmniCookieManager.prototype.writeObjs = function() {
    this.writeCookie(this.cookieName, this.objs.join('!'));
}

OmniCookieManager.prototype.makeCurrent = function(o_obj) {
    // add the difference from today
    var now = new Date();
    var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    var diff = (today - o_obj.sess_date) / (24 * 60 * 60 * 1000);

    if (diff > 0 && diff < 30) {
        //push padding
        for (var j = 0; j < diff; j++) {
            Array.insert(o_obj.history, 0, 0);
        }
        o_obj.history = o_obj.history.slice(0, 30);
    } else if (diff != 0) {
        o_obj.resetHistory();
    }
    //set the date
    o_obj.setDate(today);
}

OmniCookieManager.prototype.incrementSessionCount = function(o_obj) {
    o_obj.incrementSession();
    this.setOmniObj(o_obj);
    return o_obj;
}

/**************** Prepare ********************/
var o_cookieManager = null;
try {
    o_cookieManager = new OmniCookieManager('omni');
    o_cookieManager.loadCookie();
} catch (ex) { }

var render = {};
function reportOmniEventPrepareRenderInfo(tId, sId, optstr, chkblackandwhitebarcodeId, chknogubbinsId) {
    render.styleObj = $get(tId);
    render.sizeObj = $get(sId);
    render.optObjs = optstr.split('|');
    render.blackAndWhiteBarcodeObj = $get(chkblackandwhitebarcodeId);
    render.noGubbinsObj = $get(chknogubbinsId);
}

function reportOmniEventTrackSaveComplete(isNew, tagType) {
    try {
        if (isNew) {
            reportOmniEventTagCreateComplete(tagType);
        } else {
            reportOmniEventTagEditComplete();
        }
    } catch (ex) { }
}

/************** individual tracking functions ****************************/

function reportOmniEventTagCreateStart() {
    try {
        // Be certain to clear all variables set prior to avoid double counting
        clearOmniVars();
        s.linkTrackEvents = 'event6';
        s.linkTrackVars = 'events';
        s.events = 'event6';
        setTimeout("s.tl(s, 'o', 'Tag Creation Start');", 200);
    } catch (ex) { }
}

function reportOmniEventTagCreateComplete(tagtype) {
    try {
        var obj = o_cookieManager.getOmniObj(OmniCookieManager.TAG_CREATE);
        var tags = OmniCookieManager.GetSessionCount(o_cookieManager.incrementSessionCount(obj));
        var total = OmniCookieManager.GetCount(obj);
        // Be certain to clear all variables set prior to avoid double counting
        clearOmniVars();
        s.linkTrackEvents = 'event7';
        s.linkTrackVars = 'events,eVar10,eVar12,eVar11';
        s.events = 'event7';
        s.eVar10 = tagtype;     // Tag type should go here
        s.eVar11 = total;
        s.eVar12 = tags;        // Number of tags created this session, see eVar12
        setTimeout("s.tl(s, 'o', 'Tag Creation Complete');", 200);
    } catch (ex) { }
}

function reportOmniEventTagEditStart() {
    try {
        // Be certain to clear all variables set prior to avoid double counting
        clearOmniVars();
        s.linkTrackEvents = 'event8';
        s.linkTrackVars = 'events,eVar13';
        s.events = 'event8';
        s.eVar13 = 'Tag Edit Start';                              // Tag edit step should go here
        setTimeout("s.tl(s, 'o', 'Tag Edit Step');", 200);        
    } catch (ex) { }
}

function reportOmniEventTagEditComplete() {
    try {
        // Be certain to clear all variables set prior to avoid double counting
        clearOmniVars();
        s.linkTrackEvents = 'event8';
        s.linkTrackVars = 'events,eVar13';
        s.events = 'event8';
        s.eVar13 = 'Tag Edit Complete';                              // Tag edit step should go here
        setTimeout("s.tl(s, 'o', 'Tag Edit Step');", 200);
    } catch (ex) { }
}

function reportOmniEventTagRender() {
    try {
        var size = render.sizeObj.value;

        var style = '';
        var sel = -1;
        for (var j = 0; j < render.optObjs.length; j++) {
            var elm = $get(render.optObjs[j]);
            if (null != elm) {
                if (elm.checked == true) {
                    sel = j;
                    break;
                }
            }
        }

        switch (sel) {
            case 0:
                style = 'With Download Instructions';
                break;
            case 1:
                style = 'Custom';
                break;
            case 2:
                style = 'QR';
                break;
            case 3:
                style = 'NFC';
                break;
        }


        var filetype = '';
        if (render.styleObj.options == null) {
            return;
        }

        for (var j = 0; j < render.styleObj.options.length; j++) {
            if (render.styleObj.options[j].selected) {
                filetype = render.styleObj.options[j].text;
                break;
            }
        }

        var blackandwhitebarcode = render.blackAndWhiteBarcodeObj.checked.toString();
        var nogubbins = render.noGubbinsObj.checked.toString();
        
        clearOmniVars();
        s.linkTrackEvents = 'event9';
        s.linkTrackVars = 'events,eVar13,eVar14,eVar15,eVar16,eVar44,eVar45';
        s.events = 'event9';
        s.eVar13 = 'Tag Download Complete';               // Tag edit step should go here
        s.eVar14 = style;                       // Render Style
        s.eVar15 = size;                            // Render Size
        s.eVar16 = filetype;                                // Render Filetype
        s.eVar44 = blackandwhitebarcode;
        s.eVar45 = nogubbins;
        setTimeout("s.tl(s, 'o', 'Tag Download Step');", 200);
    } catch (ex) {}
}

function reportOmniEventReportRender(reportname) {
    try {
        var obj = o_cookieManager.getOmniObj(OmniCookieManager.REPORT_RENDER);
        var reports = OmniCookieManager.GetSessionCount(o_cookieManager.incrementSessionCount(obj));
        var total = OmniCookieManager.GetCount(obj);

        // Be certain to clear all variables set prior to avoid double counting
        clearOmniVars();
        s.linkTrackEvents = 'event13';
        s.linkTrackVars = 'events,eVar17,eVar19,eVar18';
        s.events = 'event13';
        s.eVar17 = reportname;
        s.eVar18 = total;
        s.eVar19 = reports;       // # of reports this session, see eVar19
        setTimeout("s.tl(s, 'o', 'Report Rendered');", 200);
    } catch (ex) { }
}

function reportOmniEventUserSignedUp() {
    // Be certain to clear all variables set prior to avoid double counting
    clearOmniVars();
    s.linkTrackEvents = 'event24';
    s.linkTrackVars = 'events';
    s.events = 'event24';
    s.tl(s, 'o', 'Tag manager account accept');
}

function clearOmniVars() {
    s.events = ""; s.eVar2 = ''; s.eVar3 = ''; s.eVar4 = ''; s.eVar5 = ''; s.eVar6 = ''; s.eVar7 = ''; s.eVar8 = ''; s.eVar9 = ''; s.eVar10 = ''; s.eVar11 = ''; s.eVar12 = ''; s.eVar13 = ''; s.eVar14 = ''; s.eVar15 = ''; s.eVar16 = ''; s.eVar17 = ''; s.eVar18 = ''; s.eVar19 = ''; s.eVar20 = ''; s.eVar21 = ''; s.eVar22 = ''; s.eVar23 = ''; s.eVar24 = ''; s.eVar25 = ''; s.eVar26 = ''; s.eVar27 = ''; s.eVar28 = ''; s.eVar29 = ''; s.eVar30 = ''; s.eVar31 = ''; s.eVar32 = ''; s.eVar33 = ''; s.eVar34 = ''; s.eVar35 = ''; s.eVar36 = ''; s.eVar37 = ''; s.eVar38 = ''; s.eVar39 = ''; s.eVar40 = '';
}
