<script type="text/javascript">
$(document).ready(function() {
    /* Build the DataTable with third column using our custom sort functions */
   $('select').select2();
    /*$("table.editable").on("click",".edit",function(){
        rRow = $(this).parents("tr");
        ID = $('td:first', $(this).parents('tr')).text(); 
		var selyear = <?php echo $selyear;?>;
        $("#row_edit").dialog("open");
    });
    $("#row_edit").dialog({
        autoOpen: false,
        resizable: false,        
        modal: true,
        buttons: {
            "Continue...": function() {
				window.location.href = "<?php echo base_url(); ?>glreports/accountDetailsfc/" + ID + "/<?php echo $selyear;?>";		
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });*/  

	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));
    	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 addper = $('#add_period option:selected').val();
	window.open("<?php echo base_url() ?>glreports/profitNloss_print/"+addyear+"/"+addper);
}

function do_print_Yearly(){
	var year = $('.last_year').select2('val');
	window.open("<?php echo base_url();?>glreports/profit_n_loss_last_year_print/"+year);
}

function do_print_Monthly(){
	var year_monthly = $('.year_monthly').select2('val');
	var start_period = $('.start_period').select2('val');
	var end_period = $('.end_period').select2('val');
	window.open("<?php echo base_url();?>glreports/profit_n_loss_monthly_print/"+year_monthly+"/"+start_period+"/"+end_period);
}

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 addper = $('#add_period option:selected').val();
		var urls = "<?php echo base_url();?>glreports/profitNloss/"+addyear+"/"+addper;
		window.location = urls;
	}else if($index == 1){
		var year = $('.last_year').select2('val');
		var urls = "<?php echo base_url();?>glreports/profit_n_loss_last_year/"+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/profit_n_loss_monthly/"+year_monthly+"/"+start_period+"/"+end_period;
		window.location = urls;
	}
}

function selectYearPeriod(obj){
		var year = obj.value;
//		alert(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 = [];
						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("#from_period").empty();
						jQuery("#add_period,#start_period,#end_period").append(items.join('</br>'));
						//jQuery("#from_period").append(items.join('</br>'));
						//$("#add_period").select2('val',null);
						//$("#from_period").select2('val',null);
						$("#add_period,#start_period,#end_period").select2();
					} else {
						jQuery("#add_period,#start_period,#end_period").empty();
						//jQuery("#from_period").empty();
						$("#add_period,#start_period,#end_period").select2('val',null);
						//$("#from_period").select2('val',null);
						$("#error").html('<p>'+content.message+'</p>');
					} //end if
				} //end success
				
			}); //end ajax
//	window.location.reload(true); 
}

</script>
<div class="head">
	<h2><?php echo $this->lang->line('title3') ?></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 tab3 <?php echo ($activeTab == 3)?'active':''; ?>" href="javascript:">
        		<?php echo $this->lang->line('monthly') ?>
        	</a>
        </li>
    </ul>
</div><!-- head -->
<!-- Standard tab -->
<div class="content np tabCont">
	<div class="content nb"><!-- Row 4-->
    	<div class="span2 tar"><?php echo form_label($this->lang->line('year'),'add_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'];
				}
			}
			$js='onchange="selectYearPeriod(this)"class="input-medium" id="add_year"';

			echo form_dropdown('add_year', $options, $preselyear, $js);
		?>       
        </div>
    	<div class="span2 tar"><?php echo form_label($this->lang->line('to'),'add_period');?></div>
		<div class="span4">
		<?php
			$preselperiod = $selper;
			$options = '';
			$options[] = $this->lang->line('period');
			if($period){
				foreach($period as $row){
					$options[$row->period] = $row->financialPeriodAsat;
				}
			}
			$js='class="input-medium add_period" 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']+1)."-".$row['financialYear'];
				}
			}
			$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('add_year', $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($startPer)?$startPer:$selper;
			$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($endPer)?$endPer:$selper;
			$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 -->    