<style>
.paymentmodal {
    width: 75%;
    margin: 30px auto;
}
</style>
<script src="assets/js/king-table.js"></script>
<?php
$LoanTotalPaid=0;
foreach($data['LoanApplicantRecords'] as $LoanApplicantRec)
{
	$lacc_id = $LoanApplicantRec->lacc_id;
	$lacc_no = $LoanApplicantRec->lacc_no;
}
?>
<!-- MAIN CONTENT WRAPPER -->
<div class="content">
	<div class="main-content">
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-table"></i>Loan Account Update/Preclosure Settings</h3>
<div class='btn-toolbar pull-right'>
    <div class='btn-group'>
      <button type='button' class='btn btn-primary' id="idreload" onclick="reloadpage('<?php echo $lacc_id; ?>','<?php echo $lacc_no; ?>')" ><i class="fa fa-refresh" aria-hidden="true"></i></button>
    </div>
</div>
			</div>
			<div class="widget-content">
			<form id="frmLoanApplication" name="frmLoanApplication" data-parsley-validate novalidate method="POST">
					<input type="hidden" name="_token" id="_token" value="<?php echo e(csrf_token()); ?>">
				<table  class="table table-striped table-bordered">
					<?php  $i=1;?>
					<?php $__currentLoopData = $data['LoanApplicantRecords']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $LoanApplicantRec): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
				<input type="hidden" name="lacc_id" id="lacc_id" value="<?php echo e($LoanApplicantRec->lacc_id); ?>">
						<tr>
							<th>Application No.</th><td><b><?php echo e($LoanApplicantRec->lacc_no); ?></b></td>	
							<th>Loan amount</th><td><b> <?php echo e($LoanApplicantRec->lacc_loanamt); ?></b></td>
						</tr>						
						<tr>
							<th>Name</th><td><b><?php echo e($LoanApplicantRec->lacc_custname); ?></b></td>	
							<th>Tenor</th><td><b><?php echo e($LoanApplicantRec->lacc_tenor); ?> months</b>
							<input type="hidden" name="loantenor" id="loantenor" value="<?php echo e($LoanApplicantRec->lacc_tenor); ?>">
							</td>
						</tr>
						<tr>
							<th>Branch</th><td><b><?php echo e($LoanApplicantRec->br_name); ?></b></td>	
							<th>Interest</th><td><b><?php echo e($LoanApplicantRec->lacc_intrate); ?>% (per year)</b></td>
						</tr>
						<tr>
							<th>Loan open date</th><td><b><?php echo  date("d-m-Y",strtotime($LoanApplicantRec->lacc_opendt)); ?></b></td>	
							<th>Paid Amount</th>
							<td><font color="green"><b> <?php echo $LoanTotalPaid = $data['LoanTotalPaid']; ?></b></font></td>
						</tr>
					<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>	
				</table>
			</div>
		</div>
		<!-- END DATA EXPORT -->
		
<!-- DATA EXPORT -->
<div class="widget widget-table">
	<div class="widget-header">
		<h3><i class="fa fa-table"></i> Delay Penalty settings</h3>
	</div>
	<div class="widget-content">
		<table  class="table table-striped table-bordered">
			<?php  $i=1;?>
			<?php $__currentLoopData = $data['LoanApplicantRecords']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $LoanApplicantRec): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
				<tr>
					<th>Penalty Amount (in %)</th>
					<td><input type="text" class="form-control" name="dpmt_charge" id="dpmt_charge" value="<?php echo e($LoanApplicantRec->dpmt_charge); ?>"></td>	
					<th><input type="button" class="form-control" name="btndpmt_charge" id="btndpmt_charge" value="Update Penalty Amount"></th>
				</tr>
			<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>	
		</table>
	</div>
