#!/usr/bin/perl -Tw

#++
# multi-page.cgi
#
# For Dalhousie CS 4173 (Web-centric Computing)
# Author: J. Blustein
# Created: 31 Oct 2001
# Last Modified: 18 June 2003
#
# Simplistic demonstration of saving state with hidden fields in forms
# See also
#   * time1.cgi (saving state in visible radio buttons)
#   * loan.cgi (a more elegant and complex example) by Lincoln Stein
#   * SessionTrack servlet
#--

use CGI qw(:standard :Carp);
use CGI::Carp 'fatalsToBrowser';
use diagnostics; # verbose error messages
use strict;      # check for mistakes
$| = 1;          # flush output after each output statement
$\ = "\n";       # include \n at end of all output statements



use constant ME             => '/~jamie/cgi-bin/4173/form/multi-page.cgi';
use constant DEFAULT_METHOD => 'post';

## about ME ##
# note that the form is submitted using a relative URL 
# if the URL is absolute
#   then it should be submitted to borg.cs.dal.ca *not* www.cs.dal.ca
#
# The Gory Details
#
#  1. Undergraduate webpages are all hosted on borg so any requests sent to
#      www.cs.dal.ca will be redirected by the server to borg.cs.dal.ca
#      (with one of the HTTP 3xx Redirection status messages) *but* neither
#      Netscape (versions 4.7 and 6) nor Internet Explorer (version 5) will
#      send the method="post" data to the new URL!
#
#  2.  Internet Explorer (version 5) has an option to warn the user that
#      the data they are submitting in a form is not going to the URL
#      that was specified in the form, but that option does not seem to
#      do anything.
##

# get parameter values from the submitted form, or use default values
my $name   = param('name')   || "";
my $colour = param('colour') || "";
my $method = param('m')      || DEFAULT_METHOD;

##direction##
# which page is displayed next depends on which button was used to submit
# the form.  $direction is + for forward buttons, and - for backwards
# buttons
my $direction = param('d') || "+";
   $direction = "+" if $direction eq '>>>';
   $direction = "+" if $direction eq 'Submit';
   $direction = "-" if $direction eq '<<<';

##next_page##
# we read the page number from the hidden field and apply $direction to
# decide which page to display next
my $page   = param('p') || 0;
$page = next_page($page, $direction);

# HTML output begins here
  if (1 == $page) {
      page_1($name,$colour,$method,ME);
  } elsif (2 == $page) {
      page_2($name,$colour,$method,ME);
  } elsif (3 == $page) {
      page_3($name,$colour,$method,ME);
  } elsif (4 == $page) {
      print header, XHTMLhead(),
            "<h3>Multi-page Form Example: Page 4</h3>\n",
            "<p>Goodbye!</p>", XHTMLtail();
  } else {
      die("impossible value `",$page,"' for \$page");
  }

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub next_page() {
    my ($page, $direction) = @_;
    my $next;

    return 1 if "" eq $direction;
    die "impossible direction `", $direction, "'" if ($direction !~ /[-+]/);
    $next = $page + (("+" eq $direction) ?+1 :-1);
    return $next;
} # next_page()

sub XHTMLhead() {
  return <<'EOH'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>
    Multi-page Dynamic Form Example for Dal CS 4173 : Web-centric Computing
  </title>
  <meta name="ROBOTS" content="NOINDEX" />
  <meta name="ROBOTS" content="NOFOLLOW" />
  <link rev="Made"    href="mailto:jamie@cs.dal.ca" />
  <link rel="stylesheet" type="text/css" 
        href="/~jamie/course/CS/4173/CS4173.css" />
  <style type="text/css">
      #control { border: thin black dashed; }
      th       { text-align: center; }
      td,th    { padding-right: 1ex; padding-left: 1ex; }
  </style>
 </head>
 <body>
  <h1><a href="/~jamie/course/CS/4173/"
                title="course homepage"
                >Web-centric Computing</a></h1>
  <h2><a href="/~jamie/course/CS/4173/examples/list.html"
                >Example Code</a></h2>
EOH
;
}# XHTMLhead()

sub XHTMLtail {
  return << 'EOHTML'
    <hr title="end of document proper" />
  
    <h6 title="document identification information">
     <kbd>http://www.cs.dal.ca/~jamie/cgi-bin/4173/form/multi-page.cgi</kbd>
    </h6>
     <dl style="display : compact; float:left">
       <dt>Version:</dt>
        <dd>18 June 2003</dd>
       <dt>Source code:</dt>
        <dd><a href="/~jamie/course/CS/4173/examples/CGI/forms/multi-page.pl.source"
               ><kbd>examples/CGI/forms/multi-page.pl.source</kbd></a></dd>
       <dt><a href="/~jamie/course/CS/4173/"
              ><acronym title="computer science">CS</acronym> 4173</a> <abbr
              title="professor">Prof</abbr>:</dt>
        <dd><a title="author's homepage"
               href="/%7ejamie/"
               ><abbr>J.</abbr>&#160;Blustein</a>
            <kbd>&lt;jamie@cs.dal.ca&gt;</kbd></dd>
     </dl>
     <p style="float:right">
      <a href="http://validator.w3.org/check/referer"><img
         src="/%7ejamie/image/vxhtml10.gif"
         style="height:31px;width:88px"
         alt="This document uses valid XHTML 1.0" /></a>
     </p>
   </body>
  </html>
EOHTML
} # XHTMLtail()

