#!/usr/bin/perl
##
#######################################################
#		The Comish v2.02
#     
#    		Created by Solution Scripts
# 		Email: solutions@solutionscripts.com
#		Web: http://solutionscripts.com
#
#######################################################
#
#
# COPYRIGHT NOTICE:
#
# Copyright 1999 Solution Scripts  All Rights Reserved.
#
# Selling the code for this program without prior written consent is
# expressly forbidden. In all cases
# copyright and header must remain intact.
#
######################################################

$no_stats=0;
$demo = 0;


$comish_name="";
$path="";
$comish_path="";
$url="";
$url_to_cgi="";

require "variables.pl";
require "the_comish.lib";

unless ($over_bg) { $over_bg= "white"; }
unless ($table_bg) { $table_bg= "slateblue"; }
unless ($table_head_bg) { $table_head_bg= "MediumBlue"; }
unless ($text_color) { $text_color= "black"; }
unless ($link_color) { $link_color= "blue"; }
unless ($text_table) { $text_table= "white"; }
unless ($text_table_head) { $text_table_head= "white"; }
unless ($text_highlight) { $text_highlight= "red"; }
unless ($font_face) { $font_face= "arial"; }
unless ($font_size) { $font_size= "2"; }
unless ($alt1) { $alt1= "MediumSlateBlue"; }
unless ($alt2) { $alt2= "SlateBlue"; }

print "Content-type: text/html \n\n";

$version = "2.02";
$v1 = 2;
$v2 = 0;
$v3 = 2;

$time = time;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
$year +=1900;
$mon++;
$now = "$mon.$mday.$year";

@months = ('Blank','January','February','March','April','May','June','July','August','September','October','November','December');


$cgiurl = $ENV{'SCRIPT_NAME'};
$thisurl = $ENV{'SCRIPT_NAME'};

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
	else { $INPUT{$name} = $value; }
}

&Top;

if ($path) {
	if ( -e "$path/data/password.txt") {
		open (VARIABLES, "$path/data/password.txt") || &error("Error reading password.txt");
		$admin_password = <VARIABLES>;
		close (VARIABLES);
	}
}

if ($INPUT{'newpass'}) { &newpass; }
elsif ($INPUT{'setup_2'}) { &setup_2; }
elsif ($INPUT{'setup_4'}) { &setup_4; }
elsif ($INPUT{'setup_6'}) { &setup_6; }
elsif ($INPUT{'setup_9'}) { &setup_9; }
elsif ($INPUT{'setup_10'}) { &setup_10; }

elsif ($INPUT{'features'}) { &features; }
elsif ($INPUT{'features_gen'}) { &features_gen; }
elsif ($INPUT{'features_tiers'}) { &features_tiers; }

elsif ($INPUT{'viewdaily'}) { &viewdaily; }
elsif ($INPUT{'view'}) { &view; }
elsif ($INPUT{'view_reoccur'}) { &view_reoccur; }

elsif ($INPUT{'daily_detailed'}) { &daily_detailed; }
elsif ($INPUT{'confirm'}) { &confirm; }
elsif ($INPUT{'approve'}) { &approve; }
elsif ($INPUT{'aapprove'}) { &aapprove; }
elsif ($INPUT{'delete_final'}) { &delete_final; }
elsif ($INPUT{'con_final'}) { &con_final; }
elsif ($INPUT{'acc_del_final'}) { &acc_del_final; }
elsif ($INPUT{'acc_del'}) { &acc_del; }
elsif ($INPUT{'acc_stats'}) { &acc_stats; }
elsif ($INPUT{'acc_daily'}) { &acc_daily; }
elsif ($INPUT{'acc_paid'}) { &acc_paid; }
elsif ($INPUT{'acc_purch'}) { &acc_purch; }
elsif ($INPUT{'acc_edit_purch'}) { &acc_edit_purch; }
elsif ($INPUT{'acc_edit_purch_final'}) { &acc_edit_purch_final; }
elsif ($INPUT{'edit'}) { &edit; }
elsif ($INPUT{'edit_paid_final'}) { &edit_paid_final; }
elsif ($INPUT{'edit_del_paid'}) { &edit_del_paid; }
elsif ($INPUT{'text'}) { &text; }
elsif ($INPUT{'new_purchase'}) { &new_purchase; }
elsif ($INPUT{'new_purchase_ip'}) { &new_purchase_ip; }
elsif ($INPUT{'new_purchase_acc'}) { &new_purchase_acc; }
elsif ($INPUT{'textedit'}) { &textedit; }
elsif ($INPUT{'pay'}) { &pay; }
elsif ($INPUT{'backup'}) { &backup; }
elsif ($INPUT{'restore'}) { &restore; }
elsif ($INPUT{'main'}) { &main; }
elsif ($INPUT{'paylog'}) { &paylog; }
elsif ($INPUT{'paid'}) { &paid; }
elsif ($INPUT{'purch'}) { &purch; }
elsif ($INPUT{'sendemail'}) { &sendemail; }
elsif ($INPUT{'sentemail'}) { &sentemail; }
else { &admin; }
exit;


sub setup {

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD align=left colspan=2>
<font face=arial size=-1>
<b>Welcome To The Comish</b><BR><BR>
Before you can start building your affiliates program with The Comish, we need to configure a few variables for the program to run correctly on your server.<BR><BR>
This setup has been built in such a way as to make this set-up as quick and easy for you.
Should you have any questions, the <a href="http://solutionscripts.com/lounge/comish"><font color="Red">The Comish Members Lounge</font></a> has a forum to help you out. 
Just describe your problem and copy and paste the few lines in the small text at the bottom, this will greatly help us to help you.<br><br>
Thank you again for purchasing The Comish<br>
Tim Watson, <a href="mailto:solutions\@solutionscripts.com"><font color="Red">Solution Scripts</font></a>........

<BR><BR><BR>
<b>Step 1</b><br>
EOF
if ($admin_password) {
print <<EOF;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Step 1 is where we usually ask you for your password, but since yours is already set, all we need you to do
is enter it in below, to verfiy that it is correct, and we can proceed with the rest of the setup questions
EOF
}
else {
print <<EOF;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
We will first need to set your admininstrative password, giving you access to this file and all the admin features.
After you finish this setup, and reload admin.cgi the next time, you will be asked for this password.
EOF
}

print <<EOF;
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Password:</b>
</TD>
<TD><input type="Password" name="password" size="30"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you are done, <input type="Submit" name="setup_2" value="Proceed to Step 2">
</FORM>
<br><br><br>
</font>
<font face=arial size=-2>
Server Software -- $ENV{'SERVER_SOFTWARE'}<BR>
Server Name -- $ENV{'SERVER_NAME'}<BR>
Doc. Root -- $ENV{'DOCUMENT_ROOT'}<BR>
Script Filename -- $ENV{'SCRIPT_FILENAME'}<BR>
</FONT>
</TD></TR></TABLE> 


EOF
&Footer;
exit;
}

sub setup_2 {


if ($admin_password) {
	$password = $INPUT{'password'};
	&checkpassword;
}
else {
	$newpassword = crypt($INPUT{'password'}, ai);
	$password = $INPUT{'password'};
	
	open (VARIABLES, "variables.pl") || &error("Error reading variables.pl");
	@variables = <VARIABLES>;
	close (VARIABLES);

	$variables[1] = " \n";
	$variables[2] = " \n";
	$variables[3] = " \n";
	$variables[4] = " \n";
	$variables[5] = " \n";
	$variables[6] = " \n";
	$variables[7] = " \n";
	$variables[8] = " \n";
	$variables[9] = " \n";
	$variables[10] = " \n";
	$variables[11] = " \n";										
	$variables[12] = "\n";
	
	$varilength = push(@variables);
	$varilength = $varilength-1;
	chomp($variables[$varilength]);
	unless ($variables[$varilength] eq '1\;') {
		$varilength++;
		$variables[$varilength] = "\n1\;";
	}
	
	open (VARIABLES, ">variables.pl") || &error("Error writing variables.pl");
	print VARIABLES @variables;
	close (VARIABLES);


}

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD colspan=2 >
<font face=arial size=-1>
<center><B>The Comish Setup, Steps 2 & 3</B></center>
<BR><BR>
<b>Step 2</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
We will first start by giving a name to your The Comish program. This should be the name of your affiliates program, members will see
this name when they sign up, check stats, and get email from you..
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Program name:</b>
</TD>
<TD><input type="Text" name="setup_name" value="$comish_name" size="30"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<BR><BR>
<b>Step 3</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Now we need your email address. This will be the "from" email address on all emails sent to members, and the
one that they will be refered to should they have a question or problem. 
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Your Email Address:</b>
</TD>
<TD><input type="Text" name="setup_email" value="$your_email" size="30"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you are done, <input type="Submit" name="setup_4" value="Proceed to Step 4">
</FORM>
</TD></TR></TABLE> 

EOF
&Footer;
exit;
}

sub setup_4 {

$password = $INPUT{'password'};
if ($admin_password) {
	&checkpassword;
}

$error=0;
$error_name=0;
$error_email = 0;

unless ($INPUT{'setup_name'}) {
	$error=1;
	$error_name = "You must enter a name for your The Comish program. You can always change it later";
}
unless ($INPUT{'setup_email'} =~ /.*\@.*\..*/) {
	$error=1;
	$error_email = "Problems have been detected with the email address you entered, please check it";
}
if ($error) {

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD colspan=2 >
<font face=arial size=-1>
<center><B>The Comish Setup, Steps 2 & 3</B></center>
<BR><BR>
EOF
if ($error_name) {
print <<EOF;
<b>Step 2</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
$error_name
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Top Site name:</b>
</TD>
<TD><input type="Text" name="setup_name" value="$INPUT{'setup_name'}" size="30"></TD>
</TR>
EOF
}
else {
	print "<input type=\"hidden\" name=\"setup_name\" value=\"$INPUT{'setup_name'}\">\n";
}
if ($error_email) {
print <<EOF;
<TR><TD colspan=2 >
<font face=arial size=-1>
<BR><BR>
<b>Step 3</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
$error_email
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Your Email Address:</b>
</TD>
<TD><input type="Text" name="setup_email" value="$INPUT{'setup_email'}" size="30"></TD>
</TR>
EOF
}
else {
	print"<input type=\"Hidden\" name=\"setup_email\" value=\"$INPUT{'setup_email'}\">\n";
}

print <<EOF;
<TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you have fixed your errors, <input type="Submit" name="setup_4" value="Proceed to Step 4">
</FORM>
</TD></TR></TABLE> 
EOF
&Footer;
exit;
}

$INPUT{'setup_email'} =~ s/\@/\\\@/ig;

	open (VARIABLES, "variables.pl");
	@variables = <VARIABLES>;
	close (VARIABLES);

	unless ($demo) {
		$variables[1] = "\$comish_name \= \"$INPUT{'setup_name'}\"\;\n";
		$variables[6] = "\$your_email \= \"$INPUT{'setup_email'}\"\;\n";
	}
			
	$varilength = push(@variables);
	$varilength = $varilength-1;
	chomp($variables[$varilength]);
	unless ($variables[$varilength] =~ /^1/) {
		$varilength++;
		$variables[$varilength] = "\n1\;";
	}
	open (VARIABLES, ">variables.pl");
	print VARIABLES @variables;
	close (VARIABLES);

$current_path = $ENV{'SCRIPT_FILENAME'};
$current_path =~ s/admin\.cgi//ig;

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD colspan=2 >
<font face=arial size=-1>
<center><B>The Comish Setup, Steps 4 & 5</B></center>
<BR><BR>
The next 2 steps will need you to enter a path, a path is the system of folders, we need the paths
so we can store the data files and the path to where your billing script is, so we may set the cookie correctly. A path will be something like: 
<font color=red>/mnt/web/guide/solutionscripts/comish</font> The path <B>is not the url</b> and <B>does not
start with http://</B>. With that said, and using a few enviromental variables of perl, we have helped you out a bit
by finding that the path to the dir that admin.cgi (this script) is in, is: <font color=red>$current_path</FONT>,
but of course the path you need to or want to use may be different.
<BR><BR>

<b>Step 4</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
The first path we need is the path to where you want the data for The Comish to be stored. This dir should not
be readable from the web. Either below your root is good, or in a cgi-bin where text files can not be accessed by the browser.
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Data Path:</b>
</TD>
<TD><input type="Text" name="setup_path" value="$path" size="40"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<BR><BR>
<b>Step 5</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Now we need the virtual path to where your billing script is. Remember, virtual path. 
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Billing Script Path:</b>
</TD>
<TD><input type="Text" name="setup_freepath" value="$comish_path" size="40"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you are done, <input type="Submit" name="setup_6" value="Proceed to Step 6">
</FORM>
</TD></TR></TABLE> 

EOF
&Footer;
exit;

}

sub setup_6 {

$password = $INPUT{'password'};
if ($admin_password) {
	&checkpassword;
}

$error=0;
$error_path = 0;
$error_freepath = 0;

unless ($INPUT{'setup_path'}) {
	$error=1;
	$error_path = "You must enter a path to your data dir";
}
unless ($INPUT{'setup_freepath'}) {
	$error=1;
	$error_freepath = "You must enter a path to where your billing program is located";
}

unless ( $error_path || (-e "$INPUT{'setup_path'}")) {
	$error =1;
	$error_path = "The path you entered does not correspond to an actually dir that exsists";
}

#unless ( $error_freepath || (-e "$INPUT{'setup_freepath'}")) {
#	$error =1;
#	$error_freepath = "The path you entered does not correspond to an actually dir that exsists";
#}

unless ( $error_path) {
	open (DAT,">$INPUT{'setup_path'}/test.html") || ($error_path = "$!");
	print DAT "This is just a test, you can delete me";
	close (DAT);	
	unlink("$INPUT{'setup_path'}/test.html");
}

#unless ( $error_freepath) {
#
#	open (DAT,">$INPUT{'setup_freepath'}/test.html") || ($error_freepath = "$!");
#	print DAT "This is just a test, you can delete me";
#	close (DAT);
#	unlink("$INPUT{'setup_freepath'}/test.html");
#}

if ($error_path || $error_freepath) {
	$error = 1;
}

if ($error) {
print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD colspan=2 >
<font face=arial size=-1>
<center><B>The Comish  Setup, Steps 4 & 5</B></center>
EOF
if ($error_path) {
print <<EOF;
<BR><BR>
<b>Step 4</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
There was an error in the path you want the data stored:
<font color="Red">$error_path</font>, if the error was <font color="Red">Pemission Denied</font>
then you must make the dir readable and writable by chmoding it 777. Please do this now.
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Data Path:</b>
</TD>
<TD><input type="Text" name="setup_path" value="$INPUT{'setup_path'}" size="40"></TD>
EOF
}
else {
print <<EOF;
</TD></TR>
<TD>
</TD>
<TD><input type="HIDDEN" name="setup_path" value="$INPUT{'setup_path'}" size="40"></TD>
EOF
}

print <<EOF;
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>

EOF
if ($error_freepath) {
print <<EOF;
<BR><BR>
<b>Step 5</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
There was an error in the path you want the The Comish rankings to be:
<font color="Red">$error_freepath</font>, if the error was <font color="Red">Pemission Denied</font>
then you must make the dir readable and writable by chmoding it 777. Please do this now.
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>The Comish Path:</b>
</TD>
<TD><input type="Text" name="setup_freepath" value="$INPUT{'setup_freepath'}" size="40"></TD>
EOF
}
else {
print <<EOF;
</TD></TR>
<TD>
</TD>
<TD><input type="HIDDEN" name="setup_freepath" value="$INPUT{'setup_freepath'}" size="40"></TD>
EOF
}
print <<EOF;
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you are done, <input type="Submit" name="setup_6" value="Proceed to Step 6">
</FORM>
</TD></TR></TABLE> 

EOF
&Footer;
exit;
}
else {
	open (VARIABLES, "variables.pl");
	@variables = <VARIABLES>;
	close (VARIABLES);
	
	unless($demo) {
		$variables[2] = "\$path \= \"$INPUT{'setup_path'}\"\;\n";
		$variables[3] = "\$comish_path \= \"$INPUT{'setup_freepath'}\"\;\n";
	}
	$varilength = push(@variables);
	$varilength = $varilength-1;
	chomp($variables[$varilength]);
	unless ($variables[$varilength] =~ /^1/) {
		$varilength++;
		$variables[$varilength] = "\n1\;";
	}
	open (VARIABLES, ">variables.pl");
	print VARIABLES @variables;
	close (VARIABLES);

	unless (-e "$INPUT{'setup_path'}/data") {
		mkdir("$INPUT{'setup_path'}/data", 0777) || &error("could not create dir: $INPUT{'setup_path'}/data");
	}

	
	$newpassword = crypt($INPUT{'password'}, tw);

	open (VARIABLES, ">$INPUT{'setup_path'}/data/password.txt") || &error("Error writing password.txt");
	print VARIABLES "$newpassword";
	close (VARIABLES);	

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD colspan=2 >
<font face=arial size=-1>
<center><B>The Comish  Setup, Steps 6 & 7</B></center>
<BR><BR>
Now we need to set a few urls. This is the spot where they need to start with http://. All urls should point to a directory.
<BR><BR>

<b>Step 6</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
The first url is the url to where your cgi files are, the url to the directory where this file is.</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Url to cgi directory:</b>
</TD>
<TD><input type="Text" name="setup_url_to_cgi" value="$url_to_cgi" size="40"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<BR><BR>
<b>Step 7</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
The next url we need is the url to where you want click thrus from members to be redirected, usually your front page. 
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Redirect Url:</b>
</TD>
<TD><input type="Text" name="setup_url" value="$url" size="40"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you are done, <input type="Submit" name="setup_9" value="Proceed to Step 8">
</FORM>
</TD></TR></TABLE> 

EOF
&Footer;
exit;

}

}

