<%-- 
    Document   : LoanDisbursement
    Created on : Sep 21, 2016, 8:15:48 PM
    Author     : 590685
--%>

<%@page import="DataEntryBean.LoanDisbursementHdrBean"%>
<%@page import="DataEntryBean.LoanDisbursementDetailBean"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="LoginDb.DbHandler"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@page import="java.sql.Connection"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Loan Disbursement Schedule</title>
        <script src="${pageContext.request.contextPath}/js/Calendar.js"></script>
        <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon" />
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.ico" type="image/x-icon" />
        <script src="${pageContext.request.contextPath}/jquery-ui-1.10.4.custom/development-bundle/jquery-1.10.2.js"></script>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/TableCSSCode.css" />
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/styles.css?pacs=<%=((new java.util.Date()).getTime())%>"/>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/ButtonAndText.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/info.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-ui-1.10.4.custom/development-bundle/jquery-1.10.2.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <script type="text/javascript" src="/PacsApplication/chromejs/chrome.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <script type="text/javascript" src="/PacsApplication/js/iFrame_killer.js"></script>
    </head>
    <body  onload="noBack()">

        <script type="text/javascript">
            function noBack()
            {
                window.history.forward();
            }

            function popup_productCode(width, height, loanAcc) {
                if (window.innerWidth) {
                    LeftPosition = (window.innerWidth - width) / 2;
                    TopPosition = ((window.innerHeight - height) / 2) - 20;
                }
                else {
                    LeftPosition = (parseInt(window.screen.width) - width) / 2;
                    TopPosition = ((parseInt(window.screen.height) - height) / 2) - 20;
                }
                attr = 'resizable=yes,scrollbars=yes,width=' + width + ',height=' +
                    height + ',screenX=300,screenY=200,left=' + LeftPosition + ',top=' +
                    TopPosition + '';
                var screenName = "LoanDisbursement.jsp";
                var  lovKey="LoanAcctSearch"
                popWin = open('${pageContext.request.contextPath}/CommonSearchInformationLoanLOV.jsp?loanAcc=' + loanAcc + '&screenName=' + screenName+'&lovKey='+lovKey, 'LoanAcctSearch', attr);
                popWin.focus();

            }


            function submitform(LoanDisburseForm)
            {
                if (LoanDisburseForm.loanAcc.value == "")
                {
                    alert("Please enter Loan Account Number.");
                    LoanDisburseForm.loanAcc.focus();
                    return false;
                }
               // re = /^\d+$/; //this checks whether field contains digits
               // if (!re.test(document.getElementById("loanAcc").value))
               // {
                //    alert("!! Loan Account could only contain numbers !!");
                //    LoanDisburseForm.loanAcc.focus();
                //    return false;
               // }
                document.forms[0].handle_id.value = "SearchLoanAcc";
                document.forms[0].action = '/PacsApplication/LoanDisbursementServlet';
                document.forms[0].submit();

            }


            function UpdateForm(LoanDisburseForm)
            {
                if(checkbeforeUpdate(LoanDisburseForm)){
                    if(checkDuplicateUpdate()){

                        if(checkApprovedAmtUpdate()){

                            document.LoanDisburseForm.handle_id.value = "Update";
                            document.LoanDisburseForm.action = '/PacsApplication/LoanDisbursementServlet';
                            document.LoanDisburseForm.submit();
                        }
                    }
                }
            }
            function CreateForm2(LoanDisburseForm)
            {
                if(checkbeforeCreate(LoanDisburseForm)){
                    if(checkDuplicateCreate()){
                        if(checkApprovedAmt()){

                            document.LoanDisburseForm.handle_id.value = "Create";
                            document.LoanDisburseForm.action = '/PacsApplication/LoanDisbursementServlet';
                            document.LoanDisburseForm.submit();
                        }
                    }
                }
            }
            function checkbeforeUpdate(LoanDisburseForm){
                var counter = document.forms[0].rowCounter.value;

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

                    var disburseMonthAmend = document.getElementById("disburseMonthAmend"+i).value;
                    var disburseAmtAmend= document.getElementById("disburseAmtAmend"+i).value;
                    var disburseAmtAmendInt = parseInt(document.getElementById("disburseAmtAmend"+i).value);
                    var descDisburseAmend= document.getElementById("descDisburseAmend"+i).value;
                    var amtRemainAmend= document.getElementById("amtRemainAmend"+i).value;

                    if(disburseAmtAmendInt == 0){
                        alert("Disbursement Amount Can not be zero.");
                        return false;
                    }

                    if(disburseMonthAmend=="" || disburseAmtAmend==""||descDisburseAmend==""||amtRemainAmend==""){

                        alert("Disbursement Fields are mandatory.");
                        return false;
                    }

                    re = /^\d+$/; //this checks whether field contains digits
                    if (!re.test(document.getElementById("disburseMonthAmend"+i).value))
                    {
                        alert("Disbursement Month could only contain numbers.");
                        //  LoanDisburseForm.disburseMonthAmend.focus();
                        return false;
                    }
                    re = /2[01][1-9][0-9](01|02|03|04|05|06|07|08|09|10|11|12)/; //this checks whether field starts with YYYYMM,accepts all months of Year 2010 upto Year 2199
                    if (!re.test(document.getElementById("disburseMonthAmend"+i).value))
                    {
                        alert("Disbursement Month should be in YYYYMM format !! (accepts all months of Year 2010 upto Year 2199)");
                        //  LoanDisburseForm.disburseMonthAmend.focus();
                        return false;
                    }
                    re = /^\d+$/; //this checks whether field contains digits
                    if (!re.test(document.getElementById("disburseAmtAmend"+i).value))
                    {
                        alert("Disbursement Amount could only contain numbers.");
                        //  LoanDisburseForm.disburseAmtAmend.focus();
                        return false;
                    }
                    re = /^\d+$/; //this checks whether field contains digits
                    if (!re.test(document.getElementById("amtRemainAmend"+i).value))
                    {
                        alert("Disbursement Amount Remaining could only contain numbers.");
                        //LoanDisburseForm.amtRemainAmend.focus();
                        return false;
                    }


                }

                return true;
            }
            function checkbeforeCreate(LoanDisburseForm){
                var counter = document.forms[0].rowCounter2.value;

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


                    var disburseMonthAmend = document.getElementById("disburseMonth"+i).value;
                    var disburseAmtAmend= document.getElementById("disburseAmt"+i).value;
                    var disburseAmtAmendInt = parseInt(document.getElementById("disburseAmt"+i).value);
                    var descDisburseAmend= document.getElementById("descDisburse"+i).value;
                    var amtRemainAmend= document.getElementById("amtRemain"+i).value;

                    if(disburseAmtAmendInt == 0){
                        alert("Disbursement Amount Can not be zero");
                        return false;
                    }

                    if(disburseMonthAmend=="" || disburseAmtAmend==""||descDisburseAmend==""||amtRemainAmend==""){

                        alert("Disbursement Fields are mandatory.");
                        return false;
                    }

                    re = /^\d+$/; //this checks whether field contains digits
                    if (!re.test(document.getElementById("disburseMonth"+i).value))
                    {
                        alert("Disbursement Month could only contain numbers.");
                        //  LoanDisburseForm.disburseMonthAmend.focus();
                        return false;
                    }
                    re = /2[01][1-9][0-9](01|02|03|04|05|06|07|08|09|10|11|12)/; //this checks whether field starts with YYYYMM,accepts all months of Year 2010 upto Year 2199
                    if (!re.test(document.getElementById("disburseMonth"+i).value))
                    {
                        alert("Disbursement Month should be in YYYYMM format !! (accepts all months of Year 2010 upto Year 2199)");
                        //  LoanDisburseForm.disburseMonthAmend.focus();
                        return false;
                    }
                    re = /^\d+$/; //this checks whether field contains digits
                    if (!re.test(document.getElementById("disburseAmt"+i).value))
                    {
                        alert("Disbursement Amount could only contain numbers.");
                        //  LoanDisburseForm.disburseAmtAmend.focus();
                        return false;
                    }
                    re = /^\d+$/; //this checks whether field contains digits
                    if (!re.test(document.getElementById("amtRemain"+i).value))
                    {
                        alert("Disbursement Amount Remaining could only contain numbers.");
                        //LoanDisburseForm.amtRemainAmend.focus();
                        return false;
                    }


                }

                return true;
            }
            function checkApprovedAmt(){
                var disbrsTotal = parseInt(document.getElementById('grandTotal2').value);

                var approvedAmt = parseInt(document.getElementById('apprvedAmt').value);

                if (disbrsTotal > approvedAmt){
                    alert("Disbursement Amount must be equal or less than Approved Amount.");
                    return false;
                }
                return true;
            }

            function checkApprovedAmtUpdate(){
                var disbrsTotal = parseInt(document.getElementById('grandTotal').value);

                var approvedAmt = parseInt(document.getElementById('apprvedAmt').value);

                if (disbrsTotal > approvedAmt){
                    alert("Disbursement Amount must be equal or less than Approved Amount.");
                    return false;
                }
                return true;
            }

            $("#disburseAmtAmend1").click(function () {
                alert("in dis");
                document.getElementById("amtRemainAmend1").value =document.getElementById("disburseAmtAmend1").value;
            });

        // to restrict back button
            history.pushState(null, null, location.href);
            window.addEventListener('popstate', function(event) {
                history.pushState(null, null, location.href);
            });
            
        </script>
        <jsp:include page="MenuHead_Loan.jsp" flush="true" />
        <br>

        <%int count = 1;
                    int count2 = 1;
                    int populatedRows = 0;

                    String error_msg = "";
                    Object error = request.getAttribute("message");
                    if (error != null) {
                        error_msg = error.toString();
                    }
        %>

        <%
                    String displayFlag = "";
                    Object display = request.getAttribute("displayFlag");
                    if (display != null) {
                        displayFlag = display.toString();
                    }
        %>

        <%
                    Object oLoanDisbursementHdrBeanObj = request.getAttribute("oLoanDisbursementHdrBeanObj");
                    LoanDisbursementHdrBean oLoanDisbursementHdrBean = new LoanDisbursementHdrBean();
                    if (oLoanDisbursementHdrBeanObj != null) {
                        oLoanDisbursementHdrBean = (LoanDisbursementHdrBean) request.getAttribute("oLoanDisbursementHdrBeanObj");
                    }
        %>

        <%
                    Object alLoanDisbursementDetailBeanObj = request.getAttribute("alLoanDisbursementDetailBean");
                    ArrayList<LoanDisbursementDetailBean> alLoanDisbursementDetailBean = new ArrayList<LoanDisbursementDetailBean>();
                    LoanDisbursementDetailBean oLoanDisbursementDetailBean = new LoanDisbursementDetailBean();
                    if (alLoanDisbursementDetailBeanObj != null) {
                        alLoanDisbursementDetailBean = (ArrayList<LoanDisbursementDetailBean>) request.getAttribute("alLoanDisbursementDetailBean");

                    }
        %>

        <fieldset style="border-color: black; background-image: url('/PacsApplication/img/download.jpg'); border-width: 3px">
            <legend style="padding: 8px; border-radius: 20px; background-color: #006600; color: white">Loan Disbursement Schedule</legend>
            <form name="LoanDisburseForm" id="LoanDisburseForm" action="" method="post">

                <%if (!error_msg.equalsIgnoreCase("")) {%>
                <center><div class="info" id="errorMessage"><center><%= error_msg%></center></div></center>
                <% }%>
                <br>
                <br>
                <% if (displayFlag.equalsIgnoreCase("")) {%>


                <center>
                    <table>
                        <tr><td>Enter Loan Account<font color="red" >*</font>:</td><td></td><td><input type="text" maxlength="25" name="loanAcc" id="loanAcc" style="text-align:right;" onkeypress='return event.charCode >= 48 && event.charCode <= 57' /><input type="button" value="Search" onclick="javascript:popup_productCode(420, 180, document.LoanDisburseForm.loanAcc.value);"/></td></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>

                    </table>
                    <br>
                    <input type="button" value="Submit" id="Submit" onclick="javascript:submitform(LoanDisburseForm)"/>
                    <input type="button" value="Reset" id="Reset" onclick="ShowParent();" />
                </center>

                <%}%>
                <br>

                <%!
                    String blankNull(String s) {
                        return (s == null) ? "" : s;
                    }
                %>
                <%
                            if (displayFlag.equalsIgnoreCase("Y")) {
                %>
                <div id="show" align="left" >

                    <br>
                    <center>
                        <table>
                            <tbody>
                                <tr>
                                    <td style="font:large;">Loan Account Number: </td>
                                    <td><input type="text" readonly="true" name="loanAccountNumber"  id="loanAccountNumber" style="text-align:right;" style="width: 260px" value="<%=blankNull(oLoanDisbursementHdrBean.getLoanAccountNumber())%>" /></td>
                                    <td></td>
                                    <td style="text-align:right;">CIF Number: </td>
                                    <td><input type="text" readonly="true" name="cifNo"  id="cifNo" style="text-align:right;" style="width: 260px" value="<%=blankNull(oLoanDisbursementHdrBean.getCifNo())%>" /></td>
                                </tr>
                                <tr>
                                    <td style="text-align:right;">Customer Name: </td>
                                    <td><input type="text" readonly name="CustomerName"  id="CustomerName" style="width: 260px" value="<%=blankNull(oLoanDisbursementHdrBean.getCustomerName())%>" /></td>
                                    <td></td>

                                    <td>Product Description: </td>
                                    <td><input type="text" readonly name="productDesc" style="width: 260px" id="productDesc" value="<%=blankNull(oLoanDisbursementHdrBean.getProductDesc())%>" /></td>
                                </tr>
                                <tr>
                                    <td style="text-align:right;">Loan Outstanding: </td>
                                    <td><input type="text" readonly name="loanOutStd" style="text-align:right;" style="width: 260px" id="loanOutStd" value="<%=blankNull(oLoanDisbursementHdrBean.getLoanOutStd())%>" /></td>
                                    <td></td>

                                    <td style="text-align:right;">Approved Amount: </td>
                                    <td><input type="text" readonly name="apprvedAmt"  style="text-align:right;" style="width: 260px" id="apprvedAmt" value="<%=blankNull(oLoanDisbursementHdrBean.getApprvedAmt())%>"  /></td>
                                </tr>
                                <tr>
                                    <td style="text-align:right;">Approved Date: </td>
                                    <td><input type="text" readonly name="apprvedDate" style="text-align:center;" style="width: 260px" id="apprvedDate" value="<%=blankNull(oLoanDisbursementHdrBean.getApprvedDate())%>" /></td>
                                    <td></td>

                                    <td style="text-align:right;">Advance Amount: </td>
                                    <td><input type="text" readonly name="advanceAmt" style="text-align:right;" style="width: 260px" id="advanceAmt" value="<%=blankNull(oLoanDisbursementHdrBean.getAdvanceAmt())%>" /></td>
                                </tr>
                                <tr><td colspan="5"><hr></td></tr>
                            </tbody>
                        </table></center>
                    <center>


                        <h3>Disbursement Details</h3>

                        <% if (alLoanDisbursementDetailBean.size() > 0) {%>
                        <table id="order-list" border="2" width="auto" cellspacing="1" cellpadding="1" style="text-align: center ;overflow-y: scroll; font-size: 15px">
                            <thead class="CSSTableGenerator" >
                                <tr>

                                    <td>Disbursement Month(YYYYMM)</td>
                                    <td>Disbursement Amount </td>
                                    <td>Description </td>
                                    <td>Amount Remaining</td>

                                </tr>
                            </thead>
                            <tbody>

                                <%
                                     oLoanDisbursementDetailBean = new LoanDisbursementDetailBean();
                                     count = 0;

                                     for (int i = 0; i < alLoanDisbursementDetailBean.size(); i++) {
                                         oLoanDisbursementDetailBean = alLoanDisbursementDetailBean.get(i);

                                         count++;
                                         populatedRows++;


                                %>

                                <tr>
                                    <td><input  type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' size="30" maxlength="6" class="month" name="disburseMonthAmend<%=count%>" id="disburseMonthAmend<%=count%>" value="<%=blankNull(oLoanDisbursementDetailBean.getDisburseMonth())%>"/></td>
                                    <td><input  type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' class="IDScore" name="disburseAmtAmend<%=count%>" id="disburseAmtAmend<%=count%>" value="<%=blankNull(oLoanDisbursementDetailBean.getDisburseAmt())%>"/></td>
                                    <td><input  type="text"  name="descDisburseAmend<%=count%>" id="descDisburseAmend<%=count%>" value="<%=blankNull(oLoanDisbursementDetailBean.getDescDisburse())%>"/></td>
                                    <td><input type="text"  name="amtRemainAmend<%=count%>" id="amtRemainAmend<%=count%>" value="<%=blankNull(oLoanDisbursementDetailBean.getAmtRemain())%>" readonly/></td>
                                    <td><input type="hidden"  name="loanAcctIdAmend<%=count%>" id="loanAcctIdAmend<%=count%>" value="<%=blankNull(oLoanDisbursementDetailBean.getLoanAcctId())%>"/></td>
                            <td><input type="hidden" name="rowStatus<%=count%>" id="rowStatus<%=count%>" value="A"/></td>
                            <td><input type="hidden" name="disbSchedule_table_id<%=count%>" id="disbSchedule_table_id<%=count%>" value="<%=blankNull(oLoanDisbursementDetailBean.getDisbSchedule_table_id())%>" /></td>
                        </tr>

                            <%}%>
                            </tbody>
                        </table>
                        <center>
                            <table>
                                <tr><td></td></tr>
                                <tr><td></td></tr>
                                <tr><td></td></tr>
                                <tr><td></td></tr>
                                <tr>
                                    <td><B>Total Disbursement Amount</B></td>
                                    <td></td><td></td>
                                    <td><input type="text" size="4" readonly  name="grandTotal" id="grandTotal" /></td>
                                </tr>

                            </table></center>

                        <br><br>
                        <center>
                            <table>
                                <tr>
                                    <td><input type="button" id="addRow" value="Add Row"/></td>

                                    <td> <input type="button" value="Submit" onclick="javascript:UpdateForm(LoanDisburseForm)"></td>
                                    <td><input type="button" value="Reset" onclick="ShowParent();" /></td>
                                </tr>
                            </table>
                        </center>
                        <%} else {%>
                        <table id="order-list2" border="2" width="auto" cellspacing="1" cellpadding="1" style="text-align: center ;overflow-y: scroll; font-size: 15px">
                            <thead class="CSSTableGenerator" >
                                <tr>

                                    <td>Disbursement Month(YYYYMM)</td>
                                    <td>Disbursement Amount </td>
                                    <td>Description </td>
                                    <td>Amount Remaining</td>

                                </tr>
                            </thead>
                            <tbody>
                                <tr> <td><input  type="text" maxlength="6" size="30" class="month" name="disburseMonth<%=count2%>" id="disburseMonth<%=count2%>" /></td>
                                    <td><input  type="text" class="IDScore" name="disburseAmt<%=count2%>" id="disburseAmt<%=count2%>" onchange="calculateRemaining(<%=count2%>);" onkeypress='return event.charCode >= 45 && event.charCode <= 57' /></td>
                                    <td><input  type="text"  name="descDisburse<%=count2%>" id="descDisburse<%=count2%>"  /></td>
                                    <td><input type="text" maxlength="20"  name="amtRemain<%=count2%>" id="amtRemain<%=count2%>" readonly /></td>


                                </tr>
                            </tbody>
                        </table>


                    </center>
                    <center>
                        <table>
                            <tr><td></td></tr>
                            <tr><td></td></tr>
                            <tr><td></td></tr>
                            <tr><td></td></tr>
                            <tr>
                                <td><B>Total Disbursement Amount</B></td>
                                <td></td><td></td>
                                <td><input type="text" size="4" readonly  name="grandTotal2" id="grandTotal2" /></td>
                            </tr>

                        </table></center>

                    <br><br>
                    <center>
                        <table>
                            <tr>
                                <td><input type="button" id="addRow2" value="Add Row"/></td>

                                <td> <input type="button" value="Submit" onclick="javascript:CreateForm2(LoanDisburseForm)"></td>
                                <td><input type="button" value="Reset" onclick="ShowParent();" /></td>
                            </tr>
                        </table>
                    </center>
                    <%}%>
                </div>
                <br>
                <br>


                <label style=" font-family: Berlin Sans FB; font-weight:normal; "> All Fields are mandatory</label>
                <% }%>

                <input type="hidden" name="handle_id" value="" >

                <input type="hidden" name="rowCounter" id="rowCounter" value="<%=count%>">
                <input type="hidden" name="rowCounter2" id="rowCounter2" value="<%=count2%>">

                <input type="hidden" name="all_rowsAdded" id="all_rowsAdded" value="<%=populatedRows%>">
                <input type="hidden" name="FirstPopulatedRows" id="FirstPopulatedRows" value="<%=populatedRows%>">
            </form>
            <script>
                var y = $("#FirstPopulatedRows").val();
                var x = parseInt(y);


                $("#addRow").click(function () {
                    x=x+1;
                    $("#all_rowsAdded").val(x);
                    var counter = $("#rowCounter").val();
                    counter++;
                    $('#order-list tr:last').after('<tr class="tRow">\n\
                <td><input  type="text" maxlength="6" size="30" name="disburseMonthAmend' + x + '" id="disburseMonthAmend' + x + '"/></td>\n\
<td><input  type="text" class="IDScore" name="disburseAmtAmend' + x + '" id="disburseAmtAmend' + x + '" value="" onchange="calculateRemainingAmend(' + x + ');"/></td>\n\
<td><input  type="text" maxlength="15" name="descDisburseAmend' + x + '" id="descDisburseAmend' + x + '" value=""/></td>\n\\n\
<td><input  type="text" maxlength="15" name="amtRemainAmend' + x + '" id="amtRemainAmend' + x + '" value="" readonly /></td>\n\\n\
<input type="hidden" maxlength="20" name="loanAcctIdAmend' + x + '" id="loanAcctIdAmend' + x + '" value=""/>\n\
<input type="hidden" name="rowStatus' + x + '" id="rowStatus' + x + '" value="N"/>\n\
<input type="hidden" name="disbSchedule_table_id' + x + '" id="disbSchedule_table_id' + x + '" value="" />\n\                </tr>');

                    $("#rowCounter").val(counter);
                });
                $("#addRow2").click(function () {


                   // var counter = $("#rowCounter2").val();
                    counter++;
                    $('#order-list2 tr:last').after('<tr class="tRow">\n\
                <td><input  type="text" maxlength="6" size="30" name="disburseMonth' + counter + '" id="disburseMonth' + counter + '"/></td>\n\
<td><input  type="text" class="IDScore" name="disburseAmt' + counter + '" id="disburseAmt' + counter + '" value="" onkeypress="return event.charCode >= 45 && event.charCode <= 57" onchange="calculateRemaining(' + counter + ');" /></td>\n\
<td><input  type="text"  name="descDisburse' + counter + '" id="descDisburse' + counter + '" value=""/></td>\n\\n\
<td><input  type="text" maxlength="15" name="amtRemain' + counter + '" id="amtRemain' + counter + '" value="" readonly/></td>\n\\n\
<input type="hidden" maxlength="20" name="loanAcctId' + counter + '" id="loanAcctId' + counter + '" value=""/>\n\
<input type="hidden" name="rowStatus' + counter + '" id="rowStatus' + counter + '" value="N"/>\n\
<input type="hidden" name="disbSchedule_table_id' + counter + '" id="disbSchedule_table_id' + counter + '" value="" />\n\                </tr>');

                    $("#rowCounter2").val(counter);
                });

                jQuery('#order-list').on('change','.IDScore',
                function(){
                    var counter = document.forms[0].all_rowsAdded.value;
                    if(isNaN(document.getElementById("apprvedAmt").value)){
                        document.getElementById("apprvedAmt").value = 0;
                    }
                    else{
                        var appvedAmount = $('#apprvedAmt').val();
                    }

                    var total = 0;
                    var test;
                    for (var i = 1; i <= counter; i++) {
                        try{
                            var k = document.getElementById("disburseAmtAmend" + i);
                            if(k !== null && k.value === ""){
                                test =0;
                            }
                            else
                                test=parseInt(document.getElementById("disburseAmtAmend" + i).value);
                        }catch(err){
                            test=0;
                        }
                        total = parseInt(total) + test;

                        document.getElementById("amtRemainAmend"+i).value = test;
                        document.getElementById("grandTotal").value = total;
                    }
                    if(isNaN(document.getElementById("grandTotal").value)){
                        document.getElementById("grandTotal").value = 0;
                    }

                });

                jQuery('#order-list2').on('change','.IDScore',
                function(){
                    var counter = document.forms[0].rowCounter2.value;
                    if(isNaN(document.getElementById("apprvedAmt").value)){
                        document.getElementById("apprvedAmt").value = 0;
                    }
                    else{
                        var appvedAmount = $('#apprvedAmt').val();
                    }

                    var total = 0;
                    var test;
                    for (var i = 1; i <= counter; i++) {
                        try{
                            var k = document.getElementById("disburseAmt" + i);
                            if(k !== null && k.value === ""){
                                test =0;
                            }
                            else
                                test=parseInt(document.getElementById("disburseAmt" + i).value);
                        }catch(err){
                            test=0;
                        }
                        total = parseInt(total) + test;

                        document.getElementById("grandTotal2").value = total;
                    }
                    if(isNaN(document.getElementById("grandTotal2").value)){
                        document.getElementById("grandTotal2").value = 0;
                    }

                });




                $( "body" ).click(function() {
                    $( ".IDScore" ).change();
                });


                function ShowParent()
                {
                    document.forms[0].action="${pageContext.request.contextPath}/Loan/LoanDisbursement.jsp";
                    document.forms[0].submit();
                }

                function calculateRemaining(i){

                   document.getElementById("amtRemain" + i).value =document.getElementById("disburseAmt" + i).value;

                }

                function calculateRemainingAmend(i){

                   document.getElementById("amtRemainAmend" + i).value =document.getElementById("disburseAmtAmend" + i).value;

                }

                function checkDuplicateUpdate(){
                    var arr = [];
                    $("#order-list tbody tr").each(function(){
                        arr.push($(this).find("td:first input").val());
                    });
                    for (var i = 0; i < arr.length; i++)
                    {
                        for (var j = 0; j < arr.length; j++)
                        {
                            if (i != j)
                            {
                                if (arr[i] == arr[j])
                                {
                                    alert("Loan Disbursement Month can not pertain duplicate month");
                                    return false;
                                }
                            }
                        }
                    }
                    arr = [];
                    return true;
                }
                function checkDuplicateCreate(){
                    var arr = [];
                    $("#order-list tbody tr").each(function(){
                        arr.push($(this).find("td:first input").val());
                    });

                    for (var i = 0; i < arr.length; i++)
                    {
                        for (var j = 0; j < arr.length; j++)
                        {
                            if (i != j)
                            {
                                if (arr[i] == arr[j])
                                {
                                    alert("Loan Disbursement Month can not contain duplicate month");
                                    return false;
                                }
                            }
                        }
                    }

                    arr = [];
                    return true;
                }


                var counter = document.forms[0].all_rowsAdded.value;
                if(isNaN(document.getElementById("apprvedAmt").value)){
                    document.getElementById("apprvedAmt").value = 0;
                }
                else{
                    var appvedAmount = $('#apprvedAmt').val();
                }

                var total = 0;
                var test;
                for (var i = 1; i <= counter; i++) {
                    try{
                        var k = document.getElementById("disburseAmtAmend" + i);
                        if(k !== null && k.value === ""){
                            test =0;
                        }
                        else
                            test=parseInt(document.getElementById("disburseAmtAmend" + i).value);
                    }catch(err){
                        test=0;
                    }
                    total = parseInt(total) + test;
                    document.getElementById("grandTotal").value = total;
                }
                if(isNaN(document.getElementById("grandTotal").value)){
                    document.getElementById("grandTotal").value = 0;
                }


            </script>
        </fieldset>
    </body>
</html>
