# This include file works with recipe.cgi and recipe_pend.cgi
# recipe.pl
# Jerome C. Parks
# June 26, 1996
#
# Version 1.0
# Version 1.1: Modified August 19, 1997 to support a comment field
#

sub fail_name {
  if (!$name) {
	&failed_html($html_file);
	print DEBUG "Failed due to name\n";
	exit;
  }
} # end subroutine fail_name

sub fail_v {
  if (!@v) {
	&failed_html($html_file);
	print DEBUG "Failed due to no V\n";
	exit;
  }
} # end subroutine fail_v

sub remove_blank_v {
  $i = 0;
  foreach $L (@v) { # Take out any blank lines in the arrays
	if ($L) {
	  push(@vnew,$v[$i]);
	  push(@qnew,$q[$i]);
	  push(@snew,$s[$i]);
	  push(@oqnew,$oq[$i]);
	  push(@osnew,$os[$i]);
	}
	$i++;
  }
  @v=@vnew;
  @q=@qnew;
  @s=@snew;
  @os=@osnew;
  @oq=@oqnew;

  print DEBUG join(',',@v) . ".\n";
  print DEBUG <<EOM;
Array V (after splicing): @v
Array Q (after splicing): @q
Array S (after splicing): @s
EOM
} # end subroutine remove_blank_v

sub directions_all {
  # Directions
  $count = 0;
  while ($count < $length_directions) {
        $num = ($count+1);
	@d[$count] = $rqpairs{"D$num"};
	$count++;
  }
  if (!@d) {
	&failed_html($html_file);
	print DEBUG "Failed due to no D!\n";
	exit;
  }
  $i=0;
  print DEBUG "Array D: @d\n";
  foreach $L (@d) { # Take out any blank lines in the arrays
	if ($L) {
	  push(@dnew,$d[$i]);
	}
	$i++;
  }
  @d = @dnew;
  print DEBUG "Array D (after splicing): @d\n";
} # end subroutine directions_all

sub combine_others {
  $count=0;
  $length=@v;
  while ($count < $length) { # Assigns user input to one array
	if (@q[$count] eq "Other") {
	  @q[$count] = @oq[$count]
	}
	if (@s[$count] eq "Other") {
	  @s[$count] = @os[$count];
	}
	$count++;
  }
} # end subroutine combine_others