########## SETUP 9 ##########
sub setup_9 {

$password = $INPUT{'password'};
&checkpassword;


$error=0;
$error_url_to_cgi=0;
$error_url = 0;

$INPUT{'setup_url_to_cgi'} =~ s/\/$//;
$INPUT{'setup_url'} =~ s/\/$//;

unless ($INPUT{'setup_url_to_cgi'}) {
	$error=1;
	$error_url_to_cgi = "You must enter a url for to where your cgi files are.";
}
unless ($INPUT{'setup_url'}) {
	$error=1;
	$error_url_to_icons = "You must enter a url for to where to redirect clicks thrus.";
}
if ($error) {

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD colspan=2 >
<font face=arial size=-1>
<center><B>The Comish  Setup, Steps 6 & 7</B></center>

EOF
if ($error_url_to_cgi) {
print <<EOF;
<br><br>
<b>Step 6</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
$error_url_to_cgi.</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Url to cgi directory:</b>
</TD>
<TD><input type="Text" name="setup_url_to_cgi" value="$INPUT{'setup_url_to_cgi'}" size="40"></TD>
EOF
}
else {
print <<EOF;
</TD></TR>
<TD>
</TD>
<TD><input type="HIDDEN" name="setup_url_to_cgi" value="$INPUT{'setup_url_to_cgi'}" size="40"></TD>
EOF

}
print <<EOF;
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
EOF
if ($error_url) {
print <<EOF;
<BR><BR>
<b>Step 7</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
The next url we need is the url to where you want click thrus from members to be redirected, usually your front page. 
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Redirect Url:</b>
</TD>
<TD><input type="Text" name="setup_url" value="$INPUT{'setup_url'}" size="40"></TD>
EOF
}
else {
print <<EOF; 
</TD></TR>
<TD>
</TD>
<TD><input type="HIDDEN" name="setup_url" value="$INPUT{'setup_url'}" size="40"></TD>
EOF
}
print <<EOF;

</TR><TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you are done, <input type="Submit" name="setup_9" value="Proceed to Step 8">
</FORM>
</TD></TR></TABLE> 

EOF
&Footer;
exit;
}
else {
	open (VARIABLES, "variables.pl");
	@variables = <VARIABLES>;
	close (VARIABLES);

	unless($demo) {
		$variables[4] = "\$url \= \"$INPUT{'setup_url'}\"\;\n";
		$variables[5] = "\$url_to_cgi \= \"$INPUT{'setup_url_to_cgi'}\"\;\n";
	}	
	$varilength = push(@variables);
	$varilength = $varilength-1;
	chomp($variables[$varilength]);
	unless ($variables[$varilength] =~ /^1/) {
		$varilength++;
		$variables[$varilength] = "\n1\;";
	}
	open (VARIABLES, ">variables.pl");
	print VARIABLES @variables;
	close (VARIABLES);

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD colspan=2 >
<font face=arial size=-1>
<center><B>The Comish Pro Setup, Step 8</B></center>
<BR><BR>
And finially step 8, the last and most trickest for some. The path to your sendmail program or smtp account. This is needed
to send out all emails to your accounts. Sendmail is usually available only on unix systems, if your on NT you would want to use smtp.
<b>Fill out only one text box, the one you plan on using.</B>
<BR><BR><b>Step 8</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
The path to sendmail is not the same on all servers, you may have to ask your server admin ir check other cgi files
that might already use it, or look in the dirs. /usr/sbin,  /usr/lib, or /usr/bin for a program called sendmail
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Path to sendmail:</b>
</TD>
<TD><input type="Text" name="setup_mail_prog" value="$mail_prog" size="40"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
The STMP Server is your main mail server you use with your isp. The same one you put in Microsoft Outlook or Netscape Mail to send out email.
Something like <font color="Red">mail.earthlink.net</font>. Ask your isp or network admin if you are not sure.
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>SMTP Server:</b>
</TD>
<TD><input type="Text" name="setup_smtp" value="$smtp" size="40"></TD>
</TR>


<TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you are done, you are done... <input type="Submit" name="setup_10" value="Proceed to the main admin screen">
</FORM>
</TD></TR></TABLE> 

EOF
&Footer;
exit;
}

}

########## FINAL SETUP CHECK ##########
sub setup_10 {

$password = $INPUT{'password'};
&checkpassword;


$error=0;
$error_url = 0;

unless ($INPUT{'setup_mail_prog'}) {
	unless ($INPUT{'setup_smtp'}) {
		$error=1;
		$error_url = "You must enter a path for either sendmail or your smtp server.";
	}
}
if ($INPUT{'setup_mail_prog'} && $INPUT{'setup_smtp'}) {
	$error=1;
	$error_url = "You must enter a path for either sendmail or your smtp server, not both.";
}


if ($error){
print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =8 border=0 width=85%>
<TR><TD colspan=2 >
<font face=arial size=-1>
<center><B>The Comish Pro Setup, Step 8</B></center>
<BR><BR>
There was an error in the last form you filled out:<BR><font color="Red">$error_url</font><BR><BR>
And finially step 8, the last and most trickest for some. The path to your sendmail program or smtp account. This is needed
to send out all emails to your accounts. Sendmail is usually available only on unix systems, if your on NT you would want to use smtp.
<b>Fill out only one text box, the one you plan on using.</B>
<BR><BR><b>Step 8</b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
The path to sendmail is not the same on all servers, you may have to ask your server admin ir check other cgi files
that might already use it, or look in the dirs. /usr/sbin,  /usr/lib, or /usr/bin for a program called sendmail
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>Path to sendmail:</b>
</TD>
<TD><input type="Text" name="setup_mail_prog" value="$INPUT{'setup_mail_prog'}" size="40" maxlength="50"></TD>
</TR>
<TR><TD colspan=2 >
<font face=arial size=-1>
<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<font color=red><B>NOTE: SMTP function is not yet implemented. If do not have sendmail on your server, enter anything in the SMTP box, and no mail will be sent out</B></FONT><BR><BR>
The SMTP Server is your main mail server you use with your isp. The same one you put in Microsoft Outlook or Netscape Mail to send out email.
Something like <font color="Red">mail.earthlink.net</font>. Ask your isp or network admin if you are not sure.
</TD></TR>
<TD bgcolor = #e4e4e4><font face=arial size=-1><b>SMTP Server:</b>
</TD>
<TD><input type="Text" name="setup_smtp" value="$INPUT{'setup_smtp'}" size="40" maxlength="50"></TD>
</TR>


<TR><TD colspan=2 >
<font face=arial size=-1>
<br><br>When you are done, you are done... <input type="Submit" name="setup_10" value="Proceed to the main admin screen">
</FORM>
</TD></TR></TABLE> 

EOF
&Footer;
exit;


}
else {
	open (VARIABLES, "variables.pl");
	@variables = <VARIABLES>;
	close (VARIABLES);
	unless($demo) {
		$variables[8] = "\$mail_prog \= \"$INPUT{'setup_mail_prog'}\"\;\n";
		$variables[7] = "\$smtp \= \"$INPUT{'setup_smtp'}\"\;\n";
	}	
	$varilength = push(@variables);
	$varilength = $varilength-1;
	chomp($variables[$varilength]);
	unless ($variables[$varilength] =~ /^1/) {
		$varilength++;
		$variables[$varilength] = "\n1\;";
	}
	open (VARIABLES, ">variables.pl");
	print VARIABLES @variables;
	close (VARIABLES);

&main;
}
}

########## FEATURES EDIT ##########
sub features {
$password = $INPUT{'password'};
&checkpassword;

if ($INPUT{'features_edit'} =~ /The Comish Setup/i) {
	&setup_2;
}
elsif ($INPUT{'features_edit'} =~ /General Operations/i) {

print <<EOF;
<font face=arial size=-1>
<center><B>General Comish Operations</B>
<BR><BR>
<BLOCKQUOTE>
For more information on what each item is and does, click on the help icon</BLOCKQUOTE>
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =4 border=0>
<TR><TD>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#1"  target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif"  align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Manually confirm each account before they become a member:
</TD><TD>
<font face=arial size=-1><input type="Checkbox" name="confirm_acc"
EOF
	if ($confirm_acc) {
		print " CHECKED ";
	}
print <<EOF;
>&nbsp;&nbsp;&nbsp;Yes
</TD></TR>
<TR><TD>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#2"  target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif"  align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Manually confirm each transaction before it is recorded:
</TD><TD>
<font face=arial size=-1><input type="Checkbox" name="confirm_trans"
EOF
	if ($confirm_trans) {
		print " CHECKED ";
	}
print <<EOF;
>&nbsp;&nbsp;&nbsp;Yes
</TD></TR>


<TR><TD>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#3"  target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif"  align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>In seconds the time to hold an ip address in the ip log:
</TD><TD>
<input type="Text" name="ip" value="$ip" size=10>
</TD></TR>
<TR><TD>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#4"  target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif"  align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Send email to refering account when transaction is finalized:
</TD><TD>
<font face=arial size=-1><input type="Checkbox" name="mail_purch"
EOF
	if ($mail_purch) {
		print " CHECKED ";
	}
print <<EOF;
>&nbsp;&nbsp;&nbsp;Yes
</TD></TR>
<TR><TD>
<a href="http://solutionscripts.com/man/topsitespro/1.1.2.shtml#5"  target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif"  align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Make user agree to terms and conditions on signup:
</TD><TD>
<font face=arial size=-1><input type="Checkbox" name="terms"
EOF
	if ($terms) {
		print " CHECKED ";
	}
print <<EOF;
>&nbsp;&nbsp;&nbsp;Yes
</TD></TR>

<TR><TD>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#6"  target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif"  align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Show Solution Scripts credit footer:
</TD><TD>
<font face=arial size=-1><input type="Checkbox" name="credit"
EOF
	if ($credit) {
		print " CHECKED ";
	}
print <<EOF;
>&nbsp;&nbsp;&nbsp;Yes
</TD></TR>
<TR><TD>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#7"  target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif"  align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Use file locking (flock):
</TD><TD>
<font face=arial size=-1><input type="Checkbox" name="flock"
EOF
	if ($flock) {
		print " CHECKED ";
	}
print <<EOF;
>&nbsp;&nbsp;&nbsp;Yes
</TD></TR>

<TR><TD colspan=2><br><br>
<font face=arial size=-1><B><center><a href="http://solutionscripts.com/man/comish/1.1.2.shtml#color"  target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif"  align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
The Comish Color Scheme</center></B></FONT><br><br>
</TD></TR>
<TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#8" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Background color:
</TD><TD  bgcolor=$over_bg>
<input type="Text" name="over_bg" value="$over_bg" size=10>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#9" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Table Background color:
</TD><TD bgcolor=$table_bg>
<input type="Text" name="table_bg" value="$table_bg" size=10>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#10" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Table Header Background color:
</TD><TD bgcolor=$table_head_bg>
<input type="Text" name="table_head_bg" value="$table_head_bg" size=10>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#11" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Text color:
</TD><TD bgcolor=$text_color>
<input type="Text" name="text_color" value="$text_color" size=10>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#12" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Link color:
</TD><TD bgcolor=$link_color>
<input type="Text" name="link_color" value="$link_color" size=10>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#13" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Color of text in table:
</TD><TD bgcolor=$text_table>
<input type="Text" name="text_table" value="$text_table" size=10>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#14" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Color of text in header of table:
</TD><TD bgcolor=$text_table_head>
<input type="Text" name="text_table_head" value="$text_table_head" size=10>
</TD></TR>

<TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#15" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Color of highlighted text in header of table:
</TD><TD bgcolor=$text_highlight>
<input type="Text" name="text_highlight" value="$text_highlight" size=10>
</TD></TR>

<TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#16" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>First Alternating color:
</TD><TD bgcolor=$alt1>
<input type="Text" name="alt1" value="$alt1" size=10>
</TD></TR>

<TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#17" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Second Alternating color:
</TD><TD bgcolor=$alt2>
<input type="Text" name="alt2" value="$alt2" size=10>
</TD></TR>

<TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#18" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Font:&nbsp;&nbsp;</FONT><font face=$font_face>Current Font</font>
</TD><TD>
<input type="Text" name="font_face" value="$font_face" size=10>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml#19" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Font Size:&nbsp;&nbsp;</FONT><font size=$font_size>Current Font Size</font>
</TD><TD>
<input type="Text" name="font_size" value="$font_size" size=4>
</TD></TR>

<TR><TD colspan=2><br><br><CENTER>
<input type="Submit" name="features_gen" value=" Store these values ">
</TR></TD>
</TABLE>
</FORM>
EOF
&Bottom;
exit;
}
elsif ($INPUT{'features_edit'} =~ /Commission Tiers/i) {
print <<EOF;
<font face=arial size=-1>
<center><B>Commission Tiers</B>
<BR><BR>
<BLOCKQUOTE>
For more information on what 'Commission Tiers' are and what they do, visit The Comish <a href="http://solutionscripts.com/man/comish/1.1.3.shtml" target="main"><font color=blue>online manual</FONT></A></BLOCKQUOTE>
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$password">
<table cellspacing =0 cellpadding =4 border=0>
<TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#1" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 1 Dollars:
</TD><TD>
<input type="Text" name="t1d" value="$t1d" size=5>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#2" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 1 Percent:
</TD><TD>
<input type="Text" name="t1p" value="$t1p" size=5>
<font face=arial size=-1>\%
</TD></TR>

<TR><TD valign=center>
<BR><BR>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#1" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 2 Dollars:
</TD><TD>
<BR><BR><input type="Text" name="t2d" value="$t2d" size=5>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#2" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 2 Percent:
</TD><TD>
<input type="Text" name="t2p" value="$t2p" size=5>
<font face=arial size=-1>\%
</TD></TR>

<TR><TD valign=center>
<BR><BR>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#1" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 3 Dollars:
</TD><TD>
<BR><BR><input type="Text" name="t3d" value="$t3d" size=5>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#2" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 3 Percent:
</TD><TD>
<input type="Text" name="t3p" value="$t3p" size=5>
<font face=arial size=-1>\%
</TD></TR>

<TR><TD valign=center>
<BR><BR>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#1" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 4 Dollars:
</TD><TD>
<BR><BR><input type="Text" name="t4d" value="$t4d" size=5>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#2" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 4 Percent:
</TD><TD>
<input type="Text" name="t4p" value="$t4p" size=5>
<font face=arial size=-1>\%
</TD></TR>

<TR><TD valign=center>
<BR><BR>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#1" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 5 Dollars:
</TD><TD>
<BR><BR><input type="Text" name="t5d" value="$t5d" size=5>
</TD></TR><TR><TD valign=center>
<a href="http://solutionscripts.com/man/comish/1.1.3.shtml#2" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Tier 5 Percent:
</TD><TD>
<input type="Text" name="t5p" value="$t5p" size=5>
<font face=arial size=-1>\%
</TD></TR>

<TR><TD colspan=2><br><br><CENTER>
<input type="Submit" name="features_tiers" value=" Store these values ">
</TR></TD>
</TABLE>
</FORM>
EOF
&Bottom;
exit;
}

}

