// ***** Items-per-page  for doc-lists *****
//
// Needs to re-load page when different items-per-page selected

jQuery(document).ready(function() {
	jQuery('#itemsperpage').change(newItemsPerPage);
	jQuery('.JS').css('visibility', 'visible');
	});

function stripGetParameter(url, param) // Strip GET parameter from url; for newItemsPerPage()
	{
	var locn = url.indexOf(param);
	var locnEnd;

	if (locn != -1)
		{
		locnEnd = url.indexOf('&', locn);
		if (locnEnd == -1) 
			return url.substr(0, locn - 1) // no parameters after this one
		else
			return url.substr(0, locn).concat(url.substr(locnEnd+1));
		}
	else
		return url;
	}
function newItemsPerPage()
	{
	var url = window.location.href;
	var itemsperpage = this.value;
	var startparam = 'doclist_start', startitem;

	if (url.indexOf(startparam) != -1)
		{
		startitem = parseInt(url.substr(url.indexOf(startparam) + startparam.length + 1));
		startitem = Math.floor(startitem/itemsperpage) * itemsperpage; // startitem must be set to a multiple of the new 'itemsperpage'
		}
	else startitem = 0;

	url = stripGetParameter(url, 'itemsperpage'); // Strip old parameters
	url = stripGetParameter(url, startparam);

	if (url.indexOf('?') != -1)
		{
		// GET parameters already in URL
		url = url.concat('&');
		}
	else
		url = url.concat('?');

	// append new parameters to URL
	window.location.href = url.concat('itemsperpage=').concat(itemsperpage).concat('&').concat(startparam).concat('=').concat(startitem);
	}

