#!/usr/bin/perl -Tw

##
# Modified from
#   Figure 27.33 from _Internet & World Wide Web: How To Program_
#   by H.M. Deitel, P.J. Deitel, and T.R. Nieto
#   Published by Prentice Hall, (c) 2000 by Prentice-Hall, Inc.
##

use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';
use diagnostics;
use strict;
$| = 1;

# read values from the form
my $name = param('Name')     || "";
my $height = param('Height') || "";
my $color = param('Colour')  || "";


# parameters for the cookie
my $expires = "Fri, 31-Dec-2010 16:00:00 GMT";
my $path = "/"; 
my $server_domain = "cs.dal.ca"; # numeric address don't seem to work


# the cookie headers
#  these three methods produce the same output 
#  you should choose one method (that is clear to you) and use it

print "Set-Cookie: "           .
      "Name=$name; "           .
      "expires=$expires; "     .
      "path=$path; "           .
      "domain=$server_domain\n";

print "Set-Cookie: Height=$height; path=$path; expires=$expires; ";
print "domain=$server_domain\n"; # the line above does not end with \n

print "Set-Cookie: ";
print "Color=",$color, "; path=", $path, "; expires=",$expires,
      "; domain=", $server_domain, "\n";

print header(-expires=>"now"); # stop browsers from trying to cache output



print "<head>\n";
print "  <title>Dietel et al. Fig. 27.33</title>\n";
print "  <style type='text/css'>\n";
print "    span.red    {color: red}\n";
print "    span.orange {color: orange}\n";
print "    span.yellow {color: yellow}\n";
print "    span.green  {color: green}\n";
print "    span.blue   {color: blue}\n";
print "    span.indigo {color: indigo}\n";
print "    span.violet {color: violet}\n";
print "    span.black  {color: black}\n";
print "    span.white  {color: white}\n";
print " </style>\n";
print "</head>\n";
print "<body>\n";
print " <p>\n";
print "  The following data has been read from the form ";
print "   and sent the browser in three cookies:\n";
print "  <br />",                      "<br />\n";
print "  Name:               $name",   "<br />\n";
print "  Height:             $height", "<br />\n";
print "  Favourite Colour:";
print "  <span class='", lc($color), "'>$color</span>";
print " </p>";

print '<p>[<a href="'.
      'http://www.cs.dal.ca/~jamie/course/CS/3172/examples/perl/cookies/'.
      'cookie-colour.html">return to the form</a>&nbsp;|',"\n";
print '<a href="'.
      'http://www.cs.dal.ca/~jamie/cgi-bin/3172/cookie/cookie-colour-read.cgi"'.
      '>read cookies</a>]',"\n";
print '</p>';