########## STORE GENERAL OPERATIONS VALUES ##########
sub features_gen {
if ($demo) {&demo;}

$password=$INPUT{'password'};
&checkpassword;

	open (VARIABLES, "variables.pl");
	@variables = <VARIABLES>;
	close (VARIABLES);

	$variables[10] = "\$confirm_acc \= \"$INPUT{'confirm_acc'}\"\;\n";
	$variables[11] = "\$confirm_trans \= \"$INPUT{'confirm_trans'}\"\;\n";
	$variables[12] = "\$ip \= \"$INPUT{'ip'}\"\;\n";	
	$variables[13] = "\$mail_purch \= \"$INPUT{'mail_purch'}\"\;\n";	
	$variables[14] = "\$terms \= \"$INPUT{'terms'}\"\;\n";	
	$variables[15] = "\$credit \= \"$INPUT{'credit'}\"\;\n";	
	$variables[16] = "\$flock \= \"$INPUT{'flock'}\"\;\n";	
	
	$variables[17] = "\$over_bg \= \"$INPUT{'over_bg'}\"\;\n";
	$variables[18] = "\$table_bg \= \"$INPUT{'table_bg'}\"\;\n";
	$variables[19] = "\$table_head_bg \= \"$INPUT{'table_head_bg'}\"\;\n";
	$variables[20] = "\$text_color \= \"$INPUT{'text_color'}\"\;\n";
	$variables[21] = "\$link_color \= \"$INPUT{'link_color'}\"\;\n";
	$variables[22] = "\$text_table \= \"$INPUT{'text_table'}\"\;\n";
	$variables[23] = "\$text_table_head \= \"$INPUT{'text_table_head'}\"\;\n";
	$variables[24] = "\$text_highlight \= \"$INPUT{'text_highlight'}\"\;\n";
	$variables[25] = "\$alt1 \= \"$INPUT{'alt1'}\"\;\n";
	$variables[26] = "\$alt2 \= \"$INPUT{'alt2'}\"\;\n";		
	$variables[27] = "\$font_face \= \"$INPUT{'font_face'}\"\;\n";
	$variables[28] = "\$font_size \= \"$INPUT{'font_size'}\"\;\n";

				
	$varilength = push(@variables);
	$varilength = $varilength-1;
	chomp($variables[$varilength]);
	unless ($variables[$varilength] =~ /^1/) {
		$varilength++;
		$variables[$varilength] = "\n1\;";
	}
	open (VARIABLES, ">variables.pl");
	print VARIABLES @variables;
	close (VARIABLES);
&main("General setup updated");
exit;
}

########## STORE COMMISSION TIERS VALUES ##########
sub features_tiers {
if ($demo) {&demo;}

$password=$INPUT{'password'};
&checkpassword;

	open (VARIABLES, "variables.pl");
	@variables = <VARIABLES>;
	close (VARIABLES);

	$variables[29] = "\$t1d \= \"$INPUT{'t1d'}\"\;\n";
	$variables[30] = "\$t1p \= \"$INPUT{'t1p'}\"\;\n";
	$variables[31] = "\$t2d \= \"$INPUT{'t2d'}\"\;\n";	
	$variables[32] = "\$t2p \= \"$INPUT{'t2p'}\"\;\n";	
	$variables[33] = "\$t3d \= \"$INPUT{'t3d'}\"\;\n";	
	$variables[34] = "\$t3p \= \"$INPUT{'t3p'}\"\;\n";	
	$variables[35] = "\$t4d \= \"$INPUT{'t4d'}\"\;\n";	
	$variables[36] = "\$t4p \= \"$INPUT{'t4p'}\"\;\n";	
	$variables[37] = "\$t5d \= \"$INPUT{'t5d'}\"\;\n";
	$variables[38] = "\$t5p \= \"$INPUT{'t5p'}\"\;\n";
				
	$varilength = push(@variables);
	$varilength = $varilength-1;
	chomp($variables[$varilength]);
	unless ($variables[$varilength] =~ /^1/) {
		$varilength++;
		$variables[$varilength] = "\n1\;";
	}
	open (VARIABLES, ">variables.pl");
	print VARIABLES @variables;
	close (VARIABLES);
&main("Commission Tiers updated");
exit;
}


##########START MAIN LOOP ##########
sub admin {

unless ($comish_name && $path && $comish_path && $url && $url_to_cgi) {
	&setup;
}
&setup_paths;
&do_recurring;

unless ($no_stats) {

eval { 
use Config;
};

opendir (DIR, "$path/data/accounts");
@file = grep { /.txt/ } readdir(DIR);
close (DIR);

$numaccounts = push(@file);

open (DAT,"<$path/data/total.txt");
if ($flock) {
	flock DAT, 2; 
}
@total = <DAT>;
close (DAT);

$stat_img = "\n<a href=\"http://solutionscripts.com/news/comish\"><IMG SRC=\"http://solutionscripts.com/cgi-bin/stats/comish.cgi?$numaccounts||$total[0]||$total[1]||$]||$ENV{'SERVER_SOFTWARE'}||$ENV{'GATEWAY_INTERFACE'}||$Config{'osname'}||$Config{'osvers'}\" border=0></A>\n\n";

}

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =10>
<TR bgcolor=#E4E4E4 align=center><TD>
<FONT FACE="Arial"><B>$comish_name</B></FONT><BR><BR>
<INPUT TYPE="PASSWORD" NAME="password">
<INPUT TYPE="SUBMIT" NAME="main" VALUE="Enter">
</TD></TR></TABLE>
<BR><BR>
</form>

<BR>
<table border=0 cellspacing=0 cellpadding=15 bgcolor=SlateBlue><TR align=center><TD colspan=2><font face=arial size=-1 color=white><b>The Comish Version Info</b></TD></TR>
<TR align=center><TD><font face=arial size=-1 color=white><b>Newest Version</b></TD>
<TD><font face=arial size=-1 color=white><b>Your Current Version</b></TD>
</TR>
<TR align=center>
<TD>
<font color=white>
<img src="http://solutionscripts.com/lounge/digits/comish_1.gif" width=15 height=20 border=0><B>.</B>
<img src="http://solutionscripts.com/lounge/digits/comish_2.gif" width=15 height=20 border=0><img src="http://solutionscripts.com/lounge/digits/comish_3.gif" width=15 height=20 border=0>

</TD><TD>
<font color=white>
<img src="http://solutionscripts.com/lounge/digits/$v1.gif" width=15 height=20 border=0><B>.</B>
<img src="http://solutionscripts.com/lounge/digits/$v2.gif" width=15 height=20 border=0><img src="http://solutionscripts.com/lounge/digits/$v3.gif" width=15 height=20 border=0>
</TD></TR><TR align=center><TD colspan=2><font face=arial size=-1 color=white>Log into <a href="http://solutionscripts.com/lounge/comish">The Comish Members Lounge</a> to download the newest version
<BR><BR>
$stat_img</TD></TR>
</TABLE><BR>

EOF

&Footer;
exit;
}


