<?php include_once('../../layout/header.php'); ?>
      <!-- Sidebar Start -->
      <?php include_once('../../layout/side-bar.php'); ?>
      <!-- Sidebar End -->

      <!-- Main wrapper -->
      <div class="body-wrapper">
        <!-- Header Start -->
        <?php include_once('../../layout/top-bar.php'); ?>
        <!-- Header End -->
        <div class="container-fluid note-has-grid">
          
          <?php $page = isset($_GET['page']) ? $_GET['page'] :'home'; ?>
          <?php if(is_file(__DIR__.'/'.$page.'.php')){include_once($page.'.php');}else{include('error-404.php');} ?> 
          <!-- container-fluid over -->

        </div>
      </div>
    </div>
<?php include_once('../../layout/footer.php'); ?>
<?php $max = $con->getRows('petty_cash_max', array('where'=>'status=1','return_type'=>'single')); ?>

<script type="text/javascript">
  $(document).ready(function(){
    function getDocCategories(){
      $.ajax({
          url:"get_doc_data.php",
          method:"POST",
          success:function(data){ 
              $('#show_doc_categories').html(data);
          }
      });
    }

    function fetchDI()  
      {  
           $.ajax({  
                url:"get_daily_itinerary.php",  
                method:"POST",  
                success:function(data){  
                     $('#daily_itinery').html(data);  
                }  
           });  
      }
      function getBranches(){
        let action = "get_branches";
        $.ajax({
            url:"get_department_data.php",
            method:"GET",
            data:{action:action},
            success:function(data){ 
                $('#show_branches').html(data);
            }
        });
      }
      getBranches();

      function getPettycashCap(){
        let action = "get_petty_cash_cap";
        $.ajax({
            url:"get_department_data.php",
            method:"GET",
            data:{action:action},
            success:function(data){ 
                $('#petty_cash_cap').html(data);
            }
        });
      }
      getPettycashCap();

      function fetchAdvanceReport(){
        var action = "all";
        $.ajax({
          url:"get_advance_report.php",
          method:"GET",
          data:{action:action},
          success:function(data){
            $('#show_advance_request_report').html(data);
          }
        });
      }
      fetchAdvanceReport();

      function getFuel(){
        let action = "get_fuel";
          $.ajax({
              url:"get_department_data.php",
              method:"GET",
              data:{action:action},
              success:function(data){ 
                  $('#show_fuels').html(data);
              }
          });
      }
      getFuel();

        $(document).on('click', '.delete_fuel', function(){
       var id=$(this).data("id3");

       if(confirm("Are you sure you want to delete the selected fuel?")){
           var action='delete_fuel';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#error").fadeIn(1000, function(){
                           $("#error").html('<div class="alert alert-success" role="alert"> The selected fuel has been deleted successfully.</div>');
                       });
                       $("#error").delay(5000).fadeOut(function(){});
                       //setTimeout(' window.location.href = ""; ', 5000);
                   }else{
                       $("#error").fadeIn(1000, function(){
                           $("#error").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleteting the selected fuel, please try again!</div>');
                       });
                       $("#error").delay(6000).fadeOut(function(){});
                   }
                   getFuel();
               }
           });
       }
    });

     $(document).on('click', '.delete_DI', function(){
       var id=$(this).data("id3");


       if(confirm("Are you sure you want to delete this?"))
       {
           var action='delete_DI';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){ 
                   if(data == 1){
                       $("#topic_response").fadeIn(1000, function(){
                           $("#topic_response").html('<div class="alert alert-success" role="alert"> The selected topic has been deleted !</div>');
                       });
                       $("#topic_response").delay(6000).fadeOut(function(){});
                       //setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#topic_response").fadeIn(1000, function(){
                           $("#topic_response").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting the  selected topic!</div>');
                       });
                       $("#topic_response").delay(6000).fadeOut(function(){});
                   }
                  fetchDI();
               }
           });
       }
    });

    $(document).on('click', '.btn_delete_leave', function(){
       var id=$(this).data("id3");

       if(confirm("Are you sure you want to delete the selected leave request?")){
           var action='btn_delete_leave';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#err").fadeIn(1000, function(){
                           $("#err").html('<div class="alert alert-success" role="alert"> The selected leave request has been deleted successfully.</div>');
                       });
                       $("#err").delay(6000).fadeOut(function(){});
                       setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#err").fadeIn(1000, function(){
                           $("#err").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleteting the selected request, please try again!</div>');
                       });
                       $("#err").delay(6000).fadeOut(function(){});
                   }
               }
           });
       }
    });

      $("#add-branch").validate({
      rules: {
        branch:{required:true}
      },
      messages: {
        branch:"Please enter branch name"
      },
      submitHandler: addBranch  
});    
  /* Handling form functionality */
function addBranch() {    
  var data = $("#add-branch").serialize();         
  $.ajax({        
    type : 'POST',
    url  : '../../settings/sql-master.php',
    data : data,
    beforeSend: function(){ 
      $("#branch_error").fadeOut();
      $("#add_branch").html('Adding Branch..');
      $("#update_branch").html('Updating..');
    },
    success : function(response){
      if(response == 1) {                 
       $("#branch_error").fadeIn(1000, function(){            
          $("#branch_error").html('<div class="alert alert-success"> A branch has been saved successfully!</div>');
          $("#add_branch").html('Add');
        });
        $("#branch_error").delay(6000).fadeOut(function(){});
        $("#add-branch")[0].reset();
      } else if(response == 2) {                 
        $("#branch_error").fadeIn(1000, function(){            
          $("#branch_error").html('<div class="alert alert-danger"> Sorry, there was an error saving the branch, please try again</div>');
          $("#add_branch").html('Add');
        });
        $("#branch_error").delay(6000).fadeOut(function(){});
      }
      if(response == 3) {                 
       $("#branch_error").fadeIn(1000, function(){            
          $("#branch_error").html('<div class="alert alert-success"> A branch has been updated successfully!</div>');
          $("#update_branch").html('Update');
        });
        $("#branch_error").delay(6000).fadeOut(function(){});
      } else if(response == 4) {                 
        $("#branch_error").fadeIn(1000, function(){            
          $("#branch_error").html('<div class="alert alert-danger"> There is nothing to update, please edit first.</div>');
          $("#update_branch").html('Update');
        });
        $("#branch_error").delay(6000).fadeOut(function(){});
      }
      getBranches();
    }
  });
  return false;
}  

     //delete department
    $(document).on('click', '.delete_branch', function(){
       var id=$(this).data("id3");

       if(confirm("Are you sure you want to delete this branch?"))
       {
           var action='delete_branch';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#branch_error").fadeIn(1000, function(){
                           $("#branch_error").html('<div class="alert alert-success" role="alert"> The selected branch has been deleted successfuly.</div>');
                       });
                       $("#branch_error").delay(6000).fadeOut(function(){});
                   }else{
                       $("#branch_error").fadeIn(1000, function(){
                           $("#branch_error").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting that branch, please try again!</div>');
                       });
                       $("#branch_error").delay(6000).fadeOut(function(){});
                   }
                   getBranches();
               }
           });
       }
    });

    $("#manage-departments").validate({
      rules: {
        department:{required:true},
        fuel:{required:true},
        fuel_price:{required:true},
        amount: {required:true}
      },
      messages: {
        department:"Please enter department name",
        fuel:"Please enter fuel name",
        fuel_price:"Please enter fuel current price",
        amount: "Please enter the petty cash max amount"
      },
      submitHandler: addDepartment  
});    
  /* Handling form functionality */
function addDepartment() {    
  var data = $("#manage-departments").serialize();         
  $.ajax({        
    type : 'POST',
    url  : '../../settings/sql-master.php',
    data : data,
    beforeSend: function(){ 
      $("#branch_error").fadeOut();
      $("#manage_department").html('Saving Data..');
      $("#update_branch").html('Updating..');
      $("#update_petty_cash").html('Updating...');
    },
    success : function(response){
      if(response == 1) {                 
       $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-success"> A department has been saved successfully!</div>');
          $("#manage_department").html('Save Department');
        });
        $("#error").delay(6000).fadeOut(function(){});
        $("#manage-departments")[0].reset();
      } else if(response == 2) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-danger"> Sorry, there was an error saving the department, please try again</div>');
          $("#manage_department").html('Add');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }
      if(response == 3) {                 
       $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-success"> A department has been updated successfully!</div>');
          $("#update_department").html('Update');
        });
        $("#error").delay(6000).fadeOut(function(){});
      } else if(response == 4) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-danger"> There is nothing to update, please edit first.</div>');
          $("#update_department").html('Update');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }else if(response == 5) {                 
       $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-success"> A fuel type has been saved successfully!</div>');
          $("#manage_department").html('Save Fuel');
        });
        $("#error").delay(6000).fadeOut(function(){});
        $("#manage-departments")[0].reset();
      }else if(response == 6) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-danger"> Sorry, there is an error saving fuel, please try again.</div>');
          $("#update_department").html('Save Fuel');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }else if(response == 7) {                 
       $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-success"> A fuel type has been updated successfully!</div>');
          $("#manage_department").html('Update Fuel');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }else if(response == 8) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-danger"> Sorry, there is no information to update, please try again.</div>');
          $("#update_department").html('Update Fuel');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }

      else if(response == 9) {                 
        $("#error").fadeIn(100, function(){            
          $("#error").html('<div class="alert alert-success"> The petty cash maximum amount has been updated successfully!.</div>');
          $("#update_petty_cash").html('Update');
        });
        $("#error").delay(6000).fadeOut(function(){});
        getPettycashCap();
        $("#manage-departments")[0].reset();
      }
      else if(response == 10) {                 
        $("#error").fadeIn(100, function(){            
          $("#error").html('<div class="alert alert-warning"> Sorry, there was an error updating petty cash maximum amount, please try again.</div>');
          $("#update_petty_cash").html('Update');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }
      getDepartments();
      getFuel();
    }
  });
  return false;
} 

