

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
 
 <link href="content/LoanCalc.css" rel="stylesheet" type="text/css" />

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="scripts/jquery-1.7.1.js"></script>

    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
     <script type="text/javascript">
        function graph() {
            $(document).ready(function () {
             //   $('#dvTable').hide()
                installment();
                
                
            });

        }
        function addCommas(amount) {

            var result = amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            return result;
        }
        function removeCommas(amount) {
            var result = amount.toString().replace(/\,/g, '');
            return result;

        }
        function installment() {
            $(document).ready(function () {

                var principle = document.getElementById('txtAmount').value;
                principle = removeCommas(principle);
                document.getElementById('txtAmount').value = addCommas(principle);
                var term = document.getElementById('txtTerm').value;
                var interestRate = document.getElementById('txtInterest').value;
                if (principle != '' && term != '' && interestRate != '') {
                  
                    //caclculated every time for graph
                    var interestRateForMonthlyForGraph = interestRate / 1200;
                    var actTimeForMonthlyForGraph = parseFloat(term) * 12;
                    var installmentForMonthlyForGraph = Math.round(principle * interestRateForMonthlyForGraph / (1 - (Math.pow(1 / (1 + interestRateForMonthlyForGraph), actTimeForMonthlyForGraph))));
                    //document.getElementById('lblInstallment').innerHTML = "Monthly Repayment $" + installmentForMonthlyForGraph.toFixed(2);
                    //document.getElementById('lblInsMonOrFornght').innerHTML ="Repayment  " +" $" + parseFloat(installmentForMonthlyForGraph).toFixed(2);
                    document.getElementById('txtInsMonOrFornght1').value = "$" + addCommas(parseFloat(installmentForMonthlyForGraph).toFixed(2));
                    var installment = Math.round(principle * interestRate / (1 - (Math.pow(1 / (1 + interestRate), actTimeForMonthlyForGraph))));
                    document.getElementById('hdnIntall').value = installmentForMonthlyForGraph;
                    callGraph(0);
                    installmentChange();
                    
                }
            });
        }
        function installmentChange() {
            $(document).ready(function () {
                var selectedValue = document.getElementById('slctInstallment').value;
                if (selectedValue != '0') {

                    var principle = document.getElementById('txtAmount').value;
                    principle = removeCommas(principle);
                    var amount = principle;
                    var openingBalance = parseFloat(amount).toFixed(2);
                    var closingBalance = parseFloat(amount).toFixed(2);
                    document.getElementById('txtAmount').value = addCommas(principle);
                    var term = document.getElementById('txtTerm').value;
                    var interestRate = document.getElementById('txtInterest').value;
                    var intS = parseFloat(interestRate);
                    var selectedValue = document.getElementById('slctInstallment').value;
                    var actTime = 0;
                    var factor = 0;
                    var interest = 0;
                    var installment = 0;
                    if (selectedValue == '1') {

                        interestRate = interestRate / 1200;
                        actTime = parseFloat(term) * 12;
                        factor = 12;

                    }
                    if (selectedValue == '2') {

                        //interestRate = interestRate / 2600;
                        //actTime = parseFloat(term) * 26;
                        //factor = 26;
                        interestRate = interestRate / 2608.8;
                        actTime = parseFloat(term) * 26.088;
                        factor = 26.088;
                    }
                    installment = Math.round(principle * interestRate / (1 - (Math.pow(1 / (1 + interestRate), actTime))));
                    //document.getElementById('lblInsMonOrFornght').innerHTML = "Repayment  "+"$" + addCommas(parseFloat(installment).toFixed(2));
                    document.getElementById('txtInsMonOrFornght1').value = "$" + addCommas(parseFloat(installment).toFixed(2));
                    //find new term
                    var upFrontCom = .005;
                    var trailCom = .002;
                    var newTerm = 0;
                    var countInt = 0;
                    var detUpFront = Math.round(parseFloat(amount) * parseFloat(upFrontCom));
                    amount = parseFloat(amount) + 990;
                    var newPriciple = 0;
                    if (selectedValue == "1")
                    { 
                    for (var i = amount; i >= 0;) {
                        if (i == amount) {
                            i = Math.round(parseFloat(amount) - parseFloat(detUpFront));
                        }
                        var interst = Math.round((parseFloat(i) * parseFloat(intS)) / parseFloat(parseFloat(factor * 100)));
                        countInt = parseFloat(interst) + parseFloat(countInt);
                        var pricSide = Math.round((parseFloat(installment)) - parseFloat(interst));
                        i = Math.round(parseFloat(i) - parseFloat(pricSide));
                        var trailComVal = Math.round(parseFloat(i) * parseFloat(trailCom)) / 12;
                        i = parseFloat(i) - parseFloat(trailComVal);
                        newTerm = parseFloat(newTerm) + 1;

                        
                    }
                    amount = principle;
                    var tableCreate = "";
                    var fortNightCheck = 1;
                    var today = new Date();
                    var stringdate = today.toString();
                    stringdate = stringdate.substring(0, 10);
                    var date = today.getDate();
                    var month = today.getMonth();
                    var monthToDis = month + 1;
                    var year = today.getFullYear();
                    var daysInMonth1 = daysInMonth(date,year);
                    tableCreate = "<table style='margin-left:120px'><tr><th style='width:10%; text-align: left;'>Date</th><th style='width:23%; text-align: left;'>Transaction Detail</th><th style='width:20%;text-align: right;'>Opening Balance</th><th style='width:10%;text-align: right;'>Debit</th><th style='width:10%;text-align: right;'>Credit</th><th style='width:20%; text-align: right;'>Closing Balance</th></tr>";
                    //row for initial pricipal
                    tableCreate = tableCreate + "<tr><td style='width:10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width:23%px; text-align: left;'>Loan Drawn</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: width:20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
					// First Row Loan Drawn

                    var count = 0;
                    var monthcount = 0;
                    var fortNightCount=0;
                    //fintrackfee
                    for (i = newTerm; i >= 0; i--) {
                        if (amount >= 0) {

                            if (i == newTerm) {
                                amount = Math.round(parseFloat(amount));// - parseFloat(detUpFront));
                                count++;
                            }
                            else {
                            	
                                fortNightCount = parseFloat(fortNightCount) + 1;
                                var interst = Math.round((parseFloat(amount) * parseFloat(intS)) / parseFloat(parseFloat(factor * 100)));
                                interst = parseFloat(interst).toFixed(2);
                                //newInt = Math.round(parseFloat(newInt) + parseFloat(interst));
                                var pricSide = Math.round((parseFloat(installment)) - parseFloat(interst));
                                pricSide = parseFloat(pricSide).toFixed(2);
                                amount = Math.round(parseFloat(amount) - parseFloat(installment));
                                //row for intallment credit
                                closingBalance = (parseFloat(amount).toFixed(2));
                                //set date
                                var d = new Date(year, month, date);

                                d.setDate(d.getDate() + daysInMonth(d.getDate(),d.getFullYear()));
                                year = d.getFullYear();
                                month = d.getMonth();
                                monthToDis = month + 1;
                                date = d.getDate();
                                stringdate = d.toString();
                                stringdate = stringdate.substring(0, 10);
                                //set date ends
                                if (closingBalance <= 0) {
                                    tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + fortNightCount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(installment) + "</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                                    break;
                                }
                                else {
                                    tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + fortNightCount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(installment) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                    openingBalance = parseFloat(closingBalance).toFixed(2);
                                }

                                if(i==(newTerm-1))
                                {
							
                                	  //fintrackfee
                                     closingBalance = parseFloat(parseFloat(closingBalance) + 990).toFixed(2);
                                     tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Fintrack Fee</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$990.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                     openingBalance = parseFloat(closingBalance).toFixed(2);
                                     //Upfront Fee  
                                     closingBalance = parseFloat(parseFloat(closingBalance) - parseFloat(detUpFront)).toFixed(2);
                                     tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Up front</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(detUpFront) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                     openingBalance = parseFloat(closingBalance).toFixed(2);
                                     //Interest Credit 
                                     closingBalance = parseFloat(parseFloat(closingBalance) + parseFloat(interst)).toFixed(2);
                                     tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Interest Charged</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>-$" + addCommas(interst) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                     openingBalance = parseFloat(closingBalance).toFixed(2);
                                 }
                                else
                                {
                                	if (closingBalance > 0) {
                                	 //Interest Credit 
                                    closingBalance = parseFloat(parseFloat(closingBalance) + parseFloat(interst)).toFixed(2);
                                    tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Interest Charged</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>-$" + addCommas(interst) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                    openingBalance = parseFloat(closingBalance).toFixed(2);
                                	}	
                                }
                                
                                openingBalance = parseFloat(closingBalance).toFixed(2);

                                //monthly trail
                               
                                    monthcount = parseFloat(monthcount) + 1;
                                    var trailComVal = Math.round(parseFloat(amount) * parseFloat(trailCom) / 12);
                                    trailComVal = parseFloat(trailComVal).toFixed(2);
                                    amount = amount - trailComVal;
                                    if (amount <= 0) {
                                        break;
                                        tableCreate = tableCreate + "<tr><td style='width: 300px; text-align: left;'>Month " + i + "</td><td style='width: 200px; text-align: left;'>$" + openingBalance + "</td><td style='width: 100px; text-align: left;'>$" + openingBalance + "</td><td style='width: 200px; text-align: left;'>$" + "0" + "</td></tr>";


                                    }

                                    //row for trail credit
                                    //   trailTotCom = parseFloat(trailTotCom) + parseFloat(trailComVal);
                                    closingBalance = parseFloat(closingBalance).toFixed(2) - parseFloat(trailComVal);
                                    closingBalance = parseFloat(closingBalance).toFixed(2);
                                    if (closingBalance <= 0) {
                                        tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Trail Credit </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(trailComVal) + "</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                                        break;
                                    }
                                    else {
                                        tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Trail Credit </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(trailComVal) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas( closingBalance) + "</td></tr>";

                                    }
                                    openingBalance = closingBalance;
                                    amount = openingBalance;
                                    fortNightCheck = 1;
                                
                                //row for monthly credit ends
                                count++;
                            }
                        }

                    }
                    tableCreate = tableCreate + "</table>";
                    document.getElementById('dvTable').innerHTML = tableCreate;
                 
                    }
                        //term for fortnight only
                    else if (selectedValue == "2") {
                        
                        var fortnightcount = 1;
                        for (var i = amount; i >= 0;) {
                            if (i == amount) {
                                i = Math.round(parseFloat(amount) - parseFloat(detUpFront));
                            }
                            var interst = Math.round((parseFloat(i) * parseFloat(intS)) / parseFloat(parseFloat(factor * 100)));
                            countInt = parseFloat(interst) + parseFloat(countInt);
                            var pricSide = Math.round((parseFloat(installment)) - parseFloat(interst));
                            i = Math.round(parseFloat(i) - parseFloat(pricSide));
                            if (fortnightcount == 2) {
                                var trailComVal = Math.round(parseFloat(i) * parseFloat(trailCom)) / 12;
                                i = parseFloat(i) - parseFloat(trailComVal);
                                fortnightcount = 1;
                            }
                            else {
                                fortnightcount = parseFloat(fortnightcount) + 1;
                            }
                            newTerm = parseFloat(newTerm) + 1;

                        }

                        amount = principle;

                        var tableCreate = "";
                        var fortNightCheck = 1;
                        var today = new Date();
                        var stringdate = today.toString();
                        stringdate = stringdate.substring(0, 10);
                        var date = today.getDate();
                        var month = today.getMonth();
                        var monthToDis = month + 1;
                        var year = today.getFullYear();
                        var daysInMonth1 = daysInMonth(date,year);
                        tableCreate = "<table style='margin-left:120px'><tr><th style='width:10%; text-align: left;'>Date</th><th style='width:23%; text-align: left;'>Transaction Detail</th><th style='width:20%;text-align: right;'>Opening Balance</th><th style='width:10%;text-align: right;'>Debit</th><th style='width:10%;text-align: right;'>Credit</th><th style='width:20%; text-align: right;'>Closing Balance</th></tr>";
                        //row for initial pricipal
                        tableCreate = tableCreate + "<tr><td style='width:10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width:23%px; text-align: left;'>Loan Drawn</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: width:20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
						// First Row Loan Drawn

                        var count = 0;
                        var monthcount = 0;
                        var fortNightCount=0;
                        //fintrackfee
                        for (i = newTerm; i >= 0; i--) {
                            if (amount >= 0) {

                                if (i == newTerm) {
                                    amount = Math.round(parseFloat(amount));// - parseFloat(detUpFront));
                                    count++;
                                }
                                else {
                                	
                                    fortNightCount = parseFloat(fortNightCount) + 1;
                                    var interst = Math.round((parseFloat(amount) * parseFloat(intS)) / parseFloat(parseFloat(factor * 100)));
                                    interst = parseFloat(interst).toFixed(2);
                                    //newInt = Math.round(parseFloat(newInt) + parseFloat(interst));
                                    var pricSide = Math.round((parseFloat(installment)) - parseFloat(interst));
                                    pricSide = parseFloat(pricSide).toFixed(2);
                                    amount = Math.round(parseFloat(amount) - parseFloat(installment));
                                    //row for intallment credit
                                    closingBalance = (parseFloat(amount).toFixed(2));
                                    //set date
                                    var d = new Date(year, month, date);

                                    d.setDate(d.getDate() + 14);
                                    year = d.getFullYear();
                                    month = d.getMonth();
                                    monthToDis = month + 1;
                                    date = d.getDate();
                                    stringdate = d.toString();
                                    stringdate = stringdate.substring(0, 10);
                                    //set date ends
                                    if (closingBalance <= 0) {
                                        tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Fortnight " + fortNightCount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(installment) + "</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                                        break;
                                    }
                                    else {
                                        tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Fortnight " + fortNightCount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(installment) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                        openingBalance = parseFloat(closingBalance).toFixed(2);
                                    }

                                    if(i==(newTerm-1))
                                    {

                                    	  //fintrackfee
                                    	 openingBalance = parseFloat(closingBalance).toFixed(2);
                                         closingBalance = parseFloat(parseFloat(closingBalance) + 990).toFixed(2);
                                         tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Fintrack Fee</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$990.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                         openingBalance = parseFloat(closingBalance).toFixed(2);
                                         //Upfront Fee  
                                         closingBalance = parseFloat(parseFloat(closingBalance) - parseFloat(detUpFront)).toFixed(2);
                                         tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Up front</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(detUpFront) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                         openingBalance = parseFloat(closingBalance).toFixed(2);
                                         //Interest Credit 
                                         closingBalance = parseFloat(parseFloat(closingBalance) + parseFloat(interst)).toFixed(2);
                                         tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Interest Charged</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>-$" + addCommas(interst) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                         openingBalance = parseFloat(closingBalance).toFixed(2);
                                     }
                                    else
                                    {
                                    	if (closingBalance > 0) {
                                    	 //Interest Credit 
                                        closingBalance = parseFloat(parseFloat(closingBalance) + parseFloat(interst)).toFixed(2);
                                        tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Interest Charged</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>-$" + addCommas(interst) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                        openingBalance = parseFloat(closingBalance).toFixed(2);
                                    	}	
                                    }
                                    
                                    openingBalance = parseFloat(closingBalance).toFixed(2);

                                    //monthly trail
                                    if (fortNightCheck == 2) {
                                        monthcount = parseFloat(monthcount) + 1;
                                        var trailComVal = Math.round(parseFloat(amount) * parseFloat(trailCom) / 12);
                                        trailComVal = parseFloat(trailComVal).toFixed(2);
                                        amount = amount - trailComVal;
                                        if (amount <= 0) {
                                            break;
                                            tableCreate = tableCreate + "<tr><td style='width: 300px; text-align: left;'>Month " + i + "</td><td style='width: 200px; text-align: left;'>$" + openingBalance + "</td><td style='width: 100px; text-align: left;'>$" + openingBalance + "</td><td style='width: 200px; text-align: left;'>$" + "0" + "</td></tr>";


                                        }

                                        //row for trail credit
                                        //   trailTotCom = parseFloat(trailTotCom) + parseFloat(trailComVal);
                                        closingBalance = parseFloat(closingBalance).toFixed(2) - parseFloat(trailComVal);
                                        closingBalance = parseFloat(closingBalance).toFixed(2);
                                        if (closingBalance <= 0) {
                                            tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Trail Credit </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(trailComVal) + "</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                                            break;
                                        }
                                        else {
                                            tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Trail Credit </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(trailComVal) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas( closingBalance) + "</td></tr>";

                                        }
                                        openingBalance = closingBalance;
                                        amount = openingBalance;
                                        fortNightCheck = 1;
                                    } else {
                                        fortNightCheck =parseFloat(fortNightCheck) + 1;
                                        amount = openingBalance;
                                    }
                                    //row for monthly credit ends
                                    count++;
                                }
                            }

                        }
                        tableCreate = tableCreate + "</table>";
                        document.getElementById('dvTable').innerHTML = tableCreate;
                        
                    }
                    amount = principle;
                    var actTerm = newTerm;
                    //forthnightly statement 
                    if (selectedValue == "2") {
                        actTerm = Math.round(parseFloat(newTerm) / 2.174);
                        //statement
                        var tableCreate = "";
                        var fortNightCheck = 1;
                        var today = new Date();
                        var stringdate = today.toString();
                        stringdate = stringdate.substring(0, 10);
                        var date = today.getDate();
                        var month = today.getMonth();
                        var monthToDis = month + 1;
                        var year = today.getFullYear();
                        var daysInMonth1 = daysInMonth(date,year);
                        openingBalance = parseFloat(amount).toFixed(2);
                        closingBalance = parseFloat(amount).toFixed(2);
                        tableCreate = "<table style='margin-left:120px'><tr><th style='width:10%; text-align: left;'>Date</th><th style='width:23%; text-align: left;'>Transaction Detail</th><th style='width:20%;text-align: right;'>Opening Balance</th><th style='width:10%;text-align: right;'>Debit</th><th style='width:10%;text-align: right;'>Credit</th><th style='width:20%; text-align: right;'>Closing Balance</th></tr>";
                        //row for initial pricipal
                        tableCreate = tableCreate + "<tr><td style='width:10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width:23%px; text-align: left;'>Loan Drawn</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: width:20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
						// First Row Loan Drawn
							
                      
                      
                        var count = 0;
                        var monthcount = 0;
                        var fortNightCount=0;
                        //fintrackfee
                        for (i = newTerm; i >= 0; i--) {
                            if (amount >= 0) {

                                if (i == newTerm) {
                                    amount = Math.round(parseFloat(amount));// - parseFloat(detUpFront));
                                    count++;
                                }
                                else {
                                	
                                    fortNightCount = parseFloat(fortNightCount) + 1;
                                    var interst = Math.round((parseFloat(amount) * parseFloat(intS)) / parseFloat(parseFloat(factor * 100)));
                                    interst = parseFloat(interst).toFixed(2);
                                    //newInt = Math.round(parseFloat(newInt) + parseFloat(interst));
                                    var pricSide = Math.round((parseFloat(installment)) - parseFloat(interst));
                                    pricSide = parseFloat(pricSide).toFixed(2);
                                    amount = Math.round(parseFloat(amount) - parseFloat(installment));
                                    //row for intallment credit
                                    closingBalance = (parseFloat(amount).toFixed(2));
                                    //set date
                                    var d = new Date(year, month, date);

                                    d.setDate(d.getDate() + 14);
                                    year = d.getFullYear();
                                    month = d.getMonth();
                                    monthToDis = month + 1;
                                    date = d.getDate();
                                    stringdate = d.toString();
                                    stringdate = stringdate.substring(0, 10);
                                    //set date ends
                                    if (closingBalance <= 0) {
                                        tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Fortnight " + fortNightCount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(installment) + "</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                                        break;
                                    }
                                    else {
                                        tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Fortnight " + fortNightCount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(installment) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";

                                    }

                                    if(i==(newTerm-1))
                                    {

                                    	  //fintrackfee
                                         closingBalance = parseFloat(parseFloat(closingBalance) + 990).toFixed(2);
                                         tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Fintrack Fee</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$990.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                         openingBalance = parseFloat(closingBalance).toFixed(2);
                                         //Upfront Fee  
                                         closingBalance = parseFloat(parseFloat(closingBalance) - parseFloat(detUpFront)).toFixed(2);
                                         tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Up front</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(detUpFront) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                         openingBalance = parseFloat(closingBalance).toFixed(2);
                                         //Interest Credit 
                                         closingBalance = parseFloat(parseFloat(closingBalance) + parseFloat(interst)).toFixed(2);
                                         tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Interest Charged</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>-$" + addCommas(interst) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                         openingBalance = parseFloat(closingBalance).toFixed(2);
                                     }
                                    else
                                    {
                                    	if (closingBalance > 0) {
                                    	 //Interest Credit 
                                        closingBalance = parseFloat(parseFloat(closingBalance) + parseFloat(interst)).toFixed(2);
                                        tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Interest Charged</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>-$" + addCommas(interst) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                                        openingBalance = parseFloat(closingBalance).toFixed(2);
                                    	}	
                                    }
                                    
                                    openingBalance = parseFloat(closingBalance).toFixed(2);

                                    //monthly trail
                                    if (fortNightCheck == 2) {
                                        monthcount = parseFloat(monthcount) + 1;
                                        var trailComVal = Math.round(parseFloat(amount) * parseFloat(trailCom) / 12);
                                        trailComVal = parseFloat(trailComVal).toFixed(2);
                                        amount = amount - trailComVal;
                                        if (amount <= 0) {
                                            break;
                                            tableCreate = tableCreate + "<tr><td style='width: 300px; text-align: left;'>Month " + i + "</td><td style='width: 200px; text-align: left;'>$" + openingBalance + "</td><td style='width: 100px; text-align: left;'>$" + openingBalance + "</td><td style='width: 200px; text-align: left;'>$" + "0" + "</td></tr>";


                                        }

                                        //row for trail credit
                                        //   trailTotCom = parseFloat(trailTotCom) + parseFloat(trailComVal);
                                        closingBalance = parseFloat(closingBalance).toFixed(2) - parseFloat(trailComVal);
                                        closingBalance = parseFloat(closingBalance).toFixed(2);
                                        if (closingBalance <= 0) {
                                            tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Trail Credit </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(trailComVal) + "</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                                            break;
                                        }
                                        else {
                                            tableCreate = tableCreate + "<tr><td style='10%;align:left'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Trail Credit </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(trailComVal) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas( closingBalance) + "</td></tr>";

                                        }
                                        openingBalance = closingBalance;
                                        amount = openingBalance;
                                        fortNightCheck = 1;
                                    } else {
                                        fortNightCheck =parseFloat(fortNightCheck) + 1;
                                        amount = openingBalance;
                                    }
                                    //row for monthly credit ends
                                    count++;
                                }
                            }

                        }
                        tableCreate = tableCreate + "</table>";
                        document.getElementById('dvTable').innerHTML = tableCreate;
                        //statement
                    }
                    else {

                        callGraph(0);
                    }
                    interest = countInt;
                    var actInt = document.getElementById('lblOriginalIntrest').innerHTML;
                    actInt = actInt.replace('$', 0);
                    actInt = removeCommas(actInt);
                    interest = Math.round(interest);
                    interest = parseFloat(interest).toFixed(2)
                    differentInt = parseFloat(actInt) - parseFloat(interest);
                    differentInt = differentInt.toFixed(2)
                    document.getElementById('lblCashBachIntest').innerHTML = "$" + addCommas(interest);
                    document.getElementById('lblInterestSaved').innerHTML = "$" + addCommas(differentInt);
                    document.getElementById('lblInstsaved').innerHTML = "$" + addCommas(differentInt);
                    
                    var loanWithSavingTotal = parseFloat(principle) + parseFloat(interest);
                    loanWithSavingTotal = parseFloat(loanWithSavingTotal).toFixed(2);
                    document.getElementById('lblLoanWithSavingTotal').innerHTML = "$" + addCommas(loanWithSavingTotal);
                    document.getElementById('lblCashBaOffTime').innerHTML =parseFloat(parseFloat(actTerm)/12).toFixed(2) + " years";
                    //calculate time save
                    var fac = parseFloat(term) * 12;
                    var diffTimeF = parseFloat(fac) - parseFloat(actTerm);
                    diffTime = parseFloat(diffTimeF) / 12;
                    var year = diffTime.toString().split(".")[0]; ///before
                    var month = parseFloat(diffTimeF) % 12;
                    document.getElementById('lblTimeSaved').innerHTML = year + " years " + month + " months";
                    document.getElementById('lblTimesave').innerHTML = year + " years " + month + " months";
                    
                    //caculate time save
                    document.getElementById('lblSaveStatement').innerHTML = "You could save " + "<b style=color:red>$" + addCommas(Math.round(parseFloat(differentInt)).toFixed(2)) + "</b> in interest or<b style=color:red> " + year + ' Years ' + Math.ceil(month) + ' Months' + "</b> with a cash back loan"
                   
                }
            });
        }
        function onlyNumbers(evt) {
            var e = event || evt; // for trans-browser compatibility
            var charCode = e.which || e.keyCode;

            if (charCode != 46 && charCode > 31
              && (charCode < 48 || charCode > 57))
                return false;

            return true;
        }
        window.onload = graph()
    </script>
