﻿var Base64 = {

    encode_base64: function (T) {
        var L = T.length, t = "", b0 = 0, b1, b2, i, p = (3 - L % 3) % 3;
        var code = 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", "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/", "=");

        for (i = 0; i + 2 < L; i += 3) {
            b0 = T.charCodeAt(i); b1 = T.charCodeAt(i + 1); b2 = T.charCodeAt(i + 2);
            t += code[(b0 & 252) / 4]
              + code[(b0 & 3) * 16 + (b1 & 240) / 16]
        	  + code[(b1 & 15) * 4 + (b2 & 192) / 64]
              + code[b2 & 63];
        }
        if (i < L) {
            b0 = (i < L) ? T.charCodeAt(i) : 0; i++;
            b1 = (i < L) ? T.charCodeAt(i) : 0; i++;
            b2 = (i < L) ? T.charCodeAt(i) : 0;
            t += code[(b0 & 252) / 4] + code[(b0 & 3) * 16 + (b1 & 240) / 16]
      + code[(b1 & 15) * 4 + (b2 & 192) / 64] + code[b2 & 63];
        }
        L = t.length;
        t = t.substr(0, L - p) + "==".substr(0, p);
        return t
    },

    decode_base64: function (tt) {
        var edoc = new Array(256);
        var code = 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", "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/", "=");

        for (i = 0; i < 256; i++) edoc[i] = 255; edoc[61] = 0;
        for (i = 0; i < 64; i++) edoc[code[i].charCodeAt(0)] = i;

        var L = tt.length, t = "", b0 = 0, b1, b2, b3, i = -1, j = 0, p = (4 - L % 4) % 4;
        T = "";
        while (i < L - 4) {
            do { i++; b0 = edoc[tt.charCodeAt(i)]; } while (b0 > 63);
            do { i++; b1 = edoc[tt.charCodeAt(i)]; } while (b1 > 63);
            do { i++; b2 = edoc[tt.charCodeAt(i)]; } while (b2 > 63);
            do { i++; b3 = edoc[tt.charCodeAt(i)]; } while (b3 > 63);
            t += String.fromCharCode(b0 * 4 + (b1 & 48) / 16)
		 + String.fromCharCode((b1 & 15) * 16 + (b2 & 60) / 4)
		 + String.fromCharCode((b2 & 3) * 64 + b3);
        }
        b0 = 0; b1 = 0; b2 = 0; b3 = 0;
        if (i < L - 1) {
            do { i++; b0 = edoc[tt.charCodeAt(i)]; } while (b0 > 63);
            if (i < L - 1) {
                do { i++; b1 = edoc[tt.charCodeAt(i)]; } while (b1 > 63);
                if (i < L - 1) {
                    do { i++; b2 = edoc[tt.charCodeAt(i)]; } while (b2 > 63);
                    if (i < L - 1) {
                        do { i++; b3 = edoc[tt.charCodeAt(i)]; } while (b3 > 63);
                    }
                }
            }
        }
        t += String.fromCharCode(b0 * 4 + (b1 & 48) / 16)
		 + String.fromCharCode((b1 & 15) * 16 + (b2 & 60) / 4)
		 + String.fromCharCode((b2 & 3) * 64 + b3);
        return t;
    }
}
