function percentage(num, per) {
    return (num / 100) * per;
}

function validateData(self) {

    var customer = $(self).find('#customer_id').val();
    var unit_number = $(self).find('#unit_number').val();
    var original_boooking_date = $(self).find('#original_boooking_date').val();
    var dealer_id = $(self).find('#dealer_id').val();
    var total_rebate1 = $(self).find('#total_rebate1').val();
    var schedule_start_date = $(self).find('#schedule_start_date').val();
    var booking_date = $(self).find('#booking_date').val();
    var possession = $(self).find('#possession').val();
    var down_payment = $(self).find('#down-payment').val();
    var allotment = $(self).find('#allotment').val();
    var confirmation = $(self).find('#confirmation').val();
    var booking = $(self).find('#booking').val();

    var file_processing = $(self).find('#file_processing').val();
    var registration = $(self).find('#registration').val();
    var installment = $(self).find('#installment').val();

    var ballon_date = $(self).find('#development').val();
    var ballon_payment = $(self).find('#file_processing').val();
    var bullating_date = $(self).find('#registration').val();
    var bullating_payment = $(self).find('#installment').val();


    if (confirmation == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }

    if (booking == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }


    if (development == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }



    if (file_processing == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }

    if (registration == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }


    if (installment == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }



    if (possession == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }

    if (down_payment == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }


    if (allotment == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }



    if (schedule_start_date == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }

    if (booking_date == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }
    if (bullating_payment == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }
    if (bullating_date == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }

    if (ballon_payment == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }

    if (ballon_date == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }



    //     if (total_rebate1 == "")
    //     {
    //         $('#required_field_error').show();
    //         setTimeout(function() { 
    //         $('#required_field_error').hide();        
    //         }, 3000);
    // return false;
    //     }

    //     if (dealer_id == "")
    //     {
    //         $('#required_field_error').show();
    //         setTimeout(function() { 
    //         $('#required_field_error').hide();        
    //         }, 3000);
    // return false;
    //     }

    //    if (customer == "")
    //     {
    //         $('#required_field_error').show();
    //         setTimeout(function() { 
    //         $('#required_field_error').hide();        
    //         }, 3000);
    // return false;
    //     }


    if (unit_number == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }


    if (original_boooking_date == "") {
        $('#required_field_error').show();
        setTimeout(function() {
            $('#required_field_error').hide();
        }, 3000);
        return false;
    }



}

function hideFields(self) {

    if (self === 'company') {
        $('#father_name').val('null');
        $('#father_name_parent').hide();
        $('#passport_no').hide()
        $('#date_of_birth').html('Date of Incorporation <span style="color: red">*</span>')
        $('#cnic_no').html('Incorporation no <span style="color: red">*</span>')
        $('#mobile_number').html('Phone Number <span style="color: red">*</span>')
        $('#res_number').hide()
        $('#phone_number').hide()
        $('#photo_s').hide()
        $('#mailing_label').html('Company Registered Address <span style="color: red">*</span>')
        $('#permanent_address').val($('#mailing_address').val())
        $('#permanent_add').hide()
        $('#occupations').html('Type Of Company <span style="color: red">*</span>')
        $('#imgs_label').html('Certificate  <span style="color: red">*</span>')
        $('#type').val(1)
    }

    if (self === 'individual') {
        $('#type').val(0)
        $('#father_name').val('');
        $('#father_name_parent').show();
        $('#passport_no').show()
        $('#date_of_birth').html('Date of Birth <span style="color: red">*</span>')
        $('#cnic_no').html('Cnic <span style="color: red">*</span>')
        $('#mobile_number').html('Mobile Number <span style="color: red">*</span>')
        $('#res_number').show()
        $('#photo_s').show()
        $('#phone_number').show()
        $('#mailing_label').html('Mailing Address <span style="color: red">*</span>')
        $('#permanent_add').show()
        $('#occupations').html('Occupation <span style="color: red">*</span>')
        $('#imgs_label').html('Passport size photo  <span style="color: red">*</span>')
    }


    return false;
}

function validateCnic(self) {
    var cnic = $(self).val().replaceAll('-', '');

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $.ajax({
        url: "{{ route('validate.dnfbp.cnic') }}",
        method: 'POST',
        dataType: 'json',
        data: {
            cnic: cnic
        },
        success: function(data) {
            if (data.response == 'invalid') {
                $(self).closest('.form-group').find('#cnic_error1').show();
            }

        }
    })



    return false;
}

//multiple nominees
$(document).ready(function() {
    var max_fields = 5; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap4"); //Fields wrapper

    var add_button = $(".add_field_button4"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e) { //on add input button click
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $(wrapper).append(
                '<div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">Nominee Name <span style="color: red">*</span></label><div class="col-md-8"><input type="text" class="form-control" name="nominee_name_1[]" value="" id="exampleInputuname" placeholder="Enter Name"></div></div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">S/O, D/O, W/O.<span style="color: red">*</span></label><div class="col-md-8"><input  type="text" class="form-control" name="nominee_so_do_wo_1[]" value="" id="exampleInputuname" placeholder="Enter Father Name"></div></div></div><div <div class="form-group"><label class="control-label col-md-2">CNIC</label><div class="col-md-4"><input type="text"  class="form-control" value="" name="nominee_cnic_1[]" id="nominee_cnic"  placeholder="XXXXXXXXXXXXX" onfocusout="return validateCnic(this)" style="width: 393px; margin-left: 8px;"></div><div class="col-md-12" style="color: red;display: none;" id="cnic_error1">This Customer cnic is found in list of DNFBP. You cant add this Customer record.</div></div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">Passport No.</label><div class="col-md-8"><input type="text"  class="form-control" value="" name="nominee_passport_no_1[]" id="nominee_passport_no"  placeholder="XXXXXXXXX" ></div></div></div><div class="clearfix"></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">Relation With Applicant <span style="color: red">*</span></label><div class="col-md-8"><input type="text" class="form-control" name="nominee_relation_1[]" value=""  placeholder="Enter Relation"></div></div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">Email</label><div class="col-md-8"><input type="text" class="form-control" name="nominee_email_1[]" value=""  placeholder="Enter Email"></div></div></div><div class="clearfix"></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">Mobile Number </label><div class="col-md-8"><input type="text" class="form-control" name="nominee_mobile_number_1[]" value=""  placeholder="Enter Mobile Number"></div></div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">Res. Number </label><div class="col-md-8"><input type="text" class="form-control" name="nominee_res_number_1[]" value=""  placeholder="Enter Res Number"></div></div></div><div class="clearfix"></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">Phone Number </label><div class="col-md-8"><input type="text" class="form-control" name="nominee_phone_number_1[]" value=""  placeholder="Enter Phone Number"></div></div></div><a href="#"class="remove_field" style="background: red; padding: 6px 12px; color: white; border: 2px solid red; border-radius: 4px; font-size:">Remove</a>'
            ); //add input box

        }
    });

    $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })

});

$(document).ready(function() {
    var max_fields = 2; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap3"); //Fields wrapper

    var add_button = $(".add_field_button3"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e) { //on add input button click
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $(wrapper).append(
                '<div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">Nominee Name <span style="color: red">*</span></label><div class="col-md-9"><input type="text" class="form-control" name="nominee_name_n" value="" id="exampleInputuname" placeholder="Enter Name"></div></div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">S/O, D/O, W/O.<span style="color: red">*</span></label><div class="col-md-9"><input  type="text" class="form-control" name="nominee_so_do_wo_n" value="" id="exampleInputuname" placeholder="Enter Father Name"></div></div></div><div class="clearfix"></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">CNIC</label><div class="col-md-9"><input type="text"  class="form-control" value="" name="nominee_cnic_n" id="nominee_cnic"  placeholder="XXXXXXXXXXXXX"  onfocusout="return validateCnic(this)"><input type="radio" name="nominee_cnic_flag_n" value="1"> Locally<input type="radio" name="nominee_cnic_flag_n" value="2"> Internationally</div><div style="color: red;display: none;" class="col-md-12"  id="cnic_error1">This Customer cnic is found in list of DNFBP. You cant add this Customer record. </div></div></div> <div class="col-md-6"><div class="form-group" id="nominee_address"><label for="address" class="col-sm-3 control-label" id="nominee_address">Nominee Address <span style="color: red">*</span></label><div class="col-sm-9"><div class="input-group"><div class="input-group-addon"><i class="icon-location-pin"></i></div><textarea class="form-control" id="nominee_address" name="nominee_address" ></textarea></div></div> </div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">Passport No.</label><div class="col-md-9"><input type="text"  class="form-control" value="" name="nominee_passport_no_n" id="nominee_passport_no"  placeholder="XXXXXXXXX" ></div></div></div><div class="clearfix"></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">Relation With Applicant <span style="color: red">*</span></label><div class="col-md-9"><input type="text" class="form-control" name="nominee_relation_n" value=""  placeholder="Enter Relation"></div></div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">Email</label><div class="col-md-9"><input type="text" class="form-control" name="nominee_email_n" value=""  placeholder="Enter Email"></div></div></div><div class="clearfix"></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">Mobile Number </label><div class="col-md-9"><input type="text" class="form-control" name="nominee_mobile_number_n" value=""  placeholder="Enter Mobile Number"></div></div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">Res. Number </label><div class="col-md-9"><input type="text" class="form-control" name="nominee_res_number_n" value=""  placeholder="Enter Res Number"></div></div></div><div class="clearfix"></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-3">Phone Number </label><div class="col-md-9"><input type="text" class="form-control" name="nominee_phone_number_n" value=""  placeholder="Enter Phone Number"></div></div></div><a href="#"class="remove_field" style="background: red; color: white; border-radius: 4px; border: 2px solid red; padding: 6px">Remove</a>'
            ); //add input box

        }
    });

    $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })

});


//multiple dealers
$(document).ready(function() {
    var max_fields = 5; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap5"); //Fields wrapper

    var add_button = $(".add_field_button5"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e) { //on add input button click
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $(wrapper).append(
                '<div style="margin-bottom: 10px;"><hr><div class="col-md-6"><div class="form-group"><label class="control-label col-md-4">Authorized Dealer <span style="color: red">*</span></label><div class="col-md-8"><select class="form-control chosen-select" name="dealer_id[]" ><option value="" >None</option>@foreach ($dealers as $dealer)@foreach ($dealer as $d)@if ($loop->first)<optgroup label="{{ $dealerTypes[$d->dealer_type_id] }}">@endif<option value="{{ $d->id }}" >{{ $d->name }} ( {{ $d->percentage }} %)</option>@endforeach</optgroup>@endforeach</select></div></div></div><div class="col-md-6"><div class="form-group"><label class="control-label col-md-2">Rebate <span style="color: red">*</span></label><div class="col-md-8"><div class="input-group"><span class="input-group-addon" style="font-size: 10px;">PKR</span><input id="total_rebate' +
                x +
                '" type="text" class="form-control total_rebate" name="total_rebate[]" placeholder="Rebate" value="0" ></div></div></div></div> <br><div class="clearfix"></div><div class="clearfix"></div> <a href="#"class="remove_field" style="background: red; padding: 6px 12px; color: white; border: 2px solid red; border-radius: 4px; font-size: 14px;">Remove</a>'
            ); //add input box

        }
    });

    $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })

});


