Newer
Older
ubixos-web / docroot / taskmanager / functions.php
@reddawg reddawg on 29 Jun 2004 86 KB UbixOS Web Site
<?PHP
/*-----------------------------------------------------------------------**
---------------------------------------------------------------------------
IPM (Incyte Project Manager)
PHP based project tracking tool 
Copyright (c) 2001 by phlux

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
---------------------------------------------------------------------------
**-----------------------------------------------------------------------*/
											
// FUNCTION LIST

////////// OOP RELATED FUNCTIONS ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
function task_list($id, $status, $order) {
	// print the nice indented list
	include "config.php";
	dbconnect();
	$query = "SELECT * FROM IPM_tasks WHERE id='$id' ORDER BY $order";
	$result = mysql_query($query);
	while ($task = mysql_fetch_array($result)) {
		$query2 = "SELECT $listdisplay FROM IPM_users WHERE id='$task[person]'";
		$result2 = mysql_query($query2);
		$user = mysql_fetch_array($result2);
		if (is_toplevel($task[task_id])) {
			if (get_decendents($task[task_id],$status) || $task[status]==$status) {	
			?>
			<tr valign=top>
				<td class=todolist>
					<?
					if ($task[status] == "$status") {
						$class="listlink";
					} else {
						$class="placeholder";
					}
					if ($task[description] == "") {
						 $string=popuploader("$task[task_id]", "No Description Available", "$task[title]", "$PHP_SELF?op=edittask&task_id=$task[task_id]&name=$list_row[name]", "$class");
					} else {
						 $string=popuploader("$task[task_id]", "$task[description]", "$task[title]", "$PHP_SELF?op=edittask&task_id=$task[task_id]&name=$list_row[name]", "$class");
					}
					echo "$string";
					?>
				</td>	
				<td class=todolist>
					<?=$user[$listdisplay]?>
				</td>
				<td align=center class=todolist>
					<? if ($task[comments]) {
						 echo "<img src=img/comment-on.gif border=0>";
					} else {
						 echo "<img src=img/comment-off.gif border=0>";
					} ?>
				</td>
				<td align=center class=todolist>
					<? if (!$task[billable]) {
						 echo $task[hours];
					} else {
						 echo "<font class=billable>$task[hours]</font>";
					} ?>
				</td>
				<td align=center class=todolist>
		 			<?
					if ($task[status] == "$status") {
						if ($status==1) {
							if (get_children($task[task_id],1)) {
								echo "<a href=#><img src=img/d-off.gif border=0></a> "
									."<a href=#><img src=img/f-off.gif border=0></a></td></tr>";
							} else {
								echo "<a href=$PHP_SELF?op=confirmdeletetask&task_id=$task[task_id]&id=$id><img src=img/d-on.gif border=0></a> "
									."<a href=$PHP_SELF?op=projectdetail&finish=$task[task_id]&id=$id><img src=img/f-on.gif border=0></a></td></tr>";
							}
			 			} else {
							if (get_children($task[task_id],0)) {
								echo "<a href=#><img src=img/d-off.gif border=0></a> "
									."<a href=$PHP_SELF?op=projectdetail&reset=$task[task_id]&id=$id><img src=img/r-on.gif border=0></a></td></tr>";
							} else {
								echo "<a href=$PHP_SELF?op=confirmdeletetask&task_id=$task[task_id]&id=$id><img src=img/d-on.gif border=0></a> "
									."<a href=$PHP_SELF?op=projectdetail&reset=$task[task_id]&id=$id><img src=img/r-on.gif border=0></a></td></tr>";
							}				
						}	
					} else {
						echo "<a href=#><img src=img/d-off.gif border=0></a> "
							."<a href=#><img src=img/f-off.gif border=0></a></td></tr>";
					}
			}
			print_tree($task[task_id], 0, $status);
		}
	}
}

function print_tree($task_id, $count, $status) {
	include "config.php";
	global $id;
	$count++;
	$indent=$count*10;
	$result = get_children($task_id, 2);
	for ($x=0; $x<sizeof($result); $x++) {
		$task=get_task_info($result[$x][0]);
		$query2 = "SELECT $listdisplay FROM IPM_users WHERE id='$task[person]'";
		$result2 = mysql_query($query2);
		$todouser = mysql_fetch_array($result2);
		if ($task[status]==$status || get_decendents($task[task_id],$status)) {
		?>
		<tr valign=top>
			<td class=todolist>
				<img src=img/spacer.gif border=0 height=10 width=<?=$indent?>>
				<?
				if ($task[status] == "$status") {
					$class="listlink";
				} else {
					$class="placeholder";
				}
				if ($task[description] == "") {
					 $string=popuploader("$task[task_id]", "No Description Available", "$task[title]", "$PHP_SELF?op=edittask&task_id=$task[task_id]&name=$list_row[name]", "$class");
				} else {
					 $string=popuploader("$task[task_id]", "$task[description]", "$task[title]", "$PHP_SELF?op=edittask&task_id=$task[task_id]&name=$list_row[name]", "$class");
				}
				echo "$string";
				?>
			</td>	
			<td class=todolist>
				<?=$todouser[$listdisplay]?>
			</td>
			<td align=center class=todolist>
				<? if ($task[comments]) {
					 echo "<img src=img/comment-on.gif border=0>";
				} else {
					 echo "<img src=img/comment-off.gif border=0>";
				} ?>
			</td>
		 	<td align=center class=todolist>
				<? if (!$task[billable]) {
					 echo $task[hours];
				} else {
					 echo "<font class=billable>$task[hours]</font>";
				} ?>
			</td>
			<td align=center class=todolist>
		 		<?
					if ($task[status] == "$status") {
						if ($status==1) {
							if (get_children($task[task_id],1)) {
								echo "<a href=#><img src=img/d-off.gif border=0></a> "
									."<a href=#><img src=img/f-off.gif border=0></a></td></tr>";
							} else {
								echo "<a href=$PHP_SELF?op=confirmdeletetask&task_id=$task[task_id]&id=$id><img src=img/d-on.gif border=0></a> "
									."<a href=$PHP_SELF?op=projectdetail&finish=$task[task_id]&id=$id><img src=img/f-on.gif border=0></a></td></tr>";
							}
			 			} else {
							if (get_children($task[task_id],0)) {
								echo "<a href=#><img src=img/d-off.gif border=0></a> "
									."<a href=$PHP_SELF?op=projectdetail&reset=$task[task_id]&id=$id><img src=img/r-on.gif border=0></a></td></tr>";
							} else {
								echo "<a href=$PHP_SELF?op=confirmdeletetask&task_id=$task[task_id]&id=$id><img src=img/d-on.gif border=0></a> "
									."<a href=$PHP_SELF?op=projectdetail&reset=$task[task_id]&id=$id><img src=img/r-on.gif border=0></a></td></tr>";
							}				
						}	
					} else {
						echo "<a href=#><img src=img/d-off.gif border=0></a> "
							."<a href=#><img src=img/f-off.gif border=0></a></td></tr>";
					}
	print_tree($result[$x][0], $count, $status);
	}
	}
}

function get_children($task_id, $status) {
	// returns the task_id's of all child tasks that are status=$status
	dbconnect();
	$count = 0;
	if (($status == 0) || ($status == 1)) {
		$query = "SELECT task_id FROM IPM_tasks WHERE parent_id='$task_id' AND status='$status'";
	} else {
		$query = "SELECT task_id FROM IPM_tasks WHERE parent_id='$task_id'";
	}
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result))
	{
	 		$children[$count][0] = $row["task_id"];
			$count++;
	}
	return $children;
}

function get_decendents($task_id, $status, $count = 0) {
	dbconnect();
	$result = get_children($task_id, 2);
	for ($x=0; $x<sizeof($result); $x++) {
		$task=get_task_info($result[$x][0]);
 		if (get_children($task[task_id], $status) || $status == $task[status]) {
			$decendents[$x][0] = $result[$x][0];
		}
		get_decendents($result[$x][0], $count, $status);
	}
	return $decendents;
}

function get_task_info($task_id) {
	// returns the data concerning a task
	dbconnect();
	$query = "SELECT * FROM IPM_tasks WHERE task_id='$task_id'";
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result))
	{
	 		$info["task_id"] = $task_id;
	 		$info["title"] = stripslashes($row["title"]);
			$info["hours"] = $row["hours"];
			$info["description"] = stripslashes($row["description"]);
			$info["person"] = stripslashes($row["person"]);
			$info["status"] = $row["status"];
			$info["billable"] = $row["billable"];
			$info["comments"] = stripslashes($row["comments"]);
			$info["parent_id"] = $row["parent_id"];
	}
	return $info;
}

function is_toplevel($task_id) {
	// returns 1 if the task is at the top level
	// returns null if the task is a subtask
	dbconnect();
	$query = "SELECT parent_id FROM IPM_tasks WHERE task_id='$task_id'";
	$result = mysql_query($query);
	while ($parent=mysql_fetch_row($result)) {
		if ($parent[0]==0) {
			return 1;
		} else {
		 	return;
		}
	}
}

function get_parent($task_id) {
	// returns task_id of parent task if it exists
	dbconnect();
	if (is_toplevel($task_id)) {
		 return;
	} else {
		 $query = "SELECT parent_id FROM IPM_tasks WHERE task_id='$task_id'";
		 $result = mysql_query($query);
		 if ($parent = mysql_fetch_row($result)) return $parent;
	}
}

function get_ancestors($task_id, $count = 0) {
	global $ancestors;

	$parent = get_parent($task_id);
	if($parent) {
		$ancestors[$count][0] = $parent;
		get_ancestors($parent, $count+1);
		return array_reverse($ancestors);
	}
}

function task_dropdown($id) {
	// print the nice indented list
	include "config.php";
	dbconnect();
	$query = "SELECT task_id,title FROM IPM_tasks WHERE id='$id'";
	$result = mysql_query($query);
	while ($task = mysql_fetch_array($result)) {
		if (is_toplevel($task[task_id])) {
			echo "<option value=$task[task_id]>$task[title]</option>\n";
			print_task_dropdown($task[task_id], 0);
		}
	}
}

function print_task_dropdown($task_id, $count) {
	include "config.php";
	global $id;
	$count++;
	$indent=$count*3;
	$spacing="";
	for ($x=0; $x<$indent; $x++) {
		$spacing.="&nbsp;";
	}
	$result = get_children($task_id, 2);
	for ($x=0; $x<sizeof($result); $x++) {
		$task=get_task_info($result[$x][0]);
		echo "<option value=$task[task_id]>$spacing $task[title]</option>";
		print_task_dropdown($result[$x][0], $count);
	}
}


function get_user_info($id) {
	// returns all data concerning a user
	dbconnect();
	$query = "SELECT * FROM IPM_users WHERE id='$id'";
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result))
	{
	 		$info["id"] = $id;
	 		$info["username"] = $row["username"];
			$info["password"] = $row["password"];
			$info["firstname"] = $row["firstname"];
			$info["lastname"] = $row["lastname"];
			$info["email"] = $row["email"];
			$info["role"] = $row["role"];
	}
	return $info;
}

////////// TASK RELATED FUNCTIONS ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

