

function Page(sObjName, nTotalRecord, nNumPerPage) {
	this._sObjName = sObjName;
	this._nCurPage = 0;
	this._nNumPerPage = nNumPerPage;
	this.setTotalRecord(nTotalRecord);
}

Page.prototype.setCurPage= function(nCurPage) {
	this._nCurPage= nCurPage;
}

Page.prototype.setTotalRecord = function(nTotalRecord) {
	this._nTotalRecord = nTotalRecord;
	if(nTotalRecord == 0)
	{
		this._nTotalPages = 0;
	}
	else if(nTotalRecord <= this._nNumPerPage)
	{
		this._nTotalPages = 1;
	}
	else if(nTotalRecord % this._nNumPerPage==0)
	{
		this._nTotalPages = nTotalRecord / this._nNumPerPage;
	}
	else
	{
		this._nTotalPages = parseInt(nTotalRecord / this._nNumPerPage) + 1;
	}
}

//根据需要重载此函数
Page.prototype.goPage = function(nGotoPage) {
	this._nCurPage = parseInt(nGotoPage,10);
	return 0;
}

//打印分页信息
Page.prototype.getPageInfo = function(){		
	var nShowNum = 2;
	var bufferPageList = new StringBuffer();
	bufferPageList.append(this._showTotal());
	if(this._nCurPage>1) {
		bufferPageList.append(this._showFirstLink());
		bufferPageList.append(this._showPreLink(this._nCurPage -1));		
	}
	
	if(this._nTotalPages >10){
		var prePage = this._nCurPage - nShowNum;
		if(prePage>3){
			bufferPageList.append(this._showLink(1));
			bufferPageList.append(this._showNull());
		}else {
			prePage=1;
		}
		var frePage = this._nCurPage + nShowNum;
		if(frePage > this._nTotalPages -3) {
			frePage = this._nTotalPages;
		}
		for(var i=prePage; i<=frePage; i++){
			if(i==this._nCurPage) {
				bufferPageList.append(this._showThis(i));
			}
			else {
				bufferPageList.append(this._showLink(i));
			}
		}
		if(frePage <= this._nTotalPages -3){
			bufferPageList.append(this._showNull());
			bufferPageList.append(this._showLink(this._nTotalPages));
		}
		if(this._nCurPage <this._nTotalPages ) {
			bufferPageList.append(this._showNextLink(this._nCurPage+1));
			bufferPageList.append(this._showLastLink());
		}
		bufferPageList.append(this._showGoLink());
	}else{
		for(var i=1;i<=this._nTotalPages ;i++){
			if(i == this._nCurPage) {
				if(this._nTotalPages > 1){
					bufferPageList.append(this._showThis(i));
				}
			}
			else {
				bufferPageList.append(this._showLink(i));
			}			
		}
		if(this._nCurPage < this._nTotalPages ) {
			bufferPageList.append(this._showNextLink(this._nCurPage + 1));
			bufferPageList.append(this._showLastLink());
		}	
	}	
	return bufferPageList.toString();
}

