function Pager(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.skipRows = 0;
    this.endSkipRows = 0;
    this.pages = 0;
    this.inited = false;
    this.positions = new Array(10);
    this.positionCount = 0;
    this.records = 0;
    this.checkAll = null;
    this.onPageChange = null;
    
    this.showRecords = function(from, to) {        
        var rows = document.getElementById(tableName).rows;
        // i starts from 1 to skip table header row
        for (var i = this.skipRows + 1; i < rows.length; i++) {
            if (i < from + this.skipRows || i > to + this.skipRows)  
                rows[i].style.display = 'none';
            else
                rows[i].style.display = '';
        }
    }
    
    this.setCheckall = function(challId)
    {
        this.checkAll = document.getElementById(challId);
    }
    
    this.showPage = function(pageNumber) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
    	
    	var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        if (to > this.records)
            to = this.records;
            
        for (var i = 0; i < this.positionCount; i++)
        {
            var positionId = this.positions[i];
            var oldPageAnchor = document.getElementById(positionId + 'pg'+this.currentPage);
            oldPageAnchor.className = 'pg-normal';
        }
        this.currentPage = pageNumber;
        
        for (var i = 0; i < this.positionCount; i++)
        {   
            var positionId = this.positions[i];         
            
            var newPageAnchor = document.getElementById(positionId + 'pg'+this.currentPage);
            newPageAnchor.className = 'pg-selected';
            var results = document.getElementById(positionId + 'results');
            results.innerHTML = 'Results ' + from + ' to ' + to + ' of ' + this.records;
        }
                
        this.showRecords(from, to);
        if (this.onPageChange)
            this.onPageChange(pageNumber);
    }   
    
    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }
    
    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage - (-1));
        }
    }

    this.init = function(skipRows, endSkipRows) {
        this.skipRows = skipRows;
        this.endSkipRows = endSkipRows;
        var table = document.getElementById(tableName);
        if (table) {
            var rows = table.rows;
            this.records = (rows.length - 1 - this.skipRows - this.endSkipRows);
            this.pages = Math.ceil(this.records / itemsPerPage);
            this.inited = true;
        }
    }

    this.showPageNav = function(pagerName, positionId) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
    	if (this.pages <= 1) return;
    	this.positions[this.positionCount] = positionId;
    	this.positionCount++;
    	
    	var element = document.getElementById(positionId);
    	
    	var pagerHtml = '<span id="' + positionId + 'results"></span> <br>';
    	pagerHtml += '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span> ';
        for (var page = 1; page <= this.pages; page++) 
            pagerHtml += '<span id="' + positionId + 'pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> ';
        pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>';            
        
        element.innerHTML = pagerHtml;
    }
    
    this.setPageChange = function(func)
    {
        this.onPageChange = func;
    }
}

var PageState = {
	epoch: new Date("Thu, 01-Jan-70 00:00:01 GMT"),
	getAll: function(){
		var values = new Object();
		if (document.cookie == "") return values;
		
		var cookies = document.cookie.split(";");
		for(var i=0;i<cookies.length;i++){
			var pair = cookies[i].split("=");
			values[pair[0]] = unescape(pair[1]);
		}
		return values;
	},
	
	get: function(name, default_){
		if (document.cookie == "") return default_;
		
		var cookies = document.cookie.split(";");
		name = escape(name);
		for(var i=0;i<cookies.length;i++){
			var pair = cookies[i].split("=");
			if (name==pair[0]) return unescape(pair[1]);
		}
		return default_;
	},
	
	set: function(name, value, options){
		var cookie_parts = new Array();
						
		if (value==null) value = "";
		cookie_parts.push(escape(name) + "=" + escape(value));
		
		if (options){
			var expires = options["expires"];
			if (expires != null) {
				if (typeof expires != "Date") expires = new Date(expires);
				cookie_parts.push("expires="+expires.toGMTString());
			}
			
			var path = options["path"];
			if (path != null)
				cookie_parts.push("path="+path);
	
			var domain = options["domain"];
			if (domain != null)
				cookie_parts.push("domain="+domain);
				
			if (options["secure"]) cookie_parts.push("secure");
		}
				

		document.cookie = cookie_parts.join("; ");
	},

	remove: function(name, options,secure){
		if (options == null) options = new Object();
		options["expires"] = PageState.epoch;
		PageState.set(name, null, options);
	}
};

