<script type="text/javascript">
    $(document).ready(function() {
        /* Build the DataTable with third column using our custom sort functions */
        $('select').select2();
        

        var $prevIndex = <?php echo $activeTab; ?>-1;
        $('.tabCont').not($('.tabCont:nth('+$prevIndex+')')).css('display','none').removeClass('active');  
        $('.tab').on('click',function(){
            var $index = $('.tab').index($(this));
            console.log($('.tabCont:nth('+$index+')').css('display'));
            if($('.tabCont:nth('+$index+')').css('display') == "none"){
                $('.tab:nth('+$index+')').addClass('active');
                $('.tab:nth('+$prevIndex+')').removeClass('active');
                $('.tabCont:nth('+$prevIndex+')').slideUp('fast',function(){
                    $('.tabCont:nth('+$index+')').slideDown('fast');
                    $prevIndex = $index;
                });
            }  
        });

    });
    function do_print() {
        var addyear = $('#add_year option:selected').val();
        var add_period = $('#add_period option:selected').val();
        window.open("<?php echo base_url() ?>glreports/financialPosition_print/" + addyear + "/" + add_period);
    }

    function selectYearPeriod(obj) {
        var year = obj.value;
        jQuery("#add_period").empty();
        $('#add_year,.last_year,.year_monthly').select2('val',year);
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url() ?>glreports/get_TrialBalance_year/" + year,
            data: year,
            async: false,
            success: function(content) {
                if (content.status == "success") {
                    var items = [];
                    //items.push('<option value=""><?php echo $this->lang->line('pleaseselect') ?></option>');
                    for (var i = 0; i < content.message.length; i++) {
                        items.push('<option value="' + content.message[i].period + '">'
                                + content.message[i].financialPeriodAsat + '</option>');
                    }  //end for
                    jQuery("#add_period,#start_period,#end_period").empty();
                    jQuery("#add_period,#start_period,#end_period").append(items.join('</br>'));
                    $("#add_period,#start_period,#end_period").select2();
                } else {
                    $("#error").html('<p>' + content.message + '</p>');
                    $("#add_period,#start_period,#end_period").select2('val', null);
                } //end if

            } //end success

        }); //end ajax
//  window.location.reload(true); 
    }

    function changeList(){
        var $index;
        $('.tabGroup li').each(function(){
            if($('a',this).hasClass('active')){
                $index = $('.tabGroup li').index($(this));
            }       
        });

        if($index == 0){
            var addyear = $('#add_year option:selected').val();
            var add_period = $('#add_period option:selected').val();
            if (add_period == null) {
                var urls = "<?php echo base_url(); ?>glreports/financialposition/" + addyear + "/" + 0;
                window.location = urls;
            }
            else {
                var urls = "<?php echo base_url(); ?>glreports/financialposition/" + addyear + "/" + add_period;
                window.location = urls;
            }
        }else if($index == 1){
            var year = $('.last_year').select2('val');
            var urls = "<?php echo base_url();?>glreports/financialPositionYearly/"+year;
            window.location = urls;
        }else if($index == 2){
            var year_monthly = $('.year_monthly').select2('val');
            var start_period = $('.start_period').select2('val');
            var end_period = $('.end_period').select2('val');
            var urls = "<?php echo base_url();?>glreports/financialposition_monthly/"+year_monthly+"/"+start_period+"/"+end_period;
            window.location = urls;
        }
    }

</script>

<div class="head">
    <h2><?php echo $this->lang->line('statFinPos') ?></h2>
    <ul class="buttons tabGroup">
        <li>
            <a class="tab tab1 <?php echo ($activeTab == 1)?'active':''; ?>" href="javascript:">
                <?php echo $this->lang->line('standard') ?>
            </a>
        </li>
        <li>
            <a class="tab tab2 <?php echo ($activeTab == 2)?'active':''; ?>" href="javascript:">
                <?php echo $this->lang->line('lastYear') ?>
            </a>
        </li>
        <li>
            <a class="tab tab2 <?php echo ($activeTab == 3)?'active':''; ?>" href="javascript:">
                <?php echo $this->lang->line('monthly') ?>
            </a>
        </li>
    </ul>