$('#total_rebate1').keyup(function() {

    downPC = $('#d_rebate_p1').val();
    rebate = $('#total_rebate1').val();

    $('#d_rebate1').val(Math.round((downPC * rebate) / 100));

    insPC = $('#i_rebate_p1').val();
    $('#i_rebate1').val(Math.round((insPC * rebate) / 100));

});

$('#d_rebate1').keyup(function() {
    downVC = $('#d_rebate1').val();
    rebate = $('#total_rebate1').val();

    $('#d_rebate_p1').val(Math.round((downVC * 100) / rebate));
    $('#i_rebate_p1').val(Math.round(100 - $('#d_rebate_p1').val()));

    insPC = $('#i_rebate_p1').val();
    rebate = $('#total_rebate1').val();

    $('#i_rebate1').val(Math.round((insPC * rebate) / 100));

});

$('#d_rebate_p1').keyup(function() {

    downPC = $('#d_rebate_p1').val();

    rebate = $('#total_rebate1').val();

    $('#d_rebate1').val(Math.round((downPC * rebate) / 100));
    $('#i_rebate_p1').val(Math.round(100 - downPC));

    insPC = $('#i_rebate_p1').val();
    rebate = $('#total_rebate1').val();

    $('#i_rebate1').val(Math.round((insPC * rebate) / 100));

});



function openRebate(val, id) {

    if (val) {
        $('.preloader').fadeIn();
        var data = {
            'id': val
        };
        $.get('/getDealerById', data, function(data) {
            amount = $('#total-amount').val();
            if (data.rebate_in_percent)
                rebate = (data.percentage * amount) / 100;
            else
                rebate = data.percentage;
            $('#percentage' + id).val(data.percentage);
            $('#rebate_in_percent' + id).val(data.rebate_in_percent);
            $('#total_rebate' + id).val(rebate);
            $('#d_rebate' + id).val(Math.round((rebate * data.down_payment_rebate) / 100));
            $('#i_rebate' + id).val(Math.round((rebate * data.installment_rebate) / 100));
            $('#d_rebate_p' + id).val(Math.round(data.down_payment_rebate));
            $('#i_rebate_p' + id).val(Math.round(data.installment_rebate));
            $('#p_rebate' + id).val(Math.round(data.paid_installment_rebate));
        });
        rebate = 'rebate' + id;
        document.getElementById(rebate).style.display = 'block';
        $('.preloader').fadeOut();
    } else
        document.getElementById('rebate').style.display = 'none';
}

$(document).ready(function() {
    var max_fields = 50; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap1"); //Fields wrapper
    var wrapper1 = $(".input_fields_wrap"); //Fields wrapper
    var add_button = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e) { //on add input button click
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $(wrapper).append(
                '<div class="clearfix"><br><div class="form-group"><label class="control-label col-md-2" style="margin-left: 55px;">Add Extra Payment<span style="color: red">*</span></label><div class="col-md-8"><input style="display: inline-block;width: 49%;" onkeyup="addPayment()" type="integer" class="form-control" name="add_amount[]" placeholder="Amount"><input style="display: inline-block;width: 49%; margin-left: 10px;" type="date" class="form-control"  name="add_date[]"  placeholder=""></div> </div><a href="#" class="remove_field" style="background: red; padding: 6px 12px; color: white; border-radius: 4px; border: 2px solid red; font-size: 14px;">Remove</a>'
            ); //add input box
            $(".bdpk").datepicker({
                dateFormat: "dd-mm-yy"
            });
        }
    });

    $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })
    $(wrapper1).on("click", ".remove_field", function(e) { //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })
});

//custom
$(document).ready(function() {
    var max_fields = 100; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap2"); //Fields wrapper
    var wrapper1 = $(".input_fields_wrap2"); //Fields wrapper
    var add_button = $(".add_field_button2"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e) { //on add input button click
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $(wrapper).append(
                '<div class="clearfix"><br><div class="form-group"><label class="control-label col-md-3">Installment-' +
                x +
                '<br> <span style="color: red">*</span></label><div class="col-md-9"><input style="display: inline-block;width: 43%;margin-right: 4px;" type="date" class="form-control" name="custom_date[]" placeholder="Date"><input style="display: inline-block;width: 49%;" type="integer" class="form-control" name="custom_amount[]" onkeyup="customPayment()"  placeholder="Amount"></div> </div><a href="#" class="remove_field">Remove</a>'
            ); //add input box
            $(".bdpk").datepicker({
                dateFormat: "dd-mm-yy"
            });
        }
    });

    $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })
    $(wrapper1).on("click", ".remove_field", function(e) { //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })
});

//end custom

function customPayment() {

    // net_amount = parseInt($('#net_amount').val());
    // down_payment = parseInt($('#down-payment').val());
    // possession = parseInt($('#possession').val());

    var cus_amount = 0;

    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }

    });

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    allotment = parseInt($('#allotment').val());
    confirmation = parseInt($('#confirmation').val());
    booking = parseInt($('#booking').val());

    file_processing = parseInt($('#file_processing').val());
    registration = parseInt($('#registration').val());
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());
    remaining_amount = net_amount - down_payment - possession - allotment - confirmation - booking -
        file_processing - registration - cus_amount - ballon - bullating;

    // remaining_amount = net_amount - down_payment - possession - cus_amount;

    $('#custome_rem').val(Math.round(remaining_amount));

}

function addPayment() {

    net_amount = parseInt($('#net_amount').val());
    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    }


    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });


    remaining_amount = net_amount - down_payment - possession - monthly_installment - annual_installment -
        add_amount;

    $('#remaining_amount').val(remaining_amount);

    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
}


function receiptBox() {
    if ($('#receipt').prop("checked") == true) {
        $('#receipt_div').css('display', 'block');
    } else if ($('#receipt').prop("checked") == false) {
        $('#receipt_div').css('display', 'none');
    }
}

function fullPaymentBox() {
    if ($('#discount_full_payment').prop("checked") == true) {
        $('#discount_div').css('display', 'block');
    } else if ($('#discount_full_payment').prop("checked") == false) {
        $('#discount_div').css('display', 'none');
    }
}

function changeFunc(a) {
    if (a == {{ \App\Receipt::cash }}) {
        $('#bank').css('display', 'none');
        $('#company-bank').css('display', 'none');
        $('#cheque').css('display', 'none');
        $('#adjustment').css('display', 'none');
    } else if (a == {{ \App\Receipt::bank }}) {
        $('#bank').css('display', 'block');
        $('#company-bank').css('display', 'block');
        $('#cheque').css('display', 'none');
        $('#adjustment').css('display', 'none');
    } else if (a == {{ \App\Receipt::cheque }}) {
        $('#bank').css('display', 'block');
        $('#company-bank').css('display', 'block');
        $('#cheque').css('display', 'block');
        $('#adjustment').css('display', 'none');
    } else if (a == {{ \App\Receipt::adjustment }}) {
        $('#bank').css('display', 'none');
        $('#company-bank').css('display', 'none');
        $('#cheque').css('display', 'none');
        $('#adjustment').css('display', 'block');
    }
}




$('input[type="checkbox"]').click(function() {
    if ($(this).prop("checked") == true) {
        $('textarea#permanent_address').val($('textarea#mailing_address').val());
    } else if ($(this).prop("checked") == false) {
        $('textarea#permanent_address').val('');
    }
});

function updateSchedule() {

    var type = $('#type').val();
    var size = $('#size').val();
    var floor = $('#floor').val();

    var data = {
        'type': type,
        'size': size,
        'floor': floor
    };
    $.get('/statement/updateSchedule', data, function(data) {
        // console.log(data);
        if (data) {
            $('#cost-amount').val(Math.round(data.cost_amount));
            $('#down-payment').val(Math.round(data.down_payment / 100 * data.cost_amount));
            $('#possession').val(Math.round(data.possession / 100 * data.cost_amount));
            $('#annual-installment').val(Math.round(data.annual_installment / 100 * data.cost_amount /
                4));
            $('#total-months').val(Math.round(data.total_months));
            $('#total_annual_installment_count').val(Math.round(data.total_months / 12));
            $('#total_semi_installment_count').val(Math.round(data.total_months / 6));
            $('#total_quarterly_installment_count').val(Math.round(data.total_months / 3));
            $('#total_bi_mon_installment_count').val(Math.round(data.total_months / 2));

            $('#installment').val(Math.round(data.installment / 100 * data.cost_amount / data
                .total_months));

            updateCurrency();
        }

    });

}




var a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ',
    'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '
];
var b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

//update the cost label




//update the registration

$('#registration').keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());
    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    registration = parseInt($('#registration').val());
    possession = parseFloat($('#possession').val());
    $('#per_registration').val(((registration / net_amount) * 100).toFixed(2));
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;
    num = $("#registration").val();
    $("#registration-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }

    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });
    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(registration)) {
        registration = 0;
        $('#per_registration').val(0);

    }
    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;

    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});



//update the per registraiton

$('#per_registration').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());
    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    per_registration = parseFloat($('#per_registration').val());
    possession = parseFloat($('#possession').val());

    $('#registration').val(percentage(net_amount, per_registration));

    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    num = $("#registration").val();
    $("#registration-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }

    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(registration)) {
        registration = 0;
        $('#registration').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});


//update the file processing

$('#file_processing').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());

    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    file_processing = parseInt($('#file_processing').val());
    possession = parseFloat($('#possession').val());
    $('#per_file_processing').val(((file_processing / net_amount) * 100).toFixed(2));
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    num = $("#file_processing").val();
    $("#file_processing-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(file_processing)) {
        file_processing = 0;
        $('#per_file_processing').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;

    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});



//update the per file processing

$('#per_file_processing').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());
    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    per_file_processing = parseFloat($('#per_file_processing').val());
    possession = parseFloat($('#possession').val());

    $('#file_processing').val(percentage(net_amount, per_file_processing));

    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    num = $("#file_processing").val();
    $("#file_processing-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }

    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(file_processing)) {
        file_processing = 0;
        $('#file_processing').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});

