<!---- 
Author 	: Herpan / 12-08-2014	
Revisi 	: Ferry / 12-01-2014
-->

<div class="easyui-layout" data-options="fit:true,border:false">
    <form id="fm_sales_order" name="fm_sales_order" method="post">
        <input type="hidden" name="detail" id="detail" />
    
        <!-- Top view-->
        <div id="form-header" data-options="region:'north',border:false" >
            <div id="form-header-wrapper">
                <table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
                    <tr>
                        <td align="left">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="location.reload()">New</a>
                        </td>
                        <td height="25" align="right" valign="middle">
                            <span class="form-title" style="margin-left:7px;">
                                <?=$menu_label?>
                            </span>
                        </td>
                    </tr>
                </table>		     	
            </div>			
        </div>
        <!-- End of top view -->
        
        <!-- center view -->
        <div id="form-body" data-options="region:'center',border:false">
            <div id="form-wrapper" class="easyui-panel" style="width:1180%;height:526px;padding:5px;">	
                <!-- Form Header -->
                <table width="100%" height="141" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr height="20">
                                    <td width="120"><b>Customer Name</b></td>
                                    <td width="10"><b>:</b></td>
                                    
                                    <td colspan="5">
                                        <select class="easyui-combogrid" id="cmb_customer" required="required"
                                            name="cmb_customer" 
                                            style="width:185px; height:20px;" 
                                            data-options="
                                                panelWidth: 300,
                                                idField: 'CustomerCode',
                                                textField: 'CustomerName',
                                                url: '<?=base_url()?>custom/get_data/customer',
                                                method: 'get',
                                                columns: [[
                                                    {field:'CustomerCode',title:'Customer Code',width:110},
                                                    {field:'CustomerName',title:'Customer Name',width:190}
                                                ]],
                                                fitColumns: true
                                            ">
                                        </select>

                                        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-sales'" onClick="select_sales();">Select SQ</a>
                                        <input type="checkbox" name="chk_taxtable" id="chk_taxtable" onClick="hideppn();" value="1">Taxable								  

                                        <span class="disableme" style="display:none;">
                                            <input type="checkbox" name="chk_inclusive" id="chk_inclusive" onClick="hideppn();" value="1">Inclusive Tax
                                        </span>
                                    </td>
                                </tr>
                        
                                <tr height="20">
                                    <td><b>Billing Address</b></td>
                                    <td width="10"><b>:</b></td>
                                    <td>&nbsp;</td>
                                    <td width="120"><b>Shipping Address</b></td>
                                    <td width="10"><b>:</b></td>
                                    <td>&nbsp;</td>
                                    <td width="114" rowspan="2">&nbsp;</td>
                                </tr>
                        
                                <tr height="20">
                                    <td colspan="3">
                                        <textarea name="txa_address" cols="30" rows="4" id="txa_address" style="resize:none;white-space:normal;"></textarea>
                                    </td>
                                    <td colspan="3">
                                        <textarea name="txa_ship_to" cols="30" rows="4" id="txa_ship_to" style="resize:none;white-space:normal;"></textarea>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                                <tr height="20">
                                    <td rowspan="7" valign="top"><input type="checkbox" id="chk_payment_dp" name="chk_payment_dp" onclick="hidedp()" value="1" /> Cust. Payment DP</td>
                                    <td width="190" style="background:#333399;text-align:center;color:#FFF;">SO. No.</td>
                                    <td width="122" style="background:#333399;text-align:center;color:#FFF;">SO. Date</td>
                                    <td width="122" style="background:#333399;text-align:center;color:#FFF;">FOB</td>
                                </tr>
                            
                                <tr height="20">
                                    <td><input type="text" name="txt_so_no" id="txt_so_no" class="textbox" value="<?=$so_number?>" style="width:190px;height:18px;text-align:center;" readonly /></td>
                                    <td><input name="txt_so_date" id="txt_so_date" type="text" class="easyui-datebox" style="width:122px;height:20px;" value='<?=$so_date?>' required="required" editable="false" /></td>

                                    <td>
                                        <select name="cmb_fob" id="cmb_fob" class="easyui-combobox" style="width:122px;height:20px;" editable="false">
                                            <option value="0">Shiping Point</option>
                                            <option value="1">Destination</option>    
                                        </select>
                                    </td>
                                </tr>
                        
                                <tr height="20">
                                    <td style="background:#333399;text-align:center;color:#FFF;">SQ. No.</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">SQ. Date</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">Terms</td>
                                </tr>

                                <tr height="20">
                                    <td><input name="txt_sq_no" id="txt_sq_no" type="text" class="textbox" style="width:190px;height:18px;text-align:center;" required readonly /></td>
                                    <td><input name="txt_sq_date" id="txt_sq_date" type="text" class="textbox" style="width:120px;height:18px" required readonly /></td>
                                    
                                    <td>
                                        <select name="cmb_terms" id="cmb_terms" class="easyui-combobox" style="width:122px;height:20px;" required="true" editable="false"
                                            data-options="
                                                value:'5',
                                                valueField:'TermspaymentID', 
                                                textField:'TermDesc', 
                                                url:'<?=base_url()?>custom/get_data/termspayment'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                        
                                <tr height="20">
                                    <td style="background:#333399;text-align:center;color:#FFF;">Cust. PO. No.</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">Ship Date</td>
                                    <td style="background:#333399;text-align:center;color:#FFF;">Ship Via</td>
                                </tr>
        
                                <tr height="20">
                                    <td><input name="txt_po_no" id="txt_po_no" type="text" class="textbox" style="width:190px;height:18px;text-align:center;" required readonly/></td>
                                    <td><input name="txt_ship_date" id="txt_ship_date" type="text" class="easyui-datebox" style="width:122px;height:20px;" value='<?=$so_date?>' required="required" editable="false"/></td>
                                    
                                    <td>
                                        <select name="cmb_ship_via" id="cmb_ship_via" class="easyui-combobox" style="width:122px;height:20px;" editable="false"
                                            data-options="
                                                value:'1',
                                                valueField:'ShipmentID', 
                                                textField:'ShipmentName', 
                                                url:'<?=base_url()?>custom/get_data/shipment'
                                            ">
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

                <!-- End of form header -->
                <div class="easyui-tabs" style="margin-top:5px;height:255px" data-options="tabHeight:18">
                    <div id="tab_item" title="Items">
                        <table id="dg_item"  class="easyui-edatagrid" url="<?=base_url() . $menu_link . "/ajax_item/"?>"
                            toolbar="#toolbar" idField="id"
                            rownumbers="true" fitColumns="true" singleSelect="true" style="height:233px;">
                            <thead>
                                <tr>
                                    <th width="60" halign='center' field='SQuoNumber', data-options="align:'left'">Sales Quo. No.</th>
                                    <th width="40" halign='center' field="ItemCode" data-options="align:'left'" editor="{type:'item',options:{required:true}}">Item</th>
                                    <th width="150" halign='center' field="ItemDesc" data-options="align:'left'" editor="description">Description</th>
                                    <th width="30" halign='center' field="ItemQty" data-options="align:'center'" editor="{type:'numberbox',options:{required:true, min:0}}">Qty</th>
                                    <th width="30" halign='center' field="ItemUnit" data-options="align:'center'" editor="unit">Unit</th>
                                    <th width="40" halign='center' field="ItemPrice" data-options="align:'right'" editor="{type:'numberbox'}" formatter="formatRp">Unit Price</th>
                                    <th width="40" halign='center' field="ItemAmount" data-options="align:'right'" editor="{type:'numberbox',options:{disabled:true, required:true}}" formatter="formatRp">Amount</th>	
                                    <th width="60" halign='center' field="EmployeeName" data-options="align:'left'">Salesman</th>								
                                    <th width="50" halign='center' field="SalesmanCode" data-options="align:'left'" hidden="true">Salesman</th>
                                    <th width="20" halign='center' field="ItemMax" data-options="align:'center'" editor="{type:'numberbox'}" hidden>Max</th>
                                    <th width="50" halign='center' field="ItemAverage" data-options="align:'right'" editor="{type:'numberbox'}" formatter="formatRp" hidden>Min Price</th>
                                    <th width="10" halign='center' field="SQuoDetailID" data-options="align:'center'" hidden>ID</th>
                                    <th width="30" halign='center' field="SOrdInTaxStatus" data-options="align:'center'" editor="{type:'numberbox',options:{required:true, min:0}} hidden" >InTax</th>
                                </tr>
                            </thead>
                        </table>
                        <!--
                        <div id="toolbar">
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:$('#dg_item').edatagrid('addRow')">New</a>
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg_item').edatagrid('destroyRow')">Delete</a>
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="javascript:$('#dg_item').edatagrid('cancelRow')">Cancel</a>
                        </div>
                        -->
                    </div>
                    
                    <div id="tab_downpayment" title="Down Payment 0">				
                        <table id="dg_down_payment" class="easyui-datagrid" toolbar="#tb_dg_down_payment" idField="id" rownumbers="true" fitColumns="true" singleSelect="true" style="height:253px;">
                            <thead>
                                <tr>
                                    <th field="Description" width="75" editor="text" data-options="align:'left',halign:'center'">Description</th>
                                    <th field="TotalDP" width="75" editor="{type:'numberbox',options:{required:true}}" data-options="align:'left',halign:'center'">Total DP</th>
                                    <th field="Owing" width="75" editor="text" data-options="align:'left',halign:'center'">Owing</th>															
                                    <th field="PaymentAmount" width="75" editor="text" data-options="align:'left',halign:'center'">Payment Amount</th>															
                                    <th field="Paid" width="20" editor="{type:'checkbox',options:{on:'P',off:''}}" data-options="align:'center',halign:'center'">Paid</th>															
                                    <th field="PaymentDate" width="75" editor="datebox" data-options="align:'left',halign:'center'">Payment Date</th>															
                                    <th field="ReceiveNo" width="75" editor="{type:'validatebox',options:{required:true}}" data-options="align:'left',halign:'center'">Receive No.</th>															
                                </tr>
                            </thead>
                        </table>

                        <!--
                        <div id="tb_dg_down_payment">
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:$('#dg_down_payment').edatagrid('addRow')">New</a>
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg_down_payment').edatagrid('destroyRow')">Delete</a>
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="javascript:$('#dg_down_payment').edatagrid('cancelRow')">Cancel</a>
                        </div>
                        -->

                    </div>
                </div>
                <!-- End Item Grid -->
                
                <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:10px;">                
                    <tr>
                	<td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="0">                        
                                <tr>
                                    <td valign="top" style="padding-top:4px;"><b>Description</b></td>
                                    <td width="10" valign="top" style="padding-top:4px;"><b>:</b></td>
                                    <td>
                                        <textarea name="txa_description" id="txa_description" rows="4" class="textbox" style="width:365px;resize:none;white-space:normal;"></textarea>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    
                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td rowspan="5">&nbsp;</td>
                                    <td width="120"><b>Sub Total</b></td>

                                    <td width="10"><b>:</b></td>
                                        <td width="171">
                                        <input type="numberbox" id="sub_total" name="sub_total" value="0" class="easyui-numberbox" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" style="text-align:right;" readonly />
                                    </td>
                                </tr>

                                <tr>
                                    <td><b>PPN 10%</b></td>
                                    <td><b>:</b></td>
                                    <td>
                                        <input type="text" min="0" name="ppn" id="ppn" class="easyui-numberbox" value="0" style="text-align:right;" readonly 
                                            data-options="
                                                min:0, 
                                                precision:2, 
                                                decimalSeparator:',', 
                                                groupSeparator:'.'
                                            "
                                        />
                                    </td>
                                </tr>

                                <tr>
                                    <td><b>Total Purchase</b></td>
                                    <td><b>:</b></td>
                                    <td>
                                        <input type="text" name="total_purchase" id="total_purchase" class="easyui-numberbox" value="0" style="text-align:right;" readonly 
                                             data-options="
                                                min:0, 
                                                precision:2, 
                                                decimalSeparator:',', 
                                                groupSeparator:'.', 
                                                prefix:'Rp. '
                                             "
                                        />
                                    </td>
                                </tr>
                        
                                <tr>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td style="display:none" class="dp_line"><hr/></td>
                                </tr>
                        
                                <tr>
                                    <td><span class="dp" style="display:none" ><b>Downpayment</b></span></td>
                                    <td><span class="dp" style="display:none" ><b>:</b></span></td>

                                    <td>
                                        <span class="dp" style="display:none" >
                                            <input type="numberbox" min="0" name="txt_down_payment" id="txt_down_payment" value="0" class="easyui-numberbox dp" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" style="text-align:right;" />
                                        </span>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
            
            <div id="form-footer">
                <span style="float:left;">
                    <span>Down Payment : : </span>
                    <span id="total_owning">0</span>
                    <span>DP Used : : </span>
                    <span id="dp_used">0</span>
                    <span>Available DP : : </span>
                    <span id="available_dp">0</span>
                </span>

                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" plain="true">Help</a>				
                <a href="javascript:void(0)" id="save_button" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="insert_data();">Save</a> 								
                <a href="javascript:void(0)" id="print_button" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="" disabled>Preview & Print</a>   
                <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="location.reload();">Cancel</a>						
            </div>
        </div>
    </form>
