// +------------------------------------------------------------+
// |               HTML Table Pager Version 1.3                 |
// +------------------------------------------------------------+
// | Last Modified:                  16-Dec-2006                |
// | Web Site:                       http://joeli.t35.com       |
// | Web Site (2):                   http://joeli.27h.org       |
// | EMail:                          licp at hotmail dot com    |
// +------------------------------------------------------------+
// |       Copyright 2006  Joe Li     All Rights Reserved.      |
// |     Permission given to use this script in ANY kind of     |
// |      applications if header lines are left unchanged.      |
// +------------------------------------------------------------+
// v1.1:add a function "getNavBarHTML()" in order to customize HTML of NavBar
// v1.2:customize the page layout and function
// v1.3:modify page number from 9 to 10, add functions: nextTenPages() , prevTenPages()
// v1.31:decrease the space between page number



function Pager(id, thePageSize) {
	
	this._o = document.getElementById(id);
	
	if (this._o)
	{
		this._displayElement(this._o, false); //true :show header
	}
	
	this.pageIndex = 0 ;
	this.RecordCount = 0;
	this.PageCount = 0; 
	if(thePageSize == null)
	{
		this.PageSize = 10;
	}
	else
	{
		this.PageSize = parseInt(thePageSize);
	}
	
	this._setRecordCount();
	
	this._setPageCount();
	
	this.AbsolutePage = (this.RecordCount > 0 ? 1: 0);
	this.DivObj = null;
	this.objName = "";
	this.theTargetPageTxt = null;
	
	
}

Pager.prototype.reload = function() {
	this._setRecordCount();
	this.display();
};

Pager.prototype._setRecordCount = function() {
	this.RecordCount = (this._o && this._o.rows ? this._o.rows.length - 1: 0);
};

Pager.prototype._setPageCount = function() {

	if (this.RecordCount <= 0) 
	{
		
		this.PageCount = 0;
	} else 
	{
		this.PageCount = Math.ceil(this.RecordCount / this.PageSize);
	}
	
};

Pager.prototype.toString = function() {
	return '<p>Pager:' + '<br />' +
		'RecordCount = ' + this.RecordCount + '<br />' +
		'PageSize = ' + this.PageSize + '<br />' +
		'PageCount = ' + this.PageCount + '<br />' +
		'AbsolutePage = ' + this.AbsolutePage + '</p>';
};

Pager.prototype._displayElement = function(e, bool) {
	if (!e || !e.style) return;
	e.style.display = (bool? '': 'none');
};

Pager.prototype.display = function() {
	
	this._setPageCount();
	if (this.RecordCount <= 0 || this.PageCount <= 0)
	{
		return;
	}
	this.AbsolutePage = parseInt(this.AbsolutePage);
	if (isNaN(this.AbsolutePage) || this.AbsolutePage < 1 || this.AbsolutePage > this.PageCount) this.AbsolutePage = 1;
	var fRec = (this.AbsolutePage - 1) * this.PageSize + 1;
	var lRec = this.AbsolutePage * this.PageSize;
	this._displayElement(this._o, false);
	for (var i = 0; i <= this.RecordCount; i++) this._displayElement(this._o.rows[i], i == 0 || (i >= fRec && i <= lRec));
	this._displayElement(this._o, true);
	if(!this.DivObj) 
	{
		return;
	}
	this.DivObj.innerHTML = this.getNavBarHTML(this.objName);
};

Pager.prototype.prevPage = function() {
	if (this.AbsolutePage - 1 >= 1) {
		this.AbsolutePage--;
		this.display();
	}
};

Pager.prototype.nextPage = function() {
	if (this.AbsolutePage + 1 <= this.PageCount) {
		this.AbsolutePage++;
		this.display();
	}
};
Pager.prototype.prevTenPages = function() {
	if (this.AbsolutePage - 10 >= 1) {
		this.AbsolutePage-=10;
		this.display();
	}
	else
	{
		this.AbsolutePage=1;
		this.display();
	}
};