function mytasks() {
  // This function gives a comprehensive summary for all of a user's
	// projects and tasks. All tasks assigned to the logged in user are
	// displayed along with project details. This is the default function
	$user_id=authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $finish;
	// If a task was flagged for finishing then do it now
	if (isset($finish)) {
		finishtask($finish);
	}
	// get ammount of projects the user is assigned to	
	$p_query = "SELECT id FROM IPM_tasks WHERE person='$user_id' AND status='1' GROUP BY 'id'";
	$p_result = mysql_query($p_query);
	$project_total = mysql_num_rows($p_result);
	// get ammount of open tasks the user is assigned to	
	$t_query = "SELECT id FROM IPM_tasks WHERE person='$user_id' AND status='1'";
	$t_result =  mysql_query($t_query);
	$task_total = mysql_num_rows($t_result);
	// get billable info for user
	$billable_total=0;
	$thismonth=date("F, Y");
	$b_query = "SELECT DATE_FORMAT(enddate,'%M, %Y'), hours FROM IPM_tasks WHERE person='$user_id' AND status='0' AND billable='1'";
	$b_result = mysql_query($b_query);
	while ($b_total=mysql_fetch_array($b_result)) {
			echo "$b_total[enddate]";
			$billable_total=$billable_total+$b_total[hours];
	}
	// get the general user info	
	$user_query = "SELECT * FROM IPM_users WHERE id='$user_id'";
	$result = mysql_query($user_query);
	while ($userinfo = mysql_fetch_array($result)) {
		if ($userinfo[role] == "1") {
			$role = "Administrator";
		} else {
			$role = "General User";
		}
    // Display the HTML that sums up all of this info
		?>
		<table border=1 bordercolor=black cellpadding=2 cellspacing=0 width=90% align=center>
		<tr valign=top>
				<td class=header colspan=2>
						User Details for <b><?=$userinfo[firstname]?> <?=$userinfo[lastname]?></b>
				</td>
		</tr>
		<tr valign=top>
				<td class=darker width=30% align=right>
						Username: <br>
						Role: <br>
				 	  Total Projects: <br>
						Total Open Tasks: <br>
						Completed Billable Hours for <?=$thismonth?>:
				</td>
				<td class=lighter width=70% align=left>
						<b><?=$userinfo[username]?></b><br>
						<b><?=$role?></b><br>								
						<b><?=$project_total?></b><br>
						<b><?=$task_total?></b><br>
						<b><?=$billable_total?></b><br>						
				</td>
		</tr>
		</table><br>
		<?
		// find the user's open projects
		$open_query = "SELECT * FROM IPM_tasks WHERE person='$user_id' AND status='1' GROUP BY 'id'";
		$result2 = mysql_query($open_query);
		while ($open = mysql_fetch_array($result2)) {
			echo "<table width=90% align=center cellpadding=2 cellspacing=0 border=1 bordercolor=black>";
			// grab all info for the open projects
			$project_query = "SELECT * FROM IPM_projects WHERE id='$open[id]'";
			$result3 = mysql_query($project_query);
			while ($project = mysql_fetch_array($result3)) {
				?>
				<tr valign=top>
						<td class=header width=38%>
								PROJECT: <b><a class=headerlink href=<?=$PHP_SELF?>?op=projectdetail&id=<?=$project[id]?>><?=$project[name]?></a></b><br>
						</td>
						<td nowrap width=10% class=header align=right>	
								<?
								draw_time_and_progress_bars($project[id], "CLASS=listheader");
								?>
						</td>
						<td nowrap width=10% class=header align=center>
								[<a class=headerlink href=<?=$PHP_SELF?>?op=addtask&id=<?=$project[id]?>&name=<?$project[name]?>>ADD TASK</a>] 
								 :: [<a class=headerlink href=<?=$PHP_SELF?>?op=editproject&id=<?=$project[id]?>>EDIT</a>] 
								 :: [<a class=headerlink href=<?=$PHP_SELF?>?op=confirmprojectdelete&id=<?=$project[id]?>>DELETE</a>]
						</td>
				</tr>
				</table>
				<table width=90% align=center cellpadding=2 cellspacing=0 border=1 bordercolor=black>
				<tr class=darker>
						<td align=center width=5%>
								HOURS
						</td>
						<td align=center width=5%>
								COMMENTS
						</td>
						<td align=center width=25%>
								TITLE
						</td>
						<td align=center width=60%>
								DESCRIPTION
						</td>
						<td width=5% align=center>
								ACTION
						</td>
				</tr>
				<?
				// grab all info for tasks within this active project
				$task_query = "SELECT * FROM IPM_tasks WHERE person='$user_id' AND status='1' AND id='$project[id]' ORDER BY 'hours' DESC";
				$result4 =  mysql_query($task_query);	
				while ($task = mysql_fetch_array($result4)) {
					?>
					<tr class=lighter valign=top>
						<td align=center>
							<? if (!$task[billable]) {
								 echo $task[hours];
							} else {
								 echo "<font class=billable>$task[hours]</font>";
							} ?>
						</td>
						<td align=center>
							<? if ($task[comments]) {
								 echo "<img src=img/comment-on.gif border=0>";
							} else {
								 echo "<img src=img/comment-off.gif border=0>";
							} ?>
						</td>
						<td>
							<a href=<? echo "$PHP_SELF?op=edittask&task_id=$task[task_id]"; ?>><?=$task[title]?></a>
						</td>
						<td>
							<? echo nl2br($task[description]); ?>&nbsp;
						</td>
						<td align=center nowrap>
								<a href=<? echo "$PHP_SELF?finish=$task[task_id]"; ?>><img src=img/f-on.gif border=0></a> 
								<a href=<? echo "$PHP_SELF?op=confirmdeletetask&id=$task[id]&task_id=$task[task_id]"; ?>><img src=img/d-on.gif border=0></a>
						</td>
					</tr>
					<?
				}
				echo "</table><br>";
			}
		}
	}
	echo "<p align=center>(Hours in bold are billable)<br>";
	include ("footer.php");
}


function addtask($id, $name) {
	// This function builds the form for adding a task to a project.
	// When this form is submitted the variables are passed to the function
	// "addtaskaction()"
	authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $message;
	$query = "SELECT * FROM IPM_projects WHERE id=$id";
	$result = mysql_query($query);
	$add = mysql_fetch_array($result);
	$query2 = "SELECT * FROM IPM_users";
	$result2 = mysql_query($query2);
	$query3 = "SELECT task_id, title FROM IPM_tasks WHERE id=$id";
	$result3 = mysql_query($query3);
	?>
	<form action=<?=$PHP_SELF?> method=post>
	<table border=0 cellspacing=0 cellpadding=2 width=90% align=center>
	<tr>
			<td width=100% colspan=2 class=header align=left>
					<b>Add Task</b>
		</td>
	</tr>
	<tr>
		<td width=38% align=right class=darker>
			<b>Project Name: </b>
		</td>
		<td  class=lighter width=62%>
			<a class=biglink href=<?=$PHP_SELF?>?op=projectdetail&id=<?=$id?>><?=$add[name]?></a>
		</td>
	</tr>
	<tr>
		<td align=right class=darker>
			<b>Task Name: </b>
		</td>
		<td class=lighter>
			<input name=title type=text>
		</td>
	</tr>
	<tr>
		<td align=right class=darker>
			<b>Assigned To: </b>
		</td>
		<td class=lighter>
			<select name=person size=1>
			  <option value="1" selected>Choose a User</option>
			  <option value="1">-------------</option>
			  <? while ($users=mysql_fetch_array($result2)) {
			  	  echo "<option value=$users[id]>$users[$listdisplay]</option>";
			  }?>
			</select>
<input type=checkbox name=notify value=1 checked > Notify user?
		</td> 
  	</tr>
	<tr>
		<td align=right class=darker>
			<b>Quoted Hours: </b>
		</td>
		<td class=lighter>
			<input name=hours size=10 maxlength=10 type=text>
<input type=checkbox name=billable value=1 checked > Billable? 
		</td>
	</tr>
	<tr>
		<td align=right class=darker>
			<b>Subtask of: </b>
		</td>
		<td class=lighter>
			<select name=parent_id size=1>
			  <option value="0" selected>TOP LEVEL (none)</option>
			  <option value="0">-------------------------------------</option>
			  <? task_dropdown($id); ?>
			</select>
		</td>
	</tr>
	<tr valign=top>
		<td align=right class=darker>
			<b>Description: </b>
		</td>
		<td class=lighter>
			<textarea cols=40 rows=4 name=description></textarea>
		</td>
	</tr>
	<tr>
		<td class=header align=center colspan=2>
			<input class=button type=reset> <input class=button type=submit value="Add Task">
		</td>
	</tr>
	</table>
	<input type=hidden name=op value=addtaskaction><input type=hidden name=id value=<?=$id?>>
	</form>
	<?
	include ("footer.php");
}

function addtaskaction($id, $name, $title, $hours, $description, $person, $notify, $billable, $parent_id) {
	// This function takes the results from the function "addtask()"
	// and inserts them into the database, then displays the results 
	authenticate();
	dbconnect();
	include ("config.php");
	$result = mysql_query("INSERT INTO IPM_tasks VALUES (NULL,'$id','$title','$hours','$enddate','$description','1','$person','$billable','0','$parent_id')");
	echo mysql_error();	
	addtask($id, $name);
	if ($notify == "1") {
		 $query="SELECT email FROM IPM_users WHERE id='$person'";
		 $result=mysql_query($query);
		 $address=mysql_fetch_row($result);
		 $query2="SELECT name FROM IPM_projects WHERE id='$id'";
		 $result2=mysql_query($query2);
		 $project=mysql_fetch_row($result2);
		 $message="A new task has been assigned to you in IPM. \n\n"
		 				 ."    PROJECT:   $project[0] \n"
						 ."  TASK NAME:   $title \n"
						 ."      HOURS:   $hours \n"
						 ."DESCRIPTION:   $description \n" 
						 ."Refer to $ipmhomepage for more information. \n\n" 
						 ."Thanks"; 
		 mail("$address[0]", "A new task has been assigned to you.", "$message", "From: $ipmaddress", "-f$ipmaddress");
	}
	if ($result) {
		message("Task \"$title\" Added Successfully");
	} else {
		message("Error Adding Task");
	}
	include ("footer.php");
}

