#!/usr/bin/perl
################################################################################
#
# This script generates the interface for the search form.
# Both the "strict" and "warnings" pragma have been turned on to increase
# (read - enforce) the legibility of my code, and the CGI.pm module was
# incorporated both for simpler HTML generation and also for the very handy
# "fatalsToBrowser" option, which allowed for more efficient code testing.
#
################################################################################

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

################################################################################
#
# This section prints out the title, welcome message, and user notes.
# After this, we start the <FORM> section, using the following form attributes:
#	METHOD => POST
#	ACTION => dbscript.cgi
#	NAME   => plantForm
#
# This form consists of a textfield for the plant's Common Name or Latin Name
# and numerous radiobutton and checkbox groups which represent the searchable
# fields that can be checked against the contents of the database.
#
################################################################################

print header('text/html'), "\n";
print start_html(-title => 'SQL Database Testpage', -author => 'gjohnson@marlboro.edu', -bgcolor => 'white'), "\n";
print h2('Welcome to the SQL Database Testpage'), "\n";
print p('Please select the values below that describe your plant:'), "\n";
print p(i('Notes:<br>1) You may enable a field for searching by clicking the checkbox next to its name.<br>2) You may learn more about the possible values in a field by clicking its link.')), "\n";
print hr(), "\n";
print startform(-method => 'POST', -action => 'dbscript.cgi', -name => 'plantForm'), "\n";
print 'Name:', textfield(-name => 'PlantName', -size => '25'), "\n";
print radio_group(-name => 'PlantNameType', -values => ['CommonName', 'LatinName'], -default => 'CommonName', -columns => '2'), "\n";
print hr(), "\n";

# Prominent ID Characteristics:		# These are being used as the
#       Bark Pattern			# fields for the search form.
#       Bark Color
#       Twig Characteristics
#       Leaf Branching
#       Leaf Complexity
#       Leaf Blade Shape
#       Leaf Blade Teeth
#       Other Leaf Characteristics
#       Flower Type
#       Flower Color
#       Flowering Period
#       Fruit Type
#       Fruit Color
#       Fruiting Period

################################################################################
#
# These are obviously the radiobutton and checkbox groups described above.
# The format for these two hashes is as follows:
#
#	'Group Name' => ['Traits', 'In', 'An', 'Anonymous', 'Array']
#
# Each one of these entries will create another search field with a set of
# values in either radiobutton or checkbox format depending on which of the
# two hashes it is entered under.
#
################################################################################

my %radioGroups = (
	'Bark Pattern' => ['Ridged', 'Furrowed', 'Scaly', 'Smooth', 'Peeling', 'Other'],
	'Bark Color' => ['LightGray', 'DarkGray', 'LightBrown', 'DarkBrown', 'White', 'Black', 'ReddishBrown', 'Green', 'Other'],
	'Leaf Branching' => ['Alternate', 'Opposite'],
	'Leaf Complexity' => ['Simple', 'PalmatelyCompound', 'PinnatelyCompound', 'TwiceOrThrice Compound'],
	'Leaf Blade Shape' => ['Elliptic', 'Egg-shaped', 'Wedge-shaped', 'Heart-shaped', 'PalmatelyLobed', 'PinnatelyLobed', 'NeedleOrScale-like'],
	'Leaf Blade Teeth' => ['None', 'Wavy-edged', 'Single-toothed', 'Double-toothed'],
	'Flower Type' => ['Cone', 'Catkins', 'RadiallySymmetrical', 'BilaterallySymmetrical', 'DaisyOrDandelion-Like', 'LongClusters', 'RoundClusters'],
	'Flower Color' => ['White', 'Yellow', 'Orange', 'PinkOrRed', 'BlueOrViolet', 'Green', 'Brown'],
	'Flowering Period' => ['Spring', 'LateSpring-EarlySummer', 'Summer', 'LateSummer-EarlyFall', 'Fall', 'Winter'],
	'Fruit Type' => ['SeedsOnly', 'Nuts', 'FleshyIndividuals', 'FleshyClusters'],
	'Fruit Color' => ['White', 'Yellow', 'Orange', 'PinkOrRed', 'BlueOrViolet', 'Green', 'Brown'],
	'Fruiting Period' => ['Spring', 'LateSpring-EarlySummer', 'Summer', 'LateSummer-EarlyFall', 'Fall', 'Winter']
);

my %checkboxGroups = (
	'Twig Characteristics' => ['SpurBranchesCommon', 'Thorns', 'ChamberedPith', 'EndBudsPresent'],
	'Other Leaf Characteristics' => ['Hairy', 'Glossy', 'Rough', 'Lighter-coloredBeneath', 'VeinsFollowLeafEdges']
);

################################################################################
#
# Here is the opportunity to decide in what order the fields will be displayed
# on the search form.  Of course, if an entry is inserted into one of the 
# hashes above but is not placed in the @orderedAttributes array, it will NOT
# be displayed on the webpage.
#
################################################################################

my @orderedAttributes = ('Bark Pattern', 'Bark Color', 'Twig Characteristics', 'Leaf Branching', 'Leaf Complexity', 
	'Leaf Blade Shape', 'Leaf Blade Teeth', 'Other Leaf Characteristics', 'Flower Type', 'Flower Color', 
	'Flowering Period', 'Fruit Type', 'Fruit Color', 'Fruiting Period');

################################################################################
#
# Yep.  Now we're finally looping through all those entries and spitting them
# out to the screen.  I hope this code isn't too complicated to read.
#
# The most difficult aspect here is the javascript code that has been inserted
# to enable and disable the search fields based on whether or not the checkbox
# at the top of each subgroup is selected.  If it is not checked, no data
# from that field will be passed to the search script.  This will help to speed
# along the searching program, and makes my life easier because I don't have to
# create default values for each group.
#
################################################################################

foreach my $attribute (@orderedAttributes) {
	my $attributeConcatenated = join('', split(/ /, $attribute));
	my $whichGroup = (@{$radioGroups{$attribute}}[0] ? \%radioGroups : \%checkboxGroups);

	print '<BR>', checkbox(-name => '', -onClick => 
		"if (document.plantForm." . $attributeConcatenated . "[0].disabled == 1) { 
			for (i=0; i<" . @{$$whichGroup{$attribute}} . "; i++) {
				document.plantForm." . $attributeConcatenated . "[i].disabled=0;
			}
		} else { 
			for (i=0; i<" . @{$$whichGroup{$attribute}} . "; i++) {
				document.plantForm." . $attributeConcatenated . "[i].disabled=1;
			}
		}"),
		a({-href => "javascript:window.open('helpPage.html', 'helpPageWindow').resizeTo(600,600)"}, $attribute), "\n";

	if (@{$radioGroups{$attribute}}[0]) {
		print radio_group(-name => $attributeConcatenated, -values => $radioGroups{$attribute}, 
			-default => "@{$radioGroups{$attribute}}[0]", -columns => 1, -disabled => 1), "\n";
	} else {
		print checkbox_group(-name => $attributeConcatenated, -values => $checkboxGroups{$attribute},
			-columns => 1, -disabled => 1), "\n";
	}
}

################################################################################
#
# Now we print out the Submit and Clear buttons, close the form, and clean up.
#
################################################################################

print hr(), "\n";
print submit(-value => 'Submit'), "\n";
print reset(), "\n";
print endform(), "\n";
print end_html(), "\n";