sub page_1 {
    my ($name,$colour,$how,$me) = @_;

    my $postON;
    my $getON;

    if ('post' eq $how) {
	$postON = "checked='checked'";
	$getON  = "";
    } else {
	$postON = "";
	$getON  = "checked='checked'";
    }

    print header;
    print XHTMLhead();
print << "EOH"
<h3>Multi-page Form Example: Page 1</h3>
<form action="$me" method="$how">
  <p>Hi, what's you're name?<br />
     <input type='text'   name='name'     value="$name"   />
     <input type='hidden' name='colour'   value="$colour" />
     <input type='hidden' name='p'        value='1' />
  </p>
  <p>
   <input type='submit' name="d" value='Submit'       />
   <input type='submit' name="d" title='Next page'
                                 value='&gt;&gt;&gt;' />
  </p>
  <p id='control'>Method: 
   <input type='radio' name='m' value='post' $postON />post
   &nbsp;
   <input type='radio' name='m' value='get'  $getON  />get
  </p>
</form>
EOH
    ;
    print XHTMLtail();
} # page_1()



sub page_2 {
    my ($name,$colour,$how,$me) = @_;

    my $postON;
    my $getON;

    if ('post' eq $how) {
	$postON = "checked='checked'";
	$getON  = "";
    } else {
	$postON = "";
	$getON  = "checked='checked'";
    }

    print header;
    print XHTMLhead();
print << "EOH"
<h3>Multi-page Form Example: Page 2</h3>
<form action="$me" method='$how'>
  <p>Hi $name<br />What's you're favourite colour?<br />
     <input type='hidden' name='name'   value="$name"   />
     <input type='text'   name='colour' value="$colour" />
     <input type='hidden' name='p'      value='2' />
  </p>
  <p>
   <input type='submit' name="d" title='Previous page' 
                                 value='&lt;&lt;&lt;' /> 
   <input type='submit' name="d" value='Submit'       />
   <input type='submit' name="d" title='Next page'
                                 value='&gt;&gt;&gt;' />
  </p>
  <p id='control'>Method: 
   <input type='radio' name='m' value='post' $postON />post
   &nbsp;
   <input type='radio' name='m' value='get'  $getON  />get
  </p>
</form>
EOH
    ;
    print XHTMLtail();
} # page_2()



sub page_3 {
    my ($name,$colour,$how,$me) = @_;

    my $postON;
    my $getON;

    if ('post' eq $how) {
	$postON = "checked='checked'";
	$getON  = "";
    } else {
	$postON = "";
	$getON  = "checked='checked'";
    }

    print header;
    print XHTMLhead();
print << "EOH"
<h3>Multi-page Form Example: Page 3</h3>

  <hr />

  <p>Thanks. Here is a table of all of the data I have collected about you:</p>

  <table border="1" rules="groups" class="centre" >
   <colgroup>
     <col />
     <col
       style="border-left:thin black solid; border-right:thin black solid"
     />
     <col />
   </colgroup>
   <thead>
     <tr>
       <th>Variable</th>
       <th>Represents</th>
       <th>Value</th>
     </tr>
   </thead>
   <tbody>
     <tr>
       <td><code>name</code></td>
       <td>Your Name</td>              
       <td>$name</td>
     </tr>
     <tr>
       <td><code>colour</code></td>
       <td>Your Favourite Colour</td>
       <td>$colour</td>
     </tr>
   </tbody>
  </table>

  <hr />

  <form action="$me" method='$how'>
   <p>
     <input type='hidden' name='name'   value="$name"   />
     <input type='hidden' name='colour' value="$colour" />
     <input type='hidden' name='p'      value='3' />
     <input type='submit' name="d" title='Previous page' 
                                   value='&lt;&lt;&lt;' /> 
     <input type='submit' name="d" value='Submit' /> 
   </p>
  <p id='control'>Method: 
   <input type='radio' name='m' value='post' $postON />post
   &nbsp;
   <input type='radio' name='m' value='get'  $getON  />get
  </p>
  </form>
EOH
    ;
    print XHTMLtail();
} # page_3()

## End of Example ##
