/*
	String object extensions
*/
function space(n)					//	Restituisce "n" spazi
{
	return ' '.replicate(n);
}
function _ltrim()					//	Toglie spazi iniziali
{
	var s=this;
	while( s.charAt(0) == ' ' ) s = s.slice(1)
	return s;
}
function _rtrim()					//	Toglie spazi finali
{
	var s=this;
	while( s.charAt( s.length - 1) == ' ' ) s = s.slice(0, s.length - 1)
	return s;
}
function _alltrim()				//	Toglie spazi iniziali e finali
{
	var s=this;
	return s.ltrim().rtrim();
}
function _purge()					//	Toglie spazi non significativi (iniziali, finali, doppi)
{
	var i,s=this;
	while( s.charAt(0) == ' ' ) s = s.slice(1)
	while( s.charAt( s.length - 1) == ' ' ) s = s.slice(0, s.length - 1)
	for ( i=0; i<s.length; i++)
		while ( s.slice(i,i+2) == '  ' ) s = s.slice(0,i) + s.slice(i+1);
	return s;
}
function _reverse()
{
	var i,s='';
	for ( i=this.length-1; i>=0; i--) s+=this.charAt(i);
	return s;
}
function _proper()					//   Ogni parola con iniziale maiuscola, resto minuscolo
{
	var s = this.noDoubleBlanks();
	var j, t, a = s.split(' ');
	for ( t='', j=0; j<a.length; j++ ) t+= a[j].wordproper() + ' ';
	return t;
}
function _noDoubleBlanks()			//   Toglie tutti gli spazi doppi
{
	var s = this;
	var doubleblank = /\s\s/;
	while( s.search(doubleblank) >= 0 ) s = s.replace(/\s\s/,' ');
	return s;
}
function _wordproper()				//   Stringa con iniziale maiuscola, resto minuscolo
{
	return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase();
}
function _replicate(n)				//	Restituisce la stringa ripetuta "n" volte
{
	var c, i;
	n = numero(n);
	for ( c='', i=1; i<=n; i++ ) c+= this;
	return c;
}
function _padr(n,c)				//	Allunga a dx fino a "n" caratteri usando "c" (default BLANK)
{
	var s = this;
	n = numero(n);
	if ( typeof(c) != 'string' ) c = ' ';
	while ( s.length < n ) s+= c;
	return s;
}
function _padl(n,c)				//	Allunga a sx fino a "n" caratteri usando "c" (default BLANK)
{
	var s = this;
	n = numero(n);
	if ( typeof(c) != 'string' ) c = ' ';
	while ( s.length < n ) s = c + s;
	return s;
}
function _padc(n,c)				//	Come prima ma la stringa risulta centrata
{
	var s = this;
	var x = s.length + Math.floor(( n - s.length ) / 2);
	return (s.padl(x)).padr(n,c);
}
String.prototype.trim			= _ltrim
String.prototype.ltrim			= _ltrim
String.prototype.rtrim			= _rtrim
String.prototype.alltrim		= _alltrim
String.prototype.purge			= _purge
String.prototype.reverse		= _reverse
String.prototype.proper			= _proper
String.prototype.wordproper		= _wordproper
String.prototype.noDoubleBlanks	= _noDoubleBlanks
String.prototype.replicate		= _replicate
String.prototype.padr			= _padr
String.prototype.padl			= _padl
String.prototype.padc			= _padc