function edittask($task_id) {
	// This function build the menu for editing a pre-existing task in a project.
	// When submitted, the variables are passed to the function "edittaskaction()".
	authenticate();
	include ("config.php");
	include ("header.php");
	dbconnect();
	global $PHP_SELF, $message;
	if ($message) message("$message");
	$query = "SELECT * FROM IPM_tasks where task_id=$task_id";
	$result = mysql_query($query);
	while ($edit=mysql_fetch_array($result)) { 
		$query2 = "SELECT name FROM IPM_projects where id=$edit[id]";
		$result2 = mysql_query($query2);
		$list=mysql_fetch_array($result2);
		$query3 = "SELECT * FROM IPM_users";
		$result3 = mysql_query($query3);
		$query4 = "SELECT * FROM IPM_users WHERE id='$edit[person]'";
		$result4 = mysql_query($query4);
		$currentuser = mysql_fetch_array($result4);
		$query5 = "SELECT * FROM IPM_comments WHERE task_id='$task_id'";
		$result5 = mysql_query($query5);
		$query6 = "SELECT * FROM IPM_task_depend WHERE child_id='$task_id'";
		$result6 = mysql_query($query6);
		?>
		<form action=<?=$PHP_SELF?> method=post>
		<table cellspacing=0 cellpadding=2 border=0 bordercolor=000000 align=center width=90%>
		<tr>
			<td width=38% class=header align=left>
				<b>Edit Task</b>
			</td>
			<td width=62% class=header align=right>
					[<a class=headerlink href=<?=$PHP_SELF?>?op=addcomment&task_id=<?=$task_id?>>ADD COMMENT</a>]
			</td>
		</tr>
		<tr>
			<td width=38% align=right class=darker>
				<b>Project Name: </b>
			</td>
			<td width=62% class=lighter>
				<a class=biglink href=<?=$PHP_SELF?>?op=projectdetail&id=<?=$edit[id]?>><?=$list[name]?></a>
			</td>
		</tr>
		<tr>
			<td align=right class=darker>
				<b>Task Name: </b>
			</td>
			<td class=lighter>
				<input value='<?=$edit[title]?>' name=title size=50 maxlength=80 type=text>
			</td>
		</tr>
    	<tr>
			<td align=right class=darker>
				<b>Assigned To: </b>
			</td>
			<td class=lighter>
				<select name=person size=1>
				  <option value="<?=$edit[person]?>" selected><?=$currentuser[$listdisplay]?></option>
				  <option value="<?=$edit[person]?>">-------------</option>"
				  <? while ($users=mysql_fetch_array($result3)) {
				  	  echo "<option value=$users[id]>$users[$listdisplay]</option>";
				  }?>
				</select>
			</td>
		</tr>
		<tr>
			<td align=right class=darker>
				<b>Quoted Hours: </b>
			</td>
			<td class=lighter>
				<input type=text name=hours size=10 maxlength=10 value=<?=$edit[hours]?>>
				<?if ($edit[billable] == 1) {
						 echo "<input type=checkbox checked name=billable value=1> Billable?";
				} else {
						 echo "<input type=checkbox name=billable value=1> Billable?";
				}?>
			</td>
		</tr>
		<tr>
		<td align=right class=darker>
			<b>Subtask of: </b>
		</td>
		<td class=lighter>
			<select name=parent_id size=1>
			<?
			if ($parent_task=get_parent($task_id)) {
				$parent = get_task_info($parent_task[0]);
				echo "<option value=$parent[task_id] selected>$parent[title]</option>"
					."<option value=$parent[task_id]>-------------------------------------</option>"
					."<option value=\"0\">TOP LEVEL (none)</option>"
					."<option value=$parent[task_id]>-------------------------------------</option>";
			} else {
				echo "<option value=\"0\" selected>TOP LEVEL (none)</option>"
					."<option value=\"0\">-------------------------------------</option>";
			}			
			task_dropdown($edit[id]);
			?>
			</select>
		</td>
		</tr>
	    <tr valign=top>
			<td class=darker align=right>
				<b>Description: </b>
			</td>
			<td class=lighter>
				<textarea cols=40 rows=4 name=description><?=$edit[description]?></textarea>
			</td>
		</tr>
		</tr>
		<tr>
			<td class=header align=center colspan=2><input class=button value=Restore type=reset> <input class=button type=submit value="Update Task">
		</td>
	</tr>
	</table>
	<input type=hidden name=op value=edittaskaction>
	<input type=hidden name=id value="<?=$edit[id]?>">
	<input type=hidden name=task_id value="<?=$task_id?>">
	</form>
	<? if ($edit[comments]=="1") display_comments($task_id); 
	} // end while
	include ("footer.php");
} // end function   ...a bit confusing down here =) 

function edittaskaction($task_id, $id, $title, $hours, $description, $person, $billable, $parent_id) {
	// This function takes the variables from edittask() and makes the changes
	// to the database where neccessary, then displays the results.
	authenticate();
	dbconnect();
	global $PHP_SELF;
	include ("config.php");
	if ($parent_id == $task_id) {
		$error=1;
		$errortext.="<BR>YOU CAN NOT LINK A TASK TO ITSELF!";
	}
	$children=get_children($task_id, 2);
	for ($x=0; $x<sizeof($children); $x++) {
		if ($children[$x][0] == $parent_id) {
			$error=1;
			$errortext.="<BR>YOU CAN NOT LINK A TASK TO ONE OF ITS CHILDREN!";
		}
	}
	if (!$error) {
		$query = "UPDATE IPM_tasks SET title='$title', hours='$hours', description='$description', person='$person', billable='$billable', parent_id='$parent_id' WHERE task_id='$task_id'";
		$result = mysql_query($query);
	}
	projectdetail($id);
	if ($result) {
		 message("Task Updated Successfully");
	} else {
		 message("Error Updating Task $errortext");
	}
} 


function finishtask($task_id) {
	// This function updates a task in the database.
	// All it really does is switch status from 1 to 0.
	// Then it reloads the page you were on.
	authenticate();
	dbconnect();
	$today=date("Y-m-d");
	$query = "update IPM_tasks SET status='0', enddate='$today' WHERE task_id=$task_id";
	$result = mysql_query($query);
} 

function resettask($task_id) {
	// This function updates a task in the database.
	// All it really does is switch status from 0 to 1.
	// Then it reloads the page you were on.
	authenticate();
	dbconnect();
	$query = "update IPM_tasks SET status=1 where task_id=$task_id";
	$result = mysql_query($query);
} 

function confirmdeletetask($task_id) {
	// This function may save your ass. It pops up to make sure you want to delete
	// a task. It waits for an answer. If the answer is "yes" then the id is passed
	// to the "deletetask()" function. Otherwise you are returned to the project
	// detail page
	authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $id;
	if (get_children($task_id,2)) {
		$warn="<b>WARNING</b><br>This task contains child tasks. Deleting it will delete all children as well!<BR>";
	}
	$query = "SELECT title FROM IPM_tasks WHERE task_id=$task_id";
	$result = mysql_query($query);
	$list = mysql_fetch_array($result);
	?>
	<table width=40% border=1 bordercolor=black cellspacing=0 cellpadding=4 align=center>
	<tr>
			<td class=header align=center>
					<? echo $warn;?>Are you sure you want to delete the task <b><?=$list[title]?></b>?
			</td>
	</tr>
	<tr>
			<td align=center class=message>
					<a class=smaller href=<?=$PHP_SELF?>?op=deletetask&task_id=<?=$task_id?>&id=<?=$id?>>[YES]</a>
	         <a href=<?=$PHP_SELF?>?op=projectdetail&id=<?=$id?>>[NO]</a>
			</td>
	</tr>
	</table>
	<?
	include ("footer.php");
} 

function deletetask($task_id) {
	// This function deletes a task flat out. You've already been warned at this point.
	authenticate();
	dbconnect();
	global $PHP_SELF, $id;
	$query = "DELETE FROM IPM_tasks WHERE task_id=$task_id";
	$result = mysql_query($query);
	$query2 = "DELETE FROM IPM_tasks WHERE parent_id=$task_id";
	$result2 = mysql_query($query2);
	include ("config.php");
	projectdetail($id);
	if ($result) {
		 message("Task Deleted");
	} else {
		 message("Error Deleting Task");
	}
}



////////// COMMENT RELATED FUNCTIONS ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

function addcomment($task_id) {
	// This function builds the form for adding a comment to a task.
	// When this form is submitted the variables are passed to the function
	// "addcommentaction()"
	authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $message;
	$query="SELECT title FROM IPM_tasks WHERE task_id='$task_id'";
	$result=mysql_query($query);
	$task=mysql_fetch_row($result);
	?>
	<form action=<?=$PHP_SELF?> method=post>
	<table border=0 cellspacing=0 cellpadding=2 width=90% align=center>
	<tr>
			<td width=100% colspan=2 class=header align=left>
					<b>Adding Comment To "<?=$task[0]?>"</b>
			</td>
	</tr>
	<tr valign=top>
		<td width=38% align=right class=darker>
			<b>Comment: </b>
		</td>
		<td width=62% class=lighter>
			<textarea cols=60 rows=10 name=comment></textarea>
		</td>
	</tr>
	<tr>
		<td class=header align=center colspan=2>
			<input class=button type=reset> <input class=button type=submit value="Add Comment">
		</td>
	</tr>
	</table>
	<input type=hidden name=op value=addcommentaction><input type=hidden name=task_id value=<?=$task_id?>>
	</form>
	<?
	include ("footer.php");
}


function addcommentaction($task_id, $comment) {
	// This function takes the results from the function "addcomment()"
	// and inserts them into the database, then displays the results 
	$user_id=authenticate();
	dbconnect();
	include ("config.php");
	$date=date("YmdHis");
	$formatted_comment=addslashes($comment);
	$flag_comments_on = mysql_query("UPDATE IPM_tasks SET comments='1' WHERE task_id='$task_id'");
	if (mysql_query("INSERT INTO IPM_comments VALUES (NULL,'$task_id','$user_id','$date','$formatted_comment')")) {
		$message="Comment Added Successfully";
	} else {
		$message="Error Adding Comment";
	}
	edittask($task_id);
}


function editcomment($comment_id, $task_id) {
	$user_id=authenticate();
	dbconnect();
	include ("config.php");
	global $PHP_SELF, $message;
	// get comment's owner
	$query="SELECT user,comment FROM IPM_comments WHERE comment_id='$comment_id'";
	$result=mysql_query($query);
	if ($owner=mysql_fetch_array($result)) {
		 $auth_user=$owner[user];
		 $formatted_comment=str_replace("<br />", "\n", $owner[comment]);
		 $formatted_comment=stripslashes($formatted_comment);
	}
	// get the user's info
	$query="SELECT * FROM IPM_users WHERE id='$user_id'";
	$result=mysql_query($query);
	if ($user=mysql_fetch_array($result)) {
		 $current_user=$user[id];
		 $current_role=$user[role];
	}
	// only let the comment owner or an admin edit a comment
	if (($auth_user == $current_user) || ($current_role=="1")) {
		 include ("header.php");
		 ?>
		 <form action=<?=$PHP_SELF?> method=post>
		 <table border=0 cellspacing=0 cellpadding=2 width=90% align=center>
		 <tr>
		 		 <td width=100% colspan=2 class=header align=left>
				 		 <b>Editing Comment</b>
				 </td>
		 </tr>
		 <tr valign=top>
		 		 <td width=38% align=right class=darker>
		 		 		 <b>Comment: </b>
		 		 </td>
		 		 <td width=62% class=lighter>
		 		 		 <textarea cols=60 rows=10 name=comment><?=$formatted_comment?></textarea>
		 		 </td>
		 </tr>
		 <tr>
		 		 <td class=header align=center colspan=2>
		 		 		 <input class=button type=reset> <input class=button type=submit value="Update Comment">
		 		 </td>
		 </tr>
		 </table>
		 <input type=hidden name=op value=editcommentaction>
		 <input type=hidden name=comment_id value=<?=$comment_id?>>
		 <input type=hidden name=task_id value=<?=$task_id?>>
		 </form>
		 <?
		 include ("footer.php");
	} else {
		 $message="You are not allowed to edit this comment";
		 edittask($task_id);
	}
}


function editcommentaction($comment_id, $comment, $task_id) {
	$user_id=authenticate();
	dbconnect();
	include ("config.php");
	// get comment's owner
	$query="SELECT user,comment FROM IPM_comments WHERE comment_id='$comment_id'";
	$result=mysql_query($query);
	if ($owner=mysql_fetch_array($result)) {
		 $auth_user=$owner[user];
	}
	// get the user's info
	$query="SELECT * FROM IPM_users WHERE id='$user_id'";
	$result=mysql_query($query);
	if ($user=mysql_fetch_array($result)) {
		 $current_user_id=$user[id];
		 $current_user=$user[$listdisplay];
		 $current_role=$user[role];
	}
	if (($auth_user==$current_user_id) || ($current_role=="1")) {
		 $date=date("M j, Y - h:i:s A");
		 $formatted_comment=addslashes($comment);
		 $formatted_comment .="<br /><br /> <i>**EDITED BY $current_user -- [$date]</i>";
		 if (mysql_query("UPDATE IPM_comments SET comment='$formatted_comment' WHERE comment_id='$comment_id'")) {
		 		$message="Comment Updated Successfully";
		 } else {
		 	 	$message="Error Updating Comment";
		 }
 		 edittask($task_id);
	} else {
		 $message="You are not allowed to edit this comment";
		 edittask($task_id);	
	}
}

