function ShowHelpForSimpleCalc(divID, desc)
{
        DivObj = document.getElementById(divID);
        DivObj.style.display = 'inline';
        DivObj.style.position = 'absolute';
        DivObj.style.width = '170';
        DivObj.style.backgroundColor = 'lightyellow';
        DivObj.style.border = 'dashed 1px black';
        DivObj.style.padding = '10px';
        DivObj.innerHTML = '<div style="padding: 10px 10px 10px 10px;">' + desc + '</div>';
}

function OpenWinForPrintOfSimpleChart()
{
        var content = document.getElementById('SimpleAmortChart').innerHTML;
        top.consoleRef = window.open('','SimpleCalCultaorConsole', 'width=350,height=250,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1')
        top.consoleRef.document.writeln('<html><head><title>Simple Calculator Amortization ChartConsole</title><style type="text/css">.simplecalctableformat{ border-color:#666666;}.simplecalctablecellformat{ border:0;}.simplecalctableheading{ color:#000066; font-weight:bold; font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif; font-size: 14px; border:0;}.simplecalcbutton{ border: 1px solid white; background-color: #31659C; color: white; font-size: 10px; font-weight: bold;}.simplecalctext{ font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif; font-size: 10px;}.simplecalcsyndicate{ text-decoration:none; color:#999; font-family:sans-serif; font-size:11px;}</style></head><body bgcolor=white onLoad="self.focus(); window.print();">'+content+'</body></html>');
        top.consoleRef.document.close();
}

function HideHelpForSimpleCalc(divID)
{
        DivObj = document.getElementById(divID);
        DivObj.style.display = 'none';
}

function FnCheckAndCalculate(frmObj)
{
        var _msgText = '';
        var LoanAmount, Year, Month, InterestRate, Points, PointsValue, PaymentFrequency;
        //This is to check if value is given
        if(frmObj.LoanAmount.value=='')
        {
                _msgText += "* Please give the asking price.\n";
        }
        else if(isNaN(LoanAmount=parseFloat(frmObj.LoanAmount.value)))
        {
                _msgText += "* Please give the asking price in digits only.\n";
        }
	if(frmObj.DownPaymentAmount.value=='')
        {
                _msgText += "* Please give the Down payment amount.\n";
        }
	else if(isNaN(DownPaymentAmount=parseFloat(frmObj.DownPaymentAmount.value)))
	{
		_msgText += "* Please give the down payment amount in digits only.\n";
	}
        if(frmObj.Year.value=='' && frmObj.Month.value==0)
        {
                _msgText += "* Please provide the term of loan(Loan period).\n";
        }
        else if(isNaN(Year=parseInt(frmObj.Year.value)))
        {
                _msgText += "* Please give year in digits only.\n";
        }
        if(frmObj.InterestRate.value=='')
        {
                _msgText += "* Please give the interest rate.\n";
        }
        else if(isNaN(InterestRate=parseFloat(frmObj.InterestRate.value)))
        {
                _msgText += "*  Please give interest rate in  digits only.\n";
        }

        Month = parseInt(frmObj.Month.options[frmObj.Month.selectedIndex].value);

        if(_msgText=='')
        {
		LoanAmount = LoanAmount - DownPaymentAmount;
                FnCalculateTheInterest(LoanAmount, Year, Month, InterestRate);
        }
        else
        {
                alert(_msgText);
        }

        return false;
}

