var ns = {};


// returns an array of DOM objects with class className
// className - class property of tags (eg: mandatory)
// tagName - return only tags of this type (eg: input), can be null (for all tags)
ns.getElementsByClass = function (className, tagName) {
    var matches = [],    // array for returned DOM nodes
        where = document,// search the whole document
        i = 0,           // loop counter
        j = 0,           // found element counter
        elements = null, // all descendants of where
        count = 0;       // number of the descendants

    if (!tagName) {
        tagName = '*';
    }
    
    elements = where.getElementsByTagName(tagName);
    count = elements.length;
    for (i = 0; i < count; i += 1) {
        if (-1 !== elements[i].className.indexOf(className)) {
            matches[j] = elements[i];
            j += 1;
        }
    }
    return matches;
};


/**
 * this function replaces the mailto links which have the form (href tag):
 * nospam:account__AT__sub__DOT__domain__DOT__tld
 * and the id: nospam_X (or class = 'nospam')
 * where X is a number (starting from 1 and continous)
 * innerHTML will be set to the email address 
 */
ns.antiSpam = function () {
    var i = 0,
        a = null,
        old = '',
        newHref = '',
        list = ns.getElementsByClass('nospam', 'A'),
        i = 0,
        c = 0;

    i = 0;
    while (true) {
        i += 1;
        a = document.getElementById('nospam_' + i);
        if (!a) {
            break;
        }
        list.push(a);
    }
    c = list.length;
    for (i = 0; i < c; i += 1) {
        a = list[i];
        // replace link's href attribute
        old = a.href;
        newHref = old.replace('nospam:', '');
        newHref = newHref.replace(/__AT__/, '@');
        newHref = newHref.replace(/__DOT__/g, '.'); // g --> replace all occurances
        a.href = 'mailto:' + newHref;
        // replace the link text too
        a.innerHTML = newHref;
    }
}

// make the function run when the page is loaded
if (window.addEventListener) {
    window.addEventListener("load", ns.antiSpam, false);
} else {
    if (window.attachEvent) {
        window.attachEvent("onload", ns.antiSpam);
    } else {
        // try to run now
        ns.antiSpam();
    }
}