# This subroutine outputs a temporary file that they can either submit or
# update.
sub new_html {
  open(OUT,">$html_file") || warn "Can't open $html_file for write";
  print OUT "<html>\n<head><title>$recipe<\/title><\/head>\n";
  print OUT <<EOM;
<body BACKGROUND="http://www.polylith.com/~jerparks/images/Backgrounds/leaves.gif" TEXT="#ffffff" LINK="ffff00" VLINK="#dfdfdf">
EOM
  print OUT "<FORM METHOD=\"POST\" ACTION=\"$recipe_script\">\n<center>";
  $size = length($recipe);
  print OUT "<font size=+1><B>Recipe Name:<\/B><\/font><br>\n";
  print OUT "<INPUT NAME=\"Recipe\" VALUE=\"$recipe\" SIZE=\"$size\">";
  print OUT "<p><I>Please hit the <\/I><B>Reload<\/B><I> button to be sure and get the more recent version submitted!</I><\/center>\n<hr size=4>\n<p>\n";
  print OUT "<center>\n<font color=\"#000000\">\n<SELECT NAME=\"Type\">\n";
  if ($type eq "Appetizer") 
  {
      print OUT "<OPTION SELECTED>Appetizer\n";
  } else {
      print OUT "<OPTION>Appetizer\n";
  }
  if ($type eq "Salad") 
  {
      print OUT "<OPTION SELECTED>Salad\n";
  } else {
      print OUT "<OPTION>Salad\n";
  }
  if ($type eq "Bread") 
  {
      print OUT "<OPTION SELECTED>Bread\n";
  } else {
      print OUT "<OPTION>Bread\n";
  }
  if ($type eq "Soup") 
  {
      print OUT "<OPTION SELECTED>Soup\n";
  } else {
      print OUT "<OPTION>Soup\n";
  }
  if ($type eq "Main Dish") 
  {
      print OUT "<OPTION SELECTED>Main Dish\n";
  } else {
      print OUT "<OPTION>Main Dish\n";
  }
  if ($type eq "Vegetarian") 
  {
      print OUT "<OPTION SELECTED>Vegetarian\n";
  } else {
      print OUT "<OPTION>Vegetarian\n";
  }
  if ($type eq "Dessert") 
  {
      print OUT "<OPTION SELECTED>Dessert\n";
  } else {
      print OUT "<OPTION>Dessert\n";
  }
  if ($type eq "Other") 
  {
      print OUT "<OPTION SELECTED>Other\n";
  } else {
      print OUT "<OPTION>Other\n";
  }

  print OUT "<\/font>\n<\/SELECT>\n<p>\n";
  print OUT "<table>\n";
  print OUT "<tr>\n<td align=right>Oven Temperature: <\/td>\n";
  if ($temp)
  {	
	$size = length($temp);
  } else {
	$size = 4;
  }
  print OUT "<td align=left><INPUT NAME=\"Temp\" SIZE=$size MAXSIZE=4 ";
  print OUT "VALUE=\"$temp\">&#176;F<\/td>\n<\/tr>\n";
  if ($preptime)
  {
	$size = length($preptime);
  } else {
	$size = 20;
  }
  print OUT "<tr><td align=right>Preparation Time: </td>\n";
  print OUT "<td align=left><INPUT NAME=\"PrepTime\" SIZE=$size MAXSIZE=60 ";
  print OUT "VALUE=\"$preptime\"><\/td>\n<\/tr>\n";
  if ($cooktime)
  {
	$size = length($cooktime);
  } else {
	$size = 20;
  }
  print OUT "<td align=right>Actual Cooking Time: <\/td>\n";
  print OUT "<td align=left><INPUT NAME=\"CookTime\" SIZE=$size MAXSIZE=60 ";
  print OUT "VALUE=\"$cooktime\"><\/td>\n<\/tr>\n";
  print OUT "<\/table>\n";

  print OUT "<\/center>\n<p>\n<h3><I>Ingredients:<\/I><\/h3>\n<table>\n";
  $ing_len = @v;
  $dir_len = @d;
  print OUT "<INPUT TYPE=\"Hidden\" VALUE=\"$ing_len\" NAME=\"Ing_Len\">\n";
  print OUT "<INPUT TYPE=\"Hidden\" VALUE=\"$dir_len\" NAME=\"Dir_Len\">\n";
  print OUT "<INPUT TYPE=\"Hidden\" VALUE=\"$extras\" NAME=\"Extras\">\n";

  foreach $L (@q) { # Find the max length of q
	$size = length($L);
	if ($size > $max_q) {
	  $max_q = $size;
	}
  }
  foreach $L (@s) { # Find the max length of s
        $size = length($L);
        if ($size > $max_s) {
          $max_s = $size;
        }
  }

  $length=@v;
  $num=0;
  while ($num < $length) {
	$count = ($num+1);
	print OUT "<tr>\n<td><INPUT VALUE=\"@q[$num]\" NAME=\"Q$count\" SIZE=\"$max_q\">";
	print OUT "<\/td>\n<td><INPUT VALUE=\"@s[$num]\" NAME=\"S$count\" SIZE=\"$max_s\">";
	print OUT "<\/td>\n<td><INPUT VALUE=\"@v[$num]\" NAME=\"V$count\" SIZE=\"30\">";
  	print OUT "<\/td>\n<\/tr>";
	$num++;
  }
  print OUT "\n<\/table>\n";
  &ingredients;

  print OUT "<p>\n<hr size=4>\n<p>\n<h3><I>Directions:<\/I><\/h3>\n<table>\n";
  $length=@d;
  $num=0;
  while ($num < $length) {
	$count = ($num+1);
	print OUT "<tr>\n<td align=right>Step $count:<\/td>\n";
	print OUT "<td align=left><INPUT VALUE=\"@d[$num]\" NAME=\"D$count\" SIZE=\"50\"><\/td>\n<\/tr>";
	$num++;
  }
  &directions;
  print OUT "\n<\/table>\n<p>\n";

  # Added section for comments
  print OUT "<hr size=4>\n<p>\n<h3><I>Comments:</I></h3>\n<p>\n<ul>\n";
  print OUT "<TEXTAREA WRAP Name=\"Comments\" ROWS=5 ";
  print OUT "COLS=50>$comments</TEXTAREA>\n</ul>\n<p>\n";
  # END comments section

  print OUT "<hr size=4>\n<center>\n<p>\n<I>Submitted by:<\/I><br>\n";
  $name_length = length($name);
  print OUT "<INPUT VALUE=\"$name\" NAME=\"Name\" SIZE=\"$name_length\"><br>\n";
  if ($email) {
	#$email=~s/\@/\\\@/;
        $email_length = length($email);
	print OUT "<INPUT VALUE=\"$email\" NAME=\"Email\" SIZE=\"$email_length\"><br>\n";
  }
  if ($url) {
	$url_length = length($url);
	print OUT "<INPUT VALUE=\"$url\" NAME=\"URL\" SIZE=\"$url_length\"><br>\n";
  }
  
  print OUT "<p>\n<font color=\"#000000\">\n";
  print OUT "<INPUT TYPE=\"submit\" NAME=\"Submit\" VALUE=\"Preview\">";
  print OUT "<INPUT TYPE=\"submit\" NAME=\"Submit\" VALUE=\"FINSHED!\">";
  print OUT "<INPUT TYPE=\"reset\" VALUE=\"Clear\">\n<\/font>\n";
  print OUT "<\/center>\n<p>\n<hr size=4>\n\n";
  print OUT "<!--#exec cgi=\"..\/footer.cgi\"-->\n\n<\/body>\n<\/html>\n";
  close OUT;

} # end sub new_html

