<script language="javascript">
var Utf8Encoder = {
// URL utf-8 encoding
encode : function (str) {
return escape(this._utf8_encode(str));
},
// URL utf-8 decoding
decode : function (encoded_str) {
return this._utf8_decode(unescape(encoded_str));
},
// UTF-8 encoding
_utf8_encode : function (str) {
str = str.replace(/\r\n/g,"\n");
var encoded_str = "";
for (var n = 0; n < str.length; n++) {
var c = str.charCodeAt(n);
if (c < 128) {
encoded_str += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
encoded_str += String.fromCharCode((c >> 6) | 192);
encoded_str += String.fromCharCode((c & 63) | 128);
}
else {
encoded_str += String.fromCharCode((c >> 12) | 224);
encoded_str += String.fromCharCode(((c >> 6) & 63) | 128);
encoded_str += String.fromCharCode((c & 63) | 128);
}
}
return encoded_str;
},
// UTF-8 decoding
_utf8_decode : function (encoded_str) {
var str = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < encoded_str.length ) {
c = encoded_str.charCodeAt(i);
if (c < 128) {
str += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = encoded_str.charCodeAt(i+1);
str += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = encoded_str.charCodeAt(i+1);
c3 = encoded_str.charCodeAt(i+2);
str += String.fromCharCode(((c & 15) << 12) |
((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return str;
}
}
</script>