#!/usr/bin/perl -w
#----------------------------------------------------------------------
# plus_minus.pl - expands or contracts a list using [+] and [-] buttons
# written by Eric Pence
#----------------------------------------------------------------------
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

# Define global variables
use vars qw( $curr_dir $log_file );

# Grab the current directory from $0. The following regular expression
# looks for 'CURR_DIR\xxxxx.xxx' or 'CURR_DIR/xxxxx.xxx'. Place it in
# the BEGIN block so we can use it to include modules
BEGIN {
$0 =~ '(.*[\\\/])\w+\.\w+$';
$curr_dir = $1 || "./";

# Set up error log file
$log_file = "error_log.txt";
use CGI::Carp qw(carpout);
open(LOG, ">>${curr_dir}${log_file}")
or die "Unable to append to error log: $!";
carpout(*LOG);
}

use lib ( $curr_dir );

my $q = new CGI;

my $group1 = '';
my $group2 = '';
my $group3 = '';
my $expand_button1 = '';
my $expand_button2 = '';
my $expand_button3 = '';

# Get current settings of buttons -- default to [+] button
my $expand_button1_status = $q->param('expand_button1_status') || 'plus';
my $expand_button2_status = $q->param('expand_button2_status') || 'plus';
my $expand_button3_status = $q->param('expand_button3_status') || 'plus';

# Was a button clicked to get here?
my $expand_button1_clicked = $q->param('expand_button1_clicked') || '';
my $expand_button2_clicked = $q->param('expand_button2_clicked') || '';
my $expand_button3_clicked = $q->param('expand_button3_clicked') || '';

# Check button click and format expanded status
if ($expand_button1_clicked) {
# reverse status of button -- switch [+] to [-] or [-] to [+]
if ($expand_button1_status eq 'plus') {
$expand_button1_status = 'minus';
}
else {
$expand_button1_status = 'plus';
}
}
elsif ($expand_button2_clicked) {
if ($expand_button2_status eq 'plus') {
$expand_button2_status = 'minus';
}
else {
$expand_button2_status = 'plus';
}
}
elsif ($expand_button3_clicked) {
if ($expand_button3_status eq 'plus') {
$expand_button3_status = 'minus';
}
else {
$expand_button3_status = 'plus';
}
}

# Format buttons and lists based on expanded status value
if ($expand_button1_status eq 'minus') {
$expand_button1 = "<img src=\"images/button_minus.gif\" width=\"14\" height=\"14\" border=\"0\" hspace=\"15\">";
# Make list
$group1 = format_group();
}
else {
$expand_button1 = "<img src=\"images/button_plus.gif\" width=\"14\" height=\"14\" border=\"0\" hspace=\"15\">";
}
if ($expand_button2_status eq 'minus') {
$expand_button2 = "<img src=\"images/button_minus.gif\" width=\"14\" height=\"14\" border=\"0\" hspace=\"15\">";
# Make list
$group2 = format_group();
}
else {
$expand_button2 = "<img src=\"images/button_plus.gif\" width=\"14\" height=\"14\" border=\"0\" hspace=\"15\">";
}
if ($expand_button3_status eq 'minus') {
$expand_button3 = "<img src=\"images/button_minus.gif\" width=\"14\" height=\"14\" border=\"0\" hspace=\"15\">";
# Make list
$group3 = format_group();
}
else {
$expand_button3 = "<img src=\"images/button_plus.gif\" width=\"14\" height=\"14\" border=\"0\" hspace=\"15\">";
}

print $q->header();
print "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>Expand folders</title>
<link href=\"../../penceland.css\" rel=\"stylesheet\" type=\"text/css\">
</head>
<body background=\"../../images/myback.gif\">
<center>
<p>
&nbsp;
<p>
<h2>Expand/contract list</h2>
<p>&nbsp;</p>
<table width=\"55%\"><tr><td>
<center>
<table nowrap><tr><td style=\"line-height: 1.5\">
I created this to emulate the <i>look-and-feel</i> of Windows Explorer, where you click on the<br>
<img src=\"images/button_plus.gif\" width=\"14\" height=\"14\" title=\"[+]\" hspace=\"5\"> in front of a folder to expand the list and show sub-folders, which is then replaced by the<br>
<img src=\"images/button_minus.gif\" width=\"14\" title=\"[-]\" height=\"14\" hspace=\"5\"> to click on and contract it. The code I use to produce this can be seen in <a
href=\"../print_source.pl?src=plus_minus/plus_minus.pl\" class=\"blueul\">plus_minus.pl</a>.
</td></tr></table>
<br><img src=\"images/clearspacer.gif\" height=\"40\"></br>
<table border=\"2\" width=\"50%\" bgcolor=\"ffffff\"><tr><td>
<br><img src=\"images/clearspacer.gif\" height=\"20\"></br>
<center>
<table>
<tr>
<td valign=\"top\">
<a href=\"plus_minus.pl?expand_button1_clicked=1&expand_button1_status=$expand_button1_status&expand_button2_status=$expand_button2_status&expand_button3_status=$expand_button3_status\">$expand_button1</a>
</td>
<td style=\"font-size: 10pt; font-weight: bold\">First group of items<br>
$group1
</td>
</tr>
<tr>
<td><img src=\"images/clearspacer.gif\" height=\"20\"></td>
</tr>
<tr>
<td valign=\"top\">
<a href=\"plus_minus.pl?expand_button2_clicked=1&expand_button1_status=$expand_button1_status&expand_button2_status=$expand_button2_status&expand_button3_status=$expand_button3_status\">$expand_button2</a>
</td>
<td style=\"font-size: 10pt; font-weight: bold\">Second group of items<br>
$group2
</td>
</tr>
<tr>
<td><img src=\"images/clearspacer.gif\" height=\"20\"></td>
</tr>
<tr>
<td valign=\"top\">
<a href=\"plus_minus.pl?&expand_button3_clicked=1&expand_button1_status=$expand_button1_status&expand_button2_status=$expand_button2_status&expand_button3_status=$expand_button3_status\">$expand_button3</a>
</td>
<td style=\"font-size: 10pt; font-weight: bold\">Third group of items<br>
$group3
</td>
</tr>
</table>
</center>
<br><img src=\"images/clearspacer.gif\" height=\"20\"></br>
</td></tr></table>
<br><img src=\"images/clearspacer.gif\" height=\"20\"></br>
<form>
<input type=\"button\" name=\"return\" style=\"font-size: 10pt\" value=\"Return to Programming page\" onClick=\"window.location.href='http://www.penceland.com/program.html#perl'\"><br>
<font color=\"blue\">(use this instead of Back button)</font>
</form>
</center>
</body>
</html>";
exit;

sub format_group {
my $group = "<table>";
for (my $i=1; $i<6; $i++) {
$group = $group . "<tr><td>Line of data for item$i</td></tr>";
}
$group = $group . "</table>";
return $group;
}