﻿// reverse a string http://www.bytemycode.com/snippets/snippet/400/
String.prototype.reverse = function () {
    splitext = this.split("");
    revertext = splitext.reverse();
    reversed = revertext.join("");
    return reversed;
}

var spockBlam = {};
spockBlam.Email = {};

// This function obfuscates and email address, this can be called from the console to 
// generate an encoded email.  No need to call this from website displaying the email
// address to the user.
spockBlam.Email.encMail = function (sAddr) {

    var s = "";
    var c;
    var i;
    var retVal = "";

    for (i = 0; i < sAddr.length; ++i) {

        // convert character to number
        c = sAddr.charAt(i).charCodeAt();

        // add magic number
        c += 0x6f;

        // convert to hex and pad
        s = c.toString(16);
        if (s.length == 1) {
            s = "0" + s;
        }

        // add to return value, but reverse the nibbles
        retVal += s.reverse();
    }

    return retVal;
};

// unobfuscate an email address, this is what we use in our web site for the email link.
spockBlam.Email.decMail = function (sAddr) {

    var s;
    var n;
    var retVal = "";

    // parse hex encoded string
    for (i = 0; i < sAddr.length; ) {

        // the nibbles have been reversed in encMail
        s = sAddr.charAt(i + 1) + sAddr.charAt(i);
        n = parseInt(s, 16);

        // subtract magic number added in encMail
        n -= 0x6f;

        // add to result
        retVal += String.fromCharCode(n);
        i += 2;
    }

    // return result
    return retVal;
}

// this actually opens up the email client to send the email
spockBlam.Email.mailSend = function (sAddr) {
    location.href = this.decMail(sAddr);
}


