#!/usr/bin/python
"""form.py - a python cgi script with forms"""

# import various functionality
import os
import sys
import cgi

# debugging
import cgitb; cgitb.enable()

# uncomment for more debugging
# cgi.test()

# The all-important header
print "Content-type: text/html\n\n"
print ""

print "<header><title>form.py</title></header>"
print """<link rel=stylesheet type="text/css" href="../css/form.css">"""

print '''<h1>Journals at Archemix.</h1> Simply select the journals you want to be included in your inbox from the pull-down menu. If you want to pick more than one journal simply hold down the control key when you select. This woll generate a message so that you can see that you have selected every thing you want to receive. This can be changed at anytime so feel free to adjust your list whenever you want to.
<hr>

 <form method=POST action="form.py">
 <p>Please enter your first name and last initial;
 <p><input name="name" type="text" value="">
 &nbsp <p> &nbsp 
 <p><select multiple name="Example">
     <option selected label="none" value="none">No Magazine</option>
     <optgroup label="Elsevier">
       <option value="science">Science</option>
       <option value="prosci">Protein Science</option>
       <option value="cell">CELL</option>
       </optgroup>
       <optgroup label="Molecules">
       <option label="4" value="MolecularCell">Molecular Cell</option>
       </optgroup>
 </select>

         <form method=POST action="form.py">
         <input type=submit value="Submit"> &nbsp <input type=reset value="Reset"></form>'''

# The fields and their values
form = cgi.FieldStorage()
field = form.getlist("Example")

print "<p><font size=+2>Your name:", form["name"].value
print "</font>"

for mag in field:
    print "<br>You are on the list for:", mag

# write them to a file
file = open("file_cgi.txt", "a")
file.write("<p>name - " + form["name"].value)

for name in field:
    file.write("<br><font color='orange'>")
    file.writelines(name)
    file.write("</font>")
file.close()

## InputFile = open("file_cgi.txt", "r")
## Input = InputFile.read()
## InputFile.close()
## print "<div class='InputFile'>", Input
## print "</div>"


    
