
function str_split(str) 
{
	var str_array = new Array();
	for(var i=0;i<str.length;i++) { str_array[i] = str.charAt(i); }
	return str_array;
}
function h(text)
{
	var safechars = "\t\r\n !#$%'()+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvxyz{|}~"; // not <>"&
	var entities = 
	{
		'&#34;' : '&quot;',
		'&#38;' : '&amp;',
		'&#60;' : '&lt;',	
		'&#62;' : '&gt;',	
	    '&#160;' : '&nbsp;',
		'&#161;' : '&iexcl;',
		'&#162;' : '&cent;',
		'&#163;' : '&pound;',
		'&#164;' : '&curren;',
		'&#165;' : '&yen;',
		'&#166;' : '&brvbar;',
		'&#167;' : '&sect;',
		'&#168;' : '&uml;',
		'&#169;' : '&copy;',
		'&#170;' : '&ordf;',
		'&#171;' : '&laquo;',
		'&#172;' : '&not;',
		'&#173;' : '&shy;',
		'&#174;' : '&reg;',
		'&#8482;' : '&trade;',
		'&#175;' : '&macr;',
		'&#176;' : '&deg;',
		'&#177;' : '&plusmn;',
		'&#178;' : '&sup2;',
		'&#179;' : '&sup3;',
		'&#180;' : '&acute;',
		'&#181;' : '&micro;',
		'&#182;' : '&para;',
		'&#183;' : '&middot;',
		'&#184;' : '&cedil;',
		'&#185;' : '&sup1;',
		'&#186;' : '&ordm;',
		'&#187;' : '&raquo;',
		'&#188;' : '&frac14;',
		'&#189;' : '&frac12;',
		'&#190;' : '&frac34;',
		'&#191;' : '&iquest;',
		'&#215;' : '&times;',
		'&#247;' : '&divide;',
		'&#192;' : '&Agrave;',
		'&#193;' : '&Aacute;',
		'&#194;' : '&Acirc;',
		'&#195;' : '&Atilde;',
		'&#196;' : '&Auml;',
		'&#197;' : '&Aring;',
		'&#198;' : '&AElig;',
		'&#199;' : '&Ccedil;',
		'&#200;' : '&Egrave;',
		'&#201;' : '&Eacute;',
		'&#202;' : '&Ecirc;',
		'&#203;' : '&Euml;',
		'&#204;' : '&Igrave;',
		'&#205;' : '&Iacute;',
		'&#206;' : '&Icirc;',
		'&#207;' : '&Iuml;',
		'&#208;' : '&ETH;',
		'&#209;' : '&Ntilde;',
		'&#210;' : '&Ograve;',
		'&#211;' : '&Oacute;',
		'&#212;' : '&Ocirc;',
		'&#213;' : '&Otilde;',
		'&#214;' : '&Ouml;',
		'&#216;' : '&Oslash;',
		'&#217;' : '&Ugrave;',
		'&#218;' : '&Uacute;',
		'&#219;' : '&Ucirc;',
		'&#220;' : '&Uuml;',
		'&#221;' : '&Yacute;',
		'&#222;' : '&THORN;',
		'&#223;' : '&szlig;',
		'&#224;' : '&agrave;',
		'&#225;' : '&aacute;',
		'&#226;' : '&acirc;',
		'&#227;' : '&atilde;',
		'&#228;' : '&auml;',
		'&#229;' : '&aring;',
		'&#230;' : '&aelig;',
		'&#231;' : '&ccedil;',
		'&#232;' : '&egrave;',
		'&#233;' : '&eacute;',
		'&#234;' : '&ecirc;',
		'&#235;' : '&euml;',
		'&#236;' : '&igrave;',
		'&#237;' : '&iacute;',
		'&#238;' : '&icirc;',
		'&#239;' : '&iuml;',
		'&#240;' : '&eth;',
		'&#241;' : '&ntilde;',
		'&#242;' : '&ograve;',
		'&#243;' : '&oacute;',
		'&#244;' : '&ocirc;',
		'&#245;' : '&otilde;',
		'&#246;' : '&ouml;',
		'&#248;' : '&oslash;',
		'&#249;' : '&ugrave;',
		'&#250;' : '&uacute;',
		'&#251;' : '&ucirc;',
		'&#252;' : '&uuml;',
		'&#253;' : '&yacute;',
		'&#254;' : '&thorn;',
		'&#255;' : '&yuml;',
		'&#338;' : '&OElig;',
		'&#339;' : '&oelig;',
		'&#352;' : '&Scaron;',
		'&#353;' : '&scaron;',
		'&#376;' : '&Yuml;',
		'&#710;' : '&circ;',
		'&#732;' : '&tilde;',
		'&#8194;' : '&ensp;',
		'&#8195;' : '&emsp;',
		'&#8201;' : '&thinsp;',
		'&#8204;' : '&zwnj;',
		'&#8205;' : '&zwj;',
		'&#8206;' : '&lrm;',
		'&#8207;' : '&rlm;',
		'&#8211;' : '&ndash;',
		'&#8212;' : '&mdash;',
		'&#8216;' : '&lsquo;',
		'&#8217;' : '&rsquo;',
		'&#8218;' : '&sbquo;',
		'&#8220;' : '&ldquo;',
		'&#8221;' : '&rdquo;',
		'&#8222;' : '&bdquo;',
		'&#8224;' : '&dagger;',
		'&#8225;' : '&Dagger;',
		'&#8230;' : '&hellip;',
		'&#8240;' : '&permil;',
		'&#8249;' : '&lsaquo;',
		'&#8250;' : '&rsaquo;',
		'&#8364;' : '&euro;'
	};
	var chars = str_split(text);
	for(pos in chars)
	{
		var chr = chars[pos];
		if (safechars.indexOf(chr)==-1) { chars[pos] = '&#'+chr.charCodeAt(0)+';'; }
		if (typeof(entities[chars[pos]])!='undefined') { chars[pos] = entities[chars[pos]]; }
	}
	return chars.join('');	
}
function u(text)
{
	var safechars = "!#$(),-.0123456789;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~"; // not '%/\:?&=+
	var entities = 
	{
		'%20' : '+'
	};
	var chars = str_split(text);
	for(pos in chars)
	{
		var chr = chars[pos];
		if (safechars.indexOf(chr)==-1) { chars[pos] = '%'+dechex(chr.charCodeAt(0)); }
		if (typeof(entities[chars[pos]])!='undefined') { chars[pos] = entities[chars[pos]]; }
	}
	return chars.join('');	
}
function dechex(dec)
{
	hex = dec.toString(16)
	if (hex.length==1) { hex = '0'+hex; }
	return hex;
}