//delete department
    $(document).on('click', '.delete_department', function(){
       var id=$(this).data("id3");

       if(confirm("Are you sure you want to delete this department?"))
       {
           var action='delete_department';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#department_response").fadeIn(1000, function(){
                           $("#department_response").html('<div class="alert alert-success" role="alert"> The selected department has been deleted successfuly.</div>');
                       });
                       $("#department_response").delay(6000).fadeOut(function(){});
                   }else{
                       $("#department_response").fadeIn(1000, function(){
                           $("#department_response").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting that dep, please try again!</div>');
                       });
                       $("#department_response").delay(6000).fadeOut(function(){});
                   }
                   getDepartments();
               }
           });
       }
    });

    //add positions
$("#add-position").validate({
  rules: {
    position: {required: true}
  },
  messages: {
    position:{required: "Please enter position"}
  },
  submitHandler: addPosition  
});    
/* Handling form functionality */
function addPosition() {    
  var data = $("#add-position").serialize();        
  $.ajax({        
    type : 'POST',
    url  : '../../settings/sql-master.php',
    data : data,
    beforeSend: function(){ 
      $("#position_error").fadeOut();
      $("#add_position").html(' Adding Position..');
    },
    success : function(response){
      if(response == 1) {                 
        $("#position_error").fadeIn(1000, function(){            
          $("#position_error").html('<div class="alert alert-success"> A position has been saved successfuly!</div>');
          $("#add_position").html('Save Position');
          $("#add-position")[0].reset();
        });
        $("#position_error").delay(6000).fadeOut(function(){});
      } else if(response == 2) {                 
        $("#position_error").fadeIn(1000, function(){            
          $("#position_error").html('<div class="alert alert-danger"> Sorry, either there was an error saving the positions!</div>');
          $("#add_position").html('Save Position');
        });
        $("#position_error").delay(6000).fadeOut(function(){});
      }
      getPositions();
    }
  });
  return false;
} 

  //delete_position
    $(document).on('click', '.delete_position', function(){
       var id=$(this).data("id3");

       if(confirm("Are you sure you want to delete this position?"))
       {
           var action='delete_position';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#positions_response").fadeIn(1000, function(){
                           $("#positions_response").html('<div class="alert alert-success" role="alert"> The selected position has been deleted successfuly.</div>');
                       });
                       $("#positions_response").delay(6000).fadeOut(function(){});
                       //setTimeout(' window.location.href = ""; ', 4000);

                   }else{
                       $("#positions_response").fadeIn(1000, function(){
                           $("#positions_response").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting that positions, please try again!</div>');
                       });
                       $("#positions_response").delay(6000).fadeOut(function(){});
                   }
                   getPositions();
               }
           });
       }
    });


    //petty cash requisition
    $("#petty-cash").validate({
      rules: {
        action:{required:true},
        subject:{required:true},
        sponsor:{required:true},
        amount:{required:true},
        reasons:{required:true}
      },
      messages: {
        action:"Please select the action",
        subject:"Please enter subject",
        sponsor:"Please enter sponsor",
        amount:"Enter amount, max is K<?=number_format($max['amount'],2)?>",
        reasons:"Please enter description",
      },
      submitHandler: pettyCash  
    });    
    /* Handling form functionality */
    function pettyCash() {    
      var data = $("#petty-cash").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#response").fadeOut();
          $("#petty_cash").html(' Posting Requisition...');
          $("#approve_petty").html(' Approving Requisition...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#response").fadeIn(100, function(){            
              $("#response").html('<div class="alert alert-success"> Your petty cash requisition  has been posted successfuly.</div>');
              $("#petty_cash").html('Post Requisition');
              $("#petty-cash")[0].reset();
            });
            $("#response").delay(6000).fadeOut(function(){});
            //setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 2) {                 
            $("#response").fadeIn(100, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, there was an error posting the requisition, please try again</div>');
              $("#petty_cash").html('Post Requisition');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
          if(response == 3) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> The petty cash requisition  has been approved successfuly.</div>');
              $("#approve_petty").html('Approve Requisition');
              $("#petty-cash")[0].reset();
            });
            $("#response").delay(6000).fadeOut(function(){});
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 4) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, there was an error approving the requisition, please try again</div>');
              $("#approve_petty").html('Approve Requisition');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
        }
      });
      return false;
    }  

    //btn_delete_band
    $(document).on('click', '.btn_delete_band', function(){
      var id=$(this).data("id3");


      if(confirm("Are you sure you want to delete this band?"))
      {
          var action='delete_band';
          $.ajax({
              url:"../../settings/sql-master.php",
              method:"GET",
              data:{id:id, action:action},
              dataType:"text",
              success:function(data){
                  if(data == 1){
                      $("#error").fadeIn(1000, function(){
                          $("#error").html('<div class="alert alert-success" role="alert"> The selected band has been deleted !</div>');
                      });
                      $("#error").delay(6000).fadeOut(function(){});
                      setTimeout(' window.location.href = ""; ', 6000);
                  }else{
                      $("#error").fadeIn(1000, function(){
                          $("#error").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting the  selected band!</div>');
                      });
                      $("#error").delay(6000).fadeOut(function(){});
                  }
              }
          });
      }
    });

    //generate reports
        $("#pettycash-custom").validate({
        rules:
        {
          date_from:{required:true},
          date_to:{required:true},
          section:{required:true}       
         },
         messages:
         {
           date_from:"Please select date from",
           date_to:"Please select date to",
           section: "please select the section"
         },
         submitHandler: generatePettyCashReport
        });  
        /* validation */

        /* add measurement submit */
        function generatePettyCashReport()
        {   
          var data = $("#pettycash-custom").serialize(); 
          //alert('sup'); 
            
          $.ajax({
            
          type : 'GET',
          url  : 'get_petty_cash_report.php',
          data : data,
          beforeSend: function()
          { 
            $("#error").fadeOut();
            $("#generate_report").html('Generating...');
          },
          success :  function(response)
            {   
            $('#show_petty_cash_report').html(response);
            $("#generate_report").html('Generate');

            }
          });
            return false;
        }

          //generate reports
    $("#advancereport-custom").validate({
      rules:
      {
        date_from:{required:true},
        date_to:{required:true},
        officer:{required:true}       
      },
      messages:
      {
        date_from:"Please select date from",
        date_to:"Please select date to",
        officer: "please select the officer"
      },
      submitHandler: generateAdvanceCustom
    });  
    /* validation advance_custom*/
     function generateAdvanceCustom()
    {   
      var data = $("#advancereport-custom").serialize(); 
      //alert('sup'); 
        
      $.ajax({
        
      type : 'GET',
      url  : 'get_advance_report.php',
      data : data,
      beforeSend: function()
      { 
        $("#error").fadeOut();
        $("#generate_report").html('Generating...');
      },
      success :  function(response)
        {   
        $('#show_advance_request_report').html(response);
        $("#generate_report").html('Generate');

        }
      });
        return false;
    }

   

    //generate custom invoice reports
     $("#custom-invoice-report").validate({
      rules:
      {
        date_from:{required:true},
        date_to:{required:true},
        officer:{required:true}       
      },
      messages:
      {
        date_from:"Please select date from",
        date_to:"Please select date to",
        officer: "please select the officer"
      },
      submitHandler: generateReports
    });  
    /* validation */

    /* add measurement submit */
    function generateReports()
    {   
      var data = $("#custom-invoice-report").serialize(); 
      //alert('sup'); 
        
      $.ajax({
        
      type : 'GET',
      url  : 'get_invoice_report.php',
      data : data,
      beforeSend: function()
      { 
        $("#error").fadeOut();
        $("#generate_report").html('Generating...');
      },
      success :  function(response)
        {   
        $('#show_invoice_report').html(response);
        $("#generate_report").html('Generate');

        }
      });
        return false;
    }

    function fetchTravelAdvanceReport(){
      var action = "all";
      $.ajax({
        url:"get_travel_advance_report.php",
        method:"GET",
        data:{action:action},
        success:function(data){
          $('#show_travel_advance_report').html(data);
        }
      });
    }

    fetchTravelAdvanceReport();

    //generate reports
    $("#generate-report").validate({
    rules:
    {
      date_from:{required:true},
      date_to:{required:true},
      section:{required:true}       
    },
    messages:
    {
      date_from:"Please select date from",
      date_to:"Please select date to",
      section: "please select the section"
    },
    submitHandler: generateReport
    });  
    /* validation */

    /* add measurement submit */
    function generateReport()
    {   
      var data = $("#generate-report").serialize(); 
      //alert('sup'); 
        
      $.ajax({
        
      type : 'GET',
      url  : 'get_travel_advance_report.php',
      data : data,
      beforeSend: function()
      { 
        $("#error").fadeOut();
        $("#generate_report").html('Generating...');
      },
      success :  function(response)
        {    //alert(response);
        $('#show_travel_advance_report').html(response);
        $("#generate_report").html('Generate');

        }
      });
        return false;
    }

    //add ticket
    $(document).ready(function(){
  if($("#add-ticket").length){
    $("#add-ticket").validate({
      rules: {
        title:{required:true},
        product:{required:true},
        description:{required:true},
        priority:{required:true},
        category:{required:true},
        band_title:{required:true},
        acc_ceiling:{required:true},
        lumpsum:{required:true},
        meals_acc:{required:true},
        without_meals_acc:{required:true},
        withmeals_acc:{required:true},
      },
      messages: {
        title:"Please enter ticket title",
        product:"Please select the product with an issue",
        description:"Please enter event description",
        priority:"Please select ticket priority",
        category:"Please select ticket category",
        band_title:"Please enter band title",
        acc_ceiling:"Please enter maximum accomodation ceiling",
        lumpsum:"Please enter lumpsum rate",
        meals_acc:"Please enter rate for accomodation with meals",
        without_meals_acc:"Please enter rate for without accomodation and meals",
        withmeals_acc:"Please enter rate for without accomodation and meals provided"
      },
      submitHandler: addTicket  
    });
  }
});    
/* Handling form functionality */
function addTicket() {    
  //var data = $("#add-event").serialize(); 
  var formData = new FormData($("#add-ticket")[0]);         
  $.ajax({        
    type : 'POST',
    url  : '../../settings/sql-master.php',
    data : formData,
    beforeSend: function(){ 
      $("#error").fadeOut();
      $("#add_ticket").html('Adding Ticket...');
      $("#add_band").html('Adding Band...');
      $("#update_band").html('Updating Band...');
    },
    success : function(response){
      if(response == 1) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-success"> A new ticket has been submitted successfully!</div>');
          $("#add_ticket").html('Add Ticket');
          
        });
        $("#error").delay(6000).fadeOut(function(){});
        //$("#add-topic")[0].reset();
        setTimeout(' window.location.href = ""; ', 6000);
      } else if(response == 2) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-danger"> Sorry, there was an error submitting the ticket, please try again!</div>');
          $("#add_ticket").html('Add Ticket');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }
      if(response == 3) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-success"> A new band has been saved successfully!</div>');
          $("#add_band").html('Add Band');
          
        });
        $("#error").delay(6000).fadeOut(function(){});
        $("#add-ticket")[0].reset();
        //setTimeout(' window.location.href = ""; ', 6000);
      } else if(response == 4) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-danger"> Sorry, there was an error saving the band, please try again!</div>');
          $("#add_band").html('Add Band');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }

      if(response == 5) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-success"> A selected band has been updated successfully!</div>');
          $("#update_band").html('Update Band');
          
        });
        $("#error").delay(6000).fadeOut(function(){});
        //$("#add-ticket")[0].reset();
        //setTimeout(' window.location.href = ""; ', 6000);
      } else if(response == 6) {                 
        $("#error").fadeIn(1000, function(){            
          $("#error").html('<div class="alert alert-danger"> Sorry, there was an error updating the band, please try again!</div>');
          $("#update_band").html('Update Band');
        });
        $("#error").delay(6000).fadeOut(function(){});
      }
    },
    contentType: false,
    processData: false,
    cache: false
  });
  return false;
}

