﻿/** 
* @projectDescription  Encodings Library
*
* @author Ryan Estes  http://www.codeapedia.com
* @version 0.2 
*/

var base64 = {
    _CHARS : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    
    /**
    * Base64 encodes specified string
    * @return {String} 
    */
    encode : function (pValue) {
	    var i = 0;
	    var str = "";
	    var c1, c2, c3, ce1, ce2, ce3, ce4;
	    pValue = utf8.encode(pValue);
	    while (i < pValue.length) 
	    {
		    c1 = pValue.charCodeAt(i++);
		    c2 = pValue.charCodeAt(i++);
		    c3 = pValue.charCodeAt(i++);
		    ce1 = c1 >> 2;
		    ce2 = ((c1 & 3) << 4) | (c2 >> 4);
		    ce3 = ((c2 & 15) << 2) | (c3 >> 6);
		    ce4 = c3 & 63;
		    if (isNaN(c2)) 
		    {
			    ce3 = ce4 = 64;
		    } 
		    else if (isNaN(c3)) 
		    {
			    ce4 = 64;
		    }
		    str = str +
		    this._CHARS.charAt(ce1) + this._CHARS.charAt(ce2) +
		    this._CHARS.charAt(ce3) + this._CHARS.charAt(ce4);
	    }
	    return str;
    },

    /**
    * Base64 decodes specified string
    * @return {String} 
    */
    decode : function (pValue) {
        var i = 0;
        var str = "";
	    var c1, c2, c3;
	    var ce1, ce2, ce3, ce4;
	    var pValue = pValue.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	    while (i < pValue.length) 
	    {
		    ce1 = this._CHARS.indexOf(pValue.charAt(i++));
		    ce2 = this._CHARS.indexOf(pValue.charAt(i++));
		    ce3 = this._CHARS.indexOf(pValue.charAt(i++));
		    ce4 = this._CHARS.indexOf(pValue.charAt(i++));
		    c1 = (ce1 << 2) | (ce2 >> 4);
		    c2 = ((ce2 & 15) << 4) | (ce3 >> 2);
		    c3 = ((ce3 & 3) << 6) | ce4;
		    str = str + String.fromCharCode(c1);
		    if (ce3 != 64) 
		    {
			    str = str + String.fromCharCode(c2);
		    }
		    if (ce4 != 64) 
		    {
			    str = str + String.fromCharCode(c3);
		    }
	    }
	    return utf8.decode(str);
    }
}

var utf8 = {
    /**
    * UTF8 encodes specified string
    * @return {String} 
    */
    encode : function (pValue) {
        var str = "";
	    pValue = pValue.replace(/\r\n/g,"\n");
	    for (var n = 0; n < pValue.length; n++) 
	    {
		    var c = pValue.charCodeAt(n);
		    if (c < 128) 
		    {
			    str += String.fromCharCode(c);
		    }
		    else if((c > 127) && (c < 2048)) 
		    {
			    str += String.fromCharCode((c >> 6) | 192);
			    str += String.fromCharCode((c & 63) | 128);
		    }
		    else 
		    {
			    str += String.fromCharCode((c >> 12) | 224);
			    str += String.fromCharCode(((c >> 6) & 63) | 128);
			    str += String.fromCharCode((c & 63) | 128);
		    }
	    }
	    return str;
    },

   decode : function (pValue) {
        var i = 0;
	    var str = "";
	    var c = c1 = c2 = 0;
	    while ( i < pValue.length ) 
	    {
		    c = pValue.charCodeAt(i);
		    if (c < 128) 
		    {
			    str += String.fromCharCode(c);
			    i++;
		    }
		    else if((c > 191) && (c < 224)) 
		    {
			    c2 = pValue.charCodeAt(i+1);
			    str += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			    i += 2;
		    }
		    else 
		    {
			    c2 = pValue.charCodeAt(i+1);
			    c3 = pValue.charCodeAt(i+2);
			    str += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			    i += 3;
		    }
	    }
	    return str;
    }
}