function ShowHelpForPointsCalc(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 HideHelpForPointsCalc(divID)
{
        DivObj = document.getElementById(divID);
        DivObj.style.display = 'none';
}

function FnPointCheckAndCalculate(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 amount of the Loan.\n";
        }
        else if(isNaN(LoanAmount=parseFloat(frmObj.LoanAmount.value)))
        {
                _msgText += "* Please give the 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";
        }
        if(frmObj.Points.value=='')
        {
                _msgText += "* Please give the points.\n";
        }
        else if(isNaN(Points=parseFloat(frmObj.Points.value)))
        {
                _msgText += "* Please give the points in digits only.\n";
        }
        if(frmObj.PointsValue.value=='')
        {
                _msgText += "* Please give the points value.\n";
        }
        else if(isNaN(PointsValue=parseFloat(frmObj.PointsValue.value)))
        {
                _msgText += "* Please give the points value in digits only.\n";
        }

        Month = parseInt(frmObj.Month.options[frmObj.Month.selectedIndex].value);

        PaymentFrequency = frmObj.PaymentFrequency.value;

        if(_msgText=='')
        {
                FnCalculateTheInterestWithPoint(LoanAmount, Year, Month, InterestRate, Points, PointsValue, PaymentFrequency);
        }
        else
        {
                alert(_msgText);
        }

        return false;
}

function FnCalculateTheInterestWithPoint(LoanAmount, Year, Month, InterestRate, Points, PointsValue, PaymentFrequency)
{
        var EffectiveInterestRate = InterestRate - (Points * PointsValue);
        switch(PaymentFrequency)
        {
                case 'Annual':
                        var PaymentFrequency = Year;
                        var InterestRatePerPayment = (EffectiveInterestRate / 100);
                        var InterestRatePerPaymentWithoutPoint = (InterestRate / 100);
                        break;

                case 'Quarterly':
                        var PaymentFrequency = (Year * 4) + (Month / 4);
                        var InterestRatePerPayment = (EffectiveInterestRate / 100)/4;
                        var InterestRatePerPaymentWithoutPoint = (InterestRate / 100)/4;
                        break;

                case 'Monthly':
                        var PaymentFrequency = (Year * 12) + Month;
                        var InterestRatePerPayment = (EffectiveInterestRate / 100)/12;
                        var InterestRatePerPaymentWithoutPoint = (InterestRate / 100)/12;
                        break;

                case 'Weekly':
                        var PaymentFrequency = (Year * 52) + (Month * 4);
                        var InterestRatePerPayment = (EffectiveInterestRate / 100)/52;
                        var InterestRatePerPaymentWithoutPoint = (InterestRate / 100)/52;
                        break;

                case 'Bi-weekly':
                        var PaymentFrequency = (Year * 26) + (Month * 2)/26;
                        var InterestRatePerPayment = (EffectiveInterestRate / 100)/26;
                        var InterestRatePerPaymentWithoutPoint = (InterestRate / 100)/26;
                        break;
          }

          if(LoanAmount <= 0 || EffectiveInterestRate <= 0 || Year <= 0)
          {
                   var HtmlToDisplay ='<div align="center"><table><tr><td>Sorry, invalid data.</td></tr><table></div>';
          }

          var tmp  = (1+InterestRatePerPayment);
          var tmp1 = (1+InterestRatePerPaymentWithoutPoint);

          var InterestPerPayment = Math.pow(tmp,PaymentFrequency);
          var InterestPerPaymentWithoutPoint = Math.pow(tmp1,PaymentFrequency);

          var Installment = ((LoanAmount * InterestPerPayment * InterestRatePerPayment)
                                        / (InterestPerPayment - 1));
          var InstallmentWithoutPoint = ((LoanAmount * InterestPerPaymentWithoutPoint * InterestRatePerPaymentWithoutPoint)
                                        / (InterestPerPaymentWithoutPoint - 1));

          document.getElementById('EffectiveIntRateID').innerHTML = '<b>'+EffectiveInterestRate+'%</b>';
          document.getElementById('SaveAmountID').innerHTML = 'You can <font color="#BA0B2D"><b>save</b></font> with Points  = $ <font color="#BA0B2D"><b>'+(Math.round(((InstallmentWithoutPoint * PaymentFrequency) - (Installment * PaymentFrequency))*100)/100)+'</b></font><small>';
          document.getElementById('PointAmortChartpointscalcbutton').innerHTML = '<input type="button" name="Amortization" value="Amortization Chart" class="pointscalcbutton" onclick="javascript:FnCalculateTheAmortizationChartPoints('+LoanAmount+','+InterestRate+','+EffectiveInterestRate+','+PaymentFrequency+','+Installment+','+InstallmentWithoutPoint+');">';

          document.getElementById('PrintRst').innerHTML   = '<img src="http://www.mortgagefit.com/styles/mortgage/img/print.gif" style="cursor: pointer;" onclick="alert(\'Please calculate the Amortization before you take printout.\')" alt="" border="0">&nbsp;&nbsp;';
          document.getElementById('PointAmortChart').style.overflow = '';
          document.getElementById('PointAmortChart').innerHTML = '';
          return false;
}

