	<?php
	$this->load->view("common/header");
	?>
	<!-- end: HEAD -->
       
		<!-- start: CSS REQUIRED FOR THIS PAGE ONLY -->

		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/select2/select2.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/datepicker/css/datepicker.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-timepicker/css/bootstrap-timepicker.min.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-daterangepicker/daterangepicker-bs3.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-colorpicker/css/bootstrap-colorpicker.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/jQuery-Tags-Input/jquery.tagsinput.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-fileupload/bootstrap-fileupload.min.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/summernote/build/summernote.css">
		<link rel="stylesheet" type="text/css" href="<?php echo asset_url(); ?>css/jquery-ui.css">

		<style type="text/css">
			label {
					font-weight: 700;
				  }

				.table > thead:first-child > tr:first-child > th, .table > thead:first-child > tr:first-child > td, .table-striped thead tr.primary:nth-child(2n+1) th {
				    background-color: #428bca;
				    border-color: #357ebd;
				    border-top: 1px solid #357ebd;
				    color: white;
				    text-align: center;
				}

				.table th, .table td {
				    vertical-align: middle !important;
				}		
</style>

		<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
	<!-- end: HEAD -->
	<!-- start: BODY -->
	<body>
		<!-- start: HEADER -->
		<div class="navbar navbar-inverse navbar-fixed-top">
			<!-- start: TOP NAVIGATION CONTAINER -->
			<div class="container">
				<div class="navbar-header">
					<!-- start: RESPONSIVE MENU TOGGLER -->
					<button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
						<span class="clip-list-2"></span>
					</button>
					<!-- end: RESPONSIVE MENU TOGGLER -->
					<!-- start: LOGO -->
					<a class="navbar-brand" href="index.html">
						CLIP<i class="clip-clip"></i>ONE
					</a>
					<!-- end: LOGO -->
				</div>
				<div class="navbar-tools">
					<!-- start: TOP NAVIGATION MENU -->
				<?php $this->load->view("common/notifications.php"); ?>
					<!-- end: TOP NAVIGATION MENU -->
				</div>
			</div>
			<!-- end: TOP NAVIGATION CONTAINER -->
		</div>
		<!-- end: HEADER -->
		<!-- start: MAIN CONTAINER -->
		<div class="main-container">
			<div class="navbar-content">
				<!-- start: SIDEBAR -->
				<?php $this->load->view("common/navigation"); ?>
				<!-- end: SIDEBAR -->
			</div>
			<!-- start: PAGE -->
			<div class="main-content">
				<!-- end: SPANEL CONFIGURATION MODAL FORM -->
				<div class="container">
					<!-- start: PAGE HEADER -->
					<div class="row">
						<div class="col-sm-12">
							<!-- start: PAGE TITLE & BREADCRUMB -->
							<ol class="breadcrumb">
								<li>
									<a href="#">
										 Dashboard 
									</a>
								</li>
                                
								<li>
									<a href="#">
										GRN
									</a>
								</li>

								<li class="active">
										Add GRN
								</li>

								<li class="search-box">
									<form class="sidebar-search">
										<div class="form-group">
											<input type="text" placeholder="Start Searching...">
											<button class="submit">
												<i class="clip-search-3"></i>
											</button>
										</div>
									</form>
								</li>
							</ol>
							<div class="page-header">
								<h1>Add GRN</h1>
							</div>

                            <p>Please fill in the information below. The field labels marked with * are required input fields.</p>
						</div>
					</div>
					<!-- end: PAGE HEADER -->
					<!-- start: PAGE CONTENT 
                    <!-- start grid -->
				<form class="bv-form" accept-charset="utf-8" method="post" enctype="multipart/form-data" role="form" action="<?php echo base_url('purchases/add'); ?>" id="add_perchas">
					<div class="row">
						<div class="col-md-12">
							<div class="alert alert-danger" style="<?php $s = validation_errors(); if (empty($s)) {echo 'display:none';}?>">
							<button type="button" class="close" data-dismiss="alert">×</button>
								<?php echo validation_errors(); ?>
							</div>
							<!-- start: DYNAMIC TABLE PANEL -->
							<div class="panel panel-default">
								<div class="panel-heading">
									<i class="fa fa-plus"></i>
									Add Purchase
								</div>
								<div class="panel-body">
									<div class="col-lg-12">
										<div class="col-md-4">
											<div class="form-group">
												<label>Date *</label>

										<input type="text" required id="podate" class="form-control date" name="podate" data-original-title="" title="" data-bv-field="date">
										</div>
										</div>
										<div class="col-md-4">
											<div class="form-group">
												<?php
												$ref_id = new Common_Model();
												$reference_no = $ref_id->gen_ref_number('id','purchases','GRN');
												?>
												<label>Reference No</label>
												<input readonly type="text" id="poref"   class="form-control input-tip" value="<?php echo $reference_no; ?>" name="reference_no" data-original-title="" title="">											</div>
										</div>
                                        
                                        <div class="col-md-4">
