/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var UrlLatin2Utf8 = {
	encode: function (string) {
		//alert('alles OK');
		var str = Url.encode(string);
		//var str = escape(string);
		
		str = str.replace(/%C3%A1/g, '%E1'); //á
		str = str.replace(/%C3%AD/g, '%ED'); //í
		str = str.replace(/%C5%B1/g, '%FB'); //ű
		str = str.replace(/%C5%91/g, '%F5'); //ő
		
		str = str.replace(/%C3%BC/g, '%FC') //ü
		str = str.replace(/%C3%B6/g, '%F6') //ö
		str = str.replace(/%C3%BA/g, '%FA') //ú
		str = str.replace(/%C3%B3/g, '%F3') //ó
		str = str.replace(/%C3%A9/g, '%E9') //é
		
		//alert('after the first replaces: ' + str);
		
		str = str.replace(/%C3%81/g, '%C1'); //Á
		str = str.replace(/%C3%8D/g, '%CD'); //Í
		str = str.replace(/%C5%B0/g, '%DB'); //Ű
		str = str.replace(/%C5%90/g, '%D5'); //Ő
		
		str = str.replace(/%C3%9C/g, '%DC'); //Ü
		str = str.replace(/%C3%96/g, '%D6'); //Ö
		str = str.replace(/%C3%9A/g, '%DA'); //Ú
		str = str.replace(/%C3%93/g, '%D3'); //Ó
		str = str.replace(/%C3%89/g, '%C9'); //É
		//alert('after the second replaces: ' + str);
		
		return str;
	}
}

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));
	},

	// 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);
			}

		}
		/*		
		//my hack - only for the 4 problematic Hungarian characters, only for the kgy.nava.hu, only for utf8->latin2 encoding
		for (var n=0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			switch (c) {
				case 369:
				case 337:
				case 368:
				case 336:
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
					break;
				default:
					utftext += string.charCodeAt(n);
					break;
			}
		}
		*/
		
		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;
	}

}

