#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

my %States;             # state table mapping pages to functions
my $Current_Screen;     # the current screen

# Hash of pages and functions.

%States = (
    	'Page 1'       => \&page1,
    	'Finish'    => \&finish,
    	'Submit'       => \&send,
    	'Cancel'      => \&page1,
);

$Current_Screen = param(".State") || "Page 1";
die "No screen for $Current_Screen" unless $States{$Current_Screen};

# Generate the current page.

standard_header();

while (my($screen_name, $function) = each %States) {
    $function->($screen_name eq $Current_Screen);
}

standard_footer();
exit;


################################
# header, footer, menu functions
################################

sub standard_header {
    print header(), start_html(-Title => "Office of Admissions", -BGCOLOR=>"White");
    print start_form(); # start_multipart_form() if file upload
}

sub standard_footer { print end_form(), end_html() }

sub form_menu {
	my ($page) = @_;
		if ($page eq "page1") {
			print  p(defaults("Clear Form"),
				to_page("Finish"));
		} else {
			print p(defaults("Clear Form"),
                                to_page("Page 1"),
                                to_page("Finish"));
		}
}

# first page
sub page1 {
	my $active = shift;
	my @persons;
	$persons[0]="Select";
	$persons[1]="Prospective Student";
	$persons[2]="Parent/Guardian";
	$persons[3]="Relative";
	$persons[4]="Other";

	my @state = qw(	Select AK AL AR AZ CA CO CT DC DE FL GA GU HI IA ID IL IN KS KY
			LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK 
			OR PA PR RI SC SD TN TX UT VA VI VT WA WI WV WY);

	my ($name,$address,$city,$state,$zip,$phone,$email) = (param("name"), param("address"), param("city"), param("state"), param("zip"), param("phone"), param("email"));
	
	unless ($active) {
		print hidden("name") if $name;
		print hidden("address") if $address;
		print hidden("city") if $city;
		print hidden("state") if $state;
		print hidden("zip") if $zip;
		print hidden("phone") if $phone;
		print hidden("email") if $email;
		return;
	}

	print h1("Requesting More Information");

	print p("Name:", textfield("name"));
	print p("Address:", textfield("address"));
	print p("City:", textfield("city"));
	print p("State:", popup_menu("state", \@state ));
	print p("ZIP Code:", textfield("zip"));
	print p("Phone number:", textfield("phone"));
	print p("Email address:", textfield("email"));

	form_menu("page1");

}

# Page to confirm submittal 
sub finish {
    	my $active = shift;

    	return unless $active;

    	print h1("Form Confirmation");
    	print p("You have entered the following info: ");
	print p("Click 'Submit' to send your info to or click on 'Page 1' to edit.");
	print show_data();
    	print p(to_page("Submit"), to_page("Page 1")); 
}

# Page to complete a submittal 
sub send {
    	my $active = shift;

    	unless ($active) {
        	return;
    	}
	
	# add in actual submit code here

    	print h1("Submitted!");
}

sub show_data {
        my $html = '';
        if (param("name")) {
                $html .= p("Name: ", param("name"));
        }
        if (param("address")) {
                $html .= p("Address: ", param("address"));
        }
        if (param("city")) {
                $html .= p("City: ", param("city"));
        }
        if (param("state")) {
                $html .= p("State: ", param("state"));
        }
        if (param("zip")) {
                $html .= p("Zip: ", param("zip"));
        }
        if (param("phone")) {
                $html .= p("Phone: ", param("phone"));
        }
        if (param("email")) {
                $html .= p("Email: ", param("email"));
        }

	$html = p("No data was submitted.") unless $html;

        return $html;
}

sub to_page { submit(-NAME => ".State", -VALUE => shift) }
