if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(prefix) {
    return (this.indexOf(prefix) === 0);
  };
}

if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
      return false;
    }
    return (this.lastIndexOf(suffix, startPos) == startPos);
  };
}

if (!String.prototype.withoutPrefix) {
  String.prototype.withoutPrefix = function(prefix) {
    if (this.startsWith(prefix)) {
      return this.substr(prefix.length);
    } else {
      return this;
    }
  };
}

if (!String.prototype.withoutSuffix) {
  String.prototype.withoutSuffix = function(suffix) {
    if (this.endsWith(suffix)) {
      return this.substr(0, this.length - suffix.length);
    } else {
      return this;
    }
  };
}

if (!String.prototype.strip) {
  String.prototype.strip = function() {
    return this.replace(/^\s*(.*?)\s*$/, "$1");
  };
}

if (!String.prototype.format) {
  String.prototype.format = function() {
    var args = arguments;
    return this.replace(
      /\{\{[^{}]*\}\}|\{(\d+)(,\s*([\w.]+))?\}/g,
      function(m, a1, a2, a3) {
        if (m.chatAt == '{') {
          return m.slice(1, -1);
        }
        var rpl = args[a1];
        if (a3) {
          var f = eval(a3);
          rpl = f(rpl);
        }
        return rpl ? rpl : '';
      });
  };
}

if (!String.prototype.isBlank) {
  String.prototype.isBlank = function(s) {
    return (!s || (/^\s*$/).test(s));
  };
}

if (!String.prototype.compare) {
  String.prototype.compare = function(s1, s2) {
    if (s1 == s2) {
      return 0;
    }
    if (s1 > s2) {
      return 1;
    }
    return -1;
  };
}

if (!String.prototype.replaceAll) {
	String.prototype.replaceAll = function(AFindText,ARepText)
	{
		var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
		return this.replace(raRegExp,ARepText);
	}
}

String.prototype.replaceAllString = function(AFindText,ARepText)
{
		var raRegExp = new RegExp(AFindText,"ig");
		return this.replace(raRegExp,ARepText);
}


function StringBuffer() { this.buffer = []; } 
StringBuffer.prototype.append = function(string) 
{ 
	this.buffer.push(string); 
	return this; 
} 
StringBuffer.prototype.toString = function()
{ 
	return this.buffer.join(""); 
}



function Page()
{
	var isShowFirstLast=true;
	this._padLeft=0;
	this._padRight=0;
	this.totalPages = 0;
	this.totalSize=0;
	this._beforeNext = true;
	this.displayPages=0;
	this.useFirstLast = true;
	this.firstText = "";
	this.beforeText = "";
}
Page.prototype.FIRST_PAGE_INDEX=1;
Page.prototype.PAGESIZE_MIN=1;
Page.prototype.PAGESIZE_DEFAULT=20;
Page.prototype.DISPLAYPAGES_MIN=3;
Page.prototype.DISPLAYPAGES_DEFAULT=10;
Page.prototype.FIRSTTEXT_DEFAULT='首页';
Page.prototype.LASTTEXT_DEFAULT='末页';
Page.prototype.BEFORETEXT_DEFAULT='<<';
Page.prototype.NEXTTEXT_DEFAULT='>>';
Page.prototype.PAGE_KEY_DEFAULT='page';
Page.prototype.PageKey = function( key )
{
	if( key )
	{
		this.pageKey = key;
	}
	else
	{
		var _key = this.pageKey;
		if( _key==undefined || _key=='' )
		{
			_key = this.PAGE_KEY_DEFAULT;
		}
		return _key;
	}
}
Page.prototype.isShowFirstLast = function( _value )
{
	if( _value )
	{
		this.firstLast = _value;
	}
	else
	{
		var _firstLast = this.firstLast;
		if( _firstLast==undefined || _firstLast=='' )
		{
			_firstLast = true;
		}
		return _firstLast;
	}
}

