;;; File:
;;;   colorful.ss
;;; Version:
;;;   1.0 of 11 February 2003
;;; Author:
;;;   Samuel A. Rebelsky
;;; Contents:
;;;   A simple example of some Scheme procedures for CGI.  
;;; Organization:
;;;   Preparation - Stuff I need to do to get started
;;;   Page Generators - Procedures for generating specific kinds of
;;;     pages (when given particular values)
;;;   Interface - Stuff to extract important values and then
;;;     call the page generators.  You'll find (page) here.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Preparation

;;; Look!  We're going to use Sam's CGI library.  What fun.
(load "/home/rebelsky/Web/Scheme/webutils.ss")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Page Generators

;;; Procedure
;;;   colorful-page
;;; Parameters:
;;;   name, a string
;;; Purpose:
;;;   Generates an HTML page that can greet the named person.
;;; Produces:
;;;   A string that corresponds to that page.
;;; Preconditions:
;;;   The string must be nonempty.
;;; Postconditions:
;;;   The returned page is valid HTML.
(define colorful-page
  (lambda (color)
    (make-page 
      (head "color"
            (string-append "<STYLE>\n"
                           "BODY { color: " color "; }\n"
                           "</STYLE>\n"))
      (body (string-append
              (heading 1 color)
              (string #\newline)
              (paragraph (string-append
                          "'Twas brillig and the slithy toves did "
                          "gyre and bimble in the wabe.  "
                          "All mimsy were the borogoves "
                          "and the mome raths outgrabe."))
              (paragraph (string-append
                          "\"Beware the Jabberwock, my son, "
                          "the jaws that bite, the claws that catch. "
                          "Beware the jubjub bird "
                          "and shun the frumious bandersnatch.\"")))))))
 

;;; Procedure
;;;   colorful-form
;;; Parameters:
;;;   [None]
;;; Purpose:
;;;   Generates an HTML page for an input form for this CGI script.
;;; Produces:
;;;   A string that corresponds to that page.
;;; Preconditions:
;;;   [None]
;;; Postconditions:
;;;   The returned page is valid HTML.
(define colorful-form
  (lambda ()
    (make-page 
      (head "Enter Your Favorite Color")
      (body (form "colorful.cgi"
                  "Select your favorite color and click me!"
                  (string-append "<select name=\"color\">\n"
                                 "<option value=\"blue\">Blue\n"
                                 "<option value=\"green\">Green\n"
                                 "<option value=\"red\">Red\n"
                                 "<option value=\"red\">Yellow\n"
                                 "</select>"))))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Interface

;;; Procedure:
;;;   page
;;; Parameters:
;;;   None
;;; Purpose:
;;;   Builds an HTML page according to specifications.
;;; Produces:
;;;   A string that corresponds to the HTML page.
;;; Preconditions:
;;;   [None]
;;; Postconditions:
;;;   Returns HTML for a valid page.
(define page
  (lambda ()
    (page-helper (get-cgi-variable 'color ""))))

(define page-helper
  (lambda (color)
    (if (string=? color "") ; Default value indicates no param
        (colorful-form)
        (colorful-page color))))