function deletecomment($comment_id, $task_id) {
	$user_id=authenticate();
	dbconnect();
	include ("config.php");
	global $PHP_SELF, $message;
	// get comment's owner
	$query="SELECT user,comment FROM IPM_comments WHERE comment_id='$comment_id'";
	$result=mysql_query($query);
	if ($owner=mysql_fetch_array($result)) {
		 $auth_user=$owner[user];
		 $formatted_comment=str_replace("<br />", "\n", $owner[comment]);
		 $formatted_comment=stripslashes($formatted_comment);
	}
	// get the user's info
	$query="SELECT * FROM IPM_users WHERE id='$user_id'";
	$result=mysql_query($query);
	if ($user=mysql_fetch_array($result)) {
		 $current_user=$user[id];
		 $current_role=$user[role];
	}
	// only let the comment owner or an admin edit a comment
	if (($auth_user==$current_user) || ($current_role=="1")) {
	 	 include ("header.php");
		 $message="Are you sure you want to delete this comment?<br><br>"
		 				 ."[<a href=$PHP_SELF?op=deletecommentaction&comment_id=$comment_id&task_id=$task_id>YES</a>] -- "
						 ."[<a href=$PHP_SELF?op=edittask&task_id=$task_id>NO</a>]";
		 message($message);		
	} else {
		 $message="You are not allowed to delete this comment";
		 edittask($task_id);	 
	}
}


function deletecommentaction($comment_id, $task_id) {
	$user_id=authenticate();
	dbconnect();
	include ("config.php");
	global $PHP_SELF, $message;
	// get comment's owner
	$query="SELECT user,comment FROM IPM_comments WHERE comment_id='$comment_id'";
	$result=mysql_query($query);
	if ($owner=mysql_fetch_array($result)) {
		 $auth_user=$owner[user];
		 $formatted_comment=str_replace("<br />", "\n", $owner[comment]);
		 $formatted_comment=stripslashes($formatted_comment);
	}
	// get the user's info
	$query="SELECT * FROM IPM_users WHERE id='$user_id'";
	$result=mysql_query($query);
	if ($user=mysql_fetch_array($result)) {
		 $current_user=$user[id];
		 $current_role=$user[role];
	}
	// only let the comment owner or an admin edit a comment
	if (($auth_user==$current_user) || ($current_role=="1")) {
		 if (mysql_query("DELETE FROM IPM_comments WHERE comment_id='$comment_id'")) {
		 		$message="Comment Deleted";
		 } else {
		 	  $message="Error Deleting Comment";
		 }
		 // get number of comments left, if 0 then un-flag comments for the task
		 $query="SELECT comment_id FROM IPM_comments WHERE task_id='$task_id'";
		 $result=mysql_query($query);
		 if (!$list=mysql_fetch_array($result)) {
		 		mysql_query("UPDATE IPM_tasks SET comments='0' WHERE task_id='$task_id'");
		 }
		 edittask($task_id);
	}
}


function display_comments($task_id) {
	include "config.php";
	authenticate();
	dbconnect();
	$query = "SELECT *,DATE_FORMAT(date,'%b %e, %Y - %r')\"formatted_date\" FROM IPM_comments WHERE task_id='$task_id' ORDER BY date ASC";
	$result = mysql_query($query);
	?>
	<table cellspacing=0 cellpadding=4 border=1 bordercolor=000000 align=center width=80%>
	<tr valign=top>
			<td width=20% class=header align=left>
					<b>Comments</b>
			</td>
			<td width=80% class=header align=right>
					[<a class=headerlink href=<?=$PHP_SELF?>?op=addcomment&task_id=<?=$task_id?>>ADD COMMENT</a>]
			</td>
	</tr>
	<?
	while ($comments=mysql_fetch_array($result)) {
			$query2="SELECT * FROM IPM_users WHERE id='$comments[user]'";
			$result2=mysql_query($query2);
			$name=mysql_fetch_array($result2);
			$formatted_comments=stripslashes(nl2br($comments[comment]));
			?>
			<tr valign=top>
					<td class=darker align=center>
							<b><?=$name[$listdisplay]?></b><br>
							<?=$comments[formatted_date]?><br>
							<a href=<?=$PHP_SELF?>?op=editcomment&comment_id=<?=$comments[comment_id]?>&task_id=<?=$task_id?>><img src=img/e-on.gif border=0></a> :: 
							<a href=<?=$PHP_SELF?>?op=deletecomment&comment_id=<?=$comments[comment_id]?>&task_id=<?=$task_id?>><img src=img/d-on.gif border=0></a>
					</td>
					<td class=lighter>
							<?=$formatted_comments?>
					</td>
			</tr>
			<?
	}
	echo "</table><br><br>";
}


////////// PROJECT RELATED FUNCTIONS ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

function addproject() {
	// This function builds the form for adding a new project.
	// When this form is submitted the variables are passed to the function
	// "addprojectaction()" 
	authenticate();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF;
	$today=date('j');
	$thismonthname=date('M');
	$thismonthvalue=date('n');
	$thisyear=date('Y'); 
	?>
	<form action=<?=$PHP_SELF?> method=post>
	<table cellspacing=0 cellpadding=2 border=0 bordercolor=000000 align=center width=90%>
	<tr>
			<td colspan=2 width=100% align=left class=header>
					<b>Add New Project</b>
			</td>
	</tr>
	<tr>
			<td class=darker width=38% align=right>
					<b>Project Name: </b>
			</td>
			<td class=lighter width=62%>
					<input name=name type=text size=40>
			</td>
	</tr>
	<tr>
			<td class=darker align=right>
					<b>Start Date: </b>
			</td>
			<td class=lighter>
					<select name=startmonth size=1>
					  <option value=<?=$thismonthvalue?> selected><?=$thismonthname?></option>
						<option value=<?=$thismonthvalue?>>-----</option>
						<option value=01>Jan</option>
						<option value=02>Feb</option>
						<option value=03>Mar</option>
						<option value=04>Apr</option>
						<option value=05>May</option>
						<option value=06>Jun</option>
						<option value=07>Jul</option>
						<option value=08>Aug</option>
						<option value=09>Sep</option>
						<option value=10>Oct</option>
						<option value=11>Nov</option>
						<option value=12>Dec</option>
					</select>
					<select name=startday size=1>
						<option value=<?=$today?> selected><?=$today?></option>
					  <option value=<?=$today?>>-----</option>
						<option value=01>01</option>
						<option value=02>02</option>
						<option value=03>03</option>
						<option value=04>04</option>
						<option value=05>05</option>
						<option value=06>06</option>
						<option value=07>07</option>
						<option value=08>08</option>
						<option value=09>09</option>
						<option value=10>10</option>
						<option value=11>11</option>
						<option value=12>12</option>
						<option value=13>13</option>
						<option value=14>14</option>
						<option value=15>15</option>
						<option value=16>16</option>
						<option value=17>17</option>
						<option value=18>18</option>
						<option value=19>19</option>
						<option value=20>20</option>
						<option value=21>21</option>
						<option value=22>22</option>
						<option value=23>23</option>
						<option value=24>24</option>
						<option value=25>25</option>
						<option value=26>26</option>
						<option value=27>27</option>
						<option value=28>28</option>					
						<option value=29>29</option>
						<option value=30>30</option>
						<option value=31>31</option>					
					</select>
					<select name=startyear size=1>
						<option value=<?=$thisyear?> selected><?=$thisyear?></option>
						<option value=<?$thisyear?>>-----</option>	
						<option value=2001>2001</option>
						<option value=2002>2002</option>
						<option value=2003>2003</option>
					</select>
  		</td>
	</tr>
	<tr>
			<td class=darker align=right>
					<b>Deadline: </b>
			</td>
			<td class=lighter>
					<select name=endmonth size=1>
					  <option value=<?=$thismonthvalue?> selected><?=$thismonthname?></option>
						<option value=<?=$thismonthvalue?>>-----</option>
						<option value=01>Jan</option>
						<option value=02>Feb</option>
						<option value=03>Mar</option>
						<option value=04>Apr</option>
						<option value=05>May</option>
						<option value=06>Jun</option>
						<option value=07>Jul</option>
						<option value=08>Aug</option>
						<option value=09>Sep</option>
						<option value=10>Oct</option>
						<option value=11>Nov</option>
						<option value=12>Dec</option>
					</select>
					<select name=endday size=1>
						<option value=<?=$today?> selected><?=$today?></option>
					  <option value=<?=$today?>>-----</option>
						<option value=01>01</option>
						<option value=02>02</option>
						<option value=03>03</option>
						<option value=04>04</option>
						<option value=05>05</option>
						<option value=06>06</option>
						<option value=07>07</option>
						<option value=08>08</option>
						<option value=09>09</option>
						<option value=10>10</option>
						<option value=11>11</option>
						<option value=12>12</option>
						<option value=13>13</option>
						<option value=14>14</option>
						<option value=15>15</option>
						<option value=16>16</option>
						<option value=17>17</option>
						<option value=18>18</option>
						<option value=19>19</option>
						<option value=20>20</option>
						<option value=21>21</option>
						<option value=22>22</option>
						<option value=23>23</option>
						<option value=24>24</option>
						<option value=25>25</option>
						<option value=26>26</option>
						<option value=27>27</option>
						<option value=28>28</option>					
						<option value=29>29</option>
						<option value=30>30</option>
						<option value=31>31</option>					
					</select>
					<select name=endyear size=1>
						<option value=<?=$thisyear?> selected><?=$thisyear?></option>
						<option value=<?=$thisyear?>>-----</option>	
						<option value=2001>2001</option>
						<option value=2002>2002</option>
						<option value=2003>2003</option>
					</select>
			</td>
	</tr>
	<tr align=top>
			<td class=darker align=right>
					<b>Comments: </b>
			</td>
			<td class=lighter>
					<textarea cols=40 rows=4 name=comments></textarea>
			</td>
	</tr>
	<tr>
			<td class=header align=center colspan=2>
					<input class=button type=reset> <input class=button type=submit value="Add Project">
			</td>
	</tr>
	</table>
	<input type=hidden name=op value=addprojectaction>
	</form>
	<?
	include ("footer.php");
}

function addprojectaction($name, $startmonth, $startday, $startyear, $endmonth, $endday, $endyear, $comments) {
	// This function receives the variables sent by "addproject()"
	// then it adds the project to the database.
	authenticate();
	dbconnect();
	$startdate="$startyear.$startmonth.$startday";
	$enddate="$endyear.$endmonth.$endday";
	//Format DB input
	$Fname=addslashes("$name");
	$Fcomments=addslashes("$comments");
	$result = mysql_query("insert into IPM_projects values (NULL,'$Fname','$startdate','$enddate','$Fcomments','0','0000-00-00')");
	include ("config.php");
	addproject();
	if ($result) {
		 message("Project \"$name\" Added Successfully");
	} else {
		 message("Error Adding Project");
	}
	include ("footer.php");
}