</div>
<!-- END DATA EXPORT -->
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-table"></i> Preclosure settings</h3>
			</div>
			<div class="widget-content">
				<table  class="table table-striped table-bordered" id="tabloan">
				<thead>
					<tr>
						<th>SLNo.</th>
						<th>Date</th>					
						<th>Principal</th>							
						<th>Interest</th>					
						<th>Delay Payment</th>					
						<th>Penalty</th>					
						<th>Total</th>
						<th>[o]</th>
					</tr>
				</thead>
				<tbody>	
				<?php
					$totinterestamt =0;
					$tototheramt = 0;
					$totemiamt = 0;
					$totpaidamt = 0;
					$totbalamt = 0;
					$btn=0;
					$paid_amt = 0;
					$bal_amt=0;
					$totdelaypayment=0;
					$totpaiddecreaseonloop = $data['LoanTotalPaid'];
				?>
						<?php $__currentLoopData = $data['LoanInstallmentRecords']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $LoanInstallmentRec): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
						<?php
						$lins_amt=$LoanInstallmentRec->lins_amt;						
						$lins_iamt=$LoanInstallmentRec->lins_iamt;						
						$othr_amt=$LoanInstallmentRec->penaltysum;
						$delayamt=$LoanInstallmentRec->delayamt;
						$totamt = $lins_amt+$lins_iamt+$othr_amt+$delayamt;
						//Sum Total
						$totinterestamt = $totinterestamt +  $lins_iamt;
						$tototheramt = $tototheramt + $othr_amt;
						$totemiamt = $totemiamt+ ($lins_amt+$lins_iamt+$othr_amt+$delayamt);
						$totdelaypayment = $totdelaypayment + $delayamt;
						
						if($totpaiddecreaseonloop > $totamt)
						{
							$paid_amt=$totamt;
							$totpaiddecreaseonloop = $totpaiddecreaseonloop - $totamt;
						}
						else
						{
							$paid_amt = $totpaiddecreaseonloop;
							$totpaiddecreaseonloop = 0;
						}
												
						$bal_amt=$totamt-$paid_amt;
						
						$totpaidamt = $totpaidamt + $paid_amt;
						$totbalamt = $totbalamt + $bal_amt;						
						
				//Coding to Calculate days difference between two days start here	
				$currentdt = strtotime(date("Y-m-d")); //Current day
				$installmentdt = strtotime($LoanInstallmentRec->lins_date); //Installment day
				$datediff = $currentdt - $installmentdt;				
				$daysdifference =  round($datediff / (60 * 60 * 24)) ;
				if($daysdifference >=0)
				{
					if($bal_amt > 0)
					{
						$daysdifference=$daysdifference;
					}
					else
					{
						$daysdifference=0;
					}
				}
				else
				{
					$daysdifference =0;
				}
				//Coding to Calculate days difference between two days ends here
						?>
						<tr  
						<?php
						$controlstatus="";
						if($bal_amt > 0)
						{
						echo 'style="color:black;"';
						$controlstatus = "  ";
						}
						else
						{
						echo 'style="color:green;"';
						$controlstatus = " readonly ";
						}
						?>
						>
<td>
<?php echo e($LoanInstallmentRec->lins_no); ?>

<br><input type="hidden" name="lins_id[]" id="lins_id[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_id); ?>" style="width: 50px;">
<input type="hidden" name="lins_no[]" id="lins_no[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_no); ?>" style="width: 50px;">
</td><!-- SL No. -->
<td>
<b>Date</b>
<?php
if($bal_amt == 0)
{
	echo "<input type='date' name='lins_date[]' id='lins_date[$LoanInstallmentRec->lins_no]' value='$LoanInstallmentRec->lins_date' class='form-control' {{$controlstatus}} onchange='updaterecord()' onkeyup='updaterecord()' ><br>";
}
else
{
	echo "<input type='date' name='lins_date[]' id='lins_date[$LoanInstallmentRec->lins_no]' value='$LoanInstallmentRec->lins_date' class='form-control' {{$controlstatus}} onchange='updaterecord()' onkeyup='updaterecord()' >";
}
	echo "<input type='hidden' name='hidden_lins_date[]' id='hidden_lins_date[$LoanInstallmentRec->lins_no]' value='$LoanInstallmentRec->lins_date' class='form-control' >";
?>
<b>Cheque No.</b>
<input type="text" name="lins_chqno[]" id="lins_chqno[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_chqno); ?>"  placeholder='Cheque Number' class='form-control' <?php echo e($controlstatus); ?>  onchange='updaterecord()' onkeyup='updaterecord()'>
<input type="hidden" name="hidden_lins_chqno[]" id="hidden_lins_chqno[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_chqno); ?>"  placeholder='Cheque Number' class='form-control'>
<b>Any note.</b>
<textarea name="lins_note[]" id="lins_note[<?php echo e($LoanInstallmentRec->lins_no); ?>]"  placeholder='Enter Note here' class='form-control' <?php echo e($controlstatus); ?>  onchange='updaterecord()' onkeyup='updaterecord()'><?php echo e($LoanInstallmentRec->lins_note); ?></textarea>
<input type="hidden" name="hidden_lins_note[]" id="hidden_lins_note[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_note); ?>"  class='form-control'>
</td><!-- Installment dt -->
<td><input type="text" name="lins_amt[]" id="lins_amt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_amt); ?>" class='form-control' <?php echo e($controlstatus); ?> onchange='updaterecord()' onkeyup='updaterecord()' ><input type="hidden" name="hidden_lins_amt[]" id="hidden_lins_amt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_amt); ?>" class='form-control'></td><!-- Principle Amt -->
							<td>
