var domain = "bckidsmentalhealth.org";

var letterGroup = new Array("a", "b", "c", "d", "e", "f",

                "g", "h", "i", "j", "k", "l",

                "m", "n", "o", "p", "q", "r",

                "s", "t", "u", "v", "w", "x",

                "y", "z", " ");



stringCheck = "";

for (i = 0; i < letterGroup.length; i++) {
    stringCheck += letterGroup[i];
}

function caesarCipher(plainText, shift)
{
    var cipherText  = "";
    plainText   = plainText.toLowerCase();

    for (i = 0; i < plainText.length; i++) {
        var currentText = stringCheck.indexOf(plainText.charAt(i));
        if(currentText != -1) {
            if ((shift + currentText) < (letterGroup.length)) {
                cipherText += letterGroup[(shift + currentText)];
            }
            else {
                cipherText += letterGroup[(shift + currentText) % letterGroup.length];
            }
        }
    }

    return cipherText;
}


function caesarDecipher(cipherText, shift)
{
    var plainText   = '';
    cipherText  = cipherText.toLowerCase();

    for (i = 0; i < cipherText.length; i++) {
        var currentText = stringCheck.indexOf(cipherText.charAt(i));
        if(currentText != -1) {
            if ((currentText - shift) >= 0) {
                plainText += letterGroup[currentText - shift];
            }
            else {
                plainText += letterGroup[(letterGroup.length + currentText)  - shift];
            }
        }
    }

    return plainText;
}

function mailFail()
{
    anchors = document.getElementsByTagName("span");
    for (x = 0; x < anchors.length; ++x) {
        if (anchors[x].className.indexOf("mailfail") != -1) {
            arr = anchors[x].className.split("|");
            dm = addtxt = '';
            if (arr.length == 3)
            {
              dm = domain;
              addtxt = arr[2];
            }
            else if (arr.length == 4)
            {
              dm = arr[2];
              addtxt = arr[3];
            }
            email = caesarDecipher(arr[1],3) + '@' + dm;
            anchors[x].innerHTML = addtxt + " <a href=\"mailto:" + email + "\">" + email + "</a>";
        }
    }
}

