﻿var serverLocation = "www.nbdtech.com/WebTrack/";

var protocol = document.location.protocol == "https:" ? "https://" : "http://";

document.write(
    "<img src=\"" + protocol + serverLocation + "Tracker.aspx?r=" +
    encodeURIComponent(document.referrer) + "\" alt=\"\" />");

var wt_oldOnLoad = window.onload;
window.onload = function() {
    if (wt_oldOnLoad) wt_oldOnLoad();
    initClickTracking();
}

function initClickTracking() {

    if (!document.getElementsByTagName) { return; }
    var anchors = document.getElementsByTagName("a");

    // loop through all anchor tags
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];

        var target = anchor.getAttribute("href");

        if (isExternalOrDownload(target)) {
            anchor.wt_oldOnClick = anchor.onclick;
            anchor.onclick = function() { return handleClick(this); };
        }

    }
}

function handleClick(anchor) {
    var oldOnClick = anchor.getAttribute("wt_oldOnClick");
    var retVal = true;
    if (anchor.getAttribute("rel") == "lightbox") {
        showLightbox(anchor);
        retVal = false;
    }
    else if (oldOnClick) {
        oldOnClick();
    }
    trackLink(anchor.getAttribute("href"));
    return retVal;
}

function isExternalOrDownload(target) {
    if (target) {
        if (target.substr(0, 7).toLowerCase() == "http://") {
            if (target.substr(7, window.location.hostname.length).toLowerCase() !=
                window.location.hostname.toLowerCase()) {
                return true;
            }
        }
        if (target.substr(0, 8).toLowerCase() == "https://") {
            if (target.substr(8, window.location.hostname.length).toLowerCase() !=
                window.location.hostname.toLowerCase()) {
                return true;
            }
        }
        if (/\.(jpg|gif|jpeg|png|zip|exe|doc|pdf)$/i.test(target)) {
            return true;
        }
    }
    return false;
}

function trackLink(url) {
    xmlhttp = null;
    if (window.XMLHttpRequest) {// code for all new browsers
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {// code for IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xmlhttp != null) {

        try {
            xmlhttp.open("GET",
            protocol + serverLocation + "Tracker.aspx?l=" +
            encodeURIComponent(url),
            false);
            xmlhttp.send(null);
        } catch (errVal) {
        }
    }
}