

var totalCost = parseFloat($('#item_cost').val());
var rescheduleAmount = parseFloat($('#amount_reschedule').val());
$(".add-more-Quaranty").click(function () {
    $("#sample-div4QuarantyInfo").slideToggle("slow");
});
// allow only number
$("#loAmount, #duration, #loTerm, #step, #loInterestRate,#swlDeposit").keydown(function (e) {
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
        (e.keyCode == 65 && e.ctrlKey === true) ||
        (e.keyCode >= 35 && e.keyCode <= 39)) {
        return;
    }
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
    }
});
$("#_msg_generateCustomBlock").hide();

$(document).ready(function () {


    var num_of_month_dp = 0;
    var num_of_row_dp = 0;
    var first_month_pay_dp = 0;

    $(document).on('click','.add_downpayment', function () {
        nso++;
        var date = lastDateStep('.downpaymenttbody');
        var newDate = Loan_date_generator(date);
        var n = ($('.downpaymenttbody tr').length - 0);
        n++;
        var tr = '<tr>' +
            '<td width="5%">' +
            '<label class="control-label"><strong> Step : </strong></label>' +
            '</td>' +
            '<td width="8%">' +
            '<input type="text" class="form-control" name="payment_no_dp[]" value="' + n + '" readonly="readonly">' +
            '</td>' +
            '<td width="8%">' +
            '<label class="control-label"><strong> Payment Date : </strong></label>' +
            '</td>' +
            '<td>' +
            '<input type="text" id="meng_dp_' + nso + '" class="form-control date-picker date_picker num_of_month_dp" name="itempro_date_dp[]" value="'+newDate+'" >' +
            '</td>' +
            '<td width="9%">' +
            '<label class="control-label"><strong> Payment Amount : </strong></label>' +
            '</td>' +
            '<td>' +
            '<input type="text" name="itempro_payamount_dp[]" class="number-only' + n + ' form-control affectDate itempro_payamount_dp meng_payment_dp" placeholder="Pay Amount" required >' +
            '</td>' +
            '<td width="7%">' +
            '<label class="control-label"><strong> Percentage : </strong></label>' +
            '</td>' +
            '<td>' +
            '<input type="text" name="" class="number-only' + n + ' form-control payPercentage_dp affectDate meng_percent_dp" placeholder="Pay Percentage" required >' +
            '</td>' +
            '<td>' +
            '<span class="pull-right btn-sm button_danger delete fa fa-minus"></span>' +
            '</td>' +
            '</tr>';
        $('.downpaymenttbody').append(tr);
    });

    var num_of_month = 0;
    var nso = 0;
    var num_of_row = 0;
    var first_month_pay = 0;
    $(document).on('click', '.add', function () {
        nso++;
        var date = lastDateStep('.resultbody');
        var newDate = Loan_date_generator(date);
        var n = ($('.resultbody tr').length - 0);
        n++;
        var tr = '<tr>' +
            '<td width="5%">' +
            '<label class="control-label"><strong> Step : </strong></label>' +
            '</td>' +
            '<td width="8%">' +
            '<input type="text" class="form-control" name="payment_no[]" value="' + n + '" readonly="readonly">' +
            '</td>' +
            '<td width="8%">' +
            '<label class="control-label"><strong> Payment Date : </strong></label>' +
            '</td>' +
            '<td>' +
            '<input type="text" id="meng_' + nso + '" class="form-control date-picker date_picker num_of_month" name="itempro_date[]" value="'+newDate+'" required >' +
            '</td>' +
            '<td width="9%">' +
            '<label class="control-label"><strong> Payment Amount : </strong></label>' +
            '</td>' +
            '<td>' +
            '<input type="text" name="itempro_payamount[]" class="number-only' + n + ' form-control itempro_payamount meng_payment" placeholder="Pay Amount" required >' +
            '</td>' +
            '<td width="7%">' +
            '<label class="control-label"><strong> Percentage : </strong></label>' +
            '</td>' +
            '<td>' +
            '<input type="text" name="" class="number-only' + n + ' form-control payPercentage meng_percent" placeholder="Pay Percentage" >' +
            '</td>' +
            '<td>' +
            '<span class="pull-right btn-sm button_danger delete fa fa-minus"></span>' +
            '</td>' +
            '</tr>';
        $('.resultbody').append(tr);
        $('.date_picker').datepicker({
            yearRange: "-100:+0",
            dateFormat: "dd-mm-yy",
            changeMonth: true,
            changeYear: true
        });
    });
    function lastDateStep(classname) {
        var date = '';
        $(classname+' .date_picker').each(function () {
            date = $(this).val();
        });
        return date;
    }
    $(document).on('click','.delete',function(){
        $(this).closest('tr').remove();
        calculate_amount();
    });
    function Loan_date_generator(date) {
        var next_date = date;
        var next_date = next_date.split('-');
        var str_date = next_date[2] + '-' + next_date[1] + '-' + next_date[0];
        var next_date = new Date(str_date);
        next_date.setMonth(next_date.getMonth() + 1);
        var new_next_date = new Date(next_date);
        var dd = new_next_date.getDate();
        var mm = new_next_date.getMonth() + 1;

        var yyyy = new_next_date.getFullYear();
        if (dd < 10) {
            dd = '0' + dd;
        }
        if (mm < 10) {
            mm = '0' + mm;
        }
        return new_next_date = dd + '-' + mm + '-' + yyyy;
    }
    function sum_total_payment() {
        var sum_payment = 0;
        $('.meng_payment').each(function () {
            sum_payment += parseFloat($(this).val());
        });
        $('.total_cost').val(sum_payment.toFixed(2));
    }
    function sum_total_percent() {
        var sum_percent = 0;
        $('.meng_percent').each(function () {
            sum_percent += parseFloat($(this).val());
        });
        $('.total_percent').val(sum_percent.toFixed(2));
    }
    function calculate_amount() {
        var resheduleAmount = $('#owe_amount').val();
        var cost        = parseFloat($('#item_cost').val());
        var totalDp     = totalDownpayment();
        var totalSt     = totalSteppayment();
        var dpPercent   = totalDp*100/cost;
        var stPercent   = totalSt*100/cost;
        var rmAfterDpa  = resheduleAmount - totalDp;
        var rmAfterDpp  = rmAfterDpa*100/cost;
        var remain      = resheduleAmount - totalDp - totalSt;
        var percent     = remain * 100/cost;
        $('.total_cost_dp').val(totalDp);
        $('.main_amount_dp').val(rmAfterDpa);
        $('.total_percent_dp').val(dpPercent);
        $('.main_percent_dp').val(rmAfterDpp);
        $('.total_cost').val(totalSt);
        $('.total_percent').val(stPercent);
        $('.main_amount').val(remain);
        $('.main_percent').val(percent);
        $('#loan_amount_main').val(remain);
    }
    function calculate_percent() {
        var item_cost = Number($('#item_cost').val());
        var pay_percent = $('.total_percent').val();
        var main_percent = (100 - pay_percent);
        $('.main_percent').val(main_percent.toFixed(2));
    }

    function totalDownpayment() {
        total = 0;
        $('.itempro_payamount_dp').each(function () {
          total += parseFloat(!($(this).val()) ? 0 : $(this).val());
        });
        return total;
      }
      function totalSteppayment() {
        total = 0;
        $('.itempro_payamount').each(function () {
          total += parseFloat(!($(this).val()) ? 0 : $(this).val());
        });
        return total;
      }
    $('.number-only').keypress(function (e) {
        if (isNaN(this.value + "" + String.fromCharCode(e.charCode))) return false;
    });

    $(document).on('keyup', '.itempro_payamount_dp', function () {
        var amount = parseFloat($(this).val());
        itspct = amount * 100 / totalCost;
        $(this).closest('td').siblings().find('.payPercentage_dp').val(itspct);
        calculate_amount();
    });
    $(document).on('keyup', '.payPercentage_dp', function () {
        var pay_percentage = $(this).val();
        var itempro_payamount = (totalCost * pay_percentage / 100);
        $(this).closest('td').siblings().find('.itempro_payamount_dp').val(itempro_payamount.toFixed(2));
        calculate_amount();
    });
    $(document).on('keyup', '.itempro_payamount', function () {
        var pay_amount = $(this).val();
        var payPercentage = (pay_amount * 100 / totalCost);
        $(this).closest('td').siblings().find('.payPercentage').val(payPercentage.toFixed(2));
        calculate_amount();
    });
    $(document).on('keyup', '.payPercentage', function () {
        var pay_percentage = $(this).val();
        var itempro_payamount = (totalCost * pay_percentage / 100);
        $(this).closest('td').siblings().find('.itempro_payamount').val(itempro_payamount.toFixed(2));
        calculate_amount();
    });
    function sum_total_dp() {
        var totalDp = 0;
        $('.itempro_payamount_dp').each(function () {
            totalDp += parseFloat(parseFloat(!$(this).val() ? 0 : $(this).val()));
        });
        $('.total_cost_dp').val(totalDp);
        var percentDp = totalDp * 100 / totalCost;
        $('.total_percent_dp').val(percentDp);
        var totalRemainingDp = rescheduleAmount - totalDp;
        $('.main_amount_dp').val(totalRemainingDp);
        $('.main_percent_dp').val(totalRemainingDp * 100 / totalCost);
    }
    function sum_total_pm() {
        var totalPm = 0;
        $('.itempro_payamount').each(function () {
            totalPm += parseFloat(!$(this).val() ? 0 : $(this).val());
        });
        $('.total_cost').val(totalPm);
        $('.total_percent').val(totalPm * 100 / totalCost);
        var totalDp = parseFloat($('.total_cost_dp').val());
        var remainingAmount = rescheduleAmount - totalDp - totalPm;
        $('.main_amount').val(remainingAmount);
        $('.main_percent').val(remainingAmount * 100 / totalCost);
    }
    function AmountRemaining() {
        var totalDp = parseFloat($('.total_cost_dp').val());
        var totalPm = parseFloat($('.total_cost').val());
        var remain = rescheduleAmount - totalDp - totalPm;
        $('#loan_amount_main').val(remain);
    }
    function calculate_amount_dp() {

    }

    function calculate_percent_dp() {

    }
});