<div class="form-group">

<label>Supplier Reference No</label>
<input type="text" id="poref" onkeypress="focus_supplier()" class="form-control input-tip" value="" name="supp_invocie_no" data-original-title="" title="">											</div>
</div>
										<div class="col-md-4">
											<div class="form-group">
												<label>Warehouse *</label>
					                            <select id="powarehouse" name="powarehouse" class="form-control search-select"   >
					                             <?php foreach ($warehouse as $key => $wh) {
					                                echo"<option value=".$wh->id.">".$wh->name."</option>";
					                             } ?>  
					                             </select>
											</div>
										</div>
										
											<div class="col-md-2">
											<div class="form-group">
												<label>Discount %</label>
					                          <input min="0" max="100"type="number" id="all_discount_bill"   class="form-control" value="0" name="all_discount_bill" required>
											</div>
										</div>

										<div class="col-md-8">
											<div class="panel panel-default">
												<div class="panel-heading" style="font-weight: 700;">Please select these before adding any product</div>
													<div class="panel-body">
														<div class="form-group"><div class="col-sm-12">
															<label>Supplier *</label>
															<select id="supplier" name="supplier" class="form-control search-select" onkeypress="focus_add_items()">
								                                <option value="">&nbsp;</option>
									                             <?php foreach ($supplier as $key => $sup) {
									                                echo"<option value=".$sup->supp_id.">".$sup->supp_company_name."</option>";
									                             } ?>  
								                             </select>
														</div>
														</div>
													</div>
												</div>
											</div>
										</div>

										<div id="sticker" class="col-md-12">
											<div class="well well-sm">
												<div style="margin-bottom:0;" class="form-group">
												<div class="input-group wide-tip">
													<div style="padding-left: 10px; padding-right: 10px;" class="input-group-addon">
														<i class="fa fa-2x fa-barcode addIcon"></i>
													</div>
													<input type="text" placeholder="Please add products to order list" id="add_item" class="form-control input-lg" value="" name="add_item"  style="border-radius: 6px;font-size: 18px;height: 46px;line-height: 1.33;padding: 10px 16px;">
														<div style="padding-left: 10px; padding-right: 10px;" class="input-group-addon">
															<i id="addIcon" class="fa fa-2x fa-plus-circle addIcon"></i>

														</div>
												</div>
											</div>


											<div class="clearfix"></div>
											<div class="control-group table-group">
												<label class="table-label">Order Items</label>
													<div class="controls table-controls">
														<table class="table items table-striped table-bordered table-condensed table-hover" id="poTable">
															<thead>
																<tr>
																	<th class="col-md-4">Product Name (Product Code)</th>
																	<th class="col-md-1">Net Unit Cost</th>
																	<th class="col-md-1">Retail Price</th>
																	<th class="col-md-1">Quantity</th>
																	<th class="col-md-1">Discount</th>
																	<th>Subtotal (<span class="currency">LKR</span>)</th>
																	<th style="width: 30px !important; text-align: center;"><i class="fa fa-trash-o"></i></th>
																</tr>
															</thead>
																	<tbody>

																	</tbody>
															<tfoot>
															</tfoot>
														</table>
													</div>
												</div>
											</div>
