<?php $ci = get_instance();?>
<style type="text/css">
	.transparent_input{
		background-color: transparent; 
		border-color: transparent;
	}
</style>
<form id ="div_form" onSubmit="return edit_validation(this);" action="<?php echo base_url($this->router->fetch_class()."/update")?>" method="POST" style="padding-left: 5px; padding-right: 5px;">
	<!--  -->
	<?php if(isset($data)):?>
        <input type="hidden" name="REFNO" value="<?php echo $data->REFNO;?>">
        <input type="hidden" name="WEIGHT" id="gold_total" value="<?php if(isset($gold_total)) echo number_format((float)$gold_total, 2, '.', '');?>">
        <input type="hidden" name="GOLDPRICE" id="goldprice_total" value="<?php if(isset($goldprice_total)) echo number_format((float)$goldprice_total, 2, '.', '');?>">
        <input type="hidden" name="AMOUNT" id="gold_amt" value="<?php if(isset($gold_amt)) echo number_format((float)$gold_amt, 2, '.', '');?>">
	<?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-12">
        	<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> Creditor</label>
			            <div class="has-feedback">
	                        <select name="CREDITOR" id="CREDITOR" class="form-control select2" required="required">
			                    <option value="">--- Select a value ---</option>
			                    <?php foreach($all_creditor as $v):?>
			                        <option value="<?php echo $v->CODE;?>" <?php if(isset($data) && $data->CREDITOR == $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 == 'G'){
											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="height: 100px; max-height: 800px; overflow-y: scroll;">
						<thead>
							<tr>
								<th style=" width: 50px; white-space: nowrap; text-align: center;">Gold Code <span style="color: red;"></span></th>
								<th style=" width: 150px; white-space: nowrap; text-align: center;">Description <span style="color: red;"></span></th>
								<th style=" width: 50px; white-space: nowrap; text-align: center;">Weight <span style="color: red;"></span></th>
								<th style=" width: 50px; text-align: center;">Unit </th>
								<th style=" width: 50px; text-align: center;">Rate </th>
								<th style=" width: 50px; text-align: center;">Gold Price</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;">Amount</th>
								<th style=" width: 1px; text-align: center; display: none;" class="delete_title">Delete</th>
							</tr>
						</thead>
						<tbody>
							<tr id="edit_sub_tr_add_item" style="<?php if(! $can_create) echo "display: none;";?>">
								<td id="sub_tr_add_btn" colspan="99" style="text-align: center;">
									<button type="button" onClick="check_main();" id="add_new" class="btn btn-primary btn_add">
										<i class="fa fa-plus-circle"></i>
									</button>
								</td>
							</tr>
						</tbody>
						<thead>
							<tr>
								<th colspan="2" style="text-align: right;">Total</th>
								<th style="text-align: right;" id="gold_total1"><?php if(isset($gold_total)) echo number_format((float)$gold_total, 2, '.', '');?></th>
								<th colspan="2"></th>
								<th style="text-align: right;" id="goldprice_total1"><?php if(isset($goldprice_total)) echo number_format((float)$goldprice_total, 3, '.', '');?></th>
								<th colspan="2"></th>
								<th style="text-align: right;" id="gold_gst"><?php if(isset($gold_gst)) echo number_format((float)$gold_gst, 3, '.', '');?></th>
								<th style="text-align: right;" id="gold_amt1"><?php if(isset($gold_amt)) echo number_format((float)$gold_amt, 2, '.', '');?></th>
								<th style=" width: 1px; text-align: center; display: none;" class="delete_title"></th>
							</tr>
						</thead>
					</table>
        		</tr>
        	</table>
        </div>
    </div>
    <?php if((! isset($data) && $can_create) || (isset($data) && $can_edit)):?>
		<div class="form-group" style="margin-top: 30px;">
			<label></label>
			<button type="submit" id="edit_btn_submit" class="btn btn-primary btn-xs-block btn_submit">
				<i class="fa fa-save"></i>
				Save
			</button>
		</div>
	<?php endif;?>
</form>

<!-- Sub_modal -->
	<div id="Sub_modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<form id="sub_form" action="<?php echo base_url($this->router->fetch_class()."/update")?>" method="POST" class="form-horizontal">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">Details</h4>
					</div>
					<div class="modal-body">
						<!-- Main data -->
						<?php if(isset($data)):?>
							<input type="hidden" name="REFNO" value="<?php echo $data->REFNO;?>">
							<input type="hidden" name="WEIGHT" id="gold_total" value="<?php if(isset($gold_total)) echo number_format((float)$gold_total, 2, '.', '');?>">
							<input type="hidden" name="GOLDPRICE" id="goldprice_total" value="<?php if(isset($goldprice_total)) echo number_format((float)$goldprice_total, 2, '.', '');?>">
        					<input type="hidden" name="AMOUNT" id="gold_amt" value="<?php if(isset($gold_amt)) echo number_format((float)$gold_amt, 2, '.', '');?>">
						<?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="CREDITOR" id="CREDITOR">
							<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="SUB_BAL[]" id="SUB_BAL">
						<Table>
							<tr>
								<th class="FG_th">
	                                <span style="color: red;">*</span> Gold Code:
	                            </th>
	                            <td class="FG_td" style="">
                                    <div class="has-feedback">
                                    	<select name="SUB_GOLD[]" id="SUB_GOLD" class="form-control select2" required="required" 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($sub) && $sub->GOLD == $v->CODE) echo "selected='selected'";?>>
						                        <?php echo "[".$v->CODE."] ".$v->NAME;?>
						                        </option>
						                    <?php endforeach;?>
						                </select>
                                    </div>
                                </td>
                            </tr>
                            <tr>
								<th class="FG_th">
	                                <span style="color: red;">*</span> Description:
	                            </th>
	                            <td class="FG_td" style="" colspan="3">
                                    <input type='text' size='50' name='SUB_DESCRIPT[]' id="SUB_DESCRIPT" class='form-control' autocomplete='off' style='text-align: left;' required="required">
                                </td>
                            </tr>
							<tr>
								<!-- <th class="FG_th">
	                                <span style="color: red;"></span> Inclusive:
	                            </th>
	                            <td class="FG_td" style="" colspan="3">
	                                <input type='hidden' name='SUB_INCLUSIVE[]' id="SUB_INCLUSIVE" class='hidden_checkbox_value'>
									<input type='checkbox' class='' onChange='edit_item_checkbox_set_value(this);' style='width: 20px; height: 20px;'>
								</td> -->
						</Table>
						<br/>
						<Table>
							<tr>
								<fieldset class="the-fieldset">
						    		<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Pure Gold Price</legend>
						    		<div class = "row" style="padding-left: 15px;">
								        <div class="col-md-1">
								        	<label>Weight</label>
								            <div class="form-group" style="width: 100px;">
								                <input type="number" min="0" step='.01' name="SUB_WEIGHT[]" id="SUB_WEIGHT" class="form-control" placeholder="Weight" onblur="calculate_goldprice(this);find_tax_gold(this);calculate_gold_balance(this);" required="required">
								            </div>
								        </div>
								        <div class="col-md-1" style="width: 120px; padding-left: 20px;">
								        	<label style="padding-top: 13px;"></label>
								            <div class="has-feedback" style="padding-left: 5px;">
		                                    	<select name="SUB_UNIT_W[]" class="form-control select2" id="SUB_UNIT_W" required="required">
								                    <option value="">Unit</option>
								                    <?php foreach($all_unit_w as $v):?>
								                        <option value="<?php echo $v->CODE;?>" <?php if(isset($sub) && $sub->UNIT_W == $v->CODE) echo "selected='selected'";?>>
								                        <?php echo $v->CODE;?>
								                        </option>
								                    <?php endforeach;?>
								                </select>
                                    		</div>
								        </div>
								        <div class="col-md-1" style="width: 90px; text-align: center; padding-right: 20px;">
								        	<label style="padding-top: 40px;"></label>
								            <strong style="font-size: 25px; text-align: center;">×</strong>
								        </div>
								        <div class="col-md-1">
								        	<label>Rate</label>
								            <div class="form-group" style="width: 120px;">
								                <input type="number" min="0" step='.0001' name="SUB_RATE[]" id="SUB_RATE" class="form-control" placeholder="Rate" onblur="calculate_goldprice(this);find_tax_gold(this);" required="required">
								            </div>
								        </div>
								        <div class="col-md-1" style="width: 130px; text-align: center; padding-left: 10px;">
								        	<label style="padding-top: 40px;"></label>
								            <strong style="font-size: 25px; padding-left: 30px; text-align: center;">=</strong>
								        </div>
								        <div class="col-md-1">
								        	<label style="white-space: nowrap;">Gold Price</label>
								            <div class="form-group" style="width: 300px;">
								                <input type="number" min="0" step='.01' name="SUB_GOLDPRICE[]" id="SUB_GOLDPRICE" class="form-control" placeholder="Gold Price">
								            </div>
								        </div>
						        	</div>
								</fieldset>
							</tr>
							<br/>
							<tr>
								<div class="row">
									<div class="col-md-8">
							        	<label> Tax Code</label>
							            <div class="has-feedback">
	                                    	<select name="SUB_TAXCODE[]" class="form-control select2" id="SUB_TAXCODE" onchange="find_tax_gold(this);">
							                    <option value="">--- Select a value ---</option>
							                    <?php foreach($all_taxcode as $v):?>
							                        <option value="<?php echo $v->CODE;?>" <?php if(isset($sub) && $sub->TAXCODE == $v->CODE) echo "selected='selected'";?>>
							                        <?php echo "[".$v->CODE."] ".$v->DESCRIPTION;?>
							                        </option>
							                    <?php endforeach;?>
							                </select>
                                    	</div>
							        </div>
							        <div class="col-md-2">
							        	<label> Tax %</label>
							            <div class="">
							                <input type="text" name="SUB_PERCEN[]" id="SUB_PERCEN" value="" class="form-control" autocomplete="OFF" readonly="readonly">
							            </div>
							        </div>
							        <div class="col-md-2">
							        	<label> GST</label>
							            <div class="">
							                <input type="text" name="SUB_GST[]" id="SUB_GST" value="" class="form-control" autocomplete="OFF" readonly="readonly">
							            </div>
							        </div>
								</div>
							</tr>
							<tr>
								<hr/>
							</tr>
							<tr>
								<th class="FG_th">
	                                <span style="color: red;"></span> Total Amount:
	                            </th>
	                            <td class="FG_td" style="">
                                    <input type='text' name='SUB_AMT[]' id="SUB_AMT" class='form-control' autocomplete='off' style="width: 200px;" readonly="readonly">
                                </td>
                            </tr>
						</Table>
					</div>
					<div class="modal-footer" style="padding: 10px;">
						<a href="javascript:;" data-dismiss="modal" class="btn btn-default">Close</a>
						<a href="javascript:;" onclick="calculate_sum(this);" class="btn btn-primary">Save</a>
						<!-- <button type="submit" class="btn btn-primary btn_submit">Save</button> -->
					</div>
				</form>
			</div>
		</div>
	</div>