function OpenWinForPrint()
{
        var content = document.getElementById('PointAmortChart').innerHTML;
        top.consoleRef = window.open('','PointCalcConsole', 'width=350,height=250,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1')
        top.consoleRef.document.writeln('<html><head><title>Point Calculator</title><style type="text/css">.pointscalctableformat{ border-color:#666666;}.pointscalctablecellformat{ border:0;}.pointscalctableheading{ color:#000066; font-weight:bold; font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif;font-size: 14px;}.pointscalcbutton { border: 1px solid white; background-color: #31659C; color: white; font-size: 10px; font-weight: bold;}.pointscalctext{font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif;font-size: 10px;}.pointscalcsyndicate{ text-decoration:none; color:#000; 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 FnCalculateTheAmortizationChartPoints(LoanAmount, InterestRate, EffectiveInterestRate, PaymentFrequency, Installment, InstallmentWithoutPoint)
{
        var TotalPayment = Installment * PaymentFrequency;
        var TotalPaymentWithoutPoint = InstallmentWithoutPoint * PaymentFrequency;
        var TotalInterestPaid = TotalPayment - LoanAmount;
        var InterestPaid = 0;
        var PrincipalPaid = 0;
        var HtmlForAmortizationTable = '<table width="100%" border="0" cellpadding="2" cellspacing="2" style="font-size: 12px;">';

        HtmlForAmortizationTable += '<tr><td colspan="5" class="pointscalctext" align="center"><b>Amortization Chart</b><hr /></td></tr>';
        HtmlForAmortizationTable += '<tr><td class="pointscalctext" width="15%"><b><small>Pmt #</small></b></td><td class="pointscalctext"><b><small>Date</small></b></td><td class="pointscalctext"><b><small>Princ Paid</small></b></td><td class="pointscalctext"><b><small>Int Paid</small></b></td><td class="pointscalctext"><b><small>Balance</small></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 =  (EffectiveInterestRate * LoanAmount)/(1200);
                  PrincipalPaid = Installment - InterestPaid;
                  LoanAmount = ((LoanAmount - Installment)>0) ? (LoanAmount - Installment) + InterestPaid : (LoanAmount - Installment);
                  Count++;
		  if(lastMonth>=12)
                  {
                        lastMonth = 1;
                        lastYear  = lastYear + 1;
                  }
                  else
                  {
                        lastMonth++;
                  }
		  var DateString = lastYear + "-" + lastMonth + "-" + CurrentDay;

                  HtmlForAmortizationTable +='<tr><td width="10%" class="pointscalctext">'+Count+'</td><td width="30%" class="pointscalctext">'+DateString+'</td><td width="20%" class="pointscalctext">'+(Math.round(PrincipalPaid*100)/100)+'</td><td width="20%" class="pointscalctext">'+(Math.round(InterestPaid*100)/100)+'</td>';
                  if(LoanAmount <= 0)
                  {
                             HtmlForAmortizationTable +='<td width="20%" class="pointscalctext"><b> Paid </b></td>';
                  }
                  else
                  {
                            HtmlForAmortizationTable +='<td width="20%" class="pointscalctext">'+(Math.round(LoanAmount*100)/100)+'</td>';
                  }
                  HtmlForAmortizationTable +='<tr>';
        }
        HtmlForAmortizationTable +='<tr><td colspan="5" align="left" class="pointscalctext"><hr></td></tr></table>';
        document.getElementById('PrintRst').innerHTML   = '<img src="http://www.mortgagefit.com/styles/mortgage/img/print.gif" style="cursor: pointer;" onclick="javascript:OpenWinForPrint();" alt="" border="0">&nbsp;&nbsp;';
        document.getElementById('PointAmortChart').style.overflow = 'scroll';
        document.getElementById('PointAmortChart').innerHTML = HtmlForAmortizationTable;
}

function ResetPointCalculator()
{
        document.getElementById('EffectiveIntRateID').innerHTML   = '';
        document.getElementById('SaveAmountID').innerHTML   = 'You can <font color="#BA0B2D"><b>save</b></font> with Points';
	document.getElementById('PointAmortChartpointscalcbutton').innerHTML   = '<input type="button" name="Amortization" value="Amortization Chart" class="pointscalcbutton" onclick="javascript:alert(\'Please use calculator to calculate the Effective interest rate.\')">';

        document.getElementById('PrintRst').innerHTML   = '<img src="http://www.mortgagefit.com/styles/mortgage/img/print.gif" style="cursor: pointer;" onclick="alert(\'Please fill the form before you take printout.\')" alt="" border="0">&nbsp;&nbsp;';
        document.getElementById('PointAmortChart').innerHTML      = '';
        document.getElementById('PointAmortChart').style.overflow = '';
}

function FnGetInitialPointCalculatorHtmlCode()
{
    var InitialCalculatorHtmlCode = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td id="SaveAmountID" class="pointscalctext">You can <font color="#BA0B2D"><b>save</b></font> with Points</td></tr>';
    InitialCalculatorHtmlCode += '<tr><td class="pointscalctext">Effective Interest Rate :<span id="EffectiveIntRateID"></span></td></tr>';
    InitialCalculatorHtmlCode += '<tr><td align="center" id="PointAmortChartpointscalcbutton"><input type="button" name="Amortization" value="Amortization Chart" class="pointscalcbutton" onclick="javascript:alert(\'Please use calculator to calculate the Effective interest rate.\')"></td></tr></table>';
    return InitialCalculatorHtmlCode;
}


function FnCreatePointCalculatorForm()
{
        if(document.getElementById('PointsCalculatorHomeLink')=='http://www.mortgagefit.com/')
        {
                var HtmlCodeForForm = '';
                HtmlCodeForForm += '<div align="center">';
                HtmlCodeForForm += '<table width="175" border="1" class="pointscalctableformat">';
                HtmlCodeForForm += '<tr><td align="center" bgcolor="#EAEAEA" class="pointscalctablecellformat"><font class="pointscalctableheading">Points Calculator</font></td></tr>';
                HtmlCodeForForm += '<tr><td class="pointscalctablecellformat"><form name="pointsCalculator" action="" onsubmit="return FnPointCheckAndCalculate(this)" method="POST">';
                HtmlCodeForForm += '<table width="100%" border="0" cellpadding="2" cellspacing="0">';
                HtmlCodeForForm += '<tr><td align="right" class="pointscalctext" width="30%"><b>Asking Price</b><div style="display:none; width: 140px;text-align:left;" id="pointhelp0"></td><td width="15%"><input type="text" name="LoanAmount" value="100000" class="textbox"></td><td><span onmouseover="javascript:ShowHelpForPointsCalc(\'pointhelp0\', \'The amount borrowed or the unpaid loan balance on which interest is charged.\');" onmouseout="javascript:HideHelpForPointsCalc(\'pointhelp0\')" style="font-size: 11px; font-weight: bold; cursor: pointer; color: blue;">?</span></td></tr>';
                HtmlCodeForForm += '<tr><td align="right" class="pointscalctext" width="30%"><b>Term in</b><div style="display:none; width: 140px;text-align:left;" id="pointhelp1"></div></td><td width="15%"><table width="100%" cellpadding="0" cellspacing="0"><tr><td width="12%"><input type="text" name="Year" value="30" size="2" maxlength="2"></td><td class="pointscalctext" width="18%" nowrap><small>Year(s)</small></td>';
                HtmlCodeForForm += '<td width="18%"><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 class="pointscalctext" nowrap><small>Month(s)</small></td></tr></table></td><td><span onmouseover="javascript:ShowHelpForPointsCalc(\'pointhelp1\', \'Time period over which you will have to pay back the loan.\');" onmouseout="javascript:HideHelpForPointsCalc(\'pointhelp1\')" style="font-size: 11px; font-weight: bold; cursor: pointer; color: blue;">?</span></td></tr>';
                HtmlCodeForForm += '<tr><td align="right" class="pointscalctext" width="30%"><b>Interest Rate</b><div style="display:none; width: 140px;text-align:left;" id="pointhelp2"></div></td><td width="15%"><input type="text" name="InterestRate" value="6.5" class="textbox"></td><td><span onmouseover="javascript:ShowHelpForPointsCalc(\'pointhelp2\', \'The cost of borrowing money for a certain period of time. It is expressed as a percentage of the loan amount.\');" onmouseout="javascript:HideHelpForPointsCalc(\'pointhelp2\')" style="font-size: 11px; font-weight: bold; cursor: pointer; color: blue;">?</span></td></tr>';
                HtmlCodeForForm += '<tr><td align="right" class="pointscalctext" width="30%"><b>Discount Points</b><div style="display:none; width: 140px;text-align:left;" id="pointhelp3"></div></td><td width="15%"><input type="text" name="Points" value="1" class="textbox"></td><td><span onmouseover="javascript:ShowHelpForPointsCalc(\'pointhelp3\', \'Percentage of the loan amount which the borrower pays in order to reduce the mortgage rate. 1 point is equal to 1% of the loan amount.\');" onmouseout="javascript:HideHelpForPointsCalc(\'pointhelp3\')" style="font-size: 11px; font-weight: bold; cursor: pointer; color: blue;">?</span></td></tr>';
                HtmlCodeForForm += '<tr><td align="right" class="pointscalctext" width="30%"><b>Points Value</b></td><td width="15%"><input type="text" name="PointsValue" size="15" value="0.125"></td><td>&nbsp;</td></tr>';
                HtmlCodeForForm += '<tr><td align="right" class="pointscalctext" width="30%"><b>Payment Frequency</b></td><td width="15%">';
                HtmlCodeForForm += '<select name="PaymentFrequency"><option value="Annual">Annual</option><option value="Quarterly">Quarterly</option><option selected value="Monthly">Monthly</option><option value="Weekly">Weekly</option><option value="Bi-weekly">Bi-weekly</option></select></td><td>&nbsp;</td></tr>';
                HtmlCodeForForm += '<tr><td colspan="3" align="center"><input type="submit" name="Submit" value="Calculate" class="pointscalcbutton"><input type="reset" name="Reset" value="Reset" class="pointscalcbutton" onclick="javascript:ResetPointCalculator();"></td></tr>';
                HtmlCodeForForm += '</table></form></td></tr>';
                HtmlCodeForForm += '<tr><td valign="top" class="pointscalctablecellformat" bgcolor="#EAEAEA">'+FnGetInitialPointCalculatorHtmlCode()+'</td></tr>';
                HtmlCodeForForm += '<tr><td valign="top" class="pointscalctablecellformat" align="right" id="PrintRst"><img src="http://www.mortgagefit.com/styles/mortgage/img/print.gif" style="cursor: pointer;" onclick="alert(\'Please fill the form before you take printout.\')" alt="" border="0">&nbsp;&nbsp;</td></tr>';
                HtmlCodeForForm += '<tr><td class="pointscalctablecellformat"><div id="PointAmortChart" style="border: 1px solid #DDDDDD; width:335px; height: 100px; padding: 0 4px 0 4px;"></div></td></tr></table>';

                document.getElementById('PointsCalculatorHomeLink').className = 'pointscalcsyndicate';
                document.getElementById('PointsCalculatorSection').innerHTML = HtmlCodeForForm;
        }
        else
        {
                document.getElementById('PointsCalculatorSection').innerHTML = 'To enable this calculator, please paste the full code as described at <a href="http://www.mortgagefit.com/pointscalcsyndicate/points-calculator.html">MortgageFit</a>';
        }
}
document.write('<style type="text/css">.pointscalctableformat{ border-color:#666666;}.pointscalctablecellformat{ border:0;}.pointscalctableheading{ color:#000066; font-weight:bold; font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif;font-size: 14px;}.pointscalcbutton { border: 1px solid white; background-color: #31659C; color: white; font-size: 10px; font-weight: bold;}.pointscalctext{font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif;font-size: 10px;}.pointscalcsyndicate{ text-decoration:none; color:#999; font-family:sans-serif; font-size:11px;}</style>');
FnCreatePointCalculatorForm();
