<%--
    Document   : AccountAmmendment
    Created on : May 9, 2017, 3:39:08 PM
    Author     : 981898
--%>

<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.Date"%>
<%@page import="LoginDb.DbHandler"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.UUID" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%String uuid = UUID.randomUUID().toString();%>
<% try {
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Account Amendment</title>
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.ico" type="image/x-icon" />
        <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())%>" />
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/font-awesome.min.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>
        <script type="text/javascript" src="/PacsApplication/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="${pageContext.request.contextPath}/jQuery/jquery-3.3.1.min.js"></script> 
        <style>

            .dialog{
                border:5px solid #666;
                padding:10px;
                background:#FFFFFF;
                position:absolute;
                left: 175px;
                top: 220px;
                z-index:1;
                display:none;
            }
            .dialog label{
                display:inline-block;
                color:#151414;
            }
            div .dialog input[type=text]{
                border:1px solid #333;
                display:inline-block;
                margin:5px;
                width: 180px;
            }

        </style>

    </head>
    <style>

        #recordsTable {
            font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 100%;
        }

        #recordsTable td, #recordsTable th {
            border: 2px solid #000000;
            padding: 8px;
        }

        #recordsTable tr:<%--nth-child(even)--%>{background-color: #f2f2f2;}

        #recordsTable tr:hover {background-color: #a8ecab; cursor:pointer;}

        #recordsTable th {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: left;
            background-color: #4CAF50;
            color: white;
        }
        
        #newRecord tr:hover {background-color: #a8ecab; cursor:pointer;}

    </style>
    <body  onload="noBack()">


        <script type="text/javascript">
            function noBack()
            {
                window.history.forward();
            }
            function ShowParent()
            {
                document.forms[0].action="${pageContext.request.contextPath}/AccountAmendment.jsp";
                document.forms[0].submit();
            }
            function submitform()
            {
                if(validateCif()){
                    $('#action').val('submit');
                    for(var j=11;j>=1;j--){
                        var cif = $('#cif'+j).val();
                        if(cif == "NA"){
                            $('#cif'+j).val('');
                        }
                    }
                    document.AccountAmendment.action = '/PacsApplication/AccAmendServlet';
                    document.AccountAmendment.submit();
                }
            }

            function accSelect(i){

                var accNo = $('#accNo'+i).text();
                var accType = $('#accType'+i).text();
                var prodName = $('#prodName'+i).text();
                var priCIF = $('#priCIF'+i).text();
                var priName = $('#priName'+i).text();
                var accOpenDt = $('#accOpenDt'+i).text();
                var accBal = $('#accBal'+i).text();
                //if(accType == "DEP")
                getAccDtl(accNo,accType);
                $('#chequDetTab').hide(500);
                //$("#recordsTable").remove();
                $("#recordsTable").children('tr').remove();
                $("#accNo").val(accNo);
                $("#accNo").attr('readonly', true);
                $('#priCIF').val(priCIF);
                $('#accType').val(accType);
                $('#prod').val(prodName);
                $('#priName').val(priName);
                $('#openDt').val(accOpenDt);
                $('#availBal').val(accBal);
                $('#accDetails').show(1000);
                $('#submitForm').show();
            }

            function removeCif(i){
                if(i=="1")
                {
                    alert('Primary CIF cannot be removed');
                    return;
                }
                $('#cif'+i).val('NA');
                $('#name'+i).val('NA');
                $("#cifSrch"+i).val('NA');
                $("#cif"+i).attr('readonly', false);
            }            
            function cifSelect(i,b){
                var newCif = $('#newCif'+i).text();
                var newCifName = $('#newCustName'+i).text();
                $('#srchNewCif').hide(500);
                $("#allCif tbody tr").remove();
                $("#cif"+b).val(newCif);
                $("#name"+b).val(newCifName);
                $("#cif"+b).attr('readonly', true);
                $("#cifSrch"+b).val(newCif);
            }
            function validateCif(){
                var priCif = $("#cif1").val();
                var rmk = $("#remarks").val();
                var accType = $('#accType').val();
                var i=1;
                if(priCif == "" || priCif == "NA"){
                    alert("Primary CIF cannot be blank.");
                    $("#cif1").focus();
                    return false;
                }
                for(var j=10;j>=1;j--){
                    if($("#cifSrch"+j).val() != $("#cif"+j).val()){
                        alert("Please search the CIF before proceeding");
                        $("#cif"+j).focus();
                        return false;
                    }
                }
                for(var j=10;j>=1;j--){
                    if($("#cifSrch"+j).val() != "NA"){
                        while(i<=j){
                            var cif = $("#cif"+i).val();
                            var cifSrch = $("#cifSrch"+i).val();
                            if(cif == "" || cif == "NA" || (cif != cifSrch)){
                                alert("CIF Number #"+i+" cannot be blank");
                                $("#cif"+i).focus();
                                return false;
                            }
                            i++;
                        }
                    }
                }
                if(rmk == ""){
                    alert('Remark cannot be blank.');
                    $("#remarks").focus();
                    return false;
                }
                if(accType != "DEP"){
                    alert("Only primary CIF will be updated for Loan A/C.");
                }
                return true;
            }

            function getAccountList()
            {
                var accN =document.getElementById("accNo").value;
                $("#recordsTable").children('tr').remove();
                if( accN == "" )
                {
                    alert("Enter Account Number.");
                    document.getElementById("accNo").focus();
                }
                else    {
                    $.ajax({
                        url:'/PacsApplication/AccAmendServlet',
                        data:{"action":"search" ,"accNo":document.getElementById("accNo").value},
                        type:'post',
                        cache:false,
                    
                            if(data!=null){
                                //response = $.parseJSON(data);
                                $("#recordsTable tbody tr").remove();
                                $(function() {
                                    $.each(data, function(i, item) {
                                        var $tr = $('<tr>').append(
                                        $('<td onclick="javascript:accSelect('+i+')" id="accType'+i+'">').text(item.accType),
                                        $('<td onclick="javascript:accSelect('+i+')" id="accNo'+i+'">').text(item.accNo),
                                        $('<td onclick="javascript:accSelect('+i+')" id="prodName'+i+'">').text(item.prodName),
                                        $('<td onclick="javascript:accSelect('+i+')" id="priCIF'+i+'">').text(item.cif1),
                                        $('<td onclick="javascript:accSelect('+i+')" id="priName'+i+'">').text(item.custName),
                                        $('<td onclick="javascript:accSelect('+i+')" id="accOpenDt'+i+'">').text(item.accOpenDt),
                                        $('<td onclick="javascript:accSelect('+i+')" id="accBal'+i+'">').text(item.availBal)
                                        //$('<td>').html('<input type="radio"/>')
                                        //$('<td>').html('<input type="checkbox" id="chk'+i+'"/>')
                                    ).appendTo('#recordsTable');
                                        //console.log($tr.wrap('<p>').html());
                                    });
                                    $('#chequDetTab').show();
                                });
                            }
                            else
                            {
                                alert("no records");
                            }},
                        error:function(){

                        }
                    }
                );
                }
            };

            function getCifList(a)
            {
                var newCif =document.getElementById("cif"+a).value;
                //alert(a);
                if(newCif == ""){
                    alert('Please enter CIF Number.');
                    return false;
                }
                else    {
                    $.ajax({
                        url:'/PacsApplication/AccAmendServlet',
                        data:{"action":"newCifSearch" ,"newCif":newCif},
                        type:'post',
                        cache:false,
                        
                            if(data!=null){
                                //response = $.parseJSON(data);
                                //$("#newRecord tbody").remove();
                                $('#newRecord').children('tr').remove();
                                $(function() {
                                    $.each(data, function(i, item) {
                                        var $tr = $('<tr>').append(
                                        $('<td onclick="javascript:cifSelect('+i+','+a+')" id="newCif'+i+'">').text(item.cif1),
                                        $('<td onclick="javascript:cifSelect('+i+','+a+')" id="newCustName'+i+'">').text(item.custName),
                                        $('<td onclick="javascript:cifSelect('+i+','+a+')" id="newGuardianName'+i+'">').text(item.guardianName)
                                        
                                    ).appendTo('#newRecord');
                                        
                                    });
                                    $('#srchNewCif').show();
                                });
                            }
                            else
                            {
                                alert("no records");
                            }},
                        error:function(){

                        }
                    }
                );
                }
            }

            function getAccDtl(accNo,accType)
            {
                $.ajax({
                    url:'/PacsApplication/AccAmendServlet',
                    data:{"action":"accDtl" ,"accNo":accNo,"accType":accType},
                    type:'post',
                    cache:false,
                   
                        if(data!=null){
                            //response = $.parseJSON(data);
                            $("#allCif tbody tr").remove();
                            $(function() {
                                $.each(data, function(i, item) {

                                    var $tr = $('<tr>').append(
                                    $('<td>').text("CIF Number #1"),
                                    $('<td> <input type="text" id="cif1" name="cif1" value="'+item.cif1+'" > </td>'),
                                    $('<td> <input type="button" value="Search" onclick="javascript:getCifList(1)" />'),
                                    $('<td> <input type="text" id="name1" value="'+item.custName+'" readonly> <a href="javascript:removeCif(1)">Remove</a></td>'),
                                    $('<td> <input type="hidden" id="cifSrch1" value="'+item.cif1+'"> </td>')

                                ).appendTo('#allCif');

                                    if(accType == "DEP"){
                                        var $tr = $('<tr id="2">').append(
                                        $('<td>').text("CIF Number #2"),
                                        $('<td> <input type="text" id="cif2" name="cif2" value="'+item.cif2+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(2)" />'),
                                        $('<td> <input type="text" id="name2" value="'+item.name2+'" readonly> <a href="javascript:removeCif(2)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch2" value="'+item.cif2+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="3">').append(
                                        $('<td>').text("CIF Number #3"),
                                        $('<td> <input type="text" id="cif3" name="cif3" value="'+item.cif3+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(3)" />'),
                                        $('<td> <input type="text" id="name3" value="'+item.name3+'" readonly> <a href="javascript:removeCif(3)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch3" value="'+item.cif3+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="4">').append(
                                        $('<td>').text("CIF Number #4"),
                                        $('<td> <input type="text" id="cif4" name="cif4" value="'+item.cif4+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(4)" />'),
                                        $('<td> <input type="text" id="name4" value="'+item.name4+'" readonly> <a href="javascript:removeCif(4)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch4" value="'+item.cif4+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="5">').append(
                                        $('<td>').text("CIF Number #5"),
                                        $('<td> <input type="text" id="cif5" name="cif5" value="'+item.cif5+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(5)" />'),
                                        $('<td> <input type="text" id="name5" value="'+item.name5+'" readonly> <a href="javascript:removeCif(5)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch5" value="'+item.cif5+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="6">').append(
                                        $('<td>').text("CIF Number #6"),
                                        $('<td> <input type="text" id="cif6" name="cif6" value="'+item.cif6+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(6)" />'),
                                        $('<td> <input type="text" id="name6" value="'+item.name6+'" readonly> <a href="javascript:removeCif(6)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch6" value="'+item.cif6+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="7">').append(
                                        $('<td>').text("CIF Number #7"),
                                        $('<td> <input type="text" id="cif7" name="cif7" value="'+item.cif7+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(7)" />'),
                                        $('<td> <input type="text" id="name7" value="'+item.name7+'" readonly> <a href="javascript:removeCif(7)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch7" value="'+item.cif7+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="8">').append(
                                        $('<td>').text("CIF Number #8"),
                                        $('<td> <input type="text" id="cif8" name="cif8" value="'+item.cif8+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(8)" />'),
                                        $('<td> <input type="text" id="name8" value="'+item.name8+'" readonly> <a href="javascript:removeCif(8)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch8" value="'+item.cif8+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="9">').append(
                                        $('<td>').text("CIF Number #9"),
                                        $('<td> <input type="text" id="cif9" name="cif9" value="'+item.cif9+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(9)" />'),
                                        $('<td> <input type="text" id="name9" value="'+item.name9+'" readonly> <a href="javascript:removeCif(9)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch9" value="'+item.cif9+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="10">').append(
                                        $('<td>').text("CIF Number #10"),
                                        $('<td> <input type="text" id="cif10" name="cif10" value="'+item.cif10+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(10)" />'),
                                        $('<td> <input type="text" id="name10" value="'+item.name10+'" readonly> <a href="javascript:removeCif(10)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch10" value="'+item.cif10+'"> </td>')

                                    ).appendTo('#allCif');

                                        var $tr = $('<tr id="11">').append(
                                        $('<td>').text("CIF Number #11"),
                                        $('<td> <input type="text" id="cif11" name="cif11" value="'+item.cif11+'"> </td>'),
                                        $('<td> <input type="button" value="Search" onclick="javascript:getCifList(11)" />'),
                                        $('<td> <input type="text" id="name11" value="'+item.name11+'" readonly> <a href="javascript:removeCif(11)">Remove</a></td>'),
                                        $('<td> <input type="hidden" id="cifSrch10" value="'+item.cif11+'"> </td>')

                                    ).appendTo('#allCif');
                                    }
                                    
                                });
                                $('#allCif').show(500);
                                $('#remarkDiv').show();
                            });
                        }
                        else
                        {
                            alert("no records");
                        }},
                    error:function(){

                    }
                }
            );                
            }
            
            // 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="/Deposit/MenuHead_Deposit.jsp" flush="true" />
        <br>
        <%   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();
             }
        %>

        <div class="container">

            <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">Account Amendment</legend>
                <form name="AccountAmendment" id="AccountAmendment" action="" method="post">

                    <%if (!error_msg.equalsIgnoreCase("")) {%>
                    <center><div class="info" id="errorMessage"><center><%= error_msg%></center></div></center>
                    <% }%>
                    <br>
                    <br>
                    <center>
                        <label title="accNo">Enter Account Number<font color="red" >*</font>:</label>
                        <input type="text" name="accNo" id="accNo" placeholder="Enter account number" style="text-align:right;" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
                        <input type="button" value="Get Details"  onclick="javascript:getAccountList();"/>
                        <br><br>

                        <div align="center" style="display:none" id="chequDetTab">

                            <table border="2" width="auto" cellspacing="1" cellpadding="1" id ="recordsTable" style="text-align: center ;overflow-y: scroll; font-size: 15px">
                                <thead class="CSSTableGenerator">
                                    <tr>
                                        <td>Account Type</td>
                                        <td>Account Number</td>
                                        <td>Product</td>
                                        <td>Primary CIF</td>
                                        <td>Primary Customer Name</td>
                                        <td>Account Open Date</td>
                                        <td>Available Balance</td>
                                    </tr>
                                </thead>
                            </table>
                        </div>

                        <div align="center" style="display:none" id="accDetails">

                            <table border="2" width="auto" cellspacing="1" cellpadding="1" id ="accDtl" style="text-align: center ;overflow-y: scroll; font-size: 15px">
                                <tr>
                                    <td>Primary CIF</td>
                                    <td><input type="text" id="priCIF" name="priCIF" value="" readonly/></td>
                                    <td>Name</td>
                                    <td><input type="text" id="priName" name="priName" value="" readonly/></td>
                                </tr>
                                <tr>
                                    <td>Product</td>
                                    <td><input type="text" id="prod" name="prod" value="" readonly/></td>
                                    <td>Account Type</td>
                                    <td><input type="text" id="accType" name="accType" value="" readonly/></td>
                                </tr>
                                <tr>
                                    <td>A/C Open Date</td>
                                    <td><input type="text" id="openDt" name="openDt" value="" readonly/></td>
                                    <td>Available Bal</td>
                                    <td><input type="text" id="availBal" name="availBal" value="" readonly/></td>
                                </tr>
                            </table>

                        </div>
                        <br><br>
                        <div align="center" style="display:none;" id="allCif">

                        </div>
                        <div align="center" style="display:none;" id="remarkDiv">
                            <p>Reason for CIF change:<input type="text" id="remarks" name="remarks" /></p>
                        </div>
                        <br>
                        <div align="center" style="display:none;overflow:scroll; width: 500px; height: 250px;" id="srchNewCif">

                            <table border="2" width="auto" cellspacing="1" cellpadding="1" id ="newRecord" style="text-align: center ;overflow-y: scroll; font-size: 15px">
                                <thead class="CSSTableGenerator">
                                    <tr>
                                        <td>CIF Number</td>
                                        <td>Customer Name</td>
                                        <td>Guardian Name</td>
                                    </tr>
                                </thead>
                            </table>
                        </div>
                        <input type="hidden" id="action" name="action" value=""/>
                        <input type="button" value="Update" id="submitForm" style="display:none;" onclick="javascript:submitform();" />
                    </center>
                </form>
            </fieldset>
        </div>
    </body>
</html>
<% 	} catch (Exception e) {
             
            }
%>
