src/pyams_skin/resources/js/myams-utf8.js
changeset 566 a1707c607eec
parent 565 318533413200
child 567 bca1726b1d85
equal deleted inserted replaced
565:318533413200 566:a1707c607eec
     1 /**
       
     2  * MyAMS UTF-8 features
       
     3  */
       
     4 (function($, globals) {
       
     5 
       
     6 	$.UTF8 = {
       
     7 
       
     8 		// public method for url encoding
       
     9 		encode : function (string) {
       
    10 			string = string.replace(/\r\n/g,"\n");
       
    11 			var utftext = "";
       
    12 
       
    13 			for (var n = 0; n < string.length; n++) {
       
    14 
       
    15 				var c = string.charCodeAt(n);
       
    16 
       
    17 				if (c < 128) {
       
    18 					utftext += String.fromCharCode(c);
       
    19 				}
       
    20 				else if((c > 127) && (c < 2048)) {
       
    21 					utftext += String.fromCharCode((c >> 6) | 192);
       
    22 					utftext += String.fromCharCode((c & 63) | 128);
       
    23 				}
       
    24 				else {
       
    25 					utftext += String.fromCharCode((c >> 12) | 224);
       
    26 					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
       
    27 					utftext += String.fromCharCode((c & 63) | 128);
       
    28 				}
       
    29 			}
       
    30 			return utftext;
       
    31 		},
       
    32 
       
    33 		// public method for url decoding
       
    34 		decode : function (utftext) {
       
    35 			var string = "";
       
    36 			var i = 0,
       
    37 				c = 0,
       
    38 				c2 = 0,
       
    39 				c3 = 0;
       
    40 
       
    41 			while ( i < utftext.length ) {
       
    42 
       
    43 				c = utftext.charCodeAt(i);
       
    44 
       
    45 				if (c < 128) {
       
    46 					string += String.fromCharCode(c);
       
    47 					i++;
       
    48 				}
       
    49 				else if((c > 191) && (c < 224)) {
       
    50 					c2 = utftext.charCodeAt(i+1);
       
    51 					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
       
    52 					i += 2;
       
    53 				}
       
    54 				else {
       
    55 					c2 = utftext.charCodeAt(i+1);
       
    56 					c3 = utftext.charCodeAt(i+2);
       
    57 					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
       
    58 					i += 3;
       
    59 				}
       
    60 			}
       
    61 			return string;
       
    62 		}
       
    63 	}; /** $.UTF8 */
       
    64 
       
    65 })(jQuery, this);