Page.prototype.beforeNext = function( _value )
{
	if( _value )
	{
		this._beforeNext = _value;
	}
	else
	{
		return this._beforeNext;
	}
}
Page.prototype.target = function( _value )
{
	if( _value )
	{
		this.target = _value;
	}
	else
	{
		var _target = this.target;
		if( _target==undefined || _target=='' )
		{
			_target = '_self';
		}
		return _target;
	}
}
Page.prototype.firstTextVal = function( _value )
{
	if( _value )
	{
		this.firstText = _value;
	}
	else
	{
		var _firstText = this.firstText;
		if( _firstText==undefined || _firstText=='' )
		{
			_firstText = this.FIRSTTEXT_DEFAULT;
		}
		return _firstText;
	}
}
Page.prototype.nextTextVal = function( _value )
{
	if( _value )
	{
		this.nextText = _value;
	}
	else
	{
		var _nextText = this.nextText;
		if( _nextText==undefined || _nextText=='' )
		{
			_nextText = this.NEXTTEXT_DEFAULT;
		}
		return _nextText;
	}
}
Page.prototype.lastTextVal = function( _value )
{
	if( _value )
	{
		this.lastText = _value;
	}
	else
	{
		var _lastText = this.lastText;
		if( _lastText==undefined || _lastText=='' )
		{
			_lastText = this.LASTTEXT_DEFAULT;
		}
		return _lastText;
	}
}
Page.prototype.beforeTextVal = function( _value )
{
	if( _value )
	{
		this.beforeText = _value;
	}
	else
	{
		var _beforeText = this.beforeText;
		if( _beforeText==undefined || _beforeText=='' )
		{
			_beforeText = this.BEFORETEXT_DEFAULT;
		}
		return _beforeText;
	}
}
Page.prototype.displayPage = function( _value )
{
	if( _value )
	{
		this.displayPages = _value;
	}
	else
	{
		var _displayPage = this.displayPages;
		if( _displayPage==undefined || _displayPage==0 )
		{
			_displayPage =this.DISPLAYPAGES_DEFAULT;
		}
		return _displayPage;
	}
}
Page.prototype.Href = function( _value )
{
	if( _value )
	{
		this.href = _value;
	}
	else
	{
		var _href = this.href;
		if( _href==undefined || _href=='' )
		{
			_href = '';
		}
		return _href;
	}
}

Page.prototype.PageIndex = function( _value )
{
	if( _value )
	{
		this.pageIndex = _value;
	}
	else
	{
		var _pageIndex = this.pageIndex;
		if( _pageIndex==undefined || _pageIndex=='' )
		{
			this.pageIndex = 1;
		}
		
		return this.pageIndex;
	}
}
Page.prototype.PageSize = function( _value )
{
	if( _value )
	{
		this.pageSize = _value;
	}
	else
	{
		var _pageSize = this.pageSize;
		if( _pageSize==undefined  )
		{
			this.pageSize = this.PAGESIZE_DEFAULT;
		}
		
		return this.pageSize;
	}
}
Page.prototype.TotalSize = function( _value )
{
	if( _value )
	{
		this.totalSize = _value;
	}
	else
	{
		var _totalSize = this.totalSize;
		if( _totalSize==undefined  )
		{
			this.totalSize = 0;
		}
		
		return this.totalSize;
	}
}


Page.prototype.CalcPadding = function(displayPages)
{
		 this._padLeft = parseInt(displayPages) / 2;
		 this._padRight = this._padLeft;

		if (parseInt(displayPages) % 2 == 0)
			this._padLeft--;

		if ((this.PageIndex() - this._padLeft) <= 0)
		{
			this._padRight += this._padLeft - this.PageIndex() + 1;
			this._padLeft = this.PageIndex() - 1;
		}
		else if ((this.PageIndex() + this._padRight) > this.TotalPage())
		{
			this._padLeft += this._padRight - (this.TotalPage() - this.PageIndex());
			this._padRight = this.TotalPage() - this.PageIndex();
		}
}

Page.prototype.GetIdX = function()
{
		var counter =this._padLeft;
		var idx = 1;
		while (counter > 0)
		{	
			idx = this.PageIndex() - counter;
			if (idx >= this.FIRST_PAGE_INDEX)
				break;				
			counter--;
		}
		return (idx);
}

Page.prototype.RenderLink = function(linkIndex, display, isCurrent)
{
	var queryString ="";
	var curClass ="";
	queryString = document.location.search;
	if( queryString=="" )
	{
		queryString =this.PageKey()+"="+linkIndex;
	}
	
	queryString = queryString.replaceAll("\\?", "\\&");
	queryString = queryString.replaceAll("&"+this.PageKey()+"=[0-9]+","");
	if(!queryString==("") && !queryString.startsWith("&")){
		queryString = "&"+queryString;
	}
	queryString =this.PageKey()+"="+linkIndex+queryString;
	
	queryString ="?"+queryString;
	
	if(  null==this.Href() || '' == this.Href())
	{
		this.Href(document.location.host+queryString);
	}
	else
	{
			if( this.Href().indexOf("?")>0 )
			{
				var _href = this.Href();
				var _queryString = _href.substring(_href.indexOf("?"));
				_href = _href.substring(0,_href.indexOf("?"));
				_queryString = _queryString.replaceAll("?", "&");
				_queryString = _queryString.replaceAllString("&"+this.PageKey()+"=[0-9]+","");
				if((!_queryString=="")&& !_queryString.startsWith("&")){
					_queryString = "&"+_queryString;
				}
				this.Href(_href+"?"+this.PageKey()+"="+linkIndex+_queryString);
			}
	}
	var url = "";
	if( isCurrent )
	{
		url = "<a class='current'>"+display+"</a>";
	}
	else
	{
		url = "<a href=\""+this.Href()+"\">"+display+"</a>";
	}
	
	return url;
}

