<div class="main-content">
				<div class="main-content-inner">
				<div class="page-content">
			
						<div class="page-header">
							<h1>
								MES PO
								<!--small>
									<i class="ace-icon fa fa-angle-double-right"></i>
									Static &amp; Dynamic Tables
								</small-->
							</h1>
                                                    
						</div><!-- /.page-header -->

<div class="space"></div>
<input type="hidden" id="temp_id"  name="temp_id" placeholder="" value="<?php echo $temp_id;?> "class="col-xs-12 col-sm-12" />
                                                                
                                   <div class="row">
                                                    <div class="col-sm-3">  
                                                        Supplier
                                                        <select class="chosen-select form-control" id="supselect" data-placeholder="Choose a Supplier..." name="sup">
							<option value="0"> Select Supplier </option>
                                                         <?php 
                                                         foreach ($agentdetail as $dbagent) {            
                                                          ?>
							<option value="<?php echo $dbagent->sup_id; ?>"><?php echo $dbagent->sup_name; ?></option>
                                                          <?php }?>                                                           
                                                        </select>
                                                        <input type="hidden" name="sup_type" value="" id="sup_type">                          
                                                    </div>
                                                    <div class="col-sm-3"><br>
                                                       <!--button type="submit" class="btn btn-sm btn-primary btn-white btn-round" name="showhide" id="showhide">
                                                                                <i class="ace-icon fa fa-pencil-square-o bigger-150 middle orange2"></i>
                                                                                <span class="bigger-110">Show/Hide</span>

							</button-->
                                                                  </div> 
                                                   <div class="col-sm-3">
                                                           MES Ref NO
                                                           <input type="text" id="mes_ref"  name="mes_ref" placeholder="" value=""class="col-xs-12 col-sm-12" /> 
                                                    </div>
                                                    
                                                    <div class="col-sm-3">
                                                           Sup Ref NO
                                                           <input type="text" id="sup_ref"  name="sup_ref" placeholder="" value=""class="col-xs-12 col-sm-12" /> 
                                                    </div>
                                                   
                                                   
                                                    
                                                   
                                                         
                                                     
                                               </div>
                                               
                                               <div class="row">
                                                    <div class="col-sm-3">  
                                                        Catagory
                                                        <select class="chosen-select form-control" id="catselect" data-placeholder="Choose a cat..." name="cat">
							<option value="0"> Select Category </option>
                                                         <?php 
                                                         foreach ($category as $cat) {            
                                                          ?>
							<option value="<?php echo $cat->cat_id; ?>"><?php echo $cat->cat_name; ?></option>
                                                          <?php }?>                                                           
                                                        </select>                          
                                                    </div>
                                                   <div class="col-sm-3">
                                                           Brand
                                                            <select class="form-control" id="brandselect" data-placeholder="Choose a brand..." name="brand">
							                                    <option value="0"> Select Brand </option>                                                         
                                                        </select> 
                                                    </div>
                                                    <div class="col-sm-3">   
                                                        Product/Service
                                                        <select class="form-control" id="itemselect" data-placeholder="Choose a State..." name="itemselect">
							                                <option value="">  </option>                                                          
                                                        </select>                          
                                                    </div>
                                                   
                                                              <div class="col-sm-3">
                                                            Part Number
                                                            <select class="form-control" id="partselect" data-placeholder="Choose a State..." name="partselect">
							<option value="">  </option>
                                                                                                                 
                                                        </select>
                                                              </div>
                                                    
                                                         
                                                     
                                               </div>
                                               
                                               <div class="row">
                                                   
                                                   <div class="col-sm-2">
                                                            Price (LKR)
                                                        <input type="text" id="price"  name="price" placeholder="" value="<?php  ?> "class="col-xs-12 col-sm-12" />
                                                                <?php echo form_error('productqty'); ?>
                                                    </div>
                                                    <div class="col-sm-1">
                                                            Cur
                                                        <input type="text" id="cur"  name="cur" placeholder="" value="<?php  ?> "class="col-xs-12 col-sm-12" />
                                                        <input type="hidden" id="curid"  name="curid" placeholder="" value="<?php  ?> "class="col-xs-12 col-sm-12" />
                                                        
                                                        
                                                       
                                                                
                                                    </div>
                                                    
                                                    <div class="col-sm-2">
                                                            Quantity
                                                        <input type="text" id="qty"  name="qty" placeholder="" value="<?php  ?> "class="col-xs-12 col-sm-12" onkeyup="sin_item_tot()"/>
                                                                <?php echo form_error('productqty'); ?>
                                                    </div>
                                                    
                                                    <div class="col-sm-1">
                                                            Unit
                                                        <input type="text" id="unit"  name="unit" placeholder="" value="<?php  ?> "class="col-xs-12 col-sm-12" />
                                                        <input type="hidden" id="unitid"  name="unitid" placeholder="" value="<?php  ?> "class="col-xs-12 col-sm-12" />
                                                        
                                                                
                                                    </div>
                                                    
                                                    
                                                    
                                                    <div class="col-sm-2">
                                                            Total
                                                        <input type="text" id="total"  name="total" placeholder="" value="<?php  ?> "class="col-xs-12 col-sm-12" />
                                                                <?php echo form_error('productqty'); ?>
                                                    </div>
                                                    
                                                   <div class="col-sm-2"><br>
                                                        
                                                       <button type="button" class="btn btn-sm btn-primary btn-white btn-round" name="add" id="addrow" onclick="addrow();">
                                                                                <i class="ace-icon fa fa-pencil-square-o bigger-150 middle orange2"></i>
                                                                                <span class="bigger-110">Add</span></button>
                                                                                
                                                                                

							</button>
                                                                  </div> 
                                                </div>    
                                               
                                         <div class="space"></div>
                                        
                                         <div class="row" id="agent" style="display: none">
                                            <div class="col-sm-3">
                                                            Contact
                                                        <input type="text" id="ag_cont"  name="ag_cont" placeholder="Contact Detail" value="" class="col-xs-12 col-sm-12" />
                                                                
                                                    </div>
                                            <div class="col-sm-3">
                                                            Credit Limit
                                                        <input type="text" id="ag_limit"  name="ag_limit" placeholder="" value=""class="col-xs-12 col-sm-12" />
                                                              
                                                    </div>
                                            <div class="col-sm-3">
                                                            Credit Time
                                                        <input type="text" id="ag_time"  name="ag_time" placeholder="" value="" class="col-xs-12 col-sm-12" />
                                                               
                                                    </div>
                                            <div class="col-sm-3">
                                                            Balance
                                                        <input type="text" id="ag_bal"  name="ag_bal" placeholder="" value="" class="col-xs-12 col-sm-12" />
                                                                
                                                    </div>
                                            
                                        </div>
                                        
                                        
        

  <div class="space"></div>
  
            <div class="row">
                    
            </div>
  
  
   <div class="space"></div>
  
  <div class="row">
									<div class="col-xs-12">
										<table id="simple-table" class="table  table-bordered table-hover">
											<thead>
												<tr>
													
													<th class="detail-col">Items</th>
													<th>Items Description</th>
													<th><span id="cr_chng"> Price</span> </th>
													<th >Quantity </th>
                                                                                                        <th >Total </th>

													
													

													<th>Action</th>
												</tr>
											</thead>

											<tbody id="show_data">
									
								
											</tbody>
										</table>
									</div><!-- /.span -->
                                                                        
                                                                        
								</div><!-- /.row -->
                                                                
                                                                
                                                                                  
                                                
                                                                <div class="row">
                                                                       <div class="col-sm-3 pull-right">
                                                                              Total
                                                                            <input type="text" id="inv_total"  name="inv_total" placeholder="Total" value="0" class="col-xs-12 col-sm-12" />
                                                                        </div>
                                                                        <div class="col-sm-3 pull-right">
                                                                              Total Quantity
                                                                            <input type="text" id="inv_tot_item"  name="inv_tot_item" placeholder="Total" value="0" class="col-xs-12 col-sm-12" />
                                                                        </div>
                                                                </div> 
                                                                
                                                                    <div class="row">
                                                                        
                                                                       <div class="col-sm-3 pull-right">
                                                                              Discounted Amount
                                                                            <input type="text" id="dis_total"  name="dis_total" placeholder="%" value="0" class="col-xs-12 col-sm-12" onkeyup="cal_dis_amt();" />
                                                                        </div>
                                                                        <div class="col-sm-3 pull-right">
                                                                              Discount %
                                                                              <input type="text" id="dis_pc"  name="dis_pc" placeholder="Dis_amount" value="0" class="col-xs-12 col-sm-12" onkeyup="cal_dis_pc();" />
                                                                        <div class="help-block" id="input-span-slider"></div>
                                                                        </div>
                                                                </div>
                                                                <div class="row">
                                                                        
                                                                       <div class="col-sm-3 pull-right">
                                                                              Other Amount
                                                                            <input type="text" id="other_amnt"  name="other_amnt" placeholder="Other" value="0.00" class="col-xs-12 col-sm-12" onkeyup="cal_total();" />
                                                                        </div>
                                                                        <div class="col-sm-3 pull-right">
                                                                              Other - Reason
                                                                              <input type="text" id="other_reason"  name="other_reason" placeholder="Total" value="Other" class="col-xs-12 col-sm-12" onkeyup="cal_dis_pc();" />
                                                                        <div class="help-block" id="input-span-slider"></div>
                                                                        </div>
                                                                </div>
                                                                <div id="for_foreign" style="display: none;">
                                                                    <div class="row">
                                                                            <div class="col-sm-3 pull-right">
                                                                                 Banking Fee
                                                                                  <input type="text" id="bank_fee"  name="bank_fee" placeholder="bank_fee" value="0.00" class="col-xs-12 col-sm-12" onkeyup="cal_total();" />
                                                                            <div class="help-block" id="input-span-slider"></div>
                                                                            </div>
                                                                    </div>
                                                                    <div class="row">
                                                                            <div class="col-sm-3 pull-right">
                                                                                 Shipping & Handling 
                                                                                  <input type="text" id="ship"  name="ship" placeholder="ship" value="0.00" class="col-xs-12 col-sm-12" onkeyup="cal_total();" />
                                                                            <div class="help-block" id="input-span-slider"></div>
                                                                            </div>
                                                                    </div>
                                                                </div>
                                                                <div id="for_tax" style="display: none;">
                                                                    <div class="row">
                                                                            <div class="col-sm-3 pull-right">
                                                                                 VAT 10% <input type="checkbox" id="chk_vat" name="chk_vat" value="10" onclick="myvat()">
                                                                                  <input type="text" id="vat"  name="vat" placeholder="vat" value="0.00" class="col-xs-12 col-sm-12" onkeyup="cal_total();" />
                                                                            <div class="help-block" id="input-span-slider"></div>
                                                                            </div>
                                                                    </div>
                                                                    <div class="row">
                                                                            <div class="col-sm-3 pull-right">
                                                                                 NBT 5% <input type="checkbox" id="chk_nbt" name="chk_nbt" value="10" onclick="mynbt()">
                                                                                  <input type="text" id="nbt"  name="nbt" placeholder="nbt" value="0.00" class="col-xs-12 col-sm-12" onkeyup="cal_total();" />
                                                                            <div class="help-block" id="input-span-slider"></div>
                                                                            </div>
                                                                    </div>
                                                                </div>
                                                                <div class="row">
                                                                        
                                                                       <div class="col-sm-3 pull-right">
                                                                              Total amount
                                                                            <input type="text" id="final"  name="final" placeholder="Total" value="0" class="col-xs-12 col-sm-12" />
                                                                        </div>
                                                                        </div>
                                                                
                                    
                                     <div class="row">
                                                                        
                                        <div class="col-sm-3 center-block">
                                            <button class="btn btn-white btn-info btn-bold" id="btn_save">
                                                    <i class="ace-icon fa fa-floppy-o bigger-120 blue"></i>
                                                   Save
                                            </button>
                                        </div>
                                         <div class="col-sm-3 center-block">
                                             <button class="btn btn-white btn-info btn-bold" id="btn_draft">
                                                    <i class="ace-icon fa fa-floppy-o bigger-120 blue"></i>
                                                    Save for Later
                                            </button>
                                        </div>
                                         <div class="col-sm-3 center-block">
                                            <button class="btn btn-white btn-info btn-bold" id="btn_cancel">
                                                    <i class="ace-icon fa fa-times red2"></i>
                                                    Cancel invoice
                                            </button>
                                        </div>
                                    </div>
                                    
                                                                
