<script type="text/javascript">
  
   $(document).ready(function() {
    $('select').select2();
    
     jQuery('.datepicker2').datepicker({
			dateFormat : 'yy-mm-dd',
			changeMonth : true,
			changeYear : true
			});
    
            $.each($(".select2-container"), function(i, n) {
            $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
                    $(n).prepend($(n).next());
                    $(n).delay(500).queue(function() {
            $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
                    $(this).dequeue();
            });
            });
    });
            function enable_lumpsum(id) {
            if (document.getElementById(id).disabled == true) {
            document.getElementById(id).disabled = false;
            } else {
            document.getElementById(id).disabled = true;
            }
            return true;
            }
   

    

    function delRow(me){
    var td = me.parentNode;
            var tr = td.parentNode;
            var tBody = tr.parentNode;
            tBody.removeChild(tr);
    }

    function deleteRow(me){
            var i = 0;
            delRow(me);
            $("#listDetails > tbody > tr").each (function(){
                $(this).find("td").eq(0).text(++i);
             });
            $("#trInfosSale").attr("data-currentRow", i - 1);
             $($("#trInfosSale")).find("td").eq(0).text("");
    }

    function msg(type, message){
    $("#msg_div p").html(message);
            $("#msg_div").attr("class", "alert " + type)
    }

   
    




    function savewakaf(){
        if ($("#formRow").validationEngine('validate')){
                $.ajax({
                type: "POST",
                        data : {
                               acctNo : $("#saleAcctNo").val(),
                               from : $("#startdate").val(),
                               to : $("#enddate").val(),
                               wakafPercent: accounting.unformat($("#iPerAll").val())
                        },
                        url: "<?php echo base_url() ?>wakafsetup/saveOneRowWakaf",
                        success: function(data) {
                        if (data > 0){
                        $('<tr><td>' + ($("#trInfosSale").attr("data-currentRow") * 1 + 1) + '</td><td data-acctNo="' + $("#saleAcctNo").val() + '">' + $("#saleAcctNo option:selected").text() + '</td>' +
                                '<td>' + $("#startdate").val() + '</td>' +
                                '<td>' + $("#enddate").val() + '</td>' +
                                '<td class="tar">' + accounting.formatNumber($("#iPerAll").val(), 2) + '</td>' +
                                '<td>' + 
                                '<a data-myid="'+ data +'" data-acct="'+ $("#editAcct").val() +'" onclick="editRow(this)" class="btn btn-mini"><i class="icon-pencil"></i></a>' +
                                 ' <a onclick="deleteWakaf(this)" data-myid="'+ data +'" class="btn btn-mini btn-danger"><i class="icon-trash icon-white"></i></a></td>').insertBefore("#trInfosSale");
                                $("#trInfosSale").attr("data-currentRow", $("#trInfosSale").attr("data-currentRow") * 1 + 1);
                                msg ("alert-success", "Wakaf saved !!!");
                        }
                        }
                });
        }
    }

       
       
       function editRow(me){
        if (!$("#editAcct").length) {
            var tr = $(me).parent().parent();
                   $(tr).replaceWith('<tr><td>' + $(me).parent().parent().find("td").eq(0).html() + '</td>' +
                        '<td><select id="editAcct" data-lineNo="' + $(me).parent().parent().find("td").eq(0).html() + '" data-previewvalue="' + $(me).parent().parent().find("td").eq(1).html() + '" class= "span12 validate[required]"><option></option>' +
                        <?php
                        if ($chargeAccount) {
                            foreach ($chargeAccount as $row) {
                                ?>
                                               '<option   value="<?php echo $row->ID; ?>"><?php
                                echo "[".$row->acctCode . "] ";
                                echo str_replace("'", " ", $row->acctName, $count);
                                ?></option>' +
                                <?php
                            }
                        }
                        ?>
                   '</select></td>' +
                    '<td><input value = "' + $(me).parent().parent().find("td").eq(2).html() + '" data-previewValue="'+ $(me).parent().parent().find("td").eq(2).html() +'" onkeydown= "return false" type="text" id="editFrom" class="span10 datepicker2 validate[required, past[#enddate]]" /></td>' +
                    '<td><input value = "' + $(me).parent().parent().find("td").eq(3).html() + '" data-previewValue="'+ $(me).parent().parent().find("td").eq(3).html() +'"  onkeydown= "return false" type="text" id="editTo" class="span10 datepicker2 validate[required, future[#startdate]]" /></td>' +
                     '<td><input value = "' + $(me).parent().parent().find("td").eq(4).html() + '" data-previewValue="'+ $(me).parent().parent().find("td").eq(4).html() +'"  id="editPercentage" type="text" class="span10 validate[required, future[#startdate]]" /></td>' +
                    '<td>' +
                    '<button onclick="updateRow(this)" data-acct="' + $(me).attr("data-acct") +'" data-myid="' + $(me).attr("data-myid") + '" class="btn btn-mini btn-primary" type="button"><span class="icon-ok-circle"></span></button>' +
                    ' <button  id="btnCancel" data-acct="' + $(me).attr("data-acct") +'" data-myid="' + $(me).attr("data-myid") + '" onclick="cancelEdit(this)" class="btn btn-mini" type="button"><span class="icon-remove-circle"></span></button></td>' +
                    '</tr>');
                        $("#editAcct").val($(me).attr("data-acct"));
                        jQuery('#editFrom').datepicker({
			dateFormat : 'yy-mm-dd',
			changeMonth : true,
			changeYear : true
			});
                        jQuery('#editTo').datepicker({
			dateFormat : 'yy-mm-dd',
			changeMonth : true,
			changeYear : true
			});
                        $('#editAcct').select2();
                         $.each($("#editAcct"), function(i, n) {
                        $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
                                $(n).prepend($(n).next());
                                $(n).delay(500).queue(function() {
                        $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
                                $(this).dequeue();
                        });
                        });
                    }else {
                         msg ("", "A row is been editing ... !!!");
                    }
               }
               function updateRow(me){
                var tr = $(me).parent().parent();
                  
                 $.ajax({
                    type: "POST",
                    data : {
                        rowId : $(me).attr("data-myid"),
                        acct : $("#editAcct").val(),
                        from :  $("#editFrom").val(),
                        to :  $("#editTo").val(),
                        percent:  accounting.unformat($("#editPercentage").val()),
                                
                    },
                    url: "<?php echo base_url() ?>wakafsetup/updatewakafAcrual",
                    success: function(data) {
                    if (data > 0){
                            msg ("alert-success", "Wakaf updated !!!");
                             $(tr).replaceWith('<tr><td>' + $("#editAcct").attr("data-lineNo") + '</td>' +
                                '<td>' + $("#editAcct option:selected").text()  + '</td>' +
                                '<td>' + $("#editFrom").val() + '</td>' +
                                '<td>' + $("#editTo").val() + '</td>' +
                                '<td class="tar">' + accounting.formatNumber($("#editPercentage").val(),2) + '</td>' +
                                '<td>' + 
                                '<a data-myid="'+ $(me).attr("data-myid") +'" data-acct="'+ $("#editAcct").val() +'" onclick="editRow(this)" class="btn btn-mini"><i class="icon-pencil"></i></a>' +
                                 ' <a onclick="deleteWakaf(this)" data-myid="'+ $(me).attr("data-myid")+'" class="btn btn-mini btn-danger"><i class="icon-trash icon-white"></i></a></td>' );
               
                    }
                    }
                 });
               }
               
               function cancelEdit(me){
                   
                    var tr = $(me).parent().parent();
                    $(tr).replaceWith('<tr><td>' + $("#editAcct").attr("data-lineNo") + '</td>' +
                            '<td>' + $("#editAcct").attr("data-previewvalue") + '</td>' +
                            '<td>' + $("#editFrom").attr("data-previewvalue") + '</td>' +
                            '<td>' + $("#editTo").attr("data-previewvalue") + '</td>' +
                            '<td class="tar">' + $("#editPercentage").attr("data-previewvalue") + '</td>' +
                            '<td>' + 
                                '<a data-myid="'+ $(me).attr("data-myid") +'" data-acct="'+ $(me).attr("data-myid") +'" onclick="editRow(this)" class="btn btn-mini"><i class="icon-pencil"></i></a>' +
                                 ' <a data-myid="'+ $(me).attr("data-myid") +'" onclick="deleteWakaf(this)" class="btn btn-mini btn-danger"><i class="icon-trash icon-white"></i></a></td>' );
               
                 
               }
               
               function deleteWakaf(me){
                   $('#popupModal').modal('show');
            $("#_popupTitle").html("Confirm delete");
            $("#_popupContent").html("Your about to delete definively this row. <br/> Click on ok to confirm");
            $('#_popupFooter').empty();
            $('#_popupFooter').append('<button class="btn btn-danger" id="detButton">Ok</button>' +
            '<button class="btn" id="canb">Cancel</button>');
            $("#_popupTitle").html("Confirm delete");
            $("#canb").click(function() {
    $('#popupModal').modal('hide');
    });
            $("#detButton").click(function() {
    $('#popupModal').modal('hide');
            $.ajax({
            type: "POST",
                    data: {
                    rowId :$(me).attr("data-myid"),
                    },
                    url: "<?php echo base_url() ?>wakafsetup/deleteWakafAcrual",
                    success: function(data) {
                        if(data>0){
                        msg ("alert-success", "Wakaf deleted !!!");
                        deleteRow(me);
                    }
                    } });
    });
               }

        function getListByDate(){
                window.location ="<?php echo base_url() ?>wakafsetup/wakaf_acrual_list/" + $("#sstartdate").val() + "/" + $("#senddate").val() ; 
        }