sub main {
&checkpassword;

$message = $_[0];

opendir (DIR, "$path/data/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

@files = sort(@files);
$numaccounts = push(@files);

opendir (DIR, "$path/data/temp");
@file = grep { /.txt/ } readdir(DIR);
close (DIR); 
$numtemp = push(@file);

open(LIST,"$path/data/awaiting.txt");
if ($flock) {
flock LIST,2;
}
@awaiting = <LIST>;
close(LIST);
$num_await = push(@awaiting);

open(LIST,"$path/data/total.txt");
if ($flock) {
flock LIST,2;
}
@total = <LIST>;
close(LIST);

open(LIST,"$path/data/paid.txt");
if ($flock) {
flock LIST,2;
}
$tpaid = <LIST>;
close(LIST);

$backup=0;

open(LIST,"$path/data_backup/backup.txt");
if ($flock) {
flock LIST,2;
}
$backup = <LIST>;
close(LIST);

unless ($backup) {
	$backup = "Never";
}

$start1 = $time;
unless ( -e "$path/data/start.txt" ) { 
	open( DISPLAY, ">$path/data/start.txt");
	if ($flock) {
		flock DISPLAY, 2;
	}
	print DISPLAY $time;
	close DISPLAY;
}
open(LIST,"$path/data/start.txt");
if ($flock) {
	flock LIST,2;
}
$start1 = <LIST>;
close(LIST);

$start =0;

foreach $line(@files) {
	open(LIST,"$path/data/accounts/$line");
	if ($flock) {
		flock LIST,2;
	}
	@acc = <LIST>;
	close(LIST);
	unless ($start) {
		$start = $acc[9];
	}
	else {
		if ($acc[9] < $start) {
			$start = $acc[9];
		}
	}
	$tot = $acc[13] - $acc[14];
}
unless ($start) {
	$start = $time;
}
if ($start < $start1) {
	open( DISPLAY, ">$path/data/start.txt");
	if ($flock) {
		flock DISPLAY, 2;
	}
	print DISPLAY $start;
	close DISPLAY;
}
else {
	$start = $start1;
}


open (PASSWORD, "$path/data/daily.txt");
if ($flock) {
flock PASSWORD,2;
}
@daily = <PASSWORD>;
close (PASSWORD);

@dailys = grep { /$mon\..*\.$year/ } @daily;

$month_day =0;
$month_ref =0;
$month_purch =0;
$month_in =0;

foreach $entry(@dailys) {
@out = split(/\|/,$entry);

$month_day +=$out[1];
$month_ref +=$out[2];
$month_purch +=$out[3];

}


open (PASSWORD, "$path/data/purchase.txt");
if ($flock) {
	flock PASSWORD,2;
}
@daily = <PASSWORD>;
close (PASSWORD);

@dailys = grep { /$mon\..*\.$year/ } @daily;

foreach $entry(@dailys) {
	@out = split(/\|/,$entry);
	$month_in +=$out[4];
}

open (PASSWORD, "$path/data/paidlog.txt");
if ($flock) {
	flock PASSWORD,2;
}
@paid = <PASSWORD>;
close (PASSWORD);

@paids = grep { /$mon\..*\.$year/ } @paid;
$month_paid =0;

foreach $entry(@paids) {
	@out = split(/\|/,$entry);
	$month_paid +=$out[2];
}


open(LIST,"$path/data/reoccur.txt");
if ($flock) {
flock LIST,2;
}
@reo = <LIST>;
close(LIST);

if ($message){
$message = "<font size=2 face=arial color=black>" . $message . "</FONT><BR><BR>";
}

print <<EOF;

$message

<form action="$cgiurl" method=post>
<input type="Hidden" name="password" value="$INPUT{'password'}">

<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =6>
<TR bgcolor=white align=center>
<TD align=left><FONT SIZE="-1" FACE="Arial">Catagory</TD>
<TD><FONT SIZE="-2" FACE="Arial">Total</TD>
<TD><FONT SIZE="-2" FACE="Arial">This Month</TD>
</TR>
<TR bgcolor=#E4E4E4 align=center>
<TD align=left><FONT SIZE="-1" FACE="Arial">Click thrus:</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">$total[0]</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">$month_day</TD>
</TR><TR bgcolor=#E4E4E4 align=center>
<TD align=left><FONT SIZE="-1" FACE="Arial">Purchases:</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">$total[1]</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">$month_ref</TD>
</TR><TR bgcolor=#E4E4E4 align=center>
<TD align=left><FONT SIZE="-1" FACE="Arial">Total \$\$ brought in:</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">\$$total[2]</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">\$$month_in</TD>
</TR><TR bgcolor=#E4E4E4 align=center>
<TD align=left><FONT SIZE="-1" FACE="Arial">\$\$ paid in commission:</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">\$$total[3]</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">\$$month_purch</TD>
</TR><TR bgcolor=#E4E4E4 align=center>
<TD align=left><FONT SIZE="-1" FACE="Arial">Total \$ paid out</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">\$$tpaid</TD>
<TD><FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">\$$month_paid</TD>
</TR>

</TABLE><BR><BR>

<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =6>
<tr bgcolor=#E4E4E4><td>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#1" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1>Operations and Features edit:</FONT>
&nbsp;&nbsp;&nbsp;<select name="features_edit">

	<option value="The Comish Setup">The Comish Setup
	<option value="General Operations">General Operations
	<option value="Commission Tiers">Commission Tiers
	</select>&nbsp;&nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" NAME="features" VALUE=" Edit ">
	</td></tr>
EOF
if ($confirm_trans) {
print <<EOF;
<TR bgcolor=#E4E4E4 >
<TD align=left>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#2" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">You have
<FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">$num_await</FONT> purchases awaiting approval&nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" NAME="approve" VALUE="Approve purchases"></TD>
</TR>
EOF
}
if ($confirm_acc) {
print <<EOF;
<TR bgcolor=#E4E4E4 >
<TD align=left>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#3" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">You have
<FONT SIZE="-1" FACE="Arial" COLOR="#FF0000">$numtemp</FONT> new accounts awaiting approval&nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" NAME="confirm" VALUE="Confirm Accounts"></TD>
</TR>
EOF

}

print <<EOF;
<TR bgcolor=#E4E4E4><TD>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#4" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">
Pay accounts owed more than \$</FONT><input type="Text" name="pay_owe" value="50" size="3">
&nbsp;&nbsp;&nbsp;
<input type="Submit" name="pay" value="Pay Accounts">
</TD></TR>
<TR bgcolor=#E4E4E4><TD>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#5" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">View Daily Stats for <select name="month_daily">
   <option value="all">All months
   <option value="all">---------------------------
EOF

&select_months($start);

print <<EOF;
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="SUBMIT" NAME="viewdaily" VALUE="Daily Stats">
</TD></TR>

<TR bgcolor=#E4E4E4><TD>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#6" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">View/Edit Purchase Log for <select name="month_purch">
   <option value="all">All months
   <option value="all">---------------------------
EOF

&select_months($start);

print <<EOF;
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="SUBMIT" NAME="purch" VALUE="Purchase Log">
</TD></TR>

<TR bgcolor=#E4E4E4><TD>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#7" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">View/Edit Payment Log for <select name="month_pay">
   <option value="all">All months
   <option value="all">---------------------------
EOF

&select_months($start);

print <<EOF;
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="SUBMIT" NAME="paylog" VALUE="Payment Log">
</TD></TR>


<TR bgcolor=#E4E4E4><TD>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#8" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">Of <font color=red><b>$numaccounts</b></FONT> accounts, view by letter <select name="view_letter">
   <option value="all">All accounts
   <option value="all">---------------------------
   <option value="A">A
   <option value="B">B
   <option value="C">C
   <option value="D">D
   <option value="E">E
   <option value="F">F
   <option value="G">G
   <option value="H">H
   <option value="I">I
   <option value="J">J
   <option value="K">K
   <option value="L">L
   <option value="M">M
   <option value="N">N
   <option value="O">O
   <option value="P">P
   <option value="Q">Q
   <option value="R">R
   <option value="S">S
   <option value="T">T
   <option value="U">U
   <option value="V">V
   <option value="W">W
   <option value="X">X
   <option value="Y">Y
   <option value="Z">Z

</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="SUBMIT" NAME="view" VALUE="Browse Accounts">
</TD></TR>
<TR bgcolor=#E4E4E4 >
<TD align=left>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#9" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">Enter purchases manually$num_&nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" NAME="new_purchase" VALUE="New Purchase"></TD>
</TR>
EOF

$num_reo = push(@reo);
if (@reo) { 
print <<EOF;
<TR bgcolor=#E4E4E4 >
<TD align=left>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#10" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">Currently <font color=red>$num_reo</FONT> recurring transactions active$num_&nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" NAME="view_reoccur" VALUE="View recurring Trans"></TD>
</TR>
EOF
}

print <<EOF;
<TR bgcolor=#E4E4E4 >
<TD align=left>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#11" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">Data Back up -- Last backed up <font color="Red">$backup</font> &nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" NAME="backup" VALUE="Back Up">&nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" NAME="restore" VALUE="Restore"></TD>
</TR>
<TR bgcolor=#E4E4E4 >
<TD align=left>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#12" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial">Send email to all accounts &nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" NAME="sendemail" VALUE="Send Email"></TD>
</TR>

</TABLE><BR><BR>

<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =3>
<TR bgcolor=#E4E4E4 align=center>
<TD>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#13" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<FONT SIZE="-1" FACE="Arial"><B>Select Account</B></FONT></TD></TR>
<TR bgcolor=white align=center>
<TD><SELECT NAME="function">

EOF

foreach $linn(@files) {
$accname = $linn;
$accname =~ s/\.txt//;

print <<EOF;
<option value="$accname">$accname
EOF
}

print <<EOF;
</SELECT>
</TD></TR>
<TR bgcolor=white align=center>
<TD>
<INPUT TYPE="SUBMIT" NAME="acc_stats" VALUE="Show Stats / Edit Info">
&nbsp;
<INPUT TYPE="SUBMIT" NAME="acc_del" VALUE="Delete">
</TD></TR>
</TABLE><BR><BR>
<table cellpadding=5 border=1 cellspacing=0 bgcolor="white">
<TR bgcolor=silver><TD colspan=2 align=center>
<a href="http://solutionscripts.com/man/comish/1.1.shtml#14" target="main"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>&nbsp;&nbsp;&nbsp;
<font face=arial size=-1><B>Select a file to edit
</TD></TR>
<TR><TD valign=top align=center>
<INPUT TYPE="RADIO" NAME="efile" VALUE="header.txt">
</TD><TD align=left>
<font face=arial size=-1>
header.txt -- The text above all functions in the file manager
</TD></TR>
<TR><TD valign=top align=center>
<INPUT TYPE="RADIO" NAME="efile" VALUE="footer.txt">
</TD><TD align=left>
<font face=arial size=-1>
footer.txt -- The text below all the functions in the file manager
</TD></TR>
<TR><TD valign=top align=center>
<INPUT TYPE="RADIO" NAME="efile" VALUE="welcome_html.txt">
</TD><TD align=left>
<font face=arial size=-1>
welcome_html.txt -- Html shown to new users after they sign-up (Confirm accounts must be off)
</TD></TR>
<TR><TD valign=top align=center>
<INPUT TYPE="RADIO" NAME="efile" VALUE="welcome.txt">
</TD><TD align=left>
<font face=arial size=-1>
welcome.txt -- Text added to the welcome email sent to new members
</TD></TR>
<TR><TD valign=top align=center>
<INPUT TYPE="RADIO" NAME="efile" VALUE="confirm_email.txt">
</TD><TD align=left>
<font face=arial size=-1>
confirm_email.txt -- Text added to email sent to account when purchase is made
</TD></TR>
<TR><TD valign=top align=center>
<INPUT TYPE="RADIO" NAME="efile" VALUE="rules.txt">
</TD><TD align=left>
<font face=arial size=-1>
rules.txt -- Terms and conditions file, make users accept this before they are given an account
</TD></TR>
<TR><TD colspan=2 align=center>
<font face=arial size=-1><B><INPUT TYPE="SUBMIT" NAME="text" VALUE="Edit the selected file">
</TD></TR>
</TABLE>
</form>
<table border=0>
<TR align=left>
<TD>
<font face=arial size=1>Please note: any dates entered must be in the form of:<BR><BR>
<font color=red>mm.dd.yyyy</FONT>
<BR><BR>
Where the month and days do not contain zeros in front of numbers. For example, 4 not 04.<BR>
Years must be in 4 digit numbers and days, months, and year seperated by a period.
</TD></TR></TABLE>
<BR>
EOF

&Bottom;
exit;

}


########## CONFIRM ACCOUNTS ##########
sub confirm {
&checkpassword;

opendir (DIR, "$path/data/temp");
@file = grep { /.txt/ } readdir(DIR);
close (DIR); 
$numtemp = push(@file);
@file = sort(@file);

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =8>
<TR bgcolor=#E4E4E4 align=center><TD><FONT SIZE="-1" FACE="Arial">
Confirming $numtemp accounts <small><BR><I>Select either accept or deny checkbox</I></small>
</font></td></TR></TABLE><BR><BR>
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =3>
<TR bgcolor=#E4E4E4 align=center>
<TD><FONT SIZE="-1" FACE="Arial">Accept</TD>
<TD><FONT SIZE="-1" FACE="Arial">Deny</TD>
<TD><FONT SIZE="-1" FACE="Arial">Account Name</TD>
<TD><FONT SIZE="-1" FACE="Arial">Email</TD>
<TD><FONT SIZE="-1" FACE="Arial">Site Url</TD>
<TD><FONT SIZE="-1" FACE="Arial">Company Name</TD>
<TD><FONT SIZE="-1" FACE="Arial">Contact Name</TD>
</TR>
EOF

foreach $line (@file) {
open(LIST,"$path/data/temp/$line");
if ($flock) {
flock LIST,2;
}
@acc=<LIST>;
close(LIST);
$account_name = $line;
$account_name =~ s/\.txt//;
print <<EOF;
<TR bgcolor=white align=center>
<TD><FONT SIZE="-1" FACE="Arial"><INPUT TYPE="RADIO" NAME="$line" VALUE="accept"></TD>
<TD><FONT SIZE="-1" FACE="Arial"><INPUT TYPE="RADIO" NAME="$line" VALUE="deny"></TD>
<TD><FONT SIZE="-1" FACE="Arial">$account_name</TD>
<TD><FONT SIZE="-1" FACE="Arial"><A HREF="mailto:$acc[0]"><font color=red>$acc[0]</FONT></A></TD>
<TD><FONT SIZE="-1" FACE="Arial"><a href="$acc[1]"><FONT color=red>$acc[1]</FONT></A></TD>
<TD><FONT SIZE="-1" FACE="Arial">$acc[2]</TD>
<TD><FONT SIZE="-1" FACE="Arial">$acc[3]</TD>
</TR>
EOF

}
print <<EOF;

</TABLE><BR><BR>
<input type="Hidden" name="password" value="$INPUT{'password'}">
<INPUT TYPE="SUBMIT" NAME="con_final" VALUE="Submit">
</form>


EOF

&Bottom;
exit;
}

########## CONFIRM FINAL ##########
sub con_final {
&checkpassword;

opendir (DIR, "$path/data/temp");
@file = grep { /.txt/ } readdir(DIR);
close (DIR); 
$numtemp = push(@file);
print <<EOF;

<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =8>
<TR bgcolor=#E4E4E4 align=center><TD><FONT SIZE="-1" FACE="Arial">
Accounts accepted or denied
</font></td></TR>
EOF

foreach $line (@file) {
	$account_name = $line;
	$account_name =~ s/\.txt//;
	if ($INPUT{$line} eq "accept" ) {
print <<EOF;
<TR bgcolor=white align=center>
<TD><FONT SIZE="-1" FACE="Arial">$account_name -- Accepted</TD>
</TR>
EOF
		open(LIST,"$path/data/temp/$line");
		if ($flock) {
			flock LIST,2;
		}
		@add = <LIST>;
		close(LIST);

		unlink("$path/data/temp/$line");

		open(LIST,">$path/data/accounts/$line");
		if ($flock) {
			flock LIST,2;
		}
		print LIST @add;
		close(LIST);

		&email_confirm($add[0],$add[3]);
	}
	elsif ( $INPUT{$line} eq "deny") {

print <<EOF;
<TR bgcolor=white align=center>
<TD><FONT SIZE="-1" FACE="Arial">$account_name -- Denied</TD>
</TR>
EOF

		open(LIST,"$path/data/temp/$line");
		if ($flock) {
			flock LIST,2;
		}
		@add = <LIST>;
		close(LIST);

		unlink("$path/data/temp/$line");

$mail_mes = "We're Sorry, Your request for an account at $comish_name has been denied \n\n";

		&send_email($add[0],$comish_name,$mail_mes);
	}
}

print <<EOF;
</TABLE>
EOF

&Bottom;
exit;

}


########## SHOW DAILY STATS ##########
sub acc_daily {
$account = $INPUT{'account'};
&checkpassword;
$all = 0;
if ($INPUT{'month_daily'} eq 'all') {
	$all = 1;
}
else {
	@dates = split(/\//,$INPUT{'month_daily'});
}

&stats_daily('admin',$account,$all,$dates[0],$dates[1]);

&Bottom;
exit;
}

########## PURCHASE LOG ##########
sub acc_purch {
$account = $INPUT{'account'};
&checkpassword;
$all = 0;
if ($INPUT{'month_purch'} eq 'all') {
	$all = 1;
}
else {
	@dates = split(/\//,$INPUT{'month_purch'});
}

&stats_purchase('admin',$account,$all,$dates[0],$dates[1]);

&Bottom;
exit;
}

########## EDIT / DELETE PURCHASE LOG ##########
sub acc_edit_purch {
&checkpassword;

$account = $INPUT{'account'};
unless ($account) {
	$account = "System";
}

open (PASSWORD, "$path/data/reoccur.txt");
if ($flock){
	flock PASSWORD,2;
}
@reocc = <PASSWORD>;
close (PASSWORD);

if(@reocc) {
	$colspan=7;
}
else { 
	$colspan=6;
}

print  "<FORM METHOD=POST ACTION=\"$cgiurl\"><input type=\"Hidden\" name=\"password\" value=\"$INPUT{'password'}\"><input type=\"Hidden\" name=\"account\" value=\"$account\">";

print <<EOF;
<input type="Hidden" name="purchase_edit" value="$INPUT{'purchase_edit'}">
<table cellspacing =0  border=1 cellpadding =4>
<TR bgcolor=MediumBlue align=center><TD colspan=$colspan><FONT size="-1" FACE="Arial" color=white>Please edit and/or confirm the deletion<BR>of the
following purchases for account $account</FONT>
</TD></TR>
<TR bgcolor=MediumBlue align=center>
EOF
if (@reocc) {
print <<EOF;
<TD><FONT size="-1" FACE="Arial" color=white>Stop<BR>recurring</FONT></TD>
EOF
}
print <<EOF;
<TD><FONT size="-1" FACE="Arial" color=white>Account</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Date*</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Trans</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Total \$</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Commision</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Comments</FONT></TD>
</TR>
EOF

open (PASSWORD, "$path/data/purchase.txt");
if ($flock){
	flock PASSWORD,2;
}
@purch = <PASSWORD>;
close (PASSWORD);

$INPUT{'purchase_edit'} = ',' . $INPUT{'purchase_edit'} . ',';
$INPUT{'purchase_delete'} = ',' . $INPUT{'purchase_delete'} . ',';

@colors=('MediumSlateBlue','SlateBlue');
$color=0;
$color2=0;
$delete = '';

foreach $line(@purch){
	@out = split(/\|/,$line);
	if ($INPUT{'purchase_edit'} =~ /,$out[0],/) {
		unless ($color) {
			$color=1;
		}
		else {
			$color=0;
		}
		print "<TR bgcolor=$colors[$color]>";

	if (@reocc) {
print <<EOF;
<TD align=center><FONT size="2" face="arial" color=white>&nbsp;
EOF
		foreach $reeo(@reocc) {
			@reoo = split(/\|/,$reeo);	
			if($reoo[0] == $out[0]) {
				 if ($reoo[7] eq "Inactive") {
				 	print "Inactive";
				 }
				 else {
				 	print "<input type=\"Checkbox\" name=\"stop_reoccur\" value=\"$out[0]\">";
				}
			}
		}
		print "\&nbsp\;</FONT></TD>\n";			
	}

print <<EOF;
<TD><FONT size="2" FACE="Arial" color=white><input type="TEXT" name="out1_$out[0]" value="$out[1]" size=10></FONT></TD>
<TD><FONT size="2" FACE="Arial" color=white><input type="Text" name="out2_$out[0]" value="$out[2]" size=10></FONT></TD>
<TD><FONT size="2" FACE="Arial" color=white><input type="Text" name="out3_$out[0]" value="$out[3]" size=10></FONT></TD>
<TD><FONT size="2" FACE="Arial" color=white>\$<input type="Text" name="out4_$out[0]" value="$out[4]" size=5></FONT></TD>
<TD><FONT size="2" FACE="Arial" color=white>\$<input type="Text" name="out5_$out[0]" value="$out[5]" size=5></FONT></TD>
<TD><FONT size="2" FACE="Arial" color=white><input type="Text" name="out6_$out[0]" value="$out[6]"></FONT></TD>
</TR>
EOF
		next;
	}
	if($INPUT{'purchase_delete'} =~ /,$out[0],/) {
		unless ($color2) {
			$color2=1;
		}
		else {
			$color2=0;
		}
		$delete .= "<TR bgcolor=$colors[$color2]>";	
		$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white><input type=\"Checkbox\" name=\"purchase_delete\" value=\"$out[0]\" checked></FONT></TD>";
		$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>$out[1]</FONT></TD>";
		$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>$out[2]</FONT></TD>";
		$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>$out[3]</FONT></TD>";
		$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>\$$out[4]</FONT></TD>";
		$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>\$$out[5]</FONT></TD>";
		$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>$out[6]</FONT></TD></TR>";
		next;
	}
}

print "</TABLE><BR><BR>";

if ($delete) {
print <<EOF;
<table cellspacing =0  border=1 cellpadding =4>
<TR bgcolor=MediumBlue align=center><TD colspan=7><FONT size="-1" FACE="Arial" color=white>Uncheck to save, leave checked to delete</FONT>
</TD></TR>
<TR bgcolor=MediumBlue align=center>
<TD><FONT size="-1" FACE="Arial" color=white>Delete</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Account</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Date</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Trans</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Total \$</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Commision</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Comments</FONT></TD>
</TR>
$delete
</TABLE>
<br><br>
EOF
}
print<<EOF;
<input type="Submit" name="acc_edit_purch_final" value="Delete / Edit">
</form><BR>
EOF

&Bottom;
exit;
}


########## FINAL EDIT / DELETE PURCHASE LOG ##########
sub acc_edit_purch_final {
$account = $INPUT{'account'};
&checkpassword;

$INPUT{'purchase_edit'} = ',' . $INPUT{'purchase_edit'} . ',';
$INPUT{'purchase_delete'} = ',' . $INPUT{'purchase_delete'} . ',';
$INPUT{'stop_reoccur'} = ',' . $INPUT{'stop_reoccur'} . ',';

print <<EOF;
<table cellspacing =0  border=1 cellpadding =4>
<TR bgcolor=MediumBlue align=center><TD colspan=7><FONT size="-1" FACE="Arial" color=white>The following was edited or deleted</FONT>
</TD></TR>
<TR bgcolor=MediumBlue align=center>
<TD><FONT size="-1" FACE="Arial" color=white>Action</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Account</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Date</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Trans</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Total \$</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Commision</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Comments</FONT></TD>
</TR>
EOF


open( PUR, "+<$path/data/reoccur.txt");
if ($flock) {
	flock PUR, 2; 
}
@reocc = <PUR>;
seek (PUR, 0, 0);
truncate (PUR,0);
foreach $reeo(@reocc) {
	@reoo = split(/\|/,$reeo);	
	if($INPUT{'stop_reoccur'} =~ /,$reoo[0],/) {
		$reoo[7] = "Inactive";
		$reeo = join("\,",@reoo);
	}
	print PUR $reeo;
}
close PUR;


#### OPEN GEN PURCH LOG ####
open( PUR, "+<$path/data/purchase.txt");
if ($flock) {
	flock PUR, 2; 
}
@purch = <PUR>;
seek (PUR, 0, 0);
truncate (PUR,0);

#### OPEN TOTALS FILE ####
open( TACC, "+<$path/data/total.txt");
if ($flock) {
	flock TACC, 2; 
}
@tacc = <TACC>;
seek (TACC, 0, 0);
truncate (TACC,0);

foreach $lines(@purch){
	# SORT THRU GEN. PURCH LOG #
	@out = split(/\|/,$lines);
	if ($INPUT{'purchase_edit'} =~ /,$out[0],/) {
		unless ($INPUT{'out2_' . $out[0]}) {
			$INPUT{'out2_' . $out[0]} = $out[2];
		}
		# FOUND PURCHASE #
		# CHECK IF recurring #
		open( REE, "+<$path/data/reoccur.txt");
		if ($flock) {
			flock REE, 2; 
		}
		@reocc = <REE>;
		seek (REE, 0, 0);
		truncate (REE,0);
		$freo = 0;
		foreach $reeo(@reocc) {
			@reoo = split(/\|/,$reeo);	
			if($reoo[0] == $out[0]) {
				$reoo[1] = $INPUT{'out1_' . $out[0]};
				$reoo[3] = $INPUT{'out3_' . $out[0]};
				$reoo[4] = $INPUT{'out4_' . $out[0]};
				$reoo[5] = $INPUT{'out5_' . $out[0]};
				$reoo[6] = $INPUT{'out6_' . $out[0]};
				
				# IS recurring #
				@reotrans = split(/\,/,$reoo[8]);
				foreach $trans(@reotrans) {
					chomp($trans);				
					$INPUT{'purchase_edit'} .= "$trans,";
					$INPUT{'out1_' . $trans} = $INPUT{'out1_' . $out[0]};
					$INPUT{'out2_' . $trans} = 0;
					$INPUT{'out3_' . $trans} = "$INPUT{'out3_' . $out[0]}_re";					
					$INPUT{'out4_' . $trans} = $INPUT{'out4_' . $out[0]};
					$INPUT{'out5_' . $trans} = $INPUT{'out5_' . $out[0]};
					$INPUT{'out6_' . $trans} = $INPUT{'out6_' . $out[0]};
				}
				
			}
			$reeo = join("\|",@reoo);
			print REE $reeo;	
		}		
		close REE;		
				
	## ACCOUNT ##
		if ($out[1] ne $INPUT{'out1_' . $out[0]}){
		## ACCOUNT CHANGED ##
			# OLD ACCOUNT #			
			# Account #
			# DEL,OLD ACCOUNT,AMOUNT #
			&purch_account(0,$out[1],$out[5]);
			# DAY
			# DEL,OLD ACC,AMOUNT,DAY #
			&purch_day(0,$out[1],$out[5],$out[2]);
			# PURCHASE LOG ACC #
			# DEL,ACC,TRANS#,TRANS CONTENT #
			&purch_purchase(0,$out[1],$out[0],0); 

			# NEW ACCOUNT #				
			$out[1] = $INPUT{'out1_' . $out[0]}; # RENAME TRANS OWNER #
			# Account #
			# DEL,OLD ACCOUNT,AMOUNT #
			&purch_account(1,$out[1],$out[5]);
			# DAY
			# DEL,OLD ACC,AMOUNT,DAY #
			&purch_day(1,$out[1],$out[5],$out[2]);
			# PURCHASE LOG ACC #
			# DEL,ACC,TRANS#,TRANS CONTENT #
			&purch_purchase(1,$out[1],$out[0],$lines); 
					
		}
		else {
		## ACCOUNT STAY SAME ##
			&purch_log_edit($out[1],$out[0],$INPUT{'out3_' . $out[0]},$INPUT{'out2_' . $out[0]},$INPUT{'out4_' . $out[0]},$INPUT{'out5_' . $out[0]},$INPUT{'out6_' . $out[0]});
		}
			
	## DAY ##
		## ACCOUNT ##
		if ($out[2] ne $INPUT{'out2_' . $out[0]}) {
			# DELETE OLD DAY #
			# DEL,ACC,AMOUNT,DAY #
			&purch_day(0,$out[1],$out[5],$out[2]);
			&purch_day_overall(0,$out[5],$out[2]);						

			# ADD NEW DAY #
			$out[2] = $INPUT{'out2_' . $out[0]};
			# ADD,ACC,AMOUNT,DAY #
			&purch_day(1,$out[1],$INPUT{'out5_' . $out[0]},$out[2]);			
			&purch_day_overall(1,$INPUT{'out5_' . $out[0]},$out[2]);
		}		
	## TRANSACTION ##
		unless ($out[3] == $INPUT{'out3_' . $out[0]}) {
			$out[3] = $INPUT{'out3_' . $out[0]};
		}	
	## TOTAL MONEY ##
		unless ($out[4] == $INPUT{'out4_' . $out[0]}) {
			$tacc[2] = $tacc[2] - $out[4] + $INPUT{'out4_' . $out[0]};
			$tacc[2] .="\n";
			$out[4] = $INPUT{'out4_' . $out[0]};
		}
	## COMMISSION ##
		unless ($out[5] == $INPUT{'out5_' . $out[0]}) {
			$tacc[3] = $tacc[3] - $out[5] + $INPUT{'out5_' . $out[0]};
			$tacc[3] .="\n";
			$acc[13] = $acc[13] - $out[5] + $INPUT{'out5_' . $out[0]};
			$acc[13] .="\n";
			$out[5] = $INPUT{'out5_' . $out[0]};
			
		}
	## COMMENTS ##
		unless ($out[6] eq $INPUT{'out6_' . $out[0]}) {
			$out[6] = $INPUT{'out6_' . $out[0]};
		}

print <<EOF;
<TR bgcolor=MediumBlue align=center>
<TD><FONT size="-1" FACE="Arial" color=white>Edited</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[1]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[2]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[3]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>\$ $out[4]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>\$ $out[5]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[6]</FONT></TD>
</TR>

EOF
		$lines = join("\|",@out);
		print PUR $lines;
	}
	elsif ($INPUT{'purchase_delete'} =~ /,$out[0],/) {
		#do delete stuff
		# CHECK IF recurring #
		open( REE, "+<$path/data/reoccur.txt");
		if ($flock) {
			flock REE, 2; 
		}
		@reocc = <REE>;
		seek (REE, 0, 0);
		truncate (REE,0);
		$freo = 0;
		foreach $reeo(@reocc) {
			@reoo = split(/\|/,$reeo);	
			if($reoo[0] == $out[0]) {
				# IS recurring #
				@reotrans = split(/\,/,$reoo[8]);
				foreach $trans(@reotrans) {
					chomp($trans);				
					$INPUT{'purchase_delete'} .= "$trans,";
				}
				next;
			}
			$reeo = join("\|",@reoo);
			print REE $reeo;	
		}		
		close REE;		
				
		# OLD ACCOUNT #			
		# Account #
		# DEL,OLD ACCOUNT,AMOUNT #
		&purch_account(0,$out[1],$out[5]);
		# DAY
		# DEL,OLD ACC,AMOUNT,DAY #
		&purch_day(0,$out[1],$out[5],$out[2]);
		# DEL,AMOUNT,DAY #
		&purch_day_overall(0,$out[5],$out[2]);
		# PURCHASE LOG ACC #
		# DEL,ACC,TRANS#,TRANS CONTENT #
		&purch_purchase(0,$out[1],$out[0],0); 
		
		## TOTAL MONIES ##
		$tacc[2] = $tacc[2] - $out[4];
		$tacc[2] .="\n";
		
		## COMMISSION ##
		$tacc[3] = $tacc[3] - $out[5];
		$tacc[3] .="\n";
		$acc[13] = $acc[13] - $out[5];
		$acc[13] .="\n";

print <<EOF;

<TR bgcolor=MediumBlue align=center>
<TD><FONT size="-1" FACE="Arial" color=white>Deleted</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[1]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[2]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[3]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>\$ $out[4]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>\$ $out[5]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[6]</FONT></TD>
</TR>

EOF
			
	}
	else {
	## UNCHANGED ##
		print PUR $lines;	
	}
}

print TACC @tacc;
close TACC;
close PUR;
print "</TABLE>";

&Bottom;
exit;
}

########## PAYMENT LOG ##########
sub acc_paid {
$account = $INPUT{'account'};
&checkpassword;
$all = 0;
if ($INPUT{'month_paid'} eq 'all') {
	$all = 1;
}
else {
	@dates = split(/\//,$INPUT{'month_paid'});
}

&stats_payment('admin',$account,$all,$dates[0],$dates[1]);

&Bottom;
exit;
}

########## VIEW DAILY STATS FOR SYSTEM ##########
sub viewdaily {
&checkpassword;

$all = 0;
if ($INPUT{'month_daily'} eq 'all') {
	$all = 1;
}
else {
	@dates = split(/\//,$INPUT{'month_daily'});
}

&stats_daily('admin_total',0,$all,$dates[0],$dates[1]);

print <<EOF;
<BR>
<FONT SIZE="-1" FACE="Arial">Get detailed stats on a day, by selecting<br>
the radio button next to the date and<BR>pressing the submit button below<BR>
<BR>
<INPUT TYPE="SUBMIT" NAME="daily_detailed" VALUE="Get Daily Detailed Stats">

</form><BR>
EOF

&Bottom;
exit;
}

########## DETAILED DAILY STATS FOR SYSTEM ##########
sub daily_detailed {

&checkpassword;
$theday = $INPUT{'day'};

print <<EOF;

<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =3>
<TR bgcolor=#E4E4E4 align=center><TD colspan=10><FONT FACE="Arial"><B>
Stats by account<BR>for $theday</B></FONT>
</TD></TR>
<TR bgcolor=#E4E4E4 align=center>
<TD><FONT SIZE="-2" FACE="Arial">Account</TD>
<TD><FONT SIZE="-2" FACE="Arial">Clicks</TD>
<TD><FONT SIZE="-2" FACE="Arial">Purchases</TD>
<TD><FONT SIZE="-2" FACE="Arial">\$ commission</TD>
</TR>
EOF

opendir (DIR, "$path/data/daily");
@file = grep { /.txt/ } readdir(DIR);
close (DIR); 
@file = sort(@file);

foreach $line (@file) {

	open ( ACCOUNT, "<$path/data/daily/$line" ) ;
	if ($flock) {
		flock ACCOUNT,2;
	}
	@page = <ACCOUNT>;
	close ACCOUNT;
	@days=0;
	@days = grep (/$theday/ , @page);

	if (@days) {
		chomp($days[0]);
		@day = split(/\|/,$days[0]);
		$pay_out = 0;
		$pagename = $line;
		$pagename =~ s/.txt//;
print <<EOF;

<TR bgcolor=white align=center>

<TD align=left><FONT SIZE="-1">$pagename</TD>
<TD><FONT SIZE="-2" FACE="Arial">$day[1]</TD>
<TD><FONT SIZE="-2" FACE="Arial">$day[2]</TD>
<TD><FONT SIZE="-2" FACE="Arial">\$$day[3]</TD>
</TR>
EOF

	}

} ## END PAGE
print "</TABLE><BR>";

&Bottom;
}

########## Delete Account ##########

sub acc_del { 

&checkpassword;

$account = $INPUT{'function'};
print <<EOF;
<FORM METHOD=POST ACTION="admin.cgi">
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =3>
<TR bgcolor=#E4E4E4 align=center><TD><FONT size="-1" FACE="Arial">Enter you admin password<BR>to confirm deletion of <BR>
account: <FONT COLOR="#FF0000"><B>$account</B></FONT></FONT>
<BR><BR>
<INPUT TYPE="HIDDEN" NAME="function" VALUE="$account">
<INPUT TYPE="PASSWORD" NAME="password" VALUE="">
<BR><BR>
<INPUT TYPE="SUBMIT" NAME="acc_del_final" VALUE="Delete $account">
</TD></TR>
</TABLE></FORM>

EOF
&Bottom;
exit;
}

######### Delete Final ##########

sub acc_del_final { 
&checkpassword;
$account = $INPUT{'function'};

open( PUR, "+<$path/data/reoccur.txt");
if ($flock) {
	flock PUR, 2; 
}
@reocc = <PUR>;
seek (PUR, 0, 0);
truncate (PUR,0);
foreach $reeo(@reocc) {
	@reoo = split(/\|/,$reeo);	
	unless($reoo[1] eq $account) {
		print PUR $reeo;
	}
}
close PUR;

#### OPEN GEN PURCH LOG ####
open( PUR, "+<$path/data/purchase.txt");
if ($flock) {
	flock PUR, 2; 
}
@purch = <PUR>;
seek (PUR, 0, 0);
truncate (PUR,0);

#### OPEN TOTALS FILE ####
open( TACC, "+<$path/data/total.txt");
if ($flock) {
	flock TACC, 2; 
}
@tacc = <TACC>;
seek (TACC, 0, 0);
truncate (TACC,0);

foreach $lines(@purch){
	# SORT THRU GEN. PURCH LOG #
	@out = split(/\|/,$lines);
	if ($out[1] eq $account) {
	# OLD ACCOUNT #			
		# Account #
		# DEL,OLD ACCOUNT,AMOUNT #
		&purch_account(0,$out[1],$out[5]);
		# DAY
		# DEL,OLD ACC,AMOUNT,DAY #
		&purch_day(0,$out[1],$out[5],$out[2]);
		# DEL,AMOUNT,DAY #
		&purch_day_overall(0,$out[5],$out[2]);
		# PURCHASE LOG ACC #
		# DEL,ACC,TRANS#,TRANS CONTENT #
		&purch_purchase(0,$out[1],$out[0],0); 
		
		## TOTAL MONIES ##
		$tacc[2] = $tacc[2] - $out[4];
		$tacc[2] .="\n";
		
		## COMMISSION ##
		$tacc[3] = $tacc[3] - $out[5];
		$tacc[3] .="\n";
		$acc[13] = $acc[13] - $out[5];
		$acc[13] .="\n";

	}
	else {
		print PUR "$lines";
	}
}

print TACC @tacc;
close TACC;
close PUR;


unlink("$path/data/accounts/$account.txt");
unlink("$path/data/daily/$account.txt");
unlink("$path/data/logs/$account.txt");
unlink("$path/data/paid/$account.txt");
unlink("$path/data/purch/$account.txt");

print <<EOF;
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =3>
<TR bgcolor=#E4E4E4 align=center><TD colspan=10><FONT size="-1" FACE="Arial">Account: <FONT COLOR="#FF0000">$account</FONT> deleted
</TD></TR>
</TABLE>
EOF
&Bottom;
exit;
}

########## ACCOUNT STATS ##########

sub acc_stats {
&checkpassword;

$account = $INPUT{'function'};
$mes = "View and Edit account <font color=red>$account</font>";
&account_info($account,$mes,'admin');
&Bottom;
exit;
}


########## EDIT ACCOUNT INFO ##########
sub edit {

&checkpassword;
$account = $INPUT{'account'};

&edit_info_final($account);

&main("Account <font color=red>$account</FONT> Updated");
exit;
}


########## PAY ACCOUNTS ##########
sub pay {

&checkpassword;
$amount = $INPUT{'pay_owe'};

opendir (DIR, "$path/data/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

@files = sort(@files);

print <<EOF;
<CENTER>
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$INPUT{'password'}">
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =3>
<TR bgcolor=#E4E4E4 align=center><TD colspan=10><FONT size="-1" FACE="Arial">
Accounts that are due <font color=red>\$$amount</FONT> or more
</TD></TR></TABLE><BR><BR>
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =10>
EOF

foreach $line(@files) {
	open(LIST,"$path/data/accounts/$line");
	if ($flock) {
	flock LIST,2;
	}
	@acc = <LIST>;
	close(LIST);
	$tot = $acc[13] - $acc[14];
	if ($tot >= $amount) {
		$accname = $line;
		$accname =~ s/\.txt//;

print <<EOF;
<TR bgcolor=#E4E4E4><TD><FONT size="-1" FACE="Arial"><FONT COLOR="#FF0000">$accname</FONT> -- account name
<hr noshade size=1 width=90%>
<INPUT TYPE="CHECKBOX" NAME="payme" VALUE="$line">&nbsp;&nbsp;Check to pay
<hr noshade size=1 width=90%>
\$ $tot amount owed this account
<hr noshade size=1 width=90%>
<input type="Text" name="pay_$accname" value="$amount" size="4">&nbsp;&nbsp;Enter amount to pay
<hr noshade size=1 width=90%>
Account Info:<BR><BR>
<FONT size="-1" FACE="Arial">
<A HREF="mailto:$acc[0]">$acc[3]</A><BR>
$acc[2]<BR>
$acc[4]<BR>
$acc[5] $acc[6]<BR>
$acc[7]<BR>
Last Paid: $last_paid
<BR>
</TD>
EOF
	}
}
print <<EOF;
</TABLE><BR><BR>
<INPUT TYPE="SUBMIT" NAME="paid" VALUE="Pay Accounts">
<BR><BR>
</FORM>
EOF

&Bottom;
exit;
}

########## RECORD PAYMENTS ##########
sub paid {
&checkpassword;
$file = $INPUT{'payme'};
@files = split(/\,/,$file);

print <<EOF;
<CENTER>
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =3>
<TR bgcolor=#E4E4E4 align=center><TD colspan=10><FONT size="-1" FACE="Arial">Accounts Just Paid
</TD></TR></TABLE><BR><BR>
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =10>
EOF

$pay_due =0;
$tot_amount =0;

foreach $line(@files) {
	open(LIST,"$path/data/accounts/$line");
	@acc = <LIST>;
	close(LIST);
	$accname = $line;
	$accname =~ s/\.txt//;	


print <<EOF;
<TR bgcolor=#E4E4E4><TD><FONT size="-1" FACE="Arial">Account paid: <FONT COLOR="#FF0000">$accname</FONT>
<hr noshade size=1 width=90%>
Paid \$$INPUT{'pay_' . $accname}
<hr noshade size=1 width=90%>
Account Info:<BR><BR>
<FONT size="-1" FACE="Arial">
<A HREF="mailto:$acc[0]">$acc[3]</A><BR>
$acc[2]<BR>
$acc[4]<BR>
$acc[5] $acc[6]<BR>
$acc[7]<BR>
Amount Paid: \$$INPUT{'pay_' . $accname}
</TD></TR>

EOF
		$pay_due++;
		open(LIST,">>$path/data/paidlog.txt");	
		print LIST "$now|$accname|$INPUT{'pay_' . $accname}\n";	
		close LIST;
		
		open(LIST,">>$path/data/paid/$line");	
		print LIST "$now|$INPUT{'pay_' . $accname}\n";	
		close LIST;
		
		$acc[14] += $INPUT{'pay_' . $accname};
		$acc[14] .= "\n";

		$tot_amount += $INPUT{'pay_' . $accname};
		
		open( PAGE, ">$path/data/accounts/$line");
		if ($flock) {
			flock PAGE,2;
		}
		print PAGE @acc;
		close PAGE;
}
print <<EOF;
</TABLE><BR><BR>
<FONT size="-1" FACE="Arial">
$pay_due account paid \$$tot_amount<BR>
EOF

open( PAGE, "<$path/data/paid.txt");
if ($flock) {
flock PAGE,2;
}
$tpaid = <PAGE>;
close PAGE;

$tpaid += $tot_amount;

open( PAGE, ">$path/data/paid.txt");
if ($flock) {
flock PAGE,2;
}
print PAGE $tpaid;
close PAGE;

&Bottom;
exit;

}

########## PAYMENT LOG FOR SYSTEM ##########
sub paylog {
&checkpassword;
$all = 0;
if ($INPUT{'month_pay'} eq 'all') {
	$all = 1;
}
else {
	@dates = split(/\//,$INPUT{'month_pay'});
}

&stats_payment('admin_total',0,$all,$dates[0],$dates[1]);

&Bottom;
exit;
}

########## EDIT DELETE PAYMANT ##########
sub edit_del_paid {
&checkpassword;

print  "<FORM METHOD=POST ACTION=\"$cgiurl\"><input type=\"Hidden\" name=\"pass\" value=\"$INPUT{'password'}\">";

print <<EOF;
<input type="Hidden" name="purchase_edit" value="$INPUT{'purchase_edit'}">
<table cellspacing =0  border=1 cellpadding =4>
<TR bgcolor=MediumBlue align=center><TD colspan=3><FONT size="-1" FACE="Arial" color=white>Please edit and/or confirm the deletion<BR>of the
following purchases for account $account</FONT>
</TD></TR>
<TR bgcolor=MediumBlue align=center>
<TD><FONT size="-1" FACE="Arial" color=white>Date</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Account</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Amount</FONT></TD>
</TR>
EOF



open (PASSWORD, "$path/data/paidlog.txt");
if ($flock){
	flock PASSWORD,2;
}
@purch = <PASSWORD>;
close (PASSWORD);



$INPUT{'purchase_edit'} = ',' . $INPUT{'purchase_edit'} . ',';
$INPUT{'purchase_delete'} = ',' . $INPUT{'purchase_delete'} . ',';

@colors=('MediumSlateBlue','SlateBlue');
$color=0;
$color2=0;
$delete = '';

foreach $line(@purch){

	@out = split(/\|/,$line);
	if ($INPUT{'purchase_edit'} =~ /,$out[0]$out[1],/) {
	unless ($color) {
		$color=1;
	}
	else {
		$color=0;
	}
	print "<TR bgcolor=$colors[$color]>";
print <<EOF;
<TD><FONT size="-1" FACE="Arial" color=white><input type="Text" name="out0_$out[0]$out[1]" value="$out[0]" size=10></FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[1]<input type="hidden" name="out1_$out[0]$out[1]" value="$out[1]" size=10></FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white><input type="Text" name="out2_$out[0]$out[1]" value="$out[2]" size=10></FONT></TD>
</TR>
EOF
		next;
	}
	if($INPUT{'purchase_delete'} =~ /,$out[0]$out[1],/) {
	unless ($color2) {
		$color2=1;
	}
	else {
		$color2=0;
	}
	$delete .= "<TR bgcolor=$colors[$color2]>";	
	$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white><input type=\"Checkbox\" name=\"purchase_delete\" value=\"$out[0]$out[1]\" checked></FONT></TD>";
	$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>$out[0]</FONT></TD>";
	$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>$out[1]</FONT></TD>";
	$delete .= "<TD><FONT size=\"-1\" FACE=\"Arial\" color=white>\$$out[2]</FONT></TD></TR>";
		next;
	}
}

print "</TABLE><BR><BR>";

if ($delete) {
print <<EOF;
<table cellspacing =0  border=1 cellpadding =4>
<TR bgcolor=MediumBlue align=center><TD colspan=4><FONT size="-1" FACE="Arial" color=white>Uncheck to save, leave checked to delete</FONT>
</TD></TR>
<TR bgcolor=MediumBlue align=center>
<TD><FONT size="-1" FACE="Arial" color=white>Delete</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Date</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Account</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Amount</FONT></TD>
</TR>
$delete
</TABLE>
<br><br>
EOF
}
print<<EOF;
<input type="Submit" name="edit_paid_final" value="Delete / Edit">
</form><BR>
EOF

&Bottom;
exit;


}

########## EDIT DEL PAYMENT LOG FINAL ##########
sub edit_paid_final {
&checkpassword;

print <<EOF;
<table cellspacing =0  border=1 cellpadding =4>
<TR bgcolor=MediumBlue align=center><TD colspan=4><FONT size="-1" FACE="Arial" color=white>The following has been edited or deleted in the payment log</FONT>
</TD></TR>
<TR bgcolor=MediumBlue align=center>
<TD><FONT size="-1" FACE="Arial" color=white>Action</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Date</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Account</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>Amount</FONT></TD>
</TR>
EOF


open( TACC, "+<$path/data/paidlog.txt");
if ($flock) {
	flock TACC, 2; 
}
@purch = <TACC>;
seek (TACC, 0, 0);
truncate (TACC,0);


$INPUT{'purchase_edit'} = ',' . $INPUT{'purchase_edit'} . ',';
$INPUT{'purchase_delete'} = ',' . $INPUT{'purchase_delete'} . ',';

@colors=('MediumSlateBlue','SlateBlue');
$color=0;
$color2=0;
$delete = '';
$total=0;

foreach $line(@purch){

	@out = split(/\|/,$line);
	if ($INPUT{'purchase_edit'} =~ /,$out[0]$out[1],/) {
		unless ($color) {
			$color=1;
		}
		else {
			$color=0;
		}
		print "<TR bgcolor=$colors[$color]>";
		print TACC "$INPUT{'out0_' . $out[0] . $out[1]}|$INPUT{'out1_' . $out[0] . $out[1]}|$INPUT{'out2_' . $out[0] . $out[1]}\n";
print <<EOF;
<TD><FONT size="-1" FACE="Arial" color=white>Edited</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$INPUT{'out0_' . $out[0] . $out[1]}</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$INPUT{'out1_' . $out[0] . $out[1]}</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>\$$INPUT{'out2_' . $out[0] . $out[1]}</FONT></TD>
</TR>
EOF
		$account = $INPUT{'out1_' . $out[0] . $out[1]};
		open( CC, "+<$path/data/paid/$account.txt");
		if ($flock) {
			flock CC, 2; 
		}
		@paid = <CC>;
		seek (CC, 0, 0);
		truncate (CC,0);
		foreach $lines(@paid){

			@outs = split(/\|/,$lines);
			if ($INPUT{'purchase_edit'} =~ /,$outs[0]$account,/) {
				print CC "$INPUT{'out0_' . $outs[0] . $account}|$INPUT{'out2_' . $outs[0] . $account}\n";
			}
			else {
				print CC $lines
			}
		}
		close CC;
		
		open( ACC, "+<$path/data/accounts/$account.txt");
		if ($flock) {
			flock ACC, 2; 
		}
		@acc = <ACC>;
		seek (ACC, 0, 0);
		truncate (ACC,0);
		$acc[14] = $acc[14] - $out[2] + $INPUT{'out2_' . $out[0] . $account};
		$acc[14] .= "\n";
		print ACC @acc;
		close ACC;
		$total = $total  - $out[2] + $INPUT{'out2_' . $out[0] . $account};
		
	}
	elsif($INPUT{'purchase_delete'} =~ /,$out[0]$out[1],/) {
		unless ($color) {
			$color=1;
		}
		else {
			$color=0;
		}
print <<EOF;
<TR bgcolor=$colors[$color]>
<TD><FONT size="-1" FACE="Arial" color=white>Deleted</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[0]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>$out[1]</FONT></TD>
<TD><FONT size="-1" FACE="Arial" color=white>\$$out[2]</FONT></TD>
</TR>
EOF

		$account = $INPUT{'out1_' . $out[0] . $out[1]};
		open( CC, "+<$path/data/paid/$out[1].txt");
		if ($flock) {
			flock CC, 2; 
		}
		@paid = <CC>;
		seek (CC, 0, 0);
		truncate (CC,0);
		foreach $lines(@paid){
			@outs = split(/\|/,$lines);
			if ($INPUT{'purchase_delete'} =~ /,$outs[0]$out[1],/) {
				#delete
			}
			else {
				print CC $lines
			}
		}
		close CC;

		open( ACC, "+<$path/data/accounts/$out[1].txt");
		if ($flock) {
			flock ACC, 2; 
		}
		@acc = <ACC>;
		seek (ACC, 0, 0);
		truncate (ACC,0);
		$acc[14] = $acc[14] - $out[2];
		$acc[14] .= "\n";
		print ACC @acc;
		close ACC;
		
		$total = $total  - $out[2];
	}
	else {
		print TACC $line;
	}
}
close TACC;
print "</TABLE><BR>$total<BR>";

		open( ACC, "+<$path/data/paid.txt");
		if ($flock) {
			flock ACC, 2; 
		}
		$acc = <ACC>;
		seek (ACC, 0, 0);
		truncate (ACC,0);
		$acc = $acc + $total;
		print ACC $acc;
		close ACC;

&Bottom;
exit;




}



########## BROWSE ACCOUNTS ##########
sub view {

&checkpassword;
opendir (DIR, "$path/data/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 
$numaccounts = push(@files);

@files = sort(@files);
$all = 0;
if ($INPUT{'view_letter'} eq 'all') {
$all = 1;
}

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$INPUT{'password'}">
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =3>
<TR bgcolor=#E4E4E4 align=center><TD colspan=9><FONT FACE="Arial"><B>
Review Accounts that begin with $INPUT{'view_letters'}</B></FONT>
</TD></TR>
<TR bgcolor=#E4E4E4 align=center>

<TD><FONT SIZE="-1">Select</TD>
<TD><FONT SIZE="-1" FACE="Arial">Account</TD>
<TD><FONT SIZE="-1" FACE="Arial">Clicks</TD>
<TD><FONT SIZE="-1" FACE="Arial">Purchases</TD>
<TD><FONT SIZE="-1" FACE="Arial">Commission</TD>
<TD><FONT SIZE="-1" FACE="Arial">Paid Out</TD>
<TD><FONT SIZE="-1" FACE="Arial">Days Since<BR>Last Click</TD>
<TD><FONT SIZE="-1" FACE="Arial">Name</TD>
<TD><FONT SIZE="-1" FACE="Arial">Web Site</TD>
</TR>


EOF

foreach $line(@files) {

	open(LIST,"$path/data/accounts/$line");
	@acc = <LIST>;
	close(LIST);
	$accname = $line;
	$accname =~ s/\.txt//;

	if ($all || ($accname=~/^($INPUT{'view_letter'})/i) ) {
	
		$since = 0;
		chomp($acc[10]);
		if ($acc[10]) {
			$since = int( ($time - $acc[10]) / 86400);
		}
print <<EOF;

<TR bgcolor=white>
<TD><FONT SIZE="-1"><INPUT TYPE="RADIO" NAME="function" VALUE="$accname"></TD>
<TD><FONT SIZE="-1" FACE="Arial" color="#FF0000">$accname</TD>
<TD><FONT SIZE="-1" FACE="Arial">$acc[11]</TD>
<TD><FONT SIZE="-1" FACE="Arial">$acc[12]</TD>
<TD><FONT SIZE="-1" FACE="Arial">\$$acc[13]</TD>
<TD><FONT SIZE="-1" FACE="Arial">\$$acc[14]</TD>
<TD><FONT SIZE="-1" FACE="Arial">$since</TD>
<TD><FONT SIZE="-1" FACE="Arial"><A HREF="mailto:$acc[0]"><font color=red>$acc[3]</FONT></A></TD>
<TD><FONT SIZE="-1" FACE="Arial"><A HREF="$acc[1]"><font color=red>$acc[1]</FONT></A></TD>
</TR>

EOF

	}

}
print <<EOF;
<TR  bgcolor=#E4E4E4 align=center>
<TD colspan=9>
<INPUT TYPE="SUBMIT" NAME="acc_stats" VALUE="Show Stats / Edit Info">
&nbsp;&nbsp;&nbsp;
<INPUT TYPE="SUBMIT" NAME="acc_del" VALUE="Delete Account">
</TD></TR>
</TABLE>
</FORM>

EOF
&Bottom;
exit;

}


########## DISPLAY TEXT TO EDIT ###########
sub text {

$file = $INPUT{'efile'};

open (DAT,"<$path/data/$file");
@headers = <DAT>;
close (DAT);

print <<EOF; 
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$INPUT{'password'}">
<INPUT TYPE="HIDDEN" NAME="file" VALUE="$file">
<B>Edit the file: <font color=red>$file</FONT></B>
<BR><BR><font size=-1>The html, head, title and body tags are not needed</font><BR><BR>
<TEXTAREA NAME="filecontents" ROWS=25 COLS=65 wrap="OFF">
@headers
</TEXTAREA>
<BR><BR>
<INPUT TYPE="SUBMIT" NAME="textedit" VALUE="Save your changes">
</form>
EOF

&Bottom;
exit;
}

########## SAVE TEXT ##########	
sub textedit {
if ($demo) { &demo; }
&checkpassword;
$file = $INPUT{'file'};

$INPUT{'filecontents'} =~ s/\n//gi;

open (DAT,">$path/data/$file");
print DAT $INPUT{'filecontents'};
close (DAT);

&main("<B>File <font color=red>$file</font> updated</b>");
&Bottom;
exit;

}


########## SEND EMAIL TO ALL MEMBERS ##########
sub sendemail {
$checkpassword;
print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$INPUT{'password'}">
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =8 width=400>
<TR bgcolor=#E4E4E4>
<TD><center><FONT SIZE="-1" FACE="Arial">Send email to all addresses<BR><BR></CENTER>

Subject<BR>
<input type = text name=subject size=45>
<BR><BR>Body of Message
	<TEXTAREA NAME=body ROWS=14 COLS=50></TEXTAREA>
<BR><BR>
<INPUT TYPE=SUBMIT NAME=sentemail VALUE="Send Email">
</TD></TR></TABLE></FORM><BR>

<table border=0 width=400>
<TR>
<TD><FONT SIZE="-2" FACE="Arial">
Please note: due to the time it takes for perl to handle large listes of emails addresses and send out a mailing to each

</TD></TR></TABLE>
EOF
&Bottom;
exit;

}

######### ACTUAL SENDING OF EMAILS ##########
sub sentemail {
if ($demo) { &demo; }
&checkpassword;
opendir (DIR, "$path/data/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

	$num_email=0;
foreach $line(@files) {
	open(LIST,"$path/data/accounts/$line");
	@acc = <LIST>;
	close(LIST);
	chomp($acc[0]);
	&send_email($acc[0],$INPUT{'subject'},$INPUT{'body'});
	$num_email++;
}

	print"<BR>Email sent to all $num_email members<BR>";

	&Bottom;
	exit;
	
}


########## APPROVE PURCHASES ##########
sub approve {

&checkpassword;

opendir (DIR, "$path/data/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

@files = sort(@files);


open(LIST,"$path/data/awaiting.txt");
@list=<LIST>;
close(LIST);
$numtemp = push(@list);
print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$INPUT{'password'}">
<table cellspacing =0 bgcolor =mediumblue border=1 cellpadding =8>
<TR align=center><TD colspan=9><FONT SIZE="-1" FACE="Arial" color=white>
<a href="http://solutionscripts.com/man/comish/1.1.2.shtml" target="_blank"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=middle width=20 height=22 border=0></a>
Confirming $numtemp purchases <small><BR><I>Select either accept or deny checkbox</I></small>
</font></td></TR>
<TR align=center>
<TD><FONT SIZE="1" FACE="Arial" color=white>Accept</TD>
<TD><FONT SIZE="1" FACE="Arial" color=white>Deny</TD>
<TD><FONT SIZE="1" FACE="Arial" color=white>Account</TD>
<TD><FONT SIZE="1" FACE="Arial" color=white>Date</TD>
<TD><FONT SIZE="1" FACE="Arial" color=white>Purchase id</TD>
<TD><FONT SIZE="1" FACE="Arial" color=white>Total \$</TD>
<TD><FONT SIZE="1" FACE="Arial" color=white>Commission</TD>
<TD><FONT SIZE="1" FACE="Arial" color=white>Comments</TD>
<TD><FONT SIZE="1" FACE="Arial" color=white>Recurring</TD>
</TR>
EOF
$a=0;
@colors=('MediumSlateBlue','SlateBlue');
$color=1;
foreach $line (@list) {
	@lines = split(/\|/,$line);
	unless ($color) {
		$color=1;
	}
	else {
		$color=0;
	}
print <<EOF;
<TR bgcolor=$colors[$color] align=center>
<TD><FONT SIZE="-1" FACE="Arial" color=white><INPUT TYPE="RADIO" NAME="$lines[0]" VALUE="accept"></TD>
<TD><FONT SIZE="-1" FACE="Arial" color=white><INPUT TYPE="RADIO" NAME="$lines[0]" VALUE="deny"></TD>
<TD>
<SELECT NAME="approve1_$a">
EOF

foreach $linn(@files) {
	$accname = $linn;
	$accname =~ s/\.txt//;
	
	$selected = '';
	if ($lines[1] eq $accname) {
		$selected = "SELECTED";
	}
	else {
		$selected = "";
	}
print <<EOF;
<option value="$accname" $selected >$accname
EOF
}

print <<EOF;
</SELECT>	
</TD>
<TD><FONT SIZE="-1" FACE="Arial" color=white><input type="Text" name="approve2_$a" value="$lines[2]" size="10"></TD>
<TD><FONT SIZE="-1" FACE="Arial" color=white><input type="Text" name="approve3_$a" value="$lines[3]" size="10"></TD>
<TD><FONT SIZE="-1" FACE="Arial" color=white><input type="Text" name="approve4_$a" value="$lines[4]" size="10"></TD>
<TD><FONT SIZE="-1" FACE="Arial" color=white><input type="Text" name="approve5_$a" value="$lines[5]" size="10"></TD>
<TD><FONT SIZE="-1" FACE="Arial" color=white><input type="Text" name="approve6_$a" value="$lines[6]" size="10"></TD>
EOF
chomp($lines[7]);
if ($lines[7]) {
	$reer = "CHECKED";
}
else {
	$reer = "";
}
print <<EOF;
<TD><FONT SIZE="-1" FACE="Arial" color=white><input type="CHECKBOX" name="approve7_$a" value="1" $reer></TD>
</TR>
EOF
	$a++;
}
print <<EOF;

<tr align=center><TD colspan=9>
<INPUT TYPE="SUBMIT" NAME="aapprove" VALUE="Submit">
</TD></TR></TABLE>
</form>
EOF

&Bottom;
exit;
}

########## FINAL APPROVE PURCHASES #########
sub aapprove {
&checkpassword;

open(LIST,"$path/data/awaiting.txt");
@list=<LIST>;
close(LIST);
$numtemp = push(@list);
print <<EOF;

<table cellspacing =0 bgcolor =mediumblue border=1 cellpadding =8>
<TR align=center><TD><FONT SIZE="-1" FACE="Arial" color=white>
Purchases accepted or denied
</font></td></TR>
EOF

open(LIST,">$path/data/awaiting.txt");
if ($flock) {
	flock LIST,2;
}

$a=0;
@colors=('MediumSlateBlue','SlateBlue');
$color=1;
foreach $line (@list) {
	@lines = split(/\|/,$line);
	if ($INPUT{$lines[0]} eq "accept" ) {
		unless ($color) {
			$color=1;
		}
		else {
			$color=0;
		}
print <<EOF;
<TR bgcolor=$colors[$color] align=center>
<TD><FONT SIZE="-1" FACE="Arial" color=white>Transaction $INPUT{'approve3_' . $a} for account $INPUT{'approve1_' . $a} -- Accepted</TD>
</TR>
EOF
		&purchase_made($INPUT{'approve1_' . $a},$INPUT{'approve2_' . $a},$INPUT{'approve3_' . $a},$INPUT{'approve4_' . $a},$INPUT{'approve5_' . $a},$INPUT{'approve6_' . $a},$lines[0],$INPUT{'approve7_' . $a});
	}
	elsif ( $INPUT{$lines[0]} eq "deny") {
		unless ($color) {
			$color=1;
		}
		else {
			$color=0;
		}
print <<EOF;
<TR bgcolor=$colors[$color] align=center>
<TD><FONT SIZE="-1" FACE="Arial" color=white>Transaction $INPUT{'approve3_' . $a} for account $INPUT{'approve1_' . $a} -- Denied</TD>
</TR>
EOF
	}
	else {
		print LIST $line;
	}
	$a++;

}

close LIST;
print <<EOF;
</TABLE>
EOF

&Bottom;
exit;

}


########################## PURCHASE LOG ##########################
sub purch {

&checkpassword;

$all = 0;
if ($INPUT{'month_purch'} eq 'all') {
	$all = 1;
}
else {
	@dates = split(/\//,$INPUT{'month_purch'});
}

&stats_purchase('admin_total',0,$all,$dates[0],$dates[1]);

print "<BR><BR>";

&Bottom;
exit;
}

########## NEW PURCHASES MAIN SUB ##########
sub new_purchase {
&checkpassword;

opendir (DIR, "$path/data/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

@files = sort(@files);

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl">
<input type="Hidden" name="password" value="$INPUT{'password'}">
<TABLE cellspacing =0 bgcolor =slateblue border=1 cellpadding =3><tr align=center>
<TD colspan=7><font size=-1 face=arial color=white>
<a href="http://solutionscripts.com/man/comish/1.1.9.shtml#1" target="_blank"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=left width=20 height=22 border=0></a>
Enter new purchases manually
</TD></TR>
<TR><TD>
<font size=1 face=arial color=white>Account
</TD><TD>
<font size=1 face=arial color=white>Date
</TD><TD>
<font size=1 face=arial color=white>Transaction Code
</TD><TD>
<font size=1 face=arial color=white>Total \$ amount
</TD><TD>
<font size=1 face=arial color=white>Commission Amount
</TD><TD>
<font size=1 face=arial color=white>Comments
</TD><TD>
<font size=1 face=arial color=white>Recurring
</TD></TR>

EOF

$a=0;

while ($a<5) {
print <<EOF;
<TR>
<TD><SELECT NAME="account_$a">
EOF

foreach $linn(@files) {
$accname = $linn;
$accname =~ s/\.txt//;

print <<EOF;
<option value="$accname">$accname
EOF
}

print <<EOF;
</SELECT>	
</TD><TD>
<input type="Text" name="date_$a" size="10">
</TD><TD>
<input type="Text" name="trans_$a" size="10">
</TD><TD>
<input type="Text" name="total_$a" size="10">
</TD><TD>
<input type="Text" name="comish_$a" size="10">
</TD><TD>
<input type="Text" name="comments_$a" size="10">
</TD><TD align=center>
<input type="Checkbox" name="recurring_$a">
</TD></TR>
EOF
	$a++;
}
print <<EOF;
<TR align=center>
<TD colspan=6>
<input type="Submit" name="new_purchase_acc" value="Enter these purchases">
</TD></TR>
</TABLE>
<BR><BR>

<TABLE cellspacing =0 bgcolor =slateblue border=1 cellpadding =3><tr align=center>
<TD colspan=7><font size=-1 face=arial color=white>
<a href="http://solutionscripts.com/man/comish/1.1.9.shtml#2" target="_blank"><img src="http://solutionscripts.com/man/icons/unknown.gif" align=left width=20 height=22 border=0></a>
Enter a purchase using IP Address
</TD></TR>
<TR><TD>
<font size=1 face=arial color=white>I.P. Address
</TD><TD>
<font size=1 face=arial color=white>Date
</TD><TD>
<font size=1 face=arial color=white>Transaction Code
</TD><TD>
<font size=1 face=arial color=white>Total \$ amount
</TD><TD>
<font size=1 face=arial color=white>Commission Amount
</TD><TD>
<font size=1 face=arial color=white>Comments
</TD><TD>
<font size=1 face=arial color=white>Recurring
</TD></TR>

EOF

$a=5;

while ($a<10) {
print <<EOF;
<TR><TD>
<input type="Text" name="ip_$a" size="20"></TD><TD>
<input type="Text" name="date_$a" size="10"></TD><TD>
<input type="Text" name="trans_$a" size="10"></TD><TD>
<input type="Text" name="total_$a" size="10"></TD><TD>
<input type="Text" name="comish_$a" size="10"></TD><TD>
<input type="Text" name="comments_$a" size="10"></TD><TD align=center>
<input type="Checkbox" name="recurring_$a">
</TD></TR>
EOF
	$a++;
}
print <<EOF;
<TR align=center>
<TD colspan=6>
<input type="Submit" name="new_purchase_ip" value="Enter these purchases">
</TD></TR>
</TABLE>
</FORM>
EOF

&Bottom;
exit;
}

########## MANUAL PURCHASE FINAL ACCOUNT ##########

sub new_purchase_acc {
&checkpassword;

print <<EOF;
<TABLE cellspacing =0 bgcolor =slateblue border=1 cellpadding =3><tr align=center>
<TD colspan=8><font size=-1 face=arial color=white>
The following purchases where credited to the accounts chosen
</TD></TR>
<TR><TD>
<font size=1 face=arial color=white>Status
</TD><TD>
<font size=1 face=arial color=white>Account
</TD><TD>
<font size=1 face=arial color=white>Date
</TD><TD>
<font size=1 face=arial color=white>Transaction Code
</TD><TD>
<font size=1 face=arial color=white>Total \$ amount
</TD><TD>
<font size=1 face=arial color=white>Commission Amount
</TD><TD>
<font size=1 face=arial color=white>Comments
</TD><TD>
<font size=1 face=arial color=white>Recurring
</TD></TR>

EOF

$a =0;
while ($a <5) {
	if ($INPUT{'date_' . $a} && $INPUT{'trans_' . $a} && $INPUT{'total_' . $a} && $INPUT{'comish_' . $a}) {
		&purchase_made($INPUT{'account_' . $a},$INPUT{'date_' . $a},$INPUT{'trans_' . $a},$INPUT{'total_' . $a},$INPUT{'comish_' . $a},$INPUT{'comments_' . $a},0,$INPUT{'recurring_' .$a})
	}
	else {
		$status = "Failed -- All fields not filled out";
	}

print <<EOF;
<TR align=left><TD>
<font size=1 face=arial color=white>$status</TD><TD>
<font size=1 face=arial color=white>$INPUT{'account_' . $a}</TD><TD>
<font size=1 face=arial color=white>$INPUT{'date_' . $a}</TD><TD>
<font size=1 face=arial color=white>$INPUT{'trans_' . $a}</TD><TD>
<font size=1 face=arial color=white>\$$INPUT{'total_' . $a}</TD><TD>
<font size=1 face=arial color=white>\$$INPUT{'comish_' . $a}</TD><TD>
<font size=1 face=arial color=white>$INPUT{'comments_' . $a}</TD><TD>
<font size=1 face=arial color=white>$INPUT{'recurring_' .$a}
</TD></TR>
EOF
	
	$a++;
}

print "</TABLE>";

&Bottom;
exit;
}

########## NEW PURCHASE BY IP ##########
sub new_purchase_ip {
&checkpassword;

print <<EOF;
<TABLE cellspacing =0 bgcolor =slateblue border=1 cellpadding =3><tr align=center>
<TD colspan=8><font size=-1 face=arial color=white>
The following purchases where credited to the appropiate accounts
</TD></TR>
<TR><TD>
<font size=1 face=arial color=white>Status
</TD><TD>
<font size=1 face=arial color=white>Account
</TD><TD>
<font size=1 face=arial color=white>Date
</TD><TD>
<font size=1 face=arial color=white>Transaction Code
</TD><TD>
<font size=1 face=arial color=white>Total \$ amount
</TD><TD>
<font size=1 face=arial color=white>Commission Amount
</TD><TD>
<font size=1 face=arial color=white>Comments
</TD><TD>
<font size=1 face=arial color=white>Recurring
</TD></TR>
EOF

$a =5;
while ($a <10) {
	$account = "";
	if ($INPUT{'ip_' . $a} && $INPUT{'date_' . $a} && $INPUT{'trans_' . $a} && $INPUT{'total_' . $a} && $INPUT{'comish_' . $a}) {
		## FIND IP ##
		$theip = $INPUT{'ip_' . $a};
		opendir (DIR, "$path/data/logs");
		@files = grep { /.txt/ } readdir(DIR);
		close (DIR); 
	
		foreach $liner(@files) {
	
			open(LIST,"$path/data/logs/$liner");
			@ipdb = <LIST>;
			close(LIST);
			foreach $line(@ipdb) {
				@ip = split(/\|/,$line);
				if ( $ip[0] == $theip) { 
					$found=1; 
					last;
				}		
			}
			if ($found) {
				$account = $liner;
				$account =~ s/\.txt//;
				last;
			}
		
		}
		if ($found) {
			&purchase_made($account,$INPUT{'date_' . $a},$INPUT{'trans_' . $a},$INPUT{'total_' . $a},$INPUT{'comish_' . $a},$INPUT{'comments_' . $a},0,$INPUT{'recurring_' .$a})
		}
		else {
			$status = "Failed -- IP Address entered not found";
		}
	}
	else {
		$status = "Failed -- All fields not filled out";
	}

print <<EOF;

<TR align=left><TD>
<font size=1 face=arial color=white>$status
</TD><TD>
<font size=1 face=arial color=white>$account
</TD><TD>
<font size=1 face=arial color=white>$INPUT{'date_' . $a}
</TD><TD>
<font size=1 face=arial color=white>$INPUT{'trans_' . $a}
</TD><TD>
<font size=1 face=arial color=white>\$$INPUT{'total_' . $a}
</TD><TD>
<font size=1 face=arial color=white>\$$INPUT{'comish_' . $a}
</TD><TD>
<font size=1 face=arial color=white>$INPUT{'comments_' . $a}
</TD><TD>
<font size=1 face=arial color=white>$INPUT{'recurring_' . $a}
</TD></TR>
EOF
	
	$a++;
}

print "</TABLE>";

&Bottom;
exit;
}

########## VIEW recurring ##########
sub view_reoccur {
&checkpassword;

open (PASSWORD, "$path/data/reoccur.txt");
if ($flock){
	flock PASSWORD,2;
}
@dailys = <PASSWORD>;
close (PASSWORD);

print <<EOF;
<FORM METHOD=POST ACTION="$cgiurl"><input type="Hidden" name="password" value="$INPUT{'password'}">
<table cellspacing =0  border=1 cellpadding =4>
<TR bgcolor=$table_head_bg align=center><TD colspan=11><FONT size="$font_size" face="$font_face" color=$text_table_head>Purchase log for $message</FONT>
</TD></TR>
<TR bgcolor=$table_head_bg align=center>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Stop<BR>Reoccurs</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Delete</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Edit</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Account</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Date<BR>Started</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Last<BR>Reoccur</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Next<BR>Reoccur</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Trans</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Total \$</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Commision</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table_head>Comments</FONT></TD>
</TR>
EOF

@colors=($alt1,$alt2);
$color=0;
foreach $entry(@dailys) {
	@out = split(/\|/,$entry);
	unless ($color) {
		$color=1;
	}
	else {
		$color=0;
	}
	print "<TR bgcolor=$colors[$color]>";

	unless ($out[7] eq "Inactive") {
		@dreo = split(/\./,$out[7]);
		$mreo = $dreo[0] + 1;
		$yreo = $dreo[2];
		if ($mreo == 13) {
			$mreo = 1;
			$yreo = $yreo+1;
		}
		$nextreo = "$mreo.$dreo[1].$yreo";
	}
	else {
		$nextreo = " -- ";
	}
print <<EOF;
<TD align=center><input type="Checkbox" name="reoccur_stop" value="$out[0]"></TD>
<TD align=center><input type="Checkbox" name="purchase_delete" value="$out[0]"></TD>
<TD align=center><input type="Checkbox" name="purchase_edit" value="$out[0]"></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table>$out[1]</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table>$out[2]</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table>$out[7]</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table>$nextreo</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table>$out[3]</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table>\$$out[4]</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table>\$$out[5]</FONT></TD>
<TD><FONT size="$font_size" face="$font_face" color=$text_table>$out[6]</FONT></TD>
</TR>
EOF

}

print "<TR bgcolor=$table_head_bg align=center><TD colspan=11><input type=\"Submit\" name=\"acc_edit_purch\" value=\"Delete/Edit/Stop\"></TD></TR></TABLE></FORM><BR><BR>";

&Bottom;
exit;
}

########## BACK UP DATA ##########
sub backup {
&checkpassword;

$count=0;

unless ( ( -e "$path/data_backup" ) ) {
	mkdir("$path/data_backup" ,0777) || &error("Can't make $path/data dir");
}

opendir (DIR, "$path/data_backup");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data_backup/$file");
}

opendir (DIR, "$path/data");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data_backup/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

unless ( ( -e "$path/data_backup/accounts" ) ) {
	mkdir("$path/data_backup/accounts" ,0777) || &error("Can't make $path/accounts dir");
}

opendir (DIR, "$path/data_backup/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data_backup/accounts/$file");
}

opendir (DIR, "$path/data/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data/accounts/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data_backup/accounts/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

unless ( ( -e "$path/data_backup/temp" ) ) {
	mkdir("$path/data_backup/temp" ,0777) || &error("Can't make $path/temp dir");
}


opendir (DIR, "$path/data_backup/temp");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data_backup/temp/$file");
}

opendir (DIR, "$path/data/temp");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data/temp/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data_backup/temp/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

unless ( ( -e "$path/data_backup/paid" ) ) {
	mkdir("$path/data_backup/paid" ,0777) || &error("Can't make $path/paid dir");
}


opendir (DIR, "$path/data_backup/paid");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data_backup/paid/$file");
}

opendir (DIR, "$path/data/paid");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data/paid/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data_backup/paid/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}
unless ( ( -e "$path/data_backup/logs" ) ) {
	mkdir("$path/data_backup/logs" ,0777) || &error("Can't make $path/logs dir");
}


opendir (DIR, "$path/data_backup/logs");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data_backup/logs/$file");
}

opendir (DIR, "$path/data/logs");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data/logs/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data_backup/logs/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

unless ( ( -e "$path/data_backup/daily" ) ) {
	mkdir("$path/data_backup/daily" ,0777) || &error("Can't make $path/daily dir");
}


opendir (DIR, "$path/data_backup/daily");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data_backup/daily/$file");
}

opendir (DIR, "$path/data/daily");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data/daily/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data_backup/daily/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}
unless ( ( -e "$path/data_backup/purch" ) ) {
	mkdir("$path/data_backup/purch" ,0777) || &error("Can't make $path/purch dir");
}


opendir (DIR, "$path/data_backup/purch");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data_backup/purch/$file");
}

opendir (DIR, "$path/data/purch");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data/purch/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data_backup/purch/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

	open(LIST,">$path/data_backup/backup.txt");
	print LIST $now;
	close(LIST);

&main("$count data files backed up");


}
########## BACK UP DATA ##########
sub restore {
&checkpassword;

$count=0;

opendir (DIR, "$path/data");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data/$file");
}

opendir (DIR, "$path/data_backup");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data_backup/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}


opendir (DIR, "$path/data/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data/accounts/$file");
}

opendir (DIR, "$path/data_backup/accounts");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data_backup/accounts/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data/accounts/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

opendir (DIR, "$path/data/temp");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data/temp/$file");
}

opendir (DIR, "$path/data_backup/temp");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data_backup/temp/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data/temp/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

opendir (DIR, "$path/data/paid");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data/paid/$file");
}

