function Pager(tableName, itemsPerPage) {

    this.tableName = tableName;

    this.itemsPerPage = itemsPerPage;

    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;

    	}



        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;

    	}

    	var element = document.getElementById(positionId);

    	

    	var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171  </span> | ';

        for (var page = 1; page <= this.pages; page++) 

            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';

        pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal">  &#187;</span>';            

        

        element.innerHTML = pagerHtml;

    }

}







var sortare = {



	that: false,

	isOdd: false,



	sortColumnIndex : -1,

	lastAssignedId : 0,

	newRows: -1,

	lastSortedTable: -1,



	/**

	 * Initialises the Standardista Table Sorting module

	 **/

	init : function() {

		// first, check whether this web browser is capable of running this script

		if (!document.getElementsByTagName) {

			return;

		}

		

		this.that = this;

		

		var tables = document.getElementsByTagName("table");

		
		for (var i=0; i < tables.length; i++) {

			var thisTable = tables[i];

			

			if (css.elementHasClass(thisTable, 'sortable')) {

				//this.makeSortable(thisTable);

				// ************************************* START TESTE ****************************

				var that = sortare.that;

		

		var column = 2;

		var rowNum = 0;

		while (rowNum < thisTable.tBodies[0].rows.length) {

			rowNum++;

		}

		var sortfn = that.determineSortFunction();

		

		// if the last column that was sorted was this one, then all we need to 

		// do is reverse the sorting on this column

		if (thisTable.id == that.lastSortedTable && column == that.sortColumnIndex) {

			newRows = that.newRows;

			newRows.reverse();

		// otherwise, we have to do the full sort

		} else {

			that.sortColumnIndex = column;

			var newRows = new Array();



			for (var j = 0; j < thisTable.tBodies[0].rows.length; j++) { 

				newRows[j] = thisTable.tBodies[0].rows[j]; 

			}



			newRows.sort(sortfn);

		}



		that.moveRows(thisTable, newRows);

		

				// ************************************* STOP  TESTE ****************************

			}

		}

		

	},

	

	getInnerText : function(el) {

		

		if ('string' == typeof el || 'undefined' == typeof el) {

			return el;

		}

		

		if (el.innerText) {

			return el.innerText;  // Not needed but it is faster

		}

		

		str = '';



		var cs = el.childNodes;

		var l = cs.length;

		for (var i = 0; i < l; i++) {

			// 'if' is considerably quicker than a 'switch' statement, 

			// in Internet Explorer which translates up to a good time 

			// reduction since this is a very often called recursive function

			if (1 == cs[i].nodeType) { // ELEMENT NODE

				str += this.getInnerText(cs[i]);

				break;

			} else if (3 == cs[i].nodeType) { //TEXT_NODE

				str += cs[i].nodeValue;

				break;

			}

		}

		

		

		return str;

	},



	determineSortFunction : function() {

		

		var sortfn = this.sortNumeric;

		return sortfn;

	},

	

	sortNumeric : function(a,b) { 

		var that = sortare.that;



		var aa = parseFloat(that.getInnerText(a.cells[that.sortColumnIndex]));

		if (isNaN(aa)) { 

			aa = 0;

		}

		var bb = parseFloat(that.getInnerText(b.cells[that.sortColumnIndex])); 

		if (isNaN(bb)) { 

			bb = 0;

		}

		return bb-aa;

	},



	moveRows : function(table, newRows) {

		this.isOdd = false;



		// We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones

		for (var i=0;i<newRows.length;i++) { 

			var rowItem = newRows[i];

			rowItem.cells[0].appendChild(document.createTextNode(i+1));

			if(i%2==1)

				css.addClassToElement(rowItem, 'even');

			table.tBodies[0].appendChild(rowItem); 

		}

	}	



}



function initial() {

	sortare.init();

}

var css = {

	

	privateGetClassArray: function(el) {

		return el.className.split(' '); 

	},

	

	privateCreateClassString: function(classArray) {

		return classArray.join(' ');

	},

	

	addClassToElement: function(el, classString) {

		var classArray = this.privateGetClassArray(el);



		if (this.elementHasClass(el, classString)) {

			return; // already has element so don't need to add it

		}



		classArray.push(classString);



		el.className = this.privateCreateClassString(classArray);

	},

	

	elementHasClass: function(el, classString) {

		if (!el) {

			return false;

		}

		

		var regex = new RegExp('\\b'+classString+'\\b');

		if (el.className.match(regex)) {

			return true;

		}



		return false;

	}

}