// avoiding id's for PgNum
var call_PgNum_value = "#";
var call_PgNum_input_name = "PgNum";
var call_PgNum_button_name = "PgGo";
/** ------------------------------------------------------------------------ **/
// Functions for static use
function getPage(formId, numberId, formAction, pageNum) {
	document.getElementById(numberId).value = pageNum;
	var f = document.getElementById(formId);
	if (formAction && (formAction.length > 0)) f.action = formAction;
	f.submit();
	f.action = '';
}
function getPagebyInput(button, input, formId, numberId, formAction, maxPage, addNum2href) {
	var pageNum = getInputNumber(button, input, maxPage);
	if (pageNum == 0) return;
	document.getElementById(numberId).value = pageNum;

	if (addNum2href == 1) {
		if (formAction.indexOf('/', formAction.length - 1) == - 1) formAction += '/';
		formAction += pageNum;
	}
	var form = document.getElementById(formId);
	form.action = formAction;
	form.submit();
	form.action = '';
}
function pageNumKeyDown(e, button, input, formId, numberId, formAction, maxPage, addNum2href) {
	var key = 0;
	if (window.event) key = e.keyCode;
	else if(e.which) key = e.which;
	else return false;

	if (key == 13) {
		getPagebyInput(null, input, formId, numberId, formAction, maxPage, addNum2href);
	}
}
/** ------------------------------------------------------------------------ **/
// Common functions for static and dinamic use
function getSiblingElement(elem1, elem2, name, prev_next) {
	if (!elem2) {
		if (!elem1) return null;
		var elem2 = elem1;
		while (elem2.name != name) {
			if (prev_next == "next") elem2 = elem2.nextSibling;
			else elem2 = elem2.previousSibling;
			if (!elem2) return null;
		}
	}
	return elem2;
}
function getInputNumber(button, input, maxPage) {
	if (!input) {
		input = getSiblingElement(button, input, call_PgNum_input_name, "previous");
		if (!input) return 0;
	}
	var pageNum = parseInt(input.value);
	if (isNaN(pageNum) || (pageNum < 0) || (pageNum > maxPage)) {
		input.value = "";
		input.focus();
		return 0;
	}
	return pageNum;
}
function pageNumFocus(input) {
	if (input.value == call_PgNum_value) input.value = "";
}
function pageNumBlur(input) {
	input.value = input.value.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
	if (input.value == "") input.value = call_PgNum_value;
}
function getCurrentPageNumber() {
	var curPageArr = document.getElementsByName("currentPageNumber");
	if (curPageArr.length > 0) return curPageArr[0].value;
	return 1;
}
/** ------------------------------------------------------------------------ **/
// Class for dinamic use
function CPagesPanel(funcName, id1, id2, rowsOnPage, rowsCount) {
	this.funcName = funcName;
	this.id1 = id1;
	this.id2 = id2 || '';
	this.rowsOnPage = rowsOnPage || 0;
	this.rowsCount = rowsCount || 0;

	this.pageCount = 1;
	this.curPage = 1;
	this.prev = 1;
	this.next = 1;

	if (this.rowsOnPage > 0) this.refresh(this.curPage, this.rowsOnPage, this.rowsCount);
}

CPagesPanel.prototype.refresh = function(curPage, rowsOnPage, rowsCount) {
	this.curPage = curPage;
	this.rowsOnPage = rowsOnPage;
	this.rowsCount = rowsCount;
	this.pageCount = Math.ceil(this.rowsCount / this.rowsOnPage);
	if (this.pageCount < 1) this.pageCount = 1;
	this.prev = this.curPage - 1;
	this.next = this.curPage + 1;

	this.drawPanel();
}

CPagesPanel.prototype.drawPanel = function() {
	var str = '';
	str += '<div class="pgCtrl"><table align="right"><tr><td valign="middle"><div class="noWrap">';
	str += "\n";

	if (this.curPage > 1) {
		str += '<a onclick="' + this.funcName + '(1);" href="#" title="Go to the First Page">First</a>&nbsp;|';
		str += "\n";
		str += '<a onclick="' + this.funcName + '(' + this.prev + ');" href="#" title="Go to the Previous Page" >Prev</a>&nbsp;|';
		str += "\n";
	}
	str += '<strong>Page ' + this.curPage + '/' + this.pageCount + '</strong>&nbsp;';
	if (this.pageCount > 1) str += '|';
	str += "\n";

	if (this.curPage < this.pageCount) {
		str += '<a onclick="' + this.funcName + '(' + this.next + ');" href="#" title="Go to the Next Page" >Next</a>&nbsp;|';
		str += "\n";
		str += '<a onclick="' + this.funcName + '(' + this.pageCount + ');" href="#" title="Go to the Last Page">Last</a>&nbsp;|';
		str += "\n";
	}
	if (this.pageCount > 1) {
		str += ' go to <input type="text" maxlength="5" size="4" value="#" name="' + call_PgNum_input_name + '" onblur="pageNumBlur(this);" onfocus="pageNumFocus(this);" onkeydown="var k=0;if(window.event)k=event,keyCode;else if(event.witch)k=event.witch; if(k==13){var p=getInputNumber(null, this, ' + this.pageCount + ');if(p!=0){' + this.funcName + '(p);}return false;}" />';
		str += "\n";
		str += '<input type="image" src="/images/BtnNext.gif" class="btnNext" name="' + call_PgNum_button_name + '" onclick="var p=getInputNumber(this, null, ' + this.pageCount + ');if(p!=0){' + this.funcName + '(p);}return false;" />';
		str += "\n";
	}
	str += '</div></td></tr></table></div>';
	str += "\n";

	if ((this.id1 != '') && document.getElementById(this.id1)) document.getElementById(this.id1).innerHTML = str;
	if ((this.id2 != '') && document.getElementById(this.id2)) document.getElementById(this.id2).innerHTML = str;
}