<?php //echo strtotime("now"), "\n"; ?>
												<label class="checkbox-inline"><p></p>
													<input type="checkbox" class="square-black" value="" id="extras" onkeypress="focus_add_items()">
													<label for="extras">More Options</label>
												</label>

													<div id="extras-con" class="row" style="display: none;">
															<div class="col-md-4">
																<div class="form-group">
																	<label for="podiscount">Discount (5/5%)</label>
																	<input type="text" id="podiscount" class="form-control input-tip" value="" name="discount" data-original-title="" title="" onkeypress="focus_add_items()">
																</div>
															</div>
													</div>
												<div class="clearfix"></div>
                                                <input name="s_tmp_id" type="hidden" id="s_tmp_id" value="0">
												<div class="form-group">
												<label for="ponote">Note</label> <textarea name="note" cols="40" rows="10" class="form-control" id="ponote" style="margin-top: 10px; height: 100px;" ></textarea>
												</div>
										</div>
										<div class="col-lg-12">
											<div style="" class="well well-sm" id="bottom-total">
											<table style="margin-bottom:0;" class="table table-bordered table-condensed totals">
											<tbody><tr class="warning">
											<td>Total <span id="total" class="totals_val pull-right">0.00</span><input type="hidden" value="0.00" name="total" id="total"/></td>
											<td>Order Discount <span id="tds" class="totals_val pull-right">0.00</span></td>
											<td>Grand Total <span id="gtotal" class="totals_val pull-right">0.00</span><input type="hidden" value="0.00" name="hgtotal" id="hgtotal"/></td>
											</tr>
											</tbody></table>
											</div>

											<div class="col-md-12">
											<div class="from-group"><input type="submit" name="submit" style="padding: 6px 15px; margin:15px 0;" class="btn btn-primary" id="add_pruchase" value="Submit" name="add_pruchase">
										
											<button id="reset" class="btn btn-danger" type="button">Reset</button>
											</div>
											</div>
										</div>
        							</div>
        							
								</div>
							</div>
														<!-- end: DYNAMIC TABLE PANEL -->
						</div>
					</div>
                    <!-- end grid -->
                </form> 
			</div>
            
			<!-- end: PAGE -->
		</div>
		<!-- end: MAIN CONTAINER -->
		<!-- start: FOOTER -->
		<div class="footer clearfix">
			<div class="footer-inner">
				2014 &copy; clip-one by cliptheme.
			</div>
			<div class="footer-items">
				<span class="go-top"><i class="clip-chevron-up"></i></span>
			</div>
		</div>
		<!-- end: FOOTER -->
		<!-- start: RIGHT SIDEBAR -->
		<!-- end: RIGHT SIDEBAR -->
		<div id="event-management" class="modal fade" tabindex="-1" data-width="760" style="display: none;">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="modal-title">Event Management</h4>
					</div>
					<div class="modal-body"></div>
					<div class="modal-footer">
						<button type="button" data-dismiss="modal" class="btn btn-light-grey">
							Close
						</button>
						<button type="button" class="btn btn-danger remove-event no-display">
							<i class='fa fa-trash-o'></i> Delete Event
						</button>
						<button type='submit' class='btn btn-success save-event'>
							<i class='fa fa-check'></i> Save
						</button>
					</div>
				</div>
			</div>
		</div>

		<!-- start: MAIN JAVASCRIPTS -->
		<?php $this->load->view("common/footer"); ?>

		<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>		
		<script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.min.js"></script>
		<script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.js"></script>
		<script src="<?php echo asset_url(); ?>js/moment-with-locales.js"></script>
		<script type="text/javascript" src="<?php echo asset_url(); ?>js/bootstrap-datetimepicker.js"></script>
		
		<script type="text/javascript" src="<?php echo asset_url(); ?>js/bootbox.min.js"></script>
		<script type="text/javascript" src="<?php echo asset_url();?>js/form-validation-add_grn.js"></script>