function confirmprojectdelete($id) {
	// This function may save your ass someday. When you try to delete a project,
	// this function is called. It warns you, that the function and all associated tasks
	// are about to be deleted. Then it give the choice of continuing or going back to the
	// project list.
	authenticate();
	dbconnect();
	$query = "SELECT name FROM IPM_projects WHERE id=$id";
	$result = mysql_query($query);
	$list = mysql_fetch_array($result);
	include ("config.php");
	include ("header.php");
	global $PHP_SELF;
	?>
	<table align=center width=40% border=1 bordercolor=black cellspacing=0 cellpadding=4>
	<tr>
			<td class=header align=center>
					Are you sure you want to delete the <b><?=$list[name]?></b> project?
			</td>
	</tr>
	<tr>
			<td class=importantmessage align=center>
					ALL TASKS AND ASSOCIATED INFO WILL BE DELETED
			</td>
	</tr>
	<tr>
			<td align=center class=message>
					<a class=smaller href=<?=$PHP_SELF?>?op=deleteproject&id=<?=$id?>>[YES]</a> :: <a href=<?=$PHP_SELF?>?op=listprojects>[NO]</a>
			</td>
	</tr>
	</table>
	<?
	include ("footer.php");
} 

function deleteproject($id) {
	// We warned you. This function deletes a project and takes all of its tasks with it.
	authenticate();
	dbconnect();
	global $PHP_SELF;
	$result1 = mysql_query("delete FROM IPM_projects where id=$id");
	$result2 = mysql_query("delete FROM IPM_tasks where id=$id");
	include ("config.php");
	listprojects();
	if ($result1 && $result2) {
		 message("Project Deleted");
	} else {
		 message("Error Deleting Project");
	}
}

function editproject($id) {
	// This function builds the form for editing a pre-existing project.
	// When submitted, the variables are sent to "editprojectaction()"
	authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF;
	$query = "SELECT * FROM IPM_projects where id=$id";
  $result = mysql_query($query);
  $list = mysql_fetch_array($result);
  $startyear=substr($list[startdate], 0, 4);
	$startmonth=substr($list[startdate], 5, 2);
	$startday=substr($list[startdate], 8, 2); 
	$endyear=substr($list[enddate], 0, 4);
	$endmonth=substr($list[enddate], 5, 2);
	$endday=substr($list[enddate], 8, 2);
	$Uname=stripslashes($list[name]);
	$Ucomments=stripslashes("$list[comments]");
	?>
	<form action=<?=$PHP_SELF?> method=post>
	<table border=0 bordercolor=black cellspacing=0 cellpadding=2 width=90% align=center>
	<tr>
			<td colspan=2 class=header>
					Project Editor
			</td>
	</tr>
	<tr>
			<td width=38% align=right class=darker>
					Project Name:
			</td>
			<td class=lighter align=left width=62%>
					<input value="<?=$Uname?>" size=40 name=name type=text>
			</td>
	</tr>
	<tr>
			<td class=darker align=right>
					Start Date: 
			</td>
			<td class=lighter>
					<select name=startmonth size=1>
						<?
						// MySQL doesn't store pretty names for months. So this is my workaround.
						switch ($startmonth){
							case "01":
							$startmonthname="Jan";
							break;
							case "02":
							$startmonthname="Feb";
							break;
							case "03":
							$startmonthname="Mar";
							break;
							case "04":
							$startmonthname="Apr";
							break;
					   	case "05":
							$startmonthname="May";
							break;
							case "06":
							$startmonthname="Jun";
							break;
							case "07":
							$startmonthname="Jul";
							break;
							case "08":
							$startmonthname="Aug";
							break;
							case "09":
							$startmonthname="Sep";
							break;
							case "10":
							$startmonthname="Oct";
							break;
							case "11":
							$startmonthname="Nov";
							break;
							case "12":
							$startmonthname="Dec";
							break;
							}
							?>
						<option value=<?=$startmonth?> selected><?=$startmonthname?></option>
						<option value=<?=$startmonth?>>-----</option>
						<option value=01>Jan</option>
						<option value=02>Feb</option>
						<option value=03>Mar</option>
						<option value=04>Apr</option>
						<option value=05>May</option>
						<option value=06>Jun</option>
						<option value=07>Jul</option>
						<option value=08>Aug</option>
						<option value=09>Sep</option>
						<option value=10>Oct</option>
						<option value=11>Nov</option>
					  <option value=12>Dec</option>
					</select>
					<select name=startday size=1>
						<option value=<?=$startday?> selected><?=$startday?></option>
						<option value=<?=$startday?>>-----</option>
						<option value=01>01</option>
						<option value=02>02</option>
						<option value=03>03</option>
						<option value=04>04</option>
						<option value=05>05</option>
						<option value=06>06</option>
						<option value=07>07</option>
						<option value=08>08</option>
						<option value=09>09</option>
						<option value=10>10</option>
						<option value=11>11</option>
						<option value=12>12</option>
						<option value=13>13</option>
						<option value=14>14</option>
						<option value=15>15</option>
						<option value=16>16</option>
						<option value=17>17</option>
						<option value=18>18</option>
						<option value=19>19</option>
						<option value=20>20</option>
						<option value=21>21</option>
						<option value=22>22</option>
						<option value=23>23</option>
						<option value=24>24</option>
						<option value=25>25</option>
						<option value=26>26</option>
						<option value=27>27</option>
						<option value=28>28</option>					
						<option value=29>29</option>
						<option value=30>30</option>
						<option value=31>31</option>					
					</select>
					<select name=startyear size=1>
						<option value=<?=$startyear?> selected><?=$startyear?></option>
						<option value=<?=$startyear?>>-----</option>
						<option value=2001>2001</option>
						<option value=2002>2002</option>
						<option value=2003>2003</option>
					</select>
			</td>
	</tr>
	<tr>
			<td align=right class=darker>
					Deadline: 
			</td>
			<td class=lighter>
					<select name=endmonth size=1>
						<?
						// MySQL doesn't store pretty names for months. So this is my workaround.
						switch ($endmonth){
							case "01":
							$endmonthname="Jan";
							break;
							case "02":
							$endmonthname="Feb";
							break;
							case "03":
							$endmonthname="Mar";
							break;
							case "04":
							$endmonthname="Apr";
							break;
					   	case "05":
							$endmonthname="May";
							break;
							case "06":
							$endmonthname="Jun";
							break;
							case "07":
							$endmonthname="Jul";
							break;
							case "08":
							$endmonthname="Aug";
							break;
							case "09":
							$endmonthname="Sep";
							break;
							case "10":
							$endmonthname="Oct";
							break;
							case "11":
							$endmonthname="Nov";
							break;
							case "12":
							$endmonthname="Dec";
							break;
							}
							?>
						<option value=<?=$endmonth?> selected><?=$endmonthname?></option>
						<option value=<?=$endmonth?>>-----</option>
						<option value=01>Jan</option>
						<option value=02>Feb</option>
						<option value=03>Mar</option>
						<option value=04>Apr</option>
						<option value=05>May</option>
						<option value=06>Jun</option>
						<option value=07>Jul</option>
						<option value=08>Aug</option>
						<option value=09>Sep</option>
						<option value=10>Oct</option>
						<option value=11>Nov</option>
					  <option value=12>Dec</option>
					</select>
					<select name=endday size=1>
						<option value=<?=$endday?> selected><?=$endday?></option>
						<option value=<?=$endday?>>-----</option>
						<option value=01>01</option>
						<option value=02>02</option>
						<option value=03>03</option>
						<option value=04>04</option>
						<option value=05>05</option>
						<option value=06>06</option>
						<option value=07>07</option>
						<option value=08>08</option>
						<option value=09>09</option>
						<option value=10>10</option>
						<option value=11>11</option>
						<option value=12>12</option>
						<option value=13>13</option>
						<option value=14>14</option>
						<option value=15>15</option>
						<option value=16>16</option>
						<option value=17>17</option>
						<option value=18>18</option>
						<option value=19>19</option>
						<option value=20>20</option>
						<option value=21>21</option>
						<option value=22>22</option>
						<option value=23>23</option>
						<option value=24>24</option>
						<option value=25>25</option>
						<option value=26>26</option>
						<option value=27>27</option>
						<option value=28>28</option>					
						<option value=29>29</option>
						<option value=30>30</option>
						<option value=31>31</option>					
					</select>
					<select name=endyear size=1>
						<option value=<?=$endyear?> selected><?=$endyear?></option>
						<option value=<?=$endyear?>>-----</option>
						<option value=2001>2001</option>
						<option value=2002>2002</option>
						<option value=2003>2003</option>
					</select>
			</td>
	</tr>
	<tr valign=top>
			<td class=darker align=right>
					Comments: 
			</td>
			<td class=lighter>
					<textarea cols=40 rows=4 name=comments><?=$Ucomments?></textarea>
			</td>
	</tr>
	<tr>
			<td class=header align=center colspan=2>
					<input class=button value=Restore type=reset> <input class=button type=submit value="Update Project">
			</td>
	</tr>
	</table>
	<input type=hidden name=op value=editprojectaction><input type=hidden name=id value=<?=$id?>>
	</form>
	<?
	include ("footer.php");
}
	
function editprojectaction($id, $name, $startmonth, $startday, $startyear, $endmonth, $endday, $endyear, $comments) {
	// This function receives variables from "editproject()" and updated 
	// the project in the database. Then it tells you if it worked or not.
	authenticate();
	dbconnect();
	global $PHP_SELF;
	$startdate="$startyear-$startmonth-$startday";
	$enddate="$endyear-$endmonth-$endday";
	// Format DB input
	$Fname=addslashes("$name");
	$Fcomments=addslashes("$comments");
	//
	$result = mysql_query("update IPM_projects SET name='$Fname', startdate='$startdate', enddate='$enddate', comments='$Fcomments' where id=$id");
	include ("config.php");
	listprojects();
	$Uname=stripslashes("$name");
	if ($result) {
		 message("Project \"<?=$Uname?>\" Updated Successfully");
	} else {
		 message("Error Updating Project");
	}
}

function completedlist() {
	// This function goes to the database and generates a list of 
	// completed projects. It also builds a menu for 
	// editing/deleting/reactivating projects.
	authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $order;
	switch ($order) {
	case "nameup":
	$list_query = "SELECT id, name, completed_date, comments FROM IPM_projects WHERE completed='1' ORDER BY name ASC";
  break;
	case "namedown":
	$list_query = "SELECT id, name, completed_date, comments FROM IPM_projects WHERE completed='1' ORDER BY name DESC";
	break;
	case "datedown":
	$list_query = "SELECT id, name, completed_date, comments FROM IPM_projects WHERE completed='1' ORDER BY completed_date DESC";
	break;
	case "dateup":
	$list_query = "SELECT id, name, completed_date, comments FROM IPM_projects WHERE completed='1' ORDER BY completed_date ASC";
	break;
	default:
	$list_query = "SELECT id, name, completed_date, comments FROM IPM_projects WHERE completed='1' ORDER BY completed_date ASC";
	}
	$listresult = mysql_query($list_query);
  echo "<center>"
	."<table width=90% border=1 bordercolor=black cellspacing=0 cellpadding=4>"
		."<tr valign=top>";
			if ($order == "nameup") {
				echo "<td class=header align=left>";
				$string=popuploader('nameup', 'Sort Alphabetically <b>Z-A</b>', '<b>Project Name</b>', "$PHP_SELF?op=completedlist&order=namedown", "headerlink");
				echo "$string</td>";
			} else {
				echo "<td class=header align=left>";
				$string=popuploader('namedown', 'Sort Alphabetically <b>A-Z</b>', '<b>Project Name</b>', "$PHP_SELF?op=completedlist&order=nameup", "headerlink");
				echo "$string</td>";
			}
			if ($order == "dateup") {
				 echo "<td class=header align=center>";
				 $string=popuploader('dateup', 'Sort by date completed <b>oldest first</b>', '<b>Completed</b>', "$PHP_SELF?op=completedlist&order=datedown", "headerlink");
				 echo "$string</td>";
			} else {
				 echo "<td class=header align=center>";
				 $string=popuploader('datedown', 'Sort by date completed <b>most recent first</b>', '<b>Completed</b>', "$PHP_SELF?op=completedlist&order=dateup", "headerlink");
				 echo "$string</td>";
			}
			echo "<td class=header align=center><b>Options</b></td>"
		."</tr>"; 
	$color=true;
	while ($list_row=mysql_fetch_array($listresult))
  {
		echo "<tr valign=top>";
						if (empty($list_row[comments])) {
  						 echo "<td class=lighter align=left><b>";
							 $string=popuploader("$list_row[id]", "No Comments Available", "$list_row[name]", "$PHP_SELF?op=projectsummary&id=$list_row[id]", "biglink");
							 echo "$string</b></td>";
				  	} else {
						   echo "<td class=lighter align=left><b>";
							 $string=popuploader("$list_row[id]", "$list_row[comments]", "$list_row[name]", "$PHP_SELF?op=projectsummary&id=$list_row[id]", "biglink");
							 echo "$string</b></td>";						
						}
						echo "<td nowrap width=10% class=darker align=center>$todoitem";
					  echo prettydate($list_row[completed_date]);
						echo "</td>"
						."<td nowrap width=10% class=lighter align=center>[<a href=$PHP_SELF?op=reactivateproject&id=$list_row[id]>"
									."REACTIVATE</a>] :: [<a href=$PHP_SELF?op=confirmprojectdelete&id=$list_row[id]>"
									."DELETE</a>] :: [<a href=$PHP_SELF?op=editproject&id=$list_row[id]>EDIT</a>]</td>"
				 ."</tr>";
	}
 	echo "</table></center><br>";
	include ("footer.php");
}