<b>Interest %</b>
<?php
if($bal_amt > 0)
{
?>
	<input type="text" name="lins_iperc[]" id="lins_iperc[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_iperc); ?>"  class="form-control" <?php echo e($controlstatus); ?>  onchange='updaterecord()' onkeyup='updaterecord()' >
<?php
}
else
{
?>
	<input type="text" name="lins_iperc[]" id="lins_iperc[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_iperc); ?>"  class="form-control" <?php echo e($controlstatus); ?> onchange='updaterecord()' onkeyup='updaterecord()' >
<?php
}
?><input type="hidden" name="hidden_lins_iperc[]" id="hidden_lins_iperc[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_iperc); ?>">
<hr>
<b>Interest Amt</b>
<input type="text" name="lins_iamt[]" id="lins_iamt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_iamt); ?>" class='form-control' <?php echo e($controlstatus); ?> onchange='updaterecord()' onkeyup='updaterecord()' >
<input type="hidden" name="hidden_lins_iamt[]" id="hidden_lins_iamt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->lins_iamt); ?>" >
				</td><!-- Interest -->
				<td><input type="text" name="delayamt[]" id="delayamt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->delayamt); ?>"  class='form-control' <?php echo e($controlstatus); ?>  onchange='updaterecord()' onkeyup='updaterecord()'>
				<input type="hidden" name="hidden_delayamt[]" id="hidden_delayamt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->delayamt); ?>" >
				[<?php echo $daysdifference; ?> Days]</td><!-- Delay Payment -->
				<td>
				<b>Cheque Bounce</b>
				<input type="text" name="chequebouncecharges[]" id="chequebouncecharges[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->chequebouncecharges); ?>"  class='form-control'  <?php echo e($controlstatus); ?>    onchange='updaterecord()' onkeyup='updaterecord()'>
				<input type="hidden" name="hidden_chequebouncecharges[]" id="hidden_chequebouncecharges[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->chequebouncecharges); ?>" ><br>
				<b>Legal Case</b>
				<input type="text" name="casecharges[]" id="casecharges[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->casecharges); ?>"  class='form-control'  <?php echo e($controlstatus); ?>    onchange='updaterecord()' onkeyup='updaterecord()'>
				<input type="hidden" name="hidden_casecharges[]" id="hidden_casecharges[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->casecharges); ?>"><br>
				<b>Other charges</b>
				<input type="text" name="othercharges[]" id="othercharges[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->othercharges); ?>"  class='form-control'  <?php echo e($controlstatus); ?>  onchange='updaterecord()' onkeyup='updaterecord()'>
				<input type="hidden" name="hidden_othercharges[]" id="hidden_othercharges[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo e($LoanInstallmentRec->othercharges); ?>"><br>
				</td><!-- Others Amt -->
				<td>
				<b>Total Amount: </b>
				<input type="text" name="totamt[]" id="totamt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo $totamt; ?>" class='form-control' <?php echo e($controlstatus); ?>  onchange='updaterecord()' onkeyup='updaterecord()'>
				<input type="hidden" name="hidden_totamt[]" id="hidden_totamt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo $totamt; ?>" ><br>
				<b>Paid Amount: </b>
				<input type="text" name="paid_amt[]" id="paid_amt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo $paid_amt; ?>" class='form-control' <?php echo e($controlstatus); ?>  onchange='updaterecord()' onkeyup='updaterecord()'>
				<input type="hidden" name="hidden_paid_amt[]" id="hidden_paid_amt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo $paid_amt; ?>"><br>
				<b>Balance Amount</b>
				<input type="text" name="bal_amt[]" id="bal_amt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo $bal_amt; ?>" class='form-control' readonly >
				<input type="hidden" name="hidden_bal_amt[]" id="hidden_bal_amt[<?php echo e($LoanInstallmentRec->lins_no); ?>]" value="<?php echo $bal_amt; ?>" ></td><!-- EMI Amt -->
				<td><?php
					if($bal_amt == 0)
					{
						echo "<input type='checkbox' name='chk[]' id='chk[$LoanInstallmentRec->lins_no]' value='$LoanInstallmentRec->lins_no' class='custom-control-input' checked disabled >";
					}
					else
					{
						echo "<input type='checkbox' name='chk[]' id='chk[$LoanInstallmentRec->lins_no]' value='$LoanInstallmentRec->lins_no' class='custom-control-input' checked onclick='return funchkboxpreclosure($LoanInstallmentRec->lins_no)'  >";
					}
					?>
				</td><!-- Balance Amt -->
			</tr>
						<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
				</tbody>
				<tfoot>
						<th></th>
						<th></th>					
						<th id="idtblprincipal"><?php echo e($LoanApplicantRec->lacc_loanamt); ?></th>
						<th id="idtblinterest"><?php echo e($totinterestamt); ?></th>			
						<th id="idtbldelaypayment"><?php echo e($totdelaypayment); ?></th>					
						<th id="idtblpenalty"><?php echo e($tototheramt); ?></th>
						<th id="idtbltotal"><?php echo e($totbalamt); ?></th>
						<th>-</th>
				</tfoot>
				</table>
			</div>
			<div class="widget">
				<div class="widget-header">
					<center>
						<button type="submit" class="btn btn-default btnpreclosure_submit">Confirm and Update</button>
						<img src="img/inserting.gif" class="loadingimg1" style="height: 25px;">
						<img src="img/inserting.gif" class="loadingimg2" style="height: 25px;">
						<img src="img/inserting.gif" class="loadingimg3" style="height: 25px;">
					</center>
				</div>
			</div>			
		</div>
		<!-- END DATA EXPORT -->		
		</form>
	</div>
