<?php $ci = get_instance();?>
<style type="text/css">
	.transparent_input{
		background-color: transparent; 
		border-color: transparent;
	}
</style>
<form id ="div_form" onSubmit="return edit_validation(this);" action="<?php echo base_url($this->router->fetch_class()."/update")?>" method="POST" style="padding-left: 5px; padding-right: 5px;">
	<!--  -->
	<?php if(isset($data)):?>
        <input type="hidden" name="REFNO" value="<?php echo $data->REFNO;?>">
	<?php endif;?>
	<?php if(!isset($data)):?>
        <input type="hidden" name="UNIQID" value="<?php echo uniqid();?><?php echo date("YmdHis");?>">
    <?php endif;?>
	<!--  -->
	<div class = "row">
        <div class="col-md-8">
        	<table>
        		<tr>
        			<div class="col-md-6" id="REFNO_div">
				        <label> Ref.No.</label>
				        <div class="form-group">
				            <input type="text" name="REFNO" onInput="" value="<?php if(isset($data)) echo $ci->fm->str_esc(trim($data->REFNO), 2);?>" class="form-control" placeholder="(Auto-generate)" autocomplete="OFF" disabled>
				      	</div>
				    </div>
				    <div class="col-md-6" id="DATE_div">
				    	<span style="color: red;">* </span><label> Date</label>
				        <div class="form-group">
				            <div class="has-feedback" style="max-width: 200px; display: inline-block;">
				            <input type="text" name="DATE" id="DATE" value="<?php if(isset($data) && strlen($data->DATE) > 0) echo $this->fm->str_esc(date("Y-m-d", strtotime($data->DATE)), 2);?>" maxlength="19" class="form-control datepicker1" autocomplete="OFF" required="required">
				            <span class="fa fa-calendar form-control-feedback" style="top: 10px; right: 0px;" aria-hidden="true"></span>
				            </div>
				        </div>
				    </div>
        		</tr>
        		<tr>
        			<div class="col-md-6" id="DEBTOR_div">
			        	<span style="color: red;">* </span><label> Debtor</label>
			            <div class="has-feedback">
	                        <select name="DEBTOR" id="DEBTOR" class="form-control select2" required="required" onchange="find_debtor_gold(this);">
			                    <option value="">--- Select a value ---</option>
			                    <?php foreach($all_debtor as $v):?>
			                        <option value="<?php echo $v->CODE;?>" <?php if(isset($data) && $data->DEBTOR == $v->CODE) echo "selected='selected'";?>>
			                        <?php echo "[".$v->CODE."] ".$v->NAME;?>
			                        </option>
			                    <?php endforeach;?>
			                </select>
                        </div>
			        </div>
			       <div class="col-md-6" id="TT_div">
			        	<span style="color: red;">* </span><label> Transaction Type</label>
			            <div class="has-feedback">
	                        <select name="TT" id="TT" class="form-control select2" required="required">
			                    <option value="">--- Select a value ---</option>
			                    <?php foreach($all_tt as $v):?>
			                        <option value="<?php echo $v->TT;?>" <?php if(isset($data) && $data->TT == $v->TT) echo "selected='selected'";?>>
			                        <?php echo "[".$v->TT."] ".$v->TTNAME;?>
			                        </option>
			                    <?php endforeach;?>
			                </select>
                        </div>
			        </div>
        		</tr>
        		<tr>
        			<div class="col-md-12">
        				<br/>
        			</div>
        		</tr>
        		<tr>
        			<div class="col-md-6" id="STAFF_div">
			        	<span style="color: red;">* </span><label> Staff</label>
			            <div class="has-feedback">
	                        <select name="STAFF" id="STAFF" class="form-control select2" required="required">
			                    <option value="">--- Select a value ---</option>
			                    <?php foreach($all_staff as $v):?>
			                        <option value="<?php echo $v->CODE;?>" <?php if(isset($data) && $data->STAFF == $v->CODE) echo "selected='selected'";?>>
			                        <?php echo "[".$v->CODE."] ".$v->NAME;?>
			                        </option>
			                    <?php endforeach;?>
			                </select>
                        </div>
			        </div>
			        <div class="col-md-6" id="REMARKS_div">
			        	<label> Remarks</label>
			            <div class="form-group">
			                <input type="text" name="PARTICULAR" id="REMARKS" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->PARTICULAR, 2);?>" class="form-control" autocomplete="OFF">
			            </div>
			        </div>
        		</tr>
        		<tr>
        			<div class="col-md-12">
        				<br/>
        			</div>
        		</tr>
        		<tr>
        			<table id="table_sub" class="table table-bordered" style="zoom:90%; height: 100px; max-height: 800px; overflow-y: scroll;">
						<thead>
							<tr>
								<th style=" width: 100px; white-space: nowrap; text-align: center;">Item<br/>Code <span style="color: red;"></span></th>
								<th style=" width: 50px; white-space: nowrap; text-align: center;">Description <span style="color: red;"></span></th>
								<th style=" width: 50px; white-space: nowrap; text-align: center;">Inch <span style="color: red;"></span></th>
								<th style=" width: 50px; white-space: nowrap; text-align: center;">Pcs <span style="color: red;"></span></th>
								<th style=" width: 50px; white-space: nowrap; text-align: center;">Qty <span style="color: red;"></span></th>
								<th style=" width: 50px; text-align: center;">Unit </th>
								<th style=" width: 50px; text-align: center;">Price</th>
								<th style=" width: 50px; text-align: center;">Total/<br/>Labour</th>
								<th style=" width: 50px; text-align: center;">Tax<br/>Code</th>
								<th style=" width: 50px; text-align: center;">T%</th>
								<th style=" width: 50px; text-align: center;">GST</th>
								<th style=" width: 50px; text-align: center;">AMT</th>
								<th style=" width: 1px; text-align: center; display: none;" class="delete_title">Delete</th>
							</tr>
						</thead>
						<tbody>
							<tr id="edit_sub_tr_add_item" style="<?php if(! $can_create) echo "display: none;";?>">
								<td id="sub_tr_add_btn" colspan="99" style="text-align: center;">
									<button type="button" onClick="check_main();" id="add_new" class="btn btn-primary btn_add">
										<i class="fa fa-plus-circle"></i>
									</button>
								</td>
							</tr>
						</tbody>
					</table>
        		</tr><br/>
        		<tr>
			        <fieldset class="the-fieldset">
			    		<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Item</legend>
				    		<div class = "row">
	        					<div class="col-md-4">
				    			<label> Total</label>
						            <div class="form-group">
						                <input type="text" name="ITEM_TOTAL" id="ITEM_TOTAL" class="form-control" autocomplete="OFF" readonly="readonly">
						            </div>
						        </div>
					            <div class="col-md-4">
				    			<label> Total GST</label>
						            <div class="form-group">
						                <input type="text" name="ITEM_GST" id="ITEM_GST" class="form-control" autocomplete="OFF" readonly="readonly">
						            </div>
					        	</div>
					        	<div class="col-md-4">
				    			<label> Item Amount</label>
						            <div class="form-group">
						                <input type="text" name="ITEM_AMT" id="ITEM_AMT" class="form-control" autocomplete="OFF" readonly="readonly">
						            </div>
					        	</div>
				        	</div>
			    	</fieldset>
        		</tr><br/>
        		<tr>
			        <fieldset class="the-fieldset">
			    		<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Gold</legend>
				    		<div class = "row">
	        					<div class="col-md-4">
				    			<label> Total</label>
						            <div class="form-group">
						                <input type="text" name="GOLD_TOTAL" id="GOLD_TOTAL" class="form-control" autocomplete="OFF" readonly="readonly">
						            </div>
						        </div>
					            <div class="col-md-4">
				    			<label> Add GST</label>
						            <div class="form-group">
						                <input type="text" name="GOLD_GST" id="GOLD_GST" class="form-control" autocomplete="OFF" readonly="readonly">
						            </div>
					        	</div>
					        	<div class="col-md-4">
				    			<label> Gold Amount</label>
						            <div class="form-group">
						                <input type="text" name="GOLD_AMT" id="GOLD_AMT" class="form-control" autocomplete="OFF" readonly="readonly">
						            </div>
					        	</div>
				        	</div>
			    	</fieldset>
        		</tr>
        		<tr>
        			<fieldset class="the-fieldset" style="border: transparent;">
				    	<div class = "row">
	        				<div class="col-md-4"></div>
					        <div class="col-md-4"></div>
					        <div class="col-md-4">
				    		<label> Total Amount</label>
						        <div class="form-group">
						            <input type="text" name="TOTAL_AMT" id="TOTAL_AMT" class="form-control" autocomplete="OFF" readonly="readonly">
						        </div>
					        </div>
				        </div>
			    	</fieldset>
        		</tr>
        	</table>
        </div>
        <div class="col-md-4">
        	<table>
        		<tr>
        			<fieldset class="the-fieldset">
			    		<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Payment</legend>
				    		<div class = "row">
	        					<div class="col-md-6">
				    			<label> Pay Type</label>
						            <div class="has-feedback">
				                        <select name="PAYTYPE" class="form-control select2">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_paytype as $v):?>
						                        <option value="<?php echo $v->CODE;?>" <?php if(isset($data) && $data->PAYTYPE == $v->CODE) echo "selected='selected'";?>>
						                        <?php echo "[".$v->CODE."] ".$v->NAME;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
			                        </div>
						        </div>
					            <div class="col-md-6">
				    			<label> Payment Date</label>
						            <div class="form-group">
							            <div class="has-feedback" style="max-width: 200px; display: inline-block;">
							            <input type="text" name="PAYDATE" id="PAYDATE" value="<?php if(isset($data) && strlen($data->PAYDATE) > 0) echo $this->fm->str_esc(date("Y-m-d", strtotime($data->PAYDATE)), 2);?>" maxlength="19" class="form-control datepicker2" autocomplete="OFF">
							            <span class="fa fa-calendar form-control-feedback" style="top: 10px; right: 0px;" aria-hidden="true"></span>
							            </div>
							        </div>
					        	</div>
				        	</div>
				    		<div class="form-group col-md-6">
					    		<fieldset class="the-fieldset">
					    		<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Cash Payment</legend>
						    		<div class="col-md-12">
					    			<label> Total Amount (Cash)</label>
							            <div class="form-group">
							                <input type="text" name="CASHAMT" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->CASHAMT, 2);?>" class="form-control" onblur="calculate_payamt(this);" autocomplete="OFF">
							            </div>
							        </div>
					    		</fieldset>
				    		</div>
				    		<div class="form-group col-md-6">
					    		<fieldset class="the-fieldset">
					    		<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Bank/Credit Card</legend>
					    			<div class="col-md-12">
					    			<label> Cheque No.</label>
							            <div class="form-group">
							                <input type="text" name="BANK" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->BANK, 2);?>" class="form-control" autocomplete="OFF">
							            </div>
							        </div><div class="col-md-12">
					    			<label> Credit Card</label>
							            <div class="form-group">
							                <input type="text" name="CARD" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->CARD, 2);?>" class="form-control" autocomplete="OFF">
							            </div>
							        </div>
									<div class="col-md-12">
					    			<label> Payment Amount</label>
							            <div class="form-group">
							                <input type="text" name="BCAMT" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->BCAMT, 2);?>" class="form-control" onblur="calculate_payamt(this);" autocomplete="OFF">
							            </div>
							        </div>
					    		</fieldset>
				    		</div>
			    	</fieldset>
        		</tr>
        		<br/>
        		<tr>
        			<fieldset class="the-fieldset">
			    		<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Gold Price</legend>
				    		<div class = "row">
	        					<div class="col-md-6">
				    			<label> Gold</label>
						            <div class="has-feedback">
	                                    <select name="GOLD" id="GOLD" class="form-control select2" onchange="find_gold_detail(this);">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_gold as $v):?>
						                        <option value="<?php echo $v->CODE;?>" <?php if(isset($data) && $data->GOLD == $v->CODE) echo "selected='selected'";?>>
						                        <?php echo "[".$v->CODE."] ".$v->NAME;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
                                    </div>
						        </div>
							    <div class="col-md-6">
				    			<label> Gold Stock</label>
						            <div class="input-group">
						                <input type="text" name="GOLD_BAL" id="GOLD_BAL" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->GOLD_BAL, 2);?>" class="form-control" autocomplete="OFF" readonly="readonly">
						                <span class="input-group-addon">gm</span>
						            </div>
						        </div>
				        	</div><br/>
				        	<div class = "row">
	        					<div class="col-md-6">
				    			<label> Weight</label>
						            <div class="input-group">
						                <input type="text" name="WEIGHT" id="WEIGHT" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->WEIGHT, 2);?>" onblur="calculate_puregold(this);" class="form-control" autocomplete="OFF">
						                <span class="input-group-addon">gm</span>
						            </div>
						        </div>
					            <div class="col-md-6">
				    			<label> Return Weight</label>
						            <div class="input-group">
						                <input type="text" name="WEIGHT_RETURN" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->WEIGHT_RETURN, 2);?>" class="form-control" autocomplete="OFF">
						                <span class="input-group-addon">gm</span>
						            </div>
					        	</div>
				        	</div><br/>
				        	<div class = "row">
	        					<div class="col-md-6">
				    			<label> Pure Gold</label>
						            <div class="input-group">
						                <input type="text" name="PUREGOLD" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->PUREGOLD, 2);?>" class="form-control" autocomplete="OFF" readonly="readonly">
						                <span class="input-group-addon">gm</span>
						            </div>
						        </div>
					           <!--  <div class="col-md-6">
				    			<label> Trade In</label>&nbsp; -->
				    			<!-- <?php //if(isset($tradein_data) && ($can_create_tradein)):?>
					    			<button type="submit" id="edit_btn_submit" class="btn btn-primary btn-xs" style="font-size: 14px;">
										<i class="fa fa-arrow-circle-up"></i>
										Update Trade-In data
									</button>
								<?php// endif;?> -->
						           <!--  <div class="input-group">
						            <?php if(isset($data) && ($can_create_tradein)):?>
						            	<span class="input-group-addon" id="tradein_button" onclick="check_main_tradein();" style="background-color: #ECC13F; color: black; cursor: pointer;">Trade In</span>
						            <?php endif;?>
						                <input type="text" name="TRADE_IN_TOTAL" id="TRADE_IN_TOTAL" onblur="calculate_gold_price(this);" value="<?php if(isset($tradein_total)) echo $ci->fm->str_esc($tradein_total, 2);?>" class="form-control" autocomplete="OFF" readonly="readonly">
						                <span class="input-group-addon">gm</span>
						            </div> -->