opendir (DIR, "$path/data_backup/paid");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data_backup/paid/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data/paid/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

opendir (DIR, "$path/data/logs");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data/logs/$file");
}

opendir (DIR, "$path/data_backup/logs");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data_backup/logs/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data/logs/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

opendir (DIR, "$path/data/daily");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data/daily/$file");
}

opendir (DIR, "$path/data_backup/daily");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data_backup/daily/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data/daily/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

opendir (DIR, "$path/data/purch");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@file) {
	unlink("$path/data/purch/$file");
}

opendir (DIR, "$path/data_backup/purch");
@files = grep { /.txt/ } readdir(DIR);
close (DIR); 

foreach $file(@files) {
	open(LIST,"$path/data_backup/purch/$file");
	@nfile = <LIST>;
	close(LIST);
	open(LIST,">$path/data/purch/$file");
	print LIST @nfile;
	close(LIST);
	$count++;
}

	open(LIST,"$path/data_backup/backup.txt");
	$backup = <LIST>;
	close(LIST);

&main("$count data files from <FONT COLOR=red>$backup</font> restored");


}

########## HEADER ##########
sub Top {
unless ($comish_name) {
	$comish_names = "The Comish";
}
else {
	$comish_names = $comish_name;
}

print <<EOF;
<HTML><HEAD><TITLE>$comish_names</TITLE></HEAD>
<BODY BGCOLOR="white" TEXT="#000000" link=white vlink=white><CENTER>
<table cellspacing =0 bgcolor =SlateBlue border=1 cellpadding =10>
<TR align=center><TD>
<FONT FACE="Arial" color=white><B>$comish_names Admin.</B></FONT>
</TD></TR></TABLE><BR>
<BR>
EOF

}