<!-- MODAL EDIT -->
        <form>
            <div class="modal fade" id="Modal_Edit" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
              <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Edit Product</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                        <div class="form-group row">
                            <label class="col-md-2 col-form-label">Product ID</label>
                            <div class="col-md-10">
                              <input type="text" name="product_code_edit" id="product_code_edit" class="form-control" placeholder="Product Code" readonly>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-md-2 col-form-label">Product</label>
                            <div class="col-md-10">
                                <input type="text" name="product_name_edit" id="product_name_edit" class="form-control" placeholder="Product Name" readonly="">
                            </div>
                        </div>
                      <div class="form-group row">
                            <label class="col-md-2 col-form-label">Unit Price</label>
                            <div class="col-md-10">
                                <input type="text" name="product_prc_edit" id="product_prc_edit" class="form-control" placeholder="Product Name" readonly="">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-md-2 col-form-label">Quantity</label>
                            <div class="col-md-10">
                              <input type="text" name="price_edit" id="price_edit" class="form-control" placeholder="Price">
                            </div>
                        </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" type="submit" id="btn_update" class="btn btn-primary">Update</button>
                  </div>
                </div>
              </div>
            </div>
            </form>
        <!--END MODAL EDIT-->
        
        
        <!--MODAL DELETE-->
         <form>
            <div class="modal fade" id="Modal_Delete" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Delete Product</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                       <strong>Are you sure to delete this record?</strong>
                  </div>
                  <div class="modal-footer">
                    <input type="hidden" name="product_code_delete" id="product_code_delete" class="form-control">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
                    <button type="button" type="submit" id="btn_delete" class="btn btn-primary">Yes</button>
                  </div>
                </div>
              </div>
            </div>
            </form>
        <!--END MODAL DELETE-->
							
                                                         
                                                            
								<!-- PAGE CONTENT ENDS -->
							</div><!-- /.col -->
						</div><!-- /.row -->
					</div><!-- /.page-content -->
				<!-- /.main-content -->
                        