function FnCalculateTheInterest(LoanAmount, Year, Month, InterestRate)
{
        var NumberOfMonths = (Year * 12)+Month;
        var InterestRatePerMonth = ((InterestRate/100)/12);
        var tmp = (1+InterestRatePerMonth);
        var InterestPerMonth = Math.pow(tmp,NumberOfMonths);
        var MonthlyInstallment = ((LoanAmount * InterestPerMonth * InterestRatePerMonth) / (InterestPerMonth - 1));

        if(LoanAmount <= 0 || InterestRatePerMonth <= 0 || NumberOfMonths <= 0)
        {
                 var HtmlToDisplay ='<div align="center"><table><tr><td>Sorry, invalid data.</td></tr><table></div>';
        }

        var HtmlToDisplay = '<table width="100%" border="0" cellpadding="2" cellspacing="0" bgcolor="#EAEAEA">';
	HtmlToDisplay += '<tr><td width="45%" class="simplecalctext"><b>Monthly Installment</b></td><td width="70%" class="simplecalctext"><b>: $ '+(Math.round(MonthlyInstallment*100)/100)+'</b></td></tr>';

        HtmlToDisplay += '<tr><td colspan="2" align="center" class="simplecalctext"><input type="button" class="simplecalcbutton" name="Amortization" value="Amortization Chart" onclick="javascript:FnCalculateTheAmortizationChart('+LoanAmount+', '+InterestRate+', '+InterestRatePerMonth+', '+NumberOfMonths+', '+MonthlyInstallment+');"></td></tr></table>';

        document.getElementById('SimplePrintRst').innerHTML             = '<img src="http://www.mortgagefit.com/styles/mortgage/img/print.gif" style="cursor: pointer;" onclick="javascript:alert(\'Please calculate the Amortization before you print.\');" alt="" border="0">&nbsp;&nbsp;';
        document.getElementById('SimpleInterestRate').innerHTML         = HtmlToDisplay;
        document.getElementById('SimpleAmortChart').innerHTML           = '';
        document.getElementById('SimpleAmortChart').style.overflow      = '';
        return false;
}

function FnCalculateTheAmortizationChart(LoanAmount, InterestRate, InterestRatePerMonth, NumberOfMonths, MonthlyInstallment)
{
        var InterestPaid = 0;
        var PrincipalPaid = 0;

        var AmortizationTable = '<table width="100%" cellspacing="0" cellpadding"0"><tr><td align="center" colspan="5" class="simplecalctext"><b>Simple Loan Payment - Amortization Chart</b><br /><hr /></td></tr>';
        AmortizationTable += '<tr><td class="simplecalctext"><b>Pmt #</b></td><td class="simplecalctext"><b>Date</b></td><td class="simplecalctext"><b>Princ Paid</b></td><td class="simplecalctext"><b>Int Paid</b></td><td class="simplecalctext"><b>Balance</b></td></tr>';

        var DateString = '';
        var Count = 0;
        var TodayDate = new Date();
	var lastYear = TodayDate.getFullYear();
	var lastmonth = TodayDate.getMonth();
	var CurrentDay = TodayDate.getDate();
        while(LoanAmount > 0)
        {
                InterestPaid =  (InterestRate * LoanAmount)/(1200);
                PrincipalPaid = MonthlyInstallment - InterestPaid;
                LoanAmount = ((LoanAmount - MonthlyInstallment)>0) ? (LoanAmount - MonthlyInstallment) + InterestPaid : (LoanAmount - MonthlyInstallment);

		Count++;
		if(lastmonth>=12)
		{
			lastmonth = 1;
			lastYear  = lastYear + 1;
		}
		else
		{
			lastmonth++;
		}
                DateString = lastYear + "-" + lastmonth + "-" + CurrentDay;
                AmortizationTable += '<tr><td class="simplecalctext">'+Count+'</td><td class="simplecalctext">'+DateString+'</td><td class="simplecalctext">'+(Math.round(PrincipalPaid*100)/100)+'</td><td class="simplecalctext">'+(Math.round(InterestPaid*100)/100);

                if(LoanAmount <= 0)
                {
                           AmortizationTable +='</td><td class="simplecalctext"> <b>Paid</b>';
                }
                else
                {
                          AmortizationTable +='</td><td class="simplecalctext">'+(Math.round(LoanAmount*100)/100);
                }
                AmortizationTable +='</td></tr>';
        }
        AmortizationTable += '<tr><td colspan="5"><hr /></td></tr>';

        document.getElementById('SimplePrintRst').innerHTML   = '<img src="http://www.mortgagefit.com/styles/mortgage/img/print.gif" style="cursor: pointer;" onclick="javascript:OpenWinForPrintOfSimpleChart();" alt="" border="0">&nbsp;&nbsp;';
        document.getElementById('SimpleAmortChart').style.overflow = 'scroll';
        document.getElementById('SimpleAmortChart').innerHTML = AmortizationTable;
}

