// Javascript for affiliate data persistence
// specifically to append btags to all links to the betonline website

var Adp = {

    getBtag: function() {
        // split url and retrieve btag
        var aPath = window.location.href.split("?");
        var sbtag = aPath[aPath.length - 1];
        return sbtag;
    },

    persistAffiliateData: function() {
        // get the affiliate data from the url
        var bTag = Adp.getBtag();
        if (!bTag) { return false; }

        // get all the links on the page
        var aLinks = document.getElementsByTagName("a");
        if (!aLinks) { return false; }

        // add the affiliate data to the links to betonline
        for (var i = 0; i < aLinks.length; i++) {
            if (aLinks[i].href.indexOf("betonline") > 0 || aLinks[i].href.indexOf("localhost") > 0) {
                aLinks[i].href = aLinks[i].href + "?" + bTag;
            }
        }
    }
};

function addLoadEvent(func){
    // appends unlimited functions to the onload event 
    var oldonload = window.onload;
    if(typeof window.onload != 'function'){
        window.onload = func;
    }else{
        window.onload = function(){
            oldonload();
            func();
        }
    }
}

addLoadEvent(Adp.persistAffiliateData);