########### MAIN FOOTER ##########
sub Bottom {
print <<EOF;
<BR><BR><table cellspacing =0 bgcolor =SlateBlue border=1 cellpadding =3>
<TR align=center><TD><FONT SIZE="-1" FACE="Arial">
<BR>
<form action="$cgiurl" method=post>
<input type="Hidden" name="password" value="$INPUT{'password'}">
<input type="Submit" name="main" value="Return to main admin">
</FORM>
<FONT SIZE="-1" FACE="Arial" color=white>
<A HREF="http://solutionscripts.com">The Comish</A> v$version<br>Created by <A HREF="http://solutionscripts.com">Solution Scripts</A></TD></TR></TABLE>
</FONT></BODY></HTML>
EOF
}

########## LOGIN FOOTER #########
sub Footer {
print <<EOF;
<BR><BR><table cellspacing =0 bgcolor =SlateBlue border=1 cellpadding =3>
<TR align=center><TD><FONT SIZE="-1" FACE="Arial" color=white>
<A HREF="http://solutionscripts.com">The Comish</A> v$version<br>Created by <A HREF="http://solutionscripts.com">Solution Scripts</A></TD></TR></TABLE>
</FONT></BODY></HTML>
EOF
}

########## ERROR REPORT ##########
sub error{
$errors = $_[0] ;
print <<EOF;
<table cellspacing =0 bgcolor =#00416B border=1 cellpadding =8>
<TR bgcolor=#E4E4E4 align=center><TD><FONT SIZE="-1" FACE="Arial"><B>An error has occured</B><BR><BR>
The error is: <font color=red>$errors</font><br><br>
<font color=red>$!</FONT></FONT></TD></TR></TABLE>
EOF
&Bottom;
exit;
}

