/*
*******************************************************************************
		Estende l'oggetto string aggiungendo alcuni metodi di utilità
*******************************************************************************
*/
//Restituisce una stringa senza gli spazi all'inizio e alla  fine
String.prototype.trim = function () 
{
   var s = this;
   
   var ch = s.substring(0, 1);
   while (ch == " ") 
   { 
	  s = s.substring(1, s.length);
	  ch = s.substring(0, 1);
   }
   ch = s.substring(s.length-1, s.length);
   while (ch == " ") {
	  s = s.substring(0, s.length-1);
	  ch = s.substring(s.length-1, s.length);
   }

   return s;
}

//codifica la string nel formato x-www-urlencoded
String.prototype.xWwwUrlEncode = function ()
{
	var plaintext = this;
	var UnSafeChard = "!#$%&'()*+,-./" +
						"\"" + 
						":;<=>?@[\\]^_`{|}~";
	var HEX = "0123456789ABCDEF";
	
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) 
	{
		var ch = plaintext.charAt(i);
		var charCode = ch.charCodeAt(0);
		
		if (ch == " ") 
		{
			encoded += "+";	// x-www-urlencoded, rather than %20
		} 
		else if (UnSafeChard.indexOf(ch) == -1) 
		{
			encoded += ch;
		} 
		else 
		{
			encoded += "%";
			encoded += HEX.charAt((charCode >> 4) & 0xF);
			encoded += HEX.charAt(charCode & 0xF);
		}
	}
	return encoded;
}

//decodifica la string URL
function urlDecode(str)
{
	str = str.replace(new RegExp('\\+','g'),' ');
	return unescape(str);	
}

//Codifica la string in URLEncode
String.prototype.urlEncode = function()
{
	var str = this;
		
	str = str_replace('%', '%25', str);
	str = str_replace(' ', '%20', str);
	str = str_replace('\\!', '%21', str);
	str = str_replace('\"', '%22', str);
	str = str_replace('\\#', '%23', str);
	str = str_replace('\\$', '%24', str);
	str = str_replace('&', '%26', str);
	str = str_replace('\'', '%27', str);
	str = str_replace('\\(', '%28', str);
	str = str_replace('\\)', '%29', str);
	str = str_replace('\\*', '%2A', str);
	str = str_replace('\\+', '%2B', str);
	str = str_replace(',', '%2C', str);
	str = str_replace('-', '%2D', str);
	str = str_replace('\\.', '%2E', str);
	str = str_replace('/', '%2F', str);
	str = str_replace(':', '%3A', str);
	str = str_replace(';', '%3B', str);
	str = str_replace('<', '%3C', str);
	str = str_replace('=', '%3D', str);
	str = str_replace('>', '%3E', str);
	str = str_replace('\\?', '%3F', str);
	str = str_replace('@', '%40', str);
	str = str_replace('\\[', '%5B', str);
	str = str_replace('\\\\', '%5C', str);
	str = str_replace('\\]', '%5D', str);
	str = str_replace('\\^', '%5E', str);
	str = str_replace('_', '%5F', str);
	str = str_replace('`', '%60', str);
	str = str_replace('\\{', '%7B', str);
	str = str_replace('\\|', '%7C', str);
	str = str_replace('\\}', '%7D', str);
	str = str_replace('~', '%7E', str);
	
	str = str_replace('%20', '+', 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);
			}
	}

		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;
	 }
}



// esegue il replace di una stringa.
// findText: stringa da cercare
// replaceText: stringa da sostituire
// text: stringa nella quale cercare
function str_replace(findText, replaceText, text)
{
	var txt = text;
	txt = txt.replace(new RegExp(findText, "g"), replaceText);
	return txt;
}

//funzione per eseguire l'htmlencode di un testo
function HTMLEncode(text)
{
	if (!text)
	{
		return '' ;
	}

	text = text.replace( /&/g, '&amp;' ) ;
	text = text.replace( /</g, '&lt;' ) ;
	text = text.replace( />/g, '&gt;' ) ;

	return text ;
}

//funzione per eseguire l'htmldecode di un testo
function HTMLDecode(text)
{
	if (!text)
	{
		return '' ;
	}

	text = text.replace( /&gt;/g, '>' ) ;
	text = text.replace( /&lt;/g, '<' ) ;
	text = text.replace( /&amp;/g, '&' ) ;

	return text ;
}
