#!/usr/local/bin/perl
#Includes cgi-lib file; if cgi-lib doesn't exist, returns malformed
#header error

#do "cgi-lib.pl" || die "Fatal Error: Can't load cgi library";

#calls the subroutine in the cgi-lib.pl library
#to read in the variables from the form and set them up
#as key=value pairs in the array @in

&ReadParse;

#check to make sure that the required field is filled in.

$name = $in{'name'};
unless ($name gt 0) {
#unless ($in{'name'}) {
  #sends back NO-GO for response form
  print "Content-type: text/html\n\n";
  print "<html>";
  print "<head>";
  print "<title>Cannot process response</title>";
  print "</head>";
  print "<body>";
  print "<hr>";
  print "<h2>Sorry\!</h2>";
  print "Please make sure that the <i>Name</i> field \n";
  print "is filled in, then re-submit your response.\n";
  print "<p>";
  print "<hr>";
  print "Use your browser's <i>back</i> button to return to the response \n";
  print "form, or click below to return to the Coalition Home Page.\n";
  print "<p>";
  print "<center>";
  print "<h3>Back to the <a href=\"index.html\">";
  print "Coalition Home Page</a></h3>";
  print "</center>";
  print "</body>";
  print "</html>";
  #exit from the script
  exit;
}

#assigns process id to $pid
$pid=$$;

#opens up response file for writing
open(RESPONSE,">/tmp/response_info.$pid");

#enter the form data into the file to be mailed
print RESPONSE "Online Response Form\n";
print RESPONSE "====================\n";
print RESPONSE "We have recieved a response from the following person...\n";
print RESPONSE "           Name: $in{'name'}\n";
print RESPONSE "     School/Org: $in{'org'}\n";
print RESPONSE "Mailing Address: $in{'addr_l1'}\n";
print RESPONSE "                 $in{'addr_l2'}\n";
print RESPONSE "           City: $in{'city'}\n";
print RESPONSE "          State: $in{'state'}\n";
print RESPONSE "            Zip: $in{'zip'}\n";
print RESPONSE "  Daytime Phone: $in{'day_phone'}\n";
print RESPONSE "  Evening Phone: $in{'eve_phone'}\n";
print RESPONSE "            Fax: $in{'fax'}\n";
print RESPONSE "          Email: $in{'email'}\n";
print RESPONSE "--------------------\n";

print RESPONSE "This person would like information on the following topics...\n\n";

if ($in{'affiliation'}) {
    print RESPONSE "AFFILIATION\n";
}
if ($in{'subscription'}) {
    print RESPONSE "SUBSCRIPTION\n";
}
#if ANY of the fields pertaining to PRESENTATION are filled-in, then
#assume that they meant to check off the box, even if they didn't.
if (($in{'presentation'}) || ($in{'pres_org'}) || ($in{'pres_date'})) {
    print RESPONSE "PRESENTATION\n";

    if ($in{'pres_org'}) {
        print RESPONSE "     for $in{'pres_org'}\n";
    }
    if ($in{'pres_date'}) {
        print RESPONSE "      on $in{'pres_date'}\n";
    }
}
if ($in{'sdp'}) {
    print RESPONSE "STANDARDS DISSEMINATION PROJECT\n";
}
if ($in{'web_site'}) {
    print RESPONSE "WEB SITE\n";
}

#
if ($in{'comments'}) {
    print RESPONSE "--------------------\n";
    print RESPONSE "This person had the following comments...\n";
    print RESPONSE $in{'comments'};
    print RESPONSE "\n";
}

print RESPONSE "====================\n";

#close file to be mailed
close RESPONSE;

#mails response file to user
#remember to put in something that specifies a SUBJECT
$command="mail -s \"online response form\" magarell@dimacs.rutgers.edu < /tmp/response_info.$pid";
system($command);

#erases temp file
unlink("/tmp/response_info.$pid");

#acknowledges receipt of information
print "Content-type: text/html\n\n";
print "<html>";
print "<head>";
print "<title>Thank You\!</title>";
print "</head>";
print "<body>";
print "<h2>Thank you\!</h2>";
print "Thank you for responding.\n";
print "<p>";

#if they want to become an Affiliate, inform them how 
#they should pay.
if ($in{'affiliation'}) {
  print "You have indicated that you would like to become an Affiliate\n";
  print "of the Coalition.\n";
  print "<p>";
  print "Please send your name, mailing address, and a check for \$25 to...\n";
  print "<br>";
  print "<pre>";
  print "<b>";
  print "     NJ Mathematics Coalition\n";
  print "     P.O. Box 10867\n";
  print "     New Brunswick, NJ 08906\n";
  print "</b>";
  print "</pre>";
  print "Make check payable to \n";
  print "<i>New Jersey Mathematics Coalition</i>.\n";
  print "<p>";
}
  
