////encode
Base32={
    ValidChars:"QAZ2WSX3EDC4RFV5TGB6YHN7UJM8K9LP"
    ,
    ToBase32String:function(str) 
    {
	    var output="";
	    var index;
	    var hi = 5;
	    var currentByte = 0;

	    while (currentByte < str.length) 
	    {
		    if (hi > 8) 
		    {
			    index = (str.charCodeAt(currentByte++) >> (hi - 5))&255;
			    if (currentByte != str.length) 
			    {
				    index = (((((str.charCodeAt(currentByte) << (16 - hi))&255) >> 3) | index)&255);
			    }

			    hi -= 3;
		    } 
		    else if(hi == 8) 
		    { 
			    index = ((str.charCodeAt(currentByte++) >> 3)&255);
			    hi -= 3; 
		    } 
		    else 
		    {
			    index = (((str.charCodeAt(currentByte) << (8 - hi))&255) >> 3);
			    hi += 5;
		    }

		    output=output+this.ValidChars.charAt(index);
	    }

	    return output;
    }
    ,
    FromBase32String:function(str) 
    {
	    var numBytes = parseInt(str.length * 5 / 8);
	    var bytes = new Array(numBytes);
	    var output="";
    	
	    str = str.toUpperCase();

	    var bit_buffer;
	    var currentCharIndex;
	    var bits_in_buffer;

	    if (str.length < 3) 
	    {
		    bytes[0] = 255&(this.ValidChars.indexOf(str.charAt(0)) | this.ValidChars.indexOf(str.charAt(1)) << 5);
		    return output=String.fromCharCode(bytes);
	    }

	    bit_buffer = (this.ValidChars.indexOf(str.charAt(0)) | this.ValidChars.indexOf(str.charAt(1)) << 5);
	    bits_in_buffer = 10;
	    currentCharIndex = 2;
	    for (var i = 0; i < bytes.length; i++) 
	    {
		    bytes[i] = 255 & bit_buffer;
		    bit_buffer >>= 8;
		    bits_in_buffer -= 8;
		    while (bits_in_buffer < 8 && currentCharIndex < str.length) 
		    {
			    bit_buffer |= this.ValidChars.indexOf(str.charAt(currentCharIndex++)) << bits_in_buffer;
			    bits_in_buffer += 5;
		    }
		    output+=String.fromCharCode(bytes[i]);
	    }

	    return output;
    }
};