<!--<script type="text/javascript" src="<?php echo asset_url(); ?>js/perches_.js"></script>-->
		<!-- end: MAIN JAVASCRIPTS -->
		<script type="text/javascript">

    var count = 1, an = 1, product_variant = 0, DT = 1, DC = '', shipping = 0,product_tax = 0, invoice_tax = 0, total_discount = 0, total = 0,poitems = {}; var last_row_no=0;

        			    //function to initiate bootstrap-datepicker
            $(function () {

                $('#podate').datetimepicker({
                	defaultDate : new Date()
                });
		        $("#powarehouse").select2();
		        $("#supplier").select2();
		        //$("#status").select2();
		        //$( "#podate" ).focus();
            });

        //ItemnTotals();
        $("#add_item").autocomplete({
            source: '../purchases/get_product_by_code?s_tmp_id='+9,
            minLength: 1,
            autoFocus: false,
            delay: 200,
            response: function (event, ui) {
                if ($(this).val().length >= 16 && ui.content[0].id == 0) {
                    //audio_error.play();
                    //bootbox.alert('No matching result found! Product might be out of stock in the selected warehouse.', function () {
                    // $('#add_item').focus();
                    //});
                    //  $(this).removeClass('ui-autocomplete-loading');
                    //  $(this).val('');
                }
                else if (ui.content.length == 1 && ui.content[0].id != 0) {
                    ui.item = ui.content[0];
                    $(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', ui);
                    $(this).autocomplete('close');
                    $(this).removeClass('ui-autocomplete-loading');
                    
                }
                else if (ui.content.length == 1 && ui.content[0].id == 0) {
                    // bootbox.alert('No matching result found! Product might be out of stock in the selected warehouse.', function () {
                    // $('#add_item').focus();
                    // });
                    //   $(this).removeClass('ui-autocomplete-loading');
                   // $(this).val('');
                }
            },
            select: function (event, ui) {
                
                if (ui.id !== 0) {
                    var row = add_purchase_item(ui.item);
					var s_tmp_id=parseInt($('#s_tmp_id').val());
					$('#s_tmp_id').val(s_tmp_id+1)
                    $(this).val('');
                    $('[id^=cost_]:first').focus().select(); 
                    return false;
                if (row)
                    $(this).val('');
                      
                	return false;
                } else {
                   //bootbox.alert('No matching result found! Product might be out of stock in the selected warehouse.');
					set_message("Product Info","No matching result found! Product might be out of stock in the selected warehouse.");
                }
            }
           
        });
        
      $('#add_item').bind('keypress', function (e) {
            if (e.keyCode == 13) {
                $('[id^=quantity]:first').focus().select();
                e.preventDefault();
                $(this).autocomplete("search");
                $('[id^=quantity]:first').focus().select();
            }
        });
 

        
	/*js start*/

if(localStorage.getItem('poitems')){loadItems();}

function loadItems() {
    if (localStorage.getItem('poitems')) {
        product_discount = 0;
        order_discount = 0;
        an = 0;
        count = 0;
        total = 0;
        poitems = JSON.parse(localStorage.getItem('poitems'));
        console.log(poitems);
        $("#poTable tbody").empty();
        $.each(poitems, function() {
            var item = this;
            var data_item_id = item.id;
            var product_id = item.row.product_id;
            var item_name = item.row.product_name;
            var product_code = item.row.product_code;
            var product_price = item.row.retail_cash;
            var product_cost  = item.row.product_cost;
            var item_qty = item.qty;
            var item_ds = item.row.item_discount;
            var item_stock_balance= item.stock_balance;
            var discount_text= item.discount_text;
           
              var bla = $('#all_discount_bill').val();
               if(bla>0){
                  item_ds=bla+"%";
              }
            //item_ds=bla+"%";
            item_discount = 0;
            var row_no = Math.random();
            last_row_no=row_no;
            var ds = item_ds ? item_ds : '0';
            if (ds.indexOf("%") !== -1) {
                var pds = ds.split("%");

                if (!isNaN(pds[0])) {
                    item_discount = formatDecimal(parseFloat(((product_cost) * parseFloat(pds[0])) / 100));
                } else {
                    item_discount = formatDecimal(ds);
                    item_discount = formatDecimal(parseFloat(((product_cost) * parseFloat(ds)) / 100));
                }

            } else {
                item_discount = parseFloat(ds);
                item_discount = formatDecimal(parseFloat(((product_cost) * parseFloat(ds)) / 100));
                
            }
           if(isNaN(item_discount)) {
               item_discount=0;
           }
           if(isNaN(item_ds)) {
               //item_discount=0;
           }
           item_stock_balance=item_stock_balance.toFixed(3);
           
            product_discount += parseFloat(item_discount * item_qty);
            unit_cost         = formatDecimal(product_cost - item_discount);
            product_cost     = formatDecimal(product_cost);
            var newTr = $('<tr id="row_'+row_no+'" data-item-id="'+data_item_id+'"></tr>');

            tr_html = '<td><span id="name_'+row_no+'" class="sname">'+item_name+' ('+product_code+') : Sale price :Rs.'+product_price +' Stock :'+item_stock_balance+discount_text +'</span><input type="hidden" value="'+product_code+'" class="rcode" name="product[]"><input type="hidden" value="'+product_id+'" name="product_id[]"><input type="hidden" value="'+item_name+'" class="rname" name="product_name[]"><i id="'+row_no+'" data-item="'+data_item_id+'" class="pull-right fa fa-edit tip pointer edit" title="Edit" style="cursor:pointer;"></i></td>';

            tr_html += '<td class="text-right"><input style="text-align:right;" text-align="right" data-item="' + data_item_id + '" id="cost_'+row_no+'" type="text" value="'+parseFloat(product_cost)+'" name="unit_cost[]" class="rucost" onkeypress="focus_add_price()" autocomplete="off" required><span id="scost_'+row_no+'" class="text-right scost">'+'</span></td>';

            tr_html += '<td class="text-right"><input style="text-align:right;" text-align="right" data-item="' + data_item_id + '" id="retail_p_'+row_no+'" type="number" value="'+parseFloat(product_price)+'" name="retail_price[]" class="ruprice"  onkeypress="focus_add_qty()" autocomplete="off" required><span id="sretail_'+row_no+'" class="text-right">'+'</span></td>';
            
            tr_html += '<td><input class="form-control text-center rquantity" name="quantity[]" type="number" value="' + item_qty + '" data-id="' + row_no + '" data-item="' + data_item_id + '" id="quantity_' + row_no + '" onClick="this.select();" onkeypress="focus_manual_discount()" autocomplete="off" required></td>';

            tr_html += '<td class="text-right"><input type="hidden" name="discount_cal[]" value="'+item_discount * item_qty+'" /><input class="form-control input-sm rdiscount" name="product_discount[]" type="text" id="discount_' + row_no + '" value="' + item_ds + '" data-item="' + data_item_id + '" id="discount_' + row_no + '" onClick="this.select();" onkeypress="focus_add_items()" autocomplete="off" required></td>';

            tr_html += '<td class="text-right"><input name="subtotal[]" type="hidden" value="'+parseFloat(unit_cost*item_qty)+'"/><span id="subtotal_'+row_no+'" class="text-right ssubtotal">'+accounting.formatMoney((parseFloat(unit_cost)) *(parseFloat(item_qty)),"")+'</span></td>';

            tr_html += '<td class="text-center"><i class="fa fa-times tip pointer sldel" id="row_delete_'+row_no+'" title="Remove" style="cursor:pointer;"></i></td>';

            newTr.html(tr_html);
            newTr.prependTo("#poTable");
            total += parseFloat(unit_cost) * parseFloat(item_qty);
        });

            var tfoot = '<tr id="tfoot" class="tfoot active"><th colspan="2">Total</th><th class="text-center"></th>';
            tfoot += '<th class="text-center"></th>';
            tfoot += '<th class="text-right"><input type="hidden" name="order_cal_des" value="'+product_discount+'"/>' + accounting.formatMoney(product_discount,"") + '</th>';
            tfoot += '<th class="text-right"><input type="hidden" value="'+total+'" name="gross_total"/>'+ accounting.formatMoney(total,"") + '</th><th class="text-center"><i class="fa fa-trash-o" style="opacity:0.5; filter:alpha(opacity=50);"></i></th></tr>';
            $('#poTable tfoot').html(tfoot);

        if (podiscount = localStorage.getItem('podiscount')) {
            var ds = podiscount;
            if (ds.indexOf("%") !== -1) {
                var pds = ds.split("%");
                if (!isNaN(pds[0])) {
                    order_discount = ((total) * parseFloat(pds[0])) / 100;
                } else {
                    order_discount = parseFloat(ds);
                }
            } else {
                order_discount = parseFloat(ds);
            }
        }

       var gtotal = (total - order_discount);
        $('#total').text(accounting.formatMoney(total,""));
        $('input#total').val(total);
       $('#tds').text(accounting.formatMoney(order_discount,""));
       $('#gtotal').text(accounting.formatMoney(gtotal,""));
       $('input#hgtotal').val(gtotal);
    };
   
}



var old_row_qty;
$(document).on("focus", '.rquantity', function() {
    old_row_qty = $(this).val();
}).on("change", '.rquantity', function() {
    var row = $(this).closest('tr');
    if (!is_numeric($(this).val())) {
        $(this).val(old_row_qty);
        bootbox.alert("unexpected value !");
        return;
    }
     var new_qty = parseFloat($(this).val());
     var item_id = $(this).attr('data-item');
   // alert(item_id);
    var poitems = JSON.parse(localStorage.getItem('poitems'));
    poitems[item_id].qty = new_qty
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();
});


var old_rucost;
$(document).on("focus", '.rucost', function() {
    old_rucost = $(this).val();
}).on("change", '.rucost', function() {
    var row = $(this).closest('tr');
    if (!is_numeric($(this).val())) {
        $(this).val(old_rucost);
        bootbox.alert("unexpected value !");
        return;
    }
    
    var new_rucost = parseFloat($(this).val());
    var item_id = $(this).attr('data-item');
    var poitems = JSON.parse(localStorage.getItem('poitems'));
    poitems[item_id].row.product_cost = new_rucost
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();
});



var old_ruprice;
$(document).on("focus", '.ruprice', function() {
    old_ruprice = $(this).val();
}).on("change", '.ruprice', function() {
    var row = $(this).closest('tr');
    if (!is_numeric($(this).val())) {
        $(this).val(old_ruprice);
        bootbox.alert("unexpected value !");
        return;
    }
    
    var new_ruprice = parseFloat($(this).val());
    var item_id = $(this).attr('data-item');
    var poitems = JSON.parse(localStorage.getItem('poitems'));
    poitems[item_id].row.retail_cash = new_ruprice
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();
});


    var old_podiscount;
    $('#podiscount').focus(function() {
        old_podiscount = $(this).val();
    }).change(function() {
        if (is_valid_discount($(this).val())) {
            localStorage.removeItem('podiscount');
            localStorage.setItem('podiscount', $(this).val());
            loadItems();
            return;
        }else{
            $(this).val(old_podiscount);
            bootbox.alert("unexpected value!");
        }
    });

    var old_rdiscount;
    $(document).on("focus", '.rdiscount', function() {
        old_rdiscount = $(this).val();
    }).on("change", '.rdiscount', function() {
        if (!is_valid_discount($(this).val())) {
            $(this).val(old_rdiscount);
            bootbox.alert("unexpected value!");
        }
         var new_item_discount = $(this).val();
         var item_id = $(this).attr('data-item');
           var poitems = JSON.parse(localStorage.getItem('poitems'));
           poitems[item_id].row.item_discount = new_item_discount;
           //console.log(poitems[item_id].row.item_discount);
           localStorage.setItem('poitems', JSON.stringify(poitems));
           loadItems();
    });
    

    $(document).on('click', '.sldel', function() {
        var row = $(this).closest('tr');
        var item_id = row.attr('data-item-id');
            poitems = JSON.parse(localStorage.getItem('poitems'));
            delete poitems[item_id];
            localStorage.setItem('poitems', JSON.stringify(poitems));
            row.remove();
            loadItems();
    });

//common function
function is_numeric(mixed_var){var whitespace=" \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";return(typeof mixed_var==='number'||(typeof mixed_var==='string'&&whitespace.indexOf(mixed_var.slice(-1))===-
1))&&mixed_var!==''&&!isNaN(mixed_var);}


function formatDecimal(x){
    return parseFloat(parseFloat(x).toFixed(2));
}

function is_valid_discount(mixed_var){return(is_numeric(mixed_var)||(/([0-9]%)/i.test(mixed_var)))?true:false;}
//end common function

var add_purchase_item = function (item) {
    var oldItems = JSON.parse(localStorage.getItem('poitems')) || [];
     if (count == 1) {
        if ($('#supplier').val()) {
            $('#supplier').select2("readonly", true);
        } else {
            bootbox.alert("Please select above first.");
            item = null;
            return;
        }
    }
    if (item == null) {
        return;
    }
    var item_id = item.id;
    if (poitems[item_id]) {
        poitems[item_id].row.qty = parseFloat(poitems[item_id].row.qty) + 1;
        
    } else {
        poitems[item_id] = item;
    }
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();
    return true;
};
//alert(last_row_no);
   // alert($('#qwer').val());
if (typeof(Storage) === "undefined") {
    $(window).bind('beforeunload', function(e) {
        if (count > 1) {
            var message = "You will loss data!";
            return message;
        }
    });
    
    
}
//*****************************



$('#poref').change(function(e) {

        localStorage.setItem('poref', $(this).val());

});



if (poref = localStorage.getItem('poref')) {

        $('#poref').val(poref);

}



//*****************

$('#powarehouse').change(function(e) {

    localStorage.setItem('powarehouse', $(this).val());

});



if (powarehouse = localStorage.getItem('powarehouse')) {

    $('#powarehouse').val(powarehouse);

}



//*****************

$('#postatus').change(function(e) {

    localStorage.setItem('postatus', $(this).val());

});



if (postatus = localStorage.getItem('postatus')) {

    $('#postatus').val(postatus);

}



//*****************

$('#ponote').change(function(e) {

    localStorage.setItem('ponote', $(this).val());

});



if (ponote = localStorage.getItem('ponote')) {

    $('#ponote').val(ponote);

}



//*************************



if (localStorage.getItem('poextras')) {

    $('#extras').iCheck('check');

    $('#extras-con').show();

}

$('#extras').on('ifChecked', function() {

    localStorage.setItem('poextras', 1);

    $('#extras-con').slideDown();

});

$('#extras').on('ifUnchecked', function() {

    localStorage.removeItem("poextras");

    $('#extras-con').slideUp();

});



//*****************

$('#tax_select').change(function(e) {

    localStorage.setItem('tax_select', $(this).val());

});



if (tax_select = localStorage.getItem('tax_select')) {

    $('#tax_select').val(tax_select);

}



//*****************

$('#podiscount').change(function(e) {

    localStorage.setItem('podiscount', $(this).val());

});



if (podiscount = localStorage.getItem('podiscount')) {

    $('#podiscount').val(podiscount);

}



//*****************

$('#poshipping').change(function(e) {

    localStorage.setItem('poshipping', $(this).val());

});



if (poshipping = localStorage.getItem('poshipping')) {

    $('#poshipping').val(poshipping);

}



//**********************

$('#supplier').change(function(e) {

    localStorage.setItem('supplier', $(this).val());

});



if (supplier = localStorage.getItem('supplier')) {

    $('#supplier').val(supplier);

}


//**********************

$('#all_discount_bill').change(function(e) {

    localStorage.setItem('all_discount_bill', $(this).val());

});



if (all_discount_bill = localStorage.getItem('all_discount_bill')) {

    $('#all_discount_bill').val(all_discount_bill);

}






//************** reset function *******



    $('#reset').click(function(e) {

        bootbox.confirm("Are you sure?", function(result) {

            if (result) {

                if (localStorage.getItem('poitems')) {

                    localStorage.removeItem('poitems');

                }

                if (localStorage.getItem('podiscount')) {

                    localStorage.removeItem('podiscount');

                }

                if (localStorage.getItem('tax_select')) {

                    localStorage.removeItem('tax_select');

                }

                if (localStorage.getItem('poref')) {

                    localStorage.removeItem('poref');

                }

                if (localStorage.getItem('powarehouse')) {

                    localStorage.removeItem('powarehouse');

                }

                if (localStorage.getItem('ponote')) {

                    localStorage.removeItem('ponote');

                }

                if (localStorage.getItem('posupplier')) {

                    localStorage.removeItem('posupplier');

                }

                if (localStorage.getItem('supplier')) {

                    localStorage.removeItem('supplier');

                }

                if (localStorage.getItem('poextras')) {

                    localStorage.removeItem('poextras');

                }

                if (localStorage.getItem('podate')) {

                    localStorage.removeItem('podate');

                }

                if (localStorage.getItem('postatus')) {

                    localStorage.removeItem('postatus');

                }

                if (localStorage.getItem('poshipping')) {

                    localStorage.removeItem('poshipping');

                }
                 if (localStorage.getItem('all_discount_bill')) {

                    localStorage.removeItem('all_discount_bill');

                }
                
                



               $('body').modalmanager('loading');

                location.reload();

            }

        });

    });

function focus_add_items(){
   // $('#add_item').focus(); 
  $(document).keypress(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        $('#add_item').focus(); 
        event.preventDefault();
    }
});
}

function focus_add_price(){
   // $('#add_item').focus(); 
  $(document).keypress(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
       $('[id^=retail_p_]:first').focus().select(); 
        event.preventDefault();
    }
});
}

function focus_add_qty(){
   // $('#add_item').focus(); 
  $(document).keypress(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        
        $('[id^=quantity]:first').focus().select(); 
        event.preventDefault();
    }
});
}


function focus_manual_discount(){
   // $('#add_item').focus(); 
  $(document).keypress(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        
        $('[id^=discount_]:first').focus().select(); 
        event.preventDefault();
    }
});
}


function focus_supplier(){
   // $('#add_item').focus(); 
  $(document).keypress(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        $('#supplier').focus().select();     
        event.preventDefault();
    }
});
}



//************** end reset function ***





//begin calculation function //

 //alert($('#quantity_'+row_no).val());
 

                

//end calculation function //
         
        </script>
	</body>
	<!-- end: BODY -->
</html>