//update the booking

$('#booking').keyup(function() {


    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());

    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    booking = parseInt($('#booking').val());
    possession = parseFloat($('#possession').val());
    $('#per_booking').val(((booking / net_amount) * 100).toFixed(2));
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    num = $("#booking").val();
    $("#booking-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }




    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());


    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());

    if (isNaN(booking)) {
        booking = 0;
        $('#per_booking').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});




//update the per booking

$('#per_booking').keyup(function() {


    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());
    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    per_booking = parseFloat($('#per_booking').val());
    possession = parseFloat($('#possession').val());

    $('#booking').val(percentage(net_amount, per_booking));

    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    num = $("#booking").val();
    $("#booking-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(booking)) {
        booking = 0;
        $('#per_booking').val(0);
        $('#booking').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});



//update the confirmation

$('#confirmation').keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());

    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    confirmation = parseInt($('#confirmation').val());
    possession = parseFloat($('#possession').val());
    $('#per_confirmation').val(((confirmation / net_amount) * 100).toFixed(2));
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;


    num = $("#confirmation").val();
    $("#confirmation-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });


    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(confirmation)) {
        confirmation = 0;
        $('#per_confirmation').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;

    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});



//update the per confirmation

$('#per_confirmation').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());
    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    per_confirmation = parseFloat($('#per_confirmation').val());
    possession = parseFloat($('#possession').val());

    $('#confirmation').val(percentage(net_amount, per_confirmation));

    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    num = $("#confirmation").val();
    $("#confirmation-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());


    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());


    if (isNaN(confirmation)) {
        confirmation = 0;
        $('#confirmation').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});



//update the allotment

$('#allotment').keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());

    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    possession = parseFloat($('#possession').val());
    allotment = parseFloat($('#allotment').val());
    $('#per_allotment').val(((allotment / net_amount) * 100).toFixed(2));
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    num = $("#allotment").val();
    $("#allotment-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }

    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });
    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(allotment)) {
        allotment = 0;
        $('#per_allotment').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;

    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});



//update the per allotment

$('#per_allotment').keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }


    net_amount = parseInt($('#net_amount').val());
    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    allotment_per = parseFloat($('#per_allotment').val());
    possession = parseFloat($('#possession').val());

    $('#allotment').val(percentage(net_amount, allotment_per));

    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;


    num = $("#allotment").val();
    $("#allotment-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(allotment)) {
        allotment = 0;
        $('#allotment').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});



$('#access_amount').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    access_amount = parseInt($(this).val())

    if (access_amount > 0) {
        $('#net_amount').val(net_amount + access_amount)
    } else {
        $('#net_amount').val($('#dummy_net_amount').val())
    }


    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    possession = parseInt($('#possession').val());
    $('#per_possession').val(((possession / net_amount) * 100).toFixed(2));
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;


    num = $("#possession").val();
    $("#possession-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());

    if (isNaN(booking)) {
        booking = 0;
        $('#per_booking').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;

    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});



//update the possession

$('#possession').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());

    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;
    // possession = parseInt($('#possession').val());
    if (isNaN(this.value) || this.value == "" || this.value == ' ') {
        $('#per_possession').val(0);
        possession = parseInt(0);
    } else {
        $('#per_possession').val(((possession / net_amount) * 100).toFixed(2));
        possession = parseInt($('#possession').val());
    }

    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;


    num = $("#possession").val();
    $("#possession-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());

    if (isNaN(possession)) {
        possession = 0;
        $('#per_possession').val(0);

    }
    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;

    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});




// function percentage(num, per)
// {
//   return (num/100)*per;
// }

//update the per possession

$('#per_possession').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());

    if (isNaN(this.value) || this.value == '' || this.value == " ") {
        possession_per = parseFloat(0);
        $('#possession').val(0);
    } else {
        possession_per = parseFloat($('#per_possession').val());
        $('#possession').val(percentage(net_amount, possession_per));
    }



    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;


    possession = parseFloat($('#possession').val());

    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = 0;

    num = $("#possession").val();
    $("#possession-label").text(number2TextConversion(num));

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });




    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(possession)) {
        possession = 0;
        $('#possession').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});


$('#down-payment').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());
    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;

    monthly_installment = parseInt($('#total_installment').val());

    $('#per_down_payment').val(((down_payment / net_amount) * 100).toFixed(2));

    possession = parseInt($('#possession').val());

    num = $("#down-payment").val();
    $("#down-payment-label").text(number2TextConversion(num));


    annual_installment = 0;

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(down_payment)) {
        down_payment = 0;
        $('#per_down_payment').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});


$('#per_down_payment').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());

    // possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());

    per_down_payment = parseFloat($('#per_down_payment').val());


    $('#down-payment').val(percentage(net_amount, per_down_payment));

    possession = parseFloat($('#possession').val());

    if ($('#down-payment').val())
        down_payment = parseInt($('#down-payment').val());
    else
        down_payment = 0;

    num = $("#down-payment").val();
    $("#down-payment-label").text(number2TextConversion(num));

    annual_installment = 0;

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(down_payment)) {
        down_payment = 0;
        $('#down-payment').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});

// update per ballon
$('#per_file_ballon').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());

    // possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());

    per_file_ballon = parseFloat($('#per_file_ballon').val());

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#ballon').val(0);
    } else {
        $('#ballon').val(percentage(net_amount, per_file_ballon));

    }


    possession = parseFloat($('#possession').val());

    if ($('#ballon').val())
        ballon = parseInt($('#ballon').val());
    else
        ballon = 0;


    num = $("#ballon").val();
    $("#ballon-label").text(number2TextConversion(num));

    annual_installment = 0;

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });

    annual_installment = parseFloat(annual_installment);
    bullating = parseFloat($('#bulleting').val());
    down_payment = parseFloat($('#down-payment').val());
    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());
    development = parseFloat($('#development').val());
    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(ballon)) {
        ballon = 0;
        $('#ballon').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});


//update ballon
$('#ballon').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());
    if ($('#ballon').val())
        ballon = parseInt($('#ballon').val());
    else
        ballon = 0;

    monthly_installment = parseInt($('#total_installment').val());

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#per_file_ballon').val(0);
    } else {
        $('#per_file_ballon').val(((ballon / net_amount) * 100).toFixed(2));
    }
    possession = parseInt($('#possession').val());

    num = $("#ballon").val();
    $("#ballon-label").text(number2TextConversion(num));


    annual_installment = 0;

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });

    down_payment = parseFloat($('#down-payment').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());
    development = parseFloat($('#development').val());
    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(ballon)) {
        ballon = 0;
        $('#per_file_ballon').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});

// update per bullating
$('#per_bulleting').keyup(function() {


    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());

    // possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());

    per_bulleting = parseFloat($('#per_bulleting').val());

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#bulleting').val(0);
    } else {
        $('#bulleting').val(percentage(net_amount, per_bulleting));

    }


    possession = parseFloat($('#possession').val());

    if ($('#bulleting').val())
        bullating = parseInt($('#bulleting').val());
    else
        bullating = 0;


    num = $("#bulleting").val();
    $("#bullating-label").text(number2TextConversion(num));

    annual_installment = 0;

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }


    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });
    annual_installment = parseFloat(annual_installment);
    ballon = parseFloat($('#ballon').val());
    down_payment = parseFloat($('#down-payment').val());
    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());
    development = parseFloat($('#development').val());
    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(bullating)) {
        bullating = 0;
        $('#bulleting').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});

//update bullating
$('#bulleting').keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }
    net_amount = parseInt($('#net_amount').val());
    if ($('#bulleting').val())
        bullating = parseInt($('#bulleting').val());
    else
        bullating = 0;

    monthly_installment = parseInt($('#total_installment').val());

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#per_bulleting').val(0);
    } else {
        $('#per_bulleting').val(((bullating / net_amount) * 100).toFixed(2));
    }
    possession = parseInt($('#possession').val());

    num = $("#bulleting").val();
    $("#bullating-label").text(number2TextConversion(num));


    annual_installment = 0;

    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_custom }}) {
        // monthly_installment = $('#custome_rem').val();
        monthly_installment = 0;
    }
    var add_amount = 0;

    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    var cus_amount = 0;
    $("input[name='custom_amount[]']").each(function() {
        if (!this.value) {
            cus_amount += 0;
        } else {
            cus_amount += parseInt($(this).val());
        }
    });


    down_payment = parseFloat($('#down-payment').val());
    ballon = parseFloat($('#ballon').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());
    development = parseFloat($('#development').val());
    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(bullating)) {
        bullating = 0;
        $('#per_bulleting').val(0);

    }

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating - cus_amount;


    $('#remaining_amount').val(remaining_amount);
    var radioValue = $("input[name='schedule_type']:checked").val();

    if (radioValue == '{{ \App\CustomerProperty::inst_custom }}') {
        remaining_amount = remaining_amount + parseInt($('#custome_rem').val())
    }
    $('#custome_rem').val(Math.round(remaining_amount));
});




//update the six installment fee label
$("#annual-installment").keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }
    num = $("#annual-installment").val();
    $("#annual-installment-label").text(number2TextConversion(num));
    annual_ins = $("#annual-installment").val();

    count = $('#total_annual_installment_count').val();

    $('#total_annual_installment').val(Math.round(parseInt($("#annual-installment").val()) * count));

    num = $("#total_annual_installment").val();
    $("#total-annual-installment-label").text(number2TextConversion(num));




    net_amount = parseInt($('#net_amount').val());


    $('#per_total_annual_installment').val(((num / net_amount) * 100).toFixed(2));

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#per_annual-installment').val(0);
    } else {
        $('#per_annual-installment').val(((this.value / net_amount) * 100).toFixed(2));
    }

    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = parseInt($('#total_annual_installment').val());

    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value)
            add_amount += 0;
        else
            add_amount += parseInt($(this).val());
    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(annual_installment)) {
        annual_installment = 0
    }
    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;

    $('#remaining_amount').val(Math.round(remaining_amount));
});

