<?php 
session_name('CAKEPHP'); session_start();
$PageSecurity = 11;
include('includes/session.inc');
//include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');
include("includes/function_lib.php");
include('CRM/functions.php');
$s=new db_query();
$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');
$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$lowerlist=  UserDeptList($LowerDeptList);
 if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist).",".$_SESSION['UserID'];
else
$lowerlist="'".$_SESSION['UserID']."'";
//echo $_REQUEST['month'];
//echo $_REQUEST['year'];
function getmicrotime(){ 
    list($usec, $sec) = explode(" ",microtime()); 
    return ((float)$usec + (float)$sec); 
} 
if(!isset($_GET['year'])){
    $_GET['year'] = date("Y");
}
if(!isset($_GET['month'])){
    $_GET['month'] = date("n")+1;
}
global $db;
$month = addslashes($_GET['month']);
$year = addslashes($_GET['year']);
  
 
  
$todays_date = date("j");
$todays_month = date("n");

$days_in_month = date ("t", mktime(0,0,0,$_GET['month'],0,$_GET['year']));
$first_day_of_month = date ("w", mktime(0,0,0,$_GET['month'],1,$_GET['year']));
$first_day_of_month = $first_day_of_month + 1;
$count_boxes = 0;
$days_so_far = 0;

if($_GET['month'] == 13){
    $next_month = 2;
    $next_year = $_GET['year'] + 1;
} else {
    $next_month = $_GET['month'] + 1;
    $next_year = $_GET['year'];
}

if($_GET['month'] == 2){
    $prev_month = 13;
    $prev_year = $_GET['year'] - 1;
} else {
    $prev_month = $_GET['month'] - 1;
    $prev_year = $_GET['year'];
}

?>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" >
  <tr>
    <td>
	
	<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" >
				  <tr>
					<td class="calbg">
					<table width="100%" bordercolor="#0066FF" border="1" cellpadding="0" cellspacing="1">
						<tr class="topdays"> 
						  <td><div align="center" style="color:#FF0000">Sun</div></td>
						  <td><div align="center">Mon</div></td>
						  <td><div align="center">Tue</div></td>
						  <td><div align="center">Wed</div></td>
						  <td><div align="center">Thu</div></td>
						  <td><div align="center">Fri</div></td>
						  <td><div align="center">Sat</div></td>
						</tr>
						<tr valign="top" bgcolor="#FFFFFF"> 
						<?php
						for ($i = 1; $i <= $first_day_of_month-1; $i++) {
							$days_so_far = $days_so_far + 1;
							$count_boxes = $count_boxes + 1;
							?>
							<td  width="40" height="40" style="text-align:center;" class="beforedayboxes" >&nbsp;</td>
							<?php 
						}
						for ($i = 1; $i <= $days_in_month; $i++) {
							$days_so_far = $days_so_far + 1;
								$count_boxes = $count_boxes + 1;
							if($_GET['month'] == $todays_month){
								if($i == $todays_date){
								 	$class = "highlighteddayboxes";
								} else {
									$class = "dayboxes";
								}
							} else {
								if($i == 1){
									$class = "highlighteddayboxes";
								} else {
									$class = "dayboxes";
								}
							}
							?>
							<td  width="40" height="40"  style="text-align:center;"  class=<?php echo $class;?>> 
							<?php 
				 


							$link_month = $_GET['month'];
$query =DB_query("SELECT  * FROM event WHERE  day(event_start_date)='".$i."' and month(event_start_date)='$link_month' AND  year(event_start_date)='".$_GET[year]."'",$db);
$numevent= DB_num_rows($query);
 $query1 =DB_query("SELECT  * FROM to_do WHERE  day(due_date)='".$i."' and month(due_date)='$link_month' AND  year(due_date)='".$_GET[year]."'",$db);
$numtodo= DB_num_rows($query1);

//$query2=DB_query("SELECT * FROM crm_add_to_do WHERE  day(todo_date)='".$i."' and month(todo_date)='$link_month' AND  year(todo_date)='".$_GET[year]."' and createdby in($lowerlist) ORDER BY todo_date",$db);
//$numrowsfortodo=DB_num_rows($query2);
  ?>						
							 <div align="center"><span class="toprightnumber">
							 <?php if($numevent>0) {?>
							<?php echo $i;?>	
							<input type="hidden" name="evcurday" id="evcurday" value="<?php echo $i;?>"/>	
							<input type="hidden" name="evmonth" id="evmonth" value="<?php echo $link_month; ?>" />	
							<input type="hidden" name="evyear" id="evyear" value="<?php echo $_GET[year];?>" />	
							<a href="#"  onclick="open_items()" style="font-size:9px; position:absolute">
						<?php
							 echo "(Events:".$numevent.'<br/>';?></a>
							 <?php }else if($numtodo>0) {?>
							 <?php echo $i;?>	
							<input type="hidden" name="todocurday" id="todocurday" value="<?php echo $i;?>"/>	
							<input type="hidden" name="todomonth" id="todomonth" value="<?php echo $link_month; ?>" />	
							<input type="hidden" name="todoyear" id="todoyear" value="<?php echo $_GET[year];?>" />	
							<a href="#"  onclick="open_items1()" style="font-size:9px; position:absolute">
							
							  <?php 
							  echo "To-Do:"."(".$numtodo.")".'<br/>';
							  ?>
							  </a> 
							<?php }else{ echo $i; } ?>
							
 							&nbsp;</span></div>
 							</td><?php 
							if(($count_boxes == 7) && ($days_so_far != (($first_day_of_month-1) + $days_in_month))){
								$count_boxes = 0;?>
								</tr><tr valign="top">
							<?php }
						}
						$extra_boxes = 7 - $count_boxes;
						for ($i = 1; $i <= $extra_boxes; $i++) {
						?>
							<td width="40" height="40" class="afterdayboxes">&nbsp;</td>
							<?php 
						}
						$time_end = getmicrotime();
						$time = round($time_end - $time_start, 3);
						?>
						</tr>
					  </table></td>
				  </tr>
				</table>
	</td>
  </tr>
</table>