var num_of_month2 = 0;
var num_of_row2 = 0;
$('#loan_duration').keypress(function () {
    $('.resultbody3').empty();
    num_of_month2 = 0;
});
$('.generate').on('click', function () {
    $('.resultbody3').empty();
    var ptype = $('#loan_type').val();
    var paymentDate = $("#firstpaydateloan").val();
    if (ptype == 1) {

        var amount = parseFloat($('#loan_amount_main').val());
        var numpay = $('#loan_duration').val();
        var rate = parseFloat($('#loan_interest').val());
        rate = rate / 100;
        var monthly = rate / 12;
        var payment = amount / numpay;
        var total = payment * numpay;
        var interest = total - amount;
        var newPrincipal = amount;
        var to_pay = 0;
        for (var i = 1; i <= numpay; i++) {

            var newInterest = (newPrincipal * rate);
            var reduction = payment + newInterest;
            var newPrincipal = newPrincipal - payment;
            var nextPay_date = $("#firstpaydateloan").val();
            var datearray = nextPay_date.split("-");
            var newdate = datearray[2] + '-' + datearray[1] + '-' + datearray[0];
            var dsss = new Date(newdate);
            var dd = dsss.getDate();
            if (dsss.getMonth() > 11) {
                var mm = '1';
                var yy = dsss.getFullYear() + 1;
            } else {
                var mm = dsss.getMonth() + 1;
                var yy = dsss.getFullYear();
            }
            var newdate = yy + "-" + mm + "-" + dd;

            var date = new Date(newdate);
            date.setMonth(date.getMonth() + num_of_month2);
            //alert(test);




            var tr = '<tr>' +
                '<td width="5%">' +
                '<label class="control-label"> Step</label>' +
                '</td>' +
                '<td style="min-width:80px;">' +
                '<input type="text" class="form-control" name="payment_no2[]" value="' + i + '" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Date</label>' +
                '</td>' +
                '<td style="min-width: 115px;">' +
                '<input type="text" class="form-control date-picker" value="' + paymentDate + '" name="paymentDate2[]" >' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Principal</label>' +
                '</td>' +
                '<td style="min-width: 110px;">' +
                '<input type="text" name="principal[]" class="number-only2 form-control" value="' + payment + '" placeholder="Principal Amount" >' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Interest</label>' +
                '</td>' +
                '<td style="min-width: 110px;">' +
                '<input type="text" name="interest[]" class="number-only2 form-control" value="' + newInterest + '" placeholder="Interest" >' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Payment</label>' +
                '</td>' +
                '<td style="min-width: 110px;">' +
                '<input type="text" name="payAmount2[]" class="number-only2 form-control" value="' + reduction + '" placeholder="Pay Amount" >' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Balance</label>' +
                '</td>' +
                '<td style="min-width: 110px;">' +
                '<input type="text" name="balance[]" class="form-control" value="' + newPrincipal + '" placeholder="Balance" >' +
                '</td>' +
                '</tr>';

            $('.resultbody3').append(tr);
            $(".date-picker").datepicker({
                format: 'yyyy-mm-dd'
            });
            $(".num_of_month2" + num_of_row2).datepicker("setDate", date);
            $('.number-only2' + i).keypress(function (e) {
                if (isNaN(this.value + "" + String.fromCharCode(e.charCode))) return false;
            });
            num_of_month2++;
            num_of_row2++;
            to_pay += payment;
            paymentDate = dateGenerator(paymentDate);
        }
        var num_row2 = num_of_row2 - 1;
        var last_date = $(".num_of_month2" + num_row2).val();

        var dsss = new Date(last_date);
        var dd = dsss.getDate();

        if (dsss.getMonth() == 11) {
            var mm = '1';
            var yy = dsss.getFullYear() + 1;
        } else {
            var mm = dsss.getMonth() + 2;
            var yy = dsss.getFullYear();
        }
        var newdate = dd + "/" + mm + "/" + yy;
        $('#firstpaydateloan_next').val(newdate);
        var own_amount = $('#loan_amount_main').val();
        $('.total_cost1').val(parseFloat(to_pay).toFixed(2));
        $('.main_amount1').val(parseFloat(own_amount - to_pay).toFixed(2));
        $('#total_loan_duration').val(i - 1);
        $('#loan_amount_main_next').val(own_amount - to_pay);
    }
    else {

        var amount = $('#loan_amount_main').val();
        var numpay = $('#loan_duration').val();
        var rate = $('#loan_interest').val();
        rate = rate / 100;
        var monthly = rate / 12;
        var payment = ((amount * rate) / (1 - Math.pow((1 + rate), -numpay)));
        var newPrincipal = amount;
        var to_pay = 0;
        for (var i = 1; i <= numpay; i++) {

            var newInterest = rate * newPrincipal;
            var reduction = payment - newInterest;
            var newPrincipal = newPrincipal - reduction;
            var nextPay_date = $("#firstpaydateloan").val();

            var datearray = nextPay_date.split("-");
            var newdate = datearray[2] + '-' + datearray[1] + '-' + datearray[0];
            var dsss = new Date(newdate);
            var dd = dsss.getDate();

            if (dsss.getMonth() > 11) {
                var mm = '1';
                var yy = dsss.getFullYear() + 1;
            } else {
                var mm = dsss.getMonth() + 1;
                var yy = dsss.getFullYear();
            }
            var newdate = yy + "-" + mm + "-" + dd;

            var date = new Date(newdate);

            date.setMonth(date.getMonth() + num_of_month2);

            var tr = '<tr>' +
                '<td width="5%">' +
                '<label class="control-label"> Step</label>' +
                '</td>' +
                '<td width="6%">' + '<input type="hidden" class="form-control" name="dur" value="' + i + '" readonly="readonly">' +
                '<input type="text" class="form-control" name="payment_no2[]" value="' + i + '" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Date</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" class="form-control date-picker" value="' + paymentDate + '" name="paymentDate2[]" >' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Principal</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" name="principal[]" class="number-only2 form-control" value="' + reduction + '" placeholder="Principal Amount"  readonly>' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Interest</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" name="interest[]" class="number-only2 form-control" value="' + newInterest + '" placeholder="Interest"  readonly>' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Payment</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" name="payAmount2[]" class="number-only2 form-control" value="' + payment + '" placeholder="Pay Amount"  readonly>' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Balance</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" name="balance[]" class="form-control" value="' + newPrincipal + '" placeholder="Balance"  readonly>' +
                '</td>' +
                '</tr>';

            $('.resultbody3').append(tr);

            $(".date-picker").datepicker({
                format: 'yyyy-mm-dd'
            });
            $(".num_of_month2" + num_of_row2).datepicker("setDate", date);
            $('.number-only2' + i).keypress(function (e) {
                if (isNaN(this.value + "" + String.fromCharCode(e.charCode))) return false;
            });

            num_of_month2++;
            num_of_row2++;
            to_pay += reduction;
            paymentDate = dateGenerator(paymentDate);
        }
        var own_amount = $('#loan_amount_main').val();
        $('.total_cost1').val(parseFloat(to_pay).toFixed(2));
        $('.main_amount1').val(parseFloat(own_amount - to_pay).toFixed(2));
        $('#total_loan_duration').val(i - 1);

        var num_row2 = num_of_row2 - 1;
        var last_date = $(".num_of_month2" + num_row2).val();

        var dsss = new Date(last_date);
        var dd = dsss.getDate();

        if (dsss.getMonth() == 11) {
            var mm = '1';
            var yy = dsss.getFullYear() + 1;
        } else {
            var mm = dsss.getMonth() + 2;
            var yy = dsss.getFullYear();
        }
        var newdate = dd + "/" + mm + "/" + yy;
        $('#firstpaydateloan_next').val(newdate);
        $('#loan_amount_main_next').val(own_amount - to_pay);
    }

});

