﻿/* 
Modification By CnEve 2006-10-01
----------------------
var pg = new showPages('pg');
pg.pageCount = 12; 
pg.argName = 'p';   
pg.printHtml();       

Supported in Internet Explorer, Mozilla Firefox
*/

function showPages(name) {
	this.name = name;     
	this.page = 1;         
	this.pageCount = 1;   
	this.recordCount = 0;  
	this.number = 10;	   
	this.argName = 'page'; 
}

showPages.prototype.getPage = function(){ 
	var args = location.search;
	var reg = new RegExp('[\?&]?' + this.argName + '=([^&]*)[&$]?', 'gi');
	var chk = args.match(reg);
	this.page = RegExp.$1;
}
showPages.prototype.checkPages = function(){ 
	if (isNaN(parseInt(this.page))) this.page = 1;
	if (isNaN(parseInt(this.pageCount))) this.pageCount = 1;
	if (this.page < 1) this.page = 1;
	if (this.pageCount < 1) this.pageCount = 1;
	if (this.page > this.pageCount) this.page = this.pageCount;
	this.page = parseInt(this.page);
	this.pageCount = parseInt(this.pageCount);
}
showPages.prototype.createHtml = function(mode){ 
	var strHtml = '', prevPage = this.page - 1, nextPage = this.page + 1; 
	if(this.recordCount>0)
	{
	    strHtml += '<div style="float: left; line-height: 20px; padding: 1px 20px 1px 10px;">Total: '+this.recordCount+'&nbsp; &nbsp;Page: '+ this.page +' / '+ this.pageCount +'</div>';
	    strHtml += '<div style="float: right;"><ul>';
	    if(this.pageCount>1)
	    {
	        if (prevPage >= 1) {
		        strHtml += '<li title="index"><a href="javascript:' + this.name + '.toPage(1);"><b>&#171;</b></a></li>';
		        strHtml += '<li title="previous"><a href="javascript:' + this.name + '.toPage(' + prevPage + ');"><b>&#139;</b></a></li>';
	        }
	        if (this.page % this.number ==0) {
		        var startPage = this.page - this.number + 1;
	        } else {
		        var startPage = this.page - this.page % this.number + 1;
	        }
	        if (startPage > this.number) {
	            strHtml += '<li style="border:0px" title="first 10 pages"><a href="javascript:' + this.name + '.toPage(' + (startPage - 1) + ');">...</a></li>';
	        }
	        for (var i = startPage; i < startPage + this.number; i++) {
		        if (i > this.pageCount) break;
		        if (i == this.page) {
			        strHtml += '<li title="page ' + i + '"><span>' + i + '</span></li>';
		        } else {
			        strHtml += '<li title="page ' + i + '"><a href="javascript:' + this.name + '.toPage(' + i + ');">' + i + '</a></li>';
		        }
	        }
	        if (this.pageCount >= startPage + this.number) {
	            strHtml += '<li style="border:0px" title="last 10 pages"><a href="javascript:' + this.name + '.toPage(' + (startPage + 10) + ');">...</a></li>';
	        }
	        if (nextPage <= this.pageCount) {
		        strHtml += '<li title="next"><a href="javascript:' + this.name + '.toPage(' + nextPage + ');"><b>&#155;</b></a></li>';
		        strHtml += '<li title="last"><a href="javascript:' + this.name + '.toPage(' + this.pageCount + ');"><b>&#187;</b></a></li>';
	        }
	    }
	    strHtml += '</ul></div>';
    }
	return strHtml;
}
showPages.prototype.createUrl = function (page) { 
	if (isNaN(parseInt(page))) page = 1;
	if (page < 1) page = 1;
	if (page > this.pageCount) page = this.pageCount;
	var url = location.protocol + '//' + location.host + location.pathname;
	var args = location.search;
	var reg = new RegExp('([\?&]?)' + this.argName + '=[^&]*[&$]?', 'gi');
	args = args.replace(reg,'$1');
	if (args == '' || args == null) {
		args += '?' + this.argName + '=' + page;
	} else if (args.substr(args.length - 1,1) == '?' || args.substr(args.length - 1,1) == '&') {
			args += this.argName + '=' + page;
	} else {
			args += '&' + this.argName + '=' + page;
	}
	return url + args;
}
showPages.prototype.toPage = function(page){ 
	var turnTo = 1;
	if (typeof(page) == 'object') {
		turnTo = page.options[page.selectedIndex].value;
	} else {
		turnTo = page;
	}
	self.location.href = this.createUrl(turnTo);
	//location.href = this.createUrl(turnTo);
}
showPages.prototype.printHtml = function(mode){ 
	this.getPage();
	this.checkPages();
	document.write('<div id="pages_' + this.name + '" class="pages"></div>');
	document.getElementById('pages_' + this.name ).innerHTML = this.createHtml(mode);
	
}
showPages.prototype.formatInputPage = function(e){ 
	var ie = navigator.appName=="Microsoft Internet Explorer"?true:false;
	if(!ie) var key = e.which;
	else var key = event.keyCode;
	if (key == 8 || key == 46 || (key >= 48 && key <= 57)) return true;
	return false;
}
