/**
 * Function to paginate tabular data.
 *
 * tableid: the id of the table
 * divid: the name of the div(s) to contain the page-numbers
 * no: number of rows we want per page
 * cur: the page to display
 */
function paginate(tableid, divid, no, cur)
{
	var tbl = document.getElementById(tableid);
	var trs = tbl.getElementsByTagName("tr");
	var total_pages = Math.ceil(trs.length / no);

	/* Make sure only the first no. items are showing */
	for (var i = 0; i < trs.length; i++) {
		if (Math.floor(i / no) == cur - 1) {
			trs[i].style.display = '';
		} else {
			trs[i].style.display = 'none';
		}
	}
	var target = document.getElementById(tableid + "target");
	if (target)
		target.innerHTML = "<table style='padding-top: 60px; padding-bottom: 60px'>" + tbl.innerHTML + "</table>";
	pg_generate_navbar(divid, cur, total_pages, {table: tableid, no: no, sec: target});
}

function pg_generate_navbar(divid, cur, tot, param)
{
	var paramstring = "'" + param.table + "', '" + divid + "', " + param.no + ", ";
	var style = " onmouseover='this.style.textDecoration=\"underline\";' onmouseout='this.style.textDecoration=\"none\";' style='cursor: hand' cursor='hand'";
	/* Generate the navigator bar */
	var html = "";

	/*if (tot > 1) {*/
	if (true) {
		html += "Page ";
		if (cur > 1) {
			html += "<font style='font-size: x-small'>";
			html += "<font" + style + "id='first" + divid + "' onClick=\"javascript: paginate(" + paramstring + " 1);\">&laquo;</font> ";
			html += "<font" + style + "id='prev" + divid + "' onClick=\"javascript: paginate(" + paramstring + (cur-1) + ");\">&lsaquo;</font> ";
			html += "</font>";
		}
		html += "<select onChange=\"javascript: paginate(" + paramstring + "this.value);\">";
		for (var i = 1; i <= tot; i++) {
			html += "<option value='" + i + "'" + (i == cur ? " selected" : "") + ">" + i + "</option>";
		}
		html += "</select>";
		if (cur < tot) {
			cur++;
			html += "<font style='font-size: x-small'>";
			html += " <font" + style + "id='next" + divid + "' onClick=\"javascript: paginate(" + paramstring + cur + ");\">&rsaquo;</font> ";
			html += "<font" + style + "id='last" + divid + "' onClick=\"javascript: paginate(" + paramstring + tot + ");\">&raquo;</font> ";
			html += "</font>";
		}
		html += " of " + tot;
	}

	var d = document.getElementsByName(divid);
	for (var i = 0; i < d.length; i++) {
		var hh = html;
		if (param.sec)   /* XXX: This is a workaround for the mess created by the monkeys who developed IE */
			hh += "<br><br>"+param.sec.innerHTML;
		d[i].innerHTML = hh;
	}
}

