<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(){
        var trInfos = $("#trInfosSale").clone();
        
             wDetails = '{"rs":[';
            firstime = true;
            delRow (document.getElementById("btnInserRow"));
            $("#listDetails > tbody > tr").each (function(){
            var tr = $(this);
            var acctNo = $(tr).find("td").eq(1).attr("data-acctNo");
            var from = $(tr).find("td").eq(2).text();
            var to = $(tr).find("td").eq(3).text();
            var p = accounting.unformat($(tr).find("td").eq(4).text());
            str = '';
            if (firstime){
                firstime = false;
                str = '';
            } else {
                firstime = false;
             str = ',';
            }
            wDetails += str + '{"salesAccountID":"' + acctNo + '"' +
            ',"from":"' + from + '"' +
            ',"to":"' + to + '"' +
            ',"wakafPercent":"' + p + '"}';
            });
            wDetails += ']}';
            
            master = "wDetail=" + wDetails;
            $.ajax({
            type: "POST",
                    data : master,
                    url: "<?php echo base_url() ?>wakafsetup/savewakafAcrual",
                    success: function(data) {
                    if (data > 0){
                    $("#btnSubmit").text("Go to Wakaf list");
                            $("#btnSubmit").attr("onclick", "");
                            $("#btnSubmit").attr("href", "<?php echo base_url() ?>wakafsetup/wakaf_acrual_list");
                            msg ("alert-success", "Wakaf saved !!!");
                    }
                    }
            });
            
        $("#listDetails > tbody").append(trInfos);
    }

    function insertRow(){
       if($("#formRow").validationEngine('validate') && finanEngine('financhk')){
        $('<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 onclick="deleteRow(this)" class="btn-icon btn-mini btn-danger-form"><span class="i-cross"></span></a></td>'
            ).insertBefore("#trInfosSale");
           $("#trInfosSale").attr("data-currentRow",$("#trInfosSale").attr("data-currentRow")*1+1);
       }
    }

</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-form btn-danger-form" 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="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="span4"><?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="span1"><?php echo $this->lang->line('action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            
                                <tr data-currentRow="0" id="trInfosSale">
                                    <td></td>
                                    <td>
                                        <?php
                                        $preselcurr = '0';
                                        $options = '';
                                        $options[NULL] = '--Please Select--';
                                        foreach ($chargeAccount as $row) {
                                            $options[$row->ID] =  substr($row->acctCode,0,4).'-'.substr($row->acctCode,4,3).'-'.substr($row->acctCode,7) . ' | ' . $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 financhk validate[required, past[#enddate]]',);
                                                echo form_input($data, date("d-m-Y"));
                                                ?>
                                            </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 financhk validate[required, future[#startdate]]',);
                                                echo form_input($data, date("d-m-Y"));
                                                ?>
                                            </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="insertRow()" class="btn-icon btn-mini btn-success-form"><i class="i-checkmark-3"></i></a></td>
                                </tr>
                            
                            </tbody>
                        </table> 
                        </form>
                    </div><!-- content -->
                </div><!-- grid block 1-->
            </div><!-- scCol -->
        </div><!-- row-fluid scRow-->


        <div class="row-fluid scRow">  
            <div class="span8 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 class="span4 scCol">
                <div class="block" id="grid_block_4">
                    <div class="content">
                        <div class="controls-row side fr">
                            <?php
                            $data = array(
                                'name' => 'validate',
                                'style' => 'margin-right:5px;',
                                'class' => 'btn-form btn-warning-form',
                                'onClick' => "$('#validate').validationEngine('hide');");
                            echo form_button($data, 'Hide prompts');
                            /* $data = array(
                              'name' => 'mysubmit',

                              'class' => 'btn-form btn-success-form');
                              $js = 'onclick="savewakaf()"';
                              echo form_submit($data, 'Submit', $js);
                             * 
                             */
                            ?>
                            <a href="#" class="btn-form btn-success-form" onclick="savewakaf()" id="btnSubmit" >Submit</a>
                        </div>
                    </div>
                </div>
            </div>

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