function listprojects() {
	// This function goes to the database and generates a list of current projects. 
	// It also builds a menu for editing/deleting/completing projects.
	authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $order;
	switch ($order) {
	case "nameup":
	$list_query = "SELECT id, name, enddate, comments FROM IPM_projects WHERE completed='0' ORDER BY name ASC";
 	break;
	case "namedown":
	$list_query = "SELECT id, name, enddate, comments FROM IPM_projects WHERE completed='0' ORDER BY name DESC";
	break;
	case "datedown":
	$list_query = "SELECT id, name, enddate, comments FROM IPM_projects WHERE completed='0' ORDER BY enddate DESC";
	break;
	case "dateup":
	$list_query = "SELECT id, name, enddate, comments FROM IPM_projects WHERE completed='0' ORDER BY enddate ASC";
	break;
	default:
	$list_query = "SELECT id, name, enddate, comments FROM IPM_projects WHERE completed='0' ORDER BY enddate ASC";
	}
	$listresult = mysql_query($list_query);
  echo "<table width=90% border=1 bordercolor=black cellspacing=0 cellpadding=4 align=center>"
		."<tr valign=top>";
			if ($order == "nameup") {
				echo "<td class=header align=left>";
$string=popuploader('nameup', 'Sort Alphabetically <b>Z-A</b>', '<b>Project Name</b>', "$PHP_SELF?op=listprojects&order=namedown", "headerlink");
				echo "$string</td>";
			} else {
				echo "<td class=header align=left>";
$string=popuploader('namedown', 'Sort Alphabetically <b>A-Z</b>', '<b>Project Name</b>', "$PHP_SELF?op=listprojects&order=nameup", "headerlink");
				echo "$string</td>";
			}
			echo "<td width=10% class=header align=center><b>Percent Completed</b></td>";
			if ($order == "dateup") {
				 echo "<td width=10% class=header align=center>";
				 $string=popuploader('dateup', 'Sort by deadline <b>most distant first</b>', '<b>Deadline</b>', "$PHP_SELF?op=listprojects&order=datedown", "headerlink");
				 echo "$string</td>";
			} else {
				 echo "<td width=10% class=header align=center>";
				 $string=popuploader('datedown', 'Sort by deadline <b>nearest first</b>', '<b>Deadline</b>', "$PHP_SELF?op=listprojects&order=dateup", "headerlink");
				 echo "$string</td>";
			}
			echo "<td width=10% class=header align=center><b>Edit/Delete</b></td>"
		."</tr>"; 
	$color=true;
	while ($list_row=mysql_fetch_array($listresult))
  {
		echo "<tr valign=top>";
					  echo "<td class=lighter align=left><b>";
						if (empty($list_row[comments])) {
							 $string=popuploader("$list_row[id]", "No Comments Available", "$list_row[name]", "$PHP_SELF?op=projectdetail&id=$list_row[id]", "biglink");
				  	} else {
							 $string=popuploader("$list_row[id]", "$list_row[comments]", "$list_row[name]", "$PHP_SELF?op=projectdetail&id=$list_row[id]", "biglink");
						}
						echo "$string</b></td>";
						echo "<td nowrap class=darker align=right>";
draw_time_and_progress_bars($list_row[id], "");
						echo "</td>"; 
						echo "<td nowrap class=darker align=center>";
						prettydate($list_row[enddate]);
						echo "</td>"
						."<td nowrap class=lighter align=center>[<a href=$PHP_SELF?op=addtask&id=$list_row[id]&name=$list_row[name]>"
									."ADD TASK</a>] :: [<a href=$PHP_SELF?op=editproject&id=$list_row[id]>"
									."EDIT</a>] :: [<a href=$PHP_SELF?op=confirmprojectdelete&id=$list_row[id]>"
									."DELETE</a>]</td>"
				 ."</tr>";
	}
 	echo "</table></center><br>";
	include ("footer.php");
}

function projectdetail($id) {
	// This function displays everyting you need to see about a project
	// It shows the startdate, deadline, todo list, finished list, total hours for todo and finished,
	// and it calculates the percentage of the project completed. It also gives the option to
	// add/edit/delete tasks, edit/delete/complete the project, update list items, and view item descriptions.
	authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $finish, $reset, $ordertodo, $orderdone;
	if (isset($finish)) {
		finishtask($finish);
	}
	if (isset($reset)) {
		resettask($reset);
	}
	$list_project = "SELECT * FROM IPM_projects where id=$id";
	$list_result = mysql_query($list_project);
	while ($list_row=mysql_fetch_array($list_result))	{
	?>
	<table width=90% border=0 bordercolor=black align=center cellspacing=0 cellpadding=4>
	<tr>
			<td class=header>
					<b><?=$list_row[name]?><b>
			</td>
			<td align=right class=header>
					<? if (percentcomplete($id) == "100") {
						 echo "[<a class=headerlink href=$PHP_SELF?op=completeproject&id=$id>COMPLETE PROJECT</a>] :: ";
					}?>
					[<a class=headerlink href=<?=$PHP_SELF?>?op=addtask&id=<?=$list_row[id]?>&name=<?=$list_row[name]?>>ADD TASK</a>] 
					 :: [<a class=headerlink href=<?=$PHP_SELF?>?op=confirmprojectdelete&id=<?=$list_row[id]?>>DELETE</a>]
					 :: [<a class=headerlink href=<?=$PHP_SELF?>?op=editproject&id=<?=$list_row[id]?>>EDIT</a>]
			</td>
	</tr>
	<tr class=lighter>
			<td width=50%>
					<b>Project Start: </b>
			    <? echo prettydate($list_row[startdate]);?>
			</td>
			<td width=50%>
					<b>Project Deadline: </b>
					<? echo prettydate($list_row[enddate]); ?>
			</td>
	</tr>
	<tr valign=top>
			<td width=50% class=lighter>
					<table width=100% border=0 bordercolor=black cellpadding=2 cellspacing=0>
					<tr valign=top>
							<td class=darker width=90%>				
									<? if ($ordertodo == "namedown") {
										 $string=popuploader("namedown", "Sort by Item Name <b>A-Z</b>", "<b>Todo Items</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=nameup&orderdone=$orderdone", "listheader");
									} else {
										 $string=popuploader("nameup", "Sort by Item Name <b>Z-A</b>", "<b>Todo Items</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=namedown&orderdone=$orderdone", "listheader");
									}
									echo "$string";
									?>
							</td>
							<td class=darker width=10%>
									<? if ($ordertodo == "persondown") {
										 $string=popuploader("persondown", "Sort by User <b>A-Z</b>", "<b>User</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=personup&orderdone=$orderdone", "listheader"); 
									} else {
									   $string=popuploader("personup", "Sort by User <b>Z-A</b>", "<b>User</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=persondown&orderdone=$orderdone", "listheader"); 
									}
									echo $string;
									?>
							</td>
							<td class=darker width=10%>
									<? if ($ordertodo == "commentsdown") {
										 $string=popuploader("commentsdown", "Arrange Tasks by if They Have Comments", "<b>Comments</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=commentsup&orderdone=$orderdone", "listheader"); 
									} else {
									   $string=popuploader("commentsup", "Arrange Tasks by if They Have Comments", "<b>Comments</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=commentsdown&orderdone=$orderdone", "listheader"); 
									}
									echo $string;
									?>
							</td>
							<td class=darker align=center width=10%>
									<? if ($ordertodo == "hoursdown") {
										 $string=popuploader("hoursdown", "Sort by Hours <b>least first</b>", "<b>Hours</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=hoursup&orderdone=$orderdone", "listheader");
									} else {
										 $string=popuploader("hoursup", "Sort by Hours <b>most first</b>", "<b>Hours</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=hoursdown&orderdone=$orderdone", "listheader");
									}
									echo "$string";
									?>
							</td>
							<td class=darker width=10% align=center>
									<b class=listheader>Action</b>
							</td>
					</tr>
					<?switch ($ordertodo) {
							case "nameup":
							$todo_order = "title asc";
  						break;
							case "namedown":
							$todo_order = "title desc";
  						break;
							case "commentsup":
							$todo_order = "comments asc";
  						break;
							case "commentsdown":
							$todo_order = "comments desc";
  						break;
						 	case "personup":
							$todo_order = "person asc";
  						break;
							case "persondown":
							$todo_order = "person desc";
  						break;
							case "hoursup":
							$todo_order = "hours asc";
  						break;
							case "hoursdown":
							$todo_order = "hours desc";
  						break;
							default:
							$todo_order = "person asc";
					}
					// call the full TODO list for this project ordered my todo_order
					task_list($id, 1, $todo_order);
					?>
					<tr>
							<td class=todolist colspan=5>
									<hr>
							</td>
					</tr>
					<tr valign=top>
							<td class=todolist colspan=3>
									<i>Total Hours Remaining on Project</i>
							</td>
							<td class=todolist align=center>
									<? echo hours_todo($id); ?>
							</td>
							<td class=todolist>
									&nbsp;
							</td>
					</tr>
					</table><br><br>
			</td>
			<!-- END OF TODO LIST SIDE -- BEGIN FINISHED SIDE -->
			<td class=lighter>
					<table width=100% border=0 cellpadding=2 cellspacing=0>
					<tr valign=top>
							<td class=darker width=90%>				
									<? if ($orderdone == "namedown") {
										 $string=popuploader("namedown", "Sort by Item Name <b>A-Z</b>", "<b>Finished Items</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=$ordertodo&orderdone=nameup", "listheader");
									} else {
										 $string=popuploader("nameup", "Sort by Item Name <b>Z-A</b>", "<b>Finished Items</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=$ordertodo&orderdone=namedown", "listheader");
									}
									echo "$string";
									?>
							</td>
							<td class=darker width=10%>
									<? if ($ordertodo == "persondown") {
										 $string=popuploader("persondown", "Sort by User <b>A-Z</b>", "<b>User</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=$orderdone&orderdone=personup", "listheader"); 
									} else {
									   $string=popuploader("personup", "Sort by User <b>Z-A</b>", "<b>User</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=$orderdone&orderdone=persondown", "listheader"); 
									}
									echo $string;
									?>
							</td>
							<td class=darker width=10%>
									<? if ($ordertodo == "commentsdown") {
										 $string=popuploader("commentsdown", "Arrange Tasks by if They Have Comments", "<b>Comments</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=$orderdone&orderdone=commentsup", "listheader"); 
									} else {
									   $string=popuploader("commentsup", "Arrange Tasks by if They Have Comments", "<b>Comments</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=$orderdone&orderdone=commentsdown", "listheader"); 
									}
									echo $string;
									?>
							</td>
							<td class=darker align=center width=10%>
									<? if ($ordertodo == "hoursdown") {
										 $string=popuploader("hoursdown", "Sort by Hours <b>least first</b>", "<b>Hours</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=$orderdone&orderdone=hoursup", "listheader");
									} else {
										 $string=popuploader("hoursup", "Sort by Hours <b>most first</b>", "<b>Hours</b>", "$PHP_SELF?op=projectdetail&id=$id&ordertodo=$orderdone&orderdone=hoursdown", "listheader");
									}
									echo "$string";
									?>
							</td>
							<td class=darker width=10% align=center>
									<b class=listheader>Action</b>
							</td>
					</tr>
					<?switch ($orderdone) {
							case "nameup":
							$done_order = "title asc";
  						break;
							case "namedown":
							$done_order = "title desc";
  						break;
							case "commentsup":
							$done_order = "comments asc";
  						break;
							case "commentsdown":
							$done_order = "comments desc";
  						break;
						 	case "personup":
							$done_order = "person asc";
  						break;
							case "persondown":
							$done_order = "person desc";
  						break;
							case "hoursup":
							$done_order = "hours asc";
  						break;
							case "hoursdown":
							$done_order = "hours desc";
  						break;
							default:
							$done_order = "person asc";
					}					
					// call the full DONE list for this project ordered my todo_order
					task_list($id, 0, $done_order);
					?>
					<tr>
						<td class=donelist colspan=5>
                        	<hr>
                        </td>
                    </tr>
                    <tr valign=top>
                        <td class=donelist colspan=3>
                            <i>Total Hours Completed</i>
                        </td>
                        <td class=donelist align=center>
                            <? echo hours_done($id); ?>
                        </td>
                        <td class=donelist colspan=1>
                            &nbsp;
                        </td>
                    </tr>
                    </table>
            </td>
    </tr>
  	<tr valign=top>
    	<td colspan=2 class=header>
        	<? draw_time_and_progress_bars($id, "CLASS=listheader"); ?>
        </td>
    </tr>
    </table>
    <br><br>
	<?
	}
	include ("footer.php");
}

