#!/usr/bin/perl
push(@INC,"/www/cgi-bin");

#
# First tell browser that this is an HTML document being printed
#
print "Content-type:text/html\n\n";

#
# The following formats the incoming arguments and puts them into the
# vector $FORM.  Access %FORM with variables $FORM{name}.  Names in
# interest.pl are loan, interest, time, payment
#
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;
    $value =~ s/\n/ /g; # replace newlines with spaces
    $FORM{$name} = $value;
}


#
# Set up the web variables locally
#
$p = $FORM{loan};
$i = $FORM{interest};
$k = $FORM{time};
$m = $FORM{payment};

#
# Start the HTML printing
# Note: \n gives a carriage return and \$ prints out a
# dollar sign (because printing a single $ will try to
# print out a variable
#
print "<html>\n";
print "<head>\n";
print "<title>Results of Interest Calculation</title>\n";
print "</head>\n";
print "<body>\n<center>\n";
print "<p>\n";
print "Loan Amount: \$$p<br>\n";
print "Interest Rate: $i%<br>\n";
print "<p>\n";

#
# Now change interest rate to be used
#
$i = $i / 100;


#
# If the monthly interest is greater than the monthly payment, stop
#
if($m > 0 && (1 + $i / 12) * $p - $m > $p) {
    print "You won't be able to pay off the loan!<br>\n";
    print "Your payments aren't high enough!";
    $k = 0; $m = 0;
}


#
# Go this route if we are to look for the number of payments
#
if($k == 0 && $m > 0) {
	while($p >= 0) {
		$p = (1 + $i / 12) * $p - $m;
  		$k++;
    }
    print "Monthly Payment: \$$m\n<br>";
    print "<b>Number of Payments: $k\n</b>";
}

#
# Go this route if we are to look for the amount of each
# monthly payment.
#
if($m == 0 && $k > 0) {
	if($i == 0) {
		print "Number of Payments: 1<br>\n";
		print "<b>Monthly Payment: \$$p\n";
	}
	else {
		$m = ($p * $i) / (12 * (1 - (1 + $i / 12) ** (-$k)));
		$m = int($m * 100 + 1) / 100;
		print "Number of Payments: $k<br>\n";
		print "<b>Monthly Payment: \$$m</b>\n";
    }
}

#
# Start the printing of the table--the body of the table will be
# printed in the upcoming loop
#
print "<table border=1>\n";
print "<tr>\n";
print "<td>Month</td><td>Interest Paid Off</td><td>New Loan Amount</td>\n";
print "</tr>\n";

#
# Reset variables for web printing, but now we know the time period and
# the payment size for each month.
#
$p = $FORM{loan};
$d = 1;

#
# Run a loop to simulate paying off the loan.  Each month, add the proper
# interest, record the amount of interest, then subtract the monthly
# payment, either the one given from the web or the one found from 
# the previous calculation.  Then find the new value of the loan and 
# repeat while the loan is greater than zero.
#
while($p > 0) {
	$pi = (1 + $i / 12) * $p - $p;
    $pi = int($pi * 100 + 1) / 100;
	    		
	# Total Interest
	$ti += $pi;
	
	$p = (1 + $i / 12) * $p - $m;
	if($p <= 0) {$p = 0;}
	$p = int($p * 100) / 100;

	
	print "\n<tr>\n";
	print "<td>$d</td><td>$pi</td><td>$p</td>\n";
	print "</tr>\n";
	$d++;
}

#
# Set $ti to dollars and cents and print out the rest of the table
# and the end of the HTML document.
#
$ti = int($ti * 100) / 100;
print "</table>\n";
print "<b>Total interest paid: \$$ti</b>";
print "</center>\n</body>\n</html>";
