       <link href="<?php echo base_url(); ?>assets/vendors/select2/dist/css/select2.min.css" rel="stylesheet">
        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
                <div class="title_left">
                    <h3><?php echo $form['type']; ?></h3>
                </div>
            </div>
            <div class="clearfix"></div>

            <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Add</h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                  
                   <?php  if($form['type']=="cash payment"){
                     $n="CP";
                     $name="TO";
                   }else if($form['type']=="cash receipt"){
                     $n="CR";
                     $name="From";
                   }else if($form['type']=="bank payment"){
                     $n="BP";
                     $name="TO";
                   }else if($form['type']=="bank receipt"){
                     $n="BR";
                     $name="From";
                   }else if($form['type']=="journal voucher"){
                     $n="JV";
                     $name="From";
                   }else if($form['type']=="petty cash"){
                     $n="PC";
                     $name="TO";
                   } ?>
                    <form class="form-horizontal form-label-left" enctype="multipart/form-data" action="<?php echo base_url(); ?>account/SaveTransactions"  method="post">
                     
                        <div class="item form-group">
                        <label class="control-label col-md-2 col-sm-4 col-xs-12" for="name"><?php echo "Paid ".$name; ?><span class="required">*</span>
                        </label>
                        <div class="col-md-10 col-sm-8 col-xs-12">
                        <input type="text" name="to"  id="to"  class="form-control" maxlength="50" >                    
                       
                      </div>
                      </div>
                     
                     <div class="row">
                     
                      <div class="item form-group col-md-4"  >
                        <label class="control-label col-md-4 col-sm-4 col-xs-12" for="name">Date<span class="required">*</span>
                        </label>
                        <div class="col-md-8 col-sm-8 col-xs-12">
                        <input name="date"   id="date" value="<?php echo date('Y-m-d') ?>" class="form-control date" maxlength="20" required>                    
                        </div>
                      </div>    
                      
                      <div class="item form-group col-md-4">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Cash/Chq#<span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        <input name="cheque" <?php  if($form['type']!="bank payment" && $form['type']!="bank receipt"){ echo "readonly";} ?> value="<?php if($form['type']!="bank payment" ||$form['type']!="bank receipt" ){ echo $cash;}else{ echo "";} ?>"  class="form-control num"  />                    
                        </div>
                      </div>
                     <?php  $bank=$this->account_model->allbankl5();
                          ?>
                       <div class="item form-group col-md-4">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Bank<span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select <?php if($form['type']!="bank payment" && $form['type']!="bank receipt" ){ echo "disabled";} ?>   name="bankid" id="bank" class="form-control bank" required>
                              <option value="">Select Bank</option> 
                              <?php
                              foreach ($bank as $value) {
                                
                                ?>
                                <option  data-balance="<?php echo $value['balance']; ?>"  value="<?php echo $value['id'];?>"><?php echo $value['name'];?></option>  
                                <?php
                              }  
                              ?>
                            </select>

                        </div>
                        <span id="ammount"></span>
                      </div>
                       
                      
                      <div class="item form-group col-md-4">
                        <label class="control-label col-md-5 col-sm-4 col-xs-12" for="name">Voucher #<span class="required">*</span>
                        </label>
                        <div class="col-md-7 col-sm-7 col-xs-12">
                        <input name="voucher" readonly value="<?php echo $n."".$voucher_no; ?>"    class="form-control " maxlength="20" required>                    
                        </div>
                      </div> 
                      <div class="item form-group col-md-4">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Programme<span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                             <select name="prog"   class="form-control programme programme1 " required>
                              <option value="">Select program</option>
                              <?php
                              foreach ($pro as $value) {
                                ?>
                                <option value="<?php echo $value['id'];?>"><?php echo $value['name'];?></option>  
                                <?php
                              }  
                              ?>
                            </select>            
                        </div>
                      </div>
                      <div class="item form-group col-md-4">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Project<span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                              <select  name="proj"   class="form-control project project1" required>
                              
                              
                            </select>           
                        </div>
                      </div>
                      </div>
                                        
                       
                      
                        
                          
                          <div class="table-responsive" style="scroll-x:overflow">
                        <table class="table table-striped" style="width:100%">
                        <thead>
                        <tr>
                     <td>Head Of Account</td><td>Naration</td><td>Type</td><td>Dr</td><td>Cr</td>
                        </tr>
                        </thead>
                        <tbody id="upper_tbody">
                        <tr class="row_table row1">
                        
                       <td>
                      <select  name="rec[1][level5]" data-id="1"  class="form-control select2_single level5 level51" required>
                              <option value="">Select Head Level 5</option>
                               <?php
                              foreach ($level5 as $value) {
                                ?>
                                <option value="<?php echo $value['id'];?>" data-pid="<?php echo $value['assignid']; ?>" ><?php echo $value['name'];?></option>  
                                <?php
                              }  
                              ?>
                            </select>
                      </td>
                      <td class="col-md-3">
                     
                       
                       <textarea  name="rec[1][naration]" data-id="1"  class="form-control" required placeholder="Enter Narration.........."></textarea>
                           
                        
                      </td>
                      <td>
                        <select name="rec[1][type]" class="cr_dr_select cr_dr_select1 form-control select2_naration" required data-id="1">
                                        <option value="" selected>Select Type</option>
                                        <option value="D">Dr</option>
                                        <option value="C" >Cr</option>
                                    </select>
                      </td>
                      <td>
                                    <input name="rec[1][dr_amount]" class="dr-item dr-item1 form-control" type="text" value="0" disabled="" data-id="1">
                                </td>
                                <td>
                                    <input name="rec[1][cr_amount]" class="cr-item cr-item1 form-control" type="text" value="0" disabled="" data-id="1">
                                </td>
                                 
                      <td>
                      <button class="btn btn-primary" data-remove-id="1" type="button" >
                        <i class="fa fa-remove"></i>
                      </button>
                      </td>
                      </tr>
                      <tr class="row_table row2">
                       
                       <td>
                      <select  data-id="2"  name="rec[2][level5]"  class="form-control select2_single level5 level52" required>
                              <option value="">Select Head Level 5</option>
                               <?php
                              foreach ($level5 as $value) {
                                ?>
                                <option value="<?php echo $value['id'];?>" data-pid="<?php echo $value['assignid']; ?>" ><?php echo $value['name'];?></option>  
                                <?php
                              }  
                              ?>
                            </select>
                      </td>
                      <td>
                              <textarea name="rec[2][naration]" data-id="2" maxlength="200"  class="form-control" required placeholder="Enter Narration.........."></textarea>
                           
                            </td>
                      <td>
                        <select name="rec[2][type]" class="cr_dr_select cr_dr_select2 form-control select2_naration"  required data-id="2">
                                        <option value="" selected>Select Type</option> 
                                        <option value="D">Dr</option>
                                        <option value="C" >Cr</option>
                                    </select>
                      </td>
                       <td>
                                    <input name="rec[2][dr_amount]" class="dr-item dr-item2 form-control" type="text" value="0" disabled="" data-id="2">
                                </td>
                                <td>
                                    <input name="rec[2][cr_amount]" class="cr-item cr-item2 form-control" type="text" value="0" disabled="" data-id="2">
                                </td>
                                <td>
                      <button class="btn btn-primary removeRow" data-remove-id="1" type="button" data-id="2">
                        <i class="fa fa-remove"></i>
                      </button>
                      </td>
                      </tr>
                        </tbody>
                      <tbody><tr>
                                <td colspan="3">Total</td>
                                <td id="EntryDrValue">0</td><input name="total" id="total" type="hidden" />
                                <td id="EntryCrValue">0</td>
                                <td></td>
                                
                                
                            </tr>
                            <tr>
                                <td colspan="3">Difference</td>
                                <td id="EntryDrDifference">0</td>
                                <td id="EntryCrDifference">0</td>
                                <td></td>
                            </tr>
                      </tbody>
                      </table>
                      </div>
                      <div class="ln_solid"></div>
                      
                      <div class="form-group">
                     
                        <div class="col-md-6 col-md-offset-3">
                        <button type="button" class="btn btn-primary addRow pull-right">
                    <i class="fa fa-plus" ></i>
                          <button id="EntrySubmit" type="submit" class="btn btn-success pull-right">Add</button>
                           <input type="hidden" name="type" value="<?php echo $form['type']; ?>">

                        </div>
                        
                          </div>
                          </div>
                    </form>
                      </div>
                        </div>
                     </div>
                </div>
             
              </div>
               
             
              </div>
            
            </div>
          </div>
                  <!-- /page content -->
        <script src="<?php echo base_url(); ?>assets/vendors/jquery/dist/jquery.min.js"></script>
        <script src="<?php echo base_url(); ?>assets/vendors/select2/dist/js/select2.full.min.js"></script>
      <script>
      $(document).ready(function(){
        $('#slip').hide();
      });
       $(".select2_single").select2({
            placeholder: "Select Head Of Account",
            allowClear: true
         });
          $(".select2_naration").select2({
            placeholder: "Select Type",
            allowClear: true
         });
        $(document).on('change','.programme',function(){
          var val=$(this).val();
          
           
          $.ajax({
                    url: "<?php echo base_url(); ?>Load_account/proagainstproject/"+val,
                    data: {},
                      success: function( d ) {
                         var pre='<option value="">Select Project</option>';
                         
                          $( ".project1" ).html(pre+d);
  }
});

        });
       
        $(document).on('change','.bank',function(){
         
          
         var val = $(".bank option:selected" ).attr("data-balance");
         $('#ammount').html("RS:"+val+"/")
});

