// public method for url encoding
function encodeUTF8( s )
{
  var text = s;
  text = s.replace(/&nbsp;/g, ' ');
  return encodeURIComponent(text);
}

function html_entity_decode (string, quote_style) 
{
    // Convert all HTML entities to their applicable characters  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/html_entity_decode    // +   original by: john (http://www.jd-tech.net)
    // +      input by: ger
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman    // +   improved by: marc andreu
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Ratheous
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Nick Kolosov (http://sammy.ru)    // +   bugfixed by: Fox
    // -    depends on: get_html_translation_table
    // *     example 1: html_entity_decode('Kevin &amp; van Zonneveld');
    // *     returns 1: 'Kevin & van Zonneveld'
    // *     example 2: html_entity_decode('&amp;lt;');    // *     returns 2: '&lt;'
    var hash_map = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    
    if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {        return false;
    }
 
    // fix &amp; problem
    // http://phpjs.org/functions/get_html_translation_table:416#comment_97660    delete(hash_map['&']);
    hash_map['&'] = '&amp;';
 
    for (symbol in hash_map) {
        entity = hash_map[symbol];        tmp_str = tmp_str.split(entity).join(symbol);
    }
    tmp_str = tmp_str.split('&#039;').join("'");
    
    return tmp_str;
}
	
function encode__UTF8(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 = utftext + string.charCodeAt(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext = utftext + string.fromCharCode((c >> 6) | 192);
                utftext = utftext + string.fromCharCode((c & 63) | 128);
            }
            else {
                utftext = utftext + string.fromCharCode((c >> 12) | 224);
                utftext = utftext + string.fromCharCode(((c >> 6) & 63) | 128);
                utftext = utftext + string.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }

// public method for url decoding
function decodeUTF8(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;
    }
