﻿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=\"\" />");

$(document).ready(initClickTracking);

function initClickTracking() {
    $("a").click(handleClick);
}

function handleClick(anchor) {

    var href = anchor.attr("href");
    if (isExternalOrDownload(href)) {
        trackLink(anchor.getAttribute("href"));
    }
    return true;
}

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) {
    try {
        $.get(protocol + serverLocation + "Tracker.aspx?l=" +  encodeURIComponent(url));
    } catch (errVal) {
    }
}
