
    

    $(document).ready(function() {
        $('#dataTables-example').DataTable({
            responsive: true
        });
    });
   
   
    
    $('[name="cod"]').on('change', function() {
       
$('#select').toggle(this.checked);
  
    
       
          
}).change();

 

              function bankFunction(id) {
                  
    var checkBox = document.getElementById("myCheck"+id);
                  
    //var text = document.getElementById("text"+id);
    if (checkBox.checked == true){
        $('#text'+id).toggle('slow');
        $('#text'+id).focus(); 
     $('#checked').val(11);
         
        
    } else {
        var text = document.getElementById("text"+id).value;
       
       $('#text'+id).fadeOut('slow');
         $('#text'+id).val(null);
         bbankcalculate(text );
    }
}
    
   
        function bbankcalculate(value)
         {
             
             var sum = 0;
    $(".class").each(function(){
        // var checkBox = document.getElementById("myCheck"+id);
                  

      if(value){
            sum -= -$(this).val();
              }
        else{
            //alert ($(this).val());
         sum += +$(this).val();
        }

        
    });
       
     var bValue = document.getElementById("totalbankamount");        
    $("#Minutes").val(sum.toFixed(2));
             $("#paytotal").val(sum.toFixed(2));
     $("#totalval").val(sum);
       //document.getElementById('total').value=parseFloat(sum) ;
             //
            bValue.innerText = sum.toFixed(2);
             Calculate(sum);
           }
    

function Calculate(bankvalue)
{
    $(".sweet-alert").fadeOut();
    //alert(bankvalue);
           var sum = 0;
    
    $(".bankresult").each(function(){
        // var checkBox = document.getElementById("myCheck"+id);
                  

     
        
            //alert ($(this).val());
         sum += +$(this).val();
        

        
    });
       
     var bValue = document.getElementById("answer"); 
     bValue.innerText = sum.toFixed(2);
    $("#totalval").val(sum);
  
 
}

function purchase(bvalue)
  {
    /*  if(bvalue){
          var permin -=parseFloat(bvalue);
  document.getElementById('answer').innerHTML=parseFloat(permin) ;
           document.getElementById('total').value=parseFloat(permin) ;
      }*/
     // $bank=0.0;
//var resources = document.getElementById('Resources').value;
var minutes = document.getElementById('Minutes').value;
     // var text= document.getElementById("text"+id).value;
      //alert(text);
     
      
var ticket = document.getElementById('ticket').value; 
if(minutes && ticket==0){
  var permin =parseFloat(minutes);
  document.getElementById('answer').innerHTML=parseFloat(permin.toFixed(2)) ;
  document.getElementById('total').value=parseFloat(permin) ;
  document.form.submit();
}
if(ticket && minutes==0){
  var permin = parseFloat(minutes)-parseFloat(ticket);
  document.getElementById('answer').innerHTML=parseFloat(permin.toFixed(2)) ;
  document.getElementById('total').value=parseFloat(permin) ;
  document.form.submit();
}
var permin =  parseFloat(minutes)-parseFloat(ticket);
document.getElementById('answer').innerHTML=parseFloat(permin.toFixed(2)) ;
document.getElementById('total').value=parseFloat(permin) ;
document.form.submit();
     
}
  function bankvalue(id)
         {
          var order=0;
             
             $(".sweet-alert").fadeOut();
        order= parseFloat($('#ticket').val());
             
//var inputvalue = parseFloat($('#text'+id).val());
//var oldvalue= parseFloat($('#bankvalue'+id).val());
             /*if(oldvalue<inputvalue){
                 swal("Accepting Only values 1-"+oldvalue);
                
                      $('#text'+id).val(oldvalue);
 

             }*/
             var sum = 0;
            var total=0;
    $(".bankclass").each(function(){
       
        
         sum += +$(this).val();
        

        
    });
          if(order && sum){
          total=parseFloat(sum)-parseFloat(order);    
          }
             else if(sum && !order){
                total=parseFloat(sum); 
             }
             else if(!sum && order){
                total=-parseFloat(order); 
             }
       
            
    $("#Minutes").val(sum.toFixed(2));
     $("#paytotal").val(sum.toFixed(2));        
        document.getElementById('answer').innerHTML=(total.toFixed(2)) ;
             $("#total").val(total.toFixed(2));
       //purchase(sum)  ;
              var bValue = document.getElementById("answer"); 
     
           }


   
    $("#checkAll").change(function () {
    $("input:checkbox").prop('checked', $(this).prop("checked"));
   });
      
  