// update total annaual installment
$('#total_annual_installment').keyup(function() {

    price = $('#total_annual_installment').val();
    count = $('#total_annual_installment_count').val();

    $('#annual-installment').val(Math.round(price / count));
    if (isNaN(this.value)) {
        this.value = 0;
    }
    num1 = $("#annual-installment").val();
    $("#annual-installment-label").text(number2TextConversion(num1));
    annual_ins = $("#annual-installment").val();

    count = $('#total_annual_installment_count').val();


    num = $("#total_annual_installment").val();
    $("#total-annual-installment-label").text(number2TextConversion(num));




    net_amount = parseInt($('#net_amount').val());


    $('#per_total_annual_installment').val(((num / net_amount) * 100).toFixed(2));

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#per_annual-installment').val(0);
    } else {
        $('#per_annual-installment').val(((num1 / net_amount) * 100).toFixed(2));
    }


    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = parseInt($('#total_annual_installment').val());

    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value)
            add_amount += 0;
        else
            add_amount += parseInt($(this).val());
    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(annual_installment)) {
        annual_installment = 0
    }
    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;

    $('#remaining_amount').val(Math.round(remaining_amount));

    // $('#remaining_amount').val(0);

});
// update per annual
$("#per_annual-installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());
    per_annual_installment = parseFloat($('#per_annual-installment').val());

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#annual-installment').val(0);
    } else {
        $('#annual-installment').val(percentage(net_amount, per_annual_installment));

    }
    num = $("#annual-installment").val();
    $("#annual-installment-label").text(number2TextConversion(num));
    annual_ins = $("#annual-installment").val();

    count = $('#total_annual_installment_count').val();

    $('#total_annual_installment').val(Math.round(parseInt($("#annual-installment").val()) * count));

    num = $("#total_annual_installment").val();
    $("#total-annual-installment-label").text(number2TextConversion(num));
    $('#per_total_annual_installment').val(((num / net_amount) * 100).toFixed(2));
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = parseInt($('#total_annual_installment').val());

    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value)
            add_amount += 0;
        else
            add_amount += parseInt($(this).val());
    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(annual_installment)) {
        annual_installment = 0
    }
    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;

    $('#remaining_amount').val(Math.round(remaining_amount));

});


//update per total anual installemnt
$("#per_total_annual_installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }

    net_amount = parseInt($('#net_amount').val());
    per_total_annual_installment = parseFloat($('#per_total_annual_installment').val());

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#total_annual_installment').val(0);
    } else {
        $('#total_annual_installment').val(percentage(net_amount, per_total_annual_installment));
        $('#annual-installment').val(Math.round(parseInt($("#total_annual_installment").val() /
            parseInt($(
                '#total_annual_installment_count').val()))));


    }
    num = $("#annual-installment").val();
    $("#annual-installment-label").text(number2TextConversion(num));
    annual_ins = $("#annual-installment").val();



    num = $("#total_annual_installment").val();
    $("#total_installment").text(number2TextConversion(num));

















    // count = $('#total_annual_installment_count').val();

    // $('#total_annual_installment').val(Math.round(parseInt($("#annual-installment").val()) * count));

    num = $("#total_annual_installment").val();
    $("#total-annual-installment-label").text(number2TextConversion(num));

    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = parseInt($('#total_annual_installment').val());

    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value)
            add_amount += 0;
        else
            add_amount += parseInt($(this).val());
    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());
    if (isNaN(annual_installment)) {
        annual_installment = 0
    }
    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;

    $('#remaining_amount').val(Math.round(remaining_amount));

});





$('#total_annual_installment_count').keyup(function() {

    price = $('#net_amount').val();
    count = $('#total_annual_installment_count').val();

    $('#annual-installment').val(Math.round($('#total_annual_installment').val() / count));

    //$('#annual-installment').val( Math.round(parseFloat($('#annual_installment_per').val()) / 100 * price / count));

});



//update the semi installment fee label
$("#semi-installment").keyup(function() {

    if (isNaN(this.value)) {
        this.value = 0;
    }
    num = $("#semi-installment").val();
    $("#semi-installment-label").text(number2TextConversion(num));

    count = $('#total_semi_installment_count').val();
    $('#total_semi_installment').val(Math.round(parseInt($("#semi-installment").val()) * count));

    num = $("#total_semi_installment").val();
    $("#total-semi-installment-label").text(number2TextConversion(num));


    net_amount = parseInt($('#net_amount').val());


    $('#per_total_semi_installment').val(((num / net_amount) * 100).toFixed(2));

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#per_semi_installment').val(0);
    } else {
        $('#per_semi_installment').val(((this.value / net_amount) * 100).toFixed(2));
    }


    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = parseInt($('#total_semi_installment').val());

    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value)
            add_amount += 0;
        else
            add_amount += parseInt($(this).val());

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());


    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;


    $('#remaining_amount').val(Math.round(remaining_amount));

});

//update the quarterly installment fee label
$("#bi-mon-installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    num = $("#bi-mon-installment").val();
    $("#bi-mon-installment-label").text(number2TextConversion(num));

    count = $('#total_bi_mon_installment_count').val();
    $('#total_bi_mon_installment').val(Math.round(parseInt($("#bi-mon-installment").val()) * count));

    num = $("#total_bi_mon_installment").val();
    $("#total-bi-mon-installment-label").text(number2TextConversion(num));

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = parseInt($('#total_bi_mon_installment').val());

    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());


    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;

    $('#remaining_amount').val(Math.round(remaining_amount));

});





//update the quarterly installment fee label
$("#quarterly-installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    num = $("#quarterly-installment").val();
    $("#quarterly-installment-label").text(number2TextConversion(num));

    count = $('#total_quarterly_installment_count').val();
    $('#total_quarterly_installment').val(Math.round(parseInt($("#quarterly-installment").val()) *
        count));

    num = $("#total_quarterly_installment").val();
    $("#total-quarterly-installment-label").text(number2TextConversion(num));

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    annual_installment = parseInt($('#total_quarterly_installment').val());

    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());


    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;

    $('#remaining_amount').val(Math.round(remaining_amount));

});



//update the quarterly installment fee label
$("#total_quarterly_installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {

        $('#quarterly-installment').val(0);

        $('#remaining_amount').val(Math.round(0));
    } else {
        per_inst = parseInt($('#total_quarterly_installment').val()) / parseInt($(
            '#total_quarterly_installment_count').val());

        $('#quarterly-installment').val(Math.round(per_inst));

        $('#remaining_amount').val(Math.round(0));
    }

});

//update the installment label
$("#installment").keyup(function() {
    num1 = $("#installment").val();

    $("#installment-label").text(number2TextConversion(num1));

    $('#total_installment').val(Math.round(parseInt($("#installment").val()) * parseInt($(
            '#total-months')
        .val())));

    num = $("#total_installment").val();
    $("#total-installment-label").text(number2TextConversion(num));

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());


    $('#per_installment').val(((num1 / net_amount) * 100).toFixed(2));

    $('#per_total_installment').val(Math.round(parseFloat($("#per_installment").val()) * parseFloat($(
        '#total-months').val())))

    annual_installment = 0;
    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    }
    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }
    });

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());


    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;


    $('#remaining_amount').val(Math.round(remaining_amount));

});


$("#per_installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    num1 = $("#installment").val();
    $("#installment-label").text(number2TextConversion(num1));

    $('#total_installment').val(Math.round(parseInt($("#installment").val()) * parseInt($(
            '#total-months')
        .val())));

    num = $("#total_installment").val();
    $("#total-installment-label").text(number2TextConversion(num));

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());


    $('#installment').val(percentage(net_amount, $('#per_installment').val()));
    $('#per_total_installment').val(Math.round(parseFloat($("#per_installment").val()) * parseFloat($(
        '#total-months').val())))

    annual_installment = 0;
    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    }
    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }
    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;


    $('#remaining_amount').val(Math.round(remaining_amount));

});

//update the total months label
$("#total-months").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#installment').val(0);



        $('#per_installment').val(0);
    } else {


        $('#installment').val(Math.round(parseInt($("#total_installment").val() / parseInt($(
            '#total-months').val()))));

        num = $("#installment").val();
        $("#installment-label").text(number2TextConversion(num));

        $('#per_installment').val(((num / net_amount) * 100).toFixed(2));
    }

});
//update the total months label
// $("#total-months").keyup(function(){

//         num = $("#installment").val();
//         $("#installment-label").text(number2TextConversion(num));

//         $('#total_installment').val(Math.round(parseInt($("#installment").val()) * parseInt($('#total-months').val())));

//         num = $("#total_installment").val();
//         $("#total-installment-label").text(number2TextConversion(num));

// });


// update total installments 
$("#total_installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    num1 = $("#total_installment").val();
    $("#total-installment-label").text(number2TextConversion(num1));



    net_amount = parseInt($('#net_amount').val());


    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#per_total_installment').val(0);
        $('#installment').val(0);
    } else {
        $('#per_total_installment').val(((this.value / net_amount) * 100).toFixed(2));
        $('#installment').val(Math.round(parseInt($("#total_installment").val() / parseInt($(
            '#total-months').val()))));

        num = $("#installment").val();
        $("#installment-label").text(number2TextConversion(num));
    }


    // $('#installment').val(percentage(net_amount ,  $('#per_installment').val() ));





    annual_installment = 0;
    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    }
    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }
    });

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;


    $('#remaining_amount').val(Math.round(remaining_amount));

});


// update per_total_installment

$("#per_total_installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }


    net_amount = parseInt($('#net_amount').val());



    per_total = parseFloat($('#per_total_installment').val());

    if (isNaN(this.value) || this.value == '' || this.value == ' ' || this.value == 0) {
        $('#total_installment').val(0);
    } else {
        $('#total_installment').val(percentage(net_amount, per_total));
        $('#installment').val(Math.round(parseInt($("#total_installment").val() / parseInt($(
            '#total-months').val()))));

    }



    num = $("#total_installment").val();
    $("#total_installment").text(number2TextConversion(num));








    num1 = $("#installment").val();
    $("#installment-label").text(number2TextConversion(num1));


    num = $("#total_installment").val();
    $("#total-installment-label").text(number2TextConversion(num));














    annual_installment = 0;
    val = parseInt($('input[name="schedule_type"]:checked').val());
    if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
        annual_installment = $('#total_annual_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {
        annual_installment = $('#total_quarterly_installment').val();
    } else if (val == {{ \App\CustomerProperty::inst_quar_w_semi }}) {
        annual_installment = $('#total_semi_installment').val();
    }
    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value) {
            add_amount += 0;
        } else {
            add_amount += parseInt($(this).val());
        }
    });

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());

    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;


    $('#remaining_amount').val(Math.round(remaining_amount));

});