Pager.prototype.nextTenPages = function() {
	if (this.AbsolutePage + 10 <= this.PageCount) {
		this.AbsolutePage+=10;
		this.display();
	}
	else
	{
		this.AbsolutePage=this.PageCount;
		this.display();		
	}	
};

//Pager.prototype.changePage = function() {
//		var idx = document.getElementById("pageIndex").selectedIndex ;
//		this.AbsolutePage = idx+1;
//		this.display();
//};

Pager.prototype.navBar = function(varName, eName) {
	this.objName = varName;
	this.DivObj = document.getElementById(eName);
	if (!this.DivObj)
	{
		return;
	}
	this._setPageCount();
	if (this.PageCount <= 1)
	{
		return;
	}
	this.DivObj.innerHTML = this.getNavBarHTML(varName);
};






Pager.prototype.getNavBarHTML = function(varName) {
	var s = '';
	s += '<div class="page_num" align="center">';
	s += '<img src="/wcm/Web/img/btn/page_last.gif" width="12" height="13" />';
	//s += '&nbsp;';
	s += '<a href="javascript:' + varName + '.prevTenPages()">上十頁</a>';	
	//s += '&nbsp;';
	s += '<a href="javascript:' + varName + '.prevPage()">上一頁</a>';
	//s += '&nbsp;';
	
	var i =0 ;
	var thePage = this.AbsolutePage;
	var minPage = this.AbsolutePage - 4;
	var maxPage = this.AbsolutePage + 5;//modify:4 -> 5
	if(this.PageCount<=10)//modify:9 -> 10
	{
		minPage =1;
		maxPage =this.PageCount;
	}
	else if(maxPage>this.PageCount)//for currentPage >= PageCount-4
	{
		var theOffSet = maxPage - this.PageCount ;
		minPage = minPage - theOffSet;
		maxPage = this.PageCount;
	}
	else if(minPage<1)//for currentPage<=4
	{
		maxPage = 10; //modify:9 -> 10	
		minPage = 1;	
	}
	else
	{
		//do nothing for formal case
	}
	for( i = minPage ; i <= maxPage; i++)
	{
		
			if(i!=this.AbsolutePage)
			{
				s+='<a href="javascript:' + varName + '.goToThePage('+i+')">'+i+'</a>';
			}
			else
			{
				s+= '<span class="page_current">'+this.AbsolutePage+'</span>';
			}
			//s += '&nbsp;';
			if(i>0 && i<10)
			{
				//s += '&nbsp;';
			}
		
	}
	
	s += '<span class="page_jump">跳至<input name="targetPageTxt" id="targetPageTxt" type="text" /> 頁';
//	s += '&nbsp;';
	s += '<a href="javascript:' + varName + '.goToThePageExt();" class="btn_2w">前往</a>';	
//	s += '&nbsp;';
	s += '<a href="javascript:' + varName + '.nextPage()">下一頁</a>';
	//s += '&nbsp;';
	s += '<a href="javascript:' + varName + '.nextTenPages()">下十頁</a>';
	//s += '&nbsp;';	
	s += '<img src="/wcm/Web/img/btn/page_next.gif" width="12" height="13" />';
	s += '</div>';
	return s;
};


Pager.prototype.goToThePage = function(varName) {
		
	if(varName!=null && varName!=="" && theIsNaN(varName)==false)
	{
		if (this.AbsolutePage > 0 && this.AbsolutePage <= this.PageCount) 
		{
			
			this.AbsolutePage = varName;
			this.display();
		}
		else
		{
			return;
		}
	}	
};
Pager.prototype.goToThePageExt = function() {
	
	var varName = getTheTargetPageNum();

	if(varName!=null && varName!=="" && theIsNaN(varName)==false)
	{
		if (this.AbsolutePage > 0 && this.AbsolutePage <= this.PageCount) 
		{
			
			this.AbsolutePage = varName;
			this.display();
		}
		else
		{
			return;
		}
	}	
};
function theIsNaN(varName)
{
	return this.isNaN(varName);
}
function getTheTargetPageNum()
{
	var theElementTxt = document.getElementById("targetPageTxt");
	if(theElementTxt != null)
	{
		return theElementTxt.value;
	}
	else
	{
		return 1;
	}	
}