function ResetCalculator()
{
        document.getElementById('SimplePrintRst').innerHTML             = '<img src="http://www.mortgagefit.com/styles/mortgage/img/print.gif" style="cursor: pointer;" onclick="javascript:alert(\'Please fill the form before you print.\');" alt="" border="0">&nbsp;&nbsp;';
        document.getElementById('SimpleInterestRate').innerHTML         = FnGetInitialCalculatorHtmlCode();
        document.getElementById('SimpleAmortChart').innerHTML           = '';
        document.getElementById('SimpleAmortChart').style.overflow      = '';
}

function FnGetInitialCalculatorHtmlCode()
{
    var InitialCalculatorHtmlCode = '<table width="100%" border="0" cellpadding="2" cellspacing="0" bgcolor="#EAEAEA"><tr><td width="45%" align="left" class="simplecalctext"><b>Monthly Installment</b></td></tr><tr><td width="30%" align="center" class="simplecalctext"><input type="button" name="Amortization" class="simplecalcbutton" value="Amortization Chart" onclick="javascript:alert(\'Please use calculator to calculate the Monthly installment.\')"></td></tr></table>';
    return InitialCalculatorHtmlCode;
}


function FnCreateCalculatorForm()
{
        if(document.getElementById('SimpleCalculatorHomeLink')=='http://www.mortgagefit.com/')
        {
                var HtmlCodeForForm = '';
                HtmlCodeForForm += '<div id="SmplCalBody" align="center"><table width="175" class="simplecalctableformat" border="1">';
        	HtmlCodeForForm += '<tr><td colspan="2" align="center" class="simplecalctableheading" bgcolor="#EAEAEA">Simple Loan Payment Calculator</td></tr>';
        	HtmlCodeForForm += '<tr><td width="50%" class="simplecalctablecellformat"><form style="display:inline;" name="simple" action="" onsubmit="return FnCheckAndCalculate(this);" method="post">';
                HtmlCodeForForm += '<table width="100%" border="0" cellpadding="3" cellspacing="3">';
                HtmlCodeForForm += '<tr><td width="30%" class="simplecalctext" align="right"><b><small>Asking Price</small></b><div style="display:none; width: 150px;text-align:left;" id="help0"></div></td><td width="20%"><input type="text" size="10" value="100000" maxlength="15" name="LoanAmount"></td><td><span onmouseover="javascript:ShowHelpForSimpleCalc(\'help0\', \'The amount required by the buyer to purchase the home.\');" onmouseout="javascript:HideHelpForSimpleCalc(\'help0\')" style="font-size: 11px; font-weight: bold; cursor: pointer; color: blue;">?</span></td></tr>';
                HtmlCodeForForm += '<tr><td width="30%" class="simplecalctext" align="right"><b><small>Down Payment</small></b><div style="display:none; width: 150px;text-align:left;" id="help1"></div></td><td width="20%"><input type="text" size="10" value="10000" maxlength="15" name="DownPaymentAmount"></td><td><span onmouseover="javascript:ShowHelpForSimpleCalc(\'help1\', \'Amount of money put forward by the buyer towards the asking price of a home.\');" onmouseout="javascript:HideHelpForSimpleCalc(\'help1\')" style="font-size: 11px; font-weight: bold; cursor: pointer; color: blue;">?</span></td></tr>';
                HtmlCodeForForm += '<tr><td width="30%" class="simplecalctext" align="right"><b><small>Interest Rate</small></b><div style="display:none; width: 150px;text-align:left;" id="help2"></div></td><td width="20%"><input type="text" size="10" value="6.5" maxlength="4" name="InterestRate"> %</td><td><span onmouseover="javascript:ShowHelpForSimpleCalc(\'help2\', \'The cost of borrowing money for a certain period of time. It is expressed as a percentage of the loan amount.\');" onmouseout="javascript:HideHelpForSimpleCalc(\'help2\')" style="font-size: 11px; font-weight: bold; cursor: pointer; color: blue;">?</span></td></tr>';
                HtmlCodeForForm += '<tr><td width="30%" class="simplecalctext" align="right"><b><small>Term in <small></b><div style="display:none; width: 150px;text-align:left;" id="help2"></div></td><td width="20%">';
                HtmlCodeForForm += '<table width="100%" cellpadding="0" cellspacing="0">';
                HtmlCodeForForm += '<tr><td><input name="Year" type="text" size="3" maxlength="3" value="30"></td><td nowrap><small> Year(s)</small></td>';
                HtmlCodeForForm += '<td><select name="Month"><option selected value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option></select></td><td nowrap><small>Month(s)</small></td></tr></table></td><td><span onmouseover="javascript:ShowHelpForSimpleCalc(\'help2\', \'Time period over which you will have to pay back the loan.\');" onmouseout="javascript:HideHelpForSimpleCalc(\'help2\')" style="font-size: 11px; font-weight: bold; cursor: pointer; color: blue;">?</span></td></tr>';
                HtmlCodeForForm += '<tr><td width="30%">&nbsp;</td><td width="20%" colspan="2"><input type="submit" class="simplecalcbutton" name="Calculate" value="Calculate"><input type="reset" name="Reset" value="Reset" class="simplecalcbutton" onclick="javascript:ResetCalculator();"></td></tr>';

                HtmlCodeForForm += '</table></form></div></td></tr><tr><td valign="top" class="simplecalctext" style="border:none;"><div id="SimpleInterestRate">'+FnGetInitialCalculatorHtmlCode()+'</div></td></tr>';
                HtmlCodeForForm += '<tr><td colspan="2" class="simplecalctablecellformat" align="right" id="SimplePrintRst"><img src="http://www.mortgagefit.com/styles/mortgage/img/print.gif" style="cursor: pointer;" onclick="javascript:alert(\'Please fill the form before you print.\');" alt="" border="0">&nbsp;&nbsp;</td></tr>';
                HtmlCodeForForm += '<tr><td colspan="2" align="left" class="simplecalctablecellformat"><div id="SimpleAmortChart" style="border: 1px solid #DDDDDD; width:335px; height: 120px;"></div></td></tr></table></div>';

                document.getElementById('SimpleCalculatorHomeLink').className = 'simplecalcsyndicate';
                document.getElementById('SimpleCalculatorSection').innerHTML = HtmlCodeForForm;
        }
        else
        {
                document.getElementById('SimpleCalculatorSection').innerHTML = 'To enable this calculator, please paste the full code as described at <a href="http://www.mortgagefit.com/simplecalcsyndicate/simple-calculator.html">MortgageFit</a>';
        }
}
document.write('<style type="text/css">.simplecalctableformat{ border-color:#666666;}.simplecalctablecellformat{ border:0;}.simplecalctableheading{ color:#000066; font-weight:bold; font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif; font-size: 14px; border:0;}.simplecalcbutton{ border: 1px solid white; background-color: #31659C; color: white; font-size: 10px; font-weight: bold;}.simplecalctext{ font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif; font-size: 10px;}.simplecalcsyndicate{ text-decoration:none; color:#000; font-family:sans-serif; font-size:11px;}</style>');
FnCreateCalculatorForm();