<script type="text/javascript">
    
         
    function sin_item_tot(){
        var total = $('#price').val() * $('#qty').val();
        $('[name="total"]').val(total);
    }

    function cal_dis_amt(){
      $('#dis_pc').val("");
      cal_total();
    }

    function cal_dis_pc(){
        var pc = $('#dis_pc').val();
        var dis = ($('#inv_total').val() * pc)/100;
      $('#dis_total').val(dis);
      cal_total();
    }

    function myvat(){
      var checkBox = document.getElementById("chk_vat");
            if (checkBox.checked == true){
          var vat = ($('#inv_total').val() * 10)/100;
        } else {
          var vat = 0;
        }
      
      $('[name="vat"]').val(vat);
      cal_total();
    }

    function mynbt(){
      var checkBox = document.getElementById("chk_nbt");
            if (checkBox.checked == true){
          var nbt = ($('#inv_total').val() * 5)/100;
        } else {
          var nbt = 0;
        }
      
      $('[name="nbt"]').val(nbt);
      cal_total();
    }

    function cal_total(){
     // if($('#dis_pc').val()!=""){
      //  var dis =  ($('#dis_pc').val() * $('#inv_total').val())/100;

      //}else
      if($('#dis_total').val()!=""){
        var dis =  $('#dis_total').val();
      }else{ var dis = 0;}
      
        var other = $('#other_amnt').val();
        var vat = $('#vat').val();
        var nbt = $('#nbt').val();
        var ship = $('#ship').val();
        var bank = $('#bank_fee').val();
      

        var total = parseInt($('#inv_total').val()) - parseInt(dis) + parseInt(other)+ parseInt(vat)+ parseInt(nbt)+ parseInt(ship)+ parseInt(bank);
        $('[name="final"]').val(total);
    }


    //function show all product
        function show_product(){
        var tempid = $('#temp_id').val();
            $.ajax({
                type  : 'ajax',
                url   : '<?php echo base_url(); ?>Invoice_Controller/getinvoice_product/'+tempid+'',
                async : true,
                dataType : 'json',
                success : function(data){
                  console.log(data);
                    var html = '';
                    var i;
                    var tot = 0;
                    var tot_item = 0;
                    for(i=0; i<data.length; i++){
                        var tot = tot + parseInt((data[i].po_itm_unitprice * data[i].po_itm_qty));
                        var tot_item = tot_item + parseInt(data[i].po_itm_qty);
                        html += '<tr>'+
                                //'<td>'+(i+1)+'</td>'+
                                '<td>'+(i+1)+'</td>'+
                                '<td>'+data[i].item_description+'<br>'+data[i].part_no+'</td>'+
                                '<td>'+data[i].po_itm_unitprice+'</td>'+
                                '<td>'+data[i].po_itm_qty+'</td>'+
                                '<td>'+(data[i].po_itm_unitprice * data[i].po_itm_qty)+'</td>'+
                                '<td style="text-align:right;">'+
                                '<div class="hidden-sm hidden-xs btn-group">'+
                                    //'<a href="javascript:void(0);" class="btn btn-xs btn-success item_edit" data-product_code="'+data[i].inv_pro_id+'" data-product_price="'+data[i].product_name+'" data-product_prc="'+data[i].unit_price+'" data-qty="'+data[i].qty+'"><i class="ace-icon fa fa-pencil bigger-120"></i></a>'+' '+
                                    '<a href="javascript:void(0);" class="btn btn-xs btn-danger item_delete" data-product_code="'+data[i].po_itm_id+'"><i class="ace-icon fa fa-trash-o bigger-120"></i></a>'+
                               '</div>'+
                                '</td>'+
                                '</tr>';
                    }
                    $('#show_data').html(html);
                    $('[name="inv_total"]').val(tot);
                    $('[name="inv_tot_item"]').val(tot_item);
                    cal_total();
                }
 
            });
        }

      

    
     $(document).ready(function(){
     show_product();
     //get agent detilas
      

       
          

});  


        //$('#addrow').on('click',function(){
            function addrow(){
           // alert("test");
            var price = $('#price').val();
            var qty = $('#qty').val();
            var e = document.getElementById("itemselect");
            var item = e.options[e.selectedIndex].value;
            var f = document.getElementById("supselect");
            var sup_id = f.options[f.selectedIndex].value;

            var f = document.getElementById("partselect");
            var prt_id = f.options[f.selectedIndex].value;
            
            var dis_price = 1;
            var unit = $('#unitid').val();
            var cur = $('#curid').val();
            var tempid = $('#temp_id').val();
           // if(dis_price==="1"){
                //    var tot = price * qty;
               // }else{
                   var tot = price * qty; 
            
            $.ajax({
                type : "POST",
                url  : "<?php echo site_url('Invoice_Controller/addinvoice_product')?>",
                dataType : "JSON",
                data : {temp_id:tempid, price:price , qty:qty, item:item, sup:sup_id,unit:unit,cur:cur,prt_id:prt_id},
                success: function(data){
                    
                       // alert(data);
                    
                    //$("#agentselect").prop("disabled", false);
                   // $('#agentselect').prop('disabled', 'disabled');
                    show_product();
                    
                }
            });
            return false;
        };
    
    $('#catselect').change(function(){ 
       
                $('#brandselect option').remove();
                $('#itemselect option').remove();
                
          var id = $(this).val();
          
             $.ajax({
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('Invoice_Controller/getbrand')?>',
                dataType : 'json',
                data : {u_id:id},
                success : function(data){
                    $('#itemselect').append($('<option>', {
                                                            value: "0",
                                                            text: "select"
                                                        }));
                    var i;
                    for(i=0; i<data['brand'].length; i++){
                                                $('#brandselect').append($('<option>', {
                                                            value: data['brand'][i].brand_id,
                                                            text: data['brand'][i].brand_name
                                                        }));
                    }
                    
                    var i;
                    for(i=0; i<data['item'].length; i++){
                        
                                                $('#itemselect').append($('<option>', {
                                                            value: data['item'][i].item_id,
                                                            text: data['item'][i].item_description
                                                        }));
                    }
					
                }
 
            }); 
      });
      
      $('#supselect').change(function(){ 
                
          var id = $(this).val();
          
             $.ajax({
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('Invoice_Controller/getsup')?>',
                dataType : 'json',
                data : {sup_id:id},
                success : function(data){
                    $('[name="cur"]').val(data['sup'][0].curr_symble);
                    $('[name="curid"]').val(data['sup'][0].curr_id);
                    $('[name="sup_type"]').val(data['sup'][0].sup_type);
                    if(data['sup'][0].sup_type=="Foreign"){
                      var txt = document.getElementById("for_foreign");
                      txt.style.display = "block";
                    }else{
                      var txt = document.getElementById("for_foreign");
                      txt.style.display = "none";
                    }
                    if(data['sup'][0].sup_tax == 1){
                      var txt = document.getElementById("for_tax");
                      txt.style.display = "block";
                    }else{
                      var txt = document.getElementById("for_tax");
                      txt.style.display = "none";
                    }
                    
                }
 
            }); 
      });
      
      
    $('#itemselect').change(function(){ 
       
                $('#partselect option').remove();
                
          var id = $(this).val();
          var f = document.getElementById("supselect");
            var sup_id = f.options[f.selectedIndex].value;
          
             $.ajax({
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('Invoice_Controller/getpart')?>',
                dataType : 'json',
                data : {u_id:id,sup_id:sup_id},
                success : function(data){

                  $('#partselect').append($('<option>', {
                                                            value: "0",
                                                            text: "select"
                                                        }));
                     
                    var i;
                    for(i=0; i<data['part'].length; i++){
                                                $('#partselect').append($('<option>', {
                                                            value: data['part'][i].part_id,
                                                            text: data['part'][i].part_no
                                                        }));
                    }
                   
                    $('[name="price"]').val(data['itm'][0].item_selling_price);
					$('[name="unit"]').val(data['itm'][0].unit_symble);
					$('[name="unitid"]').val(data['itm'][0].unit_id);
					$('[name="cur"]').val(data['sup'][0].curr_symble);
					$('[name="curid"]').val(data['sup'][0].curr_id);
                }
 
            }); 
      });  
      
              //get data for delete record
        $('#show_data').on('click','.item_delete',function(){
            var product_code = $(this).data('product_code');
             //alert(product_code);
            $('#Modal_Delete').modal('show');
            $('[name="product_code_delete"]').val(product_code);
        });
 
        //delete record to database
        $('#btn_delete').on('click',function(){
            var product_code = $('#product_code_delete').val();
            $.ajax({
                type : "POST",
                url  : "<?php echo site_url('Invoice_Controller/delete')?>",
                dataType : "JSON",
                data : {product_code:product_code},
                success: function(data){
                    $('[name="product_code_delete"]').val("");
                    $('#Modal_Delete').modal('hide');
                    show_product();
                }
            });
            return false;
        });

                //Save data to invoice
        $('#btn_save').on('click',function(){
            var e = document.getElementById("supselect");
            var agent_id = e.options[e.selectedIndex].value;
            var temp_id = $('#temp_id').val();
            var qty = $('#inv_tot_item').val();
            var total = $('#inv_total').val();
            var dis = $('#dis_total').val();
            var dis_val = $('#dis_total').val();
            var final = $('#final').val();
            var mes_ref = $('#mes_ref').val();
            var sup_ref = $('#sup_ref').val();
            var bank_fee = $('#bank_fee').val();
            var ship = $('#ship').val();
            var vat = $('#vat').val();
            var nbt = $('#nbt').val();

            var other = $('#other_amnt').val();
            var cur = $('#curid').val();
            var unit = $('#unitid').val();
            var sup_type = $('#sup_type').val();
       
                
                $.ajax({
                type : "POST",
                url  : "<?php echo site_url('Invoice_Controller/save_inv')?>",
                dataType : "JSON",
                data : {agent_id:agent_id, temp_id:temp_id, qty:qty, total:total, dis:dis, dis_val:dis_val, final:final,mes_ref:mes_ref,sup_ref:sup_ref,bank_fee:bank_fee,ship:ship,vat:vat,nbt:nbt,other:other,cur:cur,unit:unit,sup_type:sup_type},
                success: function(data){
                    
                    //show_product()
                    alert('Data Saved');
                    location.href = "<?php echo site_url('Invoice_Controller/view_pdf')?>/"+data;
                }
            });
            return false;
            
          });
        
   
     
			jQuery(function($) {
				$('#id-disable-check').on('click', function() {
					var inp = $('#form-input-readonly').get(0);
					if(inp.hasAttribute('disabled')) {
						inp.setAttribute('readonly' , 'true');
						inp.removeAttribute('disabled');
						inp.value="This text field is readonly!";
					}
					else {
						inp.setAttribute('disabled' , 'disabled');
						inp.removeAttribute('readonly');
						inp.value="This text field is disabled!";
					}
				});
                                
                              
			
			
				if(!ace.vars['touch']) {
					$('.chosen-select').chosen({allow_single_deselect:true}); 
					//resize the chosen on window resize
			
					$(window)
					.off('resize.chosen')
					.on('resize.chosen', function() {
						$('.chosen-select').each(function() {
							 var $this = $(this);
							 $this.next().css({'width': $this.parent().width()});
						})
					}).trigger('resize.chosen');
					//resize chosen on sidebar collapse/expand
					$(document).on('settings.ace.chosen', function(e, event_name, event_val) {
						if(event_name != 'sidebar_collapsed') return;
						$('.chosen-select').each(function() {
							 var $this = $(this);
							 $this.next().css({'width': $this.parent().width()});
						})
					});
			
			
					$('#chosen-multiple-style .btn').on('click', function(e){
						var target = $(this).find('input[type=radio]');
						var which = parseInt(target.val());
						if(which == 2) $('#form-field-select-4').addClass('tag-input-style');
						 else $('#form-field-select-4').removeClass('tag-input-style');
					});
				}
			
			
				$('[data-rel=tooltip]').tooltip({container:'body'});
				$('[data-rel=popover]').popover({container:'body'});
			
				autosize($('textarea[class*=autosize]'));
				
				$('textarea.limited').inputlimiter({
					remText: '%n character%s remaining...',
					limitText: 'max allowed : %n.'
				});
			
				$.mask.definitions['~']='[+-]';
				$('.input-mask-date').mask('99/99/9999');
				$('.input-mask-phone').mask('(999) 999-9999');
				$('.input-mask-eyescript').mask('~9.99 ~9.99 999');
				$(".input-mask-product").mask("a*-999-a999",{placeholder:" ",completed:function(){alert("You typed the following: "+this.val());}});
			
			
			
				$( "#input-size-slider" ).css('width','200px').slider({
					value:1,
					range: "min",
					min: 1,
					max: 8,
					step: 1,
					slide: function( event, ui ) {
						var sizing = ['', 'input-sm', 'input-lg', 'input-mini', 'input-small', 'input-medium', 'input-large', 'input-xlarge', 'input-xxlarge'];
						var val = parseInt(ui.value);
						$('#form-field-4').attr('class', sizing[val]).attr('placeholder', '.'+sizing[val]);
					}
				});
			
				$( "#input-span-slider" ).slider({
					value:1,
					range: "min",
					min: 1,
					max: 12,
					step: 1,
					slide: function( event, ui ) {
						var val = parseInt(ui.value);
						$('#form-field-5').attr('class', 'col-xs-'+val).val('.col-xs-'+val);
					}
				});
			
			
				
				//"jQuery UI Slider"
				//range slider tooltip example
				$( "#slider-range" ).css('height','200px').slider({
					orientation: "vertical",
					range: true,
					min: 0,
					max: 100,
					values: [ 17, 67 ],
					slide: function( event, ui ) {
						var val = ui.values[$(ui.handle).index()-1] + "";
			
						if( !ui.handle.firstChild ) {
							$("<div class='tooltip right in' style='display:none;left:16px;top:-6px;'><div class='tooltip-arrow'></div><div class='tooltip-inner'></div></div>")
							.prependTo(ui.handle);
						}
						$(ui.handle.firstChild).show().children().eq(1).text(val);
					}
				}).find('span.ui-slider-handle').on('blur', function(){
					$(this.firstChild).hide();
				});
				
				
				$( "#slider-range-max" ).slider({
					range: "max",
					min: 1,
					max: 10,
					value: 2
				});
				
				$( "#slider-eq > span" ).css({width:'90%', 'float':'left', margin:'15px'}).each(function() {
					// read initial values from markup and remove that
					var value = parseInt( $( this ).text(), 10 );
					$( this ).empty().slider({
						value: value,
						range: "min",
						animate: true
						
					});
				});
				
				$("#slider-eq > span.ui-slider-purple").slider('disable');//disable third item
			
				
				$('#id-input-file-1 , #id-input-file-2').ace_file_input({
					no_file:'No File ...',
					btn_choose:'Choose',
					btn_change:'Change',
					droppable:false,
					onchange:null,
					thumbnail:false //| true | large
					//whitelist:'gif|png|jpg|jpeg'
					//blacklist:'exe|php'
					//onchange:''
					//
				});
				//pre-show a file name, for example a previously selected file
				//$('#id-input-file-1').ace_file_input('show_file_list', ['myfile.txt'])
			
			
				$('#id-input-file-3').ace_file_input({
					style: 'well',
					btn_choose: 'Drop files here or click to choose',
					btn_change: null,
					no_icon: 'ace-icon fa fa-cloud-upload',
					droppable: true,
					thumbnail: 'small'//large | fit
					//,icon_remove:null//set null, to hide remove/reset button
					/**,before_change:function(files, dropped) {
						//Check an example below
						//or examples/file-upload.html
						return true;
					}*/
					/**,before_remove : function() {
						return true;
					}*/
					,
					preview_error : function(filename, error_code) {
						//name of the file that failed
						//error_code values
						//1 = 'FILE_LOAD_FAILED',
						//2 = 'IMAGE_LOAD_FAILED',
						//3 = 'THUMBNAIL_FAILED'
						//alert(error_code);
					}
			
				}).on('change', function(){
					//console.log($(this).data('ace_input_files'));
					//console.log($(this).data('ace_input_method'));
				});
				
				
				//$('#id-input-file-3')
				//.ace_file_input('show_file_list', [
					//{type: 'image', name: 'name of image', path: 'http://path/to/image/for/preview'},
					//{type: 'file', name: 'hello.txt'}
				//]);
			
				
				
			
				//dynamically change allowed formats by changing allowExt && allowMime function
				$('#id-file-format').removeAttr('checked').on('change', function() {
					var whitelist_ext, whitelist_mime;
					var btn_choose
					var no_icon
					if(this.checked) {
						btn_choose = "Drop images here or click to choose";
						no_icon = "ace-icon fa fa-picture-o";
			
						whitelist_ext = ["jpeg", "jpg", "png", "gif" , "bmp"];
						whitelist_mime = ["image/jpg", "image/jpeg", "image/png", "image/gif", "image/bmp"];
					}
					else {
						btn_choose = "Drop files here or click to choose";
						no_icon = "ace-icon fa fa-cloud-upload";
						
						whitelist_ext = null;//all extensions are acceptable
						whitelist_mime = null;//all mimes are acceptable
					}
					var file_input = $('#id-input-file-3');
					file_input
					.ace_file_input('update_settings',
					{
						'btn_choose': btn_choose,
						'no_icon': no_icon,
						'allowExt': whitelist_ext,
						'allowMime': whitelist_mime
					})
					file_input.ace_file_input('reset_input');
					
					file_input
					.off('file.error.ace')
					.on('file.error.ace', function(e, info) {
						//console.log(info.file_count);//number of selected files
						//console.log(info.invalid_count);//number of invalid files
						//console.log(info.error_list);//a list of errors in the following format
						
						//info.error_count['ext']
						//info.error_count['mime']
						//info.error_count['size']
						
						//info.error_list['ext']  = [list of file names with invalid extension]
						//info.error_list['mime'] = [list of file names with invalid mimetype]
						//info.error_list['size'] = [list of file names with invalid size]
						
						
						/**
						if( !info.dropped ) {
							//perhapse reset file field if files have been selected, and there are invalid files among them
							//when files are dropped, only valid files will be added to our file array
							e.preventDefault();//it will rest input
						}
						*/
						
						
						//if files have been selected (not dropped), you can choose to reset input
						//because browser keeps all selected files anyway and this cannot be changed
						//we can only reset file field to become empty again
						//on any case you still should check files with your server side script
						//because any arbitrary file can be uploaded by user and it's not safe to rely on browser-side measures
					});
					
					
					/**
					file_input
					.off('file.preview.ace')
					.on('file.preview.ace', function(e, info) {
						console.log(info.file.width);
						console.log(info.file.height);
						e.preventDefault();//to prevent preview
					});
					*/
				
				});
			
				$('#spinner1').ace_spinner({value:0,min:0,max:200,step:10, btn_up_class:'btn-info' , btn_down_class:'btn-info'})
				.closest('.ace-spinner')
				.on('changed.fu.spinbox', function(){
					//console.log($('#spinner1').val())
				}); 
				$('#spinner2').ace_spinner({value:0,min:0,max:10000,step:100, touch_spinner: true, icon_up:'ace-icon fa fa-caret-up bigger-110', icon_down:'ace-icon fa fa-caret-down bigger-110'});
				$('#spinner3').ace_spinner({value:0,min:-100,max:100,step:10, on_sides: true, icon_up:'ace-icon fa fa-plus bigger-110', icon_down:'ace-icon fa fa-minus bigger-110', btn_up_class:'btn-success' , btn_down_class:'btn-danger'});
				$('#spinner4').ace_spinner({value:0,min:-100,max:100,step:10, on_sides: true, icon_up:'ace-icon fa fa-plus', icon_down:'ace-icon fa fa-minus', btn_up_class:'btn-purple' , btn_down_class:'btn-purple'});
			
				//$('#spinner1').ace_spinner('disable').ace_spinner('value', 11);
				//or
				//$('#spinner1').closest('.ace-spinner').spinner('disable').spinner('enable').spinner('value', 11);//disable, enable or change value
				//$('#spinner1').closest('.ace-spinner').spinner('value', 0);//reset to 0
			
			
				//datepicker plugin
				//link
				$('.date-picker').datepicker({
					autoclose: true,
					todayHighlight: true
				})
				//show datepicker when clicking on the icon
				.next().on(ace.click_event, function(){
					$(this).prev().focus();
				});
			
				//or change it into a date range picker
				$('.input-daterange').datepicker({autoclose:true});
			
			
				//to translate the daterange picker, please copy the "examples/daterange-fr.js" contents here before initialization
				$('input[name=date-range-picker]').daterangepicker({
					'applyClass' : 'btn-sm btn-success',
					'cancelClass' : 'btn-sm btn-default',
					locale: {
						applyLabel: 'Apply',
						cancelLabel: 'Cancel',
					}
				})
				.prev().on(ace.click_event, function(){
					$(this).next().focus();
				});
			
			
				$('#timepicker1').timepicker({
					minuteStep: 1,
					showSeconds: true,
					showMeridian: false,
					disableFocus: true,
					icons: {
						up: 'fa fa-chevron-up',
						down: 'fa fa-chevron-down'
					}
				}).on('focus', function() {
					$('#timepicker1').timepicker('showWidget');
				}).next().on(ace.click_event, function(){
					$(this).prev().focus();
				});
				
				
			
				
				if(!ace.vars['old_ie']) $('#date-timepicker1').datetimepicker({
				 //format: 'MM/DD/YYYY h:mm:ss A',//use this option to display seconds
				 icons: {
					time: 'fa fa-clock-o',
					date: 'fa fa-calendar',
					up: 'fa fa-chevron-up',
					down: 'fa fa-chevron-down',
					previous: 'fa fa-chevron-left',
					next: 'fa fa-chevron-right',
					today: 'fa fa-arrows ',
					clear: 'fa fa-trash',
					close: 'fa fa-times'
				 }
				}).next().on(ace.click_event, function(){
					$(this).prev().focus();
				});
				
			
				$('#colorpicker1').colorpicker();
				//$('.colorpicker').last().css('z-index', 2000);//if colorpicker is inside a modal, its z-index should be higher than modal'safe
			
				$('#simple-colorpicker-1').ace_colorpicker();
				//$('#simple-colorpicker-1').ace_colorpicker('pick', 2);//select 2nd color
				//$('#simple-colorpicker-1').ace_colorpicker('pick', '#fbe983');//select #fbe983 color
				//var picker = $('#simple-colorpicker-1').data('ace_colorpicker')
				//picker.pick('red', true);//insert the color if it doesn't exist
			
			
				$(".knob").knob();
				
				
				var tag_input = $('#form-field-tags');
				try{
					tag_input.tag(
					  {
						placeholder:tag_input.attr('placeholder'),
						//enable typeahead by specifying the source array
						source: ace.vars['US_STATES'],//defined in ace.js >> ace.enable_search_ahead
						/**
						//or fetch data from database, fetch those that match "query"
						source: function(query, process) {
						  $.ajax({url: 'remote_source.php?q='+encodeURIComponent(query)})
						  .done(function(result_items){
							process(result_items);
						  });
						}
						*/
					  }
					)
			
					//programmatically add/remove a tag
					var $tag_obj = $('#form-field-tags').data('tag');
					$tag_obj.add('Programmatically Added');
					
					var index = $tag_obj.inValues('some tag');
					$tag_obj.remove(index);
				}
				catch(e) {
					//display a textarea for old IE, because it doesn't support this plugin or another one I tried!
					tag_input.after('<textarea id="'+tag_input.attr('id')+'" name="'+tag_input.attr('name')+'" rows="3">'+tag_input.val()+'</textarea>').remove();
					//autosize($('#form-field-tags'));
				}
				
				
				/////////
				$('#modal-form input[type=file]').ace_file_input({
					style:'well',
					btn_choose:'Drop files here or click to choose',
					btn_change:null,
					no_icon:'ace-icon fa fa-cloud-upload',
					droppable:true,
					thumbnail:'large'
				})
				
				//chosen plugin inside a modal will have a zero width because the select element is originally hidden
				//and its width cannot be determined.
				//so we set the width after modal is show
				$('#modal-form').on('shown.bs.modal', function () {
					if(!ace.vars['touch']) {
						$(this).find('.chosen-container').each(function(){
							$(this).find('a:first-child').css('width' , '210px');
							$(this).find('.chosen-drop').css('width' , '210px');
							$(this).find('.chosen-search input').css('width' , '200px');
						});
					}
				})
				/**
				//or you can activate the chosen plugin after modal is shown
				//this way select element becomes visible with dimensions and chosen works as expected
				$('#modal-form').on('shown', function () {
					$(this).find('.modal-chosen').chosen();
				})
				*/
			
				
				
				$(document).one('ajaxloadstart.page', function(e) {
					autosize.destroy('textarea[class*=autosize]')
					
					$('.limiterBox,.autosizejs').remove();
					$('.daterangepicker.dropdown-menu,.colorpicker.dropdown-menu,.bootstrap-datetimepicker-widget.dropdown-menu').remove();
				});
			
			});
		</script>

