
<?php 

//**************************FUNCTION FOR GET UNDER USERS********************START HERE******************************

include('CRM/functions.php');
$s=new db_query();
global $db;
 $deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');		 
  		$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
		 $lowerlist=  UserDeptList($LowerDeptList);
		 $lowerlist[]=$_SESSION['UserID'];
	  
		 if(count($lowerlist)>0)
		$lowerlist=implode(",",$lowerlist) ;
		else
		$lowerlist="'".$_SESSION['UserID']."'";
		 
	$user_ids1	=$lowerlist;
 
 $id1=array();

		

 //echo $user_ids;
 	
		 $user_id = $_SESSION['ACG_DashBoard_User'];


//**************************FUNCTION FOR GET UNDER USERS********************END HERE******************************

?>



<link href="css/cal.css" rel="stylesheet" type="text/css">
<?php $db_table ="event";

function getmicrotime(){ 
    list($usec, $sec) = explode(" ",microtime()); 
    return ((float)$usec + (float)$sec); 
} 

$time_start = getmicrotime();

if(!isset($_GET['year'])){
    $_GET['year'] = date("Y");
}
if(!isset($_GET['month'])){
    $_GET['month'] = date("n")+1;
}
global $db;
$month = addslashes($_GET['month'] - 1);
$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,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'];
}



?><script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0

  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>
   
<h1> <?php  echo  $page_title;?></h1>

<div align="center">
            <a href="<?php echo "index.php?route=Taskmgt/dashboard$rurl&month=$prev_month&amp;year=$prev_year"; ?>">&lt;&lt;</a>
          <select name="month" id="month" onChange="MM_jumpMenu('parent',this,0)">
            <?php
			for ($i = 1; $i <= 12; $i++) {
				$link = $i+1;
				if($_GET['month'] == $link){
					$selected = "selected";
				} else {
					$selected = "";
				}
				echo "<option value=\"index.php?route=Taskmgt/dashboard$rurl&month=$link&amp;year=$_GET[year]\" $selected>" . date ("F", mktime(0,0,0,$i,1,$_GET['year'])) . "</option>\n";
			}
			?>
          </select>
          <select name="year" id="year" onChange="MM_jumpMenu('parent',this,0)">
		  <?php
		  for ($i = date('Y'); $i <= date('Y')+100; $i++) {
		  	if($i == $_GET['year']){
				$selected = "selected";
			} else {
				$selected = "";
			}
		  	echo "<option value=\"index.php?route=Taskmgt/dashboard$rurl&month=$_GET[month]&amp;year=$i\" $selected>$i</option>\n";
		  }
		  ?>
          </select>
        <a href="<?php echo "index.php?route=Taskmgt/dashboard$rurl&month=$next_month&amp;year=$next_year"; ?>">&gt;&gt;</a></div>
  <br>
 <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+1){
								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'] - 1;
 $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);
$numofevents= DB_num_rows($query);
 $query1 =DB_query("SELECT  leadtodo,tododatetime,todostart_time FROM crm_leadtodo WHERE  day(tododatetime)='".$i."' and month(tododatetime)='$link_month' AND  year(tododatetime)='".$_GET[year]."' and lead_type='Task' ORDER BY tododatetime",$db);
$numofevents1= 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($numofevents>0 || $numofevents1>0 || $numrowsfortodo>0)
							 {?>
							<?php echo $i;?>						 
							<a href='index.php?route=CRM/CrmSearch_LeadEvent&modid=12&frmid=309&day=<?php echo $i;?>&amp;month=<?php echo $link_month;?>&amp;year=<?php echo $_GET[year];?>' style="font-size:9px; position:absolute">
							<?php if($numofevents>0)
							 echo "(Events:".$numofevents.'<br/>';
							  if($numofevents1>0)  
							  echo '<br/>'."Task:".$numofevents1.'<br/>';?></a>
							  
							  
							<a href='index.php?route=CRM/CrmSearch_fieldvisittodo&modid=12&frmid=309&day=<?php echo $i;?>&amp;month=<?php echo $link_month;?>&amp;year=<?php echo $_GET[year];?>' style="font-size:9px; position:absolute">
							
							  <?php if($numrowsfortodo>0)
							  echo "To-Do:"."(".$numrowsfortodo.")".'<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>
