<?php 
# org update from all.php 17my06 edit 22my06
# this is the display 12je24

#@_OUTLINE_PART_1_
#constants
$key1='bidp';
$key2='bidporg';
$data_base=$key2;
###$connect="pear_connect";
$connect='my_connect';
$filesubdir="../php/";
$include=$filesubdir."bidpall.php";
$homefile="http://bidp.bw";
$viewfile="formview_auto.php";
$addfile="formadd_auto.php";
$editfile="formeditone_auto.php";
$listfile="autolist.php";
$headwords=strtoupper($key1)." calendar: ";
$country='bw';
$holwords='';
$logo='<P CLASS="western"><IMG SRC="../data/images/bidplog.png" NAME="Graphic1" align="center" WIDTH=72 HEIGHT=100 BORDER=0></P>';
#constants

include "$include";
include 'allhere.php';
###require $filesubdir.'pear/DB.php'; #pear only

$connect ()
	or die ("Cannot connect to server");
$link=$connect()
	or die ("Cannot link to server");

#echo $_GET[test];
#print_r($_POST);

##$link=$connect() dy 5je06
##	or die ("Cannot connect to calendar server");

#calendar starts here
#use the form helper functions defined in chapter 6
?>
<html>
	<head>
		<META http-equiv="Content-Style-Type" content="text/css">
		<meta http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8">
		<LINK href="../dy01.css" rel="stylesheet" type="text/css">
		<link rel="shortcut icon" href="../pix/favicon.ico" type="image/x-icon" />
		<title>Botswana calendar</title>
		<?php
		echo $styleya;
		?>
	</head>
<body dir="ltr"
 style="background-color: rgb(204, 204, 204); background-image: http://bidp.bw/sites/all/themes/contrast/images/Page-BgGradient.jpg; color: rgb(84, 84, 84);"
 alink="#ee0000" lang="en-GB" link="#757575" vlink="#757575">
<?php
echo '<center>';
echo '<table width=70%>'; #start of overall table

print '<tr><td align="center">' .$logo. '</td></tr>';

$months=array(1=>'January', 2=>'February', 3=>'March', 4=>'April', 5=>'May', 6=>'June', 7=>'July', 8=>'August', 9=>'September', 10=>'October', 11=>'November', 12=>'December');

$years=array();
for ($year=date('Y')-3, $max_year=date('Y')+9; $year<$max_year; $year++)
{
	$years[$year]=$year;
}

if ($_GET['update']=='y')
{
	show_form();
	show_calendar($_GET['month'], $_GET['year']);
}
else
{
	#when nothing is submitted, show the form & then calendar for the current month
	show_form();
	show_calendar(date('n'), date('Y'));
}

function validate_form()
{
	global $months, $years;
	$errors=array();
	
	if (! array_key_exists($_POST['month'], $months))
	{
		$errors[]='Select a valid month';
	}
	
	if (! array_key_exists($_POST['year'], $years))
	{
		$errors[]='Select a valid year';
	}
	
	return $errors;
}

function show_form($errors='')
{
	global $months, $years, $this_year, $logo;
	
	#if the form is submitted, get defaults from submitted variables
	if ($_POST['_submit_check'])
	{
		$defaults=$_POST;
	}
	else
	{
		#otherwise set our own defaults, current month & year
		$defaults=array('year'=>date('Y'),
		                'month'=>date('n'));
	}
	
	if ($errors)
	{
		print 'You need to correct the following errors: <ul><li>';
		print implode('</li><li>', $errors);
		print '</li></ul>';
	}
}

function process_form()
{
	if ($_POST[month]>'')
		{show_calendar($_POST['month'], $_POST['year']);}
	elseif ($_GET[update]='y')
		{show_calendar($_GET[month], $_GET[year]);}
}