# This subroutine takes care of cases when they failed to correctly fill out
# the form.
sub failed_html {
  local($file) = @_;
  open(OUT,">$file") || warn "Can't open $file for write";
  print OUT <<EOM;
<html>
<head><title>Recipe Incomplete</title></head>
<body BGCOLOR="#000000" TEXT="#ffffff" LINK="#ff00ff" VLINK="#00ffff" ALINK="#00ff00">
<center><h1>Incomplete Recipe Submission</h1></center>
<hr size=4>
<ul>
<center><h2><I>Sorry, but you failed to correctly fill out the recipe form.  Use the back arrow on your browser to try filling it out again.</I><br>
Also, you may just need to reload this page!</h2></center>
</ul>
<hr size=4>

EOM
  print OUT "<!--#exec cgi=\"..\/footer.cgi\"-->\n\n<\/body>\n<\/html>\n";
  close OUT;

} # end subroutine failed_html

# This subroutine adds more ingredients if they ran out
sub ingredients {
  $count=@v;
  $maxlength = ($count+$extras);
  while ($count < $maxlength) {
        $count++;
        print OUT "<p>\n<table>\n<tr>\n<td rowspan=2 align=right><img src=\"http://www.polylith.com/~jerparks/images/Arrows/arrow_dotted.gif\"><\/td>\n<td><font color=\"#000000\">\n<SELECT NAME=\"Q$count\">\n";
        print OUT <<EOM;
<OPTION>1/4
<OPTION>1/3
<OPTION>1/2
<OPTION>2/3
<OPTION>3/4
<OPTION>1
<OPTION>1 1/4
<OPTION>1 1/3
<OPTION>1 1/2
<OPTION>1 2/3
<OPTION>1 3/4
<OPTION>2
<OPTION>2 1/4
<OPTION>2 1/3
<OPTION>2 1/2
<OPTION>2 2/3
<OPTION>2 3/4
<OPTION>3
<OPTION>3 1/2
<OPTION>4
<OPTION>4 1/2
<OPTION>5
<OPTION SELECTED>Other
</SELECT>
</font></td>
<td align=right rowspan=2><img src="http://www.polylith.com/~jerparks/images/Arrows/arrow_dotted.gif"></td>
<td><font color="#000000">
EOM
        print OUT "<SELECT NAME=\"S$count\">\n";
        print OUT <<EOM;
<OPTION>teaspoon(s)
<OPTION>tablespoon(s)
<OPTION>Cup(s)
<OPTION>ounce(s)
<OPTION>pound(s)
<OPTION>pint(s)
<OPTION>quart(s)
<OPTION>bushel(s)
<OPTION>stick(s)
<OPTION SELECTED>Other
</SELECT>
</font></td>
EOM
        print OUT "<td rowspan=2><INPUT NAME=\"V$count\" SIZE=30 MAXSIZE=80><\/td>\n<\/tr>";
        print OUT "<td><INPUT NAME=\"OQ$count\" SIZE=6 MAXSIZE=20></td>\n";
	print OUT "<td><INPUT NAME=\"OS$count\" SIZE=10 MAXSIZE=30></td>\n";
	print OUT <<EOM;
</tr>
</table>
EOM
        print OUT "<!------------------------- End Ingredients $count ------------------------->\n";
  }

} # end subroutine ingredients

# This subroutine adds more lines for additional directions
sub directions {
  $count = @d;
  $maxlength = ($count+$extras);
  while ($count < $maxlength) {
	$count++;
	print OUT "<tr>\n<td align=right>Step $count: </td>\n";
	print OUT "<td align=left><INPUT NAME=\"D$count\" SIZE=\"50\"><\/td>\n<\/tr>";
  }

} # end subroutine directions

1;
