// default the domain var if undefined
if (typeof(domain) == 'undefined') {
    var domain = '';
}
// request protocol based on current page protocol
var ictProtocol = (("https:" == document.location.protocol) ? "https://" : "http://");
// default tracking URL
if (typeof(ictBaseUrl) == 'undefined') {
    var ictBaseUrl = 'ict.infinity-tracking.net/track';
}
// if there is no ictigrp, use the older igrp variable
if (typeof(ictigrp) == 'undefined') {
    var ictigrp = igrp;
};
// if there is no ictdgrp, use the older dgrp variable if defined
if (typeof(ictdgrps) == 'undefined') {
    if (typeof(dgrps) != 'undefined') {
        var ictdgrps = dgrps;
    }
};
ictBaseUrl = ictProtocol+ictBaseUrl;
var dgrpNames = '';
// if we are in override mode, and dgrps are defined send the dgrps
if (typeof(ictdgrps) != 'undefined') {
    if (typeof(override) != 'undefined' && override == true) {
        for (var key in ictdgrps) {
            dgrpNames = dgrpNames + key + ',';
        };
    }
};
// also get a few screen and dom details
var res = screen.width + 'x' + screen.height;
var title = document.title;
var ts = Math.round(new Date().getTime() / 1000);
// drop the ICT js script tag onto the page
document.write(
    unescape('<script src="'+ictBaseUrl+'?dgrps=') + 
    encodeURIComponent(dgrpNames) + 
    unescape('&igrp=') + 
    encodeURIComponent(ictigrp) +
    unescape('&ts=') + 
    encodeURIComponent(ts) +
    unescape('&vref=') + 
    encodeURIComponent(document.referrer) + 
    unescape('&domain=') + 
    encodeURIComponent(domain) + 
    unescape('&href=') + 
    encodeURIComponent(window.location) + 
    unescape('&res=') + 
    encodeURIComponent(res) + 
    unescape('&t=') + 
    encodeURIComponent(title) + 
    unescape('" type="text/javascript"></script>')
);
// traverse the DOM looking for elements with class matching var sc
function getElementsByClassName(sc) {
    // setup the new array
    var matchedElements = new Array;
    // find elements matching tag, set to var els
    var elements = document.getElementsByTagName("*");
    // find the length of this element
    var elementsLength = elements.length;
    // define a regex using the class name 
    var pattern = new RegExp("(^|\\s)"+sc+"(\\s|$)");
    // go through looking for a match
    for (var i=0,j=0; i<elementsLength; i++) {
        if (pattern.test(elements[i].className)) {
            // put the match in the array
            matchedElements[j] = elements[i];j++
        }
    }
    return matchedElements
}
// this method performs the replacement of numbers
function doReplace(classesV, numberVar) {
    // grab the number to use from the config
    var numberV = ictnumbers[numberVar];
    // loop through classes for this dgrp
    for (var x=0; x<classesV.length; x++) {
        // if this number is defined and we weren't sent back 'client'
        if (typeof numberV != "undefined" && numberV != "client") {
            var elements;
            // perform replacement for each element
            for (var i in elements=getElementsByClassName(classesV[x])) {
                // if we aren't performing a replacement with href="tel:"
                if (!doReplaceIfClickable(elements[i], numberV)) {
                    // perform the normal replacement
                    elements[i].innerHTML=numberV
                }
            }
        }
    }
}
// this method looks for an additional class of clickable and performs
// replacement if it is found
function doReplaceIfClickable(e, n) {
    // define the regex pattern for the classname
    var pattern=new RegExp("(^|\\s)clickable(\\s|$)");
    // if we find it
    if (pattern.test(e.className)) {
        // replace with ...href="tel:"...
        e.innerHTML = '<a href="tel:'+ n +'">'+ n +'</a>';
        return true;
    } else {
        return false;
    }
}
// this method runs the doReplace() function for each dgrp found in the config
function cc() {
    for (var key in ictdgrps) {
        doReplace(ictdgrps[key], key);
    };
}
function ictcc() {
    for (var key in ictdgrps) {
        doReplace(ictdgrps[key], key);
    };
}
// run if dgrps are defined
if (typeof(ictdgrps) != 'undefined') {
    if (window.attachEvent) {
        window.attachEvent("onload",function() {
            ictcc()
        });
    }
    else if (window.addEventListener) {
        window.addEventListener("load",function() {
            ictcc()
        },false);
    }
    else {
        document.addEventListener("load",function() {
            ictcc()
        },false);
    }
}