//         });
//         $(document).on('change','.level2',function(){
//           var id=$(this).val();
//            var val = $(this).attr("data-id");
//           $.ajax({
//                     url: "<?php echo base_url(); ?>Load_account/level3_assign/"+id,
//                     data: {},
//                       success: function( d ) {
//                          var pre='<option value="">Select Head Level 3</option>';
//                           $( ".level3"+val ).html(pre+d);
//   }
// });

//         });
//           $(document).on('change','.level3',function(){
//           var id=$(this).val();
//            var val = $(this).attr("data-id");
//           $.ajax({
//                     url: "<?php echo base_url(); ?>Load_account/level4_assign/"+id,
//                     data: {},
//                       success: function( d ) {
                        
//                          var pre='<option value="">Select Head Level 4</option>';
//                           $( ".level4"+val ).html(pre+d);
//   }
// });

//         });
//          $(document).on('change','.level4',function(){
//           var id=$(this).val();
//            var val = $(this).attr("data-id");
//           $.ajax({
//                     url: "<?php echo base_url(); ?>Load_account/level5_assign/"+id,
//                     data: {},
//                       success: function( d ) {
//                          var pre='<option value="">Select Head Level 5</option>';
//                           $( ".level5"+val ).html(pre+d);
//   }
// });

        //});
         

      
    
        var rows =2;
      var wrapper = $("#upper_tbody");
      var row = '<tr class="row_table row{id}">'+
                
                '<td>'+
                  '<select name="rec[{id}][level5]" required="required" class="form-control select2_single level5 level5{id}" data-id="{id}">'+
                 '<option value="">Select level5</option>'+
                  '<?php  foreach ($level5 as $value) { ?>'+
                   '<option value="<?php echo $value['id'];?>" data-pid="<?php echo $value['assignid']; ?>"><?php echo $value['name'];?></option>'+
                    '<?php  } ?>'+
                  '</select>'+
                '</td>'+
                '<td>'+
               ' <textarea name="rec[{id}][naration]" data-id="{id}" maxlength="200"  class="form-control" required placeholder="Enter Narration.........."></textarea>'+
                           '</td>'+
                '<td>'+
        '<select name="rec[{id}][type]" class="cr_dr_select cr_dr_select{id} form-control select2_naration" required data-id="{id}">'+
            '<option value="" >Select Type</option>'+
            '<option value="D">Dr</option>'+
            '<option value="C">Cr</option>'+
        '</select>'+
    '</td>'+
                '<td>'+
        '<input name="rec[{id}][dr_amount]" class="dr-item dr-item{id} form-control" type="text" disabled="" value="0" data-id="{id}">'+
    '</td>'+
    '<td>'+
        '<input name="rec[{id}][cr_amount]" class="cr-item cr-item{id} form-control" type="text" disabled="" value="0" data-id="{id}">'+
    '</td>'+
    
                '<td>'+
                  '<button class="btn btn-primary removeRow" data-id="{id}">'+
                    '<i class="fa fa-remove" type="button"></i>'+
                  '</button>'+
                '</td>'+
              '</tr>';
      String.prototype.replaceAll = function (find, replace) {
          var str = this;
          return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
      };
              $(document).on("click",".addRow",function(e){
                
          wrapper.append(row.replaceAll("{id}",++rows));
          $(".select2_single").select2({
            placeholder: "Select Head Of Account",
            allowClear: true
         });
          $(".select2_naration").select2({
            placeholder: "Select Type",
            allowClear: true
         });
         Calculate();
         e.preventDefault();
        });
        $(document).on("click",".removeRow",function(e){
          var length = $("#upper_tbody").children().length;
          var id = $(this).attr("data-id");
          if(length!=2){
            
            $(".row"+id).remove();
            Calculate();
            }
          e.preventDefault();
        });
        
  $(document).on("change",".cr_dr_select",function(e){
    var id = $(this).attr("data-id");
    var selected = $(".cr_dr_select"+id).val();
    
    if(selected=="D")
    {
        $(".dr-item"+id).prop("disabled",false);
        $(".dr-item"+id).prop("required",true);
        $(".dr-item"+id).val("");
        $(".cr-item"+id).prop("disabled",true);
        $(".cr-item"+id).val("0");
    }else{
        $(".dr-item"+id).prop("disabled",true);
        $(".dr-item"+id).val("0");
        $(".cr-item"+id).val("");
        $(".cr-item"+id).prop("required",true);
        $(".cr-item"+id).prop("disabled",false);
    }
});
$(document).on('change','.level5',function(){
          var id = $('option:selected', this).attr('data-pid');
           var val = $(this).attr("data-id");
           
          $.ajax({
                    url: "<?php echo base_url(); ?>Load_account/level5againstallhead/"+id,
                    data: {},
                      success: function( d ) {
                         var re=JSON.parse(d);
                        var l4='<option value="'+re.level4_id+'" selected >'+re.level4_name+'</option>';
                        var l3='<option value="'+re.level3_id+'" selected >'+re["level3_name"]+'</option>';
                        var l2='<option value="'+re.level2_id+'" selected >'+re["level2_name"]+'</option>';
                        var main='<option value="'+re.main_id+'" selected >'+re["main_name"]+'</option>';
                        console.log(l4);
                          $('.main'+val).html(main);
                          $('.level2'+val).html(l2);
                          $('.level3'+val).html(l3);
                          $('.level4'+val).html(l4);
  }
});

        });
         $(document).on('change','#bank',function(){
           var id = $('option:selected', this).attr('data-balance');
           if(id!=""){
           $('#balance').val(id);}else{
             $('#balance').val(0);
           }
        });