$("#add-statement").validate({
      rules: {
        statement:{required:true}
      },
      messages: {
        statement:"Please enter your personal statement."
      },
      submitHandler: addStatement  
});    
  /* Handling form functionality */
function addStatement() {    
  var data = $("#add-statement").serialize();         
  $.ajax({        
    type : 'POST',
    url  : '../../settings/sql-master.php',
    data : data,
    beforeSend: function(){ 
      $("#state_error").fadeOut();
      $("#add_statement").html('Saving Statement..');
    },
    success : function(response){ //alert(response);
      if(response == 1) {                 
       $("#state_error").fadeIn(100, function(){            
          $("#state_error").html('<div class="alert alert-success"> Your personal statement has been saved successfully!</div>');
          $("#add_statement").html('Update Statement');
        });
        $("#state_error").delay(6000).fadeOut(function(){});
        setTimeout(' window.location.href = ""; ', 6000);
      } else if(response == 2) {                 
        $("#state_error").fadeIn(100, function(){            
          $("#state_error").html('<div class="alert alert-danger"> Sorry, there was an error saving the statement, please try again</div>');
          $("#add_statement").html('Update Statement');
        });
        $("#state_error").delay(6000).fadeOut(function(){});
      }
    }
  });
  return false;
}  

//add member
$("#add-picture").validate({
  rules: {
    file:{required:true}
  },
  messages: {
    file:"Please select a profile picture to upload"
  },
  submitHandler: addPicture  
});    

/* Handling form functionality */
function addPicture() {    
  //var data = $("#add-picture").serialize();
  var formData = $("#add-picture").submit(function () {
      return;
  });
  var formData = new FormData(formData[0]);        
  $.ajax({        
    type : 'POST',
    url  : '../../settings/sql-master.php',
    data : formData,
    beforeSend: function(){ 
      $("#error").fadeOut();
      $("#update_profile").html('Uploading Picture...');
    },
    success : function(response){ //alert(response);
      //upload profile picture success
      if(response == 1) {                 
        $("#msg").fadeIn(100, function(){            
          $("#msg").html('<div class="alert alert-success">Your profile picture has been updated successfuly!</div>');
          $("#save_member").html('Save');
          $("#update_profile").html('Upload Picture');
        });
        $("#add-picture")[0].reset();
        $("#msg").delay(6000).fadeOut(function(){});
        getProfile();
      }
      else if(response == 2) {                 
        $("#msg").fadeIn(100, function(){            
          $("#msg").html('<div class="alert alert-danger"> Sorry, there was an error updating your profile picture, please try again.</div>');
          $("#update_profile").html('Upload Picture');
        });
        $("#msg").delay(6000).fadeOut(function(){});
      }
      else if(response == 3) {                 
            $("#msg").fadeIn(1000, function(){            
              $("#msg").html('<div class="alert alert-danger"> Sorry, the picture selected is invalid, it should be of type JPG, JPEG, GIF or PNG.</div>');
              $("#update_profile").html('Upload Picture');
            });
            $("#msg").delay(6000).fadeOut(function(){});
      }
    },
    contentType: false,
    processData: false,
    cache: false
  });
  return false;
} 
/* end script */ 

$("#daily-itinery").validate({
      rules: {
        date:{required:true},
        from:{required:true},
        to:{required:true},
        title:{required:true}
      },
      messages: {
        date:"Please enter the date",
        from:"Please enter departure place",
        to:"Please enter destination",
        title:"Pleese enter backup title"
      },
      submitHandler: addItinery  
});    
  /* Handling form functionality */
function addItinery() {    
  var data = $("#daily-itinery").serialize();         
  $.ajax({        
    type : 'POST',
    url  : '../../settings/sql-master.php',
    data : data,
    beforeSend: function(){ 
      $("#error").fadeOut();
      $("#add_btn").html('Adding..');
      $("#add_db").html('Saving..');
    },
    success : function(response){
      if(response == 1) {                 
        $("#add_btn").html('Add');
          $("#daily-itinery")[0].reset();
      } else if(response == 2) {                 
        $("#itinerary_response").fadeIn(1000, function(){            
          $("#itinerary_response").html('<div class="alert alert-danger"> Sorry, there was an error saving!</div>');
          $("#add_btn").html('Add');
        });
        $("#itinerary_response").delay(6000).fadeOut(function(){});
      }else if(response == 3) {                 
        $("#itinerary_response").fadeIn(1000, function(){            
          $("#itinerary_response").html('<div class="alert alert-success"> A backup has been saved successfully</div>');
          $("#add_db").html('Add');
          $("#daily-itinery")[0].reset();
        });
        $("#itinerary_response").delay(6000).fadeOut(function(){});
        getDB();

      }
      else if(response == 4) {                 
        $("#itinerary_response").fadeIn(1000, function(){            
          $("#itinerary_response").html('<div class="alert alert-danger"> Sorry, there was error an error taking the backup, please try again!</div>');
          $("#add_db").html('Add');
          $("#daily-itinery")[0].reset();
        });
        $("#itinerary_response").delay(6000).fadeOut(function(){});
        getDB();
        
      }



      fetchDI();
    }
  });
  return false;
}  

