var ElserInteractive = {
    Tracker: function() { }
};

ElserInteractive.Tracker.prototype = {

    SCRIPT_VERSION: '2.3.6.0',
    SESSION_TIMEOUT: 60,

    cmp: '',
    vid: '',
    sid: '',
    turl: 'http://tracker.elserinteractive.com/Track.ashx',
    ptitle: '',
    purl: '',
    uagent: '',
    swidth: '',
    sheight: '',
    refu: '',
    refc: '',
    reft: '',
    c1: '',
    c2: '',
    c3: '',
    c4: '',
    c5: '',
    c6: '',
    c7: '',
    c8: '',
    c9: '',
    c10: '',
    images: [],

    /**********************************************************************
    Retrieves a parameter value from the query string
    **********************************************************************/
    getQuery: function(param) {
        var p, q, f, n, v;
        p = param.toLowerCase();
        q = window.location.search;
        q = q.substring(1).split('&');
        for (var i = 0; i < q.length; i++) {
            f = q[i].split('=');
            n = f[0].toLowerCase();
            v = f[1];
            if (n == param) {
                return v;
            }
        }
        return null;
    },

    /**********************************************************************
    Parses a url for the root domain
    **********************************************************************/
    getRootDomain: function(hostname) {
        if (hostname.substring(0, 4) == 'www.') {
            return hostname.substring(4);
        } else {
            return hostname;
        }
    },

    /**********************************************************************
    Trims a string of leading and trailing whitespace
    **********************************************************************/
    trim: function(text) {
        text = text.replace(/^\s+/g, ''); // strip leading
        text = text.replace(/\s+$/g, ''); // strip trailing
        return text;
    },

    /**********************************************************************
    Strips all non-acceptable characters from a string
    **********************************************************************/
    clean: function(text) {
        var a, c, o;
        a = new Array(
		    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
		    'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
		    'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
		    'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
		    'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
		    '3', '4', '5', '6', '7', '8', '9', ' ', '!', '@', '#',
		    '$', '%', '^', '&', '*', '(', ')', '-', '=', '+', '/',
		    '[', ']', '?', '\\', '|', '<', '>', '~', '.', ',');
        o = '';
        for (var i = 0; i < text.length; i++) {
            c = text.charAt(i);
            for (var x = 0; x < a.length; x++) {
                if (c == a[x]) o += c;
            }
        }
        return o;
    },

    /**********************************************************************
    Sets a cookie in the client browser
    **********************************************************************/
    setCookie: function(name, value, mins) {
        var n, v, m, d, dm, e, c, p;
        dm = window.location.hostname;
        dm = this.getRootDomain(dm);
        n = name.toLowerCase();
        n += '=' + value;
        p = ';path=/';
        m = mins;
        e = '';
        d = new Date();
        d.setTime(d.getTime() + (m * 60000));
        e = (mins) ? ';expires=' + d.toGMTString() : '';
        dm = (dm.indexOf('.') > -1) ? dm : null;
        dm = (dm) ? ';domain=.' + dm : '';
        c = n + e + p + dm;
        document.cookie = c;
    },

    /**********************************************************************
    Gets a cookie from the client browser
    **********************************************************************/
    getCookie: function(name) {
        var p, a, c, f;
        p = name.toLowerCase();
        a = document.cookie.split(';');
        for (var i = 0; i < a.length; i++) {
            c = this.trim(a[i]);
            f = c.split('=');
            if (f[0].toLowerCase() == p) {
                return f[1];
            }
        }
        return null;
    },

    /**********************************************************************
    Deletes a cookie from the client browser
    **********************************************************************/
    delCookie: function(name) {
        this.setCookie(name, '', -1);
    },

    /**********************************************************************
    Creates a new GUID
    **********************************************************************/
    newGuid: function() {
        var g = '';
        for (var i = 0; i < 32; i++) {
            g += Math.floor(Math.random() * 0xF).toString(0xF) +
		    (i == 8 || i == 12 || i == 16 || i == 20 ? '-' : '');
        }
        return g;
    },

    /**********************************************************************
    Tests whether cookies are enabled
    **********************************************************************/
    cookiesEnabled: function() {
        var c;
        c = this.newGuid();
        this.setCookie('_ect', c, 1);
        c = this.getCookie('_ect');
        return (c != null);
    },

    /**********************************************************************
    Determines whether a string is empty
    **********************************************************************/
    isEmpty: function(text) {
        if (!text || text == '') {
            return true;
        } else {
            return false;
        }
    },

    /**********************************************************************
    Redirects the browser to the specified url
    **********************************************************************/
    doRedirect: function(url) {
        if (this.rpopup) {
            window.open(this.rtarget);
        } else {
            window.location = this.rtarget;
        }
    },

    /**********************************************************************
    Initializes all required tracker variables
    **********************************************************************/
    initTracker: function() {
        var d, lh;

        // This thing only works if cookies are 
        // enabled. If not, skip everything.
        if (this.cookiesEnabled()) {

            // Does a visitor ID cookie already exist?
            // If so, grab it; otherwise create a new
            // one and store it in a cookie.
            if (this.getCookie('wa_visitor')) {
                this.vid = this.getCookie('wa_visitor');
            } else {
                this.vid = this.newGuid();
                this.setCookie('wa_visitor', this.vid, 9999999);
            }

            // In order to prevent cookie expiration
            // problems perpetrated by the user's browser,
            // we'll set our own cookie to determine the
            // last time we reset the session ID. If this 
            // cookie has not been modified within the 
            // specified session time out, then we'll
            // manually force a session timeout.
            if (this.getCookie('wa_lastaccess')) {
                d = new Date();
                lh = parseInt(this.getCookie('wa_lastaccess'), 10);
                if (d.getTime() - lh >= (60000 * this.SESSION_TIMEOUT)) {
                    this.delCookie('wa_session');
                }
            }

            // Does a session ID cookie already exist?
            // If so, grab it; otherwise create a new
            // one and store it in a cookie.
            if (this.getCookie('wa_session')) {
                this.sid = this.getCookie('wa_session');
                this.setCookie('wa_session', this.sid, this.SESSION_TIMEOUT);
            } else {
                this.sid = this.newGuid();
                this.setCookie('wa_session', this.sid, this.SESSION_TIMEOUT);
            }

            // Setup default variable values
            this.ptitle = this.trim(document.title);
            this.purl = window.location.href;
            this.uagent = navigator.userAgent;
            this.swidth = screen.width;
            this.sheight = screen.height;
            this.refu = document.referrer;
            this.refc = this.getQuery('refc');
            this.reft = this.getQuery('reft');
            this.c1 = this.getQuery('c1');
            this.c2 = this.getQuery('c2');
            this.c3 = this.getQuery('c3');
            this.c4 = this.getQuery('c4');
            this.c5 = this.getQuery('c5');
            this.c6 = this.getQuery('c6');
            this.c7 = this.getQuery('c7');
            this.c8 = this.getQuery('c8');
            this.c9 = this.getQuery('c9');
            this.c10 = this.getQuery('c10');
        }
    },

    /**********************************************************************
    Tracks a page view
    **********************************************************************/
    trackPage: function(url, title, clickthru, redirect, popup) {
        var d, img, tu, idx, vid, sid, cmp, pt, pu, ua, sw, sh,
		    ru, rc, rt, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, ct;

        this.initTracker();

        // If a url was passed to the function, use 
        // that one instead of the current page's url.
        if (url) {
            this.purl = this.trim(url);
        }

        // If a page title was passed to the function,
        // use that one instead of the current page title.
        if (title) {
            this.ptitle = this.trim(title);
        }

        // Store a session timestamp to enable
        // manual cookie expiration if necessary.
        // See initTracker() function for more info.
        d = new Date();
        this.setCookie('wa_lastaccess', d.getTime(), 15);

        // Build a list of query string params that 
        // will be passed to the tracking beacon.
        idx = '?idx=' + new Date().getTime();
        vid = (this.isEmpty(this.vid)) ? '' : '&vid=' + escape(this.vid);
        sid = (this.isEmpty(this.sid)) ? '' : '&sid=' + escape(this.sid);
        cmp = (this.isEmpty(this.cmp)) ? '&cmp=Default' : '&cmp=' + escape(this.cmp);
        pt = (this.isEmpty(this.ptitle)) ? '' : '&title=' + escape(this.ptitle);
        pu = (this.isEmpty(this.purl)) ? '' : '&url=' + escape(this.purl);
        ua = (this.isEmpty(this.uagent)) ? '' : '&ua=' + escape(this.uagent);
        sw = (this.isEmpty(this.swidth)) ? '' : '&sw=' + escape(this.swidth);
        sh = (this.isEmpty(this.sheight)) ? '' : '&sh=' + escape(this.sheight);
        ru = (this.isEmpty(this.refu)) ? '' : '&refu=' + escape(this.refu);
        rc = (this.isEmpty(this.refc)) ? '' : '&refc=' + escape(this.refc);
        rt = (this.isEmpty(this.reft)) ? '' : '&reft=' + escape(this.reft);
        c1 = (this.isEmpty(this.c1)) ? '' : '&c01=' + escape(this.c1);
        c2 = (this.isEmpty(this.c2)) ? '' : '&c02=' + escape(this.c2);
        c3 = (this.isEmpty(this.c3)) ? '' : '&c03=' + escape(this.c3);
        c4 = (this.isEmpty(this.c4)) ? '' : '&c04=' + escape(this.c4);
        c5 = (this.isEmpty(this.c5)) ? '' : '&c05=' + escape(this.c5);
        c6 = (this.isEmpty(this.c6)) ? '' : '&c06=' + escape(this.c6);
        c7 = (this.isEmpty(this.c7)) ? '' : '&c07=' + escape(this.c7);
        c8 = (this.isEmpty(this.c8)) ? '' : '&c08=' + escape(this.c8);
        c9 = (this.isEmpty(this.c9)) ? '' : '&c09=' + escape(this.c9);
        c10 = (this.isEmpty(this.c10)) ? '' : '&c10=' + escape(this.c10);
        ct = (clickthru) ? '&ct=1' : '';

        // Assemble the url for the tracking beacon
        tu = this.turl + idx + vid + sid + cmp + pt + pu + ua +
		     sw + sh + ru + rc + rt + c1 + c2 + c3 + c4 + c5 +
		     c6 + c7 + c8 + c9 + c10 + ct;

        // Create the beacon
        if (document.images) {
            var pos = this.images.length;
            this.images[pos] = new Image();
            this.images[pos].src = tu;
        } else {
            document.writeln('<img src="' + tu + '" alt="" width="1" height="1">');
        }
    }
}