function projectsummary($id) {
	// This function will display a task summary for a finished project
	authenticate();
	dbconnect();
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $reset, $order;
	$query = "SELECT * FROM IPM_projects where id=$id";
	$result = mysql_query($query);
	while ($project=mysql_fetch_array($result))	{
	?>
	<table width=90% border=0 bordercolor=black align=center cellspacing=0 cellpadding=4>
	<tr>
			<td class=header>
					<b><?=$project[name]?><b>
			</td>
			<td align=right class=header>
		 			[<a class=headerlink href=<?=$PHP_SELF?>?op=reactivateproject&id=<?=$id?>>REACTIVATE PROJECT</a>] 
					 :: [<a class=headerlink href=<?=$PHP_SELF?>?op=editproject&id=<?=$project[id]?>>EDIT</a>]
					 :: [<a class=headerlink href=<?=$PHP_SELF?>?op=confirmprojectdelete&id=<?=$project[id]?>>DELETE</a>] 
			</td>
	</tr>
	<tr class=lighter>
			<td width=50%>
					<b>Project Start: </b>
			    <? echo prettydate($project[startdate]);?>
			</td>
			<td width=50%>
					<b>Project Deadline: </b>
					<? echo prettydate($project[enddate]); ?>
			</td>
	</tr>
	<? if ($project[comments]) {
		 ?>
		 <tr class=lighter>
			<td colspan=2>
					<b>Project Description: </b>
			    <? echo nl2br($project[comments]);?>
			</td>
		 <?
		 }
	?>
	</tr>
	<tr valign=top>
			<td colspan=2 class=lighter>
					<table width=100% border=0 bordercolor=black cellpadding=2 cellspacing=0>
					<tr valign=top>
							<td class=darker nowrap width=10%>
									<? if ($order == "persondown") {
										 $string=popuploader("persondown", "Sort by User <b>Z-A</b>", "<b>User</b>", "$PHP_SELF?op=projectsummary&id=$id&order=personup", "listheader"); 
									} else {
									   $string=popuploader("personup", "Sort by User <b>A-Z</b>", "<b>User</b>", "$PHP_SELF?op=projectsummary&id=$id&order=persondown", "listheader"); 
									}
									echo $string;
									?>
							</td>
							<td class=darker>				
									<? if ($order == "namedown") {
										 $string=popuploader("namedown", "Sort by Item Name <b>A-Z</b>", "<b>Todo Items</b>", "$PHP_SELF?op=projectsummary&id=$id&order=nameup", "listheader");
									} else {
										 $string=popuploader("nameup", "Sort by Item Name <b>Z-A</b>", "<b>Todo Items</b>", "$PHP_SELF?op=projectsummary&id=$id&order=namedown", "listheader");
									}
									echo "$string";
									?>
							</td>
							<td class=darker align=center nowrap width=10%>
									<? if ($order == "billable") {
										 $string=popuploader("billable", "Show only Billable Tasks", "<b>Billable</b>", "$PHP_SELF?op=projectsummary&id=$id&order=billable", "listheader");
									} else {
										 $string=popuploader("billable", "Show only Billable Tasks", "<b>Billable</b>", "$PHP_SELF?op=projectsummary&id=$id&order=billable", "listheader");
									}
									echo "$string";
									?>
							</td>
							<td class=darker align=center nowrap width=10%>
									<? if ($order == "hoursdown") {
										 $string=popuploader("hoursdown", "Sort by Hours <b>least first</b>", "<b>Hours</b>", "$PHP_SELF?op=projectsummary&id=$id&order=hoursup", "listheader");
									} else {
										 $string=popuploader("hoursup", "Sort by Hours <b>most first</b>", "<b>Hours</b>", "$PHP_SELF?op=projectsummary&id=$id&order=hoursdown", "listheader");
									}
									echo "$string";
									?>
							</td>
							<td class=darker align=center nowrap width=10%>
									<? if ($order == "datedown") {
										 $string=popuploader("datedown", "Sort by Completion Date <b>most recent first</b>", "<b>Completed</b>", "$PHP_SELF?op=projectsummary&id=$id&order=dateup", "listheader");
									} else {
										 $string=popuploader("dateup", "Sort by Completion Date <b>oldest first</b>", "<b>Completed</b>", "$PHP_SELF?op=projectsummary&id=$id&order=datedown", "listheader");
									}
									echo "$string";
									?>
							</td>
					</tr>
					<?switch ($order) {
						 	case "personup":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY person ASC";
  						break;
							case "persondown":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY person DESC";
  						break;
							case "nameup":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY title ASC";
  						break;
							case "namedown":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY title DESC";
  						break;
							case "billable":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id AND billable=1 ORDER BY billable";
  						break;
							case "dateup":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY enddate ASC";
  						break;
							case "datedown":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY enddate DESC";
  						break;
							case "hoursup":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY hours ASC";
  						break;
							case "hoursdown":
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY hours DESC";
  						break;
							default:
							$task_query = "SELECT * FROM IPM_tasks WHERE id=$id ORDER BY person ASC";
					}
					$task_result = mysql_query($task_query);
					$total_billable=0;
					$total_hours=0;
					while ($task=mysql_fetch_array($task_result))	{
							$user_query = "SELECT * FROM IPM_users WHERE id='$task[person]'";
							$user_result = mysql_query($user_query);
							$user = mysql_fetch_array($user_result);
							?>
							<tr valign=top>
									<td class=todolist nowrap width=10%>
											<?=$user[$listdisplay]?>
									</td>
									<td class=todolist>
											<? if ($todo[description] == "") {
								 				 	 $string=popuploader("$task[task_id]", "No Description Available", "$task[title]", "$PHP_SELF?op=edittask&task_id=$task[task_id]", "listlink");
								 			} else {
							   					 $string=popuploader("$task[task_id]", "$task[description]", "$task[title]", "$PHP_SELF?op=edittask&task_id=$task[task_id]", "listlink");
								 			}
											echo "$string";
											?>
									</td>
		 							<td align=center class=todolist nowrap width=10%>
											<? if ($task[billable]) {
												 $total_billable=$total_billable+$task[hours];
												 echo "X";
											}?>
									</td>										
		 							<td align=center class=todolist nowrap width=10%>
											<? $total_hours=$total_hours+$task[hours];
												echo $task[hours];
											?>
									</td>
		 							<td align=center class=todolist nowrap width=10%>
											<? echo prettydate($task[enddate]);?>
									</td>
							</tr>
							<?
					}
					?>
					<tr>
							<td class=todolist colspan=5>
									&nbsp;	
							</td>
					</tr>
					<tr class=darker>
							<td class=darker colspan=2>
									&nbsp;
							</td>
							<td class=headerlink align=center>
									Billable Total
							</td>
							<td class=headerlink align=center>
									Hours Total
							</td>
							<td class=darker>
									&nbsp;
							</td>
					</tr>
					<tr>
							<td class=todolist colspan=2>
									&nbsp;
							</td>
							<td align=center class=todolist>
									<?=$total_billable?>
							</td>
							<td align=center class=todolist>
									<?=$total_hours?>
							</td>
							<td class=todolist>
									&nbsp;
							</td>
					</tr>
					</table><br><br>
			</td>
	</tr>
	</table>
	</td>
	</tr>
	</table>
	<br><br>
	<?
	}
	include ("footer.php");
}

function completeproject($id) {
	// This function makes the current project complete, taking it off of the
	// main active project list.
	dbconnect();
	include ("config.php");
	$completed_date=date('Y-m-d');
	$query="update IPM_projects SET completed='1', completed_date='$completed_date' WHERE id='$id'";
	$result=mysql_query($query);
	listprojects();
}
	
function reactivateproject($id) {
	// This function reactivates a project, taking it off of the
	// completed project list.
	dbconnect();
	include ("config.php");
	$query="update IPM_projects SET completed='0' WHERE id='$id'";
	$result=mysql_query($query);
	listprojects();
}

/* NOT USED AS OF v0.8

function deadline($enddate) {
	//This function is used for color-coding the deadlines in the project list.
  //Feel free to edit the colors if you want.
  //This won't work without the date command installed (you probably have it).
  $today=date('d');
	$thismonth=date('m');
	$thisyear=date('Y');
	$endyear=substr($enddate, 0, 4);
	$endmonth=substr($enddate, 5, 2);
	$endday=substr($enddate, 8, 2);
	$monthresult=$endmonth - $thismonth;
	$yearresult=$endyear - $thisyear;
	$dayresult=$endday - $today;

	if ($yearresult == 0) {
		 if ($monthresult == 0) {
	 	 		if ($dayresult < 7) {
					 echo "<font color=FF0000 size=2 face=verdana>";
				} else {
					 echo "<font color=000000 size=2 face=verdana>";
				}
		 } elseif ($monthresult == -1) {
				echo "<font color=660000 size=2 face=verdana><done>";
		 } elseif ($monthresult == 1) {
				echo "<font color=333333 size=2 face=verdana>";
		 } elseif ($monthresult == 2) {
				echo "<font color=666666 size=2 face=verdana>";
		 } elseif ($monthresult == 3) {
				echo "<font color=999999 size=2 face=verdana>";
		 } else {
				echo "<font color=999999 size=2 face=verdana>";
		 }
} elseif ($yearresult >= 1) {
	echo "<font color=999999 size=2 face=verdana>";
}
echo prettydate($enddate);
}
*/