function fetchInvoiceReport(){
  var action = "all";
  $.ajax({
    url:"get_invoice_report.php",
    method:"GET",
    data:{action:action},
    success:function(data){
      $('#show_invoice_report').html(data);
    }
  });
}
fetchInvoiceReport();
//invoices today
    $(document).on('click', '.invoice_today', function(){      
          //var action='pettycash_today';
          var action = "invoice_today";
          $.ajax({
            url:"get_invoice_report.php",
            method:"GET",
            data:{action:action},
            success:function(data){
              $('#show_invoice_report').html(data);
            }
          });        
    });
    $(document).on('click', '.invoice_week', function(){      
          //var action='pettycash_today';
          var action = "invoice_week";
          $.ajax({
            url:"get_invoice_report.php",
            method:"GET",
            data:{action:action},
            success:function(data){
              $('#show_invoice_report').html(data);
            }
          });        
    });
    $(document).on('click', '.invoice_month', function(){      
          //var action='pettycash_today';
          var action = "invoice_month";
          $.ajax({
            url:"get_invoice_report.php",
            method:"GET",
            data:{action:action},
            success:function(data){
              $('#show_invoice_report').html(data);
            }
          });        
    });

    $(document).on('click', '.invoice_year', function(){      
          //var action='pettycash_today';
          var action = "invoice_year";
          $.ajax({
            url:"get_invoice_report.php",
            method:"GET",
            data:{action:action},
            success:function(data){
              $('#show_invoice_report').html(data);
            }
          });        
    });

    
//travel advance today
$(document).on('click', '.advanced_today', function(){      
      //var action='advanced_today';
      var action = "advanced_today";
      $.ajax({
        url:"get_travel_advance_report.php",
        method:"GET",
        data:{action:action},
        success:function(data){
          $('#show_travel_advance_report').html(data);
        }
      });
    
});

//week
$(document).on('click', '.advanced_week', function(){      
      //var action='advanced_today';
      var action = "advanced_week";
      $.ajax({
        url:"get_travel_advance_report.php",
        method:"GET",
        data:{action:action},
        success:function(data){
          $('#show_travel_advance_report').html(data);
        }
      });    
});

//advanced_month
$(document).on('click', '.advanced_month', function(){      
      //var action='advanced_today';
      var action = "advanced_month";
      $.ajax({
        url:"get_travel_advance_report.php",
        method:"GET",
        data:{action:action},
        success:function(data){
          $('#show_travel_advance_report').html(data);
        }
      });    
});

//advanced_year
$(document).on('click', '.advanced_year', function(){      
      //var action='advanced_today';
      var action = "advanced_year";
      $.ajax({
        url:"get_travel_advance_report.php",
        method:"GET",
        data:{action:action},
        success:function(data){
          $('#show_travel_advance_report').html(data);
        }
      });
    
});

//advance request reports
//today
$(document).on('click', '.advancerequest_today', function(){      
      //var action='advanced_today';

      var action = "advanced_today";
      $.ajax({
        url:"get_advance_report.php",
        method:"GET",
        data:{action:action},
        success:function(data){
          $('#show_advance_request_report').html(data);
        }
      });
    
});