</head>
<body>
    <div id="wraper">
<div id="hearder">
            <div class="topoart">
                <div class="firstline">
                    <h1>Loan Amount $</h1>
                    <div class="whitebox">
                        <input name="Amount" id="txtAmount" type="text" style="width: 92px;" class="mac" value="500000" onKeyPress="return onlyNumbers();" onChange="graph()" /></div>
                </div>
                <div class="firstline81">
                    <h1>Loan Term (years)</h1>
                    <div class="whitebox2">
                  <input name="Term" id="txtTerm" type="text" style="width: 30px;" class="mac" value="30" onKeyPress="return onlyNumbers();" onChange="graph()" /></div>
                </div>
                <div class="firstlineTime">
                    <h1>Interest Rate %</h1>
                    <div class="whitebox3">
                        <input name="Interest" id="txtInterest" type="text" style="width: 30px;" class="mac" onKeyPress="return onlyNumbers();" onChange="graph()" value="5" />
                  </div>
                </div>
                 <input type="hidden" id="hdnIntall" />
              <div class="firstlineIns">
               
                <h1><label id="lblInsMonOrFornght">Repayment</label></h1>
                    <div class="whitebox5">
                        <input name="Interest" id="txtInsMonOrFornght1" type="text" style="width: 80px;" class="mac" readonly />
                    </div>
              </div>
        <div class="firstlineIns">
          <h1> Repayment Type</h1>
                    <div class="whitebox5">
                        <select id="slctInstallment" onChange="installmentChange()" style="border: 1px solid #848484; border-radius: 10px 10px 10px 10px; height: 28px; width: 94px; padding: 5px 6px 2px 0;">

                            <option title="select" value="0">--Select--</option>
                            <option title="Monthly"  value="1">Monthly</option>
                            <option title="Fortnightly" selected="selected" value="2">Fortnightly</option>
                        </select>
                    </div>
              </div>        
  <div class="calculate">
                    <h1>Calculate</h1>
                </div>
                <div class="bckclr">
                <div class="firstlinebtm">
                  <h1>Interest Saved</h1>
                  <div class="whiteboxbtm">
                        <label id="lblInstsaved">$000000</label>
                  </div>
                </div>
                 <div class="firstlinelst">
                  <h1>Time Saved</h1>
                  <div class="whiteboxlst">
                        <label id="lblTimesave" >1 year 3 months</label>
                  </div>
                </div></div>
                  <div class="firstline00" id="divExport">
                    <h1>Export to PDF</h1>
                    <div class="whitebox001">
                  <p style="margin:0px; padding:0px; float:left;">
            <input type="image" src="pdf-icon.png" alt="Save as PDF" id="buttonExport"  width="45px" height="51px"/>
            </p></div>
                </div>
            </div>
        </div>
      
          <div class="graph">
          <div id="container" style="width:98%; border-radius:10px;"></div>
     </div>
     <!--
        <div class="textarea textarea-small">
            <div class="nav">
                <h1>Export to PDF</h1>
            </div>
            <p>
            <input type="image" src="pdf-icon.png" alt="Save as PDF" id="buttonExport"  width="45px" height="55px"/>
            </p>
        </div>
         -->
      <div class="clear"></div>
        <div class="bottompart">
            <div class="firstbox">
                <div class="nav2">
                    <h1></h1>
                </div>
                <ul>
                    <li>Interest Paid</li>
                    <li>Principal Paid</li>
                    <li>Pay Off Time</li>
                    <li>Total Loan Repayments</li>
                    <li>Interest Saved</li>
                    <li>Time Saved</li>
                </ul>
            </div>
            <div class="firstbox">
                <div class="nav2">
                    <h1>Original loan</h1>
                </div>
                <ul>
                    <li>
                        <label id="lblOriginalIntrest"></label>
                    </li>
                    <li>
                        <label id="lblOrPriPaid"></label>
                    </li>
                    <li>
                        <label id="lblOrPayOffTime"></label>
                    </li>
                    <li>$<label id="lblOrginalTotal"></label></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
            <div class="firstbox">
                <div class="nav2">
                    <h1>Loan with Cash Back</h1>
                </div>

                <ul>
                    <li>
                        <label id="lblCashBachIntest"></label>
                    </li>
                    <li>
                        <label id="lblCashBaPriPaid"></label>
                    </li>
                    <li>
                        <label id="lblCashBaOffTime"></label>
                    </li>
                    <li>
                        <label id="lblLoanWithSavingTotal" class="htmLabel"></label>
                    </li>
                    <li>
                        <label id="lblInterestSaved"></label>
                    </li>
                    <li>
                        <label id="lblTimeSaved"></label>
                    </li>
                </ul>
            </div>
            <div class="footer">
               <div class="statement">
                    <label id="lblSaveStatement"></label></div>
               
            </div>
        </div>
           <div class="clear"></div>
               <div class="textarea">
            <div class="nav">
                <h1>Disclaimer</h1>
            </div>
            <p>
                <b>WHY WE BUILT THIS LOAN CALCULATOR</b>
                <p>
				We wanted to stimulate discussion between YOU and FINTRACK about the difference in savings between the going direct to a lender or by using a Mortgage Broker. This illustration allows you to explore the difference in savings that you could potential make by using our pro consumer service, which entails full rebate of all commissions less GST and our fee.
				</p>
			</p>
			<p>
			<b>ASSUMPTIONS MADE IN THIS ILLUSTRATION</b>
			
			<ul>
			<li>We rebate 100% of lender commission less GST</li>
			<li>We charge a fee of $990 including GST</li>
			<li>Interest rates remain constant for term of the loan</li>
			<li>Lender pays us 0.50% upfront commission on loan amount</li>
			<li>Lender pays us 0.20% trail commission on monthly loan balance</li>
			<li>Lender commission is average of lenders and may vary</li>
			<li>You make your pay minimum repayments and do not default</li>
			</ul>
			
			</p>
			<p>
			<b>WARNING</b>
			<p>
			This is an illustration of the potential savings you may experience by using the Fintrack pro consumer service. Your individual interest savings may be more or less than the results shown, as the outcomes are valid only for the data you chose and assumptions we have made. No attempt has been made to analyse costs associated with switching lenders including discharge fees, application fees or Lender Mortgage Insurance. This is not a recommendation or offer of finance.
			</p>
			</p>
			<p>
				<b>CONCLUSION</b>
				<p>
			You should not make a decision to engage our services based purely on this illustrated calculation. What you can do however is contact us for a full and concise individual assessment of your financial needs.
			</p>
		   </p>
  
        </div>
        <div class="clear"></div>
        <div class="show">
            <div class="showbuton"><a id="lkHideShow">Loan Statement Details Hide</a></div>
        </div>
       
 
       <!--  <div id="dvTable" >abcehkkkkklkjkjkklklj</div>-->
        <div class="clear"></div>
        <div class="divTable" id="dvTable"></div>
        <div class="clear"></div>

