<?php $ci = get_instance();?>
<style type="text/css">
	.transparent_input{
		background-color: transparent; 
		border-color: transparent;
	}
</style>
<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(this);">
			                    <option value="select">--- 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'";
			                        	}else if(!isset($data) && $v->TT == 'I'){
											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:70%;">
						<thead>
							<tr>
								<th style=" width: 100px;text-align: center;">Item<br/>Code <span style="color: red;"></span></th>
								<th style=" width: 50px;text-align: center;">Description <span style="color: red;"></span></th>
								<th style=" width: 50px;text-align: center;">Inch <span style="color: red;"></span></th>
								<th style=" width: 50px;text-align: center;">Pcs <span style="color: red;"></span></th>
								<th style=" width: 50px;text-align: center;">Weight <span style="color: red;"></span></th>
								<th style=" width: 50px;text-align: center;">Unit <span style="color: red;"></span></th>
								<th style=" width: 50px;text-align: center;">Foot <span style="color: red;"></span></th>
								<th style=" width: 50px;text-align: center;">Qty <span style="color: red;"></span></th>
								<th style=" width: 50px; text-align: center;">UOM </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;" class="delete_title">Delete</th>
							</tr>
						</thead>
						<tbody id="load_data_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-8">
							        <label> Tax Code</label>
							        <div class="has-feedback">
	                                    <select name="ITEM_TAXCODE" id="ITEM_TAXCODE" class="form-control select2" onchange="find_item_tax_gold(this);">
						                    <option value="">--- Select a value ---</option>
						                    <?php foreach($all_taxcode as $v):?>
						                        <option value="<?php echo $v->CODE;?>" <?php if(isset($data) && $data->ITEM_TAXCODE == $v->CODE) echo "selected='selected'";?>>
						                        <?php echo "[".$v->CODE."] ".$v->DESCRIPTION;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
                                    </div>
							    </div>
							    <div class="col-md-4">
							        <label> Tax %</label>
							        <div class="form-group">
							            <input type="text" name="ITEM_PERCEN" id="ITEM_PERCEN" onblur="calculate_item_gst(this);" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->ITEM_PERCEN, 2);?>" class="form-control" autocomplete="OFF">
							        </div>
							    </div>
							</div>
				    		<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> 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 class="has-feedback pull-left">
						            	<label> Include Gold Amount into Total Amount</label><br/>
		                                <input type='hidden' name='INCLUDE_GOLD' id="INCLUDE_GOLD" class='hidden_checkbox_value' value="<?php if(isset($data)) echo $data->INCLUDE_GOLD ;?>">
								    	<input type='checkbox' class='' onChange='edit_item_checkbox_set_value(this);get_total_amount(this);' style='width: 20px; height: 20px;' <?php if(isset($data) && $data->INCLUDE_GOLD == 1) echo "checked='checked'";?>>
		                            </div>
	        				</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);calculate_gold_amt(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><br/>
				        	<div class = "row">
				        		<div class="col-md-6">
				        			<label>Trade In</label><label id="tradein_page" style="display: none;">&nbsp;<a href="javascript:;" onclick="find_tradein_page(this);" title="Click to view more"> (Trade In Page)</a></label>
				        			<div class="form-group" id="TRADEIN_REFNO_HIDDEN_div" style="display: none;">
				        				<input type="text" id="TRADEIN_REFNO_HIDDEN" name="TRADEIN_REFNO_HIDDEN" class="form-control" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->TRADEIN_REFNO, 2);?>" readonly="readonly">
				        			</div>
				        			<div class="has-feedback" id="TRADEIN_REFNO_div">
				                        <select name="TRADEIN_REFNO" id="TRADEIN_REFNO" class="form-control select2" onchange="find_tradein_detail(this);">
						                    <option value="">--- Select a value ---</option>
						                </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" onblur="" readonly="readonly">
					        			<!-- <input type="hidden" class="form-control" name="TRADEIN_W" id="TRADEIN_W" readonly="readonly"> -->
					        			<span class="input-group-addon">gm</span>
					        		</div>
				        		</div>
				        	</div><br/>
				        	<div class = "row">
				        		<div class="col-md-6">
				        		</div>
				        		<div class="col-md-6">
				        			<label> Trade In Weight Used</label>
				        			<div class="input-group">
					        			<input type="text" class="form-control" name="TRADEIN_W" id="TRADEIN_W" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->TRADEIN_W, 2);?>" 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="input-group">
						                <input type="text" name="RATE" id="RATE" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->RATE, 2);?>" onblur="" class="form-control" autocomplete="OFF">
						                <span class="input-group-addon"><a href="javascript:;" onclick="calculate_rate(this);" title="Calculate">Calculate</a></span>
						            </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> -->
                                    <label style="padding-top: 12px;"></label>
                                    <!-- <input type="button" class="btn btn-default btn-block" onclick="recheck_debtor_gold(this);" value="Recalculate"/> -->
                                     <a data-target="#recalculate_modal" data-toggle="modal" class="btn btn-default btn-block" href="#recalculate_modal">Recalculate</a>
					        	</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);">
						                    <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" onblur='calculate_gst_gold();' 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">
        				<div class="col-md-6">
        				</div>
        				<div class="col-md-6">
	        				<label> Discount (%)</label>
							<div class="form-group">
								<div class="input-group">    
									<input type="text" name="DISCOUNT" id="DISCOUNT" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->DISCOUNT, 2);?>" class="form-control" autocomplete="OFF" onblur="calculate_price_after_discount(this);">
								<span class="input-group-addon"><a href="javascript:;" onclick="reset(this);" title="Reset Discount">Reset</a></span>
							    </div>
							</div>
							<label> Remove Discount (%)</label>
							<div class="form-group">
								<div class="input-group">    
									<input type="number" id="REMOVE_DISCOUNT" class="form-control" autocomplete="OFF" onblur="remove_discount(this);">
							    </div>
							</div>
		        			<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>
					</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" data-backdrop="static" data-keyboard="false">
			<div class="modal-dialog" style="width: 90%; margin-top: 10px;">
				<div class="modal-content">
					<form id="sub_form" action="<?php echo base_url($this->router->fetch_class()."/update")?>" method="POST" style="padding-left: 5px; padding-right: 5px;">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
							<h4 class="modal-title">Details</h4>
								<!-- 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" id="SUB_AMOUNT">
								<input type="hidden" name="PAYAMT" id="SUB_PAYAMT">
								<input type="hidden" name="BAL" id="SUB_BAL">
								<input type="hidden" name="ITEM_GST" id="SUB_ITEM_GST">
								<input type="hidden" name="ITEM_AMT" id="SUB_ITEM_AMT">
								<input type="hidden" name="ITEM_TOTAL" id="SUB_ITEM_TOTAL">
						</div>
						<div class="modal-body">
							<div class="row" style="">
								<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>
								</Table>
								<br/>
							</div>
							<div class="row">
								<fieldset class="the-fieldset">
								<legend class="the-legend">Total/Labour Charges</legend>
									<div class="row">
										<div class="form-group col-md-2">
											<label style=""><span style="color: red; padding-left: 5px;"></span>Inch</label>
											<div class="form-group" style="width: 200px;">
								                <input type="number" min="0" step='.01' name="SUB_INCH[]" id="SUB_INCH" class="form-control" placeholder="Inch" oninput="calculate_qty(this);">
								            </div>
										</div>
										<div class="form-group col-md-1">
											<label style="padding-top: 10px;"></label>
								            <center><strong style="font-size: 25px;">×</strong></center>
										</div>
										<div class="form-group col-md-2">
											<label style=""><span style="color: red; padding-left: 2px;"></span>Pcs</label>
											<div class="form-group" style="width: 200px;">
												<input type="number" min="0" step='.01' name="SUB_PCS[]" id="SUB_PCS" class="form-control" placeholder="Pcs" oninput="calculate_qty(this);">
											</div>
										</div>
										<div class="form-group col-md-1">
											<label style="padding-top: 10px;"></label>
								            <center><strong style="font-size: 25px;">=</strong></center>
										</div>
										<div class="form-group col-md-2">
											<label style=""><span style="color: red; padding-left: 2px;"></span>Qty</label>
											<div class="form-group" style="width: 200px;">
												<input type="number" min="0" step='.01' name="SUB_QTY[]" id="SUB_QTY" class="form-control" placeholder="Pcs" oninput="calculate_qty(this);">
											</div>
										</div>
										<div class="col-md-1" style="">
								        	<label style="padding-top: 14px;"></label>
								            <div class="has-feedback" style="">
		                                    	<select name="SUB_UNIT[]" class="form-control select2" id="SUB_UNIT" required="required">
								                    <option value="">UnHit</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="form-group col-md-1">
											<label style="padding-top: 10px;"></label>
								            <center><strong style="font-size: 25px;">×</strong></center>
										</div>
										<div class="form-group col-md-1">
											<label style="">Price</label>
								            <div class="form-group" style="width: 200px;">
								                <input type="number" step='.01' name="SUB_PRICE[]" id="SUB_PRICE" class="form-control" placeholder="Price" oninput="calculate_labour(this);">
								            </div>
										</div>
									</div>
									<div class = "row">
			        					<div class="col-md-5">
			        						<label style="padding-top: 10px;"></label>
								            <fieldset class="the-fieldset">
			    								<legend class="the-legend" style="font-weight: normal; font-size: 16px;">For Record Only</legend>
			    								<table id="table_sub_carton_barcode" class="table table-bordered">
												<thead>
													<tr>
														<th style="text-align: center; padding: 2px 5px;">Inch</th>
														<th style="text-align: center; padding: 2px 5px;">Weight</th>
														<th style="text-align: center; padding: 2px 5px;">Unit</th>
														<th style="text-align: center; padding: 2px 5px;">Foot</th>
													</tr>
												</thead>
												<tbody>
													<tr style="">
														<td style="text-align: center; padding: 2px 5px;">
															<div class="input-group">
																<input type='number' min="0" step="0.00001" name='SUB_LENGTH[]' id="#SUB_LENGTH" class='form-control' autocomplete='off' style='text-align: right;'>
									       					</div>
														</td>
														<td style="text-align: center; padding: 2px 5px;">
															<div class="input-group">
																<input type='number' min="0" step="0.00001" name='SUB_WEIGHT[]' id="#SUB_WEIGHT" class='form-control' autocomplete='off' style='text-align: right;'>
									       					</div>
														</td>
														<td style="text-align: center; padding: 2px 5px;">
															<div class="input-group">
																<input type='number' min="0" step="0.00001" name='SUB_UNIT_QTY' id="#SUB_UNIT_QTY" class='form-control' autocomplete='off' style='text-align: right;'>
									       					</div>
														</td>
														<td style="text-align: center; padding: 2px 5px;">
															<div class="input-group">
																<input type='number' min="0" step="0.00001" name='SUB_FOOT[]' id="#SUB_FOOT" class='form-control' autocomplete='off' style='text-align: right;'>
									       					</div>
														</td>
													</tr>
												</tbody>
											</table>
			    							</fieldset>
								        </div>
								        <div class="form-group col-md-1">
											<label style="padding-top: 30px;"></label>
								            <center><strong style="font-size: 25px;">=</strong></center>
										</div>
								        <div class="col-md-4">
								        	<label style="padding-top: 20px; white-space: nowrap;">Total/Labour Charges</label>
								            <div class="form-group" style="">
								                <input type="number" min="0" step='.01' name="SUB_LABOUR[]" id="SUB_LABOUR" class="form-control" placeholder="Total/Labour Charges" readonly="readonly">
								                <input type="hidden" name="RETURN" id="SUB_RETURN" class="form-control" value="<?php if(isset($data) && $data->RETURN != ""){ echo $ci->fm->str_esc($data->RETURN, 2);}else{echo "0";}?>">
								                <input type="hidden" id="SUB_LABOUR_HIDDEN" class="form-control">
								            </div>
								        </div>
						        	</div>
								</fieldset>
							</div><br/>
						</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>
