<form style="display: inline" action="<% $rooturi %>" method="POST">
<b>Showing <% ($offset + 1) %> through <% ($offset + $limit) %> of <% $found %>.<br>
<a href="<% $uri %>offset=0&limit=<% $limit %>">First</a> |
% if ($offset > 0) {
<a href="<% $uri %>offset=<% $offset - $limit > 0 ? $offset - $limit : 0 %>&limit=<% $limit %>">Previous <% $limit %></a> |
% }
% if ($offset + $limit < $found) {
<a href="<% $uri %>offset=<% $offset + $limit %>&limit=<% $limit %>">Next <% $limit %></a> |
% }
Next <input type="text" name="limit" value="<% $limit %>" size="4">
<input type="hidden" name="offset" value="<% $offset + $limit %>">
<input type="hidden" name="rooturi" value="<% $rooturi |h %>">
<input type="hidden" name="found" value="<% $found %>">
<input type="checkbox" id="nostamp" name="nostamp" <% $nostamp ? 'checked' : '' %>> <label for="nostamp">No stamps</label> 
<input type="submit" value="Go">
</form> | <a href="<% $uri %>offset=<% $lastoff %>&limit=<% $limit %>">Last</a> <p>

<%args>
$rooturi
$limit
$offset
$found
$nostamp => undef
</%args>

<%init>
# Constructs an HTML snippet that can be used for paging through large result
# lists.  It's up to the calling code to ensure that $limit, $offset and $found
# are known and compatible.

if (ref($limit)) {
    $limit = $limit->[-1];      # got two limits (from user-entered), pick last one
}

my $uri;
my $formuri;
my $nostamp_str = $nostamp ? 'nostamp=1&' : '';
if (!$rooturi) {
    # If empty, start with '?', otherwise concat with '&'.
    $uri = "?found=$found&$nostamp_str";
}
else {
    $uri = "$rooturi&found=$found&$nostamp_str";
}

my $lastoff = $found - $limit;
if ($lastoff < 0) {
    $lastoff = 0;
}

if ($offset + $limit > $found) {
    $limit = $found - $offset;
}

</%init>
