<?PHP session_start();	PHP?>

<html>
<head>
<title>kontiki</title>

<?PHP
	include("db.php");

	function showNoSchedule()
	{
		echo "We have no schedule for this day!";
	}

	function showCalendar()
	{
		if($_SESSION[showDate] == '')
			$_SESSION[showDate] = date('Y-m');

		$secsaday = 24*60*60;
		$currentDate = date('d');
		$currentMonth = date('m');
		$currentYear = date('Y');

		$today = date('j');

		$dateInfo = explode('-', $monthToShow);
		if($dateinfo[2] == '')
			$dateinfo[2] = 1;

		// move back/forth in time for each year/month to get a timestamp/offset that represents the first day of target month/year
		$offset = ($dateInfo[0] - $currentYear) * $secsaday * 365 + ($dateInfo[1] - $currentMonth) * $secsaday * 30 + ($today -1) * $secsaday;

		$viewDate = 0;
		$viewMonth = 0;
		$viewYear = 0;


		$prevYear = $currentYear;
		$nextYear = $currentYear;
		$currentMonthsDays = date('t');
		$prevMonthsDays = date('t', time() - $today * $secsaday);
		$prevMonth = date('m', time() - $today * $secsaday);
		if($prevMonth == 12)
			$prevYear--;
		$nextMonth = date('m', time() + ($currentMonthsDays - $today +1) * $secsaday);
		if($nextMonth == 1)
			$nextYear++;
		$firstDayInMonth = date('w', time() - (date('j') -1) * $secsaday);
		if($firstDayInMonth == 0)
			$firstDayInMonth = 7;
		--$firstDayInMonth;
		$lastDayInMonth = date('w', time() + ($currentMonthsDays - $today) * $secsaday);
		if($lastDayInMonth == 0)
			$lastDayInMonth = 7;
		if($today < 15)
			$previousDays = 7;
		else
			$futureDays = 14;

		echo "<table border=1 cellpadding=0 cellspacing=0>\n<tr>\n";
		echo "<tr>\n<td><a href=\"$_SERVER[PHP_SELF]?viewDate=$prevYear-$prevMonth\">&lt;</a></td>\n<td colspan=5><center><b>" . date('F') . "</b></center></td>\n<td><a href=\"$_SERVER[PHP_SELF]?viewDate=$nextYear-$nextMonth\">&gt;</a></td>\n</tr>\n";

		for($n = $prevMonthsDays - ($firstDayInMonth + $previousDays -1); $n <= $prevMonthsDays; $n++)
		{
			$targetDate = $currentYear . "-" . $prevMonth . "-" . str_pad($n, 2, '0', STR_PAD_LEFT);
			echo "<td bgcolor=\"#999999\"><a href=\"$_SERVER[PHP_SELF]?viewDate=$targetDate\">$n</a></td>\n";
			if(date('w', time() - ($prevMonthsDays - $n + $today) * $secsaday) == 0)
				echo "</tr>\n<tr>\n";
		}

		for($n = 1; $n <= $currentMonthsDays; $n++)
		{
			$targetDate = $currentYear . "-" . $currentMonth . "-" . str_pad($n, 2, '0', STR_PAD_LEFT);

			echo "<td bgcolor=\"";
			if($today != $n)
				echo "#cccccc";
			else
				echo "#ffcccc";
			echo "\"><a href=\"$_SERVER[PHP_SELF]?viewDate=$targetDate\">$n</a></td>\n";
			if(date('w', time() - ($today - $n) * 24 * 60 * 60) == 0)
				echo "</tr>\n<tr>\n";
		}

		for($n = 1; $n <= ($futureDays + (7 - $lastDayInMonth)); $n++)
		{
			$targetDate = $currentYear . "-" . $nextMonth . "-" . str_pad($n, 2, '0', STR_PAD_LEFT);
			echo "<td bgcolor=\"#999999\"><a href=\"$_SERVER[PHP_SELF]?viewDate=$targetDate\">$n</a></td>\n";
			if(date('w', time() + ($currentMonthsDays - $today + $n) * $secsaday) == 0)
				echo "</tr>\n<tr>\n";
		}

		echo "</table>\n";

	}

	function showInputMode()
	{
		PHP?>
<form method="post" enctype="multipart/form-data" action="<?PHP echo $_SERVER[PHP_SELF]; PHP?>">
<table border=0 celppadding=0 cellspacing=0>
	<tr>
		<td>År:</td>
		<td><select name="year"><option value="2007">2007</option><option value="2008">2008</option><option value="2009">2009</option></select></td>
	</tr>
        <tr>
                <td>Månad:</td>
                <td><select name="month"><?PHP for($n = 1;$n <= 12;$n++) echo "<option value=\"$n\">$n</option>"; PHP?></select></td>
        </tr>
        <tr>
                <td>Datum:</td>
                <td><select name="date"><?PHP for($n = 1;$n <= 31;$n++) echo "<option value=\"$n\">$n</option>"; PHP?></select></td>
        </tr>
	<tr>
		<td>Rubrik:</td>
		<td><input type="text" name="subject"></td>
	</tr>
	<tr>
		<td>Ingress:</td>
		<td><textarea name="ingress"></textarea></td>
	</tr>
	<tr>
		<td>Text:</td>
		<td><textarea name="textbody"></textarea></td>
	</tr>
	<tr>
		<td>Länk 1:</td>
		<td><input type="text" name="lnk1"></td>
	</tr>
	<tr>
		<td>Länk 2:</td>
		<td><input type="text" name="lnk2"></td>
	</tr>
	<tr>
		<td>Bild 1:</td>
		<td><input type="file" name="img1"></td>
	</tr>
	<tr>
		<td>Bild 2:</td>
		<td><input type="file" name="img2"></td>
	</tr>
	<tr>
		<td>Bild 3:</td>
		<td><input type="file" name="img3"></td>
	</tr>
	<tr>
		<td>Banner:</td>
		<td><input type="file" name="banner"></td>
	</tr>
	<tr>
		<td colspan=2><input type="submit" value="Spara"></td>
	</tr>
</table>
</form>
		<?PHP
	}

	function storeNewEvent()
	{
		$images = "";
		if($_FILES[img1] != "")
		{
			$imageDestination = "img/calendar/" . $_FILES[img1][name];
			move_uploaded_file($_FILES[img1][tmp_name], $imageDestination);
			$images = $images . $_FILES[img1][name] . ";";
		}
		else
			$images = $images . ";";
		if($_FILES[img2] != "")
		{
			$imageDestination = "img/calendar/" . $_FILES[img2][name];
			move_uploaded_file($_FILES[img2][tmp_name], $imageDestination);
			$images = $images . $_FILES[img2][name] . ";";
		}
		else
			$images = $images . ";";
		if($_FILES[img3] != "")
		{
			$imageDestination = "img/calendar/" . $_FILES[img3][name];
			move_uploaded_file($_FILES[img3][tmp_name], $imageDestination);
			$images = $images . $_FILES[img3][name] . ";";
		}
		else
			$images = $images . ";";
		if($_FILES[banner] != "")
		{
			$imageDestination = "img/banners/" . $_FILES[banner][name];
			move_uploaded_file($_FILES[banner][tmp_name], $imageDestination);
			$banner = $_FILES[banner][name];
		}

		$links = $_POST[lnk1] . ";" . $_POST[lnk2];
		$date = $_POST[year] . "-" . str_pad($_POST[month], 2, "0", STR_PAD_LEFT) . "-" . str_pad($_POST[date], 2, "0", STR_PAD_LEFT);
		$query = "insert into calendar values('','$date','$_POST[subject]','$_POST[ingress]','$_POST[textbody]','$links','$images','$banner')";
		mysql_query($query);
	}
PHP?>

</head>

<body>

<?PHP
	if($_POST[subject] != "")
	{
		storeNewEvent();
	}

	if(strlen($_GET[viewDate]) == 10)
	{
		$date = $_GET[viewDate];
		$query = "select * from calendar where date='$date'";
		$result = mysql_query($query);
		if(mysql_num_rows($result) > 0)
			while($line = @mysql_fetch_array($result))
			{
				echo "$line[rubrik]<br>$line[ingress]<br>$line[date]<br>$line[kropp]<br>";
					$links = explode(';', $line[links]);
				foreach($links as $link)
				{
					if($link != '')
						echo "<a href=\"$link\">$link</a><br>\n";
				}
					$images = explode(';', $line[images]);
				foreach($images as $image)
				{
					if($image != '')
						echo "<img src=\"img/calendar/$image\"><br>\n";
				}
				echo "<br><center><hr width=50%></center><br>\n";
			}
		else
			showNoSchedule();
	}
	elseif(strlen($_GET[viewDate]) == 7)
	{
		// set a session variable to keep track of what month we are looking at
		$_SESSION[showDate] = $_GET[viewDate];
	}
	else
	{
		showInputMode();
	}

	showCalendar();

PHP?>

</body>
</html>