//update the total months label
$("#total_semi_installment").keyup(function() {
    if (isNaN(this.value)) {
        this.value = 0;
    }
    price = $("#total_semi_installment").val();
    count = $("#total_semi_installment_count").val();


    $('#semi-installment').val((Math.round(price / count)));


    num = $("#semi-installment").val();
    $("#semi-installment-label").text(number2TextConversion(num));

    // count = $('#total_semi_installment_count').val();
    // $('#total_semi_installment').val(Math.round(parseInt($("#semi-installment").val()) * count));

    num = $("#total_semi_installment").val();
    $("#total-semi-installment-label").text(number2TextConversion(num));


    net_amount = parseInt($('#net_amount').val());


    $('#per_total_semi_installment').val(((num / net_amount) * 100).toFixed(2));




    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    if (isNaN(this.value) || this.value == '' || this.value == ' ') {

        annual_installment = 0;
    } else {

        annual_installment = parseInt($('#total_semi_installment').val());
    }

    monthly_installment = parseInt($('#total_installment').val());
    var add_amount = 0;
    $("input[name='add_amount[]']").each(function() {
        if (!this.value)
            add_amount += 0;
        else
            add_amount += parseInt($(this).val());

    });
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());


    remaining_amount = net_amount - down_payment - possession - monthly_installment -
        annual_installment -
        add_amount - allotment - confirmation - booking - file_processing - registration - ballon -
        bullating;


    $('#remaining_amount').val(Math.round(remaining_amount));














    // $("#remaining_amount").val(0);

});



function updateCurrency() {


    //update the cost label

    num = $("#total-amount").val();

    $("#total-amount-label").text(number2TextConversion(num));

    //update the net amount
    num = $("#net_amount").val();

    $("#net_amount-label").text(number2TextConversion(num));


    //update the down payemnt label
    num = $("#down-payment").val();

    $("#down-payment-label").text(number2TextConversion(num));


    //update the annual installment text
    num = $("#annual-installment").val();


    $("#annual-installment-label").text(number2TextConversion(num));


    //update the total annual installment text
    num = $("#total_annual_installment").val();

    $("#total-annual-installment-label").text(number2TextConversion(num));

    //update the semi installment text
    num = $("#semi-installment").val();

    $("#semi-installment-label").text(number2TextConversion(num));


    // //update the total semi installment
    num = $("#total_semi_installment").val();

    $("#total-semi-installment-label").text(number2TextConversion(num));

    //     //upte the quarterly installment text
    num = $("#bi-mon-installment").val();

    $("#bi-mon-installment-label").text(number2TextConversion(num));

    // //upte the quarterly installment text
    num = $("#quarterly-installment").val();

    $("#quarterly-installment-label").text(number2TextConversion(num));

    //     //update the total quarterly installment
    num = $("#total_bi_mon_installment").val();

    $("#total-bi-mon-installment-label").text(number2TextConversion(num));


    // //update the total quarterly installment
    num = $("#total_quarterly_installment").val();

    $("#total-quarterly-installment-label").text(number2TextConversion(num));

    // //update the installment label

    num = $("#installment").val();

    $("#installment-label").text(number2TextConversion(num));

    // //update the total_installment
    num = $("#total_installment").val();

    $("#total-installment-label").text(number2TextConversion(num));


    //     //update the possession
    num = $("#possession").val();

    $("#possession-label").text(number2TextConversion(num));
    //      //update the Allotment 
    num = $("#allotment").val();

    $("#allotment-label").text(number2TextConversion(num));
    //        //update the Confirmation 
    num = $("#confirmation").val();

    $("#confirmation-label").text(number2TextConversion(num));

    //      //update the Booking  
    num = $("#booking").val();

    $("#booking-label").text(number2TextConversion(num));
    //update the Booking  

    // //update the file_processing  
    num = $("#file_processing").val();

    $("#file_processing-label").text(number2TextConversion(num));
    // //update the registration  
    // num=$("#registration").val();

    $("#registration-label").text(number2TextConversion(num));
    // //update the registration  
    num = $("#ballon").val();

    $("#ballon-label").text(number2TextConversion(num));

    // //update the bulleting  
    num = $("#bulleting").val();

    $("#bullating-label").text(number2TextConversion(num));


}


//update the unit number dropdown when category change
// $('#customer').change(function() {
//     $('.preloader').fadeIn();
//         var data = { 'customer': $(this).val() };

//         $.get('/getUnitsByCustomer', data, function (data) {
//            var model = $('#unit_number');
//                     model.empty();

//                      model.append("<option value='' disabled selected>" + '' + "</option>");


//                     $.each(data, function(index, element) {
//                         model.append("<option value='"+ element.inventory.id +"'>" + element.inventory.unit_number + "</option>");
//                     });
//         });
//          $('.preloader').fadeOut();
//     });

//update the unit information when unit number change
function getInformationByUnit(data) {
    $.get('/getInformationByUnit', data, function(data) {

        if (data) {
            let inventId = $('#invent_id');
            if (data.info.inventory_type == 1) {
                $('#category_col').show();
                $("#allocated_category").hide();
                inventId.val('Unallocated Unit');




            } else {
                $('#category_col').hide();
                $("#allocated_category").show();
                inventId.val('Allocated Unit');


            }



            $("#schedule_plan_id").val(data.schedule.id).trigger("chosen:updated");

            $('#total-amount').val(data.info.total_price - data.info.extra_amount);
            $('#net_amount').val(data.info.total_price);
            $('#dummy_net_amount').val(data.info.total_price)
            $('#extra_charges').val(data.info.extra_amount);
            $('#size').val((data.info.size) ? data.info.size.name : '' + ' {{ env('SIZE') }}');
            $('#inventory-id').val(data.info.id);
            $('#description').val(data.info.description);
            name = '';
            for (i = 0; i < data.info.categories.length; i++) {
                name += data.info.categories[i].category.name + ', ';
            }
            $('#category').val(name);
            //$('#category').val(data.info.category.name);
            $("#inventory_type #type").val(data.info.type.name);
            $('#down_payment_per').val(data.schedule.down_payment);
            $('#possession_per').val(data.schedule.possession);
            $('#per_allotment').val(data.schedule.allotment);
            $('#per_confirmation').val(data.schedule.confirmation);


            $('#per_booking').val(data.schedule.booking);
            $('#per_development').val(data.schedule.development);


            $('#per_file_processing').val(data.schedule.file_processing);
            $('#per_registration').val(data.schedule.registration);

            $('#per_file_ballon').val(data.schedule.ballon);
            $('#per_bulleting').val(data.schedule.bulleting);

            $('#semi_installment_hid_per').val(data.schedule.semi_installment);




            $('#annual_installment_per').val(data.schedule.annual_installment);
            $('#monthly_installment_per').val(data.schedule.monthly_installment);
            $('#total_month_per').val(data.schedule.total_months);
            $('#total_annual_installment_count').val(Math.round(data.schedule.total_months / 12));
            $('#total_semi_installment_count').val(Math.round(data.schedule.total_months / 6));
            $('#total_quarterly_installment_count').val(Math.round(data.schedule.total_months / 3));
            $('#total_bi_mon_installment_count').val(Math.round(data.schedule.total_months / 3));


            $('#ballon').val(Math.round((parseInt(data.schedule.ballon) / 100 * data.info
                .total_price)));

            $('#bulleting').val(Math.round((parseInt(data.schedule.bulleting) / 100 * data.info
                .total_price)));


            $('#semi_installment_hid_amount').val((parseInt(data.schedule.semi_installment) / 100 *
                data.info.total_price));
            $('#down-payment').val(Math.round((parseInt(data.schedule.down_payment) / 100 * data
                .info.total_price)));
            $('#down_payment_dis').val(Math.round((parseInt(data.schedule.down_payment) / 100 * data
                .info.total_price)));
            $('#possession').val(Math.round((parseInt(data.schedule.possession) / 100 * data.info
                .total_price)));
            $('#possession1').val(Math.round((parseInt(data.schedule.possession) / 100 * data.info
                .total_price)));

            $('#allotment').val(Math.round((parseInt(data.schedule.allotment) / 100 * data.info
                .total_price)));

            $('#confirmation').val(Math.round((parseInt(data.schedule.confirmation) / 100 * data
                .info.total_price)));



            $('#booking').val(Math.round((parseInt(data.schedule.booking) / 100 * data.info
                .total_price)));

            $('#development').val(Math.round((parseInt(data.schedule.development) / 100 * data.info
                .total_price)));



            $('#file_processing').val(Math.round((parseInt(data.schedule.file_processing) / 100 *
                data.info.total_price)));

            $('#registration').val(Math.round((parseInt(data.schedule.registration) / 100 * data
                .info.total_price)));


            $('#annual-installment').val(Math.round(data.schedule.annual_installment / 100 * data
                .info.total_price / (data.schedule.total_months / 12)));
            $('#total-months').val(Math.round(data.schedule.total_months));

            $('#total_annual_installment').val(Math.round(data.schedule.annual_installment / 100 *
                data.info.total_price));

            $('#installment').val(Math.round(data.schedule.monthly_installment / 100 * data.info
                .total_price / (data.schedule.total_months)));


            $('#total_installment').val(Math.round(data.schedule.monthly_installment / 100 * data
                .info.total_price));

            manToPer();
            //  updateCurrency();

            //Full Payment
            $('#total_amount_full_payment').val(data.info.total_price);

            net_amount = parseInt($('#net_amount').val());
            down_payment = parseInt($('#down-payment').val());
            possession = parseInt($('#possession').val());
            monthly_installment = parseInt($('#total_installment').val());
            annual_installment = parseInt($('#total_annual_installment').val());


            ballon = parseInt($('#ballon').val());
            bullating = parseInt($('#bulleting').val());
            allotment = parseInt($('#allotment').val());
            confirmation = parseInt($('#confirmation').val());
            booking = parseInt($('#booking').val());

            file_processing = parseInt($('#file_processing').val());
            registration = parseInt($('#registration').val());


            remaining_amount = net_amount - down_payment - possession - monthly_installment -
                annual_installment - allotment - confirmation - booking - file_processing -
                registration - ballon - bullating;

            $('#remaining_amount').val(Math.round(remaining_amount));

            //set the rebate 
            $('#rebate_in_percentage').val(Math.round(5));

            $('#rebate_in_amount').val(Math.round((data.info.total_price) * 0.05));

            $('#sqrt').html((data.info.size) ? data.info.size.name : '');
            vl = (data.info.size) ? data.info.size.name : '';
            $('#per_sqrt_value').val(net_amount / vl);




            let data_id = $('#unit_number').data('id');

            if (data_id == 1) {
                $('#total_rebate1').val(data.dealer_rebate);


                $('#dealer_id').val(data.dealer_id);
                $("#total_rebate1").attr('readonly', 'readonly');
                //   $('#total_rebate1').attr("disabled","disabled") 
                // $('#dealer_id').attr("disabled","disabled") 
                $('#dealer_id option:not(:selected)').prop('disabled', true);
            }
        }
    });
}
$('#addUnitDetail').on('change', '#unit_number', function() {

    $('.preloader').fadeIn();
    let data_id = $('#unit_number').data('id');
    var data = {
        'unit_number': $('#unit_number').val(),
        'data_id': data_id
    };
    getInformationByUnit(data);
    $('.preloader').fadeOut();
});