<!--  -->

<div id="print_ajax_container_tradein" style="display:none;"></div><!-- AJAX -->
<a href="javascript:" id="return-to-top"><i class="fa fa-arrow-up"></i></a>

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


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

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

		<?php if(isset($gold_total)):?>
			$("#GOLD_TOTAL").val("<?php echo number_format((float)$gold_total, 2, '.', '');?>");
		<?php endif;?>
		<?php if(isset($goldprice_total)):?>
			$("#GOLDPRICE_TOTAL").val("<?php echo number_format((float)$goldprice_total, 2, '.', '');?>");
		<?php endif;?>
		<?php if(isset($gold_gst)):?>
			$("#GOLD_GST").val("<?php echo number_format((float)$gold_gst, 2, '.', '');?>");
		<?php endif;?>
		<?php if(isset($gold_amt)):?>
			$("#GOLD_AMT").val("<?php echo number_format((float)$gold_amt, 2, '.', '');?>");
		<?php endif;?>

        <?php if(isset($sub_array)):?>
			<?php foreach($sub_array as $sub):?>
		 		edit_sub_add();
		 		$(".edit_sub_tr_item:last [name='SUB_PKKEY[]']").val("<?php echo $sub->PKKEY;?>");
		 		$(".edit_sub_tr_item:last [name='SUB_GOLD_TITLE[]']").html("[<?php echo $sub->GOLD;?>] <?php echo $sub->GOLD_NAME;?>");
				$(".edit_sub_tr_item:last [name='SUB_GOLD[]']").val("<?php echo $sub->GOLD;?>");
				$(".edit_sub_tr_item:last [name='SUB_DESCRIPT[]']").val("<?php echo $sub->DESCRIPT;?>");
				$(".edit_sub_tr_item:last [name='SUB_DESCRIPT_TITLE[]']").html("<?php echo $sub->DESCRIPT;?>");
				$(".edit_sub_tr_item:last [name='SUB_WEIGHT[]']").val("<?php echo $sub->WEIGHT;?>");
				$(".edit_sub_tr_item:last [name='SUB_WEIGHT_TITLE[]']").html("<?php echo $sub->WEIGHT;?>");
				$(".edit_sub_tr_item:last [name='SUB_UNIT_W[]']").val("<?php echo $ci->fm->str_esc(trim($sub->UNIT_W), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_UNIT_W_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->UNIT_W), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_RATE[]']").val("<?php echo $ci->fm->str_esc(trim($sub->RATE), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_RATE_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->RATE), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_GOLDPRICE[]']").val("<?php echo $ci->fm->str_esc(trim($sub->GOLDPRICE), 2);?>");
				$(".edit_sub_tr_item:last [name='SUB_GOLDPRICE_TITLE[]']").html("<?php echo $ci->fm->str_esc(trim($sub->GOLDPRICE), 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);?>");
				$(".edit_sub_tr_item:last [name='SUB_BAL[]']").val("<?php echo $sub->BAL;?>");

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

	function edit_sub_add(){

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

		content += 	"<td style='white-space: nowrap; display:none;'>";
        content += 	"<input type='hidden' name='SUB_PKKEY[]'>";
        content += 	"<input type='hidden' name='SUB_BAL[]'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_GOLD[]' 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);' name='SUB_GOLD_TITLE[]'>";
        content += 	"</td>";

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

        content += 	"<td style='white-space: nowrap; display:none;'>";
        content +=   '<div class="form-group col-md-12">';
        content +=      "<input type='text' name='SUB_UNIT_W[]' 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);' name='SUB_UNIT_W_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_RATE[]' 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);' name='SUB_RATE_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_GOLDPRICE[]' 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: right;' onClick='edit_sub_detail(this);' name='SUB_GOLDPRICE_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);' 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);' 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: right;' onClick='edit_sub_detail(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 sub_amt' 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: right;' onClick='edit_sub_detail(this);' name='SUB_AMT_TITLE[]'>";
        content += 	"</td>";
		
		<?php if($can_delete):?>
		content += 	"<td style='text-align: center;'>";
		content += 	"<button type='button' onClick='edit_sub_remove(this);recalculate_sum(this);recalculate_sum_goldprice(this);recalculate_sum_gst(this);' class='btn btn-danger'>";
		content += 		"<i class='fa fa-trash-o'></i>";
		content += 	"</button>";
		content += 	"</td>";
		<?php endif;?>
		content += "</tr>";

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

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

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

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

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

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

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

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

    function calculate_goldprice(){
    	var WEIGHT = $(".modal-body [name='SUB_WEIGHT[]']").val();
    	var RATE = $(".modal-body [name='SUB_RATE[]']").val();

    	if(WEIGHT != "" && RATE != ""){
    		var GOLDPRICE = (WEIGHT*RATE).toFixed(2);
    		$(".modal-body [name='SUB_GOLDPRICE[]']").val(GOLDPRICE);
    	}
    }

    function calculate_gold_balance(){
    	var WEIGHT = parseFloat($(".modal-body [name='SUB_WEIGHT[]']").val());
    	var BAL = parseFloat($(".modal-body [name='SUB_BAL[]']").val());

    	if(WEIGHT != "" && BAL != ""){
    		var BALANCE = (WEIGHT+BAL).toFixed(2);
    		$(".modal-body [name='SUB_BAL[]']").val(BALANCE);
    	}
    }

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

	function find_gold_detail(t){
		var id = $("#SUB_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++) {
                    	$(".modal-body #SUB_RATE").val(info[i].RATE.trim());
                    	$(".modal-body #SUB_DESCRIPT").val(info[i].DESCRIPT.trim());
                    	$(".modal-body #SUB_TAXCODE").select2("val",info[i].TAXCODE.trim());
                    	$(".modal-body #SUB_PERCEN").val(info[i].PERCEN.trim());
                    	$(".modal-body #SUB_BAL").val(info[i].BAL.trim());
                    } 
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
            $(".modal-body #SUB_RATE").val('');
        	$(".modal-body #SUB_DESCRIPT").val('');
        	$(".modal-body #SUB_TAXCODE").select2("val",'');
        	$(".modal-body #SUB_PERCEN").val('');
        	$(".modal-body #SUB_BAL").val('');
        }
    }

	function find_tax_gold(t){
		var TAXCODE  = $(".modal-body #SUB_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 !=""){
                        $(".modal-body #SUB_PERCEN").val(result);
                        calculate_gst_gold()
                    }else{
                       $(".modal-body #SUB_PERCEN").val('');
                       $(".modal-body #SUB_GST").val('');
                    }
                },
                error: function(xhr,status,error){},
                complete: function(xhr,status){},
            });
        }else{
            $("#SUB_PERCEN").val('');
            $("#SUB_GST").val('');

        }
    }

    function calculate_gst_gold(){
    	var GOLDPRICE = parseFloat($(".modal-body #SUB_GOLDPRICE").val());
    	var PERCEN = parseFloat($(".modal-body #SUB_PERCEN").val());

    	if(GOLDPRICE != "" || PERCEN != ""){
    		var GST = ((GOLDPRICE*PERCEN)/100).toFixed(2);
    		$(".modal-body #SUB_GST").val(GST);
    		calculate_gold_amt()
    	}
    }

    function calculate_gold_amt(){
    	var GOLDPRICE = parseFloat($(".modal-body #SUB_GOLDPRICE").val());
    	var GST = parseFloat($(".modal-body #SUB_GST").val());

    	if(GOLDPRICE != "" || GST != ""){
    		var GOLD_AMT = (GOLDPRICE+GST).toFixed(2);
    		$(".modal-body #SUB_AMT").val(GOLD_AMT);
    	}
    }

	function edit_sub_detail(btn){
		var tr = $(btn).closest("tr");
		var sub_pkkey = $(tr).find("[name='SUB_PKKEY[]']").val();
		var sub_gold = $(tr).find("[name='SUB_GOLD[]']").val();
		var sub_descript = $(tr).find("[name='SUB_DESCRIPT[]']").val();
		var sub_weight = $(tr).find("[name='SUB_WEIGHT[]']").val();
		var sub_unit_w = $(tr).find("[name='SUB_UNIT_W[]']").val();
		var sub_rate = $(tr).find("[name='SUB_RATE[]']").val();
		var sub_goldprice = $(tr).find("[name='SUB_GOLDPRICE[]']").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();
		var sub_bal = $(tr).find("[name='SUB_BAL[]']").val();

		$("#Sub_modal").modal('show');
		$(".modal-body #SUB_PKKEY").val(sub_pkkey);
		$(".modal-body #SUB_GOLD").select2('val',sub_gold);
		$(".modal-body #SUB_DESCRIPT").val(sub_descript);
		$(".modal-body #SUB_WEIGHT").val(sub_weight);
		$(".modal-body #SUB_UNIT_W").select2('val',sub_unit_w);
		$(".modal-body #SUB_RATE").val(sub_rate);
		$(".modal-body #SUB_GOLDPRICE").val(sub_goldprice);
		$(".modal-body #SUB_TAXCODE").select2('val',sub_taxcode);
		$(".modal-body #SUB_PERCEN").val(sub_percen);
		$(".modal-body #SUB_GST").val(sub_gst);
		$(".modal-body #SUB_AMT").val(sub_amt);
		$(".modal-body #SUB_BAL").val(sub_bal);

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

	function calculate_sum(){
		var sum = 0;
		var sum_goldprice = 0;
		var current = parseFloat($(".modal-body #SUB_AMT").val());
		var old = parseFloat($(".modal-body #gold_amt").val());
		var old_goldprice = parseFloat($(".modal-body #goldprice_total").val());
		var current_goldprice = parseFloat($(".modal-body #SUB_GOLDPRICE").val());
		var old_weight = parseFloat($(".modal-body #gold_total").val());
		var current_weight = parseFloat($(".modal-body #SUB_WEIGHT").val());

		if(current != ""){
			sum = current + old;
			$("#gold_amt1").html(sum);
			$("#gold_amt").val(sum);
			$(".modal-body #gold_amt").val(sum);
		}

		if(current_goldprice != ""){
			sum_goldprice = current_goldprice + old_goldprice;
			$("#goldprice_total1").html(sum_goldprice);
			$("#goldprice_total").val(sum_goldprice);
			$(".modal-body #goldprice_total").val(sum_goldprice);
		}

		if(current_goldprice != ""){
			sum_weight = current_weight + old_weight;
			$("#gold_total1").html(sum_weight);
			$("#gold_total").val(sum_weight);
			$(".modal-body #gold_total").val(sum_weight);
		}
		
		$("#sub_form").submit();
	}

	function recalculate_sum_goldprice(btn){
		var sum = 0;
		$(".edit_sub_tr_item [name='SUB_GOLDPRICE[]']").each(function(index, element){
			sum += parseFloat($(this).val());
			$("#gold_total1").html(sum);
			$("#gold_total").val(sum);
			$(".modal-body #gold_total").val(sum);
		});
	}

	function recalculate_sum_gst(btn){
		var sum = 0;
		$(".edit_sub_tr_item [name='SUB_GST[]']").each(function(index, element){
			sum += parseFloat($(this).val());
			$("#gold_gst").html(sum);
		});
	}

	function recalculate_sum(btn){
		var sum = 0;
		$(".edit_sub_tr_item [name='SUB_AMT[]']").each(function(index, element){
			sum += parseFloat($(this).val());
			$("#gold_amt1").html(sum);
			$("#gold_amt").val(sum);
			$(".modal-body #gold_amt").val(sum);
		});
	}

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

		if(pkkey != "" && x){	
			var content = "<input type='hidden' name='DEL_SUB_PKKEY[]' value='" + pkkey + "'>";
			$(btn).closest("form").append(content);
			$(tr).remove();
			if(pkkey == first_pkkey){
				$("#gold_total1").html(0);
				$("#gold_total").val(0);
				$("#gold_gst").html(0);
				$("#gold_amt1").html(0);
				$("#gold_amt").val(0);
			}
		}else if(pkkey == "" && x){	
			$(tr).remove();
			if(pkkey == first_pkkey){
				$("#gold_total1").html(0);
				$("#gold_total").val(0);
				$("#gold_gst").html(0);
				$("#gold_amt1").html(0);
				$("#gold_amt").val(0);
			}
		}else{
			return false;
		}

	}

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

	}

</script>