﻿function createICStatsTracker(a) {
    this.a = a;
    domain = "application.icstats.nl";
    protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
    basket = null;
    infoTags = null;
    user = "";
    homepageName = "Home";
    defaultPages = new Array('/index', '/default', 'index', 'default');
    campaignKey = "campaign";
    sourceKey = "source";
    mediumKey = "medium";
    keywordKey = "keyword";
    userKey = "icsuid";
    hashPriority = false;
    ignoreNumericCategory = false;
    pageNameFilter = null;
    debug = false;

    //extra visitorinfo
    colours = "";
    resolution = "";
    useragent = "";
    extra = "";

    this.getDomain = function () {
        return protocol + domain;
    }
    this.trackEvent = function (b, f) {
        g = this.getDomain() + "/stats.aspx?";
        var _h;
        if (b != null) {
            if (b.length < 1)
                _h = this.getPageNameByUrl();
            else
                _h = b;
        }
        else
            _h = this.getPageNameByUrl();
        if (f != null) {
            _h += "&productId=" + f;
        }
        this.writeImage(g + this.getAllData() + "&page=" + _h);
    }
    this.trackPageview = function (b) {
        this.trackEvent(b);

        //set extra cookie
        if (this.icsgc("icsv") == "") {
            this.icssc("icsv", "{'colours':'" + colours + "','resolution':'" + resolution + "','user':'" + user + "','useragent':'" + useragent + "'}", this.getExpirationdate(365));
        }
    }
    //OBSOLETE
    this.trackExtra = function (x) {
        
    }
    this.getAllData = function () {
        var rv = this.getDefaultInfo();
        rv += '&his=' + this.icscc();
        rv += this.getCampaignParameters();
        rv += '&customer=' + user;
        rv += '&visitorextra=' + extra;
        if (basket != null) {
            rv += basket.toString();
        }
        return rv;
    }
    this.getDefaultInfo = function () {
        var _i, _j, c, r, _k, _l, d = document;
        _i = new Date();
        _j = navigator.appName;
        if (_j != 'Netscape') {
            c = screen.colorDepth;
        }
        else {
            c = screen.pixelDepth;
        }
        try {
            r = parent == self ? window.document.referrer : top.document.referrer;
        }
        catch (e) {
            r = window.document.referrer;
        }
        if (!r || r == "") {
            r = "-";
        }
        else {
            dm = d.domain;
            p = r.indexOf(dm);
            if ((p >= 0) && (p <= 8)) {
                r = "0";
            }
            if (r.indexOf("[") == 0 && r.lastIndexOf("]") == (r.length - 1)) {
                r = "-";
            }
        }
        _k = screen.width + 'x' + screen.height;

        //set extra visitorinfo
        colours = c;
        resolution = _k;
        useragent = escape(navigator.userAgent);

        _l = 'siteId=' + a + '&js=1&c=' + c + '&res=' + _k + '&r=' + escape(r) + '&ua=' + escape(navigator.userAgent) + '&time=' + _i.getTime();
        return _l;
    }
    this.writeImage = function (g, aObj) {
        if (debug) {
            document.write(g);
        }
        else {
            var i = new Image(1, 1);
            i.src = g;
        }
    }
    this.getPageNameByUrl = function () {
        var _m;
        if (pageNameFilter != null) {
            _m = window.location.href.replace(window.location.protocol + '//', '');
            _m = _m.replace(pageNameFilter, '');
        }
        else {
            _m = window.location.pathname;
        }
        var h;
        if (_m.lastIndexOf('.') == -1) {
            h = _m;
        }
        else {
            h = _m.substring(0, _m.lastIndexOf('.'));
        }
        h = h.replace('_', ' ');
        if (h == '/' || defaultPages.contains(h)) {
            h = homepageName;
        }
        if (ignoreNumericCategory) {
            h = this.filterNumericCategory(h);
        }
        return h;
    }
    this.filterNumericCategory = function (h) {
        var c = h.split('/');
        var rv = '';
        for (var i = 0; i < c.length; i++) {
            if (isNaN(c[i]))
                rv += '/' + c[i];
        }
        return rv;
    }
    this.setUser = function (j, k) {
        if (j != "" && j != undefined) {
            if (k)
            {
                user = this.decode64(j);
            }
            else
            {
                user = j;
            }
        }
    }
    this.setExtra = function (j) {
        if (j != "" && j != undefined) {
            extra = j;
        }
    }    
    this.addInfo = function (l, j) {
        this.getInfoTags()[this.getInfoTags().length] = new this.nameValuePair(l, j);
    }
    this.nameValuePair = function (m, j) {
        this.m = m;
        this.j = j;
        this.toString = function () {
            return this.m + "=" + this.j + ";";
        }
    }
    this.getInfoTags = function () {
        if (infoTags == null)
            infoTags = new Array();
        return infoTags;
    }
    this.setBasket = function (n, o, q, s) {
        basket = new createBasket();
        if (n != null)
            basket.n = n;
        if (o != null)
            basket.o = o;
        if (q != null)
            basket.q = q;
        if (s != null)
            basket.s = s;
    }
    this.addItem = function (t, m, u, v) {
        if (basket != null) {
            basket.addItem(new item(t, m, u, v));
        }
    }
    function createBasket() {
        this.i = 0;
        this.items = new Array();
        this.n = "";
        this.q = "";
        this.s = "";
        this.o = 0;
        this.addItem = function (w) {
            this.items[this.i++] = w;
        }
        this.toString = function () {
            var _n = "";
            for (var i = 0; i < this.items.length; i++) {
                _n += this.items[i] + "|";
            }
            _n = "&items=" + escape(_n.substring(0, _n.length - 1));
            _n += "&transId=" + this.n;
            _n += "&basketTotal=" + this.o;
            _n += "&basketCity=" + this.q;
            _n += "&basketCountry=" + this.s;
            return _n;
        }
    }
    function item(t, m, u, x) {
        this.t = t;
        this.m = m;
        this.u = u;
        this.x = x;
        this.toString = function () {
            return this.t + ";" + this.m + ";" + this.u + ";" + this.x;
        }
    }
    this.icscc = function () {
        var _i = new Date(), hid, visits, sid = this.icsgc('icstatssid'), y = 'expires=Sat, 18 Aug 2035 00:00:00 GMT;';
        if (sid == "") {
            this.icssc('icstatssid', Math.round((_i.getTime() - 819936000000) / 1000) + "" + Math.round(Math.random() * 128647), _i);
        }
        if (this.icsgc('icstatshis') != "") {
            hid = this.icsgc('icstatshis').split('.')[0];
            visits = this.icsgc('icstatshis').split('.')[1];
            if (sid != hid) {
                this.icssc('icstatssid', hid, _i);
                this.icssc('icstatshis', hid + "." + (parseInt(visits) + 1), y);
            }
        }
        if (this.icsgc('icstatshis') == "") {
            this.icssc('icstatshis', this.icsgc('icstatssid') + "." + 1, y);
        }
        return this.icsgc('icstatshis');
    }
    this.icssc = function (m, j, y) {
        document.cookie = m + "=" + escape(j) + ';' + y + '; path=/';
    }
    this.icsgc = function (m) {
        var _o = document.cookie.indexOf(m + "=");
        var _p = _o + m.length + 1;
        if ((!_o) && (m != document.cookie.substring(0, m.length))) return '';
        if (_o == -1) return '';
        var _q = document.cookie.indexOf(";", _p);
        if (_q == -1) _q = document.cookie.length;
        return unescape(document.cookie.substring(_p, _q));
    }
    this.getCampaignParameters = function () {
        var _r, _s, _t, _u;
        if (hashPriority) {
            if (this.getHashParameter(campaignKey).length > 0)
                return this.getHashCampaign();
            else
                return this.getQueryStringCampaign();
        }
        else {
            if (this.getQueryStringParameter(campaignKey).length > 0)
                return this.getQueryStringCampaign();
            else
                return this.getHashCampaign();
        }
    }
    this.getHashCampaign = function () {
        var rv = "&campaign=" + this.getHashParameter(campaignKey);
        rv += "&src=" + this.getHashParameter(sourceKey);
        rv += "&medium=" + this.getHashParameter(mediumKey);
        rv += "&kw=" + this.getHashParameter(keywordKey);
        this.setUser(this.getHashParameter(userKey), true);
        return rv;
    }
    this.getQueryStringCampaign = function () {
        var rv = "&campaign=" + this.getQueryStringParameter(campaignKey);
        rv += "&src=" + this.getQueryStringParameter(sourceKey);
        rv += "&medium=" + this.getQueryStringParameter(mediumKey);
        rv += "&kw=" + this.getQueryStringParameter(keywordKey);
        this.setUser(this.getQueryStringParameter(userKey), true);
        return rv;
    }
    this.getHashParameter = function (z) {
        var _v = unescape(document.location.hash.substring(1));
        var _w = _v.split("&");
        for (var i = 0; i < _w.length; i++) {
            var _x = _w[i].split("=");
            if (_x[0] == z && _x.length > 1) {
                return _x[1];
            }
        }
        return "";
    }
    this.getQueryStringParameter = function (z) {
        z = z.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var _y = "[\\?&]" + z + "=([^&#]*)";
        var _z = new RegExp(_y);
        var _aa = _z.exec(document.location.href);
        if (_aa == null)
            return "";
        else
            return _aa[1];
    }
    var _ab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    this.encode64 = function (_a) {
        var _ac = "";
        var _ad, _ae, _af;
        var _ag, _ah, _ai, _aj;
        var i = 0;
        do {
            _ad = _a.charCodeAt(i++);
            _ae = _a.charCodeAt(i++);
            _af = _a.charCodeAt(i++);
            _ag = _ad >> 2;
            _ah = ((_ad & 3) << 4) | (_ae >> 4);
            _ai = ((_ae & 15) << 2) | (_af >> 6);
            _aj = _af & 63;
            if (isNaN(_ae)) {
                _ai = _aj = 64;
            } else if (isNaN(_af)) {
                _aj = 64;
            }
            _ac = _ac + _ab.charAt(_ag) + _ab.charAt(_ah) +
            _ab.charAt(_ai) + _ab.charAt(_aj);
        } while (i < _a.length);
        return _ac;
    }
    this.decode64 = function (_a) {
        var _ac = "";
        var _ad, _ae, _af;
        var _ag, _ah, _ai, _aj;
        var i = 0;
        _a = _a.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        do {
            _ag = _ab.indexOf(_a.charAt(i++));
            _ah = _ab.indexOf(_a.charAt(i++));
            _ai = _ab.indexOf(_a.charAt(i++));
            _aj = _ab.indexOf(_a.charAt(i++));
            _ad = (_ag << 2) | (_ah >> 4);
            _ae = ((_ah & 15) << 4) | (_ai >> 2);
            _af = ((_ai & 3) << 6) | _aj;
            _ac = _ac + String.fromCharCode(_ad);
            if (_ai != 64) {
                _ac = _ac + String.fromCharCode(_ae);
            }
            if (_aj != 64) {
                _ac = _ac + String.fromCharCode(_af);
            }
        } while (i < _a.length);
        _ac = _ac.replace(/[^A-Za-z0-9\@\.\!\#\$\%\*\/\?\|\^\{\}\`\~\&\'\+\-\=\_]/g, "");
        return _ac;
    }
    this.addEventsToLinks = function () {
        var _ak;
        var _al = document.getElementsByTagName('a');
        var _am = new RegExp("\\.(zip|pdf|rar)$", "i");
        for (var i = 0; i < _al.length; i++) {
            _ak = _al[i];
            if (_am.test(_ak.href)) {
                this.addListener(_ak, "mousedown", this.trackDownload, true);
            }
        }
    }
    this.addListener = function (_b, _c, _d, _e) {
        _e = _e || false;
        if (window.addEventListener) {
            _b.addEventListener(_c, _d, _e);
            return true;
        } else if (window.attachEvent) {
            _b.attachEvent('on' + _c, _d);
            return true;
        } else return false;
    }
    this.trackDownload = function (e) {
        var _an = new RegExp("^https?:\/\/(.*)" + location.host.replace(/^www\./i, ""), "i");
        var str = 'download/' + e.parentElement;
    }
    this.onClickHandler = function (_f) {
        var tg = (window.event) ? _f.srcElement : _f.target;
        if (tg.nodeName == 'A') {
            if (tg.href.indexOf(location.host) == -1) {
                var g = escape(tg.href);
                var txt = escape(tg.innerHTML);
                var str = 'clickout/' + g + '/' + txt;
                try {
                    _icstats.trackEvent(str);
                }
                catch (err) {
                }
            }
        }
    }
    this.trackClickout = function () {
        this.addListener(document, 'click', this.onClickHandler);
    }
    this.setHomepageName = function (j) {
        homepageName = j;
    }
    this.setIgnoreNumericCategory = function (j) {
        ignoreNumericCategory = j;
    }
    this.setPageNameFilter = function (_g) {
        pageNameFilter = _g;
    }
    this.trackDownloads = function () {
        this.addEventsToLinks();
    }
    this.setDebug = function (j) {
        debug = j;
    }

    this.getExpirationdate = function (d) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + d);
        return "expires=" + exdate.toUTCString();
    }
}
Array.prototype.contains = function (_b) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == _b) {
            return true;
        }
    }
    return false;
};
