Base64 in Javascript

Decided to play around with writing my own base64 in javascript, as a piece to a larger project I’m considering playing around with.

function b64e(str) {

        var charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

        var str_pos = 0;
        var str_len = str.length;

        var numbits=0;
        var curval=0;

        var b64str = "";
        var done = false;

        while (done == false){
                // Not enough bits, get more
                if (numbits < 6) {
                        var nextbyte = str.charCodeAt(str_pos++);
                        curval = (curval << 8) | nextbyte;
                        numbits += 8;
                }

                sixbits = curval >> (numbits - 6);
                curval = curval - (sixbits << numbits - 6);
                numbits -= 6;

                b64str += charset[sixbits];

                if (str_pos >= str_len && numbits < 6){
                        done = true; 
                }
        }

        //handle extra bits now
        if (numbits > 0){
                sixbits = curval << (6-numbits)
                numbits -= 6;
                b64str += charset[sixbits];
        }

        b64str += Array((numbits/-2) + 1).join('=');

        return b64str;

}