########## CHECK PASSWORD #########
sub checkpassword {

open (PASSWORD, "$path/data/password.txt") || &error("Unable to open $path/data/password.txt");
$passwords = <PASSWORD>; 
close (PASSWORD);

if ($INPUT{'pass'}) {
	$newpassword = crypt($INPUT{'pass'}, $passwords);
	$INPUT{'password'} = $INPUT{'pass'};
	unless ($newpassword eq $passwords) {
print <<EOF;
<table cellspacing =0 bgcolor =slateblue border=1 cellpadding =8>
<TR align=center><TD><FONT SIZE="-1" FACE="Arial" color=white>Access denied<BR><BR>You entered the wrong password 
</TD></TR></TABLE>
EOF
		&Footer;
		exit;
	}
}
elsif ($INPUT{'password'}) {
	$INPUT{'pass'} = $INPUT{'password'};
	$newpassword = crypt($INPUT{'password'}, $passwords);
	unless ($newpassword eq $passwords) {
print <<EOF;
<table cellspacing =0 bgcolor =slateblue border=1 cellpadding =8>
<TR align=center><TD><FONT SIZE="-1" FACE="Arial" color=white>Access Denied<BR><BR>You entered the wrong password 
</TD></TR></TABLE>
EOF
		&Footer;
		exit;
	}
}
else {
print <<EOF;
<table cellspacing =0 bgcolor=slateblue border=1 cellpadding =8>
<TR align=center><TD><FONT SIZE="-1" FACE="Arial" color=white>You must enter a password
</TD></TR></TABLE>
EOF
	&Footer;
	exit;
}

}