function changeShedulePlan() {

    $('.preloader').fadeIn();


    var value = $('#unit_number').val();

    if (value.trim().length === 0) {
        Swal.fire('Please Select Unit First');

        $('#schedule_plan_id').val('').trigger('chosen:updated');
        $('.preloader').fadeOut();
        return false;
    }
    schedule_plan = $('#schedule_plan_id').val();
    if (schedule_plan.trim().length === 0) {


        $('.preloader').fadeOut();
        return false;
    }

    let data_id = $('#unit_number').data('id');

    var data = {
        'unit_number': $('#unit_number').val(),
        'data_id': data_id,
        'schedule_plan_id': $('#schedule_plan_id').val()
    };
    getInformationByUnit(data);
    $('.preloader').fadeOut();
}



$('#rebate_in_amount').keyup(function() {

    amount = $('#rebate_in_amount').val();
    percentage = $('#rebate_in_percentage').val();
    total_value = $('#net_amount').val();

    $('#rebate_in_percentage').val(Math.round((amount / total_value) * 100));

});

$('#rebate_in_percentage').keyup(function() {

    amount = $('#rebate_in_amount').val();
    percentage = $('#rebate_in_percentage').val();
    total_value = $('#net_amount').val();

    $('#rebate_in_amount').val(Math.round((total_value) * (percentage / 100)));

});

$(document).on('keyup', '.total_rebate', function() {



    var id = document.getElementsByClassName('total_rebate');
    var rebate = $('#rebate_in_amount').val();

    given_rebate = 0;

    for (i = 0; i < id.length; i++) {

        given_rebate += parseFloat(id[i].value);
    }

    $('#remaining_amount_in_rebate').val(rebate - given_rebate);

});


function manToPer() {
    cost = $('#net_amount').val();
    if (cost) {

        down_payment = $('#down-payment').val();
        if (down_payment) {
            $('#per_down_payment').val(((down_payment / cost) * 100).toFixed(2));

        }
        possession = $('#possession').val();
        if (possession) {
            $('#per_possession').val(((possession / cost) * 100).toFixed(2));

        }

        // annual_installment = $('#annual-installment').val();
        // if(annual_installment) {
        //     $('#annual-installment').val( ((annual_installment / cost) * 100).toFixed(2));

        // }


        // total_annual_installment = $('#total_annual_installment').val();
        // if(total_annual_installment) {
        //     $('#total_annual_installment').val( ((total_annual_installment / cost) * 100).toFixed(2));

        // }

        total_months = $('#total-months').val();
        if (total_months) {
            installment = $('#installment').val();
            if (installment) {
                $('#per_installment').val((((installment) / cost) * 100).toFixed(2));

            }
            total_installment = $('#total_installment').val();
            if (total_installment) {
                $('#per_total_installment').val((((total_installment) / cost) * 100).toFixed(2));

            }
        }
        updateCurrency();

    }
}

function perToMan() {
    cost = $('#net_amount').val();
    if (cost) {

        down_payment = $('#down-payment').val();
        if (down_payment) {
            $('#down-payment').val(((down_payment * cost) / 100).toFixed(0));

        }
        possession = $('#possession').val();
        if (possession) {
            $('#possession').val(((possession * cost) / 100).toFixed(0));

        }


        annual_installment = $('#annual-installment').val();
        if (annual_installment) {
            $('#annual-installment').val(((annual_installment * cost) / 100).toFixed(0));

        }
        total_annual_installment = $('#total_annual_installment').val();
        if (total_annual_installment) {
            $('#total_annual_installment').val(((total_annual_installment * cost) / 100).toFixed(0));

        }
        total_months = $('#total-months').val();
        if (total_months) {
            installment = $('#installment').val();
            if (installment) {
                $('#installment').val(((installment * cost) / 100).toFixed(0));

                total_installment = $('#total_installment').val();
                if (total_installment) {
                    $('#total_installment').val(((total_installment * cost) / 100).toFixed(0));

                }
            }
        }
        updateCurrency();

    }
}


$('#discount_s').keyup(function() {

    if ($('#discount_s').val()) {

        discount = 0;
        if ($('#discount_percent').prop("checked") == false) {
            price = Math.round(parseInt($('#total-amount').val()) + parseInt($('#extra_charges')
            .val()) -
                parseFloat($('#discount_s').val()));
            discount = parseFloat($('#discount_s').val());
        } else {
            price = Math.round(parseInt($('#total-amount').val()) + parseInt($('#extra_charges')
            .val()) -
                parseFloat($('#discount_s').val()) / 100 * (parseInt($('#total-amount').val()) +
                    parseInt($('#extra_charges').val())));
            discount = parseFloat($('#discount_s').val()) / 100 * (parseInt($('#total-amount').val()) +
                parseInt($('#extra_charges').val()));
        }


        $('#net_amount').val(price);
        var total_value = price / $('#sqrt').text()

        $('#per_sqrt_value').val(total_value.toFixed(1));

        $('#down-payment').val(Math.round(($('#down_payment_per').val() / 100) * price));
        $('#possession1').val(Math.round(($('#possession_per').val() / 100) * price));
        $('#possession').val(Math.round(($('#possession_per').val() / 100) * price));

        disableAnn(1);

        //  $('#down-payment').val( Math.round($('#down_payment_dis').val() -  discount));

        updateCurrency();
    } else {
        price = Math.round(parseInt($('#total-amount').val()) + parseInt($('#extra_charges').val()));

        $('#net_amount').val(price);

        //  $('#down-payment').val( Math.round($('#down_payment_dis').val()));



        $('#down-payment').val(Math.round(($('down_payment_per').val() / 100) * price));
        $('#possession1').val(Math.round(($('#possession_per').val() / 100) * price));
        $('#possession').val(Math.round(($('#possession_per').val() / 100) * price));

        disableAnn(1);

        updateCurrency();

    }




    amount = $('#rebate_in_amount').val();
    percentage = $('#rebate_in_percentage').val();
    total_value = $('#net_amount').val();

    $('#rebate_in_percentage').val(Math.round((amount / total_value) * 100));

    $('#rebate_in_amount').val(Math.round((total_value) * (percentage / 100)));


});

function percentCheckbox() {

    if ($('#discount_percent').prop("checked") == false) {
        document.getElementById('dis_input').innerHTML = 'PKR';
    } else {
        document.getElementById('dis_input').innerHTML = '%';
    }

    if ($('#discount_s').val()) {
        discount = 0;
        if ($('#discount_percent').prop("checked") == false) {
            price = Math.round(parseInt($('#total-amount').val()) + parseInt($('#extra_charges').val()) -
                parseFloat($('#discount_s').val()));
            discount = parseFloat($('#discount_s').val());
        } else {
            price = Math.round(parseInt($('#total-amount').val()) + parseInt($('#extra_charges').val()) -
                parseFloat($('#discount_s').val()) / 100 * (parseInt($('#total-amount').val()) + parseInt($(
                    '#extra_charges').val())));
            discount = parseFloat($('#discount_s').val()) / 100 * (parseInt($('#total-amount').val()) +
                parseInt($(
                    '#extra_charges').val()));
        }

        $('#net_amount').val(price);

        $('#down-payment').val(Math.round($('#down_payment_dis').val() - discount));
        updateCurrency();
    } else {
        price = Math.round(parseInt($('#total-amount').val()) + parseInt($('#extra_charges').val()));

        $('#net_amount').val(price);

        $('#down-payment').val(Math.round($('#down_payment_dis').val()));

        updateCurrency();

    }

    $('#discount_s').keyup()

    updateCurrency();


}


function enableAnn() {
    makeInstallmentZero();
    document.getElementById('custom').style.display = 'none';
    document.getElementById('auto').style.display = 'block';

    $('#remaining_amount').val(0);
    document.getElementById('ann_div').style.display = 'block';
    document.getElementById('semi_div').style.display = 'none';
    document.getElementById('quar_div').style.display = 'none';
    document.getElementById('bi_mon_div').style.display = 'none';

    price = parseInt($('#net_amount').val());
    count = $('#total_annual_installment_count').val();

    $('#installment').val(Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price / (parseInt(
        $(
            '#total_month_per').val()))));

    $('#total_installment').val(Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price));
    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());
    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());

    annual_installment = parseInt($('#total_annual_installment').val());

    allotment = parseInt($('#allotment').val());
    confirmation = parseInt($('#confirmation').val());
    booking = parseInt($('#booking').val());

    file_processing = parseInt($('#file_processing').val());
    registration = parseInt($('#registration').val());


    remaining_amount = net_amount - down_payment - possession - monthly_installment - annual_installment -
        allotment - confirmation - booking - file_processing - registration - ballon - bullating;





    // $('#annual-installment').val( Math.round(remaining_amount / count)).trigger("keyup");
    // $('#total-months').val( Math.round(parseInt($('#total_month_per').val()))).trigger("keyup");;
    $('#total_annual_installment').val(Math.round(remaining_amount)).trigger("keyup");;


}


// function enableAnn() {

//     document.getElementById('custom').style.display = 'none';
//     document.getElementById('auto').style.display = 'block';

//     $('#remaining_amount').val(0);
//     document.getElementById('ann_div').style.display = 'block';
//     document.getElementById('semi_div').style.display = 'none';
//     document.getElementById('quar_div').style.display = 'none';
//     document.getElementById('bi_mon_div').style.display = 'none';

//     price = parseInt($('#net_amount').val());
//     count = $('#total_annual_installment_count').val();

//      $('#installment').val( Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price / (parseInt($('#total_month_per').val()))));

//     $('#total_installment').val( Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price));

//     ballon=  parseFloat($('#ballon').val());
//     bullating=   parseFloat($('#bulleting').val());
//     net_amount = parseInt($('#net_amount').val());
//     down_payment = parseInt($('#down-payment').val());
//     possession = parseInt($('#possession').val());
//     monthly_installment = parseInt($('#total_installment').val());

//     annual_installment = parseInt($('#total_annual_installment').val());

//     allotment = parseInt($('#allotment').val());
//     confirmation = parseInt($('#confirmation').val());
//     booking = parseInt($('#booking').val());

//     file_processing = parseInt($('#file_processing').val());
//     registration = parseInt($('#registration').val());


//     remaining_amount = net_amount - down_payment - possession - monthly_installment - annual_installment - allotment - confirmation - booking  - file_processing - registration - ballon - bullating;


//     $('#annual-installment').val( Math.round(remaining_amount / count));
//     $('#total-months').val( Math.round(parseInt($('#total_month_per').val())));

