    <%--
    Document   : SellItem
    Created on : May 5, 2016, 12:31:09 PM
    Author     : 590685
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="DataEntryBean.pdsStockRegisterBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="LoginDb.DbHandler"%>
<% try {
%>
<html>

    <head>
        <title>Sell Item</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="${pageContext.request.contextPath}/js/Calendar.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/TableCSSCode.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.ico?pacs=<%=((new java.util.Date()).getTime())%>" type="image/x-icon" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <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>

            function noBack()
            {
                window.history.forward();
            }
            function submitform(tradingsaleitem)
            {
                if (checkDiv(tradingsaleitem))
                {

                    document.forms[0].handle_id.value = "Create";
                    document.forms[0].action = '/PacsApplication/TradingSaleItemEntry';
                    document.forms[0].submit();
                }
            }

            function checkDiv(tradingsaleitem)
            {
                if(document.forms[0].memberId.value=="" )
                {
                    alert("Please enter Customer ID.");
                    document.forms[0].memberId.focus();
                    return false;
                }
                if(document.forms[0].memberName.value=="" )
                {
                    alert("Please enter Customer Name.");
                    document.forms[0].memberName.focus();
                    return false;
                }
                if( document.forms[0].checkOption.value=="" )
                {
                    alert("Please select Mode of Payment.");
                    document.forms[0].checkOption.focus();
                    return false;
                }

                var counter = document.forms[0].rowCounter.value;

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

                        var productDetails = document.getElementById("product_id" + i).value;
                        var priceDetails = document.getElementById("price" + i).value;
                        var quantityDetails = document.getElementById("quantity" + i).value;
                        var quantityAvailable = document.getElementById("quantity_avl" + i).value;
                        var total = document.getElementById("total" + i).value;
                        var restQuantity = quantityAvailable - quantityDetails;

                        if(productDetails==""){
                            alert("Product Details Is Not Available.");
                            return false;
                        }
                        if(restQuantity<0){
                            alert("Stock Not Available for selling quantity.");
                            return false;
                        }
                        if (productDetails == "" || priceDetails == "" || quantityDetails == "" || total == "") {

                            alert("All Fields are mandatory.");
                            return false;
                        }
                    }
                    catch(e){}
                }
                return true;
            }
            function checkop()

            {
                if (document.getElementById("checkOption").value == "1")
                {
                    document.getElementById("payment").style.display = 'none';
                }
                else  if (document.getElementById("checkOption").value == "2")
                {
                    document.getElementById("payment").style.display = 'table-row';
                }
            }
            
            function Total(i) {

                var priceDetails = document.getElementById("price" + i).value;
                var quantityDetails = document.getElementById("quantity" + i).value;

                document.getElementById("total" + i).value = priceDetails * quantityDetails;
            }

           function populateStockDetails(width, height,indicator,memberStatus)
            {
                 alert("change occured"+indicator);
                 if (memberStatus==""){

                        alert("Please Select the member details first");
                        return false;
                    }

                    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 = "SellItem.jsp";
                    var lovKey = "TrandingProductSearchBarcode"
                   // popWin = open('/PacsApplication/CommonSearchInformationTradingLOV.jsp?screenName=' + screenName + '&memberStatus='+memberStatus+ '&lovKey=' + lovKey + '&textIndex=' + indicator+ '&invId=' + document.getElementById("stockId"+indicator).value, 'TrandingProductSearchBarcode', attr);
                    popWin.focus();
            }
            
            // to restrict back button
            history.pushState(null, null, location.href);
            window.addEventListener('popstate', function(event) {
                history.pushState(null, null, location.href);
            });

        </script>
    </head>
    <body>

        <%          int count = 1;
             String error_msg = "";
             Object error = request.getAttribute("message");
             if (error != null) {
                 error_msg = error.toString();
             }
        %>
        <%!
            String blankNull(String s) {
                return (s == null) ? "" : s;
            }
        %>

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

        <jsp:include page="MenuHead_TRADING.jsp" flush="true" />
        <br>
        <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">
                Sell Item
            </legend>

            <form id="tradingsaleitem" name="tradingsaleitem" method="post" action="">

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

                <div id="member" >
                    <center>
                        <table>
                            <tbody>

                                <tr>
                                    <td style="text-align:right">Customer ID<font color="red" >*</font>:</td>
                                    <td> <input type="text" readonly id="memberId" name="memberId" style="text-align:right;" ></td>
                                    <td> <input type="button" id="searchMemberId" name="searchMemberId" value="Search" onclick="javascript:popup_CIF(220, 180, document.tradingsaleitem.memberId.value, document.tradingsaleitem.memberName.value)" ></td>
                                </tr><br>
                            <tr>
                                <td>Customer Name<font color="red" >*</font>:</td>
                                <td> <input type="text" id="memberName" name="memberName"></td>
                                <td> <input type="button" id="searchMemberName" name="searchMemberName" value="Search" onclick="javascript:popup_CIF(220, 180, document.tradingsaleitem.memberId.value, document.tradingsaleitem.memberName.value);"></td>
                                <td style="display:none"><input type="hidden" id="memberStatus" name="memberStatus" /></td>
                            </tr>

                            <tr>
                                <td style="text-align:right;">Mode of Payment: </td>

                                <td>
                                    <select id="checkOption" onchange="checkop()" name="checkOption" >
                                        <option value=""> --Select-- </option>
                                        <option value="1" >Cash</option>
                                        <option value="2" >Credit</option>

                                    </select>
                                </td>
                            </tr>
                            <tr id="payment" style="display: none;">
                                <td>Payment Due Date: </td>
                                <td><input type="text" name="dueDate" id="dueDate" style="text-align:center;" value="">&nbsp;<a href="#" onclick="return getCalendar(document.forms[0].dueDate);"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" border="0" width="25" height="20" /></a></td>

                            </tr>
                            <tr >
                                <td style="text-align:right">Grand Total: </td>
                                <td><input type="text" readonly name="grandTotal" id="grandTotal" style="text-align:right;" value="0"></td>
                            </tr>

                            </tbody>
                        </table>
                    </center>
                </div>
                <br>
                <br>

                <tr id="productTable" style="display:none;">
                <center>
                    <table id="order-list" class="numbersOnly" 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>Stock ID / Inventory</td>
                                <td colspan="2">Commodity Type</td>
                                <td>Type Description</td>
                                <td>Commodity Sub-Type</td>
                                <td>Sub-Type Description</td>
                                <td>Quantity</td>
                                <td>Price/Unit</td>
                                <td>Total</td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td><input type="checkbox" name="chckbox<%=count%>" id="chckbox<%=count%>" onclick="return checkboxTrue(<%=count%>)"/></td>
                                <td><input type="text" name="stockId<%=count%>" id="stockId<%=count%>" oninput="populateStockDetails(520, 180,<%=count%>,document.forms[0].memberStatus.value)"/></td>
                                <td><input type="text" name="commoditytype<%=count%>" id="commoditytype<%=count%>"/></td>
                                <td><input type="button" name="SearchProduct<%=count%>" id="SearchProduct<%=count%>" value="Search"  onclick="SearchProduct(520, 180,<%=count%>,document.forms[0].memberStatus.value)" /></td>
                                <td><input type="text" readonly name="typedesc<%=count%>" id="typedesc<%=count%>"/></td>
                                <td><input type="text" readonly name="commoditysubtype<%=count%>" id="commoditysubtype<%=count%>"/></td>
                                <td><input type="text" readonly name="subtypedesc<%=count%>" id="subtypedesc<%=count%>" onchange="Total(<%=count%>);"/></td>
                                <td><input type="text" size="8" name="quantity<%=count%>" id="quantity<%=count%>" class="numbersOn" onchange="Total(<%=count%>);calculateGrandTotal();" onkeyup="Total(<%=count%>);calculateGrandTotal();"/></td>
                                <td><input type="text" size="8" name="price<%=count%>" id="price<%=count%>" onchange="Total(<%=count%>);calculateGrandTotal();" onkeyup="Total(<%=count%>);calculateGrandTotal();"/></td>
                                <td><input type="text" readonly="readonly" name="total<%=count%>" id="total<%=count%>" /></td>
                                <td style="display: none"><input type="hidden" name="product_id<%=count%>" id="product_id<%=count%>"/></td>
                                <td style="display: none"><input type="hidden" name="quantity_avl<%=count%>" id="quantity_avl<%=count%>"/></td>
                                <td style="display: none"><input type="hidden" name="p_order_id<%=count%>" id="p_order_id<%=count%>"/></td>
                            </tr>
                        </tbody>
                    </table>
                </center>

                <br/><br/>
                <center>
                    <input type="button" id="addRow" value="Add Row"/>
                    <input type="button" id="deleteRow" value="Delete Row"/>
                    <input type="button" value="Submit" onclick="javascript:submitform(tradingsaleitem)"/>
                    <input type="button" value="Reset" onclick="ShowParent();" />

                </center>
                <input type="hidden" name="rowCounter" id="rowCounter" value="<%=count%>">
                <input type="hidden" name="chkboxCounter" id="chkboxCounter" value="0">
                <input type="hidden" name="rowCounterDelete" id="rowCounterDelete" value="<%=count%>">
                </tr>

                <br>

                <input type="hidden" name="handle_id" value="" />
            </form>
            <script>

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

                    //var counter = $("#rowCounter").val();
                    ++counter;
                    $('#order-list tr:last').after('<tr>\n\
                        <td><input type="checkbox" onclick="return checkboxTrue('+counter+')" name="chckbox' + counter + '" id="chckbox' + counter + '" /></td>\n\
                        <td><input type="text" name="stockId'+counter+'" id="stockId'+counter+'" oninput="populateStockDetails(520, 180,'+counter+',document.forms[0].memberStatus.value)"/></td>\n\
                        <td><input type="text" id="commoditytype' + counter + '" name="commoditytype' + counter + '"/></td>\n\
                        <td><input type="button" name="SearchProduct' + counter + '" id="SearchProduct' + counter + '" value="Search"  onclick="SearchProduct(220,180,' + counter + ',document.forms[0].memberStatus.value);" /></td>\n\
                        <td><input type="text" name="typedesc' + counter + '" id="typedesc' + counter + '" /></td> \n\
                        <td><input type="text" id="commoditysubtype' + counter + '" name="commoditysubtype' + counter + '"/></td>\n\
                        <td><input type="text" id="subtypedesc' + counter + '" name="subtypedesc' + counter + '" /></td>\n\
                        <td><input type="text" size="8" class="numbersOn" id="quantity' + counter + '" name="quantity' + counter + '" onchange="Total(' + counter + ');calculateGrandTotal();" /></td>\n\
                        <td><input type="text" size="8" id="price' + counter + '" name="price' + counter + '" onchange="Total(' + counter + ');calculateGrandTotal();"  /></td>\n\
                        <td><input type="text" readonly="readonly" name="total' + counter + '" id="total' + counter + '"/></td>\n\
                        <td style="display: none"><input type="hidden" name="product_id' + counter + '" id="product_id' + counter + '"/></td>\n\
                        <td style="display: none"><input type="hidden" name="quantity_avl' + counter + '" id="quantity_avl' + counter + '"/></td>\n\
                        <td style="display: none"><input type="hidden" name="p_order_id' + counter + '" id="p_order_id' + counter + '"/></td>\n\
                        </tr>');
                    $("#rowCounter").val(counter);
                    $("#rowCounterDelete").val(counter);

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


                    var len = ($("#order-list tr").length)-1;
                    var counter = $("#rowCounterDelete").val();
                    var chkboxCounter=$("#chkboxCounter").val();
                    //alert(chkboxCounter);
                    counter=counter-chkboxCounter;
                    //alert(counter);
                    if (counter==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();
                        $("#rowCounterDelete").val(counter);
                        chkboxCounter=0;
                        $("#chkboxCounter").val(chkboxCounter);

                    }
                    calculateGrandTotal();
                });

                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);
                    }
                }

                function SearchProduct(width, height, i, memberStatus)
                {
                    if (memberStatus==""){
                        alert("Please Select the member details first");
                        return false;
                    }
                    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 commoditytype = document.getElementById("commoditytype"+i).value;    
                    var screenName = "SellItem.jsp";
                    var lovKey = "TrandingProductSearchForSell"
                    popWin = open('/PacsApplication/CommonSearchInformationTradingLOV.jsp?screenName=' + screenName + '&commoditytype='+commoditytype+ '&memberStatus='+memberStatus+ '&lovKey=' + lovKey + '&textIndex=' + i, 'TrandingProductSearch', attr);
                    popWin.focus();

                }

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

                function popup_CIF(width, height,memberId,name) {
                    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 = "SellItem.jsp";
                    var lovKey = "memberDetails"
                    popWin = open('/PacsApplication/CommonSearchInformationTradingLOV.jsp?&memberName=' + name + '&memberId='+memberId+'&screenName=' + screenName + '&lovKey=' + lovKey, 'memberDetails', attr);
                    popWin.focus();

                }

                function calculateGrandTotal()
                {
                    var counter = $("#rowCounter").val();

                    var grandTotal=0;

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

                        try{
                            var total = document.getElementById("total"+i).value;

                            grandTotal=Number(grandTotal)+Number(total);
                        }
                        catch(e){

                        }
                    }

                    document.forms[0].grandTotal.value=Number(grandTotal).toFixed(2);
                }

            </script>
            <script>
                $('table.numbersOnly').on('keydown','.numbersOn', function(event) {
                    if (event.shiftKey == true) {
                        event.preventDefault();
                    }
                    if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 8  || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 190 || event.keyCode == 110) {

                    } else {
                        event.preventDefault();
                    }
                    if($(this).val().indexOf('.') !== -1 && (event.keyCode == 190 || event.keyCode == 110))
                    { event.preventDefault();}
                });

            </script>
        </fieldset>
    </body>
</html>
<% 	} catch (Exception e) {
                
            }
%>
