﻿/*
	客户端页码跳转生成脚本 Write By DLL
*/
function Page(){
     this.size;            //当前页两侧伸展页数
     this.PageSize;            //每页记录数
     this.style;            /* 统计信息HTML原型代码
                       元素对应:记录总数:"{$count}".总页数:"{$pagecount}".每页记录数:"{$pagesize}".当前页码:"{$pagenum}"
                       */

     this.liststyle;            /* 页码列表HTML原型代码,规则形式,
                       将被分割成一个6个元素的数组.
                       例如为"首页|上一页|下一页|尾页|当前页码|普通页码"样子的HTML代码
                       以"{$pagenum}"为页码元素,处理后此位置则为页码值
                       如果有一项或多项为空仍需预留位置,如"首页|||尾页|当前页码|普通页码" */
     this.jumpstyle;            //自由跳转HTML原型代码
     this.count;            //总记录数
     this.urlstyle;            //附加页码前的链接代码及样式.以"{$page}"为页码元素,处理后此位置则为此链接页码值."{$location}"为链接文本元素
     this.listurlstyle;      //附加页码前的链接代码及样式(使用于页码列表).以"{$page}"为页码元素,处理后此位置则为此链接页码值."{$location}"为链接文本元素
     this.locationPage = 1;      //当前页码
     this.jumpurl;            //自由跳转的URL前缀
     this.ParName = "page";      //页码的URL参数名
	 this.FirstPage = "";

     this.CreateMain = function(){
           if(this.style==null){
                 return("缺少页码样式");
           }else{
                 var temp
                 var pageCount
                 if(this.count%this.PageSize!=0){
                       pageCount = parseInt(this.count / this.PageSize) + 1;
                 }else{
                       pageCount = parseInt(this.count / this.PageSize);
                 }
                 if(this.locationPage>pageCount) this.locationPage = pageCount;
                 temp = this.style;
                 temp = temp.replace("{\$count}",this.count);
                 temp = temp.replace("{\$pagecount}",pageCount);
                 temp = temp.replace("{\$pagesize}",this.PageSize);
                 temp = temp.replace("{\$pagenum}",this.locationPage);
                 return(temp);
           }
     }

     this.CreateList = function(){
           if(this.liststyle==null){
                 return("缺少页码样式");
           }else{                  
                 if(this.count%this.PageSize!=0){
                       ALLPageCount = parseInt(this.count / this.PageSize) + 1;
                 }else{
                       ALLPageCount = parseInt(this.count / this.PageSize);
                 }
                 if(this.locationPage>ALLPageCount) this.locationPage = ALLPageCount;
                 var temp
                 var returnTemp = "";
                 var returnstr = "";
                 temp = this.liststyle;
                 temp = temp.split("|");
                 if(temp.length<6) return("页码列表样式定义错误");
                 if(temp[0]!=""&&this.locationPage>1){
					   if (this.FirstPage == '')
					   {
							returnTemp += this.urlstyle;
					   }else{
							returnTemp += this.FirstPage;
					   }
						returnTemp = returnTemp.replace("{\$page}",1);
						returnTemp = returnTemp.replace("{\$location}",temp[0]);
                 }
                 if(temp[1]!=""&&this.locationPage>1){
					   if (this.FirstPage == ''|| this.locationPage != 2)
					   {
							returnTemp += this.urlstyle;
					   }else{
							returnTemp += this.FirstPage;
					   }
                       returnTemp = returnTemp.replace("{\$page}",this.locationPage - 1);
                       returnTemp = returnTemp.replace("{\$location}",temp[1]);
                 }
                 if(temp[5]!=""){
                       var start = this.locationPage - this.size;
                       var end = this.locationPage + this.size;
                       var looptemp = "";
                       if(start<1) start = 1;
                       if(end-start<this.size*2) end = start + this.size * 2;
                       if(end>ALLPageCount) end = ALLPageCount;
		
                       for(var i=start;i<=end;i++){
                             if(i==this.locationPage&&temp[4]!=""){
									looptemp += temp[4];
                             }else{
								
								if (this.FirstPage == ''|| i != 1)
								{
									looptemp += this.listurlstyle;
								}else{
									looptemp += this.FirstPage;
								}
								looptemp = looptemp.replace("{\$location}",temp[5]);
                             }
                             looptemp = looptemp.replace("{\$page}",i);
                             looptemp = looptemp.replace("{\$pagenum}",i);
                       }

                       returnTemp += looptemp;
                 }
                 if(temp[2]!=""&&this.locationPage<ALLPageCount){
                       returnTemp += this.urlstyle;
                       returnTemp = returnTemp.replace("{\$page}",this.locationPage + 1);
                       returnTemp = returnTemp.replace("{\$location}",temp[2]);
                 }
                 if(temp[3]!=""&&this.locationPage<ALLPageCount){
                       returnTemp += this.urlstyle;
                       returnTemp = returnTemp.replace("{\$page}",ALLPageCount);
                       returnTemp = returnTemp.replace("{\$location}",temp[3]);
                 }
                 return(returnTemp);
           }
     }
     this.CreateFreejump = function(){
           var temp = this.jumpstyle;
           if(temp==null) return("缺少页码样式");

           temp = temp.replace("{$page}",this.locationPage)
           return(temp);
     }
     this.ChangePage = function(){
           var GotoPage = document.all.PageValue.value;
           var ALLPageCount = 0;
           if(GotoPage!=null&&GotoPage!=""){
                 GotoPage = parseInt(GotoPage);
                 if(this.count%this.PageSize!=0){
                       ALLPageCount = parseInt(this.count / this.PageSize) + 1;
                 }else{
                       ALLPageCount = parseInt(this.count / this.PageSize);
                 }
                 if(GotoPage>0&&GotoPage<=ALLPageCount) location.href=this.jumpurl + this.ParName + "=" + GotoPage;
           }
     }
     this.GetPage = function(){
           var str = this.ParName + "=";
           var gvalue = 1;
           if(location.href.indexOf(str)>0){
                 gvalue = location.href.substring(location.href.indexOf(str) + str.length,location.href.length);
                 if(gvalue.indexOf('&')>0) gvalue = gvalue.substring(0,gvalue.indexOf('&'));
				if(gvalue.indexOf("#")>0)
				{
					gvalue = gvalue.split("#")[0];
				}
                 if(gvalue=="") gvalue = 1;
           }
           gvalue = parseInt(gvalue);
           if(gvalue<1)gvalue = 1;
           return(parseInt(gvalue));
     }
}

/*
使用实例
var P = new Page();
P.ParName = "page";
P.size = 5;
P.PageSize = 10;
P.count = 144532;
P.jumpurl = "msn.htm?";
P.locationPage = P.GetPage();

P.style = "共有{$count}条记录.共分{$pagecount}页.每页{$pagesize}条.当前第{$pagenum}页. ";
P.liststyle = "首页|<<|>>|末页|<font color=red><b>{$pagenum}</b></font>|<b>{$pagenum}</b>";
P.urlstyle = " <a href=\"msn.htm?page={$page}\">{$location}</a> ";
P.listurlstyle = " [ <a href=\"msn.htm?page={$page}\">{$location}</a> ] ";
P.jumpstyle = "<input type=\"text\" size=\"5\" value=\"{$page}\" name=\"PageValue\"> <input type=button value=\"跳转\" onclick=\"P.ChangePage()\">";
viewlayer.innerHTML = P.CreateMain() + P.CreateList() + P.CreateFreejump();

*/
