#!/usr/bin/perl -w
use CGI qw(:standard);
use HTML::Entities;

if(length($ENV{'QUERY_STRING'}) > 250 || length($ENV{'CONTENT_LENGTH'}) > 3 || $ENV{'CONTENT_LENGTH'} > 250){
	print "Content-type: text/html\n\n";
	print "<html>\n<head>\n";
	print "<title>Error<\/title>\n";
	print "<style>*{font-family: arial; font-size: 12px;}h2{font-size: 16px;}<\/style>\n";
	print "<\/head>\n";
	print "<body bgcolor=\"#FFFFF\">";
	print "Error - data over allowed limits";
	print '</body></html>';
	exit;
}

print "Content-type: text/html\n\n";
print "<html>\n<head>\n";
print "<style>*{font-family: arial; font-size: 12px;}h2{font-size: 16px;}<\/style>\n";

$hidden = '';
if(param("css1") ne ''){
	$stylesheet = encode_entities(param("css1"));
	$hidden = $hidden."<input type=\"hidden\" name=\"css1\" value=\"$stylesheet\">\n";
	print "<link href=\"/" . $stylesheet . "\" rel=\"stylesheet\" type=\"text/css\">";
}

$color="#cccccc";
if(param("color") ne ''){
	$color = "#".encode_entities(param("color"));
	$hidden = $hidden."<input type=\"hidden\" name=\"color\" value=\"$color\">\n";
}

$bgcolor = '';
if(param("bgcolor") ne ''){
	$bgcolor=encode_entities(param("bgcolor"));
}
else {
	$bgcolor='FFFFFF';
}

$banner = '';
if(param("banner") ne ''){
	$banner = encode_entities(param("banner"));
	$hidden = $hidden."<input type=\"hidden\" name=\"banner\" value=\"$banner\">\n";
}

$inslogo = '';
if(param("inslogo") ne ''){
	$inslogo = lc(encode_entities(param("inslogo")));
	if($inslogo eq 'ncua' || $inslogo eq 'fdic' || $inslogo eq 'fdic2' || $inslogo eq 'asi'){
		$hidden = $hidden."<input type=\"hidden\" name=\"inslogo\" value=\"$inslogo\">\n";
	}
	else {
		$inslogo = '';
	}
}

if(param("yrs") eq ''){
print "<title>Annual Yield Calculator<\/title>\n";
print "<\/head>\n";
print "<body bgcolor=\"#$bgcolor\"><center>\n";
if($banner ne '') {
	print "<center><div align=\"center\"><img src=\"".$banner."\" align=\"center\"></div></center>";
}
print<<EOF;
<h2>What's your Yield?</h2>
</center>
Enter your starting balance, your ending balance, your monthly 
contribution and the number of years and this calculator will find what monthly yield would be 
required.
<p>
<hr>
<form action="yield.pl" method="post">
<center><table border=1>
<tr>
<tr><td> Initial Amount
<td><input name="amt" type="text" value="10,000" size="10"> 
<tr><td> Monthly Deposit
<td><input name="dep" type="text" value="100" size="10"> 

<tr><td>Number of Years: 
<td><input name="yrs" type="text" value="20" size="3"> 
<tr><td>Final Amount Desired
<td><input name="fin" type="text" value="100,000" size="10"> 
</table><br>
<input type="submit" value="Calculate Yield">
EOF
print "<input type=\"hidden\" name=\"bgcolor\" value=\"$bgcolor\">\n";
print $hidden;
print "<\/form>\n";
}
else
{
  print "<title>Your Savings</title></head>\n";
  print "<body bgcolor=\"#$bgcolor\">\n";
if($banner ne '') {
	print "<center><div align=\"center\"><img src=\"".$banner."\" align=\"center\"></div></center>";
}
  $tbl = "N";
  ($yrs, $fin, $amt, $dep) = cleanAndHTMLEncode(param("yrs"), param("fin"), param("amt"), param("dep"));
  if(isNumeric($yrs) && isNumeric($fin) && isNumeric($amt) && isNumeric($dep)){
	  $py = 12;
	  if ($cmp =~ /Q/) { $py = 4; }
	  if ($cmp =~ /S/) { $py = 2; }
	  if ($cmp =~ /A/) { $py = 1; }
	  $tt = $yrs * $py; 
	  print "<center><h3>Your Yield Over $yrs Years ";
	  printf ("with monthly deposits of \$ %6.2f </h3>",$dep);
	  printf ("<h3>With an Initial Amount of \$ %9.2f:</h3>",$amt);
	  printf ("<h3>And a Final Amount of \$ %9.2f:</h3>",$fin);

	  $il = 0; $ih = 100;
	  for ($l=0; $l<30; $l++)
	  {
	    $int = ($il + $ih)/2;
	    $mi = $int / ($py * 100); $sav = $amt;
	    for ($i = 0; $i < $yrs*12; $i++)
	    {
	        $sav = $sav * (1 + $mi) + $dep;
	    }
	    if ($sav > $fin) { $ih = $int; } else { $il = $int; }
	  }
	  printf ("<H1>Your Yield : %5.2f %%</H1>\n",($il+$ih)/2);
  }
  else {
	print '<h3>The value(s) entered was not numeric.</h3>';
  }
}

# Added 7-2002 for compliance
# pdecoursey - Liberty Online
print '<p><font face="arial" size="2">The accuracy of this calculator and its applicability to your circumstances is not guaranteed.  Results should be discussed with a qualified professional before any product purchases or loan commitments are made. Using this calculator does not guarantee credit.</font></p>';
if ( $inslogo eq 'ncua' ) {
	print '<p align="center"><img src="/includes/inslogos/blueNcua.gif"></p>';
}
elsif ( $inslogo eq 'fdic' ) {
	print '<p align="center"><img src="/includes/inslogos/signFDICsm.gif"></p>';
}
elsif ( $inslogo eq 'fdic2' ) {
	print '<p align="center"><font face="arial" size="3"><b>Member FDIC</b></font></p>';
}
elsif ( $inslogo eq 'asi' ) {
	print '<p align="center"><img src="/includes/inslogos/asilogo.gif"></p>';
}
print "\n<hr>\n";

print '</body></html>';

sub isNumeric(){
	my ($toTest) = @_;
	if ($toTest =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/){
		return 1;
	}
	else {
		return 0;
	}
}

sub cleanAndHTMLEncode(){
	my (@toClean) = @_;
	my $i = 0;
	for ($i=0; $i<=$#toClean; $i++){
		@toClean[$i] =~ s/,//g;
		@toClean[$i] =~ s/\$//g;
		@toClean[$i] =~ s/\%//g;
		@toClean[$i] = encode_entities(@toClean[$i]);
	}
	return @toClean;
}
