﻿$.extend({ URLEncode: function (string, escape) {
    string = string.replace(/\r\n/g, "\n");
    //string = $.trim(string.replace(/[%\?\<\>&|\/\\\.\"\,:\{\}\-\'#`]/g, " "));
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

        if (c < 128) {
            switch (c) {
                case 34:
                case 39:
                    break;

                case 32:
                case 35:
                case 36:
                case 37:
                case 28:
                case 43:
                case 44:
                case 47:
                case 58:
                case 59:
                case 60:
                case 61:
                case 62:
                case 63:
                case 64:
                case 91:
                case 92:
                case 93:
                case 94:
                case 96:
                case 123:
                case 124:
                case 125:
                case 126:
                    utftext += escape + c.toString(16);
                    break;

                default:
                    utftext += string.charAt(n);
                    break;
            }
        }
        else if ((c > 127) && (c < 2048)) {
            utftext += escape + ((c >> 6) | 192).toString(16);
            utftext += escape + ((c & 63) | 128).toString(16);
        }
        else {
            utftext += escape + ((c >> 12) | 224).toString(16);
            utftext += escape + (((c >> 6) & 63) | 128).toString(16);
            utftext += escape + ((c & 63) | 128).toString(16);
        }

    }

    return utftext;
},
    URLDecode: 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;
    }
});