</script>

<div id="content">                        
    <div class="wrap">

        <div class="head">
            <div class="info">
                <h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
                    <?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>
                <ul class="breadcrumb">
                    <li><a href="<?php echo base_url() ?>home"><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                    <li><a href="zakat/home"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                    <li class="active"><?php echo $this->lang->line('title') ?></li>
                </ul>
            </div>

            <div class="search">
                <form action="<?php echo base_url() ?>admin/search" method="post">
                    <input name="search_text" type="text" placeholder="search..."/>                                
                    <button type="submit"><span class="i-magnifier"></span></button>
                </form>
            </div>                        
        </div>  <!-- Head -->


        <div class="content">
            <div class="row-fluid">
                <div class="span12">
                    <div class="block">
                        <div class="head">
                            <h2><?php echo $this->lang->line('w') ?>Wakaf Pre Setup</h2>
                            <div class="side fr">
                                <button class="btn" onClick="clear_form('#validate');" type="button">Clear form</button>
                            </div>
                        </div>
                       
                    </div>  <!-- content -->
                </div>  <!-- block -->
            </div>  <!-- span12 -->
        
 <div class="row-fluid scRow">  
            
            <div class="span12 scCol">
                <div class="block" id="grid_block_4">
                    <div class="content">
                        <div class="controls-row side fl">
                                <div class="form-inline">
   
                           
                            
                        
                        <div class="input-prepend span4"><span class="add-on span4">From</span>
                                                <?php
                                                $data = array(
                                                    'name' => 'sstartdate',
                                                    'id' => 'sstartdate',
                                                    'value' =>'',
                                                    'type' => 'text',
                                                    'onkeydown' => 'return false',
                                                    'class' => 'span8 datepicker2 validate[required, future[#startdate]]',);
                                                echo form_input($data, date(element('dtFormat', $this->session->userdata('logged_in'))));
                                                ?>
                                            </div>
                            <div class="input-prepend span4"><span class="add-on span4">To</span>
                                                <?php
                                                $data = array(
                                                    'name' => 'senddate',
                                                    'id' => 'senddate',
                                                    'value' =>'',
                                                    'type' => 'text',
                                                    'onkeydown' => 'return false',
                                                    'class' => 'span8 datepicker2 validate[required, future[#startdate]]',);
                                                echo form_input($data, date(element('dtFormat', $this->session->userdata('logged_in'))));
                                                ?>
                                            </div>
                                    
                                    <a href="#" class="btn btn-primary" style="margin-left: 10px;" onclick="getListByDate()" id="btnSubmit" >Get List</a>
                        </div>
                             </div>
                    </div>
                </div>
            </div>

        <div class="row-fluid scRow">                            
            <div class="span12 scCol">
                <div class="block" id="grid_block_4">
                    <div class="head">
                        <h2><?php echo $this->lang->line('') ?>
                                </h2>
                        <ul class="fr buttons">
                            <li><a class="block_toggle collapsed" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
                                    <span class="i-arrow-down-3"></span></a></li>
                        </ul>                                  
                    </div><!-- head -->
                    <div class="content np">
                        <form id="formRow" name="formRow" >
                        <table id="listDetails" cellpadding="0" cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <th class="span1">#</th>
                                    <th class="span3"><?php echo $this->lang->line('saleaccountno') ?></th>
                                    <th class="span2"><?php echo $this->lang->line('from') ?></th>
                                    <th class="span2"><?php echo $this->lang->line('to') ?></th>
                                    <th class="tar span2"><?php echo $this->lang->line('percentage') ?></th>
                                    <th class="span2"><?php echo $this->lang->line('action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php 
                             $i=0;
                             if($listWkaf){
                                
                                 foreach ($listWkaf as $row) {
                            
                            ?>
                                <tr>
                                    <td><?php echo ++$i; ?></td>
                                    <td><?php echo '['.$row->acctCode.'] '.$row->acctName; ?></td>
                                    <td><?php echo $row->from; ?></td>
                                     <td><?php echo $row->to; ?></td>
                                     <td class="tar"><?php echo number_format($row->wakafPercent,2); ?></td>
                                     <td>
                                         <a data-myid="<?php echo $row->ID; ?>" data-acct="<?php echo $row->salesAccountID; ?>" onclick="editRow(this)" class="btn btn-mini"><i class="icon-pencil"></i></a>
                                         <a data-myid="<?php echo $row->ID; ?>" onclick="deleteWakaf(this)" class="btn btn-mini btn-danger"><i class="icon-trash icon-white"></i></a>
                                     </td>
                                </tr>
                                
                               <?php
                             }
                             }
                               ?>
                                <tr data-currentRow="<?php echo $i; ?>" id="trInfosSale">
                                    <td></td>
                                    <td>
                                        <?php
                                        $preselcurr = '0';
                                        $options = '';
                                        $options[NULL] = '--Please Select--';
                                        foreach ($chargeAccount as $row) {
                                            $options[$row->ID] = '[' . $row->acctCode . '] ' . $row->acctName;
                                        }
                                        $js = ' id="saleAcctNo"  class = "span12 validate[required]"';
                                        echo form_dropdown('chargeAccount', $options, $preselcurr, $js);
                                        ?>    
                                    </td>
                                    <td>
                                        <div class="input-prepend span12"><span class="add-on span3"><i class="i-calendar"></i></span>
                                                <?php
                                                $data = array(
                                                    'name' => 'startdate',
                                                    'id' => 'startdate',
                                                    'value' =>'',
                                                    'type' => 'text',
                                                    'onkeydown' => 'return false',
                                                    'class' => 'span9 datepicker2 validate[required, past[#enddate]]',);
                                                echo form_input($data, date(element('dtFormat', $this->session->userdata('logged_in'))));
                                                ?>
                                            </div>
                                    </td>
                                    <td>
                                        <div class="input-prepend span12"><span class="add-on span3"><i class="i-calendar"></i></span>
                                                <?php
                                                $data = array(
                                                    'name' => 'enddate',
                                                    'id' => 'enddate',
                                                    'value' =>'',
                                                    'type' => 'text',
                                                    'onkeydown' => 'return false',
                                                    'class' => 'span9 datepicker2 validate[required, future[#startdate]]',);
                                                echo form_input($data, date(element('dtFormat', $this->session->userdata('logged_in'))));
                                                ?>
                                            </div>
                                    </td>
                                    <td>
                                        <div class="input-prepend input-append">
                                            <input class="span8 tar validate[required, max[100]]" type="text" id="iPerAll" />
                                            <span class="add-on">&#37;</span>
                                        </div>
                                    </td>
                                    <td>
                                                <a id="btnInserRow" onclick="savewakaf()" class="btn btn-primary"><i class="icon-ok-sign icon-white"></i></a>
                                     <?php
                            $data = array(
                                'name' => 'validate',
                                'style' => 'margin-right:5px;',
                                'class' => 'btn',
                                'onClick' => "$('#validate').validationEngine('hide');");
                            echo form_button($data, 'Hide');
                            ?>
                                    </td>
                                
                                </tr>
                            
                            </tbody>
                        </table> 
                        </form>
                    </div><!-- content -->
                </div><!-- grid block 1-->
            </div><!-- scCol -->
        </div><!-- row-fluid scRow-->


        <div class="row-fluid scRow">  
            <div class="span12 scCol">
                <div class="block" id="grid_block_4">
                    <div class="content">
                        <div style="padding:0;" class="controls-row">
                            <div id="msg_div" style="min-height: 22px;" class="alert alert-info"> <p></p> </div>
                        </div>
                    </div>
                </div>
            </div>

        </div><!-- row-fluid scRow-->
        </div>  <!-- row-fluid -->
    </div>  <!-- content -->
</div>  <!-- wrap -->
</div>  <!-- content -->