//打印分页信息
Page.prototype.getPageInfo2 = function(){		
	var nShowNum = 2;
	var bufferPageList = new StringBuffer();
	bufferPageList.append(this._showTotal());
	if(this._nCurPage>1) {
		bufferPageList.append(this._showFirstLink());
		bufferPageList.append(this._showPreLink(this._nCurPage -1));		
	}
	
	if(this._nTotalPages >10){
		var prePage = this._nCurPage - nShowNum;
		if(prePage>3){
			bufferPageList.append(this._showLink(1));
			bufferPageList.append(this._showNull());
		}else {
			prePage=1;
		}
		var frePage = this._nCurPage + nShowNum;
		if(frePage > this._nTotalPages -3) {
			frePage = this._nTotalPages;
		}
		for(var i=prePage; i<=frePage; i++){
			if(i==this._nCurPage) {
				bufferPageList.append(this._showThis(i));
			}
			else {
				bufferPageList.append(this._showLink(i));
			}
		}
		if(frePage <= this._nTotalPages -3){
			bufferPageList.append(this._showNull());
			bufferPageList.append(this._showLink(this._nTotalPages));
		}
		if(this._nCurPage <this._nTotalPages ) {
			bufferPageList.append(this._showNextLink(this._nCurPage+1));
			bufferPageList.append(this._showLastLink());
		}
		bufferPageList.append(this._showGoLink2());
	}else{
		for(var i=1;i<=this._nTotalPages ;i++){
			if(i == this._nCurPage) {
				if(this._nTotalPages > 1){
					bufferPageList.append(this._showThis(i));
				}
			}
			else {
				bufferPageList.append(this._showLink(i));
			}			
		}
		if(this._nCurPage < this._nTotalPages ) {
			bufferPageList.append(this._showNextLink(this._nCurPage + 1));
			bufferPageList.append(this._showLastLink());
		}	
	}	
	return bufferPageList.toString();
}

Page.prototype._showTotal = function (){
	return this._nCurPage +'/' + this._nTotalPages
}

Page.prototype._showLink = function (nLinkNum){
	return " <a href='#' onclick=\""+this._sObjName+".goPage('" + nLinkNum + "');return false;\">[" + nLinkNum + "]</a> ";
}

Page.prototype._showThis = function(nCurPage){
	return " " + nCurPage + " ";
}

Page.prototype._showFirstLink= function(){
	return " <a href='#' onclick=\""+this._sObjName+".goPage('1');return false;\">首页</a> ";
}

Page.prototype._showPreLink= function(nPrePage){
	return " <a href='#' onclick=\""+this._sObjName+".goPage('"+nPrePage+"');return false;\">上一页</a> ";
}

Page.prototype._showNextLink = function(nNextPage){
	return " <a href='#' onclick=\""+this._sObjName+".goPage('"+nNextPage+"');return false;\">下一页</a> ";
}

Page.prototype._showLastLink = function(){
	return " <a href='#' onclick=\""+this._sObjName+".goPage('"+this._nTotalPages+"');return false;\">末页</a> ";
}

Page.prototype._showGoLink = function(){
	return ' <input type="text" maxlength="4" id="page_'+this._sObjName+'" value="'+this._nCurPage+'" onkeydown="if(event.keyCode==13) '+this._sObjName+'.goPage($(\''+'page_'+this._sObjName+'\').value);" style="width:25px;height:22px;padding:0px;"> <input style="width:23px;height:22px;"  type="button" value="go" onclick="'+this._sObjName+'.goPage($(\''+'page_'+this._sObjName+'\').value);"> ';
}

Page.prototype._showGoLink2 = function(){
	return ' <input type="text" maxlength="4" id="page_'+this._sObjName+'2" value="'+this._nCurPage+'" onkeydown="if(event.keyCode==13) '+this._sObjName+'.goPage($(\''+'page_'+this._sObjName+'2\').value);" style="width:25px;height:22px;padding:0px;"> <input style="width:23px;height:22px;"  type="button" value="go" onclick="'+this._sObjName+'.goPage($(\''+'page_'+this._sObjName+'2\').value);"> ';
}

Page.prototype._showNull = function() {
	return "...";
}

Page.prototype._checkPage = function (nPageNum)
{
    if(nPageNum == "")
    {
        alert("您输入的页码为空，请输入正确的页码！");
        return false;
    }
	nPageNum = parseInt(nPageNum,10);
	if(isNaN(nPageNum))
	{
		alert("您输入的页码包含其它字符，请输入正确的页码！");
		return false;
	}
	else if( nPageNum <= 0 )
	{
		alert("您输入的页码过小，请输入正确的页码！");
		return false;
	}
	else if( nPageNum > this._nTotalPages )
	{
		alert("您输入的页码过大，请输入正确的页码！");
		return false;
	}	
	
	return true;
}
















