var Utf8 = { 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
	
	UTF8Encode: function(str) {
	  var character = '';
	  var unicode   = '';
	  var string    = '';
	  var i         = 0;
	
	  for (i = 0; i < str.length; i++) {
		character = str.charAt(i);
		unicode   = str.charCodeAt(i);
	
		if (character == ' ') {
		  string += '+';
		} else {
		  if (unicode == 0x2a || unicode == 0x2d || unicode == 0x2e || unicode == 0x5f || ((unicode >= 0x30) && (unicode <= 0x39)) || ((unicode >= 0x41) && (unicode <= 0x5a)) || ((unicode >= 0x61) && (unicode <= 0x7a))) {
			string = string + character;
		  } else {
			if ((unicode >= 0x0) && (unicode <= 0x7f)) {
			  character   = '0' + unicode.toString(16);
			  string += '%' + character.substr(character.length - 2);
			} else if (unicode > 0x1fffff) {
			  string += '%' + (oxf0 + ((unicode & 0x1c0000) >> 18)).toString(16);
			  string += '%' + (0x80 + ((unicode & 0x3f000) >> 12)).toString(16);
			  string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
			  string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
			} else if (unicode > 0x7ff) {
			  string += '%' + (0xe0 + ((unicode & 0xf000) >> 12)).toString(16);
			  string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
			  string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
			} else {
			  string += '%' + (0xc0 + ((unicode & 0x7c0) >> 6)).toString(16);
			  string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
			}
		  }
		}
	  }
	  return string;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		} 
		return string;
	} 
}