</div>

<div id="dialog-sq" class="easyui-dialog" style="width:300px; left:150px; top:50px; background-color:#F8F8F8; height:300px; padding: 0 0px 0 0px" closed="true" data-options="modal:true, iconCls:'icon-edit'" buttons="#dialog-buttons">
    <table id="tt_choose" class="easyui-datagrid" url="<?=base_url()?>list/sales_order/get_data_sq" style="width:auto;height:auto">
        <thead>
            <tr>
                <th field="ck" checkbox="true" width="20">All</th>
                <th field="SQuoNumber" width="120" sortable="true">SQ. Number</th>
                <th field="SQuoDate" width="100" sortable="true">SQ. Date</th>
            </tr>
        </thead>
    </table>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_item_sq();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-sq');">Cancel</a>
</div>

<script type="text/javascript">
    $(document).ready(function(){	
	$('#cmb_customer').combogrid({
            onSelect:function(rowIndex, rowData){
                $('#dg_item').datagrid('load',{
                    sq_no: ''
                });
                        
                $('#sub_total').numberbox('setValue',0);
			
                var Address = rowData.CustomerAddress + ' ' + rowData.CustCity + ' ' + rowData.CustState + ' ' + rowData.CustCountry;
			
                $('#txa_address').val( Address );
                $('#txa_ship_to').val( Address );

                if(rowData.TaxIncludedStatus == 1) {
                    $('#chk_taxtable').attr('checked', true);
                    $('.chk_tax').css('display','block');	
                }
                else {
                    $('#chk_taxtable').removeAttr('checked');
                    $('.chk_tax').css('display','none');
                }

                $('#tt_choose').datagrid('load',{
                    cust_code : rowData.CustomerCode
                });		
            }
        });	
//	
//        $('#txt_so_date').datebox({
//            onChange:function(){
//                url             = base_url + 'list/sales_order/get_sord_number';                
//                var SOrd_Date   = $('#txt_so_date').datebox('getValue');
//                
//                $.post(url, {SOrd_Date:SOrd_Date}, function(result){
//                    if (result.status === true){
//                        $('#txt_so_no').val(result.number);
//                    } 
//                },'json');
//            }
//        });        
//	
        $('#dg_item').datagrid({
            onLoadSuccess: function(){
                dg          = $('#dg_item').datagrid('getRows');
                sub_total   = 0;

                $.each(dg, function(index, value){
                    sub_total = sub_total + value.ItemAmount;
                });

                $('#sub_total').numberbox('setValue', sub_total);
                calculate_sub();
            },

            onBeginEdit:function(rowIndex){
                var editors = $('#dg_item').datagrid('getEditors', rowIndex);
                var n1	= $(editors[2].target); //Item Qty
                var n2	= $(editors[4].target); //Item Price
                var n3	= $(editors[5].target); //Item Amount
                var n4	= $(editors[6].target); //Item Qty Max
                var n5	= $(editors[7].target); //Item Min Price

                var qty = n1.numberbox('getValue');
                var price = n2.numberbox('getValue');

                n1.numberbox({
                    onChange:function(){
                        if(parseInt(n1.numberbox('getValue')) > parseInt(n4.numberbox('getValue'))) {
                            $.messager.alert('Error','Qty tidak boleh lebih dari (' + n4.numberbox('getValue') + ')','error',function(){	
                                n1.numberbox('setValue', qty);
                            });
                        }
                        else {
                            var cost = n1.numberbox('getValue') * n2.numberbox('getValue');
                            n3.numberbox('setValue',cost);
                            $('#dg_item').edatagrid('saveRow');
                            calculate_sub();
                        }
                    }
                }), 

                n2.numberbox({
                    onChange:function(){
                        if(parseInt(n2.numberbox('getValue')) < parseInt(n5.numberbox('getValue'))) {
                            $.messager.alert('Error','Harga tidak boleh kurang dari (' + n5.numberbox('getValue') + ')','error',function(){	
                                n2.numberbox('setValue',price);
                            });
                        }
                        else {
                            var cost = n1.numberbox('getValue') * n2.numberbox('getValue');
                            n3.numberbox('setValue',cost);
                            $('#dg_item').edatagrid('saveRow');
                            calculate_sub();
                        }
                    }
                });
            },

            onEndEdit:function(rowIndex, rowData, changes){
                var item = $('#dg_item').datagrid('getRows');
                var sub_total = 0;

                $.each(item, function( index, value ) {
                    sub_total+= parseInt(value.ItemAmount);				
                });

                $('#sub_total').numberbox('setValue', sub_total);	
           }
        });
    });