// $(document).on("change",".cr_dr_select",function(e){
//     var id = $(this).attr("data-id");
//     var val = $(this).val();
//     var selected = $(".cr_dr_select"+id).val();
//     if(selected!="")
//     {
//         if(val=="D")
//         {
//             $(".dr-item"+id).prop("disabled",false);
//             $(".cr-item"+id).prop("disabled",true);
//         }else{
//             $(".dr-item"+id).prop("disabled",true);
//             $(".cr-item"+id).prop("disabled",false);
//         }
//     }
// });

$(document).on("keyup",".dr-item",function(e){
    Calculate();
});

$(document).on("keyup",".cr-item",function(e){
    Calculate();
});

$(document).on("click",".recalculate",function(e){
    Calculate();
});

// Helper Functions

function Calculate()
{
    var DrTotal = DrCalculate();
    var CrTotal = CrCalculate();
    
    $("#EntryDrValue").html(DrTotal);
    $("#total").val(DrTotal);
    $("#EntryCrValue").html(CrTotal);

    NoColor();

    if(DrTotal > CrTotal)
    {
        var val = DrTotal - CrTotal;
        $("#EntryCrDifference").html(val);
        $("#EntryDrDifference").html("");
        $("#EntrySubmit").prop("disabled",true);
        RedColor();
    }else if(CrTotal > DrTotal)
    {
        var val = CrTotal - DrTotal;
        $("#EntryDrDifference").html(val);
        $("#EntryCrDifference").html("");
        $("#EntrySubmit").prop("disabled",true);
        RedColor();
    }else{
        $("#EntryDrDifference").html("-");
        $("#EntryCrDifference").html("");
        $("#EntrySubmit").prop("disabled",false);
        GreenColor();
    }
}

