function OmniTablePager(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;
    this.pagerName = "default";
    this.positionId = "default";
    
    this.showRecords = function(from, to) {        
        var rows = document.getElementById(tableName).rows;
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to)  
                rows[i].style.display = 'none';
            else
                rows[i].style.display = '';
        }
    }
    
    this.changeResultsPerPage = function(selectObj) {
    	var idx = selectObj.selectedIndex; 
 		var which = selectObj.options[idx].value;
 		itemsPerPage = eval(which);
 		this.init();
		this.showPageNav(this.pagerName, this.positionId);
		this.currentPage = 1;
 		this.showPage(1);
    }
    
    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);
    }   
    
    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }
    
    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    }                        
    
    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(pagerName, positionId) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
    	this.pagerName = pagerName;
    	this.positionId = positionId;
    	var element = document.getElementById(positionId);
    	
    	var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span>&nbsp;';
        for (var page = 1; page <= this.pages; page++) 
            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' 
            							+ pagerName + '.showPage(' + page + ');">' + page + '</span>&nbsp;';
        pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>&nbsp;';
                    
        pagerHtml += '<span>&nbsp;&nbsp;&nbsp;';
        pagerHtml += '<span class="pg-caption">Results per page : </span>';
        pagerHtml += '<select name="perPage" onchange="' + pagerName + '.changeResultsPerPage(this);" class="pg-select">';
        if (itemsPerPage == 10)
        	pagerHtml += '<option value="10" selected>10</option>';
        else
        	pagerHtml += '<option value="10">10</option>';
        if (itemsPerPage == 20)
        	pagerHtml += '<option value="20" selected>20</option>';
        else
        	pagerHtml += '<option value="20">20</option>';
        if (itemsPerPage == 30)
        	pagerHtml += '<option value="30" selected>30</option>';
        else
        	pagerHtml += '<option value="30">30</option>';
        if (itemsPerPage == 40)
        	pagerHtml += '<option value="40" selected>40</option>';
        else
        	pagerHtml += '<option value="40">40</option>';
        if (itemsPerPage == 50)
        	pagerHtml += '<option value="50" selected>50</option>';
        else
        	pagerHtml += '<option value="50">50</option>';
        if (itemsPerPage == 100)
        	pagerHtml += '<option value="100" selected>100</option>';
        else
        	pagerHtml += '<option value="100">100</option>';
        pagerHtml += '</span>';
        
        element.innerHTML = pagerHtml;
    }
}

