function ltrim(str) {
	return str.replace(/^\s+/,"");
}

function rtrim(str) {
	return str.replace(/\s+$/,"");
}
    

function trim(str) {
	return str.replace(/^\s+/,"").replace(/\s+$/,"");
}	

function left(str,length) {
	return str.substr(0,length);
}
    
function right(str,length) {
	return str.substr( str.length - length, length );
}


// compone una stringa che rappresenta una chiamata di funzione.
// il primo argomento deve essere il nome della funzione, i successivi
// gli eventuali parametri
function composeFunctionCall( ) {
	var functioncall = "return ";
	if ( arguments.length > 0 ) {
		functioncall += arguments[0] + "("; // il primo argomento è il nome della funzione
		for( var i = 1; i < arguments.length; i++ ) {
			switch( typeof(arguments[i] ) ) {
				case "string" :
					if ( arguments[i].search(/[\[\]]/gi) != -1 ) { 
						/***************************************************************************/
						/* la stringa è racchiusa fra parentesi quadre e deve essere quindi essere */
						/* trattata come un oggetto, non rachiusa tra apici                        */
						/***************************************************************************/
						functioncall += arguments[i].replace(/[\[\]]/gi,"");
					} else {
						functioncall += "'" + arguments[i] + "'";
					}
					break;
				case "number" : 
				case "boolean" :
					functioncall += arguments[i];
					break;
			}
			if ( i < arguments.length - 1 ) {
				functioncall += ",";
			}
		}
		functioncall += ");";
	}
	return functioncall;
}

function random( ) {
	var RND_MAX, RND_MIN;
	if ( arguments.length > 0 ) {
		RND_MIN = arguments[0];
		RND_MAX = arguments[1];
	} else {
		RND_MIN = 0;
		RND_MAX = 999999999999999999;
	}
	var randomNumber = parseInt(((RND_MAX - RND_MIN + 1) * Math.random( ) + RND_MIN));
	return randomNumber;
}



function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() { oldonload(); func(); }
	}
}

function escapeHtmlEntities(text) {
	var relt = /</ig;
	var regt = />/ig;
				
	text = text.replace(relt,"&lt;");
	text = text.replace(regt,"&gt;");
	return text;
}

function Paginator( currentPage, totalPages, maxPages, reccount ) {
	this.parametersInfo = new Array( );
	this.currentPage = currentPage;	// la pagina corrente;
	this.totalPages = totalPages;	// il numero di pagine totali
	this.maxPages = maxPages;		// il numero di pagine visualizzate nel navigatore
	this.reccount = reccount;		// il numero totale di record
		
	this.nextPageImage = ">";		// l'immagine da utilizzare per rappresentare il pulsante alla pagina successiva
	this.nextGroupImage = ">>";		// l'immagine da utilizzare per rappresentare il pulsante al gruppo di pagine successivo
	this.prevPageImage = "<";
	this.prevGroupImage = "<<";
	
	this.tablewidth = "";
	this.tablespacing = "";
	this.tablepadding = "";

	this.link = ""; // passare un oggetto H_HLINK che rappresenta il link di ogni pagina insieme ai relativi parametri
	this.namePageParameter = "pagenum"; // il nome del parametro della pagina da passare nell'URL
		
	this.get = function( ) {
		var text = "";
		var limit, start, index;
		
		limit = this.maxPages;
		while( this.currentPage > limit ) {
			limit = limit + this.maxPages;
		}
		start = (limit - this.maxPages) + 1;
		index = start;
			
		if ( this.link == "" ) {
			this.link = new HLink("action.asp","" );
		}
			
		text += "<table border=\"0\" cellspacing=\"" + this.tablespacing + "\" cellpadding=\"" + this.tablepadding + "\" width=\"" + this.tablewidth + "\">";
		text += "<tr>";
			
		/* se start è maggiore di uno, ci troviamo su un gruppo di pagine successivo al 
		primo e quindi bisogna creare il link al gruppo di pagine precedenti */
		if ( start > 1 ) {
			this.setupLinkParameters(this.link,this.namePageParameter,this.parametersInfo,limit-this.maxPages);
			this.link.text( this.prevGroupImage );
			text += "<td>";
				text += this.link.get( );
			text += "</td>";
		}
			
		if ( this.currentPage > 1 ) {
			this.setupLinkParameters(this.link,this.namePageParameter,this.parametersInfo,this.currentPage - 1);
			this.link.text( this.prevPageImage );
			text += "<td>";
				text += this.link.get( );
			text += "</td>";
		}
				
			
		// crea hiperlink finchè non ha raggiunto il numero totale delle pagine o
		// il massimo numero di pagine per schermata
		while ( index <= this.totalPages && index <= limit ) {
			if ( index != this.currentPage ) { // se non è la pagina corrente crea il link
				this.setupLinkParameters(this.link,this.namePageParameter,this.parametersInfo,index);
				this.link.text( index );
				text += "<td>";
					text += this.link.get( );
				text += "</td>";
			} else {
				text += "<td>";
					text += "<b>" + index + "</b>";
				text += "</td>";
			}
			index++;
		}
			
		if ( this.currentPage < this.totalPages && this.currentPage < limit ) {
			this.setupLinkParameters(this.link,this.namePageParameter,this.parametersInfo,this.currentPage + 1);
			this.link.text( this.nextPageImage );
			text += "<td>";
				text += this.link.get( );
			text += "</td>";
		}
			
			
		// se quando si esce dal ciclo di sopra l'indice è minore del numero totale delle pagine
		// bisogna creare il link al gruppo successivo
		if ( index <= this.totalPages ) { 
			this.setupLinkParameters(this.link,this.namePageParameter,this.parametersInfo,index);
			this.link.text( this.nextGroupImage );
			text += "<td>";
				text += this.link.get( );
			text += "</td>";
		}
			
		text += "</tr>";
		text += "</table>";
		return text;
	}
		
	this.setupLinkParameters = function( linkobject, parametername, parameters, pageindex ) {
		linkobject.parameters(parametername,pageindex);
	}
}	