<!--  -->

<!-- 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>
<!--  -->

<!-- recalculate_modal -->
		<div id="recalculate_modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
			<div class="modal-dialog modal-sm">
				<div class="modal-content">
					<form class="form-horizontal">
						<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 <strong>recalculate</strong> the 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>
							<a href="javascript:;" onclick="recheck_debtor_gold(this);" class="btn btn-primary">Confirm</a>
						</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_TOTAL_GST").val("<?php //echo number_format((float)$item_gst, 2, '.', '');?>");
		// <?php //endif;?>

		// <?php //if(!isset($item_gst)):?>
		// 	$("#ITEM_TOTAL_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_balance();
		// calculate_gst_gold();
		calculate_gold_amt_onload();
		//get_total_amount();
		find_item_tax_gold();
		// check_bal();
		//find_debtor();
		//find_tradein_gold();
		find_debtor_onload();

		var DISCOUNT = $("#DISCOUNT").val();
		if(DISCOUNT != ""){
			calculate_price_after_discount();
		}

		//var test = 16.895;
		//log(((test * 100).toFixed(0) / 100).toFixed(2));
		//log(roundOff_toFixed2(test));

        <?php if(isset($sub_array)):?>
			<?php foreach($sub_array as $sub):?>
		 		edit_sub_add();
		 		<?php $DESCRIPT = json_encode($sub->DESCRIPT);?> var DESCRIPT = <?php echo $DESCRIPT;?>;
		 		$(".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(DESCRIPT);
				$(".edit_sub_tr_item:last [name='SUB_DESCRIPT_TITLE[]']").html(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_UNIT_QTY[]']").val( "<?php echo $ci->fm->str_esc(trim($sub->UNIT_QTY), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_UNIT_QTY_TITLE[]']").html( "<?php echo $ci->fm->str_esc(trim($sub->UNIT_QTY), 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_WEIGHT[]']").val( "<?php echo $ci->fm->str_esc(trim($sub->WEIGHT), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_WEIGHT_TITLE[]']").html( "<?php echo $ci->fm->str_esc(trim($sub->WEIGHT), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_FOOT[]']").val( "<?php echo $ci->fm->str_esc(trim($sub->FOOT), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_FOOT_TITLE[]']").html( "<?php echo $ci->fm->str_esc(trim($sub->FOOT), 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);?>");

				

				<?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;?>

		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_modal1 [name='REFNO']").val("<?php echo $data->REFNO;?>");
		<?php endif;?>

		<?php if(isset($data->TRADEIN_REFNO)):?>
			$("#TRADEIN_REFNO_HIDDEN_div").show();
			$("#TRADEIN_REFNO_div").hide();
			$('#tradein_page').show('fast');
		<?php endif;?>

	});

	function edit_sub_add(){

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

		content += 	"<td style='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='text-align: left;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_ITEM_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='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='' 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='text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_INCH_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='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='text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_PCS_TITLE[]'>";
        content += 	"</td>";

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

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

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

        content += 	"<td style='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='text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_QTY_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='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='text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_UNIT_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='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='text-align: center;' onClick='edit_sub_detail(this);find_item_image(this);' name='SUB_PRICE_TITLE[]'>";
        content += 	"</td>";

        content += 	"<td style='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='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>";
		
		
		content += 	"<td style='text-align: center;'>";
		content += 	"<button type='button' onClick='recalculate_item_labour(this);edit_sub_remove(this);' class='btn btn-danger'>";
		content += 		"<i class='fa fa-trash-o'></i>";
		content += 	"</button>";
		content += 	"</td>";

		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 find_debtor(){
    	var id = $("#DEBTOR").val();
    	$('#TRADEIN_REFNO').find('option').remove().end().append('<option value="">--- Select a value ---</option>').val('');
    	$('#TRADEIN_REFNO').select2('val', '');
       	$('#DEBTOR_GOLD').val('');
       	var TRADEIN_REFNO_HIDDEN = $('#TRADEIN_REFNO_HIDDEN').val();
       	$('#tradein_page').show('fast');

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

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_debtor",
                data: params,
                success: function(result, status, xhr){
                    //log(JSON.parse(result));
                    var info, i;  
                    var src = "";
                    info = JSON.parse(result);     
                    for (i = 0; i < info.length; i++){
                    	if(info[i].REFNO != null){
                    		src = "<option value='"+info[i].REFNO+"'>"+'['+info[i].REFNO+'] '+info[i].BAL+' gm <option>';
							$('#TRADEIN_REFNO').append(src);
							$("#TRADEIN_REFNO option:last").remove();

							if(info[i].REFNO == TRADEIN_REFNO_HIDDEN){
                    			$("#TRADEIN_REFNO").val(info[i].REFNO).change();
                    		}
                    	}
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
        	$('#TRADEIN_REFNO').select2('val', '');
        	$('#DEBTOR_GOLD').val('');
			$('#TRADEIN_REFNO').find('option').remove().end().append('<option value="">--- Select a value ---</option>').val('');
        }
    }

    function find_debtor_onload(){
    	var id = $("#DEBTOR").val();
    	$('#TRADEIN_REFNO').find('option').remove().end().append('<option value="">--- Select a value ---</option>').val('');
    	$('#TRADEIN_REFNO').select2('val', '');
       	$('#DEBTOR_GOLD').val('');
       	var TRADEIN_REFNO_HIDDEN = $('#TRADEIN_REFNO_HIDDEN').val();
       	$('#tradein_page').show('fast');

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

            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->router->fetch_class());?>/get_debtor",
                data: params,
                success: function(result, status, xhr){
                    //log(JSON.parse(result));
                    var info, i;  
                    var src = "";
                    info = JSON.parse(result);     
                    for (i = 0; i < info.length; i++){
                    	if(info[i].REFNO != null){
       //              		src = "<option value='"+info[i].REFNO+"'>"+'['+info[i].REFNO+'] '+info[i].BAL+' gm <option>';
							// $('#TRADEIN_REFNO').append(src);
							// $("#TRADEIN_REFNO option:last").remove();

							if(info[i].REFNO == TRADEIN_REFNO_HIDDEN){
                    			$("#TRADEIN_REFNO").val(info[i].REFNO).change();
                    			$("#DEBTOR_GOLD").val(info[i].BAL);
                    		}
                    	}
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
        	$('#TRADEIN_REFNO').select2('val', '');
        	$('#DEBTOR_GOLD').val('');
			$('#TRADEIN_REFNO').find('option').remove().end().append('<option value="">--- Select a value ---</option>').val('');
        }
    }
   
    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 check_type(){
    	var radioValue = $("input[name='sub_type']:checked").val();

        if(radioValue == 1){
        	$("#Sub_modal [name='SUB_WEIGHT[]']").prop("disabled", false);
            $("#Sub_modal [name='SUB_QTY[]']").prop("disabled", true);
            $("#Sub_modal [name='SUB_QTY[]']").val('');
            $("#Sub_modal [name='SUB_FOOT[]']").prop("disabled", true);
            $("#Sub_modal [name='SUB_FOOT[]']").val('');
            $("#Sub_modal [name='SUB_LABOUR[]']").val('');

        }else if(radioValue == 2){
        	$("#Sub_modal [name='SUB_WEIGHT[]']").val('');
        	$("#Sub_modal [name='SUB_WEIGHT[]']").prop("disabled", true);
        	$("#Sub_modal [name='SUB_QTY[]']").prop("disabled", false);
          	$("#Sub_modal [name='SUB_FOOT[]']").prop("disabled", true);
          	$("#Sub_modal [name='SUB_FOOT[]']").val('');
          	$("#Sub_modal [name='SUB_LABOUR[]']").val('');
        }else{
        	$("#Sub_modal [name='SUB_WEIGHT[]']").val('');
        	$("#Sub_modal [name='SUB_WEIGHT[]']").prop("disabled", true);
        	$("#Sub_modal [name='SUB_QTY[]']").val('');
          	$("#Sub_modal [name='SUB_QTY[]']").prop("disabled", true);
          	$("#Sub_modal [name='SUB_FOOT[]']").prop("disabled", false);
          	$("#Sub_modal [name='SUB_LABOUR[]']").val('');
        }
    }

    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_bal(){
    	var PAYAMT = parseFloat($("#PAYAMT").val());
		var AMOUNT = parseFloat($("#AMOUNT").val());

		if(AMOUNT != "" && PAYAMT != 0){
			var BAL = AMOUNT - PAYAMT;
			$("#BAL").val(roundOff_toFixed2(BAL));
		}else if(AMOUNT != "" && PAYAMT == 0 || PAYAMT == ""){
			$("#BAL").val(roundOff_toFixed2(AMOUNT));
		}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());
                    	calculate_puregold();
                    	add_back_gold();
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
        	$("#DEBTOR_GOLD").val('');
        }
    }

    function recheck_debtor_gold(){
    	var id = $("#TRADEIN_REFNO_HIDDEN").val();
    	var used = parseFloat($("#TRADEIN_W").val());
    	var puregold = parseFloat($("[name='PUREGOLD']").val());
    	var gold_bal = parseFloat($("[name='GOLD_BAL']").val());
    	$("#recalculate_modal").modal('hide');

    	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++){
                    	var bal = parseFloat(info[i].BAL);
                    	var ori = used + bal;
                    	var gold_diff = puregold - used;
                    	var add_gold = gold_bal + gold_diff;

                    	$("#DEBTOR_GOLD").val(roundOff_toFixed2(ori));
                    	$("#GOLD_BAL").val(roundOff_toFixed2(add_gold));

                    	if(ori > puregold){
                    		$("#TRADEIN_W").val(roundOff_toFixed2(puregold));
                    	}else{
                    		$("#TRADEIN_W").val(roundOff_toFixed2(ori));
                    	}
                    	calculate_puregold();
                    }
                },
                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++) {
                    	if(info[i].TAXCODE != null){
                    		$("#TAXCODE").select2("val",info[i].TAXCODE.trim());
                    	}else{
                    		$("#TAXCODE").select2("val",'');
                    	}

                    	if(info[i].PERCEN != null){
							$("#PERCEN").val(info[i].PERCEN.trim());
                    	}else{
                    		$("#PERCEN").val('');
                    	}

                    	if(info[i].RATE != null){
                    		$("#RATE").val(info[i].RATE.trim());
                    		calculate_puregold();
                    	}else{
                    		$("#RATE").val(null);
                    		calculate_puregold();
                    	}

                    	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 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();
		var SUB_DESCRIPT = $(".modal-body #SUB_DESCRIPT").val();
		var GR = SUB_DESCRIPT + " (Good Returned)";

        if(id != "GR"){
            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++) {
                    	if(info[i].SALES_AMT != null){
                    		$(".modal-body #SUB_PRICE").val(info[i].SALES_AMT.trim());
                    	}else{
                    		$(".modal-body #SUB_PRICE").val('');
                    	}

                    	if(info[i].DESCRIPT != null){
                    		$(".modal-body #SUB_DESCRIPT").val(info[i].DESCRIPT.trim());
                    	}else{
                    		$(".modal-body #SUB_DESCRIPT").val('');
                    	}
                    	//$(".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{
        	var RETURN_BAL = parseFloat($(".modal-body #SUB_RETURN").val());
        	var RETURN = parseFloat($(".modal-body #SUB_LABOUR_HIDDEN").val());
        	var NEW_RETURN = RETURN_BAL + RETURN;
        	$(".modal-body #SUB_RETURN").val(NEW_RETURN);
            $(".modal-body #SUB_PRICE").val(0);
			$(".modal-body #SUB_LABOUR").val(0);
        	$(".modal-body #SUB_DESCRIPT").val(GR);
        	find_item_tax_gold();
        	//$(".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 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 calculate_qty(){
    	var INCH = $("#SUB_INCH").val();
    	var PCS = $("#SUB_PCS").val();

    	if(INCH != "" && PCS != ""){
    		var QTY = INCH*PCS;
    		$("#Sub_modal [name='SUB_QTY[]']").val(roundOff_toFixed2(QTY));
    		calculate_labour();
    	}else{
    		calculate_labour();
    	}
    }

    function calculate_labour(){
    	var QTY = $("#Sub_modal [name='SUB_QTY[]']").val();
    	var WEIGHT = $("#Sub_modal [name='SUB_WEIGHT[]']").val();
    	var FOOT = $("#Sub_modal [name='SUB_FOOT[]']").val();
    	var PRICE = $("#SUB_PRICE").val();
    	var INCLUSIVE = $("#SUB_INCLUSIVE").val();

    	if(PRICE != ""){
    		if(QTY != ""){
    			var LABOUR = QTY*PRICE;
	    		$("#SUB_LABOUR").val(roundOff_toFixed2(LABOUR));
	    		$("#SUB_LABOUR_HIDDEN").val(roundOff_toFixed2(LABOUR));
	    		calculate_gst();
    		}else if(WEIGHT != ""){
				var LABOUR = WEIGHT*PRICE;
	    		$("#SUB_LABOUR").val(roundOff_toFixed2(LABOUR));
	    		$("#SUB_LABOUR_HIDDEN").val(roundOff_toFixed2(LABOUR));
    		}else{
    			var LABOUR = FOOT*PRICE;
	    		$("#SUB_LABOUR").val(roundOff_toFixed2(LABOUR));
	    		$("#SUB_LABOUR_HIDDEN").val(roundOff_toFixed2(LABOUR));
    		}
    	}
    }

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

    	if(!isNaN(DEBTOR_GOLD)){
    		var PUREGOLD = GOLD*WEIGHT/1000;
    		var GOLDPRICE = PUREGOLD - DEBTOR_GOLD;
    		$("[name='PUREGOLD']").val(roundOff_toFixed2(PUREGOLD));
    		$("[name='GOLDPRICE']").val(roundOff_toFixed2(GOLDPRICE));
    		calculate_gold_balance();
    	}else{
    		var PUREGOLD = GOLD*WEIGHT/1000;
    		$("[name='PUREGOLD']").val(roundOff_toFixed2(PUREGOLD));
    		calculate_gold_balance();
    	}
    }

    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());
    	var GOLD_BAL = parseFloat($("[name='GOLD_BAL']").val());
    	var TRADEIN_W = parseFloat($("[name='TRADEIN_W']").val());

    	if(!isNaN(DEBTOR_GOLD)){
    		var BALANCE = roundOff_toFixed2(PUREGOLD-DEBTOR_GOLD);
    		if(!isNaN(RATE)){
    			if(BALANCE > 0){
	    			$("#GOLD_BAL").val(GOLD_BAL-BALANCE);
	    			$("#DEBTOR_GOLD").val((0).toFixed(2));
	    			var GOLDPRICE = roundOff_toFixed2(BALANCE*RATE);
	    			$("[name='GOLDPRICE']").val(GOLDPRICE);
		    		$("[name='GOLD_AMT']").val(GOLDPRICE);
		    		if(isNaN(TRADEIN_W)){
		    			$("[name='TRADEIN_W']").val(roundOff_toFixed2(DEBTOR_GOLD));
		    		}
		    		calculate_gst_gold();
	    		}else if(BALANCE <= 0){
	    			var ABS_BALANCE = Math.abs(BALANCE);
	    			$("#DEBTOR_GOLD").val(ABS_BALANCE);
	    			$("#GOLDPRICE").val((0).toFixed(2));
	    			$("[name='GOLD_AMT']").val((0).toFixed(2));
	    			if(isNaN(TRADEIN_W)){
		    			$("[name='TRADEIN_W']").val(roundOff_toFixed2(PUREGOLD));
		    		}
	    			calculate_gst_gold();
	    		}
    		}else if(isNaN(RATE)){
    			var BALANCE = roundOff_toFixed2(PUREGOLD-DEBTOR_GOLD);
    			if(BALANCE > 0){
	    			$("#GOLD_BAL").val(GOLD_BAL-BALANCE);
	    			$("#DEBTOR_GOLD").val((0).toFixed(2));
	    			var GOLDPRICE = BALANCE;
	    			$("[name='GOLDPRICE']").val(GOLDPRICE);
		    		$("[name='GOLD_AMT']").val(GOLDPRICE);
		    		if(isNaN(TRADEIN_W)){
		    			$("[name='TRADEIN_W']").val(roundOff_toFixed2(DEBTOR_GOLD));
		    		}
		    		calculate_gst_gold();
	    		}else if(BALANCE <= 0){
	    			var ABS_BALANCE = Math.abs(BALANCE);
	    			$("#DEBTOR_GOLD").val(ABS_BALANCE);
	    			$("#GOLDPRICE").val((0).toFixed(2));
	    			$("[name='GOLD_AMT']").val((0).toFixed(2));
	    			if(isNaN(TRADEIN_W)){
		    			$("[name='TRADEIN_W']").val(roundOff_toFixed2(PUREGOLD));
		    		}
	    			calculate_gst_gold();
	    		}
    		}
    	}else{
    		if(isNaN(RATE)){
    			var GOLDPRICE = roundOff_toFixed2(PUREGOLD);
    			$("[name='GOLDPRICE']").val(GOLDPRICE);
		    	$("[name='GOLD_AMT']").val(GOLDPRICE);
		    	$("#GOLD_BAL").val(GOLD_BAL-PUREGOLD);
		    	calculate_gst_gold();
    		}else{
    			var GOLDPRICE = roundOff_toFixed2(PUREGOLD*RATE);
    			$("[name='GOLDPRICE']").val(GOLDPRICE);
		    	$("[name='GOLD_AMT']").val(GOLDPRICE);
		    	$("#GOLD_BAL").val(GOLD_BAL-PUREGOLD);
		    	calculate_gst_gold();
    		}
    	}
    }

    function add_back_gold(){
    	var DEBTOR_GOLD = parseFloat($("#DEBTOR_GOLD").val());
    	var PUREGOLD = parseFloat($("[name='PUREGOLD']").val());
    	var GOLD_BAL = parseFloat($("[name='GOLD_BAL']").val());

    	if(!isNaN(DEBTOR_GOLD)){
    		var NEW_GOLD_BAL = roundOff_toFixed2(PUREGOLD + GOLD_BAL);
    		$("#GOLD_BAL").val(NEW_GOLD_BAL);
    	}
    }

    function find_tradein_page(){
    	var TRADEIN_REFNO_HIDDEN = $("#TRADEIN_REFNO_HIDDEN").val();
    	var url = "https://kengheng.com.my/management/tradein/edit/"+TRADEIN_REFNO_HIDDEN;
    	window.open(url, '_blank');
    }

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

    	if(CASHAMT != "" && isNaN(BCAMT)){
    		$("[name='PAYAMT']").val(roundOff_toFixed2(CASHAMT));
    		check_bal();
    	}else if(BCAMT != "" && isNaN(CASHAMT)){
    		$("[name='PAYAMT']").val(roundOff_toFixed2(BCAMT));
    		check_bal();
    	}else if(CASHAMT != "" && BCAMT != ""){
    		var PAYAMT = roundOff_toFixed2(CASHAMT+BCAMT);
    		$("[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 = roundOff_toFixed2((SUB_LABOUR*SUB_PERCEN)/100);
    		$("#SUB_GST").val(SUB_GST);
    		calculate_amt()
    	}
    }

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

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

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

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

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

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

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

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

    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 = roundOff_toFixed2(SUB_LABOUR+SUB_GST);
    		$("#SUB_AMT").val(SUB_AMT);
    	}
    }

	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(roundOff_toFixed2(new_item_total));
		}else if(old_total == "" && pkkey != first_pkkey){
			$("#ITEM_TOTAL").val(roundOff_toFixed2(item_total));
		}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, 2, '.', '');?>";
	// 	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(2));
	// 	}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 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_item_tax_gold(t){
		var TAXCODE  = $("#ITEM_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 !=""){
                    	if(result != ""){
 							$("#ITEM_PERCEN").val(result);
                    	}else{
                    		$("#ITEM_PERCEN").val();
                    	}
                        calculate_item_gst();
                    }else{
                       $("#ITEM_PERCEN").val('');
                       $("#ITEM_GST").val('');
                    }
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
            $("#ITEM_PERCEN").val('');
            $("#ITEM_GST").val('');

        }
    }

    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 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_weight = $(tr).find("[name='SUB_WEIGHT[]']").val();
		var sub_foot = $(tr).find("[name='SUB_FOOT[]']").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');
		$("#Sub_modal [name='SUB_PKKEY[]']").val(sub_pkkey);
		$("#Sub_modal [name='SUB_ITEM[]']").select2('val',sub_item);
		$("#Sub_modal [name='SUB_DESCRIPT[]']").val(sub_descript);
		$("#Sub_modal [name='SUB_INCH[]']").val(sub_inch);
		$("#Sub_modal [name='SUB_LENGTH[]']").val(sub_length);
		$("#Sub_modal [name='SUB_PCS[]']").val(sub_pcs);
		$("#Sub_modal [name='SUB_QTY[]']").val(sub_qty);
		$("#Sub_modal [name='SUB_WEIGHT[]']").val(sub_weight);
		$("#Sub_modal [name='SUB_FOOT[]']").val(sub_foot);
		$("#Sub_modal [name='SUB_UNIT[]']").select2('val',sub_unit);
		$("#Sub_modal [name='SUB_PRICE[]']").val(sub_price);
		$("#Sub_modal #SUB_LABOUR").val(sub_labour);
		$("#Sub_modal #SUB_LABOUR_HIDDEN").val(sub_labour);
		$("#Sub_modal #SUB_TAXCODE").select2('val',sub_taxcode);
		$("#Sub_modal #SUB_PERCEN").val(sub_percen);
		$("#Sub_modal #SUB_GST").val(sub_gst);
		$("#Sub_modal #SUB_AMT").val(sub_amt);

		// if(sub_weight != ""){
		// 	$("#Sub_modal [name='SUB_WEIGHT[]']").prop("disabled", false);
  //           $("#Sub_modal [name='SUB_QTY[]']").prop("disabled", true);
  //           $("#Sub_modal [name='SUB_FOOT[]']").prop("disabled", true);
  //           $("#Sub_modal #sub_weight_btn").prop("checked", true);
		// }else if(sub_qty != ""){
		// 	$("#Sub_modal [name='SUB_WEIGHT[]']").prop("disabled", true);
  //           $("#Sub_modal [name='SUB_QTY[]']").prop("disabled", false);
  //           $("#Sub_modal [name='SUB_FOOT[]']").prop("disabled", true);
  //           $("#Sub_modal #sub_qty_btn").prop("checked", true);
		// }else{
		// 	$("#Sub_modal [name='SUB_WEIGHT[]']").prop("disabled", true);
  //           $("#Sub_modal [name='SUB_QTY[]']").prop("disabled", true);
  //           $("#Sub_modal [name='SUB_FOOT[]']").prop("disabled", false);
  //           $("#Sub_modal #sub_foot_btn").prop("checked", true);
		// }

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

	}

	function calculate_item_gst(){
		var ITEM_TOTAL = parseFloat($("#ITEM_TOTAL").val());
		var ITEM_PERCEN = parseFloat($("#ITEM_PERCEN").val());

		if(ITEM_TOTAL != ""){
			var ITEM_GST = roundOff_toFixed2((ITEM_TOTAL*ITEM_PERCEN)/100);
			$("#ITEM_GST").val(ITEM_GST);

			var ITEM_AMT = ITEM_TOTAL + parseFloat(ITEM_GST);
			$("#ITEM_AMT").val(roundOff_toFixed2(ITEM_AMT));

			get_total_amount();
		}else{
			var ITEM_GST = 0;
			$("#ITEM_GST").val(ITEM_GST);
			var ITEM_AMT = ITEM_TOTAL + parseFloat(ITEM_GST);
			$("#ITEM_AMT").val(roundOff_toFixed2(ITEM_AMT));
			get_total_amount();
		}
	}

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

		var current = parseFloat($(".modal-body #SUB_LABOUR").val());
		var old = parseFloat($("#ITEM_TOTAL").val());
		var item_percen = parseFloat($("#ITEM_PERCEN").val());
		var sum = current + old;
		var item_gst = roundOff_toFixed2((sum*item_percen)/100);
		var item_amt = parseFloat(sum) + parseFloat(item_gst);
		var GOLD_AMT = parseFloat($("#GOLD_AMT").val());
		var PAYAMT = parseFloat($("#PAYAMT").val());
		var INCLUDE_GOLD = $("#INCLUDE_GOLD").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);
		// }
        if(isNaN(item_gst)){
            $(".modal-body #SUB_ITEM_GST").val(0);
        }else{
            $(".modal-body #SUB_ITEM_GST").val(item_gst);
        }
        
        if(isNaN(item_amt)){
            $(".modal-body #SUB_ITEM_AMT").val(0);
        }else{
            $(".modal-body #SUB_ITEM_AMT").val(item_amt);
        }

		$(".modal-body #SUB_ITEM_TOTAL").val(sum);
		
	    $("#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 calculate_rate(){
		var RATE = parseFloat($("#RATE").val());
		var GOLDPRICE = parseFloat($("#GOLDPRICE").val());
		var TAXCODE = $("#TAXCODE").val();
		var PERCEN = $("#PERCEN").val();

		if(RATE != "" && GOLDPRICE != ""){
			var NEW_GOLDPRICE = roundOff_toFixed2((RATE*GOLDPRICE));
			$("#GOLDPRICE").val(NEW_GOLDPRICE);
			$("#GOLD_TOTAL").val(NEW_GOLDPRICE);
			$("#GOLD_AMT").val(NEW_GOLDPRICE);
		}

	}

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

	}

	function calculate_price_after_discount(){
		var ITEM_TOTAL = parseFloat($("#ITEM_TOTAL").val());
		var GOLD_TOTAL = parseFloat($("#GOLD_TOTAL").val());
		var DISCOUNT = parseFloat($("#DISCOUNT").val())/100;

		if(DISCOUNT != ""){
			if(ITEM_TOTAL != ""){
				var FINAL_PRICE_ITEM =  ITEM_TOTAL - (ITEM_TOTAL * DISCOUNT);
				$("#ITEM_TOTAL").val(roundOff_toFixed2(FINAL_PRICE_ITEM));
				calculate_item_gst();
			}

			if(GOLD_TOTAL != ""){
				var FINAL_PRICE_GOLD =  GOLD_TOTAL - (GOLD_TOTAL * DISCOUNT);
				$("#GOLD_TOTAL").val(roundOff_toFixed2(FINAL_PRICE_GOLD));
				$("#GOLDPRICE").val(roundOff_toFixed2(FINAL_PRICE_GOLD));
				calculate_gst_gold();
			}

			check_bal();
		}
	}
	
    function remove_discount(){
		var ITEM_TOTAL = parseFloat($("#ITEM_TOTAL").val());
		var GOLD_TOTAL = parseFloat($("#GOLD_TOTAL").val());
		var DISCOUNT = parseFloat($("#DISCOUNT").val())/100;
		var REMOVE_DISCOUNT = parseFloat($("#REMOVE_DISCOUNT").val())/100;

		if(REMOVE_DISCOUNT != ""){
			if(ITEM_TOTAL != ""){
				var FINAL_PRICE_ITEM = ITEM_TOTAL + (ITEM_TOTAL * REMOVE_DISCOUNT);
				var FINAL_DISCOUNT =  parseFloat($("#DISCOUNT").val()) - parseFloat($("#REMOVE_DISCOUNT").val());
				$("#DISCOUNT").val(FINAL_DISCOUNT);
				$("#ITEM_TOTAL").val(roundOff_toFixed2(FINAL_PRICE_ITEM));
				calculate_item_gst();
			}

			if(GOLD_TOTAL != ""){
				var FINAL_PRICE_GOLD = GOLD_TOTAL + (GOLD_TOTAL * REMOVE_DISCOUNT);
				$("#GOLD_TOTAL").val(roundOff_toFixed2(FINAL_PRICE_GOLD));
				$("#GOLDPRICE").val(roundOff_toFixed2(FINAL_PRICE_GOLD));
				calculate_gst_gold();
			}

			check_bal();
		}
	}

	function reset(){
		//$("#DISCOUNT").val('')
		location.reload();
	}

	function edit_validation(form){
		var error = "";
		$("#load_data_tbody").find("tr:not(:last)").remove();
		
		$(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>