//     $('#total_annual_installment').val( Math.round(remaining_amount));

// }

function enableBiMonth() {
    makeInstallmentZero();
    document.getElementById('custom').style.display = 'none';
    document.getElementById('auto').style.display = 'block';

    $('#remaining_amount').val(0);
    document.getElementById('bi_mon_div').style.display = 'block';
    document.getElementById('quar_div').style.display = 'none';

    document.getElementById('semi_div').style.display = 'none';
    document.getElementById('ann_div').style.display = 'none';

    price = $('#net_amount').val();
    count = $('#total_bi_mon_installment_count').val();

    $('#installment').val(Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price / (parseInt(
        $(
            '#total_month_per').val()))));

    $('#total_installment').val(Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price));

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());
    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());

    annual_installment = parseInt($('#total_annual_installment').val());

    allotment = parseInt($('#allotment').val());
    confirmation = parseInt($('#confirmation').val());
    booking = parseInt($('#booking').val());

    file_processing = parseInt($('#file_processing').val());
    registration = parseInt($('#registration').val());


    remaining_amount = net_amount - down_payment - possession - monthly_installment - annual_installment -
        allotment - confirmation - booking - file_processing - registration - ballon - bullating;




    $('#bi-mon-installment').val(Math.round(remaining_amount / count));
    $('#total-months').val(Math.round(parseInt($('#total_month_per').val())));

    $('#total_bi_mon_installment').val(Math.round(remaining_amount));

}

function enableQuar() {

    makeInstallmentZero();
    document.getElementById('custom').style.display = 'none';
    document.getElementById('auto').style.display = 'block';

    $('#remaining_amount').val(0);
    document.getElementById('quar_div').style.display = 'block';
    document.getElementById('bi_mon_div').style.display = 'none';
    document.getElementById('semi_div').style.display = 'none';
    document.getElementById('ann_div').style.display = 'none';

    price = $('#net_amount').val();
    count = $('#total_quarterly_installment_count').val();

    $('#installment').val(Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price / (parseInt(
        $(
            '#total_month_per').val()))));

    $('#total_installment').val(Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price));

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    monthly_installment = parseInt($('#total_installment').val());

    annual_installment = parseInt($('#total_annual_installment').val());

    allotment = parseInt($('#allotment').val());
    confirmation = parseInt($('#confirmation').val());
    booking = parseInt($('#booking').val());

    file_processing = parseInt($('#file_processing').val());
    registration = parseInt($('#registration').val());


    remaining_amount = net_amount - down_payment - possession - monthly_installment - annual_installment -
        allotment - confirmation - booking - file_processing - registration - ballon - bullating;

    $('#quarterly-installment').val(Math.round(remaining_amount / count));
    $('#total-months').val(Math.round(parseInt($('#total_month_per').val())));

    $('#total_quarterly_installment').val(Math.round(remaining_amount));

}

function enableSemi() {
    makeInstallmentZero();
    document.getElementById('custom').style.display = 'none';
    document.getElementById('auto').style.display = 'block';


    document.getElementById('semi_div').style.display = 'block';
    document.getElementById('ann_div').style.display = 'none';
    document.getElementById('quar_div').style.display = 'none';
    document.getElementById('bi_mon_div').style.display = 'none';

    price = $('#net_amount').val();
    count = $('#total_semi_installment_count').val();

    $('#installment').val(Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price / (parseInt(
        $(
            '#total_month_per').val()))));

    $('#total_installment').val(Math.round(parseFloat($('#monthly_installment_per').val()) / 100 * price));

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());

    monthly_installment = parseInt($('#total_installment').val());

    annual_installment = parseInt($('#total_annual_installment').val());

    allotment = parseInt($('#allotment').val());
    confirmation = parseInt($('#confirmation').val());
    booking = parseInt($('#booking').val());

    file_processing = parseInt($('#file_processing').val());
    registration = parseInt($('#registration').val());

    semi_install_per = parseInt($('#semi_installment_hid_per').val());
    semi_install_amount = parseInt($('#semi_installment_hid_amount').val());
    $('#total_semi_installment').val(Math.round(semi_install_amount));
    $('#per_total_semi_installment').val(Math.round(semi_install_per));


    $('#semi-installment').val(Math.round(semi_install_per / 100 * net_amount / (count)));
    $('#per_semi_installment').val(((parseInt($('#semi-installment').val()) / net_amount) * 100).toFixed(2));

    remaining_amount = net_amount - down_payment - possession - monthly_installment - annual_installment -
        allotment - confirmation - booking - file_processing - registration - ballon - bullating -
        semi_install_amount;

    $('#remaining_amount').val(remaining_amount);



    //  $('#semi-installment').val( Math.round(remaining_amount / count));
    // $('#total-months').val( Math.round(parseInt($('#total_month_per').val())));

    // $('#total_semi_installment').val( Math.round(remaining_amount));

}

function disableAnn(val) {
    makeInstallmentZero();
    document.getElementById('custom').style.display = 'none';
    document.getElementById('auto').style.display = 'block';

    $('#remaining_amount').val(0);
    document.getElementById('semi_div').style.display = 'none';
    document.getElementById('ann_div').style.display = 'none';
    document.getElementById('quar_div').style.display = 'none';
    document.getElementById('bi_mon_div').style.display = 'none';
    price = $('#net_amount').val();

    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    allotment = parseFloat($('#allotment').val());
    confirmation = parseFloat($('#confirmation').val());
    booking = parseFloat($('#booking').val());

    file_processing = parseFloat($('#file_processing').val());
    registration = parseFloat($('#registration').val());

    remaining_amount = net_amount - down_payment - possession - allotment - confirmation - booking -
        file_processing - registration - ballon - bullating;



    if (val == {{ \App\CustomerProperty::inst_mon }}) {

        $('#total_installment').val(Math.round(remaining_amount));

        $('#installment').val(Math.round((remaining_amount) / parseInt($('#total_month_per').val())));

        $('#total-months').val(Math.round(parseInt($('#total_month_per').val())));
    } else if (val == {{ \App\CustomerProperty::inst_bi_mon }}) {

        $('#total_installment').val(Math.round(remaining_amount));

        $('#installment').val(Math.round((remaining_amount) / (parseInt($('#total_month_per').val()) / 2)));

        $('#total-months').val(Math.round(parseInt($('#total_month_per').val() / 2)));

    } else if (val == {{ \App\CustomerProperty::inst_quar }}) {


        $('#total_installment').val(Math.round(remaining_amount));

        $('#installment').val(Math.round((remaining_amount) / (parseInt($('#total_month_per').val()) / 3)));

        $('#total-months').val(Math.round(parseInt($('#total_month_per').val() / 3)));

    } else if (val == {{ \App\CustomerProperty::inst_semi }}) {



        $('#total_installment').val(Math.round(remaining_amount));

        $('#installment').val(Math.round((remaining_amount) / (parseInt($('#total_month_per').val()) / 6)));

        $('#total-months').val(Math.round(parseInt($('#total_month_per').val() / 6)));

    } else if (val == {{ \App\CustomerProperty::inst_ann }}) {


        $('#total_installment').val(Math.round(remaining_amount));

        $('#installment').val(Math.round((remaining_amount) /
            (parseInt($('#total_month_per').val() / 12))));

        $('#total-months').val(Math.round(parseInt($('#total_month_per').val() / 12)));

    }



}


function enableCustom(val) {
    makeInstallmentZero();
    document.getElementById('custom').style.display = 'block';
    document.getElementById('auto').style.display = 'none';



    ballon = parseFloat($('#ballon').val());
    bullating = parseFloat($('#bulleting').val());

    net_amount = parseInt($('#net_amount').val());
    down_payment = parseInt($('#down-payment').val());
    possession = parseInt($('#possession').val());
    allotment = parseInt($('#allotment').val());
    confirmation = parseInt($('#confirmation').val());
    booking = parseInt($('#booking').val());

    file_processing = parseInt($('#file_processing').val());
    registration = parseInt($('#registration').val());



    remaining_amount = net_amount - down_payment - possession - allotment - confirmation - booking -
        file_processing - registration - ballon - bullating;

    $('#custome_rem').val(Math.round(remaining_amount));


}

function makeInstallmentZero() {
    $('#annual-installment').val(0);
    $('#per_annual-installment').val(0);
    $('#total_annual_installment').val(0);
    $('#per_total_annual_installment').val(0);

    $('#total_semi_installment').val(0);
    $('#per_total_semi_installment').val(0);


    $('#semi-installment').val(0);
    $('#per_semi_installment').val(0);

    $('#quarterly-installment').val(0);
    $('#total_quarterly_installment').val(0);

}



$('#total_semi_installment_count').keyup(function() {

    price = $('#net_amount').val();
    count = $('#total_semi_installment_count').val();

    $('#semi-installment').val(Math.round($('#total_semi_installment').val() / count));

    //$('#semi-installment').val( Math.round(parseFloat($('#annual_installment_per').val()) / 100 * price / count));

});

$('#total_bi_mon_installment_count').keyup(function() {

    price = $('#net_amount').val();
    count = $('#total_bi_mon_installment_count').val();

    $('#bi-mon-installment').val(Math.round($('#total_bi_mon_installment').val() / count));

    //$('#quarterly-installment').val( Math.round(parseFloat($('#annual_installment_per').val()) / 100 * price / count));

});

$('#total_quarterly_installment_count').keyup(function() {

    price = $('#net_amount').val();
    count = $('#total_quarterly_installment_count').val();

    $('#quarterly-installment').val(Math.round($('#total_quarterly_installment').val() / count));

    //$('#quarterly-installment').val( Math.round(parseFloat($('#annual_installment_per').val()) / 100 * price / count));

});

function number2TextConversion(num) {

    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
    if (!n) return;
    var str = '';
    str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
    str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lac ' : '';
    str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
    str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
    str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) +
        'only ' : '';

    return str;
}


