sub prepare_entries
{
 # Declare all our variables as local to this subroutine:
 
 my ($page,$start,$stop,$next,$entry,$maxentries,$formcode,$sort,$sortby,
 	 $order,$maxpages,$nextpage,$prevpage,$pageinput,$javascript,@tmp,%data);
 
 
 %data = %{get_data()};		# Get a 2D hash of all the entries.
 $page = param("page");		# Get the requested page number.
 $sort = shift;				# How should we sort the entries ?
 $page ||= 1;				# If no page was specified, default to the first page.
 $page =~ s!\+!!;			# Remove any plus sign "+" from the page number.
 $page =~ s!\s!!g;			# Remove any spaces in the page number.
 
 
 # Display an error page if a negative page number was specified:
 
 if ($page =~ /^-(\d+)$/){
  print "<center><font face=\"arial\" size=\"2\">";
  print "Minus entries ? Now how could I have $page entries ?? Think about it a while..";
  print "</font><hr><p></center>";
  return;
 }
 
 # Display an error page if the requested page is not a number:
 
 if ($page !~ /^(\d+)$/){
  print "<center><font face=\"arial\" size=\"2\">";
  print "Unless you are in kindergarten, I would assume you know that \"$page\" is not a number!";
  print "</font><hr><p></center>";
  return;
 }
 
	# Now we've got to work out the index number to start at for the requested page:
 
	# 1. If the first page is requested, the index to start at must be 0.
	# 2. If any other page is requested we use the following formula: 
	
	#    (entries_per_page * page_number) - entries_per_page = index_number
	#	  Example: (3 * 2) - 2 = 4
 
 
 if ($page == 1){
  $start = 0;
 }else{
  $start = ($perpage*$page)-($perpage);
 }
 
 
	# Calculate the maximum number of pages as follows:
 
	# 1. Get the maximum number of entries.
	# 2. Divide the above number by the number of entries per page.
	# 3. If the above division has any remainder, increment the maximum pages by 1.
	# 4. The final maximum number of pages is now stored in $maxpages.
 
 $maxentries = keys(%data); 						
 $maxpages = int($maxentries/$perpage);
 if (($maxentries % $perpage) > 0){$maxpages++;}
                
                
	# Calculate the index number to stop at using the formula:
	# (start_index + entries_per_page) - 1 = stop_index
	# Example: (4 + 3) - 1 = 6
 
 $stop = ($start+$perpage)-1;
 
 
 # If the requested page doesn't exist, print an appropriate error message:
 
 if (!$data{$start}){                      			
  print "<center><font face=\"arial\" size=\"2\">
  Sorry, no more entries!</font><hr><p></center>";
 }


	# Before printing anything, we check which order entries should be displayed in:
	
	# 1. Get the fieldname and order to sort by (descending or ascending).
	# 2. Sort all the entries as determined above ready to be displayed.
	# 3. Put only the entries we need for this page, into a new array (@keys).
	
 ($sortby,$order) = split(',',$sort);
 @tmp = return_sorted(\%data,$sortby,$order);
 if ($#tmp < $stop){ $stop = $#tmp; }
 for ($i=$start;$i<=$stop;$i++){ $keys[$i] = $tmp[$i]; }
 

	# Now we print the entries as follows:
	# Loop over each key name from the sorted array @keys and do as follows:

	# 	1. If an index (value) exists for the current keyname, continue as follows:
	# 	2. Feed the data though search_replace() to replace entry content tags with the entry data.
	# 	3. A reference will be returned from above, so use it as a real array and print the contents.
	# 	4. Start a new HTML paragraph (<P>), which will just put in a few blank lines for us.
 
  
 foreach my $key (@keys){
  if ($data{$key}){
   $entry = search_replace(\%{$data{$key}},$entrytemplate,'','');
   foreach my $line (@$entry){print $line;}
   print "<P>\n";
  }
 }
 
 
	# Work out what the next page and next index are, by doing the following:
 
	# 1. The next index is 1 after the stop index (ie. stop_index + 1).
	# 2. The next page is 1 after the current page (ie. page + 1).
 
 $next = $stop+1;
 $nextpage = $page+1;
 
 
	# Now all we need to work out is the dynamic previous, next, first and last buttons.
	# First though, we need to create a Javascript function for page navigation:

 $javascript = qq(
  <SCRIPT LANGUAGE="Javascript">
  function setPage (pagenum){
   document.forms.p.page.value = pagenum; 	// Change the value of the hidden 'page' field.
   document.forms.p.submit();				// Submit the form.
  }
  </SCRIPT>
 );
 
  
 # Prepare the hidden page input for use with the above Javascript:
 $pageinput = qq(<INPUT TYPE="HIDDEN" NAME="page" value="$page">);
 
 
 # Prepare the form tag and hidden input ready for use:
 $formcode = qq(
  <center>
  <FORM NAME="p" ACTION="http://$host$scripturl" METHOD="GET">
  <INPUT TYPE="HIDDEN" NAME="action" value="view">
 );
 
 my @foo = keys(%data);
 my $foo = join('-',@foo);
 
 my @foo2 = @keys;
 my $foo2 = join('-',@keys);
 
 print qq(<INPUT TYPE="HIDDEN" NAME="max" value='$foo'>);
 print qq(<INPUT TYPE="HIDDEN" NAME="max" value='$foo2'>);
 
	# If there are no previous entries, but there is at least one more entry:
 
	# 1. Print the Javascript navigation function we prepared earlier.
	# 2. Print the HTML for the form an the hidden input 'action'.
 
 if ($start-1 < 0 && $data{$next}){
  print $javascript;
  print $formcode;
 }
 
 
	# If there is at least one previous entry:
 
	# 1. Print the Javascript navigation function.
	# 2. Print the HTML for the form and hidden 'action' input.
	# 3. Print the hidden input for the page number.
	# 4. Print the buttons for the first page and the next page.
 
 if ($start-1 >= 0){
  $prevpage = $page-1;
  print $javascript;
  print $formcode;
  print $pageinput;
  print qq(<INPUT TYPE="BUTTON" VALUE="|<<" onClick="setPage(1)">&nbsp;);
  print qq(<INPUT TYPE="BUTTON" VALUE="<< Page $prevpage" onClick="setPage($prevpage)">);
 }


	# If there is at least one more entry:

	# 1. Print the buttons for the next page and the last page.
	# 2. If there are no previous entries print the hidden 'page' input.
	# 3. Print the closing tags for the form and the centering.

 if ($data{$next}){
  print qq(<INPUT TYPE="BUTTON" VALUE="Page $nextpage >>" onClick="setPage($nextpage)">);
  print qq(&nbsp;<INPUT TYPE="BUTTON" VALUE=">>|" onClick="setPage($maxpages)">);
  if ($start-1 < 0){print $pageinput;}
  print qq(</FORM></center>);
 }
 
 # If there is at least one previous entry but no more entries, we just close the form:
 if ($start-1 >= 0 && !$data{$next}){print "</FORM></center>";}
}
1;