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

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

	function monthHas31days($month)
	{
		if($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12)
			return true;
		return false;
	}

	function monthHas30days($month)
	{
		if($month == 4 || $month == 6 || $month == 9 || $month == 11)
			return true;
		return false;
	}

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

	function calcDate($offset)
	{
		$currentYear = date("Y");
		$targetYear = $currentYear;
		$currentMonth = date("m");
		$targetMonth = $currentMonth;
		$currentDate = date("d");
		$targetDate = $currentDate + $offset;

		if($targetDate <= 0)
		{
			--$targetMonth;
			if($targetMonth == 0)
			{
				--$targetYear;
				$targetMonth = 12;
			}

			if(monthHas31days($targetMonth))
				$targetDate = $targetDate % 31 + 31;
			elseif(monthHas30days($targetMonth))
				$targetDate = $targetDate % 30 + 30;
			else
				$targetDate = $targetDate % 28 + 38;
		}
		else
		{
			if(monthHas31days($targetMonth))
			{
				if($targetDate > 31)
				{
					++$targetMonth;
					if($targetMonth > 12)
					{
						++$targetYear;
						$targetMonth = 1;
					}

					if(monthHas31days($targetMonth))
						$targetDate = $targetDate % 31 +1;
					elseif(monthHas30days($targetMonth))
						$targetDate = $targetDate % 30 +1;
					else
						$targetDate = $targetDate % 28 +1;
				}
				
			}
			elseif(monthHas30days($targetMonth))
			{
				if($targetDate > 30)
				{
					++$targetMonth;
					if(monthHas31days($targetMonth))
						$targetDate = $targetDate % 31 +1;
					elseif(monthHas30days($targetMonth))
						$targetDate = $targetDate % 30 +1;
					else
						$targetDate = $targetDate % 28 +1;
				}
			}
			else
			{
				if($targetDate > 28)
				{
					$targetMonth = 3;
					$targetDate = $targetDate % 31 +1;
				}
			}
		}

		return $targetYear . "-" . str_pad($targetMonth, 2, "0", STR_PAD_LEFT) . "-" . str_pad($targetDate, 2, "0", STR_PAD_LEFT);
	}

	function showCalendar()
	{
		for($offset = -7; $offset <= 20; $offset++)
		{
			if(!($offset % 7))
				echo "<br>";
			$thisTime = time() + $offset * 24 * 60 * 60;
			$date = date('d/m', $thisTime);
			echo "<a href=\"$_SERVER[PHP_SELF]?viewDate=$offset\"><font";
			if($offset == 0)
				echo " color=\"#00cc00\"";
			elseif(date('w', $thisTime) == 0 || date('w', $thisTime) == 6)
				echo " color=\"#cc0033\"";
			echo ">$date</font></a> ";
		}
		echo "<br>";
	}

	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 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 . ";";
		$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')";
		if(mysql_query($query))
			echo "query ok";
		else
			echo "query not ok";
	}
PHP?>

</head>

<body>

<?PHP
	showCalendar();

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

	if($_GET[viewDate] != "")
	{
		$date = calcDate($_GET[viewDate]);
		echo "trying to view date $date<br>";
		$query = "select * from calendar where date='$date'";
		$result = mysql_query($query);
		$line = @mysql_fetch_array($result);
		if($line != "")
		{
			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";
			}
		}
		else
			showNoSchedule();
	}
	else
	{
		showInputMode();
	}
PHP?>

</body>
</html>
