﻿/*
AjaxPage---对象翻页功能
- AjaxPage -
	使用方式:
   //使用前  obj=new AjaxPage();先继续此对象;
*/
function AjaxPage(){this.G_CurrentPage=1;this.G_TTLPageCount;this.G_ShowPageCount=5;this.G_ShowPagePos=3;this.G_PageSize=20;this.G_RecCount=0;this.imgUrl="images/Ajaxframe/";return this}

AjaxPage.prototype.GetPageNum=function(){if(this.G_CurrentPage<=this.G_ShowPagePos||this.G_TTLPageCount<=this.G_ShowPageCount){var strPager="";if(this.G_TTLPageCount>=this.G_ShowPageCount){for(var i=0;i<this.G_ShowPageCount;i++){strPager+=(i+1)+","}}else{for(var i=0;i<this.G_TTLPageCount;i++){strPager+=(i+1)+","}}strPager=strPager.substring(0,strPager.length-1);return strPager}else if(this.G_CurrentPage>=this.G_TTLPageCount-(this.G_ShowPageCount-this.G_ShowPagePos)){var strPager="";for(var i=0;i<this.G_ShowPageCount;i++){strPager=(this.G_TTLPageCount-i)+","+strPager}strPager=strPager.substring(0,strPager.length-1);return strPager}else{var strPager="";var intAdd=1;var intMinus=this.G_ShowPagePos-1;for(var i=0;i<this.G_ShowPageCount;i++){if(i==(this.G_ShowPagePos-1)){strPager+=this.G_CurrentPage+","}else if(i>(this.G_ShowPagePos-1)){strPager+=(parseInt(this.G_CurrentPage)+intAdd)+",";intAdd++}else if(i<(this.G_ShowPagePos-1)){strPager+=(this.G_CurrentPage-intMinus)+",";intMinus--}}strPager=strPager.substring(0,strPager.length-1);return strPager}}

AjaxPage.prototype.addDom={addSpan:function(sCssName){var span=addElement("SPAN");span.className=sCssName;return span},addImg:function(sSrc,sAlt,sAlign){var sImg=addElement("IMG");sImg.src=sSrc;sImg.alt=sAlt;if(sAlign==null)sAlign="absmiddle";sImg.align=sAlign;return sImg},addInput:function(sCssName,sId,Svalue){var Input=addElement("INPUT");Input.type="text";Input.className=sCssName;Input.size=2;Input.value=Svalue;return Input},addButton:function(CSS){var btn=addElement("BUTTON");btn.className=CSS;return btn}}

