﻿// repo support functions for Google Analytics

function repoPrefixPath(thePart, thePrefix) {
   if (thePart && thePart != '' && thePart != thePrefix) // not empty, not just prefix
        return ((thePart.indexOf(thePrefix) == 0? '' : thePrefix) + thePart.toLowerCase());
    return ('');
}

function repoEscapeTitle(theTitle) {
    return theTitle.replace(/\r/g, '').replace(/\n/g, '').replace(/</g, '{').replace(/>/g, '}').replace(/http\:\/\//g, '').replace(/https\:\/\//g, '');
}

function repoTrackN(theLink, eventbutton) { //track local file downloads
    var t = repoEscapeTitle(theLink.title);
    if (t == '')
        t = repoEscapeTitle(theLink.innerHTML);
    var t = document.title + ' - DownloadLink - ' + t;
    var thePath = repoPrefixPath(theLink.pathname, '/'); // /path/script.ext or empty
    thePath += repoPrefixPath(theLink.search, '?'); // ?arg1=val1&arg2=val2 or empty
    thePath += repoPrefixPath(theLink.hash, '#'); // #scrollto or empty
    eventbutton = (eventbutton >= 2 ? 2 : 1); // 0 or 1 for left-click, 3 or 2 for right-click
    try {
        pageTracker._trackEvent('DownloadLink', thePath, t, eventbutton);
        // Category, Action, Label, Value is 1 for left-click, 2 for right-click
        //alert(t + '\n\n' + thePath);
    } catch(err) {}
    return true;
}

function repoTrackX(theLink, eventbutton) { //track external links
    var t = repoEscapeTitle(theLink.title);
    if (t == '')
        t = repoEscapeTitle(theLink.innerHTML);
    var t = document.title + ' - ExternalLink - ' + theLink.hostname + ' - ' + t;
    var thePath = '/' + theLink.protocol.replace(/\:/g, ''); // /http
    thePath += repoPrefixPath(theLink.hostname.replace(/\./g, '_'), '/'); // /www_whatever_com
    thePath += repoPrefixPath(theLink.pathname, '/'); // /path/script.ext or empty
    thePath += repoPrefixPath(theLink.search, '?'); // ?arg1=val1&arg2=val2 or empty
    thePath += repoPrefixPath(theLink.hash, '#'); // #scrollto or empty
    eventbutton = (eventbutton >= 2 ? 2 : 1); // 0 or 1 for left-click, 3 or 2 for right-click
    try {
        pageTracker._trackEvent('ExternalLink', thePath, t, eventbutton);
        // Category, Action, Label, Value is 1 for left-click, 2 for right-click
        //alert(t + '\n\n' + thePath);
    } catch(err) {}
    return true;
}

function repoGetCookieVal (offset) {
    var endstr = document.cookie.indexOf (';', offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function repoGetCookie (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) {
            arg = repoGetCookieVal (j);
            return arg;
        }
        i = document.cookie.indexOf(' ', i) + 1;
        if (i == 0) break;
    }
    return ''; //treat missing cookie as empty cookie
}

var SetVarUpgrades = '/CLIENT/INSIDE/RMS/' //lowest to highest
function repoSetVar(theNewVar) { //make sure _setVar is not downgraded
    if (theNewVar != '') {
        var theOldVar = repoGetCookie('__utmv');
        var dotpos = theOldVar.indexOf('.'); if (dotpos >= 0)
            theOldVar = theOldVar.substr(dotpos +1);
        var theOldPos = SetVarUpgrades.indexOf('/' + theOldVar + '/'); // -1 if old empty
        var theNewPos = SetVarUpgrades.indexOf('/' + theNewVar + '/');
        if (theNewPos >= theOldPos) { //same or upgraded
            //alert(theNewVar + ' >= ' + theOldVar);
            return theNewVar;
        }
        //alert(theNewVar + ' < ' + theOldVar);
    }
    return '';
}