print "<hr>";
print "<center>";
print "<h3>Back to the <a href=\"index.html\">";
print "Coalition Home Page</a></h3>";
print "</center>";
print "</body>";
print "</html>";



# Perl Routines to Manipulate CGI input
# S.E.Brenner@bioc.cam.ac.uk
# $Header: /cys/people/brenner/http/cgi-bin/RCS/cgi-lib.pl,v 1.8 1995/04/07 21:35:29 brenner Exp $
#
# Copyright 1994 Steven E. Brenner  
# Unpublished work.
# Permission granted to use and modify this library so long as the
# copyright above is maintained, modifications are documented, and
# credit is given for any use of the library.
#
# Thanks are due to many people for reporting bugs and suggestions
# especially Meng Weng Wong, Maki Watanabe, Bo Frese Rasmussen,
# Andrew Dalke, Mark-Jason Dominus and Dave Dittrich.

# For more information, see:
#     http://www.bio.cam.ac.uk/web/form.html       
#     http://www.seas.upenn.edu/~mengwong/forms/   

# Minimalist http form and script (http://www.bio.cam.ac.uk/web/minimal.cgi):
#
# require "cgi-lib.pl";
# if (&ReadParse(*input)) {
#    print &PrintHeader, &PrintVariables(%input);
# } else {
#   print &PrintHeader,'<form><input type="submit">Data: <input name="myfield">';
#}

# ReadParse
# Reads in GET or POST data, converts it to unescaped text, and puts
# one key=value in each member of the list "@in"
# Also creates key/value pairs in %in, using '\0' to separate multiple
# selections

# Returns TRUE if there was input, FALSE if there was no input 
# UNDEF may be used in the future to indicate some failure.

# Now that cgi scripts can be put in the normal file space, it is useful
# to combine both the form and the script in one place.  If no parameters
# are given (i.e., ReadParse returns FALSE), then a form could be output.

# If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
# information is stored there, rather than in $in, @in, and %in.

sub ReadParse {
  local (*in) = @_ if @_;
  local ($i, $key, $val);

  # Read in text
  if (&MethGet) {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  }

  @in = split(/&/,$in);

  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;

    # Split into key and value.  
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;

    # Associate key and value
    $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;

  }

  return length($in); 
}


# PrintHeader
# Returns the magic line which tells WWW that we're an HTML document

sub PrintHeader {
  return "Content-type: text/html\n\n";
}


# MethGet
# Return true if this cgi call was using the GET request, false otherwise

sub MethGet {
  return ($ENV{'REQUEST_METHOD'} eq "GET");
}

# MyURL
# Returns a URL to the script
sub MyURL  {
  return  'http://' . $ENV{'SERVER_NAME'} .  $ENV{'SCRIPT_NAME'};
}

# CgiError
# Prints out an error message which which containes appropriate headers,
# markup, etcetera.
# Parameters:
#  If no parameters, gives a generic error message
#  Otherwise, the first parameter will be the title and the rest will 
#  be given as different paragraphs of the body

sub CgiError {
  local (@msg) = @_;
  local ($i,$name);

  if (!@msg) {
    $name = &MyURL;
    @msg = ("Error: script $name encountered fatal error");
  };

  print &PrintHeader;
  print "<html><head><title>$msg[0]</title></head>\n";
  print "<body><h1>$msg[0]</h1>\n";
  foreach $i (1 .. $#msg) {
    print "<p>$msg[$i]</p>\n";
  }
  print "</body></html>\n";
}

# PrintVariables
# Nicely formats variables in an associative array passed as a parameter
# And returns the HTML string.

sub PrintVariables {
  local (%in) = @_;
  local ($old, $out, $output);
  $old = $*;  $* =1;
  $output .=  "<DL COMPACT>";
  foreach $key (sort keys(%in)) {
    foreach (split("\0", $in{$key})) {
      ($out = $_) =~ s/\n/<BR>/g;
      $output .=  "<DT><B>$key</B><DD><I>$out</I><BR>";
    }
  }
  $output .=  "</DL>";
  $* = $old;

  return $output;
}

# PrintVariablesShort
# Nicely formats variables in an associative array passed as a parameter
# Using one line per pair (unless value is multiline)
# And returns the HTML string.


sub PrintVariablesShort {
  local (%in) = @_;
  local ($old, $out, $output);
  $old = $*;  $* =1;
  foreach $key (sort keys(%in)) {
    foreach (split("\0", $in{$key})) {
      ($out = $_) =~ s/\n/<BR>/g;
      $output .= "<B>$key</B> is <I>$out</I><BR>";
    }
  }
  $* = $old;

  return $output;
}

1; #return true 

