/****************************************************
decode.js
---------
****************************************************/
var encodeList, decodeList
// Load the lookup arrays once
function initLookups()
{
    encodeList = new Array();
    decodeList = new Array();

    var i;

    for (i = 0; i < 26; i++)
        encodeList[encodeList.length] = String.fromCharCode(65 + i);

    for (i = 0; i < 26; i++)
        encodeList[encodeList.length] = String.fromCharCode(97 + i);

    for (i = 0; i < 10; i++)
        encodeList[encodeList.length] = String.fromCharCode(48 + i);

    encodeList[encodeList.length] = "+";
    encodeList[encodeList.length] = "/";

    for (i = 0; i < 128; i++)
        decodeList[decodeList.length] = -1;

    for (i = 0; i < 64; i++)
        decodeList[encodeList[i].charCodeAt(0)] = i;

}

function decode(str)
{
    var c=0, d=0, e=0, f=0, i=0, n=0;
    var input = str.split("");
    var output = "";
    var ptr = 0;
    do
    {
        f = input[ptr++].charCodeAt(0);
        i = decodeList[f];
        if ( f >= 0 && f < 128 && i != -1 )
        {
            if ( n % 4 == 0 )
            {
                c = i << 2;
            }
            else if ( n % 4 == 1 )
            {
                c = c | ( i >> 4 );
                d = ( i & 0x0000000F ) << 4;
            }
            else if ( n % 4 == 2 )
            {
                d = d | ( i >> 2 );
                e = ( i & 0x00000003 ) << 6;
            }
            else
            {
                e = e | i;
            }
            n++;
            if ( n % 4 == 0 )
            {
                output += String.fromCharCode(c) + 
                          String.fromCharCode(d) + 
                          String.fromCharCode(e)
            }
        }
    }
    while (typeof input[ptr] != "undefined");
    output += (n % 4 == 3) ? String.fromCharCode(c) + String.fromCharCode(d) : ((n % 4 == 2) ? String.fromCharCode(c) : "");
    return output;
}

// Self-initialize the global variables
initLookups();
