﻿/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    trascii: function(string) {
        return this._trascii(string);
    },

    stripinput: function(string) {
        return this._stripinput(string);
    },

    // private method for UTF-8 encoding
    _utf8_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;
    },

    // private method for UTF-8 decoding
    _utf8_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;
    },

    _trascii: function(text) {
        var search = new Array('Ç', 'İ', 'Ğ', 'Ö', 'Ş', 'Ü', 'ç', 'ı', 'ğ', 'ö', 'ş', 'ü');
        var replace = new Array('&#199;', '&#304;', '&#286;', '&#214;', '&#350;', '&#220;', '&#231;', '&#305;', '&#287;', '&#246;', '&#351;', '&#252;');
        text = text.replace(/Ç/g, replace[0]);
        text = text.replace(/İ/g, replace[1]);
        text = text.replace(/Ğ/g, replace[2]);
        text = text.replace(/Ö/g, replace[3]);
        text = text.replace(/Ş/g, replace[4]);
        text = text.replace(/Ü/g, replace[5]);
        text = text.replace(/ç/g, replace[6]);
        text = text.replace(/ı/g, replace[7]);
        text = text.replace(/ğ/g, replace[8]);
        text = text.replace(/ö/g, replace[9]);
        text = text.replace(/ş/g, replace[10]);
        text = text.replace(/ü/g, replace[11]);

        return text;
    },

    //Strip Input Function, prevents HTML in unwanted places
    _stripinput: function(text) {
        var search = array("&", "\"", "'", "\\", '\"', "\'", "<", ">", "&nbsp;");
        var replace = array("&amp;", "&quot;", "&#39;", "&#92;", "&quot;", "&#39;", "&lt;", "&gt;", " ");
        text = text.replace(search[0], replace[0]);
        text = text.replace(search[1], replace[1]);
        text = text.replace(search[2], replace[2]);
        text = text.replace(search[3], replace[3]);
        text = text.replace(search[4], replace[4]);
        text = text.replace(search[5], replace[5]);
        text = text.replace(search[6], replace[6]);
        text = text.replace(search[7], replace[7]);
        text = text.replace(search[8], replace[8]);

        return text;
    }
}