sub parse_form
{
# Declare all our variables as local to this subroutine:
my ($param,$entrynum,@lines,@checkresult,$message,$cookie,$data,%formdata,$c,$filedata);

$cookie = retrieve_cookie("gwp");		# Do we have a cookie saying we've allready posted ?
$c = setcookie('gwp','xyzzy','+1h');	# Prepare a new cookie to say we've posted.


	# If there is an existing cookie, we must've allready posted, so do the following:

	# 1. Get the HTML for the appropriate HTML page as one long string.
	# 2. Feed the above HTML through expand_vars() to handle any special content tags.
	# 3. Print a content-type header to tell it we want to print HTML.
	# 4. Finally, we just print out the HTML for the page, then exit.

if ($cookie){
 $filedata = getfile($already_posted_error,1);
 $filedata = expand_vars($filedata,'');
 
 print "Content-type: text/html\n\n";
 print $filedata;
 exit;
}

	# If we've got this far, we will continue by checking for the following:
	
	# 1. Required fields left blank (must be filled in).
	# 2. Optional fields left blank (aren't required).
	# 3. Unwanted HTML.

	# We test for all of the above by doing the following:

	# 1. Loop over parameter name (form field) in @params and do as follows:
	
	# 	a. Copy the data associated with an individual field into $param.
	# 	b. If there is no value for $param, check if the field name is a required field (using grep).
	# 	c. If the field is required, add it to a list of empty fields (@checkresult) for later use.
	#	d. Any other blank fields will just be given the value "N/A".
	#	e. The data from each field is now filtered through the html filter.
	#	f. After all the above is completed, we copy the data to %formdata for processing.


foreach my $line (@params){
 $param = param("$line");
 if (!$param && grep(/^$line$/, @required_fields)){push (@checkresult,"$line");}
 $param ||= "N/A";
 $data = html_filter($param);
 $formdata{$line} = $data;
}
 
	# If there is at least one required field left blank (at least one line in @checkresult):
 
	# 1. Print an error message telling them to fill in all required fields.
	# 2. Print a list of the fields they didn't fill in.
	# 3. Tell it we're done (exit).
 
 if ($checkresult[0]){
 
	# Print the content header telling it we're sending HTML.
	# Next print the start of the error page:
 
 print "Content-type: text/html\n\n";
 
 print qq(
  <HTML>
  <head>
  <title>Guestwho: &nbsp;Error</title>
  </head>
  <body bgcolor="#FFFFFF">
  Please Fill in the following fields:<br>
  <ul>
 );
 
	# Print the list of fields they need to go back and fill in:
	# (This is in the form of an unordered list).
 
 foreach my $line (@checkresult){print "<li>".ucfirst($line)."</li>\n";}
 
 
	# Now print the end of the error page with all the closing HTML:
 
 print "</ul>\n";
 print "Click <a href=\"javascript:history.go(-1)\">here</a> to go back and re-submit your entry.";
 print "</body></HTML>";
 exit;
}

	# If everything is ok, we'll get the entry number for the new entry as follows:

	# 1. Get the contents of the entry log into an array (@lines).
	# 2. Using grep, find the number of entries we have (each entry ends with "<NEXT>").
	# 3. Now we have the number of entries, we just increment this number by 1 for this entry.

@lines = getfile($entrylog,0);
$entrynum = grep(/<NEXT>/, @lines);
$entrynum++;


# If we haven't come from the preview page, display a preview of the entry:
if (!param("previewed")){ preview_entry($entrynum); }


	# Now the last thing to do is write the new entry to the log file as follows:

	# 1. Open the file for writing, this will wipe any existing contents (which we want).
	# 2. Call the file locking function for an exclusive lock on the file.
	# 3. Print the entry number for our new entry.
	# 4. Print the date and time of our new entry.

	# 5. Next we print the data from the form fields by looping over %formdata as follows:
	# 	a. If the field is not called 'action' or 'submit' (special fields):
	# 	b. Print the field name and value in the form "name::value" to the file.
	# 	c. Print "<NEXT>" to tell it we've finished this entry.

	# 6. Finally we write the rest of the entries we saved earlier into @lines to the file.
	# 7. Now we just close the file.

open (FILE, ">$entrylog") || die ("Unable to open $entrylog: $!");
lockfile(2);
print FILE "entry::$entrynum\n";
print FILE "date::$datenow\n";
print FILE "time::$timenow\n";

foreach $line (keys %formdata){
 if ($line !~ /(action|submit|previewed)/i){
  print FILE $line."::".$formdata{$line}."\n";
 }
}

print FILE "<NEXT>\n";

foreach (@lines) {print FILE "$_";}
close (FILE);

	# Now we've added the entry, all we need to do is print a thankyou message:

	# 1. Get the HTML for the page as one long string.
	# 2. Feed it through expand_vars() to handle special content tags.
	# 3. Send the cookie we prepared earlier, and print a content-type header.
	# 4. Print the page out and then tell it we're done (exit).

$message = getfile($thankyou_confirm,1);
$message = expand_vars($message,'');

print "Set-Cookie: $c\n";
print "Content-type: text/html\n\n";
print $message;
exit;
}
1;