</div> 
    <script type="text/javascript">
    function daysInMonth(month,year) {
        return new Date(year, month, 0).getDate();
    }
     function AddMonth(month)
     {
           if(month==12)
           {
               return 1;
           }
           else
           {
				return month + 1;
           }
     }
        function callGraph(Newterm) {
            $(document).ready(function () {
                var prevInt = 0;
                var newInt = 0;
                var amount = document.getElementById('txtAmount').value;
                amount = removeCommas(amount);
                var openingBalance = parseFloat(amount).toFixed(2);
                var closingBalance = parseFloat(amount).toFixed(2);
                var term = document.getElementById('txtTerm').value;
                var interestRate = document.getElementById('txtInterest').value;
                var firstInstall = document.getElementById('hdnIntall').value;
                firstInstall = removeCommas(firstInstall);
                firstInstall = parseFloat(firstInstall).toFixed(2);
                var XaxisLabels = [];
                var Points = [];
                var NewPoints = [];
                var tableCreate = "";
                var i, count = 0;
                var upFrontTotCom = 0;
                var trailTotCom = 0;
                var today = new Date();
                var stringdate = today.toString();
                stringdate = stringdate.substring(0, 10);
                var date = today.getDate();
                var month = today.getMonth();
                var monthToDis = month + 1;
                var year = today.getFullYear();
                var year1 = today.getFullYear();
                var daysInMonth1 = daysInMonth(monthToDis,year);
                var stclosingBal=0;
                var stAmount=0;
               
               
                var fac = Math.ceil(parseFloat(term) * 12);
                var month = monthToDis;
                //Orignal Loan Loop
                for (i = fac; i >= 0; i--) {
                    if (amount >= 0) {
                        if (i == fac) {
                            Points.push([count, parseFloat(amount)]);
                            count++;
                        }
                        else {
                            var interst = (parseFloat(amount) * parseFloat(interestRate)) / 36500;
                            
                            var daysInMonth2 = daysInMonth(month,year1);
                            interst = daysInMonth2 * interst;
                            prevInt = Math.round(parseFloat(prevInt) + parseFloat(interst));
                            var pricSide = (parseFloat(firstInstall)) - parseFloat(interst);
                            amount = Math.round(parseFloat(amount) - parseFloat(pricSide));
                            amount= parseFloat(amount).toFixed(2);
                            if (amount <= 0) {
                                Points.push([count, 0]);
                                break;

                            }
                            if(month==12)
                            {
                                year = year +1;
                            }
                            month = AddMonth(month);
                            Points.push([count, parseFloat(amount)]);
                            count++;
                        }
                    }

                }
                //Cashback Loan 
                amount = document.getElementById('txtAmount').value;
                amount = removeCommas(amount);
            
               
                
                
                count = 0;
                // new term
                var upFrontCom = .005;
                var trailCom = .002;
                var newTerm = 0;
                var detUpFront = Math.round(parseFloat(amount) * parseFloat(upFrontCom));
                //row for upfront
                detUpFront = parseFloat(detUpFront).toFixed(2);
                upFrontTotCom = detUpFront;

                closingBalance = parseFloat(parseFloat(amount) - parseFloat(detUpFront)).toFixed(2);
                tableCreate = tableCreate + "<tr><td style='10%;'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Up front</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(detUpFront) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                openingBalance = parseFloat(closingBalance).toFixed(2);
                // Upfront commission 

                amount = parseFloat(amount) + 990;
                closingBalance = parseFloat(parseFloat(openingBalance) + 990).toFixed(2);
                tableCreate = tableCreate + "<tr><td style='10%;'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Fintrack Fee</td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$990.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                openingBalance = parseFloat(closingBalance).toFixed(2);
                //row fintrackfee ends

                var newPriciple = 0;
                for (var i = amount; i >= 0;) {
                    if (i == amount) {
                        i = Math.round(parseFloat(amount) - parseFloat(detUpFront));
                    }
                    var interst = Math.round((parseFloat(i) * parseFloat(interestRate)) /36500);
                    interst = daysInMonth1 * interst;
                    newInt = Math.round(parseFloat(newInt) + parseFloat(interst));
                    var pricSide = Math.round((parseFloat(firstInstall)) - parseFloat(interst));
                    i = Math.round(parseFloat(i) - parseFloat(pricSide));
                    var trailComVal = Math.round(parseFloat(i) * parseFloat(trailCom)) / 12;
                    i = parseFloat(i) - parseFloat(trailComVal);
                    newTerm = parseFloat(newTerm) + 1;
                }
                // new term
                var val = newTerm;
                var monthcount = 0;

                //new graph points
                for (i = newTerm; i >= 0; i--) {
                    if (amount >= 0) {

                        if (i == newTerm) {
                            amount = Math.round(parseFloat(amount) - parseFloat(detUpFront));
                            NewPoints.push([count, parseFloat(amount)]);

                            count++;
                        }
                        else {
                            monthcount = parseFloat(monthcount) + 1;
                            var interst = Math.round((parseFloat(amount) * parseFloat(interestRate)) / 100);
                            var dailyinterest = interst/365;
                            dailyinterest = parseFloat(dailyinterest).toFixed(2);
                            interst = daysInMonth1 * dailyinterest;
                            
                            interst = parseFloat(interst).toFixed(2);
                         //newInt = Math.round(parseFloat(newInt) + parseFloat(interst));
                            var pricSide = Math.round((parseFloat(firstInstall)) - parseFloat(interst));
                            pricSide = parseFloat(pricSide).toFixed(2);
                            stclosingBal = Math.round(parseFloat(amount) - parseFloat(firstInstall));
                            amount = Math.round(parseFloat(amount) - parseFloat(pricSide));
                            //row for intallment credit
                            closingBalance = (parseFloat(amount).toFixed(2));
                            
                            //set date
                            var d = new Date(year, month, date);
                            d.setMonth(d.getMonth() + 1);
                            year = d.getFullYear();
                            month = d.getMonth();
                            monthToDis = month + 1;
                            date = d.getDate();
                            stringdate = d.toString();
                            stringdate = stringdate.substring(0, 10);
                            //set date ends
                            if (closingBalance <= 0) {
                               // tableCreate = tableCreate + "<tr><td style='10%;'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$" + addCommas(firstInstall) + "</td><td style='width: 10%; text-align: right;'>$" + addCommas(interst) + "</td><td style='width: 10%; text-align: right;'>$" + addCommas(pricSide) + "</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                            	//tableCreate = tableCreate + "<tr><td style='10%;'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$" + addCommas(firstInstall) + "</td><td style='width: 10%; text-align: right;'>$" + addCommas(firstInstall) +  "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                            }
                            else {
                                
                                //tableCreate = tableCreate + "<tr><td style='10%;'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Credit Repayment </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$" + addCommas(firstInstall) + "</td><td style='width: 10%; text-align: right;'>$" + addCommas(firstInstall) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";
                            }
                            openingBalance = parseFloat(closingBalance).toFixed(2);
                            var trailComVal = Math.round(parseFloat(amount) * parseFloat(trailCom) / 12);
                            trailComVal = parseFloat(trailComVal).toFixed(2);
                            amount = amount - trailComVal;
                            if (amount <= 0) {
                                NewPoints.push([count, 0]);
                               //    tableCreate = tableCreate + "<tr><td style='width: 300px; text-align: left;'>Month " + i + "</td><td style='width: 200px; text-align: left;'>$" + openingBalance + "</td><td style='width: 100px; text-align: left;'>$0</td><td style='width: 100px; text-align: left;'>$" + openingBalance + "</td><td style='width: 200px; text-align: left;'>$" + "0" + "</td></tr>";
                                break;

                            }
                            NewPoints.push([count, parseFloat(amount)]);
                            //row for trail credit
                            trailTotCom = parseFloat(trailTotCom) + parseFloat(trailComVal);
                            closingBalance = parseFloat(amount).toFixed(2);
                           // if (closingBalance <= 0) {
                          //      tableCreate = tableCreate + "<tr><td style='10%;'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Trail Credit </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(trailComVal) + "</td><td style='width: 20%; text-align: right;'>$" + "0.00" + "</td></tr>";
                           // }
                          // else {
                           //     tableCreate = tableCreate + "<tr><td style='10%;'>" + date + "/" + monthToDis + "/" + year + "</td><td style='width: 23%; text-align: left;'>Month " + monthcount + " Trail Credit </td><td style='width: 20%; text-align: right;'>$" + addCommas(openingBalance) + "</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$0.00</td><td style='width: 10%; text-align: right;'>$" + addCommas(trailComVal) + "</td><td style='width: 20%; text-align: right;'>$" + addCommas(closingBalance) + "</td></tr>";

                           // }
                            openingBalance = closingBalance;
                            //row for monthly credit ends
                            count++;
                        }
                    }

                }


                //new graph points
                amount = document.getElementById('txtAmount').value;
                amount = removeCommas(amount);
               
                //..............Savings
                //principal
              
                amount = parseFloat(amount).toFixed(2);
                document.getElementById('lblOrPriPaid').innerHTML = "$" + addCommas(amount);
                document.getElementById('lblCashBaPriPaid').innerHTML = "$" + addCommas(amount);
                //document.getElementById('lblupFrontComm').innerText = "$" + upFrontTotCom;
                //document.getElementById('lblTrailComm').innerText = "$" + parseFloat(trailTotCom).toFixed(2);

                //intrest

                prevInt = parseFloat(prevInt).toFixed(2);
                newInt = parseFloat(newInt).toFixed(2);
                document.getElementById('lblOriginalIntrest').innerHTML = "$" + addCommas(prevInt);
                document.getElementById('lblCashBachIntest').innerHTML = "$" + addCommas(newInt);
                var inrstSaved = parseFloat(prevInt) - parseFloat(newInt);
                inrstSaved = parseFloat(inrstSaved).toFixed(2);
                document.getElementById('lblInterestSaved').innerHTML = "$" + addCommas(inrstSaved);
                document.getElementById('lblInstsaved').innerHTML = "$" + addCommas(inrstSaved);
                
                //paid off time
                var showv = parseFloat(fac / 12).toFixed(2);
                document.getElementById('lblOrPayOffTime').innerHTML = showv + " Years";
               
                document.getElementById('lblCashBaOffTime').innerHTML = parseFloat(newTerm / 12).toFixed(2) + ' Years';

                //Time
                var diffTimeF = parseFloat(fac) - parseFloat(newTerm);
                diffTime = parseFloat(diffTimeF) / 12;
                var year = diffTime.toString().split(".")[0]; ///before
                var month = parseFloat(diffTimeF) % 12;
                document.getElementById('lblTimeSaved').innerHTML = year + " years " + month + " months";
                document.getElementById('lblTimesave').innerHTML = year + " years " + month + " months";
                document.getElementById('lblSaveStatement').innerHTML = "You could save " + "<b style=color:red>$" + addCommas(Math.round(parseFloat(inrstSaved)).toFixed(2)) + "</b> in interest or<b style=color:red> " + year + ' Years ' + Math.ceil(month) + ' Months' + "</b> with a cash back loan";
                
                //total
                document.getElementById('lblOrginalTotal').innerHTML = addCommas(parseFloat(parseFloat(amount) + parseFloat(prevInt)).toFixed(2))
                document.getElementById('lblLoanWithSavingTotal').innerHTML ="$"+ addCommas(parseFloat(parseFloat(amount) + parseFloat(newInt)).toFixed(2));

                //..............Savings 
          
                window.chart = new Highcharts.Chart({
                    chart: {
                        renderTo: 'container',
                        type: 'area'
                    },
                    title: {
                        text: 'Loan Calculator'
                    },
                    subtitle: {

                    },
                    xAxis: {
                        title: {
                            text: 'Time (Years)'
                        },
                        labels: {
                            formatter: function () {

                                return this.value / 12 + ' years';
                            }

                        },
                        showLastLabel: true,
                        tickInterval: 60
                    },
                    yAxis: {
                        title: {
                            text: 'Loan Balance($)'
                        },
                        labels: {
                            formatter: function () {

                                return '$'+this.value;
                            }
                        }
                    },
                    tooltip: {
                        // pointFormat: '<b>${point.y:,.0f}</b><br/>  in {point.x} Months'
                        formatter: function () {
                            return '<b>Remaining $' + this.point.y + ' in </b><br/><b>' + this.point.x + ' months<b/>';
                        }
                    },
                    plotOptions: {

                        area: {

                            pointStart: amount,
                            marker: {
                                enabled: false,
                                symbol: 'circle',
                                radius: 2,
                                states: {
                                    hover: {
                                        enabled: true

                                    }
                                }
                            }
                        }
                    },
                    series: [{
                        name: 'Original Loan',
                        data: Points
                    },
                    {
                        name: 'New Loan',
                        data: NewPoints
                    }],
                    exporting: {
                        enabled: false
                    }


                });
              
                $('#buttonExport').click(function () {
                	    
                	var imageURL = '';
                	var svg = chart.getSVG();
                	var dataString = 'type=image/jpeg&filename=results&width=500&svg='+svg;
                	$.ajax({
                	    type: 'POST',
                	    data: dataString,
                	    url: 'http://fintrack.com.au/loancal/export/',
                	    async: false,
                	    success: function(data){
                	        imageURL = data;
                	        var amount = document.getElementById('txtAmount').value;
                	        amount = removeCommas(amount);
                            var openingBalance = parseFloat(amount).toFixed(2);
                            var closingBalance = parseFloat(amount).toFixed(2);
                            var term = document.getElementById('txtTerm').value;
                            var interestRate = document.getElementById('txtInterest').value;
                            var interestRate1 = interestRate;
                            var installment = 0;
                            var actTime=0;
                            var factor=0;
                            var rtype='';
                            var selectedValue = document.getElementById('slctInstallment').value;
                            if (selectedValue == '1') {

                                interestRate = interestRate / 1200;
                                actTime = parseFloat(term) * 12;
                                factor = 12;
                                rtype='Monthly';

                            }
                            if (selectedValue == '2') {

                                //interestRate = interestRate / 2600;
                                //actTime = parseFloat(term) * 26;
                                //factor = 26;
                                interestRate = interestRate / 2608.8;
                                actTime = parseFloat(term) * 26.088;
                                factor = 26.088;
                                rtype='Fortnightly';
                            }
                            installment = Math.round(amount * interestRate / (1 - (Math.pow(1 / (1 + interestRate), actTime))));
                            var Oip,Opot;
                            var CBip,CBpot,CBts;
                            Oip= $('#lblOriginalIntrest').text();
                            Opot=$('#lblOrPayOffTime').text();
                            CBip=$('#lblCashBachIntest').text();
                            CBpot=$('#lblCashBaOffTime').text();
                            CBts = $('#lblTimeSaved').text();
                	        window.open('/loancal/tcpdf/examples/pdftest.php?img='+ imageURL + '&lp=' + amount + '&lt=' + term + '&li=' + interestRate1 + '&fi=' + installment + '&rtype=' + rtype + '&Oip=' + Oip + '&Opot=' + Opot + '&CBip=' + CBip + '&CBpot=' + CBpot + '&CBts='+ CBts);
                	    }
                	});

                    

                });
            });

        }
        $('#lkHideShow').click(function () {
            $('#dvTable').slideToggle('slow', function () {
                // Animation complete.
            });
        });
        $("#lkHideShow").toggle(function () {
            $(this).text("Loan Statement Details Show");
        }, function () {
            $(this).text("Loan Statement Details Hide");
        });
    </script>
    <script type="text/javascript" src="scripts/highcharts.js"></script>
    <script type="text/javascript" src="scripts/highcharts-more.js"></script>
    <script type="text/javascript" src="scripts/exporting.js"></script>



</body>
</html>