<!-- 						            <div class="has-feedback">
				                        <select name="TRADEIN_REFNO" id="TRADEIN_REFNO" class="form-control select2">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_tradein as $v):?>
						                    <?php if($data->DEBTOR == $v->DEBTOR):?>
						                        <option value="<?php echo $v->REFNO;?>" <?php if(isset($data) && $data->TRADEIN_REFNO == $v->REFNO) echo "selected='selected'";?>>
						                        <?php echo "[".$v->REFNO."]  ".$v->BAL." gm";?>
						                        </option>
						                    <?php endif;?>
						                    <?php endforeach;?>
						                </select>
			                        </div>
					        	</div> -->
				        	</div><br/>
				        	<!-- <div class = "row">
				        		<div class="col-md-6">
				        			<label>Trade In Ref. No</label>
				        			<div class="form-group">
					        			<input type="text" class="form-control" name="DEBTOR_TRADEIN" id="DEBTOR_TRADEIN" readonly="readonly">
					        		</div>
				        		</div>
				        		<div class="col-md-6">
				        			<label> Trade In Balance</label>
				        			<div class="input-group">
					        			<input type="text" class="form-control" name="DEBTOR_GOLD" id="DEBTOR_GOLD" value="<?php if(isset($tradein_total)){ echo $ci->fm->str_esc($tradein_total, 2);}else{echo'0';}?>" readonly="readonly">
					        			<span class="input-group-addon">gm</span>
					        		</div>
				        		</div>
				        	</div> -->
				        	<div class = "row">
				        		<div class="col-md-6">
				        			<label>Trade In</label>
				        			<div class="has-feedback">
				                        <select name="TRADEIN_REFNO" id="TRADEIN_REFNO" class="form-control select2" onchange="find_tradein_detail(this);">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_tradein as $v):?>
						                    <?php if($data->DEBTOR == $v->DEBTOR):?>
						                        <option value="<?php echo $v->REFNO;?>" <?php if(isset($data) && $data->TRADEIN_REFNO == $v->REFNO) echo "selected='selected'";?>>
						                        <?php echo "[".$v->REFNO."]  ".$v->BAL." gm";?>
						                        </option>
						                    <?php endif;?>
						                    <?php endforeach;?>
						                </select>
			                        </div>
				        		</div>
				        		<div class="col-md-6">
				        			<label> Trade In Balance</label>
				        			<div class="input-group">
					        			<input type="text" class="form-control" name="DEBTOR_GOLD" id="DEBTOR_GOLD" readonly="readonly">
					        			<span class="input-group-addon">gm</span>
					        		</div>
				        		</div>
				        	</div><br/>
				        	<div class = "row">
	        					<div class="col-md-6">
				    			<label> Rate</label>
						            <div class="form-group">
						                <input type="text" name="RATE" id="RATE" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->RATE, 2);?>" onblur="calculate_puregold(this);calculate_gold_price(this);calculate_gold_amt(this);" class="form-control" autocomplete="OFF">
						            </div>
						        </div>
					            <div class="col-md-6">
				    			<label> Total (Gold Rate)</label>
						            <div class="form-group">
						                <input type="text" name="TOTAL_RATE" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->TOTAL_RATE, 2);?>" class="form-control" autocomplete="OFF">
						            </div>
					        	</div>
				        	</div><br/>
				        	<div class = "row">
	        					<div class="col-md-6">
				    			<label> Gold Price</label>
						            <div class="form-group">
						                <input type="text" name="GOLDPRICE" id="GOLDPRICE" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->GOLDPRICE, 2);?>" class="form-control" autocomplete="OFF" readonly="readonly">
						            </div>
						        </div>
					            <div class="col-md-6">
						            <!-- <div class="has-feedback pull-left">
						            	<label> Inclusive Tax</label><br/>
                                        <input type='hidden' name='INCLUSIVE' class='hidden_checkbox_value'>
								    	<input type='checkbox' class='' onChange='edit_item_checkbox_set_value(this);' style='width: 20px; height: 20px;' <?php if(isset($data) && $data->INCLUSIVE != "") echo "checked='checked'";?>>
                                    </div> -->
					        	</div>
				        	</div>
				        	<div class="row">
								<div class="col-md-6">
							        <label> Tax Code</label>
							        <div class="has-feedback">
	                                    <select name="TAXCODE" id="TAXCODE" class="form-control select2" onchange="find_tax_gold(this);calculate_puregold(this);calculate_gold_price(this);calculate_gold_amt(this);">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_taxcode as $v):?>
						                        <option value="<?php echo $v->CODE;?>" <?php if(isset($data) && $data->TAXCODE == $v->CODE) echo "selected='selected'";?>>
						                        <?php echo "[".$v->CODE."] ".$v->DESCRIPTION;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
                                    </div>
							    </div>
							    <div class="col-md-3">
							        <label> Tax %</label>
							        <div class="form-group">
							            <input type="text" name="PERCEN" id="PERCEN" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->PERCEN, 2);?>" class="form-control" autocomplete="OFF">
							        </div>
							    </div>
							    <div class="col-md-3">
							        <label> GST</label>
							        <div class="form-group">
							            <input type="text" name="GST" id="GST" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->GST, 2);?>" class="form-control" autocomplete="OFF">
							        </div>
							    </div>
							</div>
			    	</fieldset>
        		</tr><br/>
        		<tr>
        			<div class="pull-right">
	        			<label> Total Amount</label>
						<div class="form-group">
							<input type="text" name="AMOUNT" id="AMOUNT" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->AMOUNT, 2);?>" class="form-control" autocomplete="OFF" readonly="readonly">
						</div>
						<label> Payment Amount</label>
						<div class="form-group">
							<input type="text" name="PAYAMT" id="PAYAMT" value="<?php if(isset($data) && $data->PAYAMT!= ""){ echo $ci->fm->str_esc($data->PAYAMT, 2);}else{echo(0);}?>" class="form-control" autocomplete="OFF" readonly="readonly">
						</div>
						<label> Balance</label>
						<div class="form-group">
							<input type="text" name="BAL" id="BAL" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->BAL, 2);?>" class="form-control" autocomplete="OFF" readonly="readonly">
						</div>
					</div>
        		</tr>
        	</table>
        </div>
    </div>
    <?php if((! isset($data) && $can_create) || (isset($data) && $can_edit)):?>
		<div class="form-group" style="margin-top: 30px;">
			<label></label>
			<button type="submit" id="edit_btn_submit" class="btn btn-primary btn-xs-block btn_submit">
				<i class="fa fa-save"></i>
				Save
			</button>
		</div>
	<?php endif;?>
</form>