</div>
<!-- /main -->
<script>
	$('.createMod').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
	
	$('.edit').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href')); 
	});
	
	$(document).ready(function(){
		$(".loadingimg1").hide();
		$(".loadingimg2").hide();
		$(".loadingimg3").hide();
	});
	
	$('#mypayment').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanpaymentcalculation',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				var Obj=JSON.parse(data);	
				$('#lacc_id1').val($("input[name='lacc_id']").val());
				$('#lins_id1').val(lins_id);
				var CurrentPayableAmt =Obj.CurrentPayableAmt;
				var PreviousPayableAmt =Obj.PreviousPayableAmt;
				var TotalPayableAmt =Obj.TotalPayableAmt;
				$('#currentpayable').val(CurrentPayableAmt);
				$('#previousbalance').val(PreviousPayableAmt);
				$('#totalpayable').val(TotalPayableAmt);
            }
        });
    })
	
	$('#myotheramt').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanpaymentcalculation',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				var Obj=JSON.parse(data);	
				$('#lacc_id2').val($("input[name='lacc_id']").val());
				$('#lins_id2').val(lins_id);
            }
        });
    })
//Preclosure Formula Starts here
	function funchkboxpreclosure(selectedchkbox)
	{
		var previousbox = selectedchkbox-1;
		/*
		//Get the checkbox
		var checkBox = document.getElementById("chk[" + selectedchkbox + "]");
		if (checkBox.checked == true)
		{	
		}
		else
		{
		}
		*/
			//Principal Amount Block starts here
			document.getElementById("lins_amt[" + previousbox + "]").value =  parseFloat(document.getElementById("lins_amt[" + previousbox + "]").value) + parseFloat(document.getElementById("lins_amt[" + selectedchkbox + "]").value);
			document.getElementById("lins_amt[" + selectedchkbox + "]").value = 0;
			//Principal Amount Block ends here
			//Interest%  Block starts here
			//document.getElementById("lins_iperc[" + previousbox + "]").value =  parseFloat(document.getElementById("lins_iperc[" + previousbox + "]").value) + parseFloat(document.getElementById("lins_iperc[" + selectedchkbox + "]").value);
			document.getElementById("lins_iperc[" + selectedchkbox + "]").value = 0;
			//Interest% Block ends here
			//Interest Amount Block starts here
			document.getElementById("lins_iamt[" + previousbox + "]").value =  parseFloat(document.getElementById("lins_iamt[" + previousbox + "]").value) + parseFloat(document.getElementById("lins_iamt[" + selectedchkbox + "]").value);
			document.getElementById("lins_iamt[" + selectedchkbox + "]").value = 0;
			//Interest Amount Block ends here
			//Delay Amount Block starts here
			document.getElementById("delayamt[" + previousbox + "]").value =  parseFloat(document.getElementById("delayamt[" + previousbox + "]").value) + parseFloat(document.getElementById("delayamt[" + selectedchkbox + "]").value);
			document.getElementById("delayamt[" + selectedchkbox + "]").value = 0;
			//Delay Amount Block ends here
			//Cheque Bounce Block starts here
			document.getElementById("chequebouncecharges[" + previousbox + "]").value =  parseFloat(document.getElementById("chequebouncecharges[" + previousbox + "]").value) + parseFloat(document.getElementById("chequebouncecharges[" + selectedchkbox + "]").value);
			document.getElementById("chequebouncecharges[" + selectedchkbox + "]").value = 0;
			//Cheque Bounce Block ends here
			//Legal Case Block starts here
			document.getElementById("casecharges[" + previousbox + "]").value =  parseFloat(document.getElementById("casecharges[" + previousbox + "]").value) + parseFloat(document.getElementById("casecharges[" + selectedchkbox + "]").value);
			document.getElementById("casecharges[" + selectedchkbox + "]").value = 0;
			//Legal Case Block ends here
			//Other charges Block starts here
			document.getElementById("othercharges[" + previousbox + "]").value =  parseFloat(document.getElementById("othercharges[" + previousbox + "]").value) + parseFloat(document.getElementById("othercharges[" + selectedchkbox + "]").value);
			document.getElementById("othercharges[" + selectedchkbox + "]").value = 0;
			//Other charges Block ends here
			//Paid Amount Block starts here
			document.getElementById("paid_amt[" + previousbox + "]").value =  parseFloat(document.getElementById("paid_amt[" + previousbox + "]").value) + parseFloat(document.getElementById("paid_amt[" + selectedchkbox + "]").value);
			document.getElementById("paid_amt[" + selectedchkbox + "]").value = 0;
			//Paid Amount Block ends here
			updaterecord()
			document.getElementById("totamt[" + selectedchkbox + "]").value = 0;
			document.getElementById("bal_amt[" + selectedchkbox + "]").value = 0;
	}
