sub preview_entry
{
 # Declare our variables as local to this subroutine:
 my (%entry,@data,@preview);  

 # Get the HTML for the "Preview Entry" template:
 @preview = getfile($previewfile,0);
 
 # Create a hash of entry data (%entry) from the form data:
 foreach (@params){ $entry{$_} = param($_); }
 
 # Add the date, time, and entry number to the above hash:
 $entry{'time'} = $timenow;
 $entry{'date'} = $datenow;
 $entry{'entry'} = shift;
 
 
 # Feed the entry hash through search_replace() to handle entry content tags.
 # This will return a reference to an array of the entry preview:
 
 $data = search_replace(\%entry,$entrytemplate,0,1);
 
 
 # Print a content header to tell it we're sending HTML:
 print "Content-type: text/html\n\n";


	# Loop over each line line of HTML from @preview as follows:
 
	# 1. If the line contains the content tag to start the preview (see manual):
	#	a. Print the hidden form inputs containing the submitted entry data.
	#	b. Print a couple of hidden form inputs to tell it we want to add an entry.
	#	c. Print the line itself incase it contains other tags as well.
	#	d. Print each line of HTML for the entry itself that we are previewing.
 
	# 2. If the line doesn't contain the above content tag:
	#	a. Feed the line through expand_vars() to handle other special content tags.
	#	b. Print the line as normal.
 
	# 3. We're done with everything now, so we can exit.
 
 foreach my $line (@preview){

  if ($line =~ /<!--BEGIN_PREVIEW-->/){
 
   foreach my $p (@params){
    print qq(<input type="hidden" name="$p" value="@{[param($p)]}">\n);
   }
   
   print qq(
    <input type="hidden" name="action" value="submit">
    <input type="hidden" name="previewed" value="1">
    $line
   );
  
   foreach (@$data){print $_;}
  }else{
   $line = expand_vars($line,'');
   print $line;
  }
 
 }

 exit;
}
1;