function CheckDifference()
{
    var DrTotal = DrCalculate();
    var CrTotal = CrCalculate();

    if(DrTotal > CrTotal)
    {
        var val = DrTotal - CrTotal;
        return val;
    }else if(CrTotal > DrTotal)
    {
        var val = CrTotal - DrTotal;
        return val;
    }else{
        return 0;
    }
}

function DrCalculate()
{
    var DrTotal = 0;
    $.each( $(".dr-item") , function(i,v){
        var val = $(v).val();
        if(val=="")
            val = 0;
        DrTotal += parseFloat( val );
    });
    return DrTotal;
}

function CrCalculate()
{
    var CrTotal = 0;
    $.each( $(".cr-item") , function(i,v){
        var val = $(v).val();
        if(val=="")
            val = 0;
        CrTotal += parseFloat( val ); 
    });
    return CrTotal;
}

function RedColor()
{
    $("#EntryDrValue").addClass("bg-danger");
    $("#EntryCrValue").addClass("bg-danger");
}

function GreenColor()
{
    $("#EntryDrValue").addClass("bg-success");
    $("#EntryCrValue").addClass("bg-success");
}

function NoColor()
{
    $("#EntryDrValue").removeClass("bg-success").removeClass("bg-danger");
    $("#EntryCrValue").removeClass("bg-success").removeClass("bg-danger");
}
        
        </script>