mootools href mail

by sthag

HTML

<p><a href="">Should not change.</a></p>
<p><a id="noNick" href="">Should not be empty.</a></p>
<p><a id="nick" href="">Should be changed.</a></p>
<p><a id="atNick" href="">Shows (at) instead of @</a></p>
<p><a class="multiNick" href=""></a></p>
<p><a class="multiNick" href=""></a></p>

CSS

p {
    margin: 10px;
}

JavaScript

// create emails
function composeMail(tag, name, prov, suffix, text) {
    var trigger = tag.indexOf(".");
    var mailString = name + '@' + prov + '.' + suffix;
    var textString = mailString.replace(/@/g, "(at)");
    if (trigger == -1) {
        $(tag).setProperty("href", "mailto:" + mailString + "?subject=" + "Betreff");
        if (!text) {
            text = mailString;
        } else if (text == "at") {
            text = textString;
        }
        $(tag).set('text', text);
    } else {
        $$(tag).each(function(item) {
            item.setProperty("href", "mailto:" + mailString + "?subject=" + "Betreff");
            if (!text) {
                text = mailString;
            } else if (text == "at") {
                text = textString;
            }
            item.set('text', text);
        });
    }
}

//DOM READY
window.addEvent('domready', function() {

    //change adresses
    composeMail("nick", "username", "service", "tld", "Changed!");
    composeMail("noNick", "username", "service", "tld", "");
    composeMail("atNick", "username", "service", "tld", "at");
    composeMail(".multiNick", "name", "service", "tld", "Multiple links");
});