//
    function choose_item_sq() {
	var rows 	= $('#tt_choose').datagrid('getChecked');
	var po 		= new Array();
	var sq 		= new Array();
        var sd          = new Array();
	
	for(var i=0; i<rows.length; i++){ 
            po[i] = rows[i].CustPONumber;
            sq[i] = rows[i].SQuoNumber;
            sd[i] = rows[i].SQuoDate;
	}
	
	$('#dg_item').datagrid('load',{
            sq_no: sq
	});
	
	$('#txt_po_no').val(po);	
	$('#txt_sq_no').val(sq);
	$('#txt_sq_date').val(sd);

	modal_close('dialog-sq');
    }

    function select_sales() {
	var Customer 		= $('#cmb_customer').combogrid('grid');	// get datagrid object
	var SelectedCustomer    = Customer.datagrid('getSelected');// get the selected row

	if(SelectedCustomer == null ){
            $.messager.alert('Maaf','Silahkan pilih customer terlebih dahulu','warning',function(){});
	}
	else{
            modal_open('Select Sales Quotation','dialog-sq');
	}		
    }
	
//    function insert_data(){	
//	$('#dg_item').edatagrid('saveRow');
//	var downpayment	= $('#txt_down_payment').numberbox('getValue');
//	var sub_total 	= $('#sub_total').numberbox('getValue');
//	var cek         = $('#dg_item').datagrid('getRows');
//	
//	//err_dp = 0;
//	err_qty = 0;
//	err_not_nol = 0;
//
//	//var category = "";
//
//	$.each(cek, function( index, value ) {
//            // if(category != value.ItemCategoryID){
//                    // err_dp++;
//                    // category = value.ItemCategoryID;
//            // }
//
//            if(parseInt(value.ItemQty) >  parseInt(value.max_item)) {
//                err_qty++;
//            }
//            if(parseInt(value.ItemQty) == 0) {
//                err_not_nol++;
//            }
//	});
//
//	if(err_not_nol > 0) {
//            $.messager.alert('Unseccessfull','Qty tidak Boleh bernilai 0.','error',function(){	
//                $('#dg').datagrid('reload'); 	
//            })
//	}
//	else {	
//		// if(err_dp == 0) {
//				// $.messager.alert('Unseccessfull','Tidak Boleh Bernilai 0.','error',function(){	
//						// $('#dg').datagrid('reload'); 	
//				// });
//		// }
//		// else if(err_dp > 1 && downpayment > 0) {
//				// $.messager.alert('Unseccessfull','DownPayment Tidak bisa digunakan Untuk Lebih Dari Satu Category Item','error',function(){	
//						// $('#dg').datagrid('reload'); 	
//				// })
//		// }
//		// else {
//            if(err_qty > 0) {
//                $.messager.alert('Error','Qty tidak boleh lebih dari Item Qty Sales Quotation','error',function(){	
//                    $('#dg').datagrid('reload'); 
//                });	
//            }
//            else {
//                if($('#chk_payment_dp:checked').val() == 1 && parseFloat(downpayment) == 0) {
//                    $.messager.alert('Error','DownPayment tidak boleh Bernilai 0','error',function(){	
//                        $('#dg').datagrid('reload'); 
//                    });
//                }
//                else {
//                    if(parseFloat(downpayment) > parseFloat(sub_total)) {
//                        $.messager.alert('Error','DownPayment tidak boleh lebih dari Total Bayar','error',function(){	
//                            $('#dg').datagrid('reload'); 
//                        });	
//                    }
//                    else {
//                        var i=$('#dg_item').datagrid('getRows');
//                        var detail = JSON.stringify(i);
//                        $("#detail").val(detail);
//                        save_transaction('<?=$menu_link?>','#fm_sales_order');
//                    }
//                }		
//            }					
//        // } 
//        }	
//    }
//	
//    function reset() {
//	$('#cmb_terms').combobox('clear');
//	$('#cmb_ship_via').combobox('clear');
//	$('#txa_description').val("");
//	//$('#disc_persent').val("");
//	$('#disc_value').numberbox('setValue',0);
//	$('#chk_payment_dp').removeAttr('checked');
//	$('.disableme').css('display','none');
//	$('#txt_down_payment').numberbox('setValue',0);
//    }
//    
//    //function show hide dp	
//    function hidedp(){
//	if($('#chk_payment_dp').is(':checked')){
//            $('.dp_line').css('display','');			
//            $('.dp').css('display','');			
//	}
//	else{
//            $('.dp_line').css('display','none');
//            $('.dp').css('display','none');
//            $('#txt_downpayment').numberbox('setValue',0);
//	}
//    }
//
//    function hideppn(){
//	if($('#chk_taxtable').is(':checked')){
//            $('.disableme').css('display','');		
//	}
//	else{
//            $('.disableme').css('display','none');
//            $("#chk_inclusive").prop("checked",false);
//	}
//	
//        intax_ppn();
//    }
//
//    //function show intaxppn
//    function intaxppn(){
//	var itemprice   = 0;
//	var itemamount  = 0;
//	var sub_total   = 0;
//        var intax       = 0;
//	
//	item = $('#dg_item').datagrid('getRows');
//	
//	$.each(item, function(index, value) {
//            intax       = 0;
//            itemprice   = value.ItemPrice;
//
//            if(value.ItemPrice > 0 || value.ItemPrice !== null){
//                if($('#chk_taxtable').is(':checked')){
//                    if($('#chk_inclusive').is(':checked')){
//                        itemppn     = (itemprice / 1.1) * (10 / 100);
//                        itemprice   = parseFloat(itemprice) - parseFloat(itemppn);
//                        intax = 1;
//                    }
//                    else{
//                        if(value.SOrdInTaxStatus == 1){
//                            itemprice = parseFloat(itemprice) + (itemprice * (10 / 100));
//                            intax = 0;
//                        }
//                    }
//                }
//                else{
//                    if(value.SOrdInTaxStatus == 1){
//                        itemprice   = parseFloat(itemprice) + (itemprice * (10 / 100));
//                        intax = 0;
//                    }
//                }
//
//                itemamount = parseInt(value.ItemQty) * parseFloat(itemprice);
//	
//                $('#dg_item').datagrid('updateRow',{
//                    index:index,
//                        row: {
//                            ItemPrice       : itemprice,
//                            ItemAmount      : itemamount,
//                            SOrdInTaxStatus : intax
//                        }
//                });
//			
//                sub_total = sub_total + itemamount;
//            }
//	});
//	
//	calculate_sub();
//    }
//
    function calculate_sub(){
	$('#dg_item').edatagrid('saveRow');

        var item        = $('#dg_item').datagrid('getRows');
	var sub_total   = 0;
	
	$.each(item, function(index, value) {
            if(value.ItemAmount != undefined){
                sub_total+= parseFloat(value.ItemAmount);
            }				
	});
	
	$('#sub_total').numberbox('setValue', sub_total);
	calculate();
    }

    //calculating
    function calculate(){
	$('#dg_item').edatagrid('saveRow');
	
	var sub     = $('#sub_total').numberbox('getValue');	
	var total   = 0;
	var ppn     = 0;
	
        if($('#chk_inclusive').is(':checked')){
            ppn     = (sub / 1.1) * (10 / 100);
        }
	
	total = parseFloat(sub) + ppn;
	
	$('#ppn').numberbox('setValue', ppn);
	$('#sub_total').numberbox('setValue', sub);
	$('#total_purchase').numberbox('setValue', total);
    }
</script>