<%-- 
    Document   : popupnscdetails
    Created on : Nov 8, 2018, 4:45:36 PM
    Author     : 1004242
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Fill-up NSC Details</title>
        <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon" />
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/ButtonAndText.css" />
        <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>
        <script src="${pageContext.request.contextPath}/js/Calendar.js"></script>

    </head>
    <body style="background-image: url('/PacsApplication/img/download.jpg');">
        <%
                    int count = 1;
        %>
        <form name="nscDetails" action="">
            <center>
                <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></td>
                            <td>REF No</td>
                            <td>Amount</td>
                            <td colspan="2">Open Date</td>
                            <td colspan="2">Expiry Date</td>

                        </tr>
                    </thead>
                    <tbody>
                        <tr class="tRow">
                            <td><input type="checkbox" name="chckbox<%=count%>" id="chckbox<%=count%>" onclick="checkboxTrue(<%=count%>);" /></td>
                            <td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" name="ref<%=count%>" id="ref<%=count%>" maxlength="15"/></td>
                            <td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" name="amt<%=count%>" id="amt<%=count%>" maxlength="17" onkeypress='return event.charCode >= 46 && event.charCode <= 57' /></td>
                            <td><input  type="text" style="text-align:center;" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" name="openDt<%=count%>" id="openDt<%=count%>" maxlength="12"/></td>
                            <td><a href="#" onclick="return getCalendar(document.forms[0].openDt<%=count%>);"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" style="display:block;"  height="100%" width="20" /></a></td>
                            <td><input  type="text" style="text-align:center;" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" name="expDt<%=count%>" id="expDt<%=count%>" maxlength="12"/></td>
                            <td><a href="#" onclick="return getCalendar(document.forms[0].expDt<%=count%>);"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" style="display:block;"  height="100%" width="20" /></a></td>

                        </tr>
                    </tbody>
                </table>
                <table>
                    <tr>
                        <td><input type="button" id="addRow" value="Add Row"/></td>
                        <td><input type="button" id="deleteRow" value="Delete Row"/></td>
                        <td><input type="button" value="Submit" onclick="javascript:submitform()"></td>
                        
                    </tr>
                </table>
            </center>
            <input type="hidden" name="rowCounterDeleteCreate" id="rowCounterDeleteCreate" value="<%=count%>">
            <input type="hidden" name="rowCounter" id="rowCounter" value="<%=count%>">
            <input type="hidden" name="chkboxCounter" id="chkboxCounter" value="0"/>
            <input type="hidden" name="all_rowsAdded" id="all_rowsAdded" value="1">
        </form>
        <script type="text/javascript">
            var x=1;
            function checkboxTrue(i){

                if(document.getElementById("chckbox"+i).checked==true){

                    var counter = $("#chkboxCounter").val();
                    counter++;
                    $("#chkboxCounter").val(counter);

                }

                else{

                    var counter = $("#chkboxCounter").val();
                    counter--;
                    $("#chkboxCounter").val(counter);
                }

            }

            $("#addRow").click(function () {
                x=x+1;
                $("#all_rowsAdded").val(x);
                var counter = $("#rowCounter").val();
                counter++;
                $('#order-list tr:last').after('<tr class="tRow"><td><input type="checkbox" name="chckbox' + x + '" id="chckbox' + x + '" onclick="checkboxTrue(' + x + ');"/></td>  \n\
                <td><input  type="text" maxlength="17" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;"  name="ref' + x + '" id="ref' + x + '"/></td>\n\
                <td><input  type="text" maxlength="17" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;"  name="amt' + x + '" id="amt' + x + '"/></td>\n\
                <td><input  type="text" maxlength="12" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;"  name="openDt' + x + '" id="openDt' + x + '"/></td>\n\
                <td><a href="#" onclick="return getCalendar(document.forms[0].openDt' + x + ');"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" style="display:block;"  height="100%" width="20" /></a></td>\n\
                <td><input  type="text" maxlength="12" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;"  name="expDt' + x + '" id="expDt' + x + '"/></td>\n\
                <td><a href="#" onclick="return getCalendar(document.forms[0].expDt' + x + ');"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" style="display:block;"  height="100%" width="20" /></a></td>\n\
                </tr>');

                $("#rowCounter").val(counter);
                counter = $("#rowCounterDeleteCreate").val();
                counter++;
                $("#rowCounterDeleteCreate").val(counter);


            });


            $( "#deleteRow" ).click(function() {

                var len = ($("#order-list tr").length)-1;
                var counter = $("#rowCounterDeleteCreate").val();

                var chkboxCounter=$("#chkboxCounter").val();

                var counterDelete=counter-chkboxCounter;

                if (counterDelete==0){

                    alert("All rows cannot be deleted");
                }
                else{

                    if (len ==chkboxCounter) {
                        alert("All rows can not be deleted");
                        return false;
                    }

                    $('input:checkbox:checked').closest('tr').remove();
                    $("#rowCounterDeleteCreate").val(counterDelete);

                    chkboxCounter=0;
                    $("#chkboxCounter").val(chkboxCounter);
                }

            });

            function submitform(){
                if(validateNscDetails()){
                    
                    var totRow = document.getElementById("rowCounter").value;
                    var x=0;
                    var totsec=0;

                    var mindate = '01/01/2099';
                    var init = mindate.split("/")
                    var initDate = new Date(init[2], init[1] - 1, init[0])
                    //alert(totRow);
                    for(x=1;x<=totRow;x++){
                        //alert(window.opener.document.getElementById('productCodeDetails').value);
                        //alert(x);
                        var iFrame = window.opener.document.getElementById('order-list');
                        var newDivInParent = window.opener.document.createElement('tr');
                        newDivInParent.setAttribute("Id", "nscDetails"+x);
                        iFrame.appendChild(newDivInParent);
                        var ref = document.getElementById("ref"+x).value;
                        var amt = document.getElementById("amt"+x).value;
                        totsec=Number(totsec)+Number(amt);
                        var openDt = document.getElementById("openDt"+x).value;
                        var expDt = document.getElementById("expDt"+x).value;

                        //alert(expDt + ',' + mindate);
                        var from = expDt.split("/")
                        var f = new Date(from[2], from[1] - 1, from[0])



                       <%-- mindate = new Date(mindate);
                        expDt = new Date(expDt);

                        if(expDt < mindate)
                            {mindate = expDt;
                                alert(mindate);
                            }--%>
                           if( f.getTime() < initDate.getTime())
                               {
                                    initDate = f;
                                    mindate = expDt;
                               }

                        opener.document.getElementById('nscDetails'+x).innerHTML = '<tr class="tRow">\<td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" readonly name="ref' + x + '" id="ref' + x + '" value="' + ref + '"/></td>\n\<td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" readonly name="amt' + x + '" id="amt' + x + '" value="' + amt + '"/></td>\n\<td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" readonly name="openDt' + x + '" id="openDt' + x + '" value="' + openDt + '"/></td>\n\<td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" readonly name="expDt' + x + '" id="expDt' + x + '" value="' + expDt + '"/></td>\n\</tr>';

                        /*opener.document.$('#order-list tr:last').after('<tr class="tRow">\<td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" readonly name="ref' + x + '" id="ref' + x + '"/></td>\n\
                        <td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" readonly name="amt' + x + '" id="amt' + x + '"/></td>\n\
                        <td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" readonly name="openDt' + x + '" id="openDt' + x + '"/></td>\n\
                        <td><input  type="text" style="width: 100%;padding: 5px;margin: 0px;border: 1px solid #CCC;" readonly name="expDt' + x + '" id="expDt' + x + '"/></td>\n\
                        </tr>');*/
                    }
                    window.opener.document.getElementById('currentValuation').value=totsec;
                    window.opener.document.getElementById('rowCounter').value=totRow;
                    window.opener.document.getElementById('mat_dt').value=mindate;
                    window.opener.$('#nsc').show();
                    self.close();

                }
            }

            function validateNscDetails(){
                var totRow = document.getElementById("rowCounter").value;
                var x=0;
                for(x=1;x<=totRow;x++){
                    if(document.getElementById("ref"+x).value == ""){
                        alert("Reference number cannot be empty");
                        document.getElementById("ref"+x).focus();
                        return false;
                    }
                    if(document.getElementById("amt"+x).value == ""){
                        alert("Amount cannot be empty");
                        document.getElementById("amt"+x).focus();
                        return false;
                    }
                    if(document.getElementById("openDt"+x).value == ""){
                        alert("Open date cannot be empty");
                        document.getElementById("openDt"+x).focus();
                        return false;
                    }
                    if(document.getElementById("expDt"+x).value == ""){
                        alert("Expiry date cannot be empty");
                        document.getElementById("expDt"+x).focus();
                        return false;
                    }
                    re = /^\d+$/;
                    if (!re.test(document.getElementById("amt"+x).value))
                    {
                        alert("Amount could only contain numbers !!");
                        document.getElementById("amt"+x).focus();
                        return false;
                    }
                
                }
                return true;
            }
        </script>
    </body>
</html>