Page.prototype.TotalPage = function()
{
  		this.totalPages = Math.floor((this.TotalSize()+this.PageSize()-1)/this.PageSize());
  		if( this.totalPages<1 )
  			this.totalPages=1;
      	return this.totalPages;
    }
Page.prototype.display = function()
{
	var _buffer = new StringBuffer();
	_buffer.append("<div class=\"page\">");
	
	this.CalcPadding (this.displayPage());
	//输出首页导航
	this.WriteFirst (_buffer);
	//输出上一页
	this.WriteBefore(_buffer);
	
	//输出分页详细内容
	this.WriteDetail (_buffer);
	
	//输出后一页
	this.WriteNext(_buffer);
	//输出最后页导航
	this.WriteLast (_buffer);	
	//显示共多少页
	//WriteTotalPage(_buffer);
	_buffer.append("</div>");
	return _buffer.toString();
	//return this.FIRSTTEXT_DEFAULT+":"+this.PageKey();
}

Page.prototype.displayDesc = function()
{
	var _buffer = new StringBuffer();
	_buffer.append("<div class=\"page\">");

	this.CalcPadding (this.displayPage());
		
	//输出最后页导航
	this.WriteLast (_buffer);

	//输出后一页
	this.WriteNext(_buffer);

	//输出分页详细内容
	this.WriteDetailDesc (_buffer);
			
	//输出上一页
	this.WriteBefore(_buffer);

	//输出首页导航
	this.WriteFirst (_buffer);
	
	//显示共多少页
	//WriteTotalPage(_buffer);
	_buffer.append("</div>");
	return _buffer.toString();
	//return this.FIRSTTEXT_DEFAULT+":"+this.PageKey();
}

Page.prototype.WriteBefore = function( _buffer )
{
	
	if ((this.PageIndex()>this.FIRST_PAGE_INDEX)&& this.beforeNext()) 
	{
		_buffer.append(this.RenderLink(this.PageIndex()-1, this.beforeTextVal(),false));
		//this.WriteConditional(_buffer, get_spacer(), _useSpacer);
	}
}

Page.prototype.WriteFirst = function( _buffer)
{
	if (this.PageIndex() > this.FIRST_PAGE_INDEX && this.useFirstLast) 
	{
		_buffer.append(this.RenderLink(this.FIRST_PAGE_INDEX, this.firstTextVal(),false));
		//this.WriteConditional(_buffer, get_spacer(), _useSpacer);
	}
}

Page.prototype.WriteNext = function( _buffer)
{
	if ((this.PageIndex()<this.TotalPage())&& this.beforeNext()) 
	{
		_buffer.append(this.RenderLink(this.PageIndex()+1, this.nextTextVal(),false));
	}
}

Page.prototype.WriteLast = function(_buffer)
{
	if ((this.PageIndex()< this.TotalPage()) && this.useFirstLast) 
	{
		_buffer.append(this.RenderLink(this.TotalPage(), this.lastTextVal(),false));
	}
}
  	
Page.prototype.WriteDetail = function(_buffer)
{
	var idx = Math.floor(this.GetIdX());
//	this.WritePreDisplay(_buffer);
	for (var i = idx; i < idx + this.displayPage(); i++)
	{
		if (i >= this.FIRST_PAGE_INDEX && i <= this.TotalPage())
		{
			_buffer.append(this.RenderLink(i,i, i == this.PageIndex()));					
		}
	}
//	this.WriteSufDisplay(_buffer);
	
}

Page.prototype.WriteDetailDesc = function(_buffer)
{
	var idx = Math.floor(this.GetIdX());
//	this.WritePreDisplay(_buffer);
	for (var i = idx + this.displayPage() - 1; i >= idx; i--)
	{
		if (i >= this.FIRST_PAGE_INDEX && i <= this.TotalPage())
		{
			_buffer.append(this.RenderLink(i,i, i == this.PageIndex()));					
		}
	}
//	this.WriteSufDisplay(_buffer);
}

Page.prototype.WriteSufDisplay = function(_buffer)
{
	var idx = Math.floor(this.GetIdX())
	if( this._padLeft==0 )
	{
		this._padLeft = this.displayPage()/ 2;
		this._padRight = this._padLeft;

		if (this.displayPage() % 2 == 0)
		{
			this._padLeft--;
		}
	}
	if( (idx+this.displayPage()+this._padLeft)<this.TotalPage() )
	{
		_buffer.append(this.RenderLink(idx+this.displayPage()+this._padLeft, ">|",false));
	}
}
  	

Page.prototype.WritePreDisplay = function(_buffer)
{
	var idx = Math.floor(this.GetIdX())
	if( idx> this.displayPage() )
	{
		var index = idx-this._padLeft-this.displayPage();
		if( index<=0 )
			index=1;
			_buffer.append(this.RenderLink(index, "|<",false));
	}
}

Page.prototype.WriteConditional = function(_buffer, value, condition)
{
	if (condition)
	{
		 _buffer.append(value);
	}
}