#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "
APR/Front End Cost Calculator\n";
print "\n";
$args = $ENV{'QUERY_STRING'};
$args =~ s/\+/ /g;
$args =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;
if (length($args) < 2)
{
print <APR Calculator
EmptyForm
}
else
{
@f = split("&",$args);
for ($i=0; $i<=$#f; $i++)
{
$av = @f[$i];
if ($av =~ /^amt/) { $amt = substr($av,4); }
if ($av =~ /^int/) { $int = substr($av,4); }
if ($av =~ /^clc/) { $clc = substr($av,4); }
if ($av =~ /^pts/) { $pts = substr($av,4); }
if ($av =~ /^trm/) { $trm = substr($av,4); }
}
print "APR Calculator Results
";
print "";
print "";
printf("
\n", $amt,$int,$trm);
$mi = $int/1200; $tt = $trm * 12; $m2 = $apr/1200;
$temp = (1.0 / (1 + $mi) ** $tt);
$temp2 = (1.0 / (1 + $m2) ** $tt);
if ($temp < 1) { $paymt = $amt * $mi / (1 - $temp); }
else { $paymt = $amt / $trm; }
printf("| Principal & Interest Monthly Payment : | \$ %9.2f |
\n",$paymt);
printf("| Loan Amount before closing costs: | \$ %12.2f |
\n",$amt);
$pta = $amt * $pts / 100; $costs = $pta + $clc;
printf("| Points at %6.3f %% | \$ %7.2f |
",$pts,$pta);
printf("| Other Costs | \$ %7.2f |
\n",$clc);
printf("| Total up-front costs: | \$ %12.2f |
\n",$costs);
$eamt = $amt + $costs;
printf("| So the effective amount of your loan is: | \$ %12.2f |
\n",$eamt);
$epaymt = $eamt * $mi / (1 - $temp);
printf("| The Monthly Payment for this amount would be: | \$%9.2f |
\n",$epaymt);
$m1 = $mi; $m2 = 2 * $mi;
for ($j=0; $j<25; $j++)
{
$mnew = ($m1 + $m2)/2;
$temp = (1.0 / (1 + $mnew) ** $tt);
$epaymt2 = $amt * $mnew / (1 - $temp);
if ($epaymt2 < $epaymt) { $m1 = $mnew; } else { $m2 = $mnew; }
#printf("%12.7f %12.7f\n",$m1,$m2);
}
$apr = ($m1 + $m2)*600;
printf("|
\n",$apr);
}
print<