function Pager(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;	//numero di documenti visualizzati per ogni pagina
    this.rangePages = 5;   //pagine mostrate nella barra della paginazione (inserire un numero dispari)
    this.pagerName = "";
    this.positionId = "";
    this.currentPage = 1;
    this.pages = 0;
    this.lower = 1;
    this.upper = 1;
    this.inited = false;
    
  //Stampa i record della tabella dei risultati dal from -> to
    this.showRecords = function(from, to) {        
        var rows = document.getElementById(tableName).rows;
        // i starts from 1 to skip table header row
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to)  
                rows[i].style.display = 'none';
            else
                rows[i].style.display = '';
        }
    }
    
  //Mostra le tuple inerenti alla pagina dei risultati passata come parametro
    this.showPage = function(pageNumber) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}

        var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        oldPageAnchor.className = 'pg-normal';
        
        this.currentPage = pageNumber;
        var newPageAnchor = document.getElementById('pg'+this.currentPage);
        newPageAnchor.className = 'pg-selected';
        
        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    }   
    
  //Mostra i risultati della prima pagina
    this.showFirstPage = function() {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}

	 //var oldPageAnchor = null;
    	 var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        oldPageAnchor.className = 'pg-normal';
        
        this.currentPage = 1;
        var pageNumber = this.currentPage;
        var newPageAnchor = document.getElementById('pg'+this.currentPage);
        newPageAnchor.className = 'pg-selected';
        
        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
		this.showPageNav(this.currentPage, this.pagerName, this.positionId);        	
    } 
    
  //Mostra i risultati dell'ultima pagina
    this.showLastPage = function() {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}

        var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        oldPageAnchor.className = 'pg-normal';
        
        this.currentPage = this.pages;
        var pageNumber = this.currentPage;
        var newPageAnchor = document.getElementById('pg'+this.currentPage);
        newPageAnchor.className = 'pg-selected';
        
        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
		this.showPageNav(this.currentPage, this.pagerName, this.positionId);        	
    } 
    
  //Permette di scorrere blocchi di pagine intere in avanti (rangePages alla volta)
    this.prev = function() {
    	
    	this.lower -= this.rangePages;
    	this.upper -= this.rangePages;
    	
        if (this.lower > 1){		//se il prev non va al di sotto della prima pagina...
        	this.currentPage = this.lower + ((this.rangePages - 1) / 2);
           // this.showPage(this.currentPage);
        	this.showPageNav(this.currentPage, this.pagerName, this.positionId)
        
        }else{		//..altrimenti passo la prima pagina      
        	this.currentPage = 1;
    		this.showPageNav(this.currentPage, this.pagerName, this.positionId);        	
        }
    }
    
  //Permette di scorrere blocchi di pagine intere all'indietro (rangePages alla volta)
    this.next = function() {
    	
    	this.lower += this.rangePages;
    	this.upper += this.rangePages;
    	
    	if (this.upper < this.pages) {	//se il next non supera l'ultima pagina...
        	this.currentPage = this.lower + ((this.rangePages - 1) / 2);
           // this.showPage(this.currentPage);
            this.showPageNav(this.currentPage, this.pagerName, this.positionId)

        }else{		//..altrimenti passo l'ultima pagina        
        	this.currentPage = this.pages;
        	this.showPageNav(this.currentPage, this.pagerName, this.positionId);        	
        }
    }                        
    
  //Inizializza alcune variabili globali prendendo informazioni dalla tabella dei risultati
    this.init = function() {
        //if(document.getElementById(tableName)){
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1); 
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
	 //}
    }

  //Mostra la barra di paginazione in relazione alla pagina corrente
    this.showPageNav = function(selectedPage, pagerName, positionId) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
    	
    	this.pagerName = pagerName;
    	this.positionId = positionId;
    	var element = document.getElementById(positionId);
        element.innerHTML = null;
        
        this.lower = selectedPage - ((this.rangePages - 1) / 2);
        this.upper = selectedPage + ((this.rangePages - 1) / 2);
        
      //Se si tratta di una delle prime n pagine...
        if(selectedPage <= ((this.rangePages - 1) / 2)){
        	this.lower = 1
        	this.upper = this.rangePages;        	
        }
        
      //Se si tratta di una delle ultime n pagine...
        if(selectedPage >= this.pages - ((this.rangePages - 1) / 2)){
        	this.lower = this.pages - (this.rangePages - 1);
        	this.upper = this.pages;        	
        }
        
        if(this.lower < 1)
        	this.lower = 1;
        
        if(this.upper > this.pages)
        	this.upper = this.pages;
        
        var pagerHtml = "";
        
      //Controllo se c'è bisogno di pagine precedenti
        if(selectedPage > this.rangePages)        
        	pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Precedenti </span>  ';
        
        if(this.lower > 1)
        	pagerHtml += '| <span id="pg1" class="pg-normal" onclick="' + pagerName + '.showFirstPage();">1</span> | ... | ';
        
     	for (var page = this.lower; page <= this.upper; page++)
            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';
       
     	if(this.upper < this.pages)
    	pagerHtml += '... | <span id="pg' + this.pages + '" class="pg-normal" onclick="' + pagerName + '.showLastPage();">' + this.pages + '</span> |  ';
     	
      //Controllo se c'è bisogno di pagine successive
        if(selectedPage < (this.pages - this.rangePages))  
        	pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Successive &#187;</span>';            
        
        if(this.pages==0){
        	pagerHtml="Nessun risultato presente";
		turnOn();
	 }
        element.innerHTML = pagerHtml;
    }
}