$(document).ready(function(){
   // $('[data-toggle="tooltip"]').tooltip();   
});
$(document).ready(function(){
    $('a').on('click',function(){
        var aID = $(this).attr('href');
        var elem = $(''+aID).html();
        
        $('.target').html(elem);
    });
});
  function yesnoCheck(that) {
        if (that.value == "No Pay") {
            document.getElementById('pay').readOnly = true;
           
        } else {
            document.getElementById('pay').readOnly = false;
        
        }
    }
function orderyesnoCheck(that) {
        if (that.value == "No Pay") {
           
         
        } else {
           

        }
    }

 function printDiv(divName) {
    var printButton = document.getElementById("printpagebutton");
//Set the print button visibility to 'hidden' 
printButton.style.visibility = 'hidden';
//Print the page content

var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}

//payorder

 
//modal pay oder
 var paymodal = document.getElementById('payModal');
  var btn = document.getElementById("myBtn");
  var span = document.getElementsByClassName("close")[0];
if(btn){
  btn.onclick = function() {
 $('#pay_order').trigger("reset");
       $('#pay_typ').text("");
        $('.paybankclass').css('display','none');
      $('#purchase_bankpay').css('display','none');
      $('#due_bal').text(00);
    paymodal.style.display = "block";
     paymodal.modal({backdrop:'static'});
       
      
  }
}
if(span){
  span.onclick = function() {
    paymodal.style.display = "none";
  }
}

  window.onclick = function(event) {
    if (event.target == paymodal) {
      paymodal.style.display = "none";
    }
  }
 
  function closeDialog(element)
  {
      
      
    var modalID = element.parentNode.parentNode.getAttribute("id");
    var modal = document.getElementById(modalID);
    modal.style.display = "none";
  }
//field calculate
 
//warning
   $('#submit').click(function(){
   if($('#from').val() == '' && $('#to').val() == ''  ){
   swal({
          
          text: "You Have To  select a Date Range First!",
          type: "warning",
         
          confirmButtonText: 'ok!'
        });
       return false;
   }
    });
//newtype
$(function() {
  
  // Get the form fields and hidden div
  var checkbox = $("#trigger");
  var hidden = $("#hidden_fields");
  var populate = $("#populate");
  
  // Hide the fields.
  // Use JS to do this in case the user doesn't have JS 
  // enabled.
  hidden.hide();
  
  // Setup an event listener for when the state of the 
  // checkbox changes.
  checkbox.change(function() {
    // Check to see if the checkbox is checked.
    // If it is, show the fields and populate the input.
    // If not, hide the fields.
    if (checkbox.is(':checked')) {
      // Show the hidden fields.
      hidden.show();
      // Populate the input.
      populate.val("Dude, this input got populated!");
    } else {
      // Make sure that the hidden fields are indeed
      // hidden.
      hidden.hide();
      
      // You may also want to clear the value of the 
      // hidden fields here. Just in case somebody 
      // shows the fields, enters data to them and then 
      // unticks the checkbox.
      //
      // This would do the job:
      //
      // $("#hidden_field").val("");
    }
  });
});
//new type 
function mycheckFunction() {
    var checkBox = document.getElementById("myCheck");
    var text = document.getElementById("text");
    if (checkBox.checked == true){
        text.style.display = "block";
        $('#autoUpdate').fadeOut('slow');
        
    } else {
        $('#autoUpdate').fadeIn('slow');
       text.style.display = "none";
    }
}
     function mycheck() {
          $('#reset').click(function () {
    if (!$('#chk').is(':checked')) {
        swal({
          title: "Please Select a Record Only!",
         
          type: "warning",
         
          confirmButtonText: 'ok!'
        });
        return false;
    }
               else{
                   swal({
          title: "Are you sure?",
          text: "You will be able to recover the data from trash!",
          type: "warning",
          showCancelButton: true,
          confirmButtonClass: 'btn-danger',
          confirmButtonText: 'Yes, delete it!',
          closeOnConfirm: false,
          //closeOnCancel: false
        },
        function(){
                                                     var fav = [];
 $.each($("input[name='selected_item']:checked"), function(){ 
     
     
     
   myFunction($(this).val());
    //fav.push($(this).val());
     
 //alert(fav);
 
}); 
          swal("Deleted!", "Your data has been deleted!", "success");
                       
        });
                   
  
                   
               }   
 
          
});
     
    }