</div><!-- head -->
<div class="content np tabCont">    
    <div class="controls-row"><!-- Row 4-->
        <div class="span5 tac">
            <div class="span2 tar"><?php echo form_label($this->lang->line('year'), 'add_year'); ?></div>
            <?php
            $preselyear = $selyear;
            $options = '';
            $options[] = $this->lang->line('selectyear');
            if (!empty($yrs)) {
                foreach ($yrs as $row) {
                    $options[$row['financialYear']] = $row['financialYear'];
                }
            }
            $js = 'onchange="selectYearPeriod(this)" class="input-medium" id="add_year"';
            echo form_dropdown('add_year', $options, $preselyear, $js);
            ?>       
        </div>
        <div class="span5 tac">
            <div class="span2 tar"><?php echo form_label($this->lang->line('period'), 'add_period'); ?></div>
            <?php
            $preselperiod = $selperiod;
            $options = '';
            $options[] = $this->lang->line('period');
            if (!empty($period)) {
                foreach ($period as $row) {
                    $options[$row->period] = $row->financialPeriodAsat;
                }
            }
            $js = 'class="input-medium" id="add_period" style="width:75%"';

            echo form_dropdown('add_period', $options, $preselperiod, $js);
            ?>       
        </div>
    </div><!-- End Row 4-->
</div><!-- content -->
<!-- Last year tab -->
<div class="content np tabCont">
    <div class="content nb"><!-- Row 4-->
        <div class="span2 tar"><?php echo form_label($this->lang->line('year'),'last_year');?></div>
        <div class="span4">
        <?php
            $preselyear = $selyear;
            $options = '';
            $options[] = $this->lang->line('selectyear');
            if(!empty($yrs)){
                foreach($yrs as $row){
                    $options[$row['financialYear']] = $row['financialYear']."-".($row['financialYear']-1);
                }
            }
            $js='onchange="selectYearPeriod(this)"class="input-medium last_year" id="last_year"';

            echo form_dropdown('last_year', $options, $preselyear, $js);
        ?>       
        </div>
    </div><!-- End Row 4-->
</div><!-- content -->
<!-- Monthly tab -->
<div class="content np tabCont">
    <div class="content nb"><!-- Row 4-->
        <div class="span2 tar"><?php echo form_label($this->lang->line('year'),'year_monthly');?></div>
        <div class="span4">
        <?php
            $preselyear = $selyear;
            $options = '';
            $options[] = $this->lang->line('selectyear');
            if(!empty($yrs)){
                foreach($yrs as $row){
                    $options[$row['financialYear']] = $row['financialYear'];
                }
            }
            $js='onchange="selectYearPeriod(this)"class="input-medium year_monthly" id="year_monthly"';

            echo form_dropdown('year_monthly', $options, $preselyear, $js);
        ?>       
        </div>
    </div><!-- End Row 4-->
    <div class="content nb">
        <div class="span2 tar"><?php echo form_label($this->lang->line('from'),'start_period');?></div>
        <div class="span4">
        <?php
            $preselperiod = isset($start)?$start:$selperiod;
            $options = '';
            $options[] = $this->lang->line('period');
            if($period){
                foreach($period as $row){
                    $options[$row->period] = $row->financialPeriodAsat;
                }
            }
            $js='class="input-medium start_period" id="start_period" style="width:75%"';

            echo form_dropdown('start_period', $options, $preselperiod, $js);
        ?>       
        </div>
        <div class="span2 tar"><?php echo form_label($this->lang->line('to'),'end_period');?></div>
        <div class="span4">
        <?php
            $preselperiod = isset($end)?$end:$selperiod;
            $options = '';
            $options[] = $this->lang->line('period');
            if($period){
                foreach($period as $row){
                    $options[$row->period] = $row->financialPeriodAsat;
                }
            }
            $js='class="input-medium end_period" id="end_period" style="width:75%"';

            echo form_dropdown('end_period', $options, $preselperiod, $js);
        ?>       
        </div>
    </div>
</div><!-- content -->  