<!-- Sub_modal -->
	<div id="Sub_modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<form id="sub_form" action="<?php echo base_url($this->router->fetch_class()."/update")?>" method="POST" class="form-horizontal">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">Details</h4>
					</div>
					<div class="modal-body">
						<!-- Main data -->
						<?php if(isset($data)):?>
							<input type="hidden" name="REFNO" value="<?php echo $data->REFNO;?>">
						<?php endif;?>
						<?php if(!isset($data)):?>
							<input type="hidden" name="UNIQID" id="UNIQID">
							<input type="hidden" name="DATE" id="SUB_DATE">
							<input type="hidden" name="DEBTOR" id="SUB_DEBTOR">
							<input type="hidden" name="TT" id="SUB_TT">
							<input type="hidden" name="STAFF" id="SUB_STAFF">
							<input type="hidden" name="REMARKS" id="SUB_REMARKS">
						<?php endif;?>
						<!---->
						<input type="hidden" name="SUB_PKKEY[]" id="SUB_PKKEY">
						<input type="hidden" name="AMOUNT" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->AMOUNT, 2);?>">
						<input type="hidden" name="BAL" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->BAL, 2);?>">
						<input type="hidden" name="item_amt" id="item_amt1" value="<?php if(isset($item_amt)) echo number_format((float)$item_amt, 2, '.', '');?>">
						<input type="hidden" name="item_gst" id="item_gst1" value="<?php if(isset($item_amt)) echo number_format((float)$item_gst, 2, '.', '');?>">
						<input type="hidden" name="item_total" id="item_total1" value="<?php if(isset($item_amt)) echo number_format((float)$item_total, 2, '.', '');?>">
						<Table>
							<tr>
								<th class="FG_th">
	                                <span style="color: red;">*</span> Item Code:
	                            </th>
	                            <td class="FG_td" style="" colspan="3">
                                    <div class="has-feedback">
                                    	<select name="SUB_ITEM[]" id="SUB_ITEM" class="form-control select2" onchange="find_item_image(this);find_item_detail(this);" required="required">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_item as $v):?>
						                        <option value="<?php echo $v->CODE;?>" <?php if(isset($sub) && $sub->ITEM == $v->CODE) echo "selected='selected'";?>>
						                        <?php echo "[".$v->CODE."] ".$v->NAME;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
                                    </div>
                                </td>
                                <td class="FG_td" style="display: none;" id="item_image" rowspan="3">
                                	<center><div id="container"></div></center>
                                </td>
                            </tr>
                            <tr>
								<th class="FG_th">
	                                <span style="color: red;">*</span> Description:
	                            </th>
	                            <td class="FG_td" style="" colspan="3">
                                    <input type='text' size='50' name='SUB_DESCRIPT[]' id="SUB_DESCRIPT" class='form-control' autocomplete='off' style='text-align: left;' required="required">
                                </td>
                            </tr>
							<tr>
								<!-- <th class="FG_th">
	                                <span style="color: red;"></span> Inclusive:
	                            </th>
	                            <td class="FG_td" style="" colspan="3">
	                                <input type='hidden' name='SUB_INCLUSIVE[]' id="SUB_INCLUSIVE" class='hidden_checkbox_value'>
									<input type='checkbox' class='' onChange='edit_item_checkbox_set_value(this);' style='width: 20px; height: 20px;'>
								</td> -->
						</Table>
						<br/>
						<Table>
							<tr>
								<fieldset class="the-fieldset">
						    		<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Total/Labour Charges</legend>
						    		<div class = "row" style="padding-left: 15px;">
			        					<div class="col-md-1">
								            <div class="form-group" style="width: 100px;">
								                <input type="number" min="0" step='.01' name="SUB_INCH[]" id="SUB_INCH" class="form-control" placeholder="Inch" onblur="calculate_qty(this);">
								            </div>
								        </div>
								        <div class="col-md-1" style="width: 100px; text-align: center;">
								            <strong style="font-size: 25px; padding-left: 15px;">×</strong>
								        </div>
							            <div class="col-md-1">
								            <div class="form-group" style="width: 100px;">
								                <input type="number" min="0" step='.01' name="SUB_PCS[]" id="SUB_PCS" class="form-control" placeholder="Pcs" onblur="calculate_qty(this);">
								            </div>
								        </div>
								        <div class="col-md-1" style="width: 100px; text-align: center;">
								            <strong style="font-size: 25px; padding-left: 15px;">=</strong>
								        </div>
								        <div class="col-md-1">
								            <div class="form-group" style="width: 100px;">
								                <input type="number" min="0" step='.01' name="SUB_QTY[]" id="SUB_QTY" class="form-control" placeholder="Qty" onblur="calculate_qty(this);">
								            </div>
								        </div>
								        <div class="col-md-1" style="width: 120px;">
								            <div class="has-feedback">
		                                    	<select name="SUB_UNIT[]" class="form-control select2" id="SUB_UNIT" required="required">
								                    <option value="">Unit</option>
								                    <?php foreach($all_unit as $v):?>
								                        <option value="<?php echo $v->CODE;?>" <?php if(isset($sub) && $sub->UNIT == $v->CODE) echo "selected='selected'";?>>
								                        <?php echo $v->CODE;?>
								                        </option>
								                    <?php endforeach;?>
								                </select>
                                    		</div>
								        </div>
								        <div class="col-md-1" style="width: 100px; text-align: center;">
								            <strong style="font-size: 25px; padding-left: 15px;">×</strong>
								        </div>
								        <div class="col-md-1">
								            <div class="form-group" style="width: 100px;">
								                <input type="number" min="0" step='.01' name="SUB_PRICE[]" id="SUB_PRICE" class="form-control" placeholder="Price" onblur="calculate_labour(this);">
								            </div>
								        </div>
						        	</div>
						        	<div class = "row" style="padding-left: 15px;">
			        					<div class="col-md-3" style="width: 240px;">
								            <fieldset class="the-fieldset">
			    								<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Inch (For Record Only)</legend>
			    								<input type="number" min="0" step='.01' name="SUB_LENGTH[]" id="SUB_LENGTH" class="form-control" placeholder="Length">
			    							</fieldset>
								        </div>
								        <div class="col-md-1" style="width: 100px; text-align: center;">
								            <strong style="font-size: 25px; padding-left: 15px;">=</strong>
								        </div>
								        <div class="col-md-1">
								            <div class="form-group" style="width: 200px;">
								                <input type="number" min="0" step='.01' name="SUB_LABOUR[]" id="SUB_LABOUR" class="form-control" placeholder="Total/Labour Charges" readonly="readonly">
								            </div>
								        </div>
						        	</div>
								</fieldset>
							</tr>
							<br/>
							<tr>
								<div class="row">
									<div class="col-md-8">
							        	<label> Tax Code</label>
							            <div class="has-feedback">
	                                    	<select name="SUB_TAXCODE[]" class="form-control select2" id="SUB_TAXCODE" onchange="find_tax(this);">
							                    <option value="">--- Select a value ---</option>
							                    <?php foreach($all_taxcode as $v):?>
							                        <option value="<?php echo $v->CODE;?>" <?php if(isset($sub) && $sub->TAXCODE == $v->CODE) echo "selected='selected'";?>>
							                        <?php echo "[".$v->CODE."] ".$v->DESCRIPTION;?>
							                        </option>
							                    <?php endforeach;?>
							                </select>
                                    	</div>
							        </div>
							        <div class="col-md-2">
							        	<label> Tax %</label>
							            <div class="">
							                <input type="text" name="SUB_PERCEN[]" id="SUB_PERCEN" value="" class="form-control" autocomplete="OFF" readonly="readonly">
							            </div>
							        </div>
							        <div class="col-md-2">
							        	<label> GST</label>
							            <div class="">
							                <input type="text" name="SUB_GST[]" id="SUB_GST" value="" class="form-control" autocomplete="OFF" readonly="readonly">
							            </div>
							        </div>
								</div>
							</tr>
							<tr>
								<hr/>
							</tr>
							<tr>
								<th class="FG_th">
	                                <span style="color: red;"></span> Total Amount:
	                            </th>
	                            <td class="FG_td" style="">
                                    <input type='text' name='SUB_AMT[]' id="SUB_AMT" class='form-control' autocomplete='off' style="width: 200px;" readonly="readonly">
                                </td>
                            </tr>
						</Table>
					</div>
					<div class="modal-footer" style="padding: 10px;">
						<a href="javascript:;" data-dismiss="modal" class="btn btn-default">Close</a>
						<!-- <button type="submit" class="btn btn-primary btn_submit">Save</button> -->
						<a href="javascript:;" onclick="calculate_sum(this);" class="btn btn-primary">Save</a>
					</div>
				</form>
			</div>
		</div>
	</div>
<!--  -->