//image file input 
$(document).ready( function() {
    	$(document).on('change', '.btn-file :file', function() {
		var input = $(this),
			label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
		input.trigger('fileselect', [label]);
		});

		$('.btn-file :file').on('fileselect', function(event, label) {
		    
		    var input = $(this).parents('.input-group').find(':text'),
		        log = label;
		    
		    if( input.length ) {
		        input.val(log);
		    } else {
		        if( log ) alert(log);
		    }
	    
		});
		function readURL(input) {
		    if (input.files && input.files[0]) {
		        var reader = new FileReader();
		        
		        reader.onload = function (e) {
		            $('#img-upload').attr('src', e.target.result);
		        }
		        
		        reader.readAsDataURL(input.files[0]);
		    }
		}

		$("#imgInp").change(function(){
		    readURL(this);
		}); 	
	});
//delete user sale

 function pmyFunction(ref) {
        swal({
          
  title: "Are you sure?",
  text: "You will be able to recover the data from trash!",
  type: "warning",
  showCancelButton: true,
  confirmButtonClass: "btn-danger",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
},
function(){
              var datastr=ref;
      
        //var datastr='ref='+ encodeURIComponent(ref)
       
            $.ajax({
            url: "<?php echo base_url()?>purchases/value_delete", 
            type: "POST",
            data    : {'uristring':datastr},
           success: function(data){
    
    
      
  }
        });
  swal("Deleted!", "Your data has been deleted.", "success");
          $(".delete_mem" + ref).fadeOut('slow');
    
});
  
      // alert(ref);
       
      
 
    }
//worker cal

function workercalculate()
{
     $('.confirm').trigger('click');
          var sum = 0;
            
    $(".paybankclass").each(function(){
       
        
         sum += +$(this).val();
        

        
    });
    
          
       document.getElementById('answer').value=sum.toFixed(2) ;
        $("#answer").html(sum.toFixed(2));    
    $("#total").val(sum.toFixed(2));

}
function transferbankvalue (){
     var sum = 0;
           
    $(".bankclass").each(function(){
       
        
         sum += +$(this).val();
        

        
    });
    
          
       document.getElementById('answer').value=sum.toFixed(2) ;
        $("#answer").html(sum.toFixed(2));    
    $("#total").val(sum.toFixed(2));
}
function expensebankvalue()
{
    
          var sum = 0;
            $('.confirm').trigger('click');
    $(".bankclass").each(function(){
       
        
         sum += +$(this).val();
        

        
    });
    
          
       document.getElementById('answer').value=sum.toFixed(2) ;
        $("#answer").html(sum.toFixed(2));    
    $("#total").val(sum.toFixed(2));

}
//for edit and new 

