#!/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 'asi'){
		$hidden = $hidden."<input type=\"hidden\" name=\"inslogo\" value=\"$inslogo\">\n";
	}
	else {
		$inslogo = '';
	}
}

$inslogo2 = '';
if(param("inslogo2") ne ''){
	$inslogo2 = lc(encode_entities(param("inslogo2")));
	if($inslogo2 eq 'ncua' || $inslogo2 eq 'fdic' || $inslogo2 eq 'fdic2' || $inslogo2 eq 'asi'){
		$hidden = $hidden."<input type=\"hidden\" name=\"inslogo2\" value=\"$inslogo2\">\n";
	}
	else {
		$inslogo2 = '';
	}
}

if (param("AG") ne ''){
  ($AG, $HI, $NW) = cleanAndHTMLEncode(param("AG"), param("HI"), param("NW"));
}

print "<title>How Wealthy are You?<\/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 <<Header;
<h2>How Wealthy are You?</h2>
Based on calculations from <b>The Millionaire Next Door</b>
by Thomas J. Stanley and  William D. Danko
<hr>

<form action="wealth.pl" method="post">
<table border=0>
<tr><td>Your Age (or age of primary household breadwinner)
<td><input type="text" name="AG" size="5" value="$AG">
<tr><td>Your total annual household income
<td><input type="text" name="HI" size="10" value="$HI">
<tr><td>Your total net worth
<td><input type="text" name="NW" size="10" value="$NW">
</table><br>
<input type="submit" value="Find Out Where You Stand">
Header
print "<input type=\"hidden\" name=\"bgcolor\" value=\"$bgcolor\">\n";
print $hidden;
print "<\/form>\n";
print "<\/center>\n";

if ($AG ne '')
{
  if(isNumeric($AG) && isNumeric($HI) && isNumeric($NW)){
	  $aaw = sprintf("%8d",$AG * $HI/10);
	  $paw = $aaw * 2;
	  $uaw = sprintf("%8d",$aaw /2);
	  $msg = "You are an AAW";
	  if ($NW < $uaw) { $msg = "You are a UAW"; }
	  if ($NW > $paw) { $msg = "You are a PAW"; }
	  $frac = sprintf("%5.3f",$NW/$aaw);
	  print "<h3>$msg </h3>\n";
	  print <<Explain;
	  <H3>For Your Age and Income, Your Desired Net Worth would be \$ $aaw</h3>
	  <H4>Your Net Worth is $frac of the Desired Net Worth</h4>
	  <table border=4>
	  <tr><td>UAW<td>Under Accumulator of Wealth<td>Net Worth &lt; $uaw
	  <tr><td>AAW<td>Average Accumulator of Wealth<td>$uaw &lt; Net Worth  &lt; $paw
	  <tr><td>PAW<td>Prodigious Accumulator of Wealth<td>Net Worth &gt; $paw
	  </table>
	  <P>
	  </center>
	  Stanley and Danko argue that your total net worth alone does not tell 
	  a good enough
	  picture of one's wealth, what matters is how much worth you can accumulate
	  on your income considering your own age. Therefore, a 30 year old making 
	  \$ 25,000 who has a net worth of \$ 80,000 has more relative wealth than a
	  50 year old doctor making \$ 200,000 a year who is only worth  \$500,000.
Explain
  }
  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>';
print "\n<hr>\n";
if ( $inslogo2 eq 'ncua' ) {
	print '<p align="center"><img src="/includes/inslogos/blueNcua.gif"></p>';
}
elsif ( $inslogo2 eq 'fdic' ) {
	print '<p align="center"><img src="/includes/inslogos/signFDICsm.gif"></p>';
}
elsif ( $inslogo2 eq 'fdic2' ) {
	print '<p align="center"><font face="arial" size="3"><b>Member FDIC</b></font></p>';
}
elsif ( $inslogo2 eq 'asi' ) {
	print '<p align="center"><img src="/includes/inslogos/asilogo.gif"></p>';
}

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;
}