////////// MISCELLANEOUS FUNCTIONS ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

function prettydate($enddate) {
// This function makes the date look more human readable	
	$endyear=substr($enddate, 0, 4);
	$endmonth=substr($enddate, 5, 2);
	$endday=substr($enddate, 8, 2);
	switch ($endmonth) {
	case "01":
		$prettymonth="January";
		break;
	case "02":
		$prettymonth="February";
		break;
	case "03":
		$prettymonth="March";
		break;
	case "04":
		$prettymonth="April";
		break;
	case "05":
		$prettymonth="May";
		break;
	case "06":
		$prettymonth="June";
		break;
	case "07":
		$prettymonth="July";
		break;
	case "08":
		$prettymonth="August";
		break;
	case "09":
		$prettymonth="September";
		break;
	case "10":
		$prettymonth="October";
		break;
	case "11":
		$prettymonth="November";
		break;
	case "12":
		$prettymonth="December";
		break;
	}
	echo "$prettymonth $endday, $endyear";
}

function hours_todo($id) {
	// input project id
	// returns the ammount of hours left todo on a project
	dbconnect();
	$query = "SELECT SUM(hours) FROM IPM_tasks WHERE id='$id' AND status=1";
	$result = mysql_query($query);
	if ($row = mysql_fetch_array($result)) {
		 return $row[0];			
	}
}

function hours_done($id) {
	// input project id
	// returns the ammount of hours done on a project
	dbconnect();
	$query = "SELECT SUM(hours) FROM IPM_tasks WHERE id='$id' AND status=0";
	$result = mysql_query($query);
	if ($row = mysql_fetch_array($result)) {
		 return $row[0];			
	}
}


function percentTimeComplete($id) {
	// Use built-in MySQL functions to calculate timespan of project and see how
	// far into a project (time-wise) we are.
	// FUNCTION SUBMITTED BY: DAVID HUGHES  (David.W.Hughes@cern.ch)
	dbconnect();
	$query = "SELECT to_days(current_date)-to_days(startdate) AS elapsed, to_days(enddate)-to_days(startdate) AS total, completed FROM IPM_projects WHERE id = $id";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$elapsed = $row[elapsed];
	$total = $row[total];
	if ($total == 0) {
		 return 100;
	} else {
		 if ((100 * $elapsed / $total) == 0) {
		 		return 0;
		 } else {
		 	  return sprintf("%02d", 100 * $elapsed / $total);
		 }
	}
}

function percentcomplete($id) {
	// This function takes a project id and and compares the todo hours
	// to the finished hours to make a percent done
	dbconnect();
	$Qtodo="SELECT * FROM IPM_tasks WHERE id=$id AND status=1";
	$Qdone="SELECT * FROM IPM_tasks WHERE id=$id AND status=0";
	$Rtodo=mysql_query($Qtodo); 
	$Rdone=mysql_query($Qdone);
	$totaltodo=0;
	$totaldone=0;
	while ($todo=mysql_fetch_array($Rtodo)) 
	{
		$totaltodo=$totaltodo + $todo[hours];
	}
	while ($done=mysql_fetch_array($Rdone))
	{
		$totaldone=$totaldone + $done[hours];
	}
	$totalboth=$totaltodo+$totaldone;
	if ($totalboth > 0) {
	   $percent=100 * ($totaldone / $totalboth);
	   $percentdone = sprintf("%2d",$percent);
	   return $percentdone;
	}else{
	   return $percenttotal;
	}
}

function popuploader($id, $description, $linkname, $url, $class) {
	include "config.php";
  	echo "<a class=$class name=$id href=$url onMouseOver=\"window.status='$linkname'; show('box-$id'); return true;\" onMouseOut=\"hide('box-$id'); return true;\">$linkname</a>";
 	$res= "<DIV ID=box-$id class=hidden>\n"
       ."<table border=1 cellpadding=5 cellspacing=0 bordercolor=black width=300>"
       ."<tr>"
	   ."	<td class=popup>"
       ."   	&nbsp;&nbsp;<b>$linkname</b><br>"
       ."       <li class=popup>";
  	$res .=nl2br($description);
  	$res .=" 		</td>"
	   ."</tr>"
       ."</table>"
       ."</div>";
  	return $res;
}

function message($message) {
  // writes a pre-formated message to the screen (used for results display)
	?>
	<table align=center width=40% border=1 bordercolor=black cellspacing=0 cellpadding=4>
	<tr class=message>
			<td align=center>
					<?=$message?>
	 		</td>
	</tr>
	</table>
	<br><br>
	<?
}

function draw_time_and_progress_bars ($id, $text_class) {
	// FUNCTION SUBMITTED BY: DAVID HUGHES  (David.W.Hughes@cern.ch)
	$timedone = percentTimeComplete($id);
	$workdone = percentcomplete($id);
	echo "<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0>\n";
	$safe_td = max(0, min(100, $timedone));
	if ($completed) { $safe_td = min(100, $safe_td); }
	if ($completed || $safe_td < $workdone) {
		 // project complete or within schedule
		 $colour = "green";
	} elseif ($timedone < 100) {
		 // within deadline but behind schedule
		 $colour = "yellow";
	} else {
		 // project over deadline
		 $colour = "red";
	}
	if ($timedone <= 0) {
		$timedone="scheduled";
	} elseif ($timedone >= 100) {
		$timedone="<b>overdue</b>";
	} else {
		$timedone="$timedone%&nbsp;time";
	}
	if ($workdone <= 0) {
		$workdone="not started";
	} elseif ($workdone >= 100) {
		$workdone="<b>complete</b>";
	} else {
		$workdone="$workdone%&nbsp;work";
	}
	echo "<TR><TD ALIGN=left $text_class>$timedone</TD><TD>&nbsp;</TD><TD>", drawbar_colour($colour,$safe_td), "</TD></TR>\n";
	echo "<TR><TD ALIGN=left $text_class>$workdone</TD><TD>&nbsp;</TD><TD>", drawbar_colour("green",$workdone), "</TD></TR>\n";
	echo "</TABLE>\n";
}

function drawbar_colour ($colour, $percent) {
	// This function takes the integer (from 0-100) $percent as an argument
	// to build a bargraph using small images. It doesn't depend on the gd lib.
	// safety: normalise $percent if it's not between 0 and 100.
	// FUNCTION SUBMITTED BY: DAVID HUGHES  (David.W.Hughes@cern.ch)

	$percent = max(0, min(100, $percent));
	$l_colour = ($percent == 0   ? "grey" : $colour);
	$r_colour = ($percent == 100 ? $colour : "grey");
	// This is a hack to avoid bad browser behaviour from using <img width=0>
	if ($percent == 0  ) { $percent = 1; }
	if ($percent == 100) { $percent = 99; }
	echo "<img src=img/bars/bar-left-$l_colour.gif border=0 height=12 width=5>";
	echo "<img src=img/bars/bar-tile-$l_colour.gif border=0 height=12 width=", ($percent*2), ">";
	echo "<img src=img/bars/bar-tile-$r_colour.gif border=0 height=12 width=", (200 - $percent*2), ">";
	echo "<img src=img/bars/bar-right-$r_colour.gif border=0 height=12 width=5>\n";
}


function dbconnect() {
	// This function makes a connection to the SQL server and grabs the appropriate
	// database for use in all the other functions.
	include ("config.php");
	if (!$db=mysql_connect($db_servername, $db_username, $db_password)) {
		echo "ERROR! UNABLE TO CONNECT TO DATABASE";
	}
	if (!mysql_select_db($db_dbname)) {
		echo "ERROR! UNABLE TO FIND $db_dbname DATABASE";
	}
}

function logout() {
	// Terminate session
	unset($GLOBALS['uid']);
  setcookie("uid");
  login();
}

function unauthorized() {
	// If you hit cancel on the authentication screen or you fail 3 times
	// this function is called and displays a message.
	include ("config.php");
	echo "<html><head><link rel=stylesheet type=text/css href=$stylesheet_url>"
			 ."<title>$page_title$version</title></head><body><br><br>";
	message("$unauthorizedmessage"); 
	echo "</body></html>";
}

function logged_out() {
	// If you hit cancel on the authentication screen or you fail 3 times
	// this function is called and displays a message.
	include ("config.php");
	echo "<html><head><link rel=stylesheet type=text/css href=$stylesheet_url>"
			 ."<title>$page_title$version</title></head><body><br><br>";
	message("You are now logged out of IPM"); 
	echo "</body></html>";
}
	
function login() { 
	include ("config.php");
	include ("header.php");
	global $PHP_SELF, $finish;
	// If you hit cancel on the authentication screen or you fail 3 times
	// this function is called and displays a message.
	include ("config.php");
	echo "<html><head><link rel=stylesheet type=text/css href=$stylesheet_url>"
			 ."<title>$page_title$version</title></head><body><br><br>";
	?>
	<form action=<?=$PHP_SELF?> method=post>
  <table width=90% align=center cellpadding=2 cellspacing=0 border=1 bordercolor=black>
	<tr>
			<td colspan=2 class=header>
					User Login
			</td>
	</tr>
	<tr>
			<td width=38% align=right class=darker>
					User Name:
			</td>
			<td class=lighter align=left width=62%>
					<input size=40 name=username type=text>
			</td>
	</tr>
	<tr>
			<td width=38% align=right class=darker>
					Password:
			</td>
			<td class=lighter align=left width=62%>
					<input size=40 name=password type=password>
			</td>
	</tr>
	<tr>
			<td class=header align=center colspan=2>
					<input class=button type=submit value="Login">
			</td>
	</tr>
  </table>
	<input type=hidden name=op value=loginaction>
  </form>
  <?
	echo "</body></html>";
}

function loginaction($username, $password) {
  global $uid;
  dbconnect();
  $query = "SELECT * FROM IPM_users WHERE username='$username'"; 
  if (!($result = mysql_query($query))) { 
  	die("Couldn't retrieve authorization data, please try again later");
  } 
  if (mysql_num_rows($result) > 0) {
  	if ($user=mysql_fetch_array($result)) {
  	  if (md5($password) == $user[password]) {
        ($SECURE_COOKIES == "true")? $SSL=1: $SSL=0;
        setcookie("uid", $user[id], "0", "", "", $SSL);
        $uid = $user[id];
        mytasks();
      } else {
        login();
      }
    }
  } else { 
    login(); 
  }			
}

function authenticate() {
  global $uid;
  return $uid;
} 

function isadmin() {
	// Returns true if the logged in user has admin privleges.
	include ("config.php");
	dbconnect();
	$user_id=authenticate();
	$query="SELECT role FROM IPM_users WHERE id='$user_id'";
	$result=mysql_query($query);
	while ($list=mysql_fetch_row($result)){
			if ($list[0] == 1) {
				 return 1;
			} else {
				 return;
			}
	}
}

function isauthenticated() {
    global $uid;
    return !empty($uid);
}