function Pager(tableName, itemsPerPage, instanceName, navId) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.instanceName = instanceName;
    this.navId = navId;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;
    
    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 = '';
        }
    }
    
    this.showPage = function(pageNumber) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}

        this.currentPage = pageNumber;
        
        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
        
        this.from = from;
        this.to = to;
        
        this.showPageNav();
    }   
    
    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
            setPage(pager.currentPage);
    }
    
    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
            setPage(pager.currentPage);
        }
    }                        
    
    this.init = function() {
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1); 
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
    }

    this.showPageNav = function() {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
    	var element = document.getElementById(navId);
    	
    	var rows = document.getElementById(tableName).rows;
    	
    	var pagerHtml = '<a href="javascript:' + instanceName + '.prev();" class="pg-normal"> &#171;</a>';
        pagerHtml += '<a href="javascript:'+instanceName+'.next();" class="pg-normal">&#187;</a> <div id="pg-count">Showing ' + this.from + '-' + this.to + ' of ' + (rows.length - 1) + '</id>';
        
        element.innerHTML = pagerHtml;
    }
}

function ChangeColor(tableRow, highLight)
{
	for (var i = 0; i < tableRow.cells.length; i++)
    {
		if (highLight)
		{
		  tableRow.cells[i].style.backgroundColor = '#eee';
		}
		else
		{
		  tableRow.cells[i].style.backgroundColor = '#ccc';
		}
	}
  }

function DoNav(tr)
{
	window.location = tr.cells[0].childNodes[0].href;
}