function adjustAmount(val) {

    amount = parseInt($('#remaining_amount').val());



    if (amount) {
        if (val == 1) { //amount adjustment to per installment
            $('#total_installment').val(parseInt($('#total_installment').val()) + amount);
            $('#installment').val(Math.round((parseInt($('#total_installment').val())) / parseInt($(
                    '#total-months')
                .val())));

            $('#per_installment').val(((parseInt($('#installment').val()) / net_amount) * 100).toFixed(2));
            total = $('#total_installment').val();
            net_amount = parseInt($('#net_amount').val());
            $('#per_total_installment').val(((total / net_amount) * 100).toFixed(2));
        } else if (val == 2) {
            val = parseInt($('input[name="schedule_type"]:checked').val());
            if (val == {{ \App\CustomerProperty::inst_mon_w_semi }}) {
                $('#total_semi_installment').val(parseInt($('#total_semi_installment').val()) + amount);
                $('#semi-installment').val(Math.round((parseInt($('#total_semi_installment').val())) / parseInt(
                    $(
                        '#total_semi_installment_count').val())));
            } else if (val == {{ \App\CustomerProperty::inst_mon_w_ann }}) {
                $('#total_annual_installment').val(parseInt($('#total_annual_installment').val()) + amount);
                $('#annual-installment').val(Math.round((parseInt($('#total_annual_installment').val())) /
                    parseInt(
                        $('#total_annual_installment_count').val())));
            } else if (val == {{ \App\CustomerProperty::inst_mon_w_quar }}) {

                $('#total_quarterly_installment').val(parseInt($('#total_quarterly_installment').val()) +
                    amount);
                $('#quarterly-installment').val(Math.round((parseInt($('#total_quarterly_installment').val())) /
                    parseInt($('#total_quarterly_installment_count').val())));
            }

        } else if (val == 3) {

            $('#down-payment').val(parseInt($('#down-payment').val()) + amount);


            $('#per_down_payment').val(((parseInt($('#down-payment').val()) / net_amount) * 100).toFixed(2));

        } else if (val == 4) {
            $('#possession').val(parseInt($('#possession').val()) + amount);

            $('#per_possession').val(((parseInt($('#possession').val()) / net_amount) * 100).toFixed(2));
        } else if (val == 5) {
            $('#allotment').val(parseInt($('#allotment').val()) + amount);

            $('#per_allotment').val(((parseInt($('#allotment').val()) / net_amount) * 100).toFixed(2));
        } else if (val == 6) {
            $('#confirmation').val(parseInt($('#confirmation').val()) + amount);

            $('#per_confirmation').val(((parseInt($('#confirmation').val()) / net_amount) * 100).toFixed(2));
        } else if (val == 7) {
            $('#booking').val(parseInt($('#booking').val()) + amount);

            $('#per_booking').val(((parseInt($('#booking').val()) / net_amount) * 100).toFixed(2));
        } else if (val == 8) {
            $('#development').val(parseInt($('#development').val()) + amount);

            $('#per_development').val(((parseInt($('#development').val()) / net_amount) * 100).toFixed(2));
        } else if (val == 9) {
            $('#file_processing').val(parseInt($('#file_processing').val()) + amount);

            $('#per_file_processing').val(((parseInt($('#file_processing').val()) / net_amount) * 100).toFixed(
                2));
        } else if (val == 10) {
            $('#registration').val(parseInt($('#registration').val()) + amount);

            $('#per_registration').val(((parseInt($('#registration').val()) / net_amount) * 100).toFixed(2));
        } else if (val == 18) {
            $('#ballon').val(parseInt($('#ballon').val()) + amount);
            $('#per_file_ballon').val(((parseInt($('#ballon').val()) / net_amount) * 100)
                .toFixed(2));
        } else if (val == 19) {
            $('#bulleting').val(parseInt($('#bulleting').val()) + amount);
            $('#per_bulleting').val(((parseInt($('#bulleting').val()) / net_amount) * 100)
                .toFixed(2));
        }
        $('#remaining_amount').val(0);
        $("input:radio[name='adjust']").each(function(i) {
            this.checked = false;
        });
        updateCurrency();
    }
}

function activeNormal() {

    document.getElementById('normal').style.display = 'block';

    document.getElementById('full_payment').style.display = 'none';
}


function activeFullPayment() {
    document.getElementById('normal').style.display = 'none';

    {{-- document.getElementById('full_payment').style.display = 'block'; --}}
}

$('#return_tenure_full_payment').change(function() {
    tenure = $('#return_tenure_full_payment').val();
    total_month = $('#total_tenure_full_payment').val();

    if (tenure == 2) {
        total_month = total_month / 3;
    } else if (tenure == 3) {
        total_month = total_month / 6;
    } else if (tenure == 4) {
        total_month = total_month / 12;
    }

    $('#per_full_payment').val(Math.round($('#net_amount_full_payment').val() / total_month));

    //update the total_installment
    num = $("#per_full_payment").val();
    var str = '';
    if ((num = num.toString()).length < 9) {
        n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
        if (n) {
            str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
            str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lac ' : '';
            str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
            str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
            str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[
                n[5]
                [1]]) + 'only ' : '';
        }
        $("#per-full-payment-label").text(str);
    }
});

$('#total_tenure_full_payment').keyup(function() {
    tenure = $('#return_tenure_full_payment').val();
    total_month = $('#total_tenure_full_payment').val();

    if (tenure == 2) {
        total_month = total_month / 3;
    } else if (tenure == 3) {
        total_month = total_month / 6;
    } else if (tenure == 4) {
        total_month = total_month / 12;
    }

    $('#per_full_payment').val(Math.round($('#net_amount_full_payment').val() / total_month));

    //update the total_installment
    num = $("#per_full_payment").val();
    var str = '';
    if ((num = num.toString()).length < 9) {
        n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
        if (n) {
            str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
            str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lac ' : '';
            str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
            str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
            str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[
                n[5]
                [1]]) + 'only ' : '';
        }
        $("#per-full-payment-label").text(str);
    }
});

$('#discount_percentage_full_payment').keyup(function() {

    if ($('#discount_percentage_full_payment').val()) {

        price = Math.round(parseFloat($('#discount_percentage_full_payment').val()) / 100 * $(
            '#total_amount_full_payment').val());

        $('#discount_amount_full_payment').val(price);
        $('#net_amount_full_payment').val(price);

        //update per tenure full payment

        tenure = $('#return_tenure_full_payment').val();
        total_month = $('#total_tenure_full_payment').val();

        if (tenure == 2) {
            total_month = total_month / 3;
        } else if (tenure == 3) {
            total_month = total_month / 6;
        } else if (tenure == 4) {
            total_month = total_month / 12;
        }

        $('#per_full_payment').val(Math.round(price / total_month));



        //update the total_installment
        num = $("#discount_amount_full_payment").val();
        var str = '';
        if ((num = num.toString()).length < 9) {
            n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
            if (n) {
                str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
                str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lac ' : '';
                str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' :
                    '';
                str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' :
                    '';
                str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] +
                    ' ' + a[
                        n[5][1]]) + 'only ' : '';
            }
            $("#discount-amount-full-payment-label").text(str);
        }

        //update the total_installment
        num = $("#net_amount_full_payment").val();
        var str = '';
        if ((num = num.toString()).length < 9) {
            n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
            if (n) {
                str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
                str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lac ' : '';
                str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' :
                    '';
                str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' :
                    '';
                str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] +
                    ' ' + a[
                        n[5][1]]) + 'only ' : '';
            }
            $("#net-amount-full-payment-label").text(str);
        }

        //update the total_installment
        num = $("#per_full_payment").val();
        var str = '';
        if ((num = num.toString()).length < 9) {
            n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
            if (n) {
                str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
                str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lac ' : '';
                str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' :
                    '';
                str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' :
                    '';
                str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] +
                    ' ' + a[
                        n[5][1]]) + 'only ' : '';
            }
            $("#per-full-payment-label").text(str);
        }
    } else {
        $('#discount_amount_full_payment').val('');
    }

});

$('#deduction_amount_full_payment').keyup(function() {

    if ($('#deduction_amount_full_payment').val()) {

        price = Math.round(parseInt($('#discount_amount_full_payment').val()) - parseInt($(
            '#deduction_amount_full_payment').val()));

        $('#net_amount_full_payment').val(price);

    } else {
        $('#net_amount_full_payment').val($('rebate_amount_full_payment').val());
    }

    //update per tenure full payment

    tenure = $('#return_tenure_buy_back').val();
    total_month = $('#total_tenure_buy_back').val();

    if (tenure == 2) {
        total_month = total_month / 3;
    } else if (tenure == 3) {
        total_month = total_month / 6;
    } else if (tenure == 4) {
        total_month = total_month / 12;
    }

    $('#per_buy_back').val(Math.round($('#net_amount_full_payment').val() / total_month));



    //update the total_installment
    num = $("#net_amount_full_payment").val();
    var str = '';
    if ((num = num.toString()).length < 9) {
        n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
        if (n) {
            str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
            str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lac ' : '';
            str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
            str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
            str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[
                n[5]
                [1]]) + 'only ' : '';
        }
        $("#net-amount-full-payment-label").text(str);
    }

    //update the total_installment
    num = $("#per_full_payment").val();
    var str = '';
    if ((num = num.toString()).length < 9) {
        n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
        if (n) {
            str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
            str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lac ' : '';
            str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
            str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
            str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[
                n[5]
                [1]]) + 'only ' : '';
        }
        $("#per-full-payment-label").text(str);
    }

});


function addBasicUnit() {
    $('#dealer_rebate_information').show();

    var apend_data = `<select class="form-control chosen-select" id="unit_number" name="unit_number" data-id='0'>
                                    <option value="" selected="">None</option>
                                    @foreach ($unitNumbers as $unit)
                                    <option value="{{ $unit->id }}">{{ $unit->multiProject ? $unit->multiProject->name : '' }} {{ $unit->name }}</option>
                                    @endforeach
                                    </select>`;
    $('#addUnitDetail').empty();
    $('#addUnitDetail').append(apend_data);
    $('.chosen-select').chosen();
}

function addTokenHoldUnit() {
    $('#dealer_rebate_information').show();

    var apend_data = `<select class="form-control chosen-select" id="unit_number" name="unit_number" data-id='0'>
                                    <option value="" selected="">None</option>
                                    @foreach ($unitNumbersTokenHolds as $unit)
                                    <option value="{{ $unit->id }}">{{ $unit->multiProject ? $unit->multiProject->name : '' }} {{ $unit->name }}</option>
                                    @endforeach
                                    </select>`;
    $('#addUnitDetail').empty();
    $('#addUnitDetail').append(apend_data);
    $('.chosen-select').chosen();
}



// if condition exist here
function addDealerUnit() {

    $('#dealer_rebate_information').hide();
    var apend_data = `<select class="form-control chosen-select" id="unit_number" name="unit_number" data-id='1'> 
                                        <option value="" selected="">None</option>
                                        @foreach ($unitNumbers_of_dealer_package_file as $dealer_unit)
                                        <option value="{{ $dealer_unit->id }}">{{ $dealer_unit->multiProject ? $dealer_unit->multiProject->name : '' }} {{ $dealer_unit->name }} -- Security Code({{ $dealer_unit->currency_number }})</option>
                                        @endforeach
                                        </select>`;
    $('#addUnitDetail').empty();
    $('#addUnitDetail').append(apend_data);

    $('.chosen-select').chosen();


}

