<?php 

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

require_once('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******************************

?>
<style type="text/css">
.week_days{background-color:#e5f1f9; border:#aed0ea 1px solid; border-right:none; font-family:Arial; font-size:12px; font-weight:bold;}
.days{background-color:#fafbfc; border:#dddddd 1px solid; font-family:Arial; font-size:12px; border-top:none;}
</style>
<link href="css/colorbox1.css" rel="stylesheet" type="text/css" media="screen">
<link href="css/cal.css" rel="stylesheet" type="text/css">
<?php 
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>
<script type="text/javascript" src="js/jquery_002.js"></script>
   
<h2><?php  echo  $page_title;?></h2>

  <table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr> 
      
      <td width="100%">
          <div align="center">
            <a href="<?php echo "index.php?route=CRM/CalenderManagement$rurl&month=$prev_month&amp;year=$prev_year"; ?>"><img src="images/previous_month.gif" width="20" /></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=CRM/CalenderManagement$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=CRM/CalenderManagement$rurl&month=$_GET[month]&amp;year=$i\" $selected>$i</option>\n";
		  }
		  ?>
          </select>
        <a href="<?php echo "index.php?route=CRM/CalenderManagement$rurl&month=$next_month&amp;year=$next_year"; ?>"><img src="images/next_month.gif" width="20" /></a></div></td>
    
    </tr>
</table>

  <br>
 <table width="800" border="0" align="center" cellpadding="0" cellspacing="0" >
  <tr>
  <td valign="top">
  <table width="100" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="20" style="background:#ff0000;opacity: 0.5"></td>
    <td width="10">&nbsp;</td>
    <td valign="middle">To Do</td>
  </tr>
  <tr>
    <td height="10" colspan="3"></td>
    </tr>
  <tr>
    <td style="background:#FFFF00;opacity: 0.5"></td>
    <td>&nbsp;</td>
    <td>Task</td>
  </tr>
  <tr>
    <td height="10" colspan="3"></td>
    </tr>
  <tr>
    <td style="background-color:grey;opacity: 0.5"></td>
    <td>&nbsp;</td>
    <td>Follow Up</td>
  </tr>
  <tr>
    <td height="10" colspan="3"></td>
    </tr>
	  <tr>
    <td style="background:#0000FF;opacity: 0.5"></td>
    <td>&nbsp;</td>
    <td>Events</td>
  </tr>
</table>


  </td>
    <td>	
	<table width="700" border="0" align="center" cellpadding="0" cellspacing="0" >
				  <tr>
					<td class="calbg">
					<table width="700" border="0" cellpadding="0" cellspacing="0">
						<tr class="topdays"> 
						  <td height="25" class="week_days"><div align="center" style="color:#FF0000">Sun</div></td>
						  <td class="week_days"><div align="center">Mon</div></td>
						  <td class="week_days"><div align="center">Tue</div></td>
						  <td class="week_days"><div align="center">Wed</div></td>
						  <td class="week_days"><div align="center">Thu</div></td>
						  <td class="week_days"><div align="center">Fri</div></td>
						  <td class="week_days" style="border-right:#aed0ea 1px solid;"><div align="center">Sat</div></td>
						</tr>
						<tr valign="top"> 
						<?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="100" height="40" style="text-align:center;" class="beforedayboxes days">&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 = "dayboxes";
								} else {
									$class = "dayboxes";
								}
							}
							?>
                                                        <td  width="100" height="40"  style="text-align:center; padding:0px;" class="<?php echo $class;?> , days" onmouseover="this.style.background='#3a87ad';"  <?php if($class == "highlighteddayboxes"){ ?>onmouseout="this.style.background='#FFFF99';" <?php } else {?> onmouseout="this.style.background='white';" <?php } ?> valign="middle">  
<?php 
$link_month = $_GET['month'] - 1;
$date=$_GET[year].'-'.$link_month.'-'.$i;
$rev_date= date("jS F, Y", strtotime($date) );
$query =DB_query("SELECT * FROM to_do WHERE due_date='".$date."' and CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
$numofevents= DB_num_rows($query);

 $query1 =DB_query("SELECT * FROM tsk_taskassignment WHERE start_date='".$date."' and user_id='".$_SESSION['UserID']."' and CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
$numofevents1= DB_num_rows($query1);

$queryfollow =DB_query("SELECT * FROM follow_up WHERE next_followup_date='".$date."' ",$db);
$numoffollow= DB_num_rows($queryfollow);

$queryremarks =DB_query("SELECT * FROM `event` WHERE event_start_date='".$date."' and CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
$numofremarks= DB_num_rows($queryremarks);

$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" style="height:40px; width:100px; position:relative;"><span class="toprightnumber" style="position:absolute; margin-top:12px;">
							 <?php if($numofevents>0 || $numofevents1>0 || $numrowsfortodo>0 || $numoffollow>0 || $numofremarks>0)
							 {?>
							<?php  echo $i;?>						 
							</span>
							<?php if($numofevents>0)
							 echo "<a href='calendar_data_popup.php?date=".$date."&type=to_do' class='cboxElement'  title='".$rev_date."' rel='day_view'><div style='background-color: #FF0000; width:50%; height:50%; float:left; position:absolute; opacity: 0.5'>&nbsp;".$numofevents."</div></a>";
							  if($numofevents1>0)  
							  echo "<a href='calendar_data_popup.php?date=".$date."&type=task' class='cboxElement'  title='".$rev_date."' rel='day_view'><div style='background-color: yellow; width:50%; height:50%; left:50; float:left; position:absolute; opacity: 0.5'>&nbsp;".$numofevents1."</div></a>";
                                                         if($numoffollow>0)  
							  echo "<a href='calendar_data_popup.php?date=".$date."&type=follow_up' class='cboxElement'   title='".$rev_date."' rel='day_view'><div style='background-color: grey; width:50%; height:50%; float:left; top:20; position:absolute; opacity: 0.5'>&nbsp;".$numoffollow."</div></a>";
                                                        if($numofremarks>0)  
							  echo "<a href='calendar_data_popup.php?date=".$date."&type=event' class='cboxElement'  title='".$rev_date."' rel='day_view'><div style='background-color: Blue; width:50%; height:50%; top:20; left:50; float:left; position:absolute; opacity: 0.5;'>&nbsp;".$numofremarks."</div></a>";?>
							  
							  
							<a href='index.php?route=CRM/CrmSearch_fieldvisittodo&modid=12&frmid=309&mode=plan&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 "<br/>To-Do:"."(".$numrowsfortodo.")";
							  ?>
							  </a> 
							<?php }else{ echo $i; } ?>
							
 							&nbsp;</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="100" height="40" class="afterdayboxes days">&nbsp;</td>
							<?php 
						}
						$time_end = getmicrotime();
						$time = round($time_end - $time_start, 3);
						?>
						</tr>
					  </table></td>
				  </tr>
	  </table>
	</td>
  </tr>
</table>
<script type="text/javascript">
jQuery(document).ready(function(){ $("a[rel='day_view']").colorbox({width:"680px", height:"450px", iframe:true}); });
</script>