#!/usr/local/bin/perl
#Set preferences for a page. Uses a cookie to save the preferences.
use CGI;

# Some constants to use in our form.
@colors=qw/gray coral bisque beige gold green lime linen 
    orchid seashell sienna silver wheat/;
@sizes=("<default>",1..7);

# Read in the CGI parameters
$q = new CGI;

# recover the "preferences" cookie.
%preferences = $q->cookie('preferences');

# If the user wants to change the background color or her
# name, they will appear among our CGI parameters.
$preferences{'color'} = $q->param('color') if $q->param('color');
$preferences{'name'}  = $q->param('name')  if $q->param('name');
$preferences{'size'}  = $q->param('size')  if $q->param('size');

# Choose the default 'silver' color if not otherwise specified:
$preferences{'color'} = 'silver' unless $preferences{'color'};

# Refresh the cookie so that it doesn't expire.  This also
# makes any changes the user made permanent.
$the_cookie = $q->cookie(-name=>'preferences',
			 -value=>\%preferences,
			 -expires=>'+30d');
print $q->header(-cookie=>$the_cookie);

# Adjust the title to incorporate the user's name, if provided.
$title = $preferences{'name'} ? 
    "Welcome back, $preferences{name}!" : "Customizable Page";

# Create the HTML page.  We use several of Netscape's
# extended tags to control the background color and the
# font size.  It's safe to use Netscape features here because
# cookies don't work anywhere else anyway.
print $q->start_html(-title=>$title,
		     -bgcolor=>$preferences{'color'});

print "<BASEFONT SIZE=$preferences{size}>\n" if $preferences{'size'} > 0;

print <<END;
<H1>$title</H1>
You can change the appearance of this page by submitting
the fill-out form below.  If you return to this page any time
within 30 days, your preferences will be restored.
END
    ;

# Create the form
print join("\n",
	   "<HR>",
	   $q->start_form,

	   "Your first name: ",
	   $q->textfield(-name=>'name',
			 -default=>$preferences{'name'},
			 -size=>30),"<br>",

	   "Preferred page color: ",
	   $q->popup_menu(-name=>'color',
			  -values=>\@colors,
			  -default=>$preferences{'color'}),

	   "Font size: ",
	   $q->popup_menu(-name=>'size',
			  -values=>\@sizes,
			  -default=>$preferences{'size'}),"<br>",

	   $q->submit(-label=>'Set preferences'),
	   "<HR>");
	   
print <<END;
<A HREF="/">The Capricorn Organization Home Page</A>
END
    ;