//week
$(document).on('click', '.advancerequest_week', function(){      
      //var action='advanced_today';
      var action = "advanced_week";
      $.ajax({
        url:"get_advance_report.php",
        method:"GET",
        data:{action:action},
        success:function(data){
          $('#show_advance_request_report').html(data);
        }
      });
    
});

  //advanced_month
  $(document).on('click', '.advancerequest_month', function(){      
        //var action='advanced_today';
        var action = "advanced_month";
        $.ajax({
          url:"get_advance_report.php",
          method:"GET",
          data:{action:action},
          success:function(data){
            $('#show_advance_request_report').html(data);
          }
        });
      
  });

  //advanced_year
  $(document).on('click', '.advancerequest_year', function(){      
        //var action='advanced_today';
        var action = "advanced_year";
        $.ajax({
          url:"get_advance_report.php",
          method:"GET",
          data:{action:action},
          success:function(data){
            $('#show_advance_request_report').html(data);
          }
        });
        
    });


    //petty cash
    $(document).on('click', '.pettycash_today', function(){      
          //var action='pettycash_today';
          var action = "pettycash_today";
          $.ajax({
            url:"get_petty_cash_report.php",
            method:"GET",
            data:{action:action},
            success:function(data){
              $('#show_petty_cash_report').html(data);
            }
          });
        
    });

    $(document).on('click', '.pettycash_week', function(){      
          //var action='pettycash_today';
          var action = "pettycash_week";
          $.ajax({
            url:"get_petty_cash_report.php",
            method:"GET",
            data:{action:action},
            success:function(data){
              $('#show_petty_cash_report').html(data);
            }
          });
        
    });

    $(document).on('click', '.pettycash_month', function(){      
          //var action='pettycash_month';
          var action = "pettycash_month";
          $.ajax({
            url:"get_petty_cash_report.php",
            method:"GET",
            data:{action:action},
            success:function(data){
              $('#show_petty_cash_report').html(data);
            }
          });
        
    });

    $(document).on('click', '.pettycash_year', function(){      
          //var action='pettycash_year';
          var action = "pettycash_year";
          $.ajax({
            url:"get_petty_cash_report.php",
            method:"GET",
            data:{action:action},
            success:function(data){
              $('#show_petty_cash_report').html(data);
            }
          });
        
    });

    function fetchPettyCashReport(){
      var action = "all";
      $.ajax({
        url:"get_petty_cash_report.php",
        method:"GET",
        data:{action:action},
        success:function(data){
          $('#show_petty_cash_report').html(data);
        }
      });
    }

    fetchPettyCashReport()

  
    //request an advance
    $("#request-advance").validate({
      rules: {
        action:{required:true},
        start:{required:true},
        end:{required:true},
        amount:{required:true},
        reasons:{required:true},
        comment:{required:true},
        paid_date:{required:true},
      },
      messages: {
        action:"Please select the action",
        start:"Please enter starting month of installment",
        end:"Please enter ending  month of installment",
        amount:"Please enter amount",
        reasons:"Please enter purpose of the requested advance",
        comment:"Please enter comments on status of previous advances",
        paid_date:"Please enter date of payment",
      },
      submitHandler: requestAdvance  
    });    
    /* Handling form functionality */
    function requestAdvance() {    
      var data = $("#request-advance").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#response").fadeOut();
          $("#request_advance").html(' Posting Request...');
          $("#verify_advance").html(' Submitting...');
          $("#check_advance").html(' Submitting...');
          $("#approve_advance").html(' Approving...');
          $("#make_payment").html(' Posting...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> Your staff advance request has been posted successfuly.</div>');
              $("#request_advance").html('Advance Request');
              $("#request-advance")[0].reset();
            });
            $("#response").delay(6000).fadeOut(function(){});
            //setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 2) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, there was an error posting the staff advance request, please try again</div>');
              $("#request_advance").html('Advance Request');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
          if(response == 3) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-warning"> The repayment period can not be more than 3 months, please check your start and end dates.</div>');
              $("#request_advance").html('Advance Request');
            });
            $("#response").delay(6000).fadeOut(function(){});
            //setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 4) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, make sure the start and end dates are in order.</div>');
              $("#request_advance").html('Advance Request');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
          if(response == 5) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> The advance request has been checked successfuly.</div>');
              $("#verify_advance").html('Submitting');
              //request-advance
            });
            $("#response").delay(6000).fadeOut(function(){});
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 6) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, there was an error checking the advance request, please try again.</div>');
              $("#verify_advance").html('Submitting');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
          if(response == 7) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> The advance request has been verified successfuly.</div>');
              $("#check_advance").html('Submitting');
              //request-advance
            });
            $("#response").delay(6000).fadeOut(function(){});
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 8) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, there was an error verifying the advance request, please try again.</div>');
              $("#check_advance").html('Submitting');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }

          if(response == 9) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> The advance request has been approved successfuly.</div>');
              $("#approve_advance").html('Approve Advance');
              //request-advance
            });
            $("#response").delay(6000).fadeOut(function(){});
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 10) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, there was an error approving the advance request, please try again.</div>');
              $("#approve_advance").html('Approve Advance');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
          if(response == 11) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> The advance repayment has been recorded successfuly.</div>');
              $("#make_payment").html('Make Payment');
              //request-advance
            });
            $("#response").delay(6000).fadeOut(function(){});
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 12) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, there was an error recording the advance repayment, please try again.</div>');
              $("#make_payment").html('Make Payment');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
          else if(response == 13) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> Sorry, the remaining balance can not be less than zero, check the figures.</div>');
              $("#make_payment").html('Make Payment');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
        }
      });
      return false;
    } 

    //add invoice
    $("#add-invoice").validate({
      rules: {
        sacco:{required:true},
        description:{required:true},
        invoice_number:{required:true},
        amount:{required:true},
        amount_paid:{required:true},
        invoice:{required:true},
        category_name:{required:true},
        due_date:{required:true}
      },
      messages: {
        sacco:"Please select the owner of the invoice",
        description:"Please give the invoice a description.",
        invoice_number:"Please enter invoice number",
        invoice:"Please attach the scanned invoice",
        amount:"Please enter the amount on invoice",
        amount_paid:"Please enter the amount paid",
        category_name:"Please enter the category name",
        due_date:"Please enter due date",
      },
      submitHandler: addInvoice  
    });    
    /* Handling form functionality */
    function addInvoice() {    
      var formData = $("#add-invoice").submit(function () {
          return;
      });
      var formData = new FormData(formData[0]);       
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : formData,
        beforeSend: function(){ 
          $("#response").fadeOut();
          $("#add_invoice").html('Saving Invoice...');
          $("#add_category").html('Saving Category...');
          $("#change_status").html('Updating...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> An invoice has been saved successfuly.</div>');
              $("#add_invoice").html('Save Invoice');

            });
            setTimeout(' window.location.href = ""; ', 4000);
          } else if(response == 2) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> An error occoured saving the invoice, please try again.</div>');
              $("#add_invoice").html('Save Invoice');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
          else if(response == 3) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> There was an error uploading the invoice, please try again.</div>');
              $("#add_invoice").html('Save Invoice');
            });
            $("#response").delay(6000).fadeOut(function(){});
          }
          else if(response == 4) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-danger"> The file extension of the invoice attached is not supported, it must be pdf, doc, docx, jpg or jpeg.</div>');
              $("#add_invoice").html('Save Invoice');
            });
            $("#response").delay(10000).fadeOut(function(){});
          }
          else if(response == 6) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> An invoice has been updated successfuly.</div>');
              $("#add_invoice").html('Save Invoice');
            });
            setTimeout(' window.location.href = ""; ', 4000);
          }

          if(response == 7) {                 
            $("#response_cat").fadeIn(1000, function(){            
              $("#response_cat").html('<div class="alert alert-success"> A category has been added successfuly.</div>');
              $("#add_category").html('Add Category');
            });
            setTimeout(' window.location.href = ""; ', 4000);
          }
          if(response == 8) {                 
            $("#response_cat").fadeIn(1000, function(){            
              $("#response_cat").html('<div class="alert alert-success"> An invoice has been updated successfuly.</div>');
              $("#add_category").html('Add Category');
            });
            $("#response_cat").delay(10000).fadeOut(function(){});
          }

          if(response == 9) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> A loan status has been updated successfuly.</div>');
              $("#change_status").html('Update Status');
            });
            setTimeout(' window.location.href = ""; ', 4000);
          }
          if(response == 10) {                 
            $("#response").fadeIn(1000, function(){            
              $("#response").html('<div class="alert alert-success"> Sorry, there was an error in updating the loan status.</div>');
              $("#change_status").html('Update Status');
            });
            $("#response").delay(10000).fadeOut(function(){});
          }


          getPendinginvoices();
        },
        contentType: false,
        processData: false,
        cache: false
      });
      return false;
    }

    //update invoice status
    $("#update-invoice").validate({
      rules: {
        status: {required: true},
        comment: {required: true},
        amount:{required:true}
      },
      messages: {
        status:{required: "Please select invoice status"},
        comment:{required: "Please enter invoice status comment"},
        amount:{required:"Please enter amount paid"}
      },
      submitHandler: updateInvoice  
    });    
    /* Handling form functionality */
    function updateInvoice() {    
      //var data = $("#update-invoice").serialize(); 
      var formData = $("#update-invoice").submit(function () {
          return;
      });
      var formData = new FormData(formData[0]);       
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : formData,
        beforeSend: function(){ 
          $("#update_response").fadeOut();
          $("#update_invoice").html(' Updating Invoice...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#update_response").fadeIn(1000, function(){            
              $("#update_response").html('<div class="alert alert-success"> Invoice details have been updated successfuly!</div>');
              $("#update_invoice").html('Update Invoice');
            });
            $("#update_response").delay(6000).fadeOut(function(){});
          } 
          else if(response == 3) {                 
            $("#update_response").fadeIn(1000, function(){            
              $("#update_response").html('<div class="alert alert-success"> Invoice status has been updated successfuly!</div>');
              $("#update_invoice").html('Update Invoice');
            });
            setTimeout(' window.location.href = ""; ', 4000);
          } 
          else if(response == 2) {                 
            $("#update_response").fadeIn(1000, function(){            
              $("#update_response").html('<div class="alert alert-danger"> Sorry, either there was an error or there is no new information to update!</div>');
              $("#update_invoice").html('Update Invoice');
            });
            $("#update_response").delay(6000).fadeOut(function(){});
          }
        },
        contentType: false,
        processData: false,
        cache: false
      });
      return false;
    } 

    //add event
    $("#add-event").validate({
      rules: {
        title:{required:true},
        venue:{required:true},
        description:{required:true},
        date_from:{required:true},
        access_rights:{required:true}
      },
      messages: {
        title:"Please enter event title",
        venue:"Please enter event venue",
        description:"Please enter event description",
        date_from:"Please enter start date",
        access_rights:"Please select authorised usergroup"
      },
      submitHandler: addEvent  
    });    
    /* Handling form functionality */
    function addEvent() {    
      //var data = $("#add-event").serialize(); 
      var formData = $("#add-event").submit(function () {
          return;
      });
      var formData = new FormData(formData[0]);         
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : formData,
        beforeSend: function(){ 
          $("#error").fadeOut();
          $("#add_event").html(' Saving Event...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-success"> An event has been saved!</div>');
              $("#add_event").html('Save Event');
            });
            $("#error").delay(6000).fadeOut(function(){});
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 2) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error saving the event, please try again!</div>');
              $("#add_event").html('Save Event');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 3) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error adding the event attachment, please check if the attached file is valid</div>');
              $("#add_event").html('Save Event');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 4) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> The attached file is invalid, make sure it is of type pdf, docx, .doc, .jpeg, .jpg, .png </div>');
              $("#add_event").html('Save Event');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 5) {                 
                $("#error").fadeIn(1000, function(){            
                  $("#error").html('<div class="alert alert-danger"> The event start date can not be a date in the past, please chack the dates.</div>');
                  $("#add_event").html('Save Event');
                });
                $("#error").delay(6000).fadeOut(function(){});
              }
              else if(response == 6) {                 
                $("#error").fadeIn(1000, function(){            
                  $("#error").html('<div class="alert alert-danger"> The event start date can not be a date in the future than the end date, please chack the dates.</div>');
                  $("#add_event").html('Save Event');
                });
                $("#error").delay(6000).fadeOut(function(){});
              }
        },
        contentType: false,
        processData: false,
        cache: false
      });
      return false;
    }


    //leave types, FY, add leave days 
    $("#leave-type").validate({
      rules: {
        name:{required:true},
        leave:{required:true},
        days:{required:true},
        start_date:{required:true},
        end_date:{required:true},
        leave_days:{required:true},
        action:{required:true},
        reasons:{required:true},
        date:{required:true},
        holiday:{required:true}
      },
      messages: {
        name:{required:"Please enter the leave type"},
        leave:{required:"Please select leave type"},
        days:{required:"Please eneter leave days entitled"},
        start_date:{required:"Please select start of FY"},
        end_date:{required:"Please select end of FY"},
        leave_days:{required:"Please enter verified leave days"},
        action:"Please select action",
        reasons:"Please enter a remark",
        holiday:"Please enter the public holiday",
        date:"Please enter the date"
      },
      submitHandler: addLeave  
    });    
    /* Handling form functionality */
    function addLeave() {    
      var data = $("#leave-type").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#product_response").fadeOut();
          $("#add_leave").html(' Saving...');
          $("#assign_leave").html(' Saving...');
          $("#add_fy").html(' Saving FY...');
          $("#update_status").html(' Updating Status...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-success"> A new leave type has been saved successfuly.</div>');
              $("#add_leave").html('Add Leave');
              $("#leave-type")[0].reset();
            });
            $("#product_response").delay(6000).fadeOut(function(){});
            getLeave();
          }else if(response == 2) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-danger"> Sorry, there was an error saving the leave type, please try again.</div>');
              $("#add_leave").html('Add Leave');
            });
            $("#product_response").delay(6000).fadeOut(function(){});
          }
          else if(response == 3) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-success"> The employee entitlement has been saved successfuly.</div>');
              $("#assign_leave").html('Save Days');
              $("#leave-type")[0].reset();
            });
            $("#product_response").delay(6000).fadeOut(function(){});
            getDays();
          }
          else if(response == 4) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-danger"> Sorry, there was an error saving the employee entitlement, please try again.</div>');
              $("#assign_leave").html('Save Days');
            });
            $("#product_response").delay(6000).fadeOut(function(){});
          }
          else if(response == 5) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-success"> The financial year has been updated successfuly.</div>');
              $("#add_fy").html('Update FY');
              $("#leave-type")[0].reset();
            });
            $("#product_response").delay(6000).fadeOut(function(){});
            getFY();
          }
          else if(response == 6) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-danger"> Sorry, there was an error updating the financial year, please try again.</div>');
              $("#add_fy").html('Update FY');
            });
            $("#product_response").delay(6000).fadeOut(function(){});
          }

          else if(response == 7) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-success"> The leave application status has been updated successfuly.</div>');
            });
            setTimeout(' window.location.href = ""; ', 6000);
          }
          else if(response == 8) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-danger"> Sorry, there was an error updating the leave application, please try again.</div>');
            });
            $("#product_response").delay(6000).fadeOut(function(){});
          }

          else if(response == 9) {                 
            $("#product_response").fadeIn(500, function(){            
              $("#product_response").html('<div class="alert alert-success"> The Public holiday has been recorded successfuly.</div>');
            });
            setTimeout(' window.location.href = ""; ', 6000);
          }
          else if(response == 10) {                 
            $("#product_response").fadeIn(1000, function(){            
              $("#product_response").html('<div class="alert alert-danger"> Sorry, there was an error saving the public holiday, please try again.</div>');
            });
            $("#product_response").delay(6000).fadeOut(function(){});
          }


        }
      });
      return false;
    } 

    //apply leave
    $("#apply-leave").validate({
      rules: {
        type:{required:true},
        leave_roaster:{required:true},
        leave_grant:{required:true},
        date_from:{required:true},
        date_to:{required:true},
        reasons:{required:true}
      },
      messages: {
        type:{required:"Please select the leave type"},
        leave_roaster:{required:"Please select a response to the question"},
        leave_grant:{required:"Please select a response to the question"},
        date_from:{required:"Please select start of leave"},
        date_to:{required:"Please select end of leave"},
        reasons:{required:"Please enter your reasons"}
      },
      submitHandler: applyLeave  
    });    
    /* Handling form functionality */
    function applyLeave() {    
      var data = $("#apply-leave").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#error").fadeOut();
          $("#apply_leave").html(' Applying Leave...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#error").fadeIn(500, function(){            
              $("#error").html('<div class="alert alert-success"> A new leave application has been posted successfuly.</div>');
              $("#apply_leave").html('Add Leave');
              $("#apply-leave")[0].reset();
            });
            $("#error").delay(6000).fadeOut(function(){});
            
          }else if(response == 2) {                 
            $("#error").fadeIn(500, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error posting the new leave application, please try to refresh and submit again.</div>');
              $("#apply_leave").html('Add Leave');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 3) {                 
            $("#error").fadeIn(500, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, make sure the date from and date to selected are from the future not from the past.</div>');
              $("#apply_leave").html('Add Leave');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 4) {                 
            $("#error").fadeIn(500, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, it seems the selected dates are falling under a weekend or during a public calender, please crosscheck your calender.</div>');
              $("#apply_leave").html('Add Leave');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 5) {                 
            $("#error").fadeIn(500, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, the selected start dates is in the future than the end date, please check the dates and put them in a correct order.</div>');
              $("#apply_leave").html('Add Leave');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }

          else if(response == 6) {                 
            $("#error").fadeIn(500, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, You don\'t have any leave days allocated for this leave type, check with your HR to update your leave days.</div>');
              $("#apply_leave").html('Add Leave');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }

          else if(response == 7) {                 
            $("#error").fadeIn(500, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, Your leave days remaining are less than the leave days you have applied, please check your remaining days correctly..</div>');
              $("#apply_leave").html('Add Leave');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }

        }
      });
      return false;
    } 

    $("#add-topic").validate({
      rules: {
        title:{required:true},
        description:{required:true},
        access_rights:{required:true}
      },
      messages: {
        title:{required:"Please enter the topic"},
        description:{required:"Please enter description of the topic"},
        access_rights:{required:"Please choose who can see and comment on your topic"}
      },
      submitHandler: addTopic  
    });    
    /* Handling form functionality */
    function addTopic() {    
      var data = $("#add-topic").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#topic_response").fadeOut();
          $("#add_topic").html(' Adding Topic...');
          $("#add_comment").html(' Commenting...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#topic_response").fadeIn(1000, function(){            
              $("#topic_response").html('<div class="alert alert-success"> A new topic has been posted.</div>');
              $("#add_topic").html('Add Topic');
              $("#add-topic")[0].reset();
            });
            $("#topic_response").delay(6000).fadeOut(function(){});
            getTopics();
            
          }else if(response == 2) {                 
            $("#topic_response").fadeIn(1000, function(){            
              $("#topic_response").html('<div class="alert alert-danger"> Sorry, there was an error posting the new topic , please try to refresh and submit again.</div>');
              $("#add_topic").html('Add Topic');
            });
            $("#topic_response").delay(6000).fadeOut(function(){});
          }
          else if(response == 3){
            $("#add-topic")[0].reset();
            $("#add_comment").html('Comment');
            getComments();
          }
          else if(response == 4) {                 
            $("#topic_response").fadeIn(1000, function(){            
              $("#topic_response").html('<div class="alert alert-danger"> Sorry, there was an error posting your comment, please try to refresh and submit again.</div>');
              $("#add_comment").html('Comment');
            });
            $("#topic_response").delay(6000).fadeOut(function(){});
          }
        }
      });
      return false;
    } 

     $("#vehicle-request").validate({
      rules: {
        driver:{required:true},
        to:{required:true},
        date_from:{required:true},
        date_to:{required:true},
        activity:{required:true},
        from:{required:true},
        pillar:{required:true},
        purpose:{required:true},
        logistics:{required:true},
        nights:{required:true},
      },
      messages: {
        driver:"Please enter the name of the driver",
        to:"Please enter the destination",
        from:"Please enter departure place",
        date_from:"Please select departure date",
        date_to:"Please select arrival date",
        activity:"Please enter the name of the activity",
        pillar:"Please select the pillar/activity",
        purpose:"Please enter the purpose of your trip",
        logistics:"Please select the logistics",
        nights:"Please enter number of nights",
      },
      submitHandler: vehicleRequest  
    });    
    /* Handling form functionality */
    function vehicleRequest() {    
      //var data = $("#add-event").serialize(); 
      var formData = $("#vehicle-request").submit(function () {
          return;
      });
      var formData = new FormData(formData[0]); 

      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : formData,
        beforeSend: function(){ 
          $("#error").fadeOut();
          $("#vehicle_request").html('Submitting Request...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-success"> Your vehicle request has been submitted successfully!</div>');
              $("#vehicle_request").html('Submit Request');
              $("#vehicle-request")[0].reset();
            });
            $("#error").delay(6000).fadeOut(function(){});
            
            //setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 2) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error submitting the vehicle request, please try again!</div>');
              $("#vehicle_request").html('Submit Request');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          if(response == 3) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-success"> Your travel advance request has been submitted successfully!</div>');
              $("#vehicle_request").html('Submit Request');
              $("#vehicle-request")[0].reset();
            });
            $("#error").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = "dashboard.php?page=travel_advance_request"; ', 6000);
          } else if(response == 4) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error submitting your travel advance request, please try again!</div>');
              $("#vehicle_request").html('Submit Request');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }else if(response == 5) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there are no Daily Itinerary records, please add them first </div>');
              $("#vehicle_request").html('Submit Request');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 6) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Make sure that whenever you add mileage you must select fuel type </div>');
              $("#vehicle_request").html('Submit Request');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }

          else if(response == 7) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-success"> Your travel advance liquidation has been submitted successfully!</div>');
              $("#vehicle_request").html('Liquidate');
            });
            $("#error").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = ""; ', 6000);
          } 
          else if(response == 8) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error submitting your liquidation information, please try again </div>');
              $("#vehicle_request").html('Liquidate');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 9) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error uploading your receipts, please try again </div>');
              $("#vehicle_request").html('Liquidate');
            });
            $("#error").delay(9000).fadeOut(function(){});
          }
          else if(response == 10) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, the attached receipts format is invalid, please make sure it is of type png, jpeg, jpg or pdf </div>');
              $("#vehicle_request").html('Liquidate');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
        },
        contentType: false,
        processData: false,
        cache: false
      });
      return false;
    }

    //assign vehicle
     $("#assign-vehicle").validate({
      rules: {
        reg_number:{required:true},
        mileage:{required:true},
        fuel:{required:true},
        dent:{required:true},
        clean:{required:true},
        tools:{required:true},
        spare:{required:true},
        action:{required:true}
      },
      messages: {
        reg_number:"Please enter the RN of the assigned vehicle",
        mileage:"Please enter the opening mileage",
        fuel:"Please select fuel level",
        dent:"",
        clean:"",
        tools:"",
        spare:"",
        action:"Please select action."
      },
      submitHandler: assignVehicle  
    });    
    /* Handling form functionality */
    function assignVehicle() {    
      var data = $("#assign-vehicle").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#assign_response").fadeOut();
          $("#assign_vehicle").html('Assigning Vehicle...');
          $("#receive_vehicle").html('Receiving Vehicle...');
          $("#check_request").html('Checking...');
          $("#approve_request").html('Approving...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#assign_response").fadeIn(1000, function(){            
              $("#assign_response").html('<div class="alert alert-success"> A vehicle has been assigned successfully!</div>');
              $("#assign_vehicle").html('Assign Vehicle');
              $("#assign-vehicle")[0].reset();
            });
            $("#assign_response").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 2) {                 
            $("#assign_response").fadeIn(1000, function(){            
              $("#assign_response").html('<div class="alert alert-danger"> Sorry, there was an error assigning the vehicle, please try again!</div>');
              $("#assign_vehicle").html('Assign Vehicle');
            });
            $("#assign_response").delay(6000).fadeOut(function(){});
          }
          if(response == 3) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-success"> A vehicle has been authorised successfully!</div>');
              $("#authorize_vehicle").html('Assign Vehicle');
              $("#assign-vehicle")[0].reset();
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 4) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-danger"> Sorry, there was an error authorizing the vehicle, please try again!</div>');
              $("#authorize_vehicle").html('Assign Vehicle');
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
          }
          if(response == 5) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-success"> A vehicle has been received successfully!</div>');
              $("#receive_vehicle").html('Assign Vehicle');
              $("#assign-vehicle")[0].reset();
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 6) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-danger"> Sorry, there was an error marking as received, please try again!</div>');
              $("#receive_vehicle").html('Assign Vehicle');
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
          }
          if(response == 7) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-success"> A travel advance request has been marked as checked successfully!</div>');
              $("#check_request").html('Check');
              $("#assign-vehicle")[0].reset();
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 8) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-danger"> Sorry, there was an error marking as checked, please try again!</div>');
              $("#check_request").html('Check');
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
          }

          if(response == 9) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-success"> A travel advance request has been marked as approved successfully!</div>');
              $("#approve_request").html('Approve');
              $("#assign-vehicle")[0].reset();
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 10) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-danger"> Sorry, there was an error marking as approved, please try again!</div>');
              $("#approve_request").html('Approve');
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
          }

          if(response == 11) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-success"> A travel advance liquidation has been marked as approved successfully!</div>');
              $("#approve_request").html('Approve');
              $("#assign-vehicle")[0].reset();
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 12) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-danger"> Sorry, there was an error marking the travel advance liquidation as approved, please try again!</div>');
              $("#approve_request").html('Approve');
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
          }

          if(response == 13) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-success"> A travel advance liquidation has been marked as Checked successfully!</div>');
              $("#approve_request").html('Check');
              $("#assign-vehicle")[0].reset();
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
            
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 14) {                 
            $("#authorize_response").fadeIn(1000, function(){            
              $("#authorize_response").html('<div class="alert alert-danger"> Sorry, there was an error marking the travel advance liquidation as Checked, please try again!</div>');
              $("#approve_request").html('Check');
            });
            $("#authorize_response").delay(6000).fadeOut(function(){});
          }
        }
      });
      return false;
    }

    //add member
    $("#add-member").validate({
      rules: {
        password: {
          required: true,
          minlength: 8

        },
        re_password: {
          required: true,
          equalTo:"#password"
        },
        first_name: {required:true},
        last_name: {required:true},
        emp_number: {required:true},
        username: {required:true},
        position: {required:true},
        department: {required:true},

      },
      messages: {
        password:{
          required: "Please enter the password"
         },
        re_password: {
          required:"Please re enter the password",
          equalTo: "Please confirm the password entered"
        },
        first_name:"Please enter the first name",
        last_name:"Please enter the last name",
        username:"Please enter the username",
        emp_number:"Please enter the employment number",
        position:"Please select the position of the staff member",
        department:"Please select the department of the staff member",
      },
      submitHandler: addMember  
    });    
    /* Handling form functionality */
    function addMember() {    
      var data = $("#add-member").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#error").fadeOut();
          $("#save_member").html('Saving Member...');
          $("#update_member").html('Updating Details...');
        },
        success : function(response){ 
          if(response == 1){
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-success"> You have updated your details successfuly!</div>');
              $("#save_member").html('Save');
              $("#update_member").html('Update Details');
            });
            $("#add-member")[0].reset();
            $("#error").delay(6000).fadeOut(function(){});
          }else if(response == 2){
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error updating your details or there is no new information to save!</div>');
              $("#save_member").html('Save');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          else if(response == 3){
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error updating your details or there is no new information to save</div>');
              $("#update_member").html('Update Details');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
          
        }
      });
      return false;
    } 
    /* end script */ 

    $("#change-username").validate({
      rules: {
        username:{required:true}
      },
      messages: {
        username:{required:"Please enter the new username"}
      },
      submitHandler: changeUsername  
    });    
    /* Handling form functionality */
    function changeUsername() {    
      var data = $("#change-username").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#username_response").fadeOut();
          $("#change_username").html(' Changing Username...');
        },
        success : function(response){
          if(response == 5) {                 
            $("#username_response").fadeIn(1000, function(){            
              $("#username_response").html('<div class="alert alert-danger"> Sorry, the username entered is already in use, please choose a different username.</div>');
              $("#change_username").html('Change Username');
            });
            $("#username_response").delay(6000).fadeOut(function(){});
          }else if(response == 4) {                 
            $("#username_response").fadeIn(1000, function(){            
              $("#username_response").html('<div class="alert alert-danger"> Sorry, there was an error changing the username, please try again.</div>');
              $("#change_username").html('Change Username');
            });
            $("#username_response").delay(6000).fadeOut(function(){});
          }
          else if(response == 3) {                 
            $("#username_response").fadeIn(1000, function(){            
              $("#username_response").html('<div class="alert alert-success"> The username has been changed successfuly.</div>');
              $("#change_username").html('Change Username');
            });
            $("#username_response").delay(6000).fadeOut(function(){});
          }


        }
      });
      return false;
    } 

     //reset password
    $("#reset-password").validate({
      rules: {
        password: {
          required: true,
          minlength: 8
        },
        re_password: {
          required: true,
          equalTo:"#password"
        },
        old_password:{required:true},
        username:{required:true}
      },
      messages: {
        password:{
          required: "Please enter the new password"
        },
        re_password:{
          required: "Please re enter the new password to confirm"
        },
        username:{required:"Please enter the new username"},
        old_password:"Please enter your current password"
      },
      submitHandler: resetPassword  
    });    
    /* Handling form functionality */
    function resetPassword() {    
      var data = $("#reset-password").serialize();        
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : data,
        beforeSend: function(){ 
          $("#password_response").fadeOut();
          $("#reset").html(' Resetting Password...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#password_response").fadeIn(1000, function(){            
              $("#password_response").html('<div class="alert alert-success"> The password has been changed successfuly!</div>');
              $("#reset").html('Reset Password');          
            });
            $("#password_response").delay(6000).fadeOut(function(){});
            setTimeout(' window.location.href = ""; ', 6000);
            $("#reset-password")[0].reset();        
            
          } else if(response == 2) {                 
            $("#password_response").fadeIn(1000, function(){            
              $("#password_response").html('<div class="alert alert-danger"> Sorry, there was an error updating the password, please try again.</div>');
              $("#reset").html('Reset Password');
            });
            $("#password_response").delay(6000).fadeOut(function(){});
          }

          else if(response == 3) {                 
            $("#password_response").fadeIn(1000, function(){            
              $("#password_response").html('<div class="alert alert-danger"> The current password entered is incorrect.</div>');
              $("#reset").html('Reset Password');
            });
            $("#password_response").delay(6000).fadeOut(function(){});
          }
          else if(response == 4) {                 
            $("#password_response").fadeIn(1000, function(){            
              $("#password_response").html('<div class="alert alert-danger"> Make sure your password includes an uppercase & lowercase letter, a number,and one special character.</div>');
              $("#reset").html('Reset Password');
            });
            $("#password_response").delay(6000).fadeOut(function(){});
          }
        }
      });
      return false;
    } 

    //btn_ticket_delete
    $(document).on('click', '.btn_ticket_delete', function(){
       var id=$(this).data("id3");
       var file = $(this).data("file");

       if(confirm("Are you sure you want to delete this ticket?")){
           var action='ticket_delete';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, file:file, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#err").fadeIn(1000, function(){
                           $("#err").html('<div class="alert alert-success" role="alert"> The selected ticket has been deleted.</div>');
                       });
                       $("#err").delay(6000).fadeOut(function(){});
                       setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#err").fadeIn(1000, function(){
                           $("#err").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleteting the selected ticket, please try again!</div>');
                       });
                       $("#err").delay(6000).fadeOut(function(){});
                   }
               }
           });
       }
    });


    //delete event
    $(document).on('click', '.btn_event_delete', function(){
       var id=$(this).data("id3");
       var file = $(this).data("file");

       if(confirm("Are you sure you want to delete this event?"))
       {
           var action='delete_event';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action, file:file},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#err").fadeIn(1000, function(){
                           $("#err").html('<div class="alert alert-success" role="alert"> The selected event has been deleted successfully!</div>');
                       });
                       setTimeout(' window.location.href = "dashboard.php?page=event_list"; ', 6000);
                   }else{
                       $("#err").fadeIn(1000, function(){
                           $("#err").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting the  selected event, please try again!</div>');
                       });
                       $("#err").delay(6000).fadeOut(function(){});
                   }
               }
           });
       }
    });

    //delete leave
    $(document).on('click', '.delete_leave_type', function(){
       var id=$(this).data("id3");

       if(confirm("Are you sure you want to delete this leave type?"))
       {
           var action='delete_leave_type';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#product_response").fadeIn(1000, function(){
                           $("#product_response").html('<div class="alert alert-success" role="alert"> The selected leave type has been deleted successfully!</div>');
                       });
                       setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#product_response").fadeIn(1000, function(){
                           $("#product_response").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting the  selected leave type, please try again!</div>');
                       });
                       $("#product_response").delay(6000).fadeOut(function(){});
                   }
               }
           });
       }
    });

    //update_fy
    $(document).on('click', '.update_fy', function(){
       var id=$(this).data("id3");

       if(confirm("Are you sure you want to update the financial year?"))
       {
           var action='update_fy';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#product_response").fadeIn(1000, function(){
                           $("#product_response").html('<div class="alert alert-success" role="alert"> The financial year has been updated successfully!</div>');
                       });
                       setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#product_response").fadeIn(1000, function(){
                           $("#product_response").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error updating the financial year, please try again!</div>');
                       });
                       $("#product_response").delay(6000).fadeOut(function(){});
                   }
               }
           });
       }
    });

    //cancel event
    $(document).on('click', '.btn_event_cancel', function(){
       var id=$(this).data("id3");


       if(confirm("Are you sure you want to cancel this event?"))
       {
           var action='cancel_event';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#err").fadeIn(1000, function(){
                           $("#err").html('<div class="alert alert-success" role="alert"> The selected event has been marked as cancelled successfully!</div>');
                       });
                       setTimeout(' window.location.href = ""; ', 4000);
                   }else{
                       $("#err").fadeIn(1000, function(){
                           $("#err").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error marking the  selected event as cancelled, please try again!</div>');
                       });
                       $("#err").delay(6000).fadeOut(function(){});
                   }
               }
           });
       }
    });



    //complete event
    $(document).on('click', '.btn_event_complete', function(){
       var id=$(this).data("id3");


   if(confirm("Are you sure you want to mark this event as complete?"))
   {
       var action='complete_event';
       $.ajax({
           url:"../../settings/sql-master.php",
           method:"GET",
           data:{id:id, action:action},
           dataType:"text",
           success:function(data){
               if(data == 1){
                   $("#err").fadeIn(1000, function(){
                       $("#err").html('<div class="alert alert-success" role="alert"> The selected event has been marked as complete successfully!</div>');
                   });
                   setTimeout(' window.location.href = ""; ', 4000);
               }else{
                   $("#err").fadeIn(1000, function(){
                       $("#err").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error marking the  selected event as complete, please try again!</div>');
                   });
                   $("#err").delay(6000).fadeOut(function(){});
               }
           }
       });
   }
    });

    //add document
    $("#add-document").validate({
      rules: {
        category: {required: true},
        title: {required: true},
        file: {required: true},
        access_rights: {required: true},
      },
      messages: {
        category:{required: "Please select document category"},
        title:{required: "Please enter document title"},
        file:{required: "Please select a document to upload"},
        access_rights:{required: "Please select authorised usergroup"},
      },
      submitHandler: addDocument  
    });    
    /* Handling form functionality */
    function addDocument() {    
      //var data = $("#add-document").serialize(); 
      var formData = $("#add-document").submit(function () {
          return;
      });
      var formData = new FormData(formData[0]);       
      $.ajax({        
        type : 'POST',
        url  : '../../settings/sql-master.php',
        data : formData,
        beforeSend: function(){ 
          $("#error").fadeOut();
          $("#add_document").html(' Uploading Document...');
        },
        success : function(response){
          if(response == 1) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-success"> A document has been uploaded.</div>');
              $("#add_document").html('Upload Document');
            });
            setTimeout(' window.location.href = ""; ', 6000);
          } else if(response == 2) {                 
            $("#error").fadeIn(1000, function(){            
              $("#error").html('<div class="alert alert-danger"> Sorry, there was an error uploading the document, please try again.</div>');
              $("#add_document").html('Upload Document');
            });
            $("#error").delay(6000).fadeOut(function(){});
          }
        },
        contentType: false,
        processData: false,
        cache: false
      });
      return false;
    } 

    //delete documents
    $(document).on('click', '.delete_document', function(){
       var id=$(this).data("id3");
       var doc=$(this).data("doc");


       if(confirm("Are you sure you want to delete this document?"))
       {
           var action='delete_document';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action, doc:doc},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#doc_response").fadeIn(1000, function(){
                           $("#doc_response").html('<div class="alert alert-success" role="alert"> The selected document has been deleted !</div>');
                       });
                       setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#doc_response").fadeIn(1000, function(){
                           $("#doc_response").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting the  selected document!</div>');
                       });
                       $("#doc_response").delay(6000).fadeOut(function(){});
                   }
               }
           });
       }
    });

    //delete document category
    $(document).on('click', '.delete_category', function(){
       var id=$(this).data("id3");
       var doc=$(this).data("doc");


       if(confirm("Are you sure you want to delete this document category?"))
       {
           var action='delete_category';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action, doc:doc},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#category_msg").fadeIn(1000, function(){
                           $("#category_msg").html('<div class="alert alert-success" role="alert"> The selected category has been deleted !</div>');
                       });
                       $("#category_msg").delay(6000).fadeOut(function(){});
                       setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#category_msg").fadeIn(1000, function(){
                           $("#category_msg").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting the  selected category!</div>');
                       });
                       $("#category_msg").delay(6000).fadeOut(function(){});
                   }
                  getDocCategories();
               }
           });
       }
    });

    //delete_topic
    $(document).on('click', '.delete_topic', function(){
       var id=$(this).data("id3");


       if(confirm("Are you sure you want to delete this topic?"))
       {
           var action='delete_topic';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#topic_response").fadeIn(1000, function(){
                           $("#topic_response").html('<div class="alert alert-success" role="alert"> The selected topic has been deleted !</div>');
                       });
                       $("#topic_response").delay(6000).fadeOut(function(){});
                       //setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#topic_response").fadeIn(1000, function(){
                           $("#topic_response").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting the  selected topic!</div>');
                       });
                       $("#topic_response").delay(6000).fadeOut(function(){});
                   }
                  getTopics();
               }
           });
       }
    });

    //delete_topic
    $(document).on('click', '.btn_delete_holidy', function(){
       var id=$(this).data("id3");


       if(confirm("Are you sure you want to delete this holiday?"))
       {
           var action='delete_holiday';
           $.ajax({
               url:"../../settings/sql-master.php",
               method:"GET",
               data:{id:id, action:action},
               dataType:"text",
               success:function(data){
                   if(data == 1){
                       $("#product_response").fadeIn(500, function(){
                           $("#product_response").html('<div class="alert alert-success" role="alert"> The selected holiday has been deleted !</div>');
                       });
                       $("#product_response").delay(6000).fadeOut(function(){});
                       setTimeout(' window.location.href = ""; ', 6000);
                   }else{
                       $("#product_response").fadeIn(500, function(){
                           $("#product_response").html('<div class="alert alert-warning" role="alert"> Sorry, there was an error deleting the  selected topic!</div>');
                       });
                       $("#product_response").delay(6000).fadeOut(function(){});
                   }
               }
           });
       }
    });

});
  
  
</script>