function show_calendar($month, $year)
{
	global $months, $link, $country, $logo;
	$weekdays=array('Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su');
	
	#find timestamp for midnight on the first day of the month
	$first_day=mktime(0,0,0,$month, 1, $year);
	#how many days in the month?
	$days_in_month=date('t', $first_day);
	#numeric first day of the month, needed to correctly position the first day
	$day_offset=date('w', $first_day); #adjust here for day of week at date=1
	if ($day_offset==0)
		{$day_offset=7;}
	#print the table header & row of weekday names
	#echo '<tr><td>';
	print '<table border="0" cellspacing="0" cellpadding="2">';
	echo '<tr bgcolor="yellow"><td><a href="'.$_SERVER[PHP_SELF].'?update=y&year='.($year-1).'&month='.$month.'"><<</a></td>';
	echo '<td align="right"><a href="'.$_SERVER[PHP_SELF].'?update=y';
	if ($month!=1)
		{echo '&year=' .$year. '&month=' .($month-1). '"><</a></td>';}
	else
		{echo '&year=' .($year-1). '&month=12"><</a></td>';}
	echo '<td colspan="3" align="center"><a href="'.$_SERVER[PHP_SELF].'?update=y&year='.date('Y').'&month='.date('n').'">Current</a></td>';
	echo '<td><a href="'.$_SERVER[PHP_SELF].'?update=y';
	if ($month!=12)
		{echo '&month=' .($month+1). '&year=' .$year. '">></a></td>';}
	else
		{echo '&month=1&year=' .($year+1). '">></a></td>';}
	echo '<td><a href="' .$_SERVER[PHP_SELF]. '?update=y&month='.$month.'&year='.($year+1).'">>></a></td></tr>';
	echo '<tr><th colspan="7" align="center">' .$months[$month]. ' ' .$year. '</th></tr>
	<tr><td align="center">';
	print implode('</td><td align="center">', $weekdays); #display day abbreviations from array
	print '</td></tr>'; 
	
	#if the first day of the month is, say, a Tuesday then put blank cells in for sun & mon
	$holiday="";
	if ($day_offset>1)
	{
		for ($i=1; $i<$day_offset; $i++) 
			{print '<td>&nbsp;</td>';} #print blank cell
	}
	
	#print a table cell for each day of the month
	for ($day=1; $day <= $days_in_month; $day++)
	{
		#render loop date as $date22
		if ($month<'10')
			{$month2='0'.$month;}
		else 
			{$month2=$month;}
		if ($day<'10')
			{$day2='0'.$day;}
		else
			{$day2=$day;}
		$date22="$year-$month2-$day2"; #defines the current date in this loop
#		echo '<br>date 22='.$date22.'<br>'; #ok

		if ($day==date('j') && $month==date('m') && $year==date('Y')) #today (no frame for individual cell)
		{
			$bg=' bgcolor="red"';
		}	
		elseif ($month==1 && $day==1) 
		{
			$bg=' bgcolor="orange"';
			$holiday="New year's day";
		}
		elseif ($month==1 && $day==1) 
		{
			$bg=' bgcolor="orange"';
			$holiday="New year's day";
		}
		elseif ($month==1 && $day==1) 
		{
			$bg=' bgcolor="orange"';
			$holiday="New year's day";
		}
		elseif ($month==1 && $day==1) 
		{
			$bg=' bgcolor="orange"';
			$holiday="New year's day";
		}
		elseif ($month==1 && $day==2) 
		{
			$bg=' bgcolor="orange"';
			$holiday="Second new year";
		}
				elseif ($month==5 && $day==1) 
		{
			$bg=' bgcolor="orange"';
			$holiday="Labour day";
		}
		elseif ($month==7 && $day==1) 
		{
			$bg=' bgcolor="orange"';
			$holiday="Sir Seretse Khama day";
		}
		elseif ($month==7 && $day==17) 
		{
			$bg=' bgcolor="orange"';
			$holiday="President's day";
		}
				elseif ($month==7 && $day==18) 
		{
			$bg=' bgcolor="orange"';
			$holiday="Public holiday";
		}
				elseif ($month==9 && $day==30) 
		{
			$bg=' bgcolor="orange"';
			$holiday="Botswana day";
		}
				elseif ($month==10 && $day==1) 
		{
			$bg=' bgcolor="orange"';
			$holiday="Public holiday";
		}
		elseif ($month==12 && $day==25) 
		{
			$bg=' bgcolor="orange"';
			$holiday="Christmas day";
		}
		elseif ($month==12 && $day==26) 
		{
			$bg=' bgcolor="orange"';
			$holiday="Boxing day";
		}
		elseif ($date22=='2006-05-25') 
		{
			$bg=' bgcolor="orange"';
			$holiday="Ascension day";
		}
		elseif ($date22=='2006-07-03') 
		{
			$bg=' bgcolor="orange"';
			$holiday="Public holiday";
		}
		elseif ($date22=='2006-04-14') 
		{
			$bg=' bgcolor="orange"';
			$holiday="Good Friday";
		}
		elseif ($date22=='2006-04-17') 
		{
			$bg=' bgcolor="orange"';
			$holiday="Easter Monday";
		}
		elseif ($date22=='2006-10-02') 
		{
			$bg=' bgcolor="orange"';
			$holiday="Public holiday";
		}
		elseif ($day_offset>5)
		{
			$bg=' bgcolor="pink"';
		}
		else
		{
			$bg='';
		}
		#$GLOBALS[test]='345'; #works 14my06
		if ($bg==' bgcolor="orange"') #write holiday list
		{
			#echo 'isbg'; #ok
			if ($holwords=='') #first day
			{
				if ($holiday>'')
					{$holwords=$day. ': ' .$holiday. '<br>';}
			}
			else #subsequent days
			{
				if ($holiday>'')
					{$holwords=$holwords.$day. ': ' .$holiday. '<br>';}
			}
		}
		print '<td align="center"'.$bg.'>'.$day.'</td>'; $day_offset++;
		#if this was the seventh in row, restart on next line & reset $day_offset
		if ($day_offset==8)
		{
			if ($bg==' bgcolor="orange"')
			{
				#try to get holiday text by week
				
				$day_offset=1;
				$weektag='';
				print "</tr>\n";
			}
			else
			{
				$day_offset=1;
				print "</tr>\n";
			}
			#if there are more days to come then start new row
			if ($day<$days_in_month)
			{
				print '<tr>';
			}
		}
	}
	
	#at this point one cell has been printed for each day of the month. If the last day of the month isn't a sat, pad the last row with blanks
	if ($day_offset>1)
	{
		for ($i=$day_offset; $i<8; $i++)
		{
			print '<td>&nbsp;</td>';
		}
		print '</tr>';
	}
	print '</table>'; #inner table
	#echo '</td></tr>';
	$GLOBALS['holwords']=$holwords; #pass to global data
}

#calendar display ends here

#print_r($GLOBALS);
echo '<tr><td>';
echo '<p><font color="orange">' .$GLOBALS['holwords']. '</font>';
#print_r($GLOBALS);
#print $holwords. '</font><br>';
print "<p><font color=\"red\">Today is ".date("l jS F, Y").$holiday.strftime(', week %W of the year')."</font>";
echo '</td></tr>';
echo '</table>'; #end of overall table
echo '<center>';
echo '<hr>';
print "<p><a href=$homefile><font color=\"#999966\"><b>Home</b></font></a>";	

print $madehere;
html_end();
?>