//print
 function printDiv(divName) {
//var printButton = document.getElementById("printpagebutton");
//printButton.style.visibility = 'hidden';
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
//fieldshow
/*function yesnoCheck(that) {
        if (that.value == that.value) {
           
            document.getElementById("ifYes").style.display = "block";
        } else {
            document.getElementById("ifYes").style.display = "none";
        }
    }*/
/* function yesnoCheck(that) {
        if (that.value == that.value) {
           
            document.getElementById("ifYes").style.display = "block";
        } else {
            document.getElementById("ifYes").style.display = "none";
        }
    }*/
///bankdiv show hide 


     function bankshow(that){
          if (that.value == "CB" || that.value == "Cheque" || that.value == "Cash") {
            $('#purchase_bank').fadeIn('slow'); 
             
         
        } 
             if (that.value == "No Pay"  ) {
                
        
            
         
        } 
       
         if (that.value == "Cash" || that.value == "TR"){
           $('#purchase_bank').fadeOut('slow'); 
       
              

        }
         
      }
  
    
      function bankshowpay(that){
          if (that.value == "CB"  || that.value == "Cheque" || that.value == "Cash") {
            $('#purchase_bankpay').fadeIn('slow'); 
         $('#pay_total').removeAttr('disabled','disabled'); 
        } 
             else if (that.value == "No Pay"  ) {
                 $('#purchase_bankpay').fadeOut('slow'); 
            $('#pay_total').attr('disabled','disabled'); 
                 
         
        } 
       
         if (that.value == "cash" || that.value == "tr") {
           $('#purchase_bankpay').fadeOut('slow'); 
             $('#pay_total').removeAttr('disabled','disabled'); 
             

        }
         
      }
   function filterprofit(that){
       alert(that.value);
   }
function pur_bankshow(that){
    
     if (that.value == "CB"  || that.value == "Cheque" || that.value == "Cash") {
            $('#purchase_bank').fadeIn('slow'); 
        
        
        } 
             else if (that.value == "No Pay"  ) {
                 $('#purchase_bank').fadeOut('slow'); 
                  var total=$('#ticket').val();
                 $('#Minutes').val(00); 
                 $('.bankclass').val(00); 
                 
                 $('#paytotal').text(00);
                 $('#paytotal').val(00);
                 $('#total').val(-(total)); 
                 $('#answer').text(-(total)); 
                 
         
        } 
   
       
         
}
function whole_bankshow(that){
   
     if (that.value == "CB"  || that.value == "Cheque" || that.value == "Cash") {
            $('#wholesale_bank').fadeIn('slow'); 
        
        
        } 
             else   {
                 $('#wholesale_bank').fadeOut('slow'); 
          
                 var total=$('#ticket').val();
                 $('#Minutes').val(00); 
                 $('.bankclass').val(00); 
                 
                 $('#paytotal').text(00);
                 $('#paytotal').val(00);
                 $('#total').val(-(total)); 
                 $('#answer').text(-(total));  
         
        } 
    
       
         
}


//purchase page 
 function purchases (value){
        if(!value){
        	var sumval= Number($("#total_due").val()); 
         $("#due_bal").text( sumval.toFixed(2));
        $("#tdue").val(sumval);
     }
            if(value){
                 
    
	
        
	var sumval=( Number($("#total_due").val())+(Number(value)));
              //  alert(sumval);
	$("#due_bal").text( sumval.toFixed(2));
        $("#tdue").val(sumval);
                /*document.getElementById('due_bal').innerHTML=(sumval) ;
document.getElementById('tdue').value=(sumval) ;
document.form1.submit();*/
       // $("#tdue").val(sum);
        //sumfun(sum);
	

            }

     
else{
        $(function() {
    $("#pay_total").on("keydown keyup", sum);
	function sum() {
        
	var sumval=(Number($("#pay_total").val()) + Number($("#total_due").val()));
	$("#due_bal").text(Number($("#pay_total").val()) + Number($("#total_due").val()));
        $("#tdue").val(Number($("#pay_total").val()) + Number($("#total_due").val()));
       // $("#tdue").val(sum);
        //sumfun(sum);
	}
});
        }
        }
 
     function paybankvalue(id)
         {
             //$(".sweet-alert").fadeOut();
             $('.confirm').trigger('click');
             var inputvalue = parseFloat($('#paytext'+id).val());
             var oldvalue= parseFloat($('#bankvalue'+id).val());
             
             
             /*if(oldvalue<inputvalue){
                 swal("Accepting Only values 1-"+oldvalue);
                
                      $('#paytext'+id).val(oldvalue);
 

             }*/
            var tvalue=0;
             var totalsum=0;
               tvalue = document.getElementById("pay_total").value;
            
             var sum = 0;
           $(".paybankclass").each(function(){
        //alert("hello");
        // var checkBox = document.getElementById("myCheck"+id);
                  
               
        
         sum += +$(this).val();
        
 //totalsum=sum+parseFloat(tvalue);
        
    });
        // sum=  +(parseFloat(tvalue));  
    //totalsum=sum+(parseFloat(tvalue));
            var bValue = document.getElementById("pay_total");    
     document.getElementById('pay_total').value=sum.toFixed(2) ;
      purchases(sum.toFixed(2))  ;
            
     
           }

// search menu
document.addEventListener("DOMContentLoaded", function () {
    const toggleBtn = document.getElementById("toggleBtn");
    const searchBox = document.getElementById("searchBox");
    const tabsContainer = document.getElementById("categoryTabs");

    if (toggleBtn && searchBox && tabsContainer) {
        const items = tabsContainer.querySelectorAll(".category-item");
        const limit = 15;
        let expanded = false;

        // Show only 15 initially
        const showInitial = () => {
            items.forEach((item, index) => {
                item.style.display = index < limit ? "block" : "none";
            });
            toggleBtn.textContent = "Show All";
            expanded = false;
        };
        showInitial();

        // Show More / Show Less
        toggleBtn.addEventListener("click", () => {
            expanded = !expanded;
            items.forEach((item, index) => {
                if (index >= limit) item.style.display = expanded ? "block" : "none";
            });
            toggleBtn.textContent = expanded ? "Hide All" : "Show All";
        });

        // Search filter
        searchBox.addEventListener("keyup", function () {
            const value = this.value.toLowerCase().trim();
            if (value !== "") {
                // show all matches when searching
                items.forEach((item) => {
                    const name = item.getAttribute("data-name");
                    item.style.display = name.includes(value) ? "block" : "none";
                });
                toggleBtn.style.display = "none"; // hide toggle button while searching
            } else {
                // search cleared → reset to initial state
                toggleBtn.style.display = "inline-block";
                showInitial();
            }
        });
    }
});





 /*function paybankvalue(value)
         {
            var tvalue=0;
             var totalsum=0;
               tvalue = document.getElementById("pay_total").value;
            
             var sum = 0;
    $(".paybankclass").each(function(){
        //alert("hello");
        // var checkBox = document.getElementById("myCheck"+id);
                  
               
        
         sum += +$(this).val();
        
 //totalsum=sum+parseFloat(tvalue);
        
    });
        // sum=  +(parseFloat(tvalue));  
    //totalsum=sum+(parseFloat(tvalue));
            var bValue = document.getElementById("pay_total");    
     document.getElementById('pay_total').value=sum ;
       //purchases(sum)  ;
            
     
           }*/
//calculate

   
         //function purchases(value)
         // {
       /* var sum=0;      
var totaldue = document.getElementById('total_due').value;
var minutes = document.getElementById('pay_total').value; 
              //alert(totaldue);
              
                   var permin = (parseFloat(minutes));
                      var tt=parseFloat(totaldue);


if(minutes){
   sum=tt+permin;
    document.getElementById('total_due').value=sum ;
    document.getElementById('due_bal').innerHTML=parseFloat(sum) ;
}
              else{
                 document.getElementById('total_due').value=totaldue ;
                  document.getElementById('due_bal').innerHTML=parseFloat(totaldue) ;
              }
         */
              
              
//var ticket = document.getElementById('total_ord').value; 
/*if(minutes && ticket==0){
  var permin =parseFloat(minutes);
  document.getElementById('due_bal').innerHTML=parseFloat(permin) ;
  document.getElementById('total_due').value=parseFloat(permin) ;
  document.form1.submit();
}
              if(minutes && value){
  var permin =parseFloat(minutes)+parseFloat(value);
  document.getElementById('pay_total').innerHTML=parseFloat(permin) ;
  document.getElementById('total_due').value=parseFloat(permin) ;
  document.form1.submit();
}
if(ticket && minutes==0){
  var permin = parseFloat(ticket);
  document.getElementById('due_bal').innerHTML=parseFloat(permin) ;
  document.getElementById('total_due').value=parseFloat(permin) ;
  document.form1.submit();
}*/
/*var permin =  parseFloat(minutes)-parseFloat(ticket);
document.getElementById('due_bal').innerHTML=parseFloat(permin) ;
document.getElementById('total_due').value=parseFloat(permin) ;
document.form1.submit();*/
//}

//buisness edit

