<?php
function thorr_calendar($cur_selected_month=0){//functie calendar
	$uid=$_SESSION['med']['uid'];
	$uid=floor($uid);
	if($cur_selected_month < -200){$cur_selected_month=-200;}
	if($cur_selected_month > 200){$cur_selected_month=200;}


	$today=date("j"); $month= date("n")+$cur_selected_month; $year=date("Y");
	while ($month>12) {$month=$month-12; $year++;}
	while ($month<1)  {$month=$month+12; $year--;}
	$month_num_days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
	
	$prevMonth_num=$month-1;  	$prevMonth_year=$year;
	while ($prevMonth_num>12) {$prevMonth_num=$prevMonth_num-12; $prevMonth_year++;}
	while ($prevMonth_num<1)  {$prevMonth_num=$prevMonth_num+12; $prevMonth_year--;}
	$prevMonth_num_days = cal_days_in_month(CAL_GREGORIAN, $prevMonth_num, $prevMonth_year);

	$nextMonth_num=$month+1;  	$nextMonth_year=$year;
	while ($nextMonth_num>12) {$nextMonth_num=$nextMonth_num-12; $nextMonth_year++;}
	while ($nextMonth_num<1)  {$nextMonth_num=$nextMonth_num+12; $nextMonth_year--;}

	$firstOfMonth=date("w",mktime(0, 0, 0, $month , 1, $year )); //aflu in ce zi va fi prima luni
	if ( $firstOfMonth == 0 ){$firstLuni = 2;}
	elseif ( $firstOfMonth == 1 ){$firstLuni = 1;}
	else {$firstLuni = 9 - $firstOfMonth;}
	$days_fill_before = 8- $firstLuni; //days from previewos month to be included
	$start_num = $prevMonth_num_days-$days_fill_before+1; //prev month date to start from
	$days_fill_after = 7- ($days_fill_before + $month_num_days)%7; //days from next month



?>
<h2 align="center">Calendar programari:</h2>
<style>
table.calendar {
	border-width: 2px;
	border-spacing: 0px;
	border-style: solid;
	border-color: #99C;
	border-collapse: collapse;
}
table.calendar th {
	border-width: 2px;
	padding: 1px;
	border-style: solid;
	border-color: #99C;
	-moz-border-radius: 0px;
	background-color: #A2CBEA;
}
table.calendar td {
	border-width: 2px;
	padding: 1px;
	border-style: solid;
	border-color: #99C;
	-moz-border-radius: 0px;
	opacity: 1;
}
table.calendar td:HOVER {
	opacity: 0.85;
	outline-color: #333;
	outline-style: solid;
	outline-width: 2px;
	cursor: pointer;

}

.calendar_prevm {background-color: #EEE;color: #999;}
.calendar_nowm {background-color: #EEF;color: #000;}
.calendar_green {background-color: #6F9;color: #000;}
.calendar_green2 {background-color: #AFCBB8;color: #999;}
.calendar_yellow {background-color: #FF9;color: #000;}
.calendar_yellow2 {background-color: #DFDFB9;color: #999;}
.calendar_red {background-color: #F99;color: #000;}
.calendar_red2 {background-color: #D9BFBF;color: #999;}
.calendar_multi {color: #000;background-image: url(./img/cal_multi.png);}
.calendar_multi2 {color: #999;background-image: url(./img/cal_mult2i.png);}
.calendar_gy {color: #000;background-image: url(./img/cal_gy.png);}
.calendar_gy2 {color: #999;	background-image: url(./img/cal_gy2.png);}
.calendar_gr {color: #000;background-image: url(./img/cal_gr.png);}
.calendar_gr2 {color: #999;	background-image: url(./img/cal_gr2.png);}
.calendar_yr {color: #000;background-image: url(./img/cal_yr.png);}
.calendar_yr2 {color: #999;	background-image: url(./img/cal_yr2.png);}
.events_tabl_cell{cursor:pointer;}
.events_tabl_cell:hover {cursor:pointer;background-color: #c8ceec;}
.events_div {
	font-size: 14px;
	padding-top: 2px;
	padding-bottom: 3px;
	padding-left: 12px;
	padding-right: 4px;
	overflow: visible;
	background-position:center right;
	background-repeat:no-repeat;
}
.events_div_date {
	font-size: 16px;
	padding-top: 2px;
	padding-bottom: 3px;
	padding-left: 30px;
	padding-right: 4px;
	background-image: url(./img/calendar-blue.gif);
	background-position: 6px center;
	background-repeat: no-repeat;
	overflow: visible;
}

.spacer_main_left {
	background-image: url(./img/bg_1px_border_w.png);
	height: 1px;
	margin-bottom: 0px;
	margin-top: 5px;
}
div#mainmiddle {
	width: 580px;
	float: left;
	margin-top: 4px;
	z-index: 2;
	display: block;
	overflow: visible;
	text-align: left;
}

.spacer_mare {
	background-image: url(./img/spliter.png);
	background-repeat: no-repeat;
	height: 19px;
	background-position: center;
	margin: 5px;
}
</style>
<table width="258" border="0" cellpadding="0" cellspacing="0" class="calendar" align="center">
<tr><td onclick="document.location='<?php echo htmlentities($_SERVER['PHP_SELF']).'?month='.($cur_selected_month-1); ?>';">
<div align="center"><strong>&laquo;</strong></div></td><th colspan="5"><div align="center"><strong><?php 
echo date("F", mktime(0, 0, 0, $month, 1, $year))." - $year"; ?></strong></div></th>
<td onclick="document.location='<?php echo htmlentities($_SERVER['PHP_SELF']).'?month='.($cur_selected_month+1); ?>';">
<div align="center"><strong>&raquo;</strong></div></td></tr>
<tr><th width="33"><div align="center">L</div></th><th width="33" ><div align="center">M</div></th>
<th width="33"><div align="center">M</div></th><th width="33" ><div align="center">J</div></th>
<th width="33"><div align="center">V</div></th><th width="33" ><div align="center">S</div></th>
<th><div align="center">D</div></th></tr><?php


$j=1; /* luni */
for ($i=$start_num;$i<=$prevMonth_num_days;$i++) {
	$onclick=" onclick=\"document.location='$svlocation?month=".($cur_selected_month-1)."&bookdate=$year-$prevMonth_num-$i';\" ";
	if ($j==1) {echo '<tr>';}	
	$date= date("Y-m-d", mktime(0, 0, 0, $prevMonth_num, $i, $prevMonth_year));

	
	$query=mysql_query("SELECT * FROM `rezervari` WHERE `uid` = '$uid' AND `data_ora`>='$date 00:00:00' AND `data_ora`<'$date 23:59:59' "); $nr_rez_data=mysql_num_rows($query);
	if($nr_rez_data!=0){	$ok=0;$pen=0;$bad=0;
		while ($data=mysql_fetch_array($query,1)){
			if ($data['status']=='ok' || $data['status']=='finalizat' ) {$ok++;}
			if ($data['status']=='anulat de user' || $data['status']=='anulat de firma' || $data['status']=='respins'){$bad++;}
			if ($data['status']=='pending' ) {$pen++;}
			}
if($bad!=0) {$color='red2';} elseif ($pen!=0) {$color='yellow2';} else {$color='green2';}
if($bad!=0 && $pen!=0){$color='yr2';} elseif($pen!=0 && $ok!=0){$color='gy2';} elseif($bad!=0 && $ok!=0){$color='gr2';} 
if ($bad!=0 && $pen!=0 && $ok!=0){$color='multi2';}
			echo '<td '.$onclick.' class="calendar_'.$color.'"><div align="center">'.$i.'</div></td>';
		}
	else{echo '<td '.$onclick.' class="calendar_prevm"><div align="center">'.$i.'</div></td>';}
	if ($j==7) {$j=0;echo '</tr>';} $j++;
	}
/////////////////////////////////////
for ($i=1;$i<=$month_num_days;$i++) {
	$onclick=" onclick=\"document.location='$svlocation?month=$cur_selected_month&bookdate=$year-$month-$i';\" ";
	if ($j==1){echo '<tr>';}
	if ($cur_selected_month==0 && $i==$today){$azi=' style="color:#D00"';} else {$azi='';}
	$date= date("Y-m-d", mktime(0, 0, 0, $month, $i, $year));
	$sql="SELECT * FROM `rezervari` WHERE `uid` = '$uid' AND `data_ora`>='$date 00:00:00' AND `data_ora`<'$date 23:59:59' ";
	$query=mysql_query($sql); $nr_rez_data=mysql_num_rows($query);
	if($nr_rez_data!=0){	$ok=0;$pen=0;$bad=0;
		while ($data=mysql_fetch_array($query,1)){
			if ($data['status']=='ok' || $data['status']=='finalizat' ) {$ok++;}
			if ($data['status']=='anulat de user' || $data['status']=='anulat de firma' || $data['status']=='respins') {$bad++;}
			if ($data['status']=='pending' ) {$pen++;}
			}
if($bad!=0) {$color='red';} elseif ($pen!=0) {$color='yellow';} else {$color='green';}
if($bad!=0 && $pen!=0){$color='yr';} elseif($pen!=0 && $ok!=0){$color='gy';} elseif($bad!=0 && $ok!=0){$color='gr';} 
if ($bad!=0 && $pen!=0 && $ok!=0) {$color='multi';}
		echo '<td '.$onclick.' class="calendar_'.$color.'"><div align="center" '.$azi.'>'.$i.'</div></td>';
		}
	else{echo '<td '.$onclick.' class="calendar_nowm"><div align="center" '.$azi.'>'.$i.'</div></td>';}	
	if ($j==7) {$j=0;echo '</tr>';} $j++;
	}
//////////////////////////////////////
for ($i=1;$i<=$days_fill_after;$i++) {
	$onclick=" onclick=\"document.location='$svlocation?month=".($cur_selected_month+1)."&bookdate=$year-$nextMonth_num-$i';\" ";
	if ($j==1) {echo '<tr>';}	
	$date= date("Y-m-d", mktime(0, 0, 0, $nextMonth_num, $i, $nextMonth_year));
	$sql="SELECT * FROM `rezervari` WHERE `uid` = '$uid' AND `data_ora`>='$date 00:00:00' AND `data_ora`<'$date 23:59:59' ";
	$query=mysql_query($sql); $nr_rez_data=mysql_num_rows($query);
	if($nr_rez_data!=0){	$ok=0;$pen=0;$bad=0;
		while ($data=mysql_fetch_array($query,1)){
			if ($data['status']=='ok' || $data['status']=='finalizat' ) {$ok++;}
			if ($data['status']=='anulat de user' || $data['status']=='anulat de firma' || $data['status']=='respins') {$bad++;}
			if ($data['status']=='pending' ) {$pen++;}
			}
if($bad!=0) {$color='red2';} elseif ($pen!=0) {$color='yellow2';} else {$color='green2';}
if($bad!=0 && $pen!=0) {$color='yr2';} elseif($pen!=0 && $ok!=0){$color='gy2';} elseif($bad!=0 && $ok!=0){$color='gr2';} 
if ($bad!=0 && $pen!=0 && $ok!=0) {$color='multi2';}
			echo '<td '.$onclick.' class="calendar_'.$color.'"><div align="center">'.$i.'</div></td>';
		}
	else{echo '<td '.$onclick.' class="calendar_prevm"><div align="center">'.$i.'</div></td>';}
	if ($j==7) {$j=0;echo '</tr>';} $j++;
	}
?></table><?php  

		}//end of calendar

?>