/* -------------------------------------------------------------------------- */
/*  Repayment amount                                                          */
/* -------------------------------------------------------------------------- */
function repaymentAmount(totalAmount, interestRate, total_payments, freq_per_year) {

    var rate = interestRate / 100 / freq_per_year;
        
    if (total_payments == 0) {
        return 0;
    } 
    total_payments = Math.floor(total_payments);
    if (interestRate == 0) {
        return totalAmount / total_payments;        
    } else {
        return (rate + rate/(Math.pow(rate + 1, total_payments)-1)) * totalAmount;
    } 
} 
function showRepaymentAmount(amount, freq_per_year) {
  switch (freq_per_year) {
    case "12": $('#periodical').html('Monthly'); break;
    case "26": $('#periodical').html('Fortnightly'); break;
    case "52": $('#periodical').html('Weekly'); break;
  }
  $('#monthly_repayment').html(amount);
  $('#monthly_repayment').formatCurrency();
}


/* -------------------------------------------------------------------------- */
/*  Total interest payable                                                    */
/* repayment_type = 1 ==> Principal & Interest                                */ 
/* repayment_type = 2 ==> Interest only                                       */
/* -------------------------------------------------------------------------- */
function totalInterest(totalAmount, interestRate, term, payments_per_year, repayment_type, repayment_amount) {
  if (repayment_type == 1) 
    return repaymentAmount(totalAmount, interestRate, term, payments_per_year)*term - totalAmount;
  if (repayment_type == 2) {
    return repayment_amount*term;
  }
}
function showTotalInterest(amount) {
  $('#total_interest').html(amount);
  $('#total_interest').formatCurrency();
}


/* -------------------------------------------------------------------------- */
/*  Principal calculation                                                     */
/* -------------------------------------------------------------------------- */
function newPrincipal(principal, interestRate, repaymentAmount, term) 
{
    var value = principal + principal * interestRate / 100 / term - repaymentAmount;
    if (value < 0) value = 0;
    return value;
}


/* -------------------------------------------------------------------------- */
/*  Interest only calculation                                                 */
/* -------------------------------------------------------------------------- */
function newInterestOnly(totalAmount, interest_only, term) {
    var value = totalAmount + interest_only * term;
    if (value < totalAmount) value = totalAmount;
    return value;
}

function interestOnly(totalAmount, interestRate, total_payments, term, freq_per_year) {
  var rate = interestRate / 100 / freq_per_year * term;
  return totalAmount * rate / term;
}
function showInterestOnly(amount) {
  $('#monthly_repayment').html(amount);
  $('#monthly_repayment').formatCurrency(); 
}


/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/*  Output                                                                    */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
function output() {
            var graph_total = [], graph_principal = [], graph_interest_only = [];
            var loan_term = $('#loan_term').val(); // x
            var step = loan_term / 12;
            
            var repayment_amount = repaymentAmount(
                $('#loan_amount').asNumber(),
                $('#interest_rate').asNumber(),
                $('#loan_term').asNumber() * $('#payment_freq').val(),
                $('#payment_freq').val()
            );
            showRepaymentAmount(repayment_amount, $('#payment_freq').val());
            
            // *******
            // Principal & interest
            // *******
            if ($('#repayment_type').val() == 1) {

                var cur_month = 0;
                var principal = $('#loan_amount').asNumber();
                var months_remains = $('#loan_term').asNumber() * $('#payment_freq').val();
                while( months_remains >= 0) {
                    graph_total.push([cur_month, repayment_amount*months_remains]);
                    graph_principal.push([cur_month, principal]);
//if (cur_month==48) alert(principal);                    
                    principal = newPrincipal(
                        principal, 
                        $('#interest_rate').asNumber(), 
                        repayment_amount, 
                        $('#payment_freq').val());
                    ++cur_month;
                    --months_remains;
                }


                plot1.setData([ 
                { 
                    data: graph_total,
                    lines: { show: true, steps: true }, 
                    label: "Total", 
                    color: "#6285C9" 
                }
                , { 
                    data: graph_principal,
                    lines: { show: true, steps: true }, 
                    label: "Principal"
                }
                 ]);
            }
             
            // *******
            // Interest only
            // *******
            if ($('#repayment_type').val() == 2) {
            
                var interest_only = interestOnly(
                        $('#loan_amount').asNumber(), // 12000-
                        $('#interest_rate').asNumber(),
                        $('#payment_freq').val(),
                        $('#loan_term').asNumber() * $('#payment_freq').val(),
                        $('#payment_freq').val()
                   )
                showInterestOnly(interest_only);
                
                var cur_month = 0;
                var months_remains = $('#loan_term').asNumber() * $('#payment_freq').val();
                while( months_remains >= 0) {
                    graph_interest_only.push([cur_month, 
                        newInterestOnly(
                            $('#loan_amount').asNumber(), 
                            interest_only,
                            $('#loan_term').asNumber() * $('#payment_freq').val() - cur_month
                        )
                    ]);
                    --months_remains;
                    ++cur_month;
                }

                plot1.setData([ 
                { 
                    data: graph_interest_only,
                    lines: { show: true, steps: true }, 
                    label: "Interest only", 
                    color: "#6285C9" 
                }
                 ]);
                 
                repayment_amount = interest_only; 
            }
             
             
             
             
            plot1.setupGrid();
            plot1.draw();
            //$('#transport_registration_a').asNumber() +
            
            showTotalInterest(
                totalInterest(
                    $('#loan_amount').asNumber(),
                    $('#interest_rate').asNumber(),
                    $('#loan_term').asNumber() * $('#payment_freq').val(),
                    $('#payment_freq').val(),
                    $('#repayment_type').val(),
                    repayment_amount
                )
            );

}