<!-- tradein_modal -->
	<div id="tradein_modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-lg" style="width: 60%;">
			<div class="modal-content">
				<form onSubmit="disable_btn('.btn_submit');" action="<?php echo base_url($this->router->fetch_class()."/update_tradein")?>" method="POST" class="form-horizontal">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">Trade In</h4>
						<?php if(isset($tradein_data)):?>
						<div style="margin-top: -23px; margin-left: 80px;">
							<a href="javascript:;" onclick="print_tradein();" class="hidden-xs btn btn-primary btn-xs">
							   <i class="fa fa-print"></i>
							    Print
							</a>
						</div>
						<?php endif;?>
					</div>
					<div class="modal-body" style="">
						<!---->
							<input type="hidden" name="INVOICE" value="<?php if(isset($data)) echo $data->REFNO;?>">
							<?php if(isset($tradein_data)):?>
								<input type="hidden" name="SUB_TRADEIN_REFNO" id="tradein_refno" value="<?php echo $tradein_data->REFNO;?>">
							<?php endif;?>
							<?php if(!isset($data)):?>
						        <input type="hidden" name="SUB_TRADEIN_UNIQID" value="<?php echo uniqid();?><?php echo date("YmdHis");?>">
						    <?php endif;?>
						    <input type="hidden" id="SUB_TRADEIN_TOTAL_TRADEIN" name="SUB_TRADEIN_TOTAL_TRADEIN">
						<!---->
						<table>
							<tr>
			        			<div class="col-md-6" style="padding-left: 10px;"> 
							        <label> Ref.No.</label>
							        <div class="">
							            <input type="text" name="SUB_TRADEIN_REFNO" value="<?php if(isset($tradein_data)) echo $ci->fm->str_esc(trim($tradein_data->REFNO), 2);?>" class="form-control" placeholder="(Auto-generate)" autocomplete="OFF" disabled>
							      	</div>
							    </div>
							    <div class="col-md-6">
							    	<span style="color: red;">* </span><label> Date</label>
							        <div class="">
							            <div class="has-feedback" style="max-width: 200px; display: inline-block;">
							            <input type="text" name="SUB_TRADEIN_DATE" id="SUB_TRADEIN_DATE" value="<?php if(isset($tradein_data) && strlen($tradein_data->DATE) > 0) echo $this->fm->str_esc(date("Y-m-d", strtotime($tradein_data->DATE)), 2);?>" maxlength="19" class="form-control datepicker1" autocomplete="OFF" required="required">
							            <span class="fa fa-calendar form-control-feedback" style="top: 10px; right: 0px;" aria-hidden="true"></span>
							            </div>
							        </div>
							    </div>
			        		</tr>
			        		<tr>
			        			<div class="col-md-12">
			        				<br/>
			        			</div>
			        		</tr>
			        		<tr>
			        			<div class="col-md-6">
						        	<span style="color: red;">* </span><label> Debtor</label>
						            <div class="has-feedback">
				                        <select name="SUB_TRADEIN_DEBTOR" id="SUB_TRADEIN_DEBTOR" class="form-control select2">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_debtor as $v):?>
						                        <option value="<?php echo $v->CODE;?>" <?php if(isset($tradein_data) && $tradein_data->DEBTOR == $v->CODE) echo "selected='selected'";?>>
						                        <?php echo "[".$v->CODE."] ".$v->NAME;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
			                        </div>
						        </div>
						       <div class="col-md-6">
						        	<span style="color: red;">* </span><label> Transaction Type</label>
						            <div class="has-feedback">
				                        <select name="SUB_TRADEIN_TT" id="SUB_TRADEIN_TT" class="form-control select2">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_tt as $v):?>
						                        <option value="<?php echo $v->TT;?>" <?php if(isset($tradein_data) && $tradein_data->TT == $v->TT) echo "selected='selected'";?>>
						                        <?php echo "[".$v->TT."] ".$v->TTNAME;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
			                        </div>
						        </div>
			        		</tr>
			        		<tr>
			        			<div class="col-md-12">
			        				<br/>
			        			</div>
			        		</tr>
			        		<tr>
			        			<div class="col-md-6" id="STAFF_div">
						        	<span style="color: red;">* </span><label> Staff</label>
						            <div class="has-feedback">
				                        <select name="SUB_TRADEIN_STAFF" id="SUB_TRADEIN_STAFF" class="form-control select2">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_staff as $v):?>
						                        <option value="<?php echo $v->CODE;?>" <?php if(isset($tradein_data) && $tradein_data->STAFF == $v->CODE) echo "selected='selected'";?>>
						                        <?php echo "[".$v->CODE."] ".$v->NAME;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
			                        </div>
						        </div>
						        <div class="col-md-6">
						        	<label> Remarks</label>
						            <div class="">
						                <input type="text" name="SUB_TRADEIN_REMARK" value="<?php if(isset($tradein_data)) echo $ci->fm->str_esc(trim($tradein_data->PARTICULAR), 2);?>" class="form-control" autocomplete="OFF">
						            </div>
						        </div>
			        		</tr>
			        		<tr>
			        			<div class="col-md-12">
			        				<br/>
			        			</div>
			        		</tr>
							<tr>
								<table id="table_sub" class="table table-bordered" style="">
									<thead>
										<tr>
											<th style="text-align: center; width: 20%;">Gold</th>
											<th style="text-align: center; width: 30%;">Description<br/>of Gold</th>
											<th style="text-align: center;">Weight<br/>of Gold</th>
											<th style="text-align: center;">Rate %</th>
											<th style="text-align: center;">Nett Weight<br/>of Pure Gold</th>
											<th style="text-align: center;">Delete</th>
										</tr>
									</thead>
									<tbody>
										<tr id="edit_sub_tr_add">
											<td id="sub_tr_add_btn" colspan="99" style="text-align: center;">
												<button type="button" onClick="add_sub_tradein();" class="btn btn-primary">
													<i class="fa fa-plus-circle"></i>
												</button>
											</td>
										</tr>
									</tbody>
									<thead>
										<tr>
											<th colspan="4" style="text-align: right;">Total (gram):</th>
											<th class="" id="SUB_TRADEIN_TOTAL_TRADEIN" name="SUB_TRADEIN_TOTAL_TRADEIN" style="text-align: right;"><?php if(isset($tradein_total)) echo $ci->fm->str_esc(trim($tradein_total), 2);?></th>
											<th></th>
										</tr>
									</thead>
								</table>
							</tr>
						</table>	
					</div>
					<div class="modal-footer" style="padding: 10px;">
						<?php if(isset($tradein_data)):?>
							<a href="#delete_modal" data-toggle="modal" class="btn btn-danger pull-left" style="display: none;" title="Delete" id="delete_tradein">
								<i class="fa fa-trash-o"></i>
								Delete
							</a>
						<?php endif;?>
						<a href="javascript:;" data-dismiss="modal" class="btn btn-default">Close</a>
						<button type="submit" class="btn btn-primary btn_submit" id="edit_btn_submit">Save</button>
					</div>
				</form>
			</div>
		</div>
	</div>
<!--  -->

<!-- delete_modal -->
	<div id="delete_modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-sm">
			<div class="modal-content">
				<form onSubmit="disable_btn('.btn_submit');" action="<?php echo base_url($this->router->fetch_class()."/delete_tradein")?>" method="POST" class="form-horizontal">
					<!--  -->
                    <input type="hidden" name="SUB_TRADEIN_REFNO" value="<?php if(isset($tradein_data)) echo $ci->fm->str_esc(trim($tradein_data->REFNO), 2);?>">
					<!--  -->
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">Confirmation</h4>
					</div>
					<div class="modal-body">
						<p>
							Are you sure to delete this data?
							<br>
							<span style="color: red;"><strong>Note:</strong> This action is unable to be undone in the future.</span>
						</p>
					</div>
					<div class="modal-footer" style="padding: 10px;">
						<a href="javascript:;" data-dismiss="modal" class="btn btn-default">Close</a>
						<button type="submit" class="btn btn-danger btn_submit">Confirm</button>
					</div>
				</form>
			</div>
		</div>
	</div>
<!--  -->
<div id="print_ajax_container_tradein" style="display:none;"></div><!-- AJAX -->
<a href="javascript:" id="return-to-top"><i class="fa fa-arrow-up"></i></a>