########## SETUP ##########
sub setup_paths {

unless ( ( -e "$path/data" ) ) {
mkdir("$path/data" ,0777) || &error("Can't make $path/data dir");
#chown(1,1,"$path/data") || &error("Can't chown $path/data dir");
}
unless ( ( -e "$path/data/accounts" ) ) {
mkdir("$path/data/accounts" ,0777) || &error("Can't make $path/accounts dir");
#chown(1,1,"$path/data/accounts") || &error("Can't chown $path/data/accounts dir");
}
unless ( ( -e "$path/data/temp" ) ) {
mkdir("$path/data/temp" ,0777) || &error("Can't make $path/temp dir");
#chown(1,1,"$path/data/temp") || &error("Can't chown $path/data/temp dir");
}
unless ( ( -e "$path/data/paid" ) ) {
mkdir("$path/data/paid" ,0777) || &error("Can't make $path/paid dir");
#chown(1,1,"$path/data/paid") || &error("Can't chown $path/data/paid dir");
}
unless ( ( -e "$path/data/logs" ) ) {
mkdir("$path/data/logs" ,0777) || &error("Can't make $path/logs dir");
#chown(1,1,"$path/data/logs") || &error("Can't chown $path/data/logs dir");
}
unless ( ( -e "$path/data/daily" ) ) {
mkdir("$path/data/daily" ,0777) || &error("Can't make $path/daily dir");
#chown(1,1,"$path/data/daily") || &error("Can't chown $path/data/daily dir");
}
unless ( ( -e "$path/data/purch" ) ) {
mkdir("$path/data/purch" ,0777) || &error("Can't make $path/purch dir");
#chown(1,1,"$path/data/purch") || &error("Can't chown $path/data/purch dir");
}

}

sub demo {

print <<EOF;
<html>
<head>
<title>Error</title>
</head>
<body>
<TABLE border=1 bgcolor=Gainsboro><TR align=left><TD>
<font face="Arial" size="-1"><center>
<B>The Comish Feature Disabled<BR><BR>
<BR><BR>
Because this is a demo open to the public, some features must be disabled<BR>
should you have any questions about this feature, please email us <a href="mailto:solutions\@solutionscripts.com">solutions\@solutionscripts.com</a>
<BR><BR>
</TD></TR></TABLE>
</BODY></HTML>
EOF
&Footer;
exit;
}