<html>
	<head>	
		<link rel="stylesheet" type="text/css" href="../usa/stylesheet.css">
	</head>
	<form>
		<table>
			<tr><td rowspan=1000>&nbsp;</td><td colspan=3><br><b><?=$username?>'s To Do List ...<br><br></b></td></tr>
<?php

	function myFormat($str) {
		$string2 = str_replace("&", "&amp;", $str);
		$string2 = str_replace("'", "&acute;", $string2);
		$string2 = str_replace("<", "&lt;", $string2);
		$string2 = ereg_replace("(\r\n|\n|\r)", "<br>", $string2);
		
		$string = "";
		$lastSpace = -1;
		for($i=0; $i<strlen($string2); $i++) {
			if($i - $lastSpace > 60) {
				$string = "$string ";
				$lastSpace = $i;
			}
			$c = $string2{$i};
			$string = "$string$c";
			if($c == ' ') $lastSpace = $i;
		}
		return $string;
	}


  // establish connection
  include_once "../phorum/admin/settings/forums.php";

	
	$table = "TODO";
	if($username == "Dawn") {
		$table = "TODO_DAWN";
	}
	


	$server = mysql_connect($PHORUM['DatabaseServer'], $PHORUM['DatabaseUser'], $PHORUM['DatabasePassword']);
	$connection = mysql_select_db($PHORUM['DatabaseName'], $server); 

	//$result1 = mysql_query("CREATE TABLE TODO (TODO_ID varchar(200) primary key, DATE varchar(200) not null, COMMENT text not null);");
	
	// if able to connect
	if($server) {
		
		// handle request
		$date = date("Y.m.d H:i" ,time());		

		if (isset($_GET['delete'])) {
			$result = mysql_query("SELECT * from $table");		
			while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
				if(isset($_GET[$row[0]])) {
					$resultT = mysql_query("DELETE from $table where TODO_ID='".$row[0]."';");
					//echo "value of checkbox $row[0] is " . $_GET[$row[0]] . "<br>";
				}
			}			
		}
		elseif (isset($_GET['newtodo'])) {
			$id      = $_GET['newid'];
			$resultT = mysql_query("SELECT * from $table where TODO_ID='$id';");		
			
			// if haven't already added this record
			if(!@mysql_num_rows($resultT)) {
				$comment = myFormat($_GET['newtodo']);
				$resultT = mysql_query("INSERT INTO $table (TODO_ID, DATE, COMMENT) VALUES ('$id', '$date', '$comment');");
			}
		}
	
		// show all todos
		$result = mysql_query("SELECT * from $table order by DATE");		

		$biggestid = -1;
		while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
      echo "<tr><td><input type=checkbox name=" . $row[0] . "></td>";
      printf ("<td>%s</td><td>%s</td></tr>", $row[1], $row[2]);  
      if($row[0] > $biggestid) {
      	$biggestid = $row[0];
      }
		}
		mysql_close($server);
	}
?>
	<tr><td colspan=3>
		<br>
  	Add todo: <input type="text" name="newtodo" size="60">
  	<input type="hidden" name="newid" value="<?= $biggestid + 1;?>">
		<input type="submit" name="add" value="add">
		<input type="submit" name="delete" value="delete selected">
	</td></tr>
</form>
</html>