<script>
	$(window).scroll(function() {
	    if ($(this).scrollTop() >= 50) {        // If page is scrolled more than 50px
	        $('#return-to-top').fadeIn(200);    // Fade in the arrow
	    } else {
	        $('#return-to-top').fadeOut(200);   // Else fade out the arrow
	    }
	    });
	    $('#return-to-top').click(function() {      // When arrow is clicked
	        $('body,html').animate({
	            scrollTop : 0                       // Scroll to top of body
	        }, 500);
    });


	$(document).ready(function(){
		$("#edit_ajax_container .select2").select2();

		var UNIQID = $("#edit_ajax_container [name='UNIQID']").val();
		if(UNIQID != ""){
			$(".modal-body #UNIQID").val(UNIQID);
		}

		<?php if(isset($item_total)):?>
			$("#ITEM_TOTAL").val("<?php echo number_format((float)$item_total, 2, '.', '');?>");
		<?php endif;?>
		<?php if(!isset($item_total)):?>
			$("#ITEM_TOTAL").val((0).toFixed(2));
		<?php endif;?>

		<?php if(isset($item_gst)):?>
			$("#ITEM_GST").val("<?php echo number_format((float)$item_gst, 4, '.', '');?>");
		<?php endif;?>
		<?php if(!isset($item_gst)):?>
			$("#ITEM_GST").val((0).toFixed(2));
		<?php endif;?>

		<?php if(isset($item_amt)):?>
			$("#ITEM_AMT").val("<?php echo number_format((float)$item_amt, 2, '.', '');?>");
		<?php endif;?>
		<?php if(!isset($item_amt)):?>
			$("#ITEM_AMT").val((0).toFixed(2));
		<?php endif;?>

		calculate_gold_price();
		calculate_gst_gold();
		calculate_gold_amt();
		get_total_amount();
		check_bal();
		//find_tradein_gold();

        <?php if(isset($sub_array)):?>
			<?php foreach($sub_array as $sub):?>
		 		edit_sub_add();
		 		$(".edit_sub_tr_item:last [name='SUB_PKKEY[]']").val("<?php echo $sub->PKKEY;?>");
		 		$(".edit_sub_tr_item:last [name='SUB_ITEM_TITLE[]']").html("[<?php echo $sub->ITEM;?>] <?php echo $sub->ITEM_NAME;?>");
				$(".edit_sub_tr_item:last [name='SUB_ITEM[]']").val("<?php echo $sub->ITEM;?>");
				$(".edit_sub_tr_item:last [name='SUB_DESCRIPT[]']").val("<?php echo $sub->DESCRIPT;?>");
				$(".edit_sub_tr_item:last [name='SUB_DESCRIPT_TITLE[]']").html("<?php echo $sub->DESCRIPT;?>");
				$(".edit_sub_tr_item:last [name='SUB_INCH[]']").val("<?php echo $sub->INCH;?>");
				$(".edit_sub_tr_item:last [name='SUB_INCH_TITLE[]']").html("<?php echo $sub->INCH;?>");
				$(".edit_sub_tr_item:last [name='SUB_LENGTH[]']").val("<?php echo $sub->LENGTH;?>");
				$(".edit_sub_tr_item:last [name='SUB_PCS[]']").val("<?php echo $ci->fm->str_esc(trim($sub->PCS), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_PCS_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->PCS), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_QTY[]']").val( "<?php echo $ci->fm->str_esc(trim($sub->QTY), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_QTY_TITLE[]']").html( "<?php echo $ci->fm->str_esc(trim($sub->QTY), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_UNIT[]']").val("<?php echo $ci->fm->str_esc(trim($sub->UNIT), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_UNIT_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->UNIT), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_PRICE[]']").val("<?php echo $ci->fm->str_esc(trim($sub->PRICE), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_PRICE_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->PRICE), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_LABOUR[]']").val("<?php echo $ci->fm->str_esc(trim($sub->LABOUR), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_LABOUR_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->LABOUR), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_TAXCODE[]']").val( "<?php echo $ci->fm->str_esc(trim($sub->TAXCODE), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_TAXCODE_TITLE[]']").html( "<?php echo $ci->fm->str_esc(trim($sub->TAXCODE), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_PERCEN[]']").val("<?php echo $ci->fm->str_esc(trim($sub->PERCEN), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_PERCEN_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->PERCEN), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_GST[]']").val("<?php echo $ci->fm->str_esc(trim($sub->GST), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_GST_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->GST), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_AMT[]']").val("<?php echo $ci->fm->str_esc(trim($sub->AMT), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_AMT_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->AMT), 2);?>");

				var LABOUR_T = <?php echo $ci->fm->str_esc(trim($sub->LABOUR), 2);?>;

				<?php $index_array1 = array("INCLUSIVE");?>
				<?php foreach($index_array1 as $index):?>
					<?php if($sub->$index == "1"):?>
						var selector = $(".modal-body [name='SUB_INCLUSIVE[]']").closest("td").find("[type='checkbox']");
						$(selector).prop("checked", true);
						$(selector).change();
					<?php endif;?>
				<?php endforeach;?>

		 	<?php endforeach;?>
		<?php endif;?>

		<?php if(isset($tradein_sub_array)):?>
			<?php foreach($tradein_sub_array as $sub_tradein):?>
		 		add_sub_tradein();
		 		
		 		$(".edit_sub_tr:last [name='SUB_TRADEIN_PKKEY[]']").val("<?php echo $sub_tradein->PKKEY;?>");
		 		$(".edit_sub_tr:last [name='SUB_TRADEIN_GOLD[]']").select2("val","<?php echo $sub_tradein->GOLD;?>");
		 		$(".edit_sub_tr:last [name='SUB_TRADEIN_DESCRIPT[]']").val("<?php echo $sub_tradein->DESCRIPT;?>");
		 		$(".edit_sub_tr:last [name='SUB_TRADEIN_WEIGHT[]']").val("<?php echo $sub_tradein->WEIGHT;?>");
		 		$(".edit_sub_tr:last [name='SUB_TRADEIN_RATE[]']").val("<?php echo $sub_tradein->RATE;?>");
		 		$(".edit_sub_tr:last [name='SUB_TRADEIN_GOLDPRICE[]']").val("<?php echo $sub_tradein->GOLDPRICE;?>");
		 	<?php endforeach;?>
		<?php endif;?>

		resize_textarea("#edit_ajax_container textarea");

		$(".click_sub").click(function(){
			
		});
		
		<?php if(! $can_edit):?>
			$("#edit_ajax_container input").prop("disabled", true);
			$("#edit_ajax_container select").prop("disabled", true);
			$("#edit_ajax_container textarea").prop("disabled", true);
		<?php endif;?>

		<?php if(isset($data)):?>

			<?php if($can_delete):?>
				$(".btn_delete").show();
				$(".delete_title").show();
			<?php endif;?>

			$("#edit_div_action").show("fast");
            $("#delete_modal [name='REFNO']").val("<?php echo $data->REFNO;?>");
		<?php endif;?>

		<?php if($can_delete_tradein):?>
			$("#delete_tradein").show();
		<?php endif;?>
	});

	function add_sub_tradein(){
		var content = "";
		content += "<tr class='edit_sub_tr'>";

		content += 	"<td class='' style=''>";
		content += 		"<input type='hidden' name='SUB_TRADEIN_PKKEY[]'>";
		content +=         '<select name="SUB_TRADEIN_GOLD[]" class="form-control select2" required="required" onChange="calculate_tradein_gold(this);find_gold_subdetail(this);">';
		content +=           '<option value="">--- Select a value ---</option>';
	    <?php foreach($all_gold as $v):?>
	    content +=			 '<option value="<?php echo $v->CODE;?>">';
	    content +=           ' <?php echo "[".$v->CODE."] ".$v->NAME;?>';
	    content +=           '</option>';
	    <?php endforeach;?>
	    content +=         '</select>';
		content += 	"</td>";

		content += 	"<td class='' style=''>";
		content += 	"	<input type='text' maxlength='50' name='SUB_TRADEIN_DESCRIPT[]' id='SUB_TRADEIN_DESCRIPT' class='form-control' style='text-align: left;' autocomplete='OFF'/>";
		content += 	"</td>";

		content += 	"<td class='' style=''>";
		content += 	"	<div class='input-group'>"
		content += 	"		<input type='number' min='0' step='.0001' name='SUB_TRADEIN_WEIGHT[]' id='SUB_TRADEIN_WEIGHT' class='form-control' style='text-align: left;' onblur='calculate_tradein_gold(this);edit_total_tradein(this);' autocomplete='OFF'/>";
		content += 	"		<span class='input-group-addon'>gram</span>"
		content += 	" 	</div>"
		content += 	"</td>";

		content += 	"<td class='' style=''>";
		content += 		"<input type='number' min='0' step='.0001' name='SUB_TRADEIN_RATE[]' id='SUB_TRADEIN_RATE' class='form-control' style='text-align: left;' onblur='calculate_tradein_gold(this);edit_total_tradein(this);' autocomplete='OFF'/>";
		content += 	"</td>";

		content += 	"<td class='' style=''>";
		content += 	"	<div class='input-group'>"
		content += 	"		<input type='number' min='0' step='.0001' name='SUB_TRADEIN_GOLDPRICE[]' id='SUB_TRADEIN_GOLDPRICE' class='form-control' style='text-align: left;' autocomplete='OFF'/>";
		content += 	"		<span class='input-group-addon'>gram</span>"
		content += 	" 	</div>"
		content += 	"</td>";

		<?php if($can_delete):?>
		content += 	"<td style='text-align: center;'>";
		content += 	"<button type='button' onClick='edit_sub_remove_tradein(this);' class='btn btn-danger'>";
		content += 		"<i class='fa fa-trash-o'></i>";
		content += 	"</button>";
		content += 	"</td>";
		<?php endif;?>
		content += "</tr>";

		$("#edit_sub_tr_add").before(content);
		$(".edit_sub_tr:last .select2").select2();

	}

	function edit_sub_add(){

		var content = "";
		content += "<tr class='edit_sub_tr_item' style='cursor: pointer;'>";

		content += 	"<td style='white-space: nowrap; display:none;'>";
        content += 	"<input type='hidden' name='SUB_PKKEY[]'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_ITEM[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=   '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_ITEM_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_DESCRIPT[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_DESCRIPT_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_INCH[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_INCH_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_LENGTH[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_PCS[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_PCS_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_QTY[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_QTY_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_UNIT[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_UNIT_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_PRICE[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_PRICE_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_LABOUR[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_LABOUR_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_TAXCODE[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_TAXCODE_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_PERCEN[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_PERCEN_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_GST[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_GST_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_AMT[]' class='form-control' style='text-align: center; cursor: pointer; background-color: transparent; border-color: transparent;' readonly>";
        content +=      '</div>';
        content += 	"</td>";
        content += 	"<td style='white-space: nowrap; text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_AMT_TITLE[]'>";
        content += 	"</td>";
		
		<?php if($can_delete):?>
		content += 	"<td style='text-align: center;'>";
		content += 	"<button type='button' onClick='recalculate_item_amt(this);recalculate_item_gst(this);recalculate_item_labour(this);edit_sub_remove(this);' class='btn btn-danger'>";
		content += 		"<i class='fa fa-trash-o'></i>";
		content += 	"</button>";
		content += 	"</td>";
		<?php endif;?>
		content += "</tr>";

		$("#edit_sub_tr_add_item").before(content);
		$(".edit_sub_tr_item:last .select2").select2();
	}

	shortcut.add("Ctrl+Alt",function() {
    document.getElementById("edit_btn_submit").click();
    //alert("Saving...");
    },{
      'type':'keydown',
      'propagate':true,
      'target':document
    });

	//Datepicker
	var DATE = $("[name='DATE']").val();
	var PAYDATE = $("[name='PAYDATE']").val();

    if(DATE == ""){
        $("#edit_ajax_container .datepicker1").datepicker({
            format: "yyyy-mm-dd",
            autoclose: true,
            todayHighlight: true,
        }).datepicker("setDate", new Date());
    }else{
        $("#edit_ajax_container .datepicker1").datepicker({
            format: "yyyy-mm-dd",
            autoclose: true,
            todayHighlight: true,
          });
    }

    $("#edit_ajax_container .datepicker2").datepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        todayHighlight: true,
    });
   
    function check_main(){
    	var DATE = $("#DATE").val();
    	var DEBTOR = $("#DEBTOR").val();
    	var STAFF = $("#STAFF").val();
    	var TT = $("#TT").val();

    	if(DATE == "" || DEBTOR == "" || STAFF == "" || TT == ""){
    		prompt_error("Please fill out all fields marked by *asterisk*.");
    	}else{
    		edit_sub_detail();
    		fill_main();
    	}
    }

    function fill_main(){
    	var DATE = $("#DATE").val();
    	var DEBTOR = $("#DEBTOR").val();
    	var STAFF = $("#STAFF").val();
    	var TT = $("#TT").val();
    	var REMARKS = $("#REMARKS").val();

    	$("#SUB_DATE").val(DATE);
    	$("#SUB_DEBTOR").val(DEBTOR);
    	$("#SUB_STAFF").val(STAFF);
    	$("#SUB_TT").val(TT);
    	$("#SUB_REMARKS").val(REMARKS);
    }

    function check_main_tradein(){
    	var DATE = $("[name='DATE']").val();
    	var DEBTOR = $("[name='DEBTOR']").val();
    	var STAFF = $("[name='STAFF']").val();
    	var TT = $("[name='TT']").val();

    	if(DATE == "" || DEBTOR == "" || STAFF == "" || TT == ""){
    		prompt_error("Please fill out all fields marked by *asterisk*.");
    	}else{
    		trade_in();
    	}
    }

    function trade_in(){
    	$("#tradein_modal").modal('show');
    }

    function check_bal(){
    	var PAYAMT = parseFloat($("#PAYAMT").val());
		var AMOUNT = parseFloat($("#AMOUNT").val());

		if(AMOUNT != "" && PAYAMT != 0){
			var BAL = AMOUNT - PAYAMT;
			$("#BAL").val((BAL).toFixed(2));
		}else if(AMOUNT != "" && PAYAMT == 0 || PAYAMT == ""){
			$("#BAL").val((AMOUNT).toFixed(2));
		}else{
			$("#BAL").val((0).toFixed(2));
			$("#PAYAMT").val((0).toFixed(2));
		}
    }

    function find_tradein_detail(t){
		var id = $("#TRADEIN_REFNO").val();

        if(id !=""){
            var params = "id=" + id;

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_tradein_detail",
                data: params,
                success: function(result, status, xhr){
                    //log(JSON.parse(result));
                    var info, i;
                    info = JSON.parse(result);
                    
                    for (i = 0; i < info.length; i++){
                    	$("#DEBTOR_GOLD").val(info[i].BAL.trim());
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
        	$("#DEBTOR_GOLD").val('');
        }
    }

    function find_gold_detail(t){
		var id = $("#GOLD").val();

        if(id !=""){
            var params = "id=" + id;

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_gold_detail",
                data: params,
                success: function(result, status, xhr){
                    //log(JSON.parse(result));
                    var info, i;
                    info = JSON.parse(result);
                    
                    for (i = 0; i < info.length; i++) {
                    	$("#TAXCODE").select2("val",info[i].TAXCODE.trim());
                    	$("#PERCEN").val(info[i].PERCEN.trim());
                    	if(info[i].RATE != null){
                    		$("#RATE").val(info[i].RATE.trim());
                    		calculate_puregold();
                    		calculate_gold_price();
                    	}else{
                    		$("#RATE").val(null);
                    		calculate_puregold();
                    		calculate_gold_price();
                    	}

                    	if(info[i].GOLD_BAL != null){
                    		$("#GOLD_BAL").val(info[i].GOLD_BAL.trim());
                    	}else{
                    		$("#GOLD_BAL").val(null);    		
                    	}
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
        	$("#TAXCODE").select2("val",'');
        	$("#PERCEN").val('');
        	$("#RATE").val('');
        }
    }

  //   function calculate_gold_balance(){
  //   	var WEIGHT = parseFloat($("#WEIGHT").val());
  //   	var BAL = parseFloat($("#GOLD_BAL").val());
  //   	var TRADE_IN_TOTAL = parseFloat($("#TRADE_IN_TOTAL").val());
  //   	var DEBTOR_GOLD = parseFloat($("#DEBTOR_GOLD").val());

		// if(BAL >= WEIGHT && !isNaN(DEBTOR_GOLD)){
		// 	if(DEBTOR_GOLD > WEIGHT){
		// 		var BALANCE = ((DEBTOR_GOLD-WEIGHT)).toFixed(2);
		// 		$("#DEBTOR_GOLD").val(BALANCE);
		// 		calculate_puregold();
		// 		calculate_gold_price();
		// 		calculate_gold_amt();
		// 	}else if(DEBTOR_GOLD <= WEIGHT){
		// 		var BALANCE = parseFloat((DEBTOR_GOLD-WEIGHT).toFixed(2));
		// 		if(BALANCE <= 0){
		// 			var FINAL_BALANCE = BAL+(BALANCE);
		// 			$("#DEBTOR_GOLD").val((0).toFixed(2));
		// 			$("#GOLD_BAL").val(FINAL_BALANCE);
		// 			calculate_puregold();
		// 			calculate_gold_price();
		// 			calculate_gold_amt();
		// 		}else{
		// 			$("#DEBTOR_GOLD").val((BALANCE).toFixed(2));
		// 			calculate_puregold();
		// 			calculate_gold_price();
		// 			calculate_gold_amt();
		// 		}
		// 	}
		// }else if(BAL >= WEIGHT && isNaN(DEBTOR_GOLD)){
		// 	var BALANCE = ((BAL-WEIGHT)).toFixed(2);
		// 	$("#GOLD_BAL").val(BALANCE);
		// 	calculate_puregold();
		// 	calculate_gold_price();
		// 	calculate_gold_amt();
		// }else{
		// 	prompt_error("Insufficient Stock !");
		// 	$("#WEIGHT").val((0).toFixed(2));
		// 	calculate_puregold();
		// 	calculate_gold_price();
		// 	calculate_gold_amt();
		// }
  //   }

    function find_item_image(){
    	var id = $("#SUB_ITEM").val();

    	if(id != ""){
    		$.ajax({
	            type: "POST",
	            url: "<?php echo base_url($this->router->fetch_class());?>/load_item_images",
	            data:"id="+id,
	            success:function(result, status, xhr)
	            {
	   				var api = "<?php echo base_url("api/get_attach/");?>"
	   				var path = api + "/" +result;
					var src = "";
					src += '<img src="'+path+'" style="width: auto;height: 200px;">';
					document.getElementById("container").innerHTML = src;

					if(result != ""){
						$("#item_image").show();
					}else{
						$("#item_image").hide();
					}
				}
	        });
    	}else{
    		$("#item_image").hide();
    	}
    }

    function find_item_detail(t){
		var id = $("#SUB_ITEM").val();

        if(id !=""){
            var params = "id=" + id;

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_item_detail",
                data: params,
                success: function(result, status, xhr){
                    //log(JSON.parse(result));
                    var info, i;
                    info = JSON.parse(result);
                    
                    for (i = 0; i < info.length; i++) {
                    	$(".modal-body #SUB_PRICE").val(info[i].SALES_AMT.trim());
                    	$(".modal-body #SUB_DESCRIPT").val(info[i].DESCRIPT.trim());
                    	$(".modal-body #SUB_TAXCODE").select2("val",info[i].TAXCODE.trim());
                    	$(".modal-body #SUB_PERCEN").val(info[i].PERCEN.trim());
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
            $(".modal-body #SUB_PRICE").val('');
        	$(".modal-body #SUB_DESCRIPT").val('');
        	$(".modal-body #SUB_TAXCODE").select2("val",'');
        	$(".modal-body #SUB_PERCEN").val('');
        }
    }

    function find_gold_subdetail(t){
    	var tr = $(t).closest("tr");
		var id = $(tr).find("[name='SUB_TRADEIN_GOLD[]']").val();

        if(id !=""){
            var params = "id=" + id;

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_gold_detail",
                data: params,
                success: function(result, status, xhr){
                    //log(JSON.parse(result));
                    var info, i;
                    info = JSON.parse(result);
                    
                    for (i = 0; i < info.length; i++) {
                    	$(tr).find("[name='SUB_TRADEIN_DESCRIPT[]']").val(info[i].DESCRIPT.trim());
                    	$(tr).find("[name='SUB_TRADEIN_RATE[]']").val(info[i].RATE.trim());
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
        	$(tr).find("[name='SUB_TRADEIN_DESCRIPT[]']").val('');
        	$(tr).find("[name='SUB_TRADEIN_RATE[]']").val('');
        }
    }

    function calculate_qty(){
    	var INCH = $("#SUB_INCH").val();
    	var PCS = $("#SUB_PCS").val();

    	if(INCH != "" && PCS != ""){
    		var QTY = (INCH*PCS).toFixed(2);
    		$("#SUB_QTY").val(QTY);
    		calculate_labour();
    	}
    }

    function calculate_labour(){
    	var QTY = $("#SUB_QTY").val();
    	var PRICE = $("#SUB_PRICE").val();
    	var INCLUSIVE = $("#SUB_INCLUSIVE").val();

    	if(QTY != "" && PRICE != ""){
    		var LABOUR = (QTY*PRICE).toFixed(2);
    		$("#SUB_LABOUR").val(LABOUR);
    		calculate_gst();
    	}
    }

    function calculate_puregold(){
    	var GOLD = $("[name='GOLD']").val();
    	var WEIGHT = $("[name='WEIGHT']").val();
    	var DEBTOR_GOLD = $("[name='DEBTOR_GOLD']").val();

    	if(DEBTOR_GOLD != ""){
    		var PUREGOLD = ((GOLD*WEIGHT)/1000).toFixed(2);
    		$("[name='PUREGOLD']").val(PUREGOLD);
    		calculate_gold_balance();
    	}else{
    		var PUREGOLD = ((GOLD*WEIGHT)/1000).toFixed(2);
    		$("[name='PUREGOLD']").val(PUREGOLD);
    		calculate_gold_price();
    	}
    }

    function calculate_gold_balance(){
    	var WEIGHT = parseFloat($("#WEIGHT").val());
    	var DEBTOR_GOLD = parseFloat($("#DEBTOR_GOLD").val());
    	var RATE = parseFloat($("#RATE").val());
    	var PUREGOLD = parseFloat($("[name='PUREGOLD']").val());

    	if(DEBTOR_GOLD != ""){
    		var BALANCE = ((PUREGOLD-DEBTOR_GOLD)).toFixed(2);
    		if(BALANCE > 0){
    			$("#GOLD_BAL").val(BALANCE);
    			$("#DEBTOR_GOLD").val((0).toFixed(2));
    			calculate_gold_price();
    		}else if(BALANCE <= 0){
    			var ABS_BALANCE = Math.abs(BALANCE);
    			//$("#GOLD_BAL").val((PUREGOLD).toFixed(2));
    			$("#DEBTOR_GOLD").val(ABS_BALANCE);
    			calculate_gold_price();
    		}
    	}
    }

    // function calculate_gold_price(){
    // 	var PUREGOLD = parseFloat($("[name='PUREGOLD']").val());
    // 	var RATE = $("[name='RATE']").val();
    // 	var TRADE_IN_TOTAL = parseFloat($("[name='TRADE_IN_TOTAL']").val());

    // 	if(PUREGOLD > TRADE_IN_TOTAL && PUREGOLD != "" && RATE != "" && TRADE_IN_TOTAL != ""){
    // 		var GOLDPRICE = ((PUREGOLD-TRADE_IN_TOTAL)*RATE).toFixed(2);
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);
    // 		calculate_gst_gold();console.log('1');

    // 	}else if(PUREGOLD > TRADE_IN_TOTAL && PUREGOLD != "" && RATE != "" && TRADE_IN_TOTAL == ""){
    // 		var GOLDPRICE = (PUREGOLD*RATE).toFixed(2);
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);console.log('2');
    // 		calculate_gst_gold();
    // 	}else if(PUREGOLD > TRADE_IN_TOTAL && PUREGOLD != "" && RATE == "" && TRADE_IN_TOTAL != ""){
    // 		var GOLDPRICE = (PUREGOLD-TRADE_IN_TOTAL);
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);console.log('3');
    // 		calculate_gst_gold();

    // 	}else if(PUREGOLD > TRADE_IN_TOTAL && PUREGOLD != "" && RATE == "" && TRADE_IN_TOTAL == ""){
    // 		var GOLDPRICE = PUREGOLD;
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);console.log('4');
    // 		calculate_gst_gold();
    // 	}else if(PUREGOLD < TRADE_IN_TOTAL && PUREGOLD != "" && RATE != "" && TRADE_IN_TOTAL != ""){
    // 		var DEBTOR_GOLD = TRADE_IN_TOTAL-PUREGOLD;
	   //  	var GOLDPRICE = (PUREGOLD*RATE).toFixed(2);
	   //  	$("[name='DEBTOR_GOLD']").val((DEBTOR_GOLD).toFixed(2));
	   //  	$("[name='GOLDPRICE']").val(GOLDPRICE);
	   //  	$("[name='GOLD_TOTAL']").val(GOLDPRICE);console.log('5');
    // 		calculate_gst_gold();

    // 	}else if(PUREGOLD <  TRADE_IN_TOTAL && PUREGOLD != "" && RATE != "" && TRADE_IN_TOTAL == ""){
    // 		var GOLDPRICE = (PUREGOLD*RATE).toFixed(2);
    // 		var DEBTOR_GOLD = TRADE_IN_TOTAL-PUREGOLD;
    // 		$("[name='DEBTOR_GOLD']").val((DEBTOR_GOLD).toFixed(2));
    // 		$("[name='GOLDPRICE']").val((GOLDPRICE).toFixed(2));console.log('6');
    // 		$("[name='GOLD_TOTAL']").val((GOLDPRICE).toFixed(2));
    // 		calculate_gst_gold();
    // 	}else if(PUREGOLD <  TRADE_IN_TOTAL && PUREGOLD != "" && RATE == "" && TRADE_IN_TOTAL != ""){
    // 		var GOLDPRICE = PUREGOLD;
    // 		var DEBTOR_GOLD = TRADE_IN_TOTAL-PUREGOLD;
    // 		$("[name='DEBTOR_GOLD']").val((DEBTOR_GOLD).toFixed(2));
    // 		$("[name='GOLDPRICE']").val((GOLDPRICE).toFixed(2));console.log('7');
    // 		$("[name='GOLD_TOTAL']").val((GOLDPRICE).toFixed(2));
    // 		calculate_gst_gold();

    // 	}else if(PUREGOLD <  TRADE_IN_TOTAL && PUREGOLD != "" && RATE == "" && TRADE_IN_TOTAL == ""){
    // 		var GOLDPRICE = PUREGOLD;
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);console.log('8');
    // 		calculate_gst_gold();
    // 	}else{
    // 		$("[name='GOLDPRICE']").val((0).toFixed(2));
    // 		$("[name='GOLD_TOTAL']").val((0).toFixed(2));console.log('9');
    // 	}
    // }

    // function calculate_gold_price1(){
    // 	var PUREGOLD = parseFloat($("[name='PUREGOLD']").val());
    // 	var RATE = $("[name='RATE']").val();
    // 	var TRADE_IN_TOTAL = parseFloat($("[name='TRADE_IN_TOTAL']").val());
    // 	var DEBTOR_GOLD = parseFloat($("[name='DEBTOR_GOLD']").val());

    // 	if(DEBTOR_GOLD != "" && DEBTOR_GOLD < PUREGOLD){
    // 		if(RATE != ""){
	   //  		var GOLDPRICE = ((PUREGOLD-DEBTOR_GOLD)*RATE).toFixed(2);
	   //  	}else{
	   //  		var GOLDPRICE = (PUREGOLD-DEBTOR_GOLD).toFixed(2);
	   //  	}
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);
    // 		//$("[name='DEBTOR_GOLD']").val((0).toFixed(2));
    // 		calculate_gst_gold();

    // 	}else if(DEBTOR_GOLD != "" && DEBTOR_GOLD > PUREGOLD){
    // 		var DEBTOR_GOLD = DEBTOR_GOLD-PUREGOLD;
    // 		//$("[name='DEBTOR_GOLD']").val((DEBTOR_GOLD).toFixed(2));
    // 		$("[name='GOLDPRICE']").val((0).toFixed(2));
    // 		$("[name='GOLD_AMT']").val((0).toFixed(2));
    // 		calculate_gst_gold();
    // 		calculate_gold_amt();
    // 		get_total_amount();
    		    	
    // 	}else if(isNaN(DEBTOR_GOLD)){
    // 		if(RATE != ""){
	   //  		var GOLDPRICE = (PUREGOLD*RATE).toFixed(2);
	   //  	}else{
	   //  		var GOLDPRICE = (PUREGOLD).toFixed(2);
	   //  	}
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);
    // 		calculate_gst_gold();
    // 	}
    // }

    function calculate_gold_price(){
    	var WEIGHT = parseFloat($("#WEIGHT").val());
    	var DEBTOR_GOLD = parseFloat($("#DEBTOR_GOLD").val());
    	var RATE = parseFloat($("#RATE").val());
    	var PUREGOLD = parseFloat($("[name='PUREGOLD']").val());
    	var RATE = parseFloat($("[name='RATE']").val());

    	if(!isNaN(DEBTOR_GOLD)){
    		var BALANCE = ((PUREGOLD-DEBTOR_GOLD)).toFixed(2);
    		if(BALANCE > 0){
	    		var GOLDPRICE = BALANCE*RATE;log(GOLDPRICE);
	    		$("[name='GOLDPRICE']").val(GOLDPRICE);
	    		$("[name='GOLD_AMT']").val(GOLDPRICE);
	    	}else if(BALANCE <= 0){
	    		$("[name='GOLDPRICE']").val((0).toFixed(2));
	    		$("[name='GOLD_AMT']").val((0).toFixed(2));
	    	}
    	}else{
    		if(!isNaN(RATE)){log(RATE);
				var GOLDPRICE = (PUREGOLD*RATE).toFixed(2);
	    		$("[name='GOLDPRICE']").val(GOLDPRICE);
	    		$("[name='GOLD_AMT']").val(GOLDPRICE);
	    	}else{
	    		var GOLDPRICE = (PUREGOLD).toFixed(2);
	    		$("[name='GOLDPRICE']").val(GOLDPRICE);
	    		$("[name='GOLD_AMT']").val(GOLDPRICE);
	    	}
    	}

    }

    // function calculate_gold_price(){
    // 	var PUREGOLD = parseFloat($("[name='PUREGOLD']").val());
    // 	var RATE = $("[name='RATE']").val();

    // 	if(RATE != ""){
    // 		var GOLDPRICE = (PUREGOLD*RATE).toFixed(2);
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);
    // 		calculate_gst_gold();
    // 	}else{
    // 		var GOLDPRICE = (PUREGOLD).toFixed(2);
    // 		$("[name='GOLDPRICE']").val(GOLDPRICE);
    // 		$("[name='GOLD_TOTAL']").val(GOLDPRICE);
    // 		calculate_gst_gold();
    // 	}
    // }

    function calculate_payamt(){
    	var CASHAMT = parseFloat($("[name='CASHAMT']").val());
    	var BCAMT = parseFloat($("[name='BCAMT']").val());

    	if(CASHAMT != "" && isNaN(BCAMT)){
    		$("[name='PAYAMT']").val((CASHAMT).toFixed(2));
    		check_bal();
    	}else if(BCAMT != "" && isNaN(CASHAMT)){
    		$("[name='PAYAMT']").val((BCAMT).toFixed(2));
    		check_bal();
    	}else if(CASHAMT != "" && BCAMT != ""){
    		var PAYAMT = (CASHAMT+BCAMT).toFixed(2);
    		$("[name='PAYAMT']").val(PAYAMT);
    		check_bal();
    	}else{
    		$("[name='PAYAMT']").val((0).toFixed(2));
    	}
    }

    function calculate_gst(){
    	var SUB_LABOUR = parseFloat($("#SUB_LABOUR").val());
    	var SUB_PERCEN = parseFloat($("#SUB_PERCEN").val());

    	if(SUB_LABOUR != "" || SUB_PERCEN != ""){
    		var SUB_GST = ((SUB_LABOUR*SUB_PERCEN)/100).toFixed(4);
    		$("#SUB_GST").val(SUB_GST);
    		calculate_amt()
    	}
    }

    function calculate_gst_gold(){
    	var GOLDPRICE = parseFloat($("#GOLDPRICE").val());
    	var PERCEN = parseFloat($("#PERCEN").val());

    	if(GOLDPRICE != "" && PERCEN != ""){
    		var GST = ((GOLDPRICE*PERCEN)/100).toFixed(4);
    		$("#GST").val(GST);
    		$("#GOLD_GST").val(GST);
    		calculate_gold_amt();
    	}else{
    		$("#GST").val((0).toFixed(4));
    		$("#GOLD_GST").val((0).toFixed(4));
    	}
    }

    function calculate_gold_amt(){
    	var GOLDPRICE = parseFloat($("#GOLDPRICE").val());
    	var GST = parseFloat($("#GST").val());

    	if(GOLDPRICE != "" || GST != ""){
    		var GOLD_AMT = (GOLDPRICE+GST).toFixed(2);
    		$("#GOLD_AMT").val(GOLD_AMT);
    		$("#GOLD_TOTAL").val((GOLDPRICE).toFixed(2));
    		$("#GOLD_GST").val((GST).toFixed(4));
    		get_total_amount();
    	}else{
    		$("#GOLD_AMT").val((0).toFixed(2));
    		$("#GOLD_TOTAL").val((0).toFixed(2));
    		$("#GOLD_GST").val((0).toFixed(4));
    	}
    }

     function get_total_amount(){
     	var GOLD_AMT = parseFloat($("#GOLD_AMT").val());
		var ITEM_AMT = parseFloat($("#ITEM_AMT").val());

		if(GOLD_AMT != "" && ITEM_AMT != ""){
			var TOTAL_AMT = GOLD_AMT + ITEM_AMT
			$("#TOTAL_AMT").val((TOTAL_AMT).toFixed(2));
			$("#AMOUNT").val((TOTAL_AMT).toFixed(2));
			check_bal();
		}else if(GOLD_AMT != "" && ITEM_AMT == 0){
			$("#TOTAL_AMT").val((GOLD_AMT).toFixed(2));
			$("#AMOUNT").val((GOLD_AMT).toFixed(2));
			check_bal();
		}else if(GOLD_AMT == 0 && ITEM_AMT != ""){
			$("#TOTAL_AMT").val((ITEM_AMT).toFixed(2));
			$("#AMOUNT").val((ITEM_AMT).toFixed(2));
			check_bal();
		}else{
			$("#TOTAL_AMT").val((0).toFixed(2));
			$("#AMOUNT").val((0).toFixed(2));
		}
	}

    function calculate_amt(){
    	var SUB_LABOUR = parseFloat($("#SUB_LABOUR").val());
    	var SUB_GST = parseFloat($("#SUB_GST").val());

    	if(SUB_LABOUR != "" || SUB_GST != ""){
    		var SUB_AMT = (SUB_LABOUR+SUB_GST).toFixed(2);
    		$("#SUB_AMT").val(SUB_AMT);
    	}
    }

    function calculate_tradein_gold(t){
    	var tr = $(t).closest("tr");
    	var gold = $(tr).find("[name='SUB_TRADEIN_GOLD[]']").val();
		var weight = $(tr).find("[name='SUB_TRADEIN_WEIGHT[]']").val();
		var rate = $(tr).find("[name='SUB_TRADEIN_RATE[]']").val();

		if(weight != "" && rate != "" && gold != ""){
			var goldprice = ((weight*rate)/1000).toFixed(2);
			$(".edit_sub_tr:last [name='SUB_TRADEIN_GOLDPRICE[]']").val(goldprice);
		}
    }

    function edit_total_tradein(){
		var sum = 0;
		$(".edit_sub_tr [name='SUB_TRADEIN_GOLDPRICE[]']").each(function(index, element){
			sum += parseFloat($(this).val());
			$(".modal-body #SUB_TRADEIN_TOTAL_TRADEIN").html(number_format(sum, 2));
			$(".modal-body #SUB_TRADEIN_TOTAL_TRADEIN").val(number_format(sum, 2));
		});
	}

	function recalculate_item_labour(btn){
		var tr = $(btn).closest("tr");
		var old_total = $(tr).find("[name='SUB_LABOUR[]']").val();
		var pkkey = $(tr).find("[name='SUB_PKKEY[]']").val();
		var first_pkkey = $(".edit_sub_tr_item:first [name='SUB_PKKEY[]']").val();
		var item_total = "<?php if(isset($item_total)) echo number_format((float)$item_total, 2, '.', '');?>";
		var GOLD_AMT = $("#GOLD_AMT").val();
		
		if(old_total != "" && pkkey != first_pkkey){
			var new_item_total = item_total-old_total;
			$("#ITEM_TOTAL").val(new_item_total.toFixed(2));
		}else{
			$("#ITEM_TOTAL").val(0);
			$("#TOTAL_AMT").val(GOLD_AMT);
			$("#AMOUNT").val(GOLD_AMT);
			calculate_payamt();
		}
	}

	function recalculate_item_gst(btn){
		var tr = $(btn).closest("tr");
		var old_gst = $(tr).find("[name='SUB_GST[]']").val();
		var pkkey = $(tr).find("[name='SUB_PKKEY[]']").val();
		var first_pkkey = $(".edit_sub_tr_item:first [name='SUB_PKKEY[]']").val();
		var item_gst = "<?php if(isset($item_gst)) echo number_format((float)$item_gst, 4, '.', '');?>";
		var GOLD_AMT = $("#GOLD_AMT").val();
		
		if(old_gst != "" && pkkey != first_pkkey){
			var new_item_gst = item_gst-old_gst;
			$("#ITEM_GST").val(new_item_gst.toFixed(4));
		}else{
			$("#ITEM_GST").val(0);
			$("#TOTAL_AMT").val(GOLD_AMT);
			$("#AMOUNT").val(GOLD_AMT);
		}
	}

	function recalculate_item_amt(btn){
		var tr = $(btn).closest("tr");
		var old_amt = $(tr).find("[name='SUB_AMT[]']").val();
		var pkkey = $(tr).find("[name='SUB_PKKEY[]']").val();
		var first_pkkey = $(".edit_sub_tr_item:first [name='SUB_PKKEY[]']").val();
		var item_amt = "<?php if(isset($item_amt)) echo number_format((float)$item_amt, 2, '.', '');?>";
		var GOLD_AMT = $("#GOLD_AMT").val();
		
		if(old_amt != "" && pkkey != first_pkkey){
			var new_item_amt = item_amt-old_amt;
			$("#ITEM_AMT").val(new_item_amt.toFixed(2));
			get_total_amount();
		}else{
			$("#ITEM_AMT").val(0);
			$("#TOTAL_AMT").val(GOLD_AMT);
			$("#AMOUNT").val(GOLD_AMT);
		}
	}


	function edit_item_checkbox_set_value(selector){
	 	var value = $(selector).is(":checked")? 1:0;
		$(selector).closest("td").find(".hidden_checkbox_value").val(value);
		$(selector).closest("div").find(".hidden_checkbox_value").val(value);
		$(selector).find(".hidden_checkbox_value").val(value);
	}

	function find_tax(t){
		var SUB_TAXCODE  = $(".modal-body #SUB_TAXCODE").val();

        if(SUB_TAXCODE !=""){
            var params = "SUB_TAXCODE=" + SUB_TAXCODE;

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_tax",
                data: params,
                success: function(result, status, xhr){
                    //log(result);
                    if(result !=""){
                        $(".modal-body #SUB_PERCEN").val(result);
                        calculate_gst()
                    }else{
                       $(".modal-body #SUB_PERCEN").val('');
                       $(".modal-body #SUB_GST").val('');
                       $(".modal-body #SUB_AMT").val('');
                    }
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
            $(".modal-body #SUB_PERCEN").val('');
            $(".modal-body #SUB_GST").val('');
            $(".modal-body #SUB_AMT").val('');

        }
    }

    function find_tax_gold(t){
		var TAXCODE  = $("#TAXCODE").val();

        if(TAXCODE !=""){
            var params = "TAXCODE=" + TAXCODE;

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_tax_gold",
                data: params,
                success: function(result, status, xhr){
                    // log(result);
                    if(result !=""){
                        $("#PERCEN").val(result);
                        calculate_gst_gold()
                    }else{
                       $("#PERCEN").val('');
                       $("#GST").val('');
                    }
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
            $("#PERCEN").val('');
            $("#GST").val('');

        }
    }

  //   function find_debtor_gold(t){
		// var DEBTOR  = $("#DEBTOR").val();
		// var DEBTOR_GOLD = parseFloat($("[name='DEBTOR_GOLD']").val());

  //       if(DEBTOR !=""){
  //           var params = "DEBTOR=" + DEBTOR;

  //           $.ajax({
  //               type: "POST",
  //               url: "<?php echo base_url($this->router->fetch_class());?>/get_debtor_gold",
  //               data: params,
  //               success: function(result, status, xhr){
  //                   //log(result);
  //                   if(result !=""){
  //                       $("#DEBTOR_GOLD").val(result);
  //                       $("#tradein_button").hide();
  //                       calculate_gold_price();
  //                   }else{
  //                      $("#DEBTOR_GOLD").val('');
  //                      calculate_gold_price();
  //                   }
  //               },
  //               error: function(xhr,status,error){},
  //               complete: function(xhr,status){},
  //           });
  //       }else{
  //           $("#DEBTOR_GOLD").val('');
  //           calculate_gold_price();

  //       }
  //   }

    function find_debtor_gold(t){
    	var id  = $("#DEBTOR").val();
		var DEBTOR_GOLD = parseFloat($("[name='DEBTOR_GOLD']").val());

        if(id !=""){
            var params = "id=" + id;

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_debtor_gold",
                data: params,
                success: function(result, status, xhr){
                    //log(JSON.parse(result));
                    var info, i;
                    info = JSON.parse(result);
                    
                    for (i = 0; i < info.length; i++) {
                    	$("#DEBTOR_GOLD").val(info[i].DEBTOR_GOLD);
                    	if(info[i].DEBTOR_GOLD != 0){
                    		//$("#tradein_button").hide();
                    		$("#DEBTOR_TRADEIN").val(info[i].TRADEIN_REFNO);
                    	}else{
                    		$("#DEBTOR_TRADEIN").val('');
                    	}
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
        	$("#DEBTOR_GOLD").val('');
        	$("#DEBTOR_TRADEIN").val('');
        }
    }

    function print_tradein() {
    	var id = $("#tradein_refno").val();

    	$.ajax({
			type: "POST",
			url: "<?php echo base_url($this->router->fetch_class());?>/load_print_tradein",
            data: "REFNO=" + id,
			success: function(result, status, xhr){
				browser_print(result);
			},
			error: function(xhr,status,error){
				$("#print_ajax_container_tradein").html("<?php echo $this->text_ajax_fail;?>");
			},
			complete: function(xhr,status){
			},
		});
    }

	function edit_sub_detail(btn){
		var tr = $(btn).closest("tr");
		var sub_pkkey = $(tr).find("[name='SUB_PKKEY[]']").val();
		var sub_item = $(tr).find("[name='SUB_ITEM[]']").val();
		var sub_descript = $(tr).find("[name='SUB_DESCRIPT[]']").val();
		var sub_inch = $(tr).find("[name='SUB_INCH[]']").val();
		var sub_length = $(tr).find("[name='SUB_LENGTH[]']").val();
		var sub_pcs = $(tr).find("[name='SUB_PCS[]']").val();
		var sub_qty = $(tr).find("[name='SUB_QTY[]']").val();
		var sub_unit = $(tr).find("[name='SUB_UNIT[]']").val();
		var sub_price = $(tr).find("[name='SUB_PRICE[]']").val();
		var sub_labour = $(tr).find("[name='SUB_LABOUR[]']").val();
		var sub_taxcode = $(tr).find("[name='SUB_TAXCODE[]']").val();
		var sub_percen = $(tr).find("[name='SUB_PERCEN[]']").val();
		var sub_gst = $(tr).find("[name='SUB_GST[]']").val();
		var sub_amt = $(tr).find("[name='SUB_AMT[]']").val();
		var sub_inclusive = $(tr).find("[name='SUB_INCLUSIVE[]']").val();

		$("#Sub_modal").modal('show');
		$(".modal-body #SUB_PKKEY").val(sub_pkkey);
		$(".modal-body #SUB_ITEM").select2('val',sub_item);
		$(".modal-body #SUB_DESCRIPT").val(sub_descript);
		$(".modal-body #SUB_INCH").val(sub_inch);
		$(".modal-body #SUB_LENGTH").val(sub_length);
		$(".modal-body #SUB_PCS").val(sub_pcs);
		$(".modal-body #SUB_QTY").val(sub_qty);
		$(".modal-body #SUB_UNIT").select2('val',sub_unit);
		$(".modal-body #SUB_PRICE").val(sub_price);
		$(".modal-body #SUB_LABOUR").val(sub_labour);
		$(".modal-body #SUB_TAXCODE").select2('val',sub_taxcode);
		$(".modal-body #SUB_PERCEN").val(sub_percen);
		$(".modal-body #SUB_GST").val(sub_gst);
		$(".modal-body #SUB_AMT").val(sub_amt);

		if(sub_inclusive == 1){
			$(".modal-body #SUB_INCLUSIVE").prop( "checked", true );
		}else{
			$(".modal-body #SUB_INCLUSIVE").prop( "checked", false );
		}

	}

	function calculate_sum(){
		var sum = 0; var bal = 0;

		var current = parseFloat($(".modal-body #SUB_AMT").val());
		var old = parseFloat($(".modal-body #item_amt1").val());

		var current_gst = parseFloat($(".modal-body #SUB_GST").val());
		var old_gst = parseFloat($(".modal-body #item_gst1").val());

		var current_total = parseFloat($(".modal-body #SUB_LABOUR").val());
		var old_total = parseFloat($(".modal-body #item_total1").val());

		var GOLD_AMT = parseFloat($("#GOLD_AMT").val());
		var PAYAMT = parseFloat($("#PAYAMT").val());

		if(current != ""){
			sum = current + old;
			sum_gst = current_gst + old_gst;
			sum_total = current_total + old_total;
			$("#ITEM_AMT").val(sum.toFixed(2));
			$("#ITEM_GST").val(sum_gst.toFixed(2));
			$("#ITEM_TOTAL").val(sum_total.toFixed(2));
			$(".modal-body #AMOUNT").val(sum + GOLD_AMT);
			$("#AMOUNT").val(sum + GOLD_AMT);
			$("#TOTAL_AMT").val(sum + GOLD_AMT);

			bal = (sum + GOLD_AMT)-PAYAMT;
			$("#BAL").val(bal);
			$(".modal-body #BAL").val(bal);
		}
		
		$("#sub_form").submit();
	}

	function edit_sub_remove(btn){
		var tr = $(btn).closest("tr");
		var pkkey = $(tr).find("[name='SUB_PKKEY[]']").val();
		var first_pkkey = $(".edit_sub_tr_item:first [name='SUB_PKKEY[]']").val();
		var x = confirm("Are you sure you want to delete this data ?");
		var GOLD_AMT = $("#GOLD_AMT").val();
		
		if(pkkey != "" && x){	
			var content = "<input type='hidden' name='DEL_SUB_PKKEY[]' value='" + pkkey + "'>";
			$(btn).closest("form").append(content);
			if(pkkey == first_pkkey){
				$("#ITEM_TOTAL").html(0);
				$("#ITEM_GST").html(0);
				$("#ITEM_AMT").html(0);
				$("#TOTAL_AMT").html(GOLD_AMT);
				$("#AMOUNT").val(GOLD_AMT);
			}
			$(tr).remove();
		}else if(pkkey == "" && x){	
			if(pkkey == first_pkkey){
				$("#ITEM_TOTAL").html(0);
				$("#ITEM_GST").html(0);
				$("#ITEM_AMT").html(0);
				$("#TOTAL_AMT").html(GOLD_AMT);
				$("#AMOUNT").val(GOLD_AMT);
			}
			$(tr).remove();
		}else{
			return false;
		}

	}

	function edit_sub_remove_tradein(btn){
		var tr = $(btn).closest("tr");
		var pkkey = $(tr).find("[name='SUB_TRADEIN_PKKEY[]']").val();
		var first_pkkey = $(".edit_sub_tr:first [name='SUB_TRADEIN_PKKEY[]']").val();
		var x = confirm("Are you sure you want to delete this data ?");
		
		if(pkkey != "" && x){	
			var content = "<input type='hidden' name='DEL_SUB__TRADEIN_PKKEY[]' value='" + pkkey + "'>";
			$(btn).closest("form").append(content);
			$(tr).remove();
			if(pkkey == first_pkkey){
				$(".modal-body #SUB_TRADEIN_TOTAL_TRADEIN").html(0);
			}
			edit_total_tradein();
		}else if(pkkey == "" && x){	
			$(tr).remove();
			if(pkkey == first_pkkey){
				$(".modal-body #SUB_TRADEIN_TOTAL_TRADEIN").html(0);
			}
			edit_total_tradein();
		}else{
			return false;
		}

	}

	function edit_validation(form){
		var error = "";
		
		$(form).find("[required]").each(function(){
			if($(this).val() == ""){
				if(error != "") error += "<br>";
				switch($(this).attr("name")){
					case "NAME":
						error += "Please fill in the Name!";
						break;
					default:
						error += "Please complete the Form!";
						break;
				}
				
				return false;
			}
		});

	}

</script>