function auto_generate() {
    $('.resultbody3').empty();
    var ptype = $('#loan_type').val();
    var paymentDate = $("#firstpaydateloan").val();
    if (ptype == 1) {

        var amount = parseFloat($('#loan_amount_main').val());
        var numpay = $('#loan_duration').val();
        var rate = parseFloat($('#loan_interest').val());
        rate = rate / 100;
        var monthly = rate / 12;
        var payment = amount / numpay;
        var total = payment * numpay;
        var interest = total - amount;
        var newPrincipal = amount;
        var to_pay = 0;

        for (var i = 1; i <= numpay; i++) {

            var newInterest = (newPrincipal * rate);
            var reduction = payment + newInterest;
            var newPrincipal = newPrincipal - payment;
            var nextPay_date = $("#firstpaydateloan").val();

            var datearray = nextPay_date.split("-");
            var newdate = datearray[2] + '-' + datearray[1] + '-' + datearray[0];
            var dsss = new Date(newdate);
            var dd = dsss.getDate();
            if (dsss.getMonth() > 11) {
                var mm = '1';
                var yy = dsss.getFullYear() + 1;
            } else {
                var mm = dsss.getMonth() + 1;
                var yy = dsss.getFullYear();
            }
            var newdate = yy + "-" + mm + "-" + dd;

            var date = new Date(newdate);
            date.setMonth(date.getMonth() + num_of_month2);
            //alert(test);

            var tr = '<tr>' +
                '<td width="5%">' +
                '<label class="control-label"> Step</label>' +
                '</td>' +
                '<td style="min-width:80px;">' +
                '<input type="text" class="form-control" name="payment_no2[]" value="' + i + '" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Date</label>' +
                '</td>' +
                '<td style="min-width: 115px;">' +
                '<input type="text" class="form-control date-picker num_of_month2" value="' + paymentDate + '" name="paymentDate2[]" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Principal</label>' +
                '</td>' +
                '<td style="min-width: 110px;">' +
                '<input type="text" name="principal[]" class="number-only2 form-control" value="' + payment + '" placeholder="Principal Amount" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Interest</label>' +
                '</td>' +
                '<td style="min-width: 110px;">' +
                '<input type="text" name="interest[]" class="number-only2 form-control" value="' + newInterest + '" placeholder="Interest" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Payment</label>' +
                '</td>' +
                '<td style="min-width: 110px;">' +
                '<input type="text" name="payAmount2[]" class="number-only2 form-control" value="' + reduction + '" placeholder="Pay Amount" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Balance</label>' +
                '</td>' +
                '<td style="min-width: 110px;">' +
                '<input type="text" name="balance[]" class="form-control" value="' + newPrincipal + '" placeholder="Balance" readonly="readonly">' +
                '</td>' +
                '</tr>';

            $('.resultbody3').append(tr);
            $(".date-picker").datepicker({
                format: 'yyyy-mm-dd'
            });
            $(".num_of_month2" + num_of_row2).datepicker("setDate", date);
            $('.number-only2' + i).keypress(function (e) {
                if (isNaN(this.value + "" + String.fromCharCode(e.charCode))) return false;
            });
            paymentDate = dateGenerator(paymentDate);
            num_of_month2++;
            num_of_row2++;
            to_pay += payment;
        }
        var num_row2 = num_of_row2 - 1;
        var last_date = $(".num_of_month2" + num_row2).val();

        var dsss = new Date(last_date);
        var dd = dsss.getDate();

        if (dsss.getMonth() == 11) {
            var mm = '1';
            var yy = dsss.getFullYear() + 1;
        } else {
            var mm = dsss.getMonth() + 2;
            var yy = dsss.getFullYear();
        }
        var newdate = dd + "/" + mm + "/" + yy;
        $('#firstpaydateloan_next').val(newdate);
        var own_amount = $('#loan_amount_main').val();
        $('.total_cost1').val(parseFloat(to_pay).toFixed(2));
        $('.main_amount1').val(parseFloat(own_amount - to_pay).toFixed(2));
        $('#total_loan_duration').val(i - 1);
        $('#loan_amount_main_next').val(own_amount - to_pay);
    }
    else {

        var amount = $('#loan_amount_main').val();
        var numpay = $('#loan_duration').val();
        var rate = $('#loan_interest').val();
        rate = rate / 100;
        var monthly = rate / 12;
        var payment = ((amount * rate) / (1 - Math.pow((1 + rate), -numpay)));
        var newPrincipal = amount;
        var to_pay = 0;
        for (var i = 1; i <= numpay; i++) {

            var newInterest = rate * newPrincipal;
            var reduction = payment - newInterest;
            var newPrincipal = newPrincipal - reduction;
            var nextPay_date = $("#firstpaydateloan").val();
            var datearray = nextPay_date.split("-");
            var newdate = datearray[2] + '-' + datearray[1] + '-' + datearray[0];
            var dsss = new Date(newdate);
            var dd = dsss.getDate();

            if (dsss.getMonth() > 11) {
                var mm = '1';
                var yy = dsss.getFullYear() + 1;
            } else {
                var mm = dsss.getMonth() + 1;
                var yy = dsss.getFullYear();
            }
            var newdate = yy + "-" + mm + "-" + dd;

            var date = new Date(newdate);

            date.setMonth(date.getMonth() + num_of_month2);

            var tr = '<tr>' +
                '<td width="5%">' +
                '<label class="control-label"> Step</label>' +
                '</td>' +
                '<td width="6%">' + '<input type="hidden" class="form-control" name="dur" value="' + i + '" readonly="readonly">' +
                '<input type="text" class="form-control" name="payment_no2[]" value="' + i + '" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Date</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" class="form-control date-picker num_of_month2" value="' + paymentDate + '" name="paymentDate2[]" readonly="readonly">' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Principal</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" name="principal[]" class="number-only2 form-control" value="' + reduction + '" placeholder="Principal Amount"  readonly>' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Interest</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" name="interest[]" class="number-only2 form-control" value="' + newInterest + '" placeholder="Interest"  readonly>' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Payment</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" name="payAmount2[]" class="number-only2 form-control" value="' + payment + '" placeholder="Pay Amount"  readonly>' +
                '</td>' +
                '<td>' +
                '<label class="control-label"> Balance</label>' +
                '</td>' +
                '<td>' +
                '<input type="text" name="balance[]" class="form-control" value="' + newPrincipal + '" placeholder="Balance"  readonly>' +
                '</td>' +
                '</tr>';

            $('.resultbody3').append(tr);

            $(".date-picker").datepicker({
                format: 'yyyy-mm-dd'
            });
            $(".num_of_month2" + num_of_row2).datepicker("setDate", date);
            $('.number-only2' + i).keypress(function (e) {
                if (isNaN(this.value + "" + String.fromCharCode(e.charCode))) return false;
            });
            paymentDate = dateGenerator(paymentDate);
            num_of_month2++;
            num_of_row2++;
            to_pay += reduction;
        }
        var own_amount = $('#loan_amount_main').val();
        $('.total_cost1').val(parseFloat(to_pay).toFixed(2));
        $('.main_amount1').val(parseFloat(own_amount - to_pay).toFixed(2));
        $('#total_loan_duration').val(i - 1);

        var num_row2 = num_of_row2 - 1;
        var last_date = $(".num_of_month2" + num_row2).val();

        var dsss = new Date(last_date);
        var dd = dsss.getDate();

        if (dsss.getMonth() == 11) {
            var mm = '1';
            var yy = dsss.getFullYear() + 1;
        } else {
            var mm = dsss.getMonth() + 2;
            var yy = dsss.getFullYear();
        }
        var newdate = dd + "/" + mm + "/" + yy;
        $('#firstpaydateloan_next').val(newdate);
        $('#loan_amount_main_next').val(own_amount - to_pay);
    }
}
function dateGenerator(date) {
    var next_date = date;
    var next_date = next_date.split('-');
    var str_date = next_date[2] + '-' + next_date[1] + '-' + next_date[0];
    var next_date = new Date(str_date);
    next_date.setMonth(next_date.getMonth() + 1);
    var new_next_date = new Date(next_date);
    var dd = new_next_date.getDate();
    var mm = new_next_date.getMonth() + 1;

    var yyyy = new_next_date.getFullYear();
    if (dd < 10) {
        dd = '0' + dd;
    }
    if (mm < 10) {
        mm = '0' + mm;
    }
    return new_next_date = dd + '-' + mm + '-' + yyyy;
}