//Preclosure Formula ends here

	function calculatebal()
	{
		balanceamt = parseFloat($('#totalpayable').val()) - parseFloat($('#ltr_amt').val());
		$('#balanceamt').val(balanceamt);
		if($('#balanceamt').val() == "NaN")
		{
			$('#balanceamt').val(0);
		}
	}
	
	//### Payment submission entry code starts here
	flag=0;
	$('.btnloanpayment').click(function(e)
	{
		e.preventDefault();
		_token = $("input[name='_token']").val();		           
		var lacc_id1 = $("input[name='lacc_id1']").val();
		var lins_id1 = $("input[name='lins_id1']").val();
		var ltr_transdt = $("input[name='ltr_transdt']").val();
		var currentpayable = $("input[name='currentpayable']").val();
		var previousbalance = $("input[name='previousbalance']").val();
		var totalpayable = $("input[name='totalpayable']").val();
		var ltr_amt = $("input[name='ltr_amt']").val();
		var balanceamt = $("input[name='balanceamt']").val();
		var ltr_billno = $("input[name='ltr_billno']").val();
		var ltr_chqno = $("input[name='ltr_chqno']").val();
		var ltr_paymenttype = $('#ltr_paymenttype').val();
		var ltr_bank = $("input[name='ltr_bank']").val();
		var ltr_note = $("input[name='ltr_note']").val();
		if(ltr_billno == "" )
		{
			alert("Transaction No. Should not be empty...");
		}
		else if(ltr_transdt == "" )
		{
			alert("Transaction Date Should not be empty...");
		}
		else if(ltr_amt == "" )
		{
			alert("Paid Amount. Should not be empty...");
		}
		else if(parseFloat(ltr_amt) < parseFloat(previousbalance) )
		{
			alert("Paid amount should be greater than Previous balance...");
		}
		else if(ltr_paymenttype == "" )
		{
			alert("Payment type should not be empty...");
		}
		else
		{
			$.ajax({
				url: "loaninstallmentpayment",
				type:'POST',
				dataType: 'json',
				//cache : false,
				//processData: false,
				data: {_token:_token,lacc_id:lacc_id1, lins_id:lins_id1, ltr_transdt:ltr_transdt, ltr_transtype:"Payment", ltr_billno:ltr_billno, ltr_amt:ltr_amt, ltr_paymenttype:ltr_paymenttype, ltr_chqno:ltr_chqno, ltr_bank:ltr_bank, ltr_note:ltr_note, ltr_status:"Active"},
				success: function(data)
				{
					if(data.status == "Success")
					{
						alert("Loan payment done successfully..");
						//$('#sideMenu').load('viewloanaccount');
						LoadLoanApplication(lacc_id1,lins_id1);
						$('#mypayment').modal('toggle');
					}
					else
					{
						alert("Failed to Generate Purchase Bill..");
						//printErrorMsg(data.message)
					}
				}
			});
		}
	});
	//### Payment submission entry code ends here
	
	function LoadLoanApplication(b,acno)
	{
		$.ajax({
			url: 'LoanAcPanel',
			type: 'GET',
			data:'&lacc_id='+b+'&acno='+acno,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
	
	//###Delay Payment charge starts here###
	$('#btndpmt_charge').click(function(e)
	{
		e.preventDefault();
		_token = $("input[name='_token']").val();	
		var lacc_id = $("input[name='lacc_id']").val();
		var dpmt_charge = $("input[name='dpmt_charge']").val();
		$.ajax({
			url: "updatedelaypaymentcharge",
			type:'POST',
			dataType: 'json',
			//cache : false,
			//processData: false,
			data: {_token:_token,lacc_id:lacc_id, dpmt_charge:dpmt_charge},
			success: function(data)
			{
				if(data.status == "Success")
				{
					alert("Delay payment charge updated successfully..");
					//$('#sideMenu').load('viewloanaccount');
				}
				else
				{
					alert("Failed to update Delay payment charge..");
					//printErrorMsg(data.message)
				}
			}
		});
	});
	//###Delay Payment charge ends here###
	//#Reload Starts here
	function reloadpage(lacc_id,lacc_no)
	{
		document.getElementById("idreload").innerHTML = "<i class='fa fa-refresh fa-spin' aria-hidden='true'></i>";
		$.ajax({
			url: 'loanAccountPreclosure',
			type: 'get',
			data:'&lacc_id='+lacc_id+'&lacc_no='+lacc_no,
			success: function(data)
			{
				$('#sideMenu').html(data);
				document.getElementById("idreload").innerHTML = "<i class='fa fa-refresh' aria-hidden='true'></i>";
			}
		});
	}
	//#Reload ends here
	//#*#Recalculation starts here#*#
	function updaterecord()
	{
		var totSumPrincipal = 0;
		var totSumPerc = 0.00;
		var totSumInterest = 0;
		var totSumDelayPayment = 0;
		var totSumChequeBounce = 0;
		var totSumLegalCase = 0;
		var totSumOtherCharges = 0;
		var totSumTotAmt = 0;
		var totSumPaidAmt = 0;
		var totSumBalAmt = 0;
		
		//Loan Tenor
		var loantenor=document.getElementById("loantenor").value;	
		
		lins_id1=[];
		lins_no=[];
		lins_amt=[];
		lins_iamt=[];
		delayamt=[];
		totamt=[];
		paid_amt=[];
		bal_amt=[];
		lins_date=[];
		lins_iperc=[];
		lins_chqno=[];
		lins_note=[];
		chequebouncecharges=[];
		casecharges=[]; 
		othercharges=[];
		
		arrlins_id=[];
		arrlins_no=[];
		arrlins_amt=[];
		arrlins_iamt=[];
		arrdelayamt=[];
		arrpenaltysum=[];
		arrtotamt=[];
		arrpaid_amt=[];
		arrbal_amt=[];
		arrlins_date=[];
		arrlins_iperc=[];
		arrlins_chqno=[];
		arrlins_note=[];
		arrchequebouncecharges=[];
		arrcasecharges=[];
		arrothercharges=[];
		
		var lins_id1=document.getElementsByName("lins_id[]");
		var lins_no=document.getElementsByName("lins_no[]");
		var lins_amt=document.getElementsByName("lins_amt[]");
		var lins_iamt=document.getElementsByName("lins_iamt[]");
		var delayamt=document.getElementsByName("delayamt[]");
		var totamt=document.getElementsByName("totamt[]");
		var paid_amt=document.getElementsByName("paid_amt[]");
		var bal_amt=document.getElementsByName("bal_amt[]");
		var lins_date=document.getElementsByName("lins_date[]");
		var lins_iperc=document.getElementsByName("lins_iperc[]");
		var lins_chqno=document.getElementsByName("lins_chqno[]");
		var lins_note=document.getElementsByName("lins_note[]");
		var chequebouncecharges=document.getElementsByName("chequebouncecharges[]");
		var casecharges=document.getElementsByName("casecharges[]");
		var othercharges=document.getElementsByName("othercharges[]");
		var arrlength = lins_no.length;
		for(index = 1; index <= arrlength; index++) 
		{
			arrlins_id[index] = lins_id1[index-1].value;
			arrlins_no[index] = lins_no[index-1].value;
			arrlins_amt[index] = lins_amt[index-1].value;
			arrlins_iamt[index] = lins_iamt[index-1].value;
			arrdelayamt[index] = delayamt[index-1].value;
			arrtotamt[index] = totamt[index-1].value;
			arrpaid_amt[index] = paid_amt[index-1].value;
			arrbal_amt[index] = bal_amt[index-1].value;
			arrlins_date[index] = lins_date[index-1].value;
			arrlins_iperc[index] = lins_iperc[index-1].value;
			arrlins_chqno[index] = lins_chqno[index-1].value;
			arrlins_note[index] = lins_note[index-1].value;
			arrchequebouncecharges[index] = chequebouncecharges[index-1].value;
			arrcasecharges[index] = casecharges[index-1].value;
			arrothercharges[index] = othercharges[index-1].value;

			//Interest Amt
			document.getElementById("lins_iamt[" + index + "]").value = Math.round(parseFloat(document.getElementById("lins_iperc[" + index + "]").value) * parseFloat(document.getElementById("lins_amt[" + index + "]").value) / 100 * parseFloat(loantenor));
			
			//
var month = $("#emiMonth").val();
var rate = $("#InterestRate").val();
var pamt = $("#prncipleAmount").val();

var monthlyInterestRatio = (rate/100)/12;
var monthlyInterest = (monthlyInterestRatio*pamt);
var top = Math.pow((1+monthlyInterestRatio),month);
var bottom = top -1;
var sp = top / bottom;
var emi = ((pamt * monthlyInterestRatio) * sp);
var result = emi.toFixed(2);
var totalAmount = emi*month;
var yearlyInteret = totalAmount-pamt;
var downPayment = pamt*(20/100);

$("#result").empty();
$("#result").append("Your EMI = "+result);

			//Total Amt
			document.getElementById("totamt[" + index + "]").value = Math.round(parseFloat(document.getElementById("lins_amt[" + index + "]").value) + parseFloat(document.getElementById("lins_iamt[" + index + "]").value) + parseFloat(document.getElementById("delayamt[" + index + "]").value) + parseFloat(document.getElementById("chequebouncecharges[" + index + "]").value) + parseFloat(document.getElementById("casecharges[" + index + "]").value) + parseFloat(document.getElementById("othercharges[" + index + "]").value));
			
			//Balance Amount
			document.getElementById("bal_amt[" + index + "]").value = Math.round((parseFloat(document.getElementById("lins_amt[" + index + "]").value) + parseFloat(document.getElementById("lins_iamt[" + index + "]").value) + parseFloat(document.getElementById("delayamt[" + index + "]").value) + parseFloat(document.getElementById("chequebouncecharges[" + index + "]").value) + parseFloat(document.getElementById("casecharges[" + index + "]").value) + parseFloat(document.getElementById("othercharges[" + index + "]").value)) - parseFloat(document.getElementById("paid_amt[" + index + "]").value) );
			
			totSumPrincipal = totSumPrincipal + parseFloat(lins_amt[index-1].value);
			totSumInterest = totSumInterest + parseFloat(lins_iamt[index-1].value);
			totSumPerc = totSumPerc + parseFloat(lins_iperc[index-1].value);
			totSumDelayPayment = totSumDelayPayment + parseFloat(delayamt[index-1].value);
			totSumChequeBounce = totSumChequeBounce + parseFloat(chequebouncecharges[index-1].value);
			totSumLegalCase = totSumLegalCase + parseFloat(casecharges[index-1].value); 
			totSumOtherCharges = totSumOtherCharges + parseFloat(othercharges[index-1].value);
			totSumTotAmt = totSumTotAmt + parseFloat(totamt[index-1].value);
			totSumPaidAmt = totSumPaidAmt +  parseFloat(paid_amt[index-1].value);
			totSumBalAmt = totSumBalAmt + parseFloat(bal_amt[index-1].value);
		}

		document.getElementById("idtblprincipal").innerHTML = "" + totSumPrincipal.toFixed(2);
		document.getElementById("idtblinterest").innerHTML = "Interest - " + totSumPerc.toFixed(2) + "% <br>" + "Int Amt - " + "" + totSumInterest.toFixed(2);
		document.getElementById("idtbldelaypayment").innerHTML = "" + totSumDelayPayment.toFixed(2);
		document.getElementById("idtblpenalty").innerHTML = "Cheque Bounce - " + totSumChequeBounce.toFixed(2) + " <br>" + "Legal Case - " + "" + totSumLegalCase.toFixed(2) + " <br>" + "Other Charges - " + "" + totSumOtherCharges.toFixed(2);
		document.getElementById("idtbltotal").innerHTML =  "Total Amount - " + totSumTotAmt.toFixed(2) + " <br>" + "Paid Amount - " + "" + totSumPaidAmt.toFixed(2) + " <br>" + "Balance Amount - " + "" + totSumBalAmt.toFixed(2);
	}
	//#*#Recalculation ends here#*#
	//###Coding to Update Preclosure starts here
	$('.btnpreclosure_submit').click(function(e)
	{
		e.preventDefault();
		$(".btnpreclosure_submit").hide();
		$(".loadingimg1").show();
		$(".loadingimg2").show();
		$(".loadingimg3").show();
		_token = $("input[name='_token']").val();
		var lacc_id = $("input[name='lacc_id']").val();
		
		//
		lins_id=[];
		lins_no=[];
		lins_amt=[];
		lins_iamt=[];
		delayamt=[];
		totamt=[];
		paid_amt=[];
		bal_amt=[];
		lins_date=[];
		lins_iperc=[];
		lins_chqno=[];
		lins_note=[];
		chequebouncecharges=[];
		casecharges=[]; 
		othercharges=[];
		//
		hidden_lins_amt=[];
		hidden_lins_iamt=[];
		hidden_delayamt=[];
		hidden_totamt=[];
		hidden_paid_amt=[];
		hidden_bal_amt=[];
		hidden_lins_date=[];
		hidden_lins_iperc=[];
		hidden_lins_chqno=[];
		hidden_lins_note=[];
		hidden_chequebouncecharges=[];
		hidden_casecharges=[];
		hidden_othercharges=[];
		//
		arrlins_id=[];
		arrlins_no=[];
		arrlins_amt=[];
		arrlins_iamt=[];
		arrdelayamt=[];
		arrpenaltysum=[];
		arrtotamt=[];
		arrpaid_amt=[];
		arrbal_amt=[];
		arrlins_date=[];
		arrlins_iperc=[];
		arrlins_chqno=[];
		arrlins_note=[];
		arrchequebouncecharges=[];
		arrcasecharges=[];
		arrothercharges=[];
		//
		hidden_arrlins_amt=[];
		hidden_arrlins_iamt=[];
		hidden_arrdelayamt=[];
		hidden_arrpenaltysum=[];
		hidden_arrtotamt=[];
		hidden_arrpaid_amt=[];
		hidden_arrbal_amt=[];
		hidden_arrlins_date=[];
		hidden_arrlins_iperc=[];
		hidden_arrlins_chqno=[];
		hidden_arrlins_note=[];
		hidden_arrchequebouncecharges=[];
		hidden_arrcasecharges=[];
		hidden_arrothercharges=[];
		
		var lins_id=document.getElementsByName("lins_id[]");
		var lins_no=document.getElementsByName("lins_no[]");
		var lins_amt=document.getElementsByName("lins_amt[]");
		var lins_iamt=document.getElementsByName("lins_iamt[]");
		var delayamt=document.getElementsByName("delayamt[]");
		var totamt=document.getElementsByName("totamt[]");
		var paid_amt=document.getElementsByName("paid_amt[]");
		var bal_amt=document.getElementsByName("bal_amt[]");
		var lins_date=document.getElementsByName("lins_date[]");
		var lins_iperc=document.getElementsByName("lins_iperc[]");
		var lins_chqno=document.getElementsByName("lins_chqno[]");
		var lins_note=document.getElementsByName("lins_note[]");
		var chequebouncecharges=document.getElementsByName("chequebouncecharges[]");
		var casecharges=document.getElementsByName("casecharges[]");
		var othercharges=document.getElementsByName("othercharges[]");

		var hidden_lins_amt=document.getElementsByName("hidden_lins_amt[]");
		var hidden_lins_iamt=document.getElementsByName("hidden_lins_iamt[]");
		var hidden_delayamt=document.getElementsByName("hidden_delayamt[]");
		var hidden_totamt=document.getElementsByName("hidden_totamt[]");
		var hidden_paid_amt=document.getElementsByName("hidden_paid_amt[]");
		var hidden_bal_amt=document.getElementsByName("hidden_bal_amt[]");
		var hidden_lins_date=document.getElementsByName("hidden_lins_date[]");
		var hidden_lins_iperc=document.getElementsByName("hidden_lins_iperc[]");
		var hidden_lins_chqno=document.getElementsByName("hidden_lins_chqno[]");
		var hidden_lins_note=document.getElementsByName("hidden_lins_note[]");
		var hidden_chequebouncecharges=document.getElementsByName("hidden_chequebouncecharges[]");
		var hidden_casecharges=document.getElementsByName("hidden_casecharges[]");
		var hidden_othercharges=document.getElementsByName("hidden_othercharges[]");
		
		for(index = 0; index < lins_no.length; ++index)
		{
			arrlins_id[index] = lins_id[index].value;
			arrlins_no[index] = lins_no[index].value;
			arrlins_amt[index] = lins_amt[index].value;
			arrlins_iamt[index] = lins_iamt[index].value;
			arrdelayamt[index] = delayamt[index].value;
			arrtotamt[index] = totamt[index].value;
			arrpaid_amt[index] = paid_amt[index].value;
			arrbal_amt[index] = bal_amt[index].value;
			arrlins_date[index] = lins_date[index].value;
			arrlins_iperc[index] = lins_iperc[index].value;
			arrlins_chqno[index] = lins_chqno[index].value;
			arrlins_note[index] = lins_note[index].value;
			arrchequebouncecharges[index] = chequebouncecharges[index].value;
			arrcasecharges[index] = casecharges[index].value;
			arrothercharges[index] = othercharges[index].value;
			
			hidden_arrlins_amt[index] = hidden_lins_amt[index].value;
			hidden_arrlins_iamt[index] = hidden_lins_iamt[index].value;
			hidden_arrdelayamt[index] = hidden_delayamt[index].value;
			hidden_arrtotamt[index] = hidden_totamt[index].value;
			hidden_arrpaid_amt[index] = hidden_paid_amt[index].value;
			hidden_arrbal_amt[index] = hidden_bal_amt[index].value;
			hidden_arrlins_date[index] = hidden_lins_date[index].value;
			hidden_arrlins_iperc[index] = hidden_lins_iperc[index].value;
			hidden_arrlins_chqno[index] = hidden_lins_chqno[index].value;
			hidden_arrlins_note[index] = hidden_lins_note[index].value;
			hidden_arrchequebouncecharges[index] = hidden_chequebouncecharges[index].value;
			hidden_arrcasecharges[index] = hidden_casecharges[index].value;
			hidden_arrothercharges[index] = hidden_othercharges[index].value;
		}  
		$.ajax({
			url: "updatepreclosure",
			type:'POST',
			dataType: 'json',
			//cache : false,
			//processData: false,
			data: {_token:_token,lacc_id:lacc_id,arrlins_id:arrlins_id,arrlins_no:arrlins_no,arrlins_amt:arrlins_amt,arrlins_iamt:arrlins_iamt,arrdelayamt:arrdelayamt,arrtotamt:arrtotamt,arrpaid_amt:arrpaid_amt,arrbal_amt:arrbal_amt,arrlins_date:arrlins_date,arrlins_iperc:arrlins_iperc,arrlins_chqno:arrlins_chqno,arrlins_note:arrlins_note,arrchequebouncecharges:arrchequebouncecharges,arrcasecharges:arrcasecharges,arrothercharges:arrothercharges,hidden_arrlins_amt:hidden_arrlins_amt,hidden_arrlins_iamt:hidden_arrlins_iamt,hidden_arrdelayamt:hidden_arrdelayamt,hidden_arrtotamt:hidden_arrtotamt,hidden_arrpaid_amt:hidden_arrpaid_amt,hidden_arrbal_amt:hidden_arrbal_amt,hidden_arrlins_date:hidden_arrlins_date,hidden_arrlins_iperc:hidden_arrlins_iperc,hidden_arrlins_chqno:hidden_arrlins_chqno,hidden_arrlins_note:hidden_arrlins_note,hidden_arrchequebouncecharges:hidden_arrchequebouncecharges,hidden_arrcasecharges:hidden_arrcasecharges,hidden_arrothercharges:hidden_arrothercharges},
			success: function(data)
			{
				if(data.status == "Success")
				{	
					$(".btnpreclosure_submit").show();
					$(".loadingimg1").hide();
					$(".loadingimg2").hide();
					$(".loadingimg3").hide();
					alert("Loan Installment detail updated successfully..");			
				}
				else
				{
					alert("Failed to Generate Purchase Bill..");
				}
			}
		});
	});
	//###Coding to update preclosure ends here
</script>