var buildPaging = function (ofLabel) {
	var tableArray = new Array();
	var pageCount = Math.ceil(totalSize / recordCount);

	if( pageCount == 1 ) return;

	tableArray.push( '<tr><td colspan="' );
	tableArray.push( invColspan );
	tableArray.push( '">' );
	tableArray.push( '<div class="pagingBar">' );

	var recordEnd = new Number((recordCount+recordStart-1));
	if( recordEnd > totalSize ) recordEnd = totalSize;

	tableArray.push( '<table width="100%" align="center" cellpadding=0 cellspacing=0 border=0>' );
	tableArray.push( '<tr><td width="30%" nowrap class="pageStatus" style="padding-left: 5px;">' );
	tableArray.push( recordStart );
	tableArray.push( ' - ' );
	tableArray.push( recordEnd );
	var ofInfo = ' '+((ofLabel)?ofLabel:'of')+' ';
	tableArray.push( ofInfo );
	tableArray.push( totalSize );
	tableArray.push( '</td>' );
	tableArray.push( '<td width="40%" style="padding-left: 5px; padding-right: 5px;">' );

	tableArray.push( '<table align="center" cellpadding=1 cellspacing=0 border=0><tr>' );

//	tableArray.push( '<td>'+pagingNote+'</td>' )
	if( hasPreviousPage ) {
		tableArray.push( '<td onClick="gotoPage(1);"><img src="' );
		tableArray.push( firstPageImage );
		tableArray.push( '" alt="first page" style="cursor: pointer; cursor: hand;"></td>' );

		var prev = new Number((currentPage-1));
		tableArray.push( '<td onClick="gotoPage('+prev+');"><img src="' );
		tableArray.push( previousPageImage );
		tableArray.push( '" alt="previous page" style="cursor: pointer; cursor: hand;"></td>' );
	}

	var start = 0;
	var mid = parseInt((visiblePageCount > pageCount)?visiblePageCount:(visiblePageCount/2));

	if( currentPage > mid )
		start = new Number((currentPage - mid));

// FLAWED LOGIC IF > 10 PAGES BECAUSE $paging.getPageCount() (old pageCount) returns visiblePageCount (10)
/*
	if( (new Number((pageCount - start))) < visiblePageCount ){
		ln = pageCount;
		if( pageCount > visiblePageCount )
			start = new Number((pageCount - visiblePageCount));
	}else ln = new Number((start + visiblePageCount));
*/
/* rewrote, 3/30/04 - wick */
	ln = ((start + visiblePageCount) > pageCount)?pageCount:(start + visiblePageCount);
	if (pageCount >= visiblePageCount) {
		start = (ln - visiblePageCount);
	}

	for( var i=ln; i>start; i-- ){
		var index = new Number((ln-i)+1+start);
		//var className = ((current==parseInt(index))?'currentPage':'page');
		var className = ((currentPage==parseInt(index))?"currentPageStyle":"pageStyle");
		tableArray.push( '<td class="'+className+'" onClick="gotoPage('+index+')" style="cursor: pointer; cursor: hand;"' );
		tableArray.push( ' onMouseOver="if(className != \'currentPageStyle\') { className = \'pageOver\'; }"' );
		tableArray.push( ' onMouseOut="if(className != \'currentPageStyle\') { className=\'pageStyle\'; } ">' );
		tableArray.push( index );
		tableArray.push( '</td>' );
	}
	//alert(className+", "+recordEnd);

	if( hasNextPage ){
		var next = new Number((currentPage+1));
		tableArray.push( '<td onClick="gotoPage('+next+')"><img src="' );
		tableArray.push( nextPageImage );
		tableArray.push( '" alt="next page" style="cursor: pointer; cursor: hand;"></td>' );
		tableArray.push( '<td onClick="gotoPage('+pageCount+');"><img src="' );
		tableArray.push( lastPageImage );
		tableArray.push( '" alt="last page" style="cursor: pointer; cursor: hand;"></td>' );
	}

	tableArray.push( '</tr></table>' );

	tableArray.push( '</td><td width="30%">&nbsp;</td></tr></table>' );

	tableArray.push( '</div>' );
	tableArray.push( '</td></tr>' );

	document.write(tableArray.join(''));
	delete tableArray;
	tableArray = new Array();
};

/* Copyright (c) 2007 Marlin Forbes (http://www.datashaman.com)
 * Dual licensed under the MIT
 * (http://www.opensource.org/licenses/mit-license.php)
 * and GPL
 * (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * function setupParameters
 * Creates an object property window.location.parameters which
 * is an associative array of the URL querystring parameters used
 * when requesting the current document.
 * If the parameter is present but has no value, such as the parameter
 * flag in http://example.com/index.php?flag&id=blah, null is stored.
 */
var setupParameters = function () {
	var parameters = new Object();
	if (window.location.search) {
		var paramArray = window.location.search.substr(1).split('&');
		var length = paramArray.length;
		for (var index = 0;index <length; index++ ) {
			var param = paramArray[index].split('=');
			var name = param[0];
			var value =
				typeof param[1] == "string"
				? decodeURIComponent(param[1].replace(/\+/g, ' '))
				: null;
			parameters[name] = value;
		}
	}
	window.location.parameters = parameters;
};

var toParamString = function (arg, x, y, z) {
	x = x || '?';	// parameter string prefix
	y = y || '&';	// parameter pair delimiter
	z = z || '=';	// key / value pair delimiter
	
	var c,
		i,
		l,
		s = '',
		v;
	var urlencode = function (str) {
		return escape(str).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
	};
	
	switch (typeof arg) {
		case 'object':
			if (arg) {
				for (i in arg) {
					if ((typeof (v = toParamString(arg[i])) !== 'function')) {
						if (s) { s += y; }
						s += urlencode(toParamString(i)) + z + urlencode(v);
					}
				}
				return (s.length >= 1 ? x : '') + s;
			} else {
				return 'null';
			}
			break;
		case 'string': return arg;
		case 'number': return String(arg);
		default: return 'null';
	}
}

var gotoPage = function (page) {
	// parse and store all the existing URL parameters
	setupParameters();
	// change value of start param, or add it if it doesn't exist
	window.location.parameters.start = recordCount*((new Number(page-1)));
	// goto inventory page using existing parameters (except for our new start value)
	document.location.href = link_inventory + toParamString(window.location.parameters);
	// notify user of load action
	this.updateStatusBar('Loading inventory, one moment please...', true);
};