AjaxPage.prototype.show_Page_now=function(){var PageDiv=this.options.PageDiv;if(this.G_PageSize!=null)this.G_PageSize=this.G_PageSize.trim();PageDiv.innerHTML="";var ShowPage=document.createElement("DIV");ShowPage.className="Pages";ShowPage.appendChild(this.addDom.addSpan("txt1").appendChild(addTextNode('总记录数:'+this.G_RecCount)));var ByPageNum=this.GetPageNum().split(",");var CurrentPage=parseInt(this.G_CurrentPage);if(CurrentPage-1!=0){var spanIndex_1=this.addDom.addButton("i_firstpage x-btn-button");spanIndex_1.onclick=this.FirstPage.bind(this);spanIndex_1.alt="首 页";ShowPage.appendChild(spanIndex_1)}else{var spanIndex_2=this.addDom.addButton("i_firstpage_require x-btn-button");spanIndex_2.alt="首 页";ShowPage.appendChild(spanIndex_2)}if(CurrentPage>1){var spanUp_1=this.addDom.addButton("i_previouspage x-btn-button");spanUp_1.alt="上一页";spanUp_1.onclick=this.BackPage.bind(this);ShowPage.appendChild(spanUp_1)}else{var spanUp_2=this.addDom.addButton("i_previouspage_require x-btn-button");spanUp_2.alt="已经是首页了";ShowPage.appendChild(spanUp_2)}for(var i=0;i<ByPageNum.length;i++){var PageNum=parseInt(ByPageNum[i]);if(PageNum==CurrentPage){var spanPageNum_2=this.addDom.addSpan("pageSel");spanPageNum_2.appendChild(addTextNode(PageNum));ShowPage.appendChild(spanPageNum_2)}else{var spanPageNum_1=this.addDom.addSpan("pageNum hand");spanPageNum_1.appendChild(addTextNode(PageNum));spanPageNum_1.onclick=this.GetPage.bind(this,PageNum);ShowPage.appendChild(spanPageNum_1)}}if(CurrentPage<this.G_TTLPageCount){var spanNext_1=this.addDom.addButton("i_nextpage x-btn-button");spanNext_1.onclick=this.NextPage.bind(this);spanNext_1.alt="下一页";ShowPage.appendChild(spanNext_1)}else{var spanNext_2=this.addDom.addButton("i_nextpage_require x-btn-button");spanNext_2.alt="已经是最后一页了";ShowPage.appendChild(spanNext_2)}if(CurrentPage==this.G_TTLPageCount&&parseInt(CurrentPage)+1>parseInt(this.G_TTLPageCount)){var spanLast_2=this.addDom.addButton("i_lastpage_require x-btn-button");spanLast_2.alt="末  页";ShowPage.appendChild(spanLast_2)}else{var spanLast_1=this.addDom.addButton("i_lastpage x-btn-button");spanLast_1.onclick=this.LastPage.bind(this);spanLast_1.alt="下一页";ShowPage.appendChild(spanLast_1)}this.get_page=this.addDom.addInput("page_input","get_page",CurrentPage);this.get_Size=this.addDom.addInput("page_input","get_Size",this.G_PageSize);ShowPage.appendChild(this.addDom.addSpan("txt1").appendChild(addTextNode("当前页码")));ShowPage.appendChild(this.get_page);ShowPage.appendChild(this.addDom.addSpan("txt1").appendChild(addTextNode("每页记录")));ShowPage.appendChild(this.get_Size);var spanUpDate=this.addDom.addSpan("Button");spanUpDate.onclick=this.get_Page_Size.bind(this);spanUpDate.appendChild(this.addDom.addImg(this.imgUrl+"done.gif","提 交","absmiddle"));ShowPage.appendChild(spanUpDate);PageDiv.appendChild(ShowPage);CkIsNumber(this.get_page,"int"),CkIsNumber(this.get_Size,"int")}

AjaxPage.prototype.FirstPage=function(){this.G_CurrentPage=1;this.init(); };

AjaxPage.prototype.BackPage=function(){ this.G_CurrentPage=this.G_CurrentPage-1; this.init(); }; 

AjaxPage.prototype.GetPage=function(strNum){this.G_CurrentPage=parseInt(strNum); this.init(); };
AjaxPage.prototype.NextPage=function(){this.G_CurrentPage=parseInt(this.G_CurrentPage)+1; this.init(); }
AjaxPage.prototype.LastPage=function(){ this.G_CurrentPage=this.G_TTLPageCount;this.init(); };

AjaxPage.prototype.get_Page_Size=function(){var get_page=parseInt(this.get_page.value);var get_P_S=parseInt(this.get_Size.value);if(get_page>this.G_TTLPageCount){get_page=this.G_TTLPageCount}else if(get_page<1||get_page==""){get_page=1}if(get_P_S>this.G_RecCount){get_P_S=this.G_RecCount}else if(get_P_S<1){get_P_S=1}else if(get_P_S==""){get_P_S=this.G_PageSize}var pageNow=parseInt(this.G_RecCount/get_P_S);if(pageNow<get_page){get_page=pageNow}if(get_page!=this.G_CurrentPage||get_P_S!=this.G_PageSize){this.G_CurrentPage=get_page;this.G_PageSize=get_P_S;this.init()}}
