<SCRIPT language=Javascript>
       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
    </SCRIPT>
	
	<script>
	var oTable;
	$(document).ready(function() {
    /* Build the DataTable with third column using our custom sort functions */
    oTable = $("#companyFinancialCalendar").dataTable( {
        "iDisplayLength": 25,
		 "aLengthMenu": [5,10,25,50,100],
		 "asSorting": [ "asc"], "aTargets": [ 1 ],
		 "sPaginationType": "full_numbers", "aoColumns": [ 
		 {"bSortable": false }, 
			null, 
			null, 
			null, 
			null,
			null, 
			null, 
		{"bSortable": false } ]});
     oTable.fnSort( [ [1,'asc'] ] );
    $("table.editable").on("click",".remove",function(){
        rRow = $(this).parents("tr");
        ID = $('td:first', $(this).parents('tr')).text(); 
        $("#row_delete").dialog("open");
    });
    $("table.editable").on("click",".edit",function(){
        rRow = $(this).parents("tr");
        ID = $('td:first', $(this).parents('tr')).text(); 
        $("#row_edit").dialog("open");
    });
    $("#row_edit").dialog({
        autoOpen: false,
        resizable: false,        
        modal: true,
        buttons: {
            "Continue...": function() {
				window.location.href = "calendar?id=" + ID;		
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });    
    function remove_row(row){
        row.remove();
 		$.ajax({
  			type: "POST",
  			url: "companyFinancialCalendar",
			data:"ID="+ID,
  			success: function(){
				alert('Row has been deleted...');
			},
		});       // Here your ajax action after delete confirmed
    }
    $("#row_delete").dialog({
        autoOpen: false,
        resizable: false,        
        modal: true,

        buttons: {
            "Delete": function() {
				window.location.href = "financialCalendarDelete?ID=" + ID;		
                remove_row(rRow);
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });
	
	$('#journalTable').hide();
	$('select').select2();
	jQuery('.datepicker2').datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
			});
	
    
    /*$("table.editable").on("click",".edit",function(){
        var eRow   = $(this).parents('tr');
        var eState = $(this).attr('data-state');
        
        if(eState == null){
            $(this).html('Save');
            eState = 1;
        }
        
        
        
        if(eState == 1) 
            $(this).attr('data-state','2');
        else{
            $(this).removeAttr('data-state');
            $(this).html('Edit');
        }
    });
//	$("#startPeriod").val(12);*/	
});

function yearperiodCheck(){
	if($('#financialYear').val() == ''){ 
		alert('<?php echo $this->lang->line('year') ?>');
		$('#financialYear').focus();
	}
	else if ($('#period').val() == ''){
		alert('<?php echo $this->lang->line('period') ?>');
		$('#period').focus();
	}
	else{
		var financialYear = $('#financialYear').val();
		var period = $('#period').val();
		//$('#hidePrompt').hide();
		//$('#journalTable').show();
		var postData = {
			'financialYear' : financialYear,
			'period' : period,
		};
		//alert("<?php echo base_url()?>financialCalendarCheck_yearperiod");
 		$.ajax({
  			type: "POST",
  			dataType: "json",
			url: "<?php echo base_url()?>glsetting/financialCalendarCheck_yearperiod",
			data: postData ,
  			success: function(content){
				if(content.status == "success") {
					$('#startDate').removeAttr('disabled');
					$('#endDate').removeAttr('disabled');
					$('#startDate').focus();
				}else if(content.status == "false"){
					alert('Data exists...');
					
					$('#startDate').removeAttr('disabled');
					$('#endDate').removeAttr('disabled');
					$('#startDate').val(content.startDate);
					$('#endDate').val(content.endDate);
					$('#fcID').val(content.ID);
					$('#startDate').focus();
					
				}
			}
		});        // Here your ajax action after delete confirmed
//		location.reload();
	}
}
function dateSearch(){
	var financialYear = $("#financialYear").val();
	var period = $("#period").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var startPeriod = $("#startPeriod").val();
	
	//alert(endDate);
	
		var postData = {
			'financialYear' : financialYear,
			'period' : period,
			'startDate' : startDate,
			'endDate' : endDate,
			'startPeriod' : startPeriod,
		};
 		$.ajax({
  			type: "POST",
  			dataType: "json",
			url: "<?php echo base_url()?>glsetting/dateSearch",
			data: postData ,
  			success: function(content){
				if(content.status == "success") {
					$('#financialYear').val(content.financialYear);
					$('#period').val(content.period);
					$('#startDate').val(content.startDate);
					$('#endDate').val(content.endDate);
				}
			}
		});        // Here your ajax action after delete confirmed
}

function checkYear(obj){
	/*var financialYear = $("#financialYear").val();
	var period = $("#period").val();*/
	var financialYear = obj.value;
	var period = $("#period").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	
	var startPeriod = $("#startPeriod").val();
	
	//alert(startPeriod);
	
		var postData = {
			'financialYear' : financialYear,
			'period' : period,
			'startDate' : startDate,
			'endDate' : endDate,
		};
		
 		$.ajax({
  			type: "POST",
  			dataType: "json",
					//alert("alert biasa");
			url: "<?php echo base_url()?>glsetting/checkYear",
			data: postData ,
  			success: function(content){
				oTable.fnClearTable();
				
				if(content.status == "success") 
				{
//					$('#companyFinancialCalendar').dataTable().fnClearTable();
					//$("#submit").prop("readonly", true);
					$("#period").prop("readonly", false);
					
					$("#startDate").prop("readonly", true);
					$("#startDate").datepicker( "destroy" );
					
					$("#startPeriod").prop("readonly", true);
					$('#companyFinancialCalendar thead').empty();
					$('#companyFinancialCalendar tbody').empty();
					
					
					$("#companyFinancialCalendar thead").append('<tr><th style="display:none"></th><th class="tac">' + 
						"#" + '</th><th class="tac">' +  
						"<?php echo $this->lang->line('year');?>" + '</th><th class="tac">' + 
						"<?php echo $this->lang->line('period');?>" + '</th><th class="tac">' + 
						"<?php echo $this->lang->line('startDate');?>" + '</th><th class="tac">' + 
						"<?php echo $this->lang->line('endDate');?>" + '</th><th class="tac">' +
						"<?php echo $this->lang->line('monthendclosing');?>" + '</th><th class="tac">' +
						"<?php echo $this->lang->line('action');?>" + '</th></tr>');
						
					for (var i=0; i<content.message.length; i++){
						var status ='';
						var ID = content.message[i].ID;
						if (content.message[i].status==1){
							status = "Close";
						}
						else if (content.message[i].status==0){
							status = "Open";
						}
						
						var rowIndex = oTable.fnAddData([
											content.message[i].ID,
											i+1,
											content.message[i].financialYear,
											content.message[i].period,
											(content.message[i].startDate).substr(8, 2) + "-" + (content.message[i].startDate).substr(5, 2) + "-" + (content.message[i].startDate).substr(0, 4),
											(content.message[i].endDate).substr(8, 2) + "-" + (content.message[i].endDate).substr(5, 2) + "-" + (content.message[i].endDate).substr(0, 4),
											'<a title="<?php echo $this->lang->line('edit');?>" class="btn btn-mini btn-link edit">' + status  + '</a>',
											'<?php $js = 'class="btn-icon btn-mini btn-danger-form remove" title="'.$this->lang->line('delete').'"'; echo form_button('remove','<span class="i-trashcan">',$js); ?>'						
										]);
						var row = oTable.fnGetNodes(rowIndex);
						$(row).find('td:nth-child(1)').css('display','none');
						$(row).find('td:nth-child(2)').addClass('tac');
						$(row).find('td:nth-child(3)').addClass('tac');
						$(row).find('td:nth-child(4)').addClass('tac');
						$(row).find('td:nth-child(5)').addClass('tac');
						$(row).find('td:nth-child(6)').addClass('tac');
						$(row).find('td:nth-child(7)').addClass('tac');
						$(row).find('td:nth-child(8)').addClass('tac');
						<?php /*$("#companyFinancialCalendar tbody").append('<tr><td style="display:none">' + 
						content.message[i].ID + '</td><td class="tac">' + 
						(i+1) + '</td><td class="tac"><a title="<?php echo $this->lang->line('edit');?>" class="btn btn-mini btn-link edit">' + 
						content.message[i].financialYear + '</a></td><td class="tac">' + 
						content.message[i].period + '</td><td class="tac">' + 
						(content.message[i].startDate).substr(8, 2) + 
							"-" + (content.message[i].startDate).substr(5, 2) +
							"-" + (content.message[i].startDate).substr(0, 4) + '</td><td class="tac">' + 
						(content.message[i].endDate).substr(8, 2) + 
							"-" + (content.message[i].endDate).substr(5, 2) +
							"-" + (content.message[i].endDate).substr(0, 4) + '</td><td class="tac">' + 
						status + '</td><td class="tac"><?php
							$js = 'class="btn-icon btn-mini btn-danger-form remove" title="'.$this->lang->line('delete').'"';
							echo form_button('remove','<span class="i-trashcan">',$js);
						?> </td></tr>');*/ ?>
        			}
					$("#ID").val(financialYear);
				} 
				else 
				{
					$("#submit").prop("readonly", false);
					$("#period").prop("readonly", false);
					$("#startDate").prop("readonly", false);
					$("#startPeriod").prop("readonly", false);
					$('#companyFinancialCalendar thead').empty();
					$('#companyFinancialCalendar tbody').empty();
					
					
					$("#startDate").addClass("datepicker2");
					jQuery('#startDate').datepicker({
					dateFormat : 'dd-mm-yy',
					changeMonth : true,
					changeYear : true
					});
					
					
				}
					$("#startDate").val(content.startDate);
					$("#endDate").val(content.endDate);
					$("#startPeriod").val(content.startPeriod);
			}
		});        // Here your ajax action after delete confirmed
}
</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 $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                <li><a href="#"><?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="wrap">                    
	<div class="row-fluid">
		<div class="span12">
        	<div class="block">
			<?php 
				$hidden = array('order_time' => date('Y-m-d H-i-s'));
				$attrib = array('id'=>'validate'); 
				echo form_open('', $attrib, $hidden);?>
                                <!-- form id="validate" method="POST" action="javascript:alert('Form #validate submited');"-->
                                <div class="block">
                                    <div class="head">
                                        <h2><?php echo $this->lang->line('title');?></h2>
                                    </div>                      
                                    <div class="content np">
											<div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('year');?></div>
                                                <div class="span4">
                                                   <?php
														if($fycmaster){
															$preselyear = $fycmaster->financialYear;
														}else{
															$preselyear = date('Y');
														}
														$options = '';
														$options[] = $this->lang->line('pleaseSelect');
														for($i=-5; $i<6; $i++){
															$options[date('Y', strtotime($i . "  year"))] = date('Y', strtotime($i . "  year"));
														}
														$js='onChange="checkYear(this)"; style="width: 150px;"';
														echo form_dropdown('financialYear', $options, $preselyear, $js);
													?>                                       
                                            	</div>
                                            </div>
                                            <div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('startperiod');?></div>
                                                <div class="span10"><?php
													$data = array(
													'name'=>'startPeriod',
													'id'=>'startPeriod',
													'onkeypress'=>'return isNumberKey(event)',
													'maxlength'=>'2');
													//'onBlur' =>'yearperiodCheck()');
													$js2='onChange="dateSearch(this)" class="span1 tar" readonly';
													$js='onChange="dateSearch(this)" class="span1 tar"';
													
													if($fycmaster){
														echo form_input($data,  $fycmaster->period + 1, $js2);                      
													}
													else
													{
														echo form_input($data, '1', $js); 
													}
													?> 
                                                </div>
                                            </div>
                                            <div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('noofperiod');?></div>
                                                <div class="span10"><?php
													$data = array(
													'name'=>'period',
													'id'=>'period',
													'onkeypress'=>'return isNumberKey(event)',
													'maxlength'=>'2');
													//'onBlur' =>'yearperiodCheck()');
													$js='onChange="dateSearch(this)" class="span1 tar"';
														echo form_input($data, '1', $js); 
													?> 
                                                </div>
                                            </div>
											<div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('startDate');?></div>
                                                <div class="span9">
                                                <div class="input-prepend">
                                                    <span class="add-on"><i class="i-calendar"></i></span><?php
													
													if($fycmaster){
														$data = array(
														'name' => 'startDate',
														'id' => 'startDate',
														'onc' => 'text',
														'class' => 'input-small financhk',);
														$js2='onChange="dateSearch()" readonly';
														echo form_input($data, date('d-m-Y', strtotime($fycmaster->startDate. '+1 months')), $js2);                      
													}
													else
													{
														$data = array(
														'name' => 'startDate',
														'id' => 'startDate',
														'onc' => 'text',
														'class' => 'input-small datepicker2 financhk',);
														$js='onChange="dateSearch()"';
														echo form_input($data, date('01-m-Y'), $js); 
													}
													?>
                                                </div>                                                                                                
                                            	</div>
                                            </div>
											<div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('endDate');?></div>
                                                <div class="span9">
                                                <div class="input-prepend">
                                                    <span class="add-on"><i class="i-calendar"></i></span><?php
													$data = array(
													'name' => 'endDate',
													'id' => 'endDate',
													'type' => 'text',
													'class' => 'input-small',);
													if($fycmaster){
														echo form_input($data, date('t-m-Y', strtotime($fycmaster->startDate. '+1 months')), 'readonly');                      
													}
													else
													{
														echo form_input($data, date('t-m-Y'), 'readonly'); 
													}
													?>
                                                </div>                                                                                                
                                            	</div>
                                            </div>
											                                    
                                    </div>
            <div class="footer">
            	<div class="side fr">
                	<div class="btn-group">
                        <?php
							$data = array(
									'name'=>'submit',
									'id'=>'submit',
									'class'=>'btn-form btn-success-form');
							echo form_submit($data,$this->lang->line('submit'));
							?>
                    </div>
                </div>
            </div>
            <div class="content np table-sorting">
            	<table cellpadding="0" cellspacing="0" width="100%" id="companyFinancialCalendar" class="editable">
                	<thead>
                    <tr>
            			<th style="display:none"></th>
            			<th class="tac">#</th>
            			<th class="tac"><?php echo $this->lang->line('year');?></th>
                		<th class="tac"><?php echo $this->lang->line('period');?></th>
                		<th class="tac"><?php echo $this->lang->line('startDate');?></th>
                		<th class="tac"><?php echo $this->lang->line('endDate');?></th>                
                		<th class="tac"><?php echo $this->lang->line('monthendclosing');?></th>               
                		<th width="55" class="tac"><?php echo $this->lang->line('action');?></th>
            		</tr>
        			</thead>
       				 <tbody>
			<?php $bil = 0; 
				if($datatbls){
				foreach($datatbls as $datatbl):
				$bil++?>
            	<tr>
                	<td style="display:none" ><?php echo $datatbl->ID; ?></td>
                	<td class="tac"><?php echo $bil; ?></td>
                	<td class="tac"><?php echo $datatbl->financialYear ?></td>
                	<td class="tac"><?php echo $datatbl->period; ?></td>
                	<td class="tac"><?php echo date("d-m-Y", strtotime($datatbl->startDate)); ?></td>
                	<td class="tac"><?php echo date("d-m-Y", strtotime($datatbl->endDate)); ?></td>
                	<td class="tac"><a title="<?php echo $this->lang->line('edit');?>" class="btn btn-mini btn-link edit"><?php if(($datatbl->status) == 0){echo "Open";}else{echo "Close";} ?></a></td>
						<td class="tac"><?php
						if($datatbl->yearEnd == 1){
							$js = 'class="btn-icon btn-mini btn-danger-form remove" title="'.$this->lang->line('delete').'"';
							echo form_button('remove','<span class="i-trashcan">',$js);
						}
						?></td>
                </tr>
            <?php endforeach; } ?>
					</tbody>
			</table>                                         
			</div>                                    
                                </div>
                                </form>

                            </div>

                        </div>

<div class="dialog" id="row_delete" style="display: none;" title="Delete?">
    <p>Row will be deleted</p>
</div>   
<div class="dialog" id="row_edit" style="display: none;" title="Edit ?">
    <p>To editing page.....</p>
</div> </div></div></div></div></div>