Location: PHPKode > projects > Care For Kids > careforkids/user/content.inc.php
<script language="javascript" type="text/javascript">
<!--
	function titlecaps()
	{
	
		  UpperCase(document.getElementById("lastname").value,"last");
		  UpperCase(document.getElementById("firstname").value,"first");
	}	
		
	function UpperCase(name,type)
	{
		var htext, nhtext;
		
		htext = name;
			
		htext = htext.toLowerCase();
		// Just in case they're all caps.
		j = htext.length;
		nhtext = "";
		
		for(i = 0;i < j; i++)
		{
			if(i == 0)
			// To capitalize the first character.
			{
				nhtext=nhtext+htext.substr(i,1).toUpperCase();
			}
			else if (htext.charAt(i) == " ")
			{
				// Checks for the appearance of the space character.
				nhtext = nhtext + htext.substr(i,1);
				// Adds that space character to the string.
				nhtext = nhtext + htext.substr(++i,1).toUpperCase();
				// Capitalizes and adds the next character to the string.
			}
			else if (htext.charAt(i) == "")
			{
				// Checks for the appearance of the newline
				nhtext = nhtext + htext.substr(i,1);
				// Adds the newline character to the string.
				nhtext = nhtext + htext.substr(++i,1).toUpperCase();
				// Capitalizes and adds the next character to the string.
			}
			else
			{
				nhtext = nhtext + htext.substr(i,1);
				// Adds the character in a normal way.
			}
		}
		if (type == "first")
		{
			document.getElementById("firstname").value=nhtext;
		}
		else if (type == "last")
		{
			document.getElementById("lastname").value=nhtext;
		}
	}

	function password_confirm()
	{
		if (document.getElementById("password").value == "")
		{
			alert("Please enter a password.")
			document.getElementById("password").focus();
		}
		else if (document.getElementById("confirmpass").value == "")
		{
			alert("Please enter a password confirmation.")
			document.getElementById("confirmpass").focus();
		}
		else if (document.getElementById("password").value != document.getElementById("confirmpass").value)
		{
			alert("You have entered two different passwords." + "\n" + "Please re-enter the password and the confirmation password.");
			document.getElementById("password").value = "";
			document.getElementById("confirmpass").value = "";
			document.getElementById("password").focus();
		}
		else if (document.getElementById("password").value.length < 4)
		{
			alert("The minimum length for a password is four characters." + "\n" + "Please enter a new password.");
			document.getElementById("password").value = "";
			document.getElementById("confirmpass").value = "";
			document.getElementById("password").focus();
		}
		else
		{
			return true;
		}
	}
	
	function homephone()
	{
		numberCheck('homeno');
	}
	
	function workphone()
	{
		numberCheck('workno');
	}
	
	function cellphone()
	{
		numberCheck('cellno');
	}
	
	function numberCheck(type)
	{
	   var ValidChars = "0123456789";
	   var Char;
	   
		var $new="";
		if ( type == "workno")
		{
			var $num = document.getElementById("workno").value;
		}
		else if ( type == "homeno")
		{
			var $num = document.getElementById("homeno").value;
		}
		else if ( type == "cellno")
		{
			var $num = document.getElementById("cellno").value;
		}
		
		var i= 0;
		do 
		{ 
			Char = $num.charAt(i); 
			if (ValidChars.indexOf(Char) != -1 ) 
			{
				$new = $new+Char; 
			}
		  	i++;
		 }
		while  (i < $num.length ) 
		if ( type == "workno")
		{
			document.getElementById("workno").value = $new;
		}
		else if ( type == "homeno")
		{
			document.getElementById("homeno").value = $new;
		}
		else if ( type == "cellno")
		{
			document.getElementById("cellno").value = $new;
		}	  
	 }
 
	function user_form_confirm()
	{
		// Checks the user form
		if (document.getElementById("username").value == "")
		{
			alert("Please enter a user name.")
			document.getElementById("username").focus();
		}
		else if (document.getElementById("firstname").value == "")
		{
			alert("Please enter a first name.")
			document.getElementById("firstname").focus();
		}
		else if (document.getElementById("lastname").value == "")
		{
			alert("Please enter a surname.")
			document.getElementById("lastname").focus();
		}
		else
		{
			var check = password_confirm();
			if (check == true)
			{
				document.getElementById("user_form").submit();
			}
		}
	}
	
	function user_form_edit_confirm()
	{
		// Checks the user form
		if (document.getElementById("username").value == "")
		{
			alert("Please enter a user name.")
			document.getElementById("username").focus();
		}
		else if (document.getElementById("firstname").value == "")
		{
			alert("Please enter a first name.")
			document.getElementById("firstname").focus();
		}
		else if (document.getElementById("lastname").value == "")
		{
			alert("Please enter a surname.")
			document.getElementById("lastname").focus();
		}
		else
		{
			document.getElementById("user_form").submit();
		}
	}
	
	function user_form_edit_activate()
	{
		document.getElementById("update").disabled = false;
		document.getElementById("edit_details").disabled = true;
		document.getElementById("delete").disabled = true;
		document.getElementById("password_change").disabled = true;
		document.getElementById("discard").disabled = false;
		document.getElementById("canceladd").disabled = true;
		
		//document.getElementById("username").readonly = false;
		document.getElementById("firstname").readOnly = false;
		document.getElementById("lastname").readOnly = false;
		document.getElementById("homeno").readOnly = false;
		document.getElementById("workno").readOnly = false;
		document.getElementById("cellno").readOnly = false;
		document.getElementById("email").readOnly = false;
		
		document.getElementById("accesslevel").disabled = false;
		if (document.getElementById("accesslevel").value == 2) // if the dude is a remote user
		{
			document.getElementById("ovcselect").disabled = false;
		} // otherwise leave the group disabled
		document.getElementById("interface").disabled = false;
	}
	
	function confirm_discard()
	{
		var conf = confirm("Are you sure you want to discard your changes?");
		if (conf == true) // if user confirms, then reload the edit page
		{ // switch to php to generate the correct userid to load the viewUser section with ...
			<? echo "document.location='".$_SESSION['base_url']."/user/index.php?action=viewUser&userid=".$_REQUEST['userid']."'"; ?>
		} // otherwise do nothing
	}
	
	function ovcSelect()
	{
		var x = document.getElementById("ovcselect");
		var y = document.getElementById("ovcselect_all");
		if (document.getElementById("accesslevel").value == '2')
		{
			x.disabled = false;
			y.disabled = true;
			y.selected = false;
		}
		else
		{
			x.disabled = true;
			y.disabled = false;
			y.selected = true;
		}
	}

	function password_edit_confirm()
	{
		var check = password_confirm();
		if (check == true)
		{
			document.getElementById("password_form").submit();
		}
	}
	
	function delete_form_confirm(name)
	{
		var is_confirmed = confirm("Delete user "+name+"?");
		if (is_confirmed)
		{
			document.getElementById("delete_form").submit();
		}
	}
	
	function password_own_form_confirm()
	{
		var check = password_confirm();
		if (check == true)
		{
			if (document.getElementById("oldpassword").value == "")
			{
				alert("Please enter in your old password");
				document.getElementById("oldpassword").focus();
			}
			else
			{
				document.getElementById("password_form").submit();
			}
		}
	}

	function password_form_cancel(userid)
	{
		document.location = <?php echo "\"".$_SESSION['base_url']."/user/index.php?action=viewUser&userid=\"";?>+userid;
	}

	function password_own_form_cancel()
	{
		document.location = <?php echo "\"".$_SESSION['base_url']."/index-container.php\"";?>;
	}
	
	///removing invalid characters	
	function pass()
	{
		escapeSlash('lastname');
		escapeSlash('firstname');
		escapeSlash('username');	
	}

	function escapeSlash(type)
	{
		var inValidChars = "<>;\"\'";
		var Char;
		var $new="";
   
		if ( type == "lastname")
		{
			var $check = document.getElementById("lastname").value;
		}
		else if ( type == "firstname")
		{
			var $check = document.getElementById("firstname").value;
		}
		else if ( type == "username")
		{
			var $check = document.getElementById("username").value;
		}

		var i= 0 ;
		do 
		{ 
      		Char = $check.charAt(i); 
		 	if (inValidChars.indexOf(Char) == -1 ) 
         	{
         		$new = $new+Char; 
         	}
      		i++;
		}
		while  (i < $check.length ) 

		if ( type == "lastname")
		{
			document.getElementById("lastname").value = $new;
		}
		else if ( type == "firstname")
		{
			document.getElementById("firstname").value = $new;
		}
		else if ( type == "username")
		{
			document.getElementById("username").value = $new;
		}
 }
//-->
</script>

<?php
	include_once($_SESSION['base_dir']."/classes/user.inc.php");
	include_once($_SESSION['base_dir']."/classes/ovc_group.inc.php");
	include_once($_SESSION['base_dir']."/classes/accessLevel.inc.php");
	include_once($_SESSION['base_dir']."/classes/interface.inc.php");
	
	// Sort 2d Array
	// Limitation - Must be exactly 6 categories.
	// SYNTAX: TwoDSort(name of 2d array,name of index to be sorted, asc / desc, str / int) 
	// Not case sensitive
	function TwoDSort($arr,$sortBy,$sortOrder,$sortType)
	{
		if (strtolower($sortOrder) == "asc")
		{
			$sO = SORT_ASC;
		}
		else if (strtolower($sortOrder) == "desc")
		{
			$sO = SORT_DESC;
		}
		if (strtolower($sortType) == "str")
		{
			$sT = SORT_REGULAR;
		}
		else if (strtolower($sortType) == "int")
		{
			$sT = SORT_NUMERIC;
		}
		//Finds all the categories in the array
		$keyNames = array_keys($arr);
		$c = 0;
		// keyLeft has the chosen sort category removed
		for ($i=1;$i<count($keyNames);$i++)
		{
			if ($keyNames[$i] != $sortBy)
			{
				$keyLeft[$c] = $keyNames[$i];
				$c++;
			}
		}
		array_multisort($arr[$sortBy], $sO, $sT, $arr[$keyLeft[0]],$arr[$keyLeft[1]],$arr[$keyLeft[2]],$arr[$keyLeft[3]],$arr[$keyLeft[4]],$arr[$keyLeft[5]]);
		return $arr;
	}

	if ($_REQUEST['action'] == 'viewAll')
	// display a grid view of users, allowing the user to sort by columns or select a user to edit
	{
	  	echo "<h2>User Administration</h2>";
	  	echo "<div id='top_buttons'>";
		echo "<form class='formbuttons' name='nav_add' action='".$_SESSION['base_url']."/user/index.php' method='post'>";
		echo "<input class='button' type='submit' name='nav' value='Add New'>";
		echo "<input type='hidden' name='action' value='add'>";
		echo "</form>";

		echo "<form class='formbuttons' name='nav_help' target='help_window' action='".$_SESSION['base_url']."/help/index.php' method='get'>";
		echo "<input class='button' type='submit' name='help' value='Help' />";
		echo "<input type='hidden' name='chapter' value='useradministration' />";
		echo "<input type='hidden' name='section' value='search'>";
		echo "</form>";

		echo "</div>";
    
		// checks if a session message has been set and displays it
		if ($_SESSION['user_msg_disp'] == true)
		{
			echo "<p class='message'>".$_SESSION['user_msg']."</p>";
			$_SESSION['user_msg_disp'] = false;
		}
		// If clearText button is clicked, delete all filtering session vars so $details reverts to all record and shows only active
		if ($_REQUEST['clear'] == 'clearAll')
		{
			unset($_SESSION['userStart']);
			unset($_SESSION['userFiltered']);
			unset($_SESSION['userSorted']);
			unset($_SESSION['userSortBy']);
			unset($_SESSION['userNoShown']);
			$_SESSION['userShowAll'] = false;
		}
		if (!isset($_SESSION['userStart']))
		{
			$_SESSION['userStart'] = '0';
			$_SESSION['userDepth'] = '0';
		}
		if (!isset($_SESSION['userNoShown']))
		{
			$_SESSION['userNoShown'] = '10';
		}
		if ($_REQUEST['direction'] == 'next')
		{
			unset($_SESSION['userSorted']);
			unset($_SESSION['userSortBy']);			
			$_SESSION['userStart'] = $_SESSION['userDepth'];
		}
		if ($_REQUEST['direction'] == 'previous')
		{
			unset($_SESSION['userSorted']);
			unset($_SESSION['userSortBy']);			
			$_SESSION['userLastSortBy'] = "";
			$_SESSION['userDepth'] = $_SESSION['userStart'];
			$_SESSION['userDepth']--;
		}
		// if no sorting stipulated then set defaults - Surname Ascending
		if (!isset($_SESSION['userSortBy']))
		{
			$_SESSION['userSortBy'] = "UserName";
			$_SESSION['userLastSortBy'] = "";
			$_SESSION['userOrder'] = "ASC";
			$_REQUEST['sortType'] = "str";
		}
		$results = User::getAllUsers();
		$ovcgrp = new Group;
		$accessLevel = new AccessLevel;
		// 2d array structure
		$details = array(array("UserName" => "","FName" => "","Surname" => "","OGroup" => "","ALevel" => "","isActive" => "","userID" => ""));
		$count = 0;
		// Populating a 2d array using user info
		foreach($results as $result)
		{
			$details['UserName'][$count] = $result->userName;
			$details['FName'][$count] = $result->firstName;
			$details['Surname'][$count] = $result->lastName;
			if ($result->ovcgrpID != 0)
			{
				$ovcgrp->getGroup($result->ovcgrpID);
				$details['OGroup'][$count] = $ovcgrp->church;
			}
			else
			{
				$details['OGroup'][$count] = "All";
			}			
			$accessLevel->getAccessLevel($result->accessLevelID);		
			$details['ALevel'][$count] = $accessLevel->name;
			$details['isActive'][$count] = $result->isActive;
			$details['userID'][$count] = $result->userID;
			$count++;
		}
		$details = TwoDSort($details,'UserName','asc','str');
		// If filtering has been done, replace $details (which is all info) with filtered info
		if (isset($_SESSION['userFiltered']))
		{
			$details = $_SESSION['userFiltered'];
		}
		$_SESSION['userDetails'] = $details;
		if (count($details['UserName']) != 0)
		{
			// Form for filtering choices
			echo "<div id='search'>";
			echo "<form class='formbuttons' name='filtering' action='".$_SESSION['base_url']."/user/index.php' method='get'>";
			echo "Search for ";
			echo "<input type='text' name='filter' value=''>";
			echo " in ";
			echo "<SELECT name='category'>";
			echo "<OPTION SELECTED value='UserName'> User Name";
			echo "<OPTION value='FName'> First Name";
			echo "<OPTION value='Surname'> Surname";
			echo "<OPTION value='OGroup'> Church";
			echo "<OPTION value='ALevel'> Access Level";
			echo "</SELECT>";
			echo " &nbsp; &nbsp; Show:  ";
			if ($_SESSION['userShowAll'])
			{
				echo "<input type='checkbox' name='showAll' checked='true'>";
			}
			else
			{
				echo "<input type='checkbox' name='showAll'>";
			}
			echo " Removed Users (<img src='".$_SESSION['base_url']."/images/redFlag.gif'>)";
			echo "<br><br>";
			echo "Display ";
			$choices = array('10','30','50','100','500');
			echo "<SELECT name='numberRecords'>";
			for ($k=0;$k<5;$k++)
			{
				echo "<OPTION value='".$choices[$k]."'";
				if ($choices[$k] == $_SESSION['userNoShown'])
				{
					echo " selected='selected'";
				}
				echo ">".$choices[$k];
			}
			echo "</SELECT> records / page &nbsp; &nbsp; &nbsp; ";
			echo "<input class='button' type='submit' name='filter_button' value='Perform Search'>";
			
			echo " ";
			echo "<input type='hidden' name='action' value='filtered'>";
			echo "</form> ";
			
			// Clear All form
			echo "<form class='formbuttons' name='clearForm' action='".$_SESSION['base_url']."/user/index.php' method='get'>";
			echo "<input class='button' type='submit' name='clearText' value='Clear Results'>";
			echo "<input type='hidden' name='action' value='viewAll'>";
			echo "<input type='hidden' name='clear' value='clearAll'>";
			echo "</form>";
			echo "</div>";
	
			// Start of user info grid
			echo "<div id='main_content'>";
			echo "<table class='grid'>";
			echo "<tr>";
			echo "<th width='150'>";
			echo "<a href='".$_SESSION['base_url']."/user/index.php?action=sorting&sortBy=UserName&sortType=str'>User Name </a>";
			if ($_SESSION['userSortBy'] == 'UserName')
			{
				if ($_SESSION['userOrder'] == 'ASC')
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortUp.gif'>";
				}
				else
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortDown.gif'>";
				}
			}
			echo "</th>";
			echo "<th width='150'>";
			echo "<a href='".$_SESSION['base_url']."/user/index.php?action=sorting&sortBy=FName&sortType=str'>First Name </a>";
			if ($_SESSION['userSortBy'] == 'FName')
			{
				if ($_SESSION['userOrder'] == 'ASC')
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortUp.gif'>";
				}
				else
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortDown.gif'>";
				}
			}
			echo "</th>";
			echo "<th width='150'>";
			echo "<a href='".$_SESSION['base_url']."/user/index.php?action=sorting&sortBy=Surname&sortType=str"."'>Surname </a>";
			if ($_SESSION['userSortBy'] == 'Surname')
			{
				if ($_SESSION['userOrder'] == 'ASC')
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortUp.gif'>";
				}
				else
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortDown.gif'>";
				}
			}
			echo "</th>";
			echo "<th width='230'>";
			echo "<a href='".$_SESSION['base_url']."/user/index.php?action=sorting&sortBy=OGroup&sortType=str"."'>Church </a>";
			if ($_SESSION['userSortBy'] == 'OGroup')
			{
				if ($_SESSION['userOrder'] == 'ASC')
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortUp.gif'>";
				}
				else
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortDown.gif'>";
				}
			}
			echo "</th>";
			echo "<th width='150'>";
			echo "<a href='".$_SESSION['base_url']."/user/index.php?action=sorting&sortBy=ALevel&sortType=str"."'>Access Level </a>";
			if ($_SESSION['userSortBy'] == 'ALevel')
			{
				if ($_SESSION['userOrder'] == 'ASC')
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortUp.gif'>";
				}
				else
				{
					echo "<img src='".$_SESSION['base_url']."/images/arrowSortDown.gif'>";
				}
			}
			echo "</th>";
			echo "</tr>";
			$col = 1;
			if (!isset($_SESSION['userSorted']))
			{
				// Beginning of hitcounter logic for determining depth
				$hitCounter = 0;
				while ($hitCounter < $_SESSION['userNoShown'])
				{
					// Only want to display Active users
					if (!$_SESSION['userShowAll'])
					{
						if ($details['isActive'][$_SESSION['userDepth']] == 1)
						{
							$hitCounter++;
						}
						if ($_REQUEST['direction'] == 'previous')
						{
							$_SESSION['userDepth']--;
						}
						else
						{
							$_SESSION['userDepth']++;
						}
					}
					// Viewing all users so don't need to match check. Depth is same as number of records 
					else
					{
						$hitCounter = $_SESSION['userNoShown'];
						if ($_REQUEST['direction'] == 'previous')
						{
							$_SESSION['userDepth'] = $_SESSION['userDepth'] - $_SESSION['userNoShown'];
						}
						else
						{
							$_SESSION['userDepth'] = $_SESSION['userDepth'] + $_SESSION['userNoShown'];
						}
					}
					if ($_SESSION['userDepth'] == count($details['Surname']))
					{
						$hitCounter = $_SESSION['userNoShown'];
					}
				}
			}
			// If viewing all users and on last page then depth is simply total number of records
			if (($_SESSION['userShowAll']) && ($_SESSION['userDepth'] > count($details['Surname'])))
			{
				$_SESSION['userDepth'] = count($details['Surname']);
			}
			// If using 'previous' tab then depth is one off (due to dec at top) and start and depth values the wrong way round
			if ($_REQUEST['direction'] == 'previous')
			{
				$_SESSION['userDepth']++;
				$placeholder = $_SESSION['userDepth'];
				$_SESSION['userDepth'] = $_SESSION['userStart'];
				$_SESSION['userStart'] = $placeholder;
			}
			// Sets record set to sorted set just before outputting. After outputting sets it back to original again.
			if (isset($_SESSION['userSorted']))
			{
				$details = $_SESSION['userSorted'];
			}
			// Outputing Details
			for ($i=$_SESSION['userStart'];$i<$_SESSION['userDepth'];$i++)
			{
				if (!$_SESSION['userShowAll'])
				{			
					if ($details['isActive'][$i] == 1)
					{
						if ($col == 1)
						{
							$col = 2;
						}
						else 
						{
							$col = 1;
						}		
						echo "<tr>";
						echo "<td class='data$col'>";
						echo "<a href='".$_SESSION['base_url']."/user/index.php?action=viewUser&userid=".$details['userID'][$i]."'>";
						echo $details['UserName'][$i]."</a>";
						echo "</td>";
						echo "<td class='data$col'>";
						echo $details['FName'][$i];
						echo "</td>";
						echo "<td class='data$col'>";
						echo $details['Surname'][$i];
						echo "</td>";
						echo "<td class='data$col'>";
						echo $details['OGroup'][$i];
						echo "</td>";
						echo "<td class='data$col'>";
						echo $details['ALevel'][$i];
						echo "</td>";
						echo "</tr>";
					}
				}
				else
				{
					if ($col == 1)
					{
						$col = 2;
					}
					else 
					{
						$col = 1;
					}		
					echo "<tr>";
					echo "<td class='data$col'>";
					if ($details['isActive'][$i] == 0)
					{
						echo "<img src='".$_SESSION['base_url']."/images/redFlag.gif'>  ";
					} 
					echo "<a href='".$_SESSION['base_url']."/user/index.php?action=viewUser&userid=".$details['userID'][$i]."'>";
					echo $details['UserName'][$i]."</a>";
					echo "</td>";
					echo "<td class='data$col'>";
					echo $details['FName'][$i];
					echo "</td>";
					echo "<td class='data$col'>";
					echo $details['Surname'][$i];
					echo "</td>";
					echo "<td class='data$col'>";
					echo $details['OGroup'][$i];
					echo "</td>";
					echo "<td class='data$col'>";
					echo $details['ALevel'][$i];
					echo "</td>";
					echo "</tr>";
				}
			}
			echo "</table>";
	
			// Returns $details to 'orginal' pre-sorted state
			if (isset($_SESSION['userFiltered']))
			{
				$details = $_SESSION['userFiltered'];
			}
			else
			{
				$details = $_SESSION['userDetails'];
			}
	
			echo "<table>";
			echo "<tr>";
			echo "<td class='grid_previous'>";
			// Beginning of page checks and calculating all the numbers for "Showing X - Y of Z"
			// Checks if at the start of the record set.
			$firstPage = false;
			if (!$_SESSION['userShowAll'])
			{
				$checker = $_SESSION['userStart'];
				do
				{
					if ($checker == 0)
					{
						$firstPage = true;
					}
					$checker--;
					if ($details['isActive'][$checker] == 1)
					{
						$firstPage = false;
						$checker = -1;
					}
				}
				while ($checker >= 0);
			}
			else
			{
				if ($_SESSION['userStart'] == 0)
				{
					$firstPage = true;
				}
			}
			// Checks if at end of record set. If only showing active, assume it is the last page and then check to see if its not.
			if (!$_SESSION['userShowAll'])
			{
				$lastPage = true;
				$checker = $_SESSION['userDepth'];
				do
				{
					if ($checker == count($details['Surname']))
					{
						$checker++;
					}
					else
					{
						if ($details['isActive'][$checker] == 1)
						{
							$lastPage = false;
							$checker = count($details['Surname']);
						}
						$checker++;
					}
				}
				while ($checker < count($details['Surname']));
			}
			else
			{
				if ($_SESSION['userDepth'] >= count($details['Surname']))
				{
					$lastPage = true;
				}
			}
			// If so disables left tab
			if ($firstPage)
			{
				$_SESSION['userStartingRecord'] = 1;
			}
			else
			{
				echo "<a href='".$_SESSION['base_url']."/user/index.php?action=viewAll&direction=previous'> Previous </a>";
			}
			echo "</td>";
			echo "<td class='grid_records'>";
			// Checking size of record set
			if ($_SESSION['userShowAll'])
			{
				$totalRecords = count($details['Surname']);
			}
			else
			{
				$totalRecords = 0;
				for ($j=0;$j<count($details['Surname']);$j++)
				{
					if ($details['isActive'][$j] == 1)
					{
						$totalRecords++;
					}
				}
			}		
			// Checks if direction is previous and NOT the first page (Important)
			if (($_REQUEST['direction'] == 'previous') && (!$firstPage))
			{
				$_SESSION['userStartingRecord'] = $_SESSION['userStartingRecord'] - $_SESSION['userNoShown'];
			}
			// Direction forward
			if ($_REQUEST['direction'] == 'next')
			{
				$_SESSION['userStartingRecord'] = $_SESSION['userStartingRecord'] + $_SESSION['userNoShown'];
			}
			echo "Showing ".$_SESSION['userStartingRecord']." - ";
			// If last page then top end is obviously the total numbers of records
			if (!$lastPage)
			{
				echo $_SESSION['userStartingRecord'] + $_SESSION['userNoShown'] - 1;
			}
			else
			{
				echo $totalRecords; 		
			}
			echo " of ".$totalRecords;
			echo "</td>";
			echo "<td class='grid_next'>";
			//Checks if at the end of the record set. If so disables right tab
			if (!$lastPage)
			{
				echo "<a href='".$_SESSION['base_url']."/user/index.php?action=viewAll&direction=next'> Next </a>";
			}
			echo "</td>";
			echo "</tr>";
			echo "</table>";
			echo "</div>";
		}
		else
		{
			echo "<div id='main_content'>";
			echo "At present there are no users on record";
			echo "<br><br>";
			echo "Users can be entered using the 'Add New' Button above";
			echo "</div>";
		}
	}

	else if ($_REQUEST['action'] == 'filtered')
	{
		$filtered = array(array("UserName" => "","FName" => "","Surname" => "","OGroup" => "","ALevel" => "","isActive" => "","userID" => ""));
		$_SESSION['userNoShown'] = $_REQUEST['numberRecords'];
		$c = 0;
		$clear = "";
		$match = false;
		unset($_SESSION['userStart']);
		unset($_SESSION['userSorted']);
		unset($_SESSION['userSortBy']);
		for ($i=0;$i<count($_SESSION['userDetails']['UserName']);$i++)
		{
			// Take substring of chosen categoty at the length of the filter word and compare it to the filter choice
			if (strncasecmp($_SESSION['userDetails'][$_REQUEST['category']][$i],$_REQUEST['filter'],strlen($_REQUEST['filter'])) == 0)
			{
				$filtered['UserName'][$c] = $_SESSION['userDetails']['UserName'][$i];
				$filtered['FName'][$c] = $_SESSION['userDetails']['FName'][$i];
				$filtered['Surname'][$c] = $_SESSION['userDetails']['Surname'][$i];
				$filtered['OGroup'][$c] = $_SESSION['userDetails']['OGroup'][$i];	
				$filtered['ALevel'][$c] = $_SESSION['userDetails']['ALevel'][$i];
				$filtered['isActive'][$c] = $_SESSION['userDetails']['isActive'][$i];
				$filtered['userID'][$c] = $_SESSION['userDetails']['userID'][$i];
				$c++;
				$match = true;
			}
		}
		if (!$match)
		{
			$_SESSION['user_msg'] = "Your search did not return any results - showing all records.";
			$_SESSION['user_msg_disp'] = true;
			$clear = 'clearAll';
		}
		$_SESSION['userFiltered'] = $filtered;
		// To check track of whether or not the Show Removed userren box has been checked
		if (isset($_REQUEST['showAll']))
		{
			 $_SESSION['userShowAll'] = true;
		}
		else
		{
			$_SESSION['userShowAll'] = false;
		}
		echo "<script language = 'javascript'>document.location='".$_SESSION['base_url']."/user/index.php?action=viewAll&clear=".$clear."';</script>";
	} 
	
	else if ($_REQUEST['action'] == 'sorting')
	{
		// if session variables are set, then store the previous sortby and assign the new
		$_SESSION['userLastSortBy'] = $_SESSION['userSortBy'];
		$_SESSION['userSortBy'] = $_REQUEST['sortBy'];
		// if lastsortby and sortby are the same, then flip the value of the ASC/DESC
		if ($_SESSION['userLastSortBy'] == $_SESSION['userSortBy'])
		{
			if ($_SESSION['userOrder'] == "ASC")
			{
				$_SESSION['userOrder'] = "DESC";
			}
			else $_SESSION['userOrder'] = "ASC";
		}
		else
		{
			// otherwise, the sort must be ASC
			$_SESSION['userOrder'] = "ASC";
		}
		$sorted = array(array("UserName" => "","FName" => "","Surname" => "","OGroup" => "","ALevel" => "","isActive" => "","userID" => ""));
		$c = 0;
		// Creating smaller array out just the records in the current page section (e.g 8 - 15)
		for ($i=$_SESSION['userStart'];$i<$_SESSION['userDepth'];$i++)
		{
			$sorted['UserName'][$i] = $_SESSION['userDetails']['UserName'][$i];
			$sorted['FName'][$i] = $_SESSION['userDetails']['FName'][$i];
			$sorted['Surname'][$i] = $_SESSION['userDetails']['Surname'][$i];
			$sorted['OGroup'][$i] = $_SESSION['userDetails']['OGroup'][$i];	
			$sorted['ALevel'][$i] = $_SESSION['userDetails']['ALevel'][$i];
			$sorted['isActive'][$i] = $_SESSION['userDetails']['isActive'][$i];
			$sorted['userID'][$i] = $_SESSION['userDetails']['userID'][$i];
		}
		$sorted = TwoDSort($sorted,$_SESSION['userSortBy'],$_SESSION['userOrder'],$_REQUEST['sortType']);

		// php in its infinite wisdom shifts the indexing of the array up to 0 if there are empty spaces. So if the record 
		// set was for 8 - 15 it could be shifted up to 0 - 7. This destroys the outputting logic as all my counters are
		// based the position of the records in terms of the full 2d array. so such userSorted starts as a copy
		// of the full 2d array and then gets the current page records overwritten with sorted ones.
		$_SESSION['userSorted'] = $_SESSION['userDetails'];
		for ($i=$_SESSION['userStart'];$i<$_SESSION['userDepth'];$i++)
		{
			$_SESSION['userSorted']['UserName'][$i] = $sorted['UserName'][$c];
			$_SESSION['userSorted']['FName'][$i] = $sorted['FName'][$c];
			$_SESSION['userSorted']['Surname'][$i] = $sorted['Surname'][$c];
			$_SESSION['userSorted']['OGroup'][$i] = $sorted['OGroup'][$c];	
			$_SESSION['userSorted']['ALevel'][$i] = $sorted['ALevel'][$c];
			$_SESSION['userSorted']['isActive'][$i] = $sorted['isActive'][$c];
			$_SESSION['userSorted']['userID'][$i] = $sorted['userID'][$c];
			$c++;
		}
		echo "<script language = 'javascript'>document.location='".$_SESSION['base_url']."/user/index.php?action=viewAll&sortBy=sorting';</script>";
	}

	else if ($_REQUEST['action'] == 'add')
	{
		// Add a new user

		echo "<h2>Add User</h2>";

		// the following block of code checks if a session message has been set and displays it
		if ($_SESSION['user_msg_disp'] == true)
		{
			echo "<p class='message'>".$_SESSION['user_msg']."</p>";
			$_SESSION['user_msg_disp'] = false;
		}
		
		echo "<table class='form'>";
		echo "<form  name='user_form' id='user_form' action='".$_SESSION['base_url']."/user/index.php' method='post'>";
		echo "<th >User Details</th>";
		echo "<tr>";
		echo "<td class='data1'>User Name</td><td class='data1'><input type='text' name='username' id='username' onkeyup= 'javascript: pass();' ></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data2'>First Name</td><td class='data2'><input type='text' name='firstname' id='firstname' onblur= 'javascript: titlecaps();' onkeyup= 'javascript: pass();'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data1'>Surname</td><td class='data1'><input type='text' name='lastname' id='lastname' onblur= 'javascript: titlecaps();' onkeyup= 'javascript: pass();'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data2'>Home Telephone No.</td><td class='data2'><input type='text' name='homeno' id='homeno'  onkeyup= 'javascript: homephone();'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data1'>Work Telephone No.</td><td class='data1'><input type='text' name='workno' id='workno' onkeyup= 'javascript: workphone();'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data2'>Cell No.</td><td class='data2'><input type='text' name='cellno' id = 'cellno'  onkeyup= 'javascript: cellphone();'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data1'>Email Address</td><td class='data1'><input type='text' name='email' id='email'></td>";
		echo "</tr>";

		// use the static method in the accessLevel class to get all the names and their values out
		$results = AccessLevel::getAccessLevels();
		echo "<tr>";
		echo "<td class='data1'>User Type</td><td class='data2'><select name='accesslevel' id='accesslevel' onChange='javascript: ovcSelect();'>";
		foreach ($results as $result)
		{
			echo "<option value='".$result->accessLevelID."'>".$result->name."</option>";
		}
		echo "</select></td>";
		echo "</tr>";

		// now, use the static method in the ovcgrp class to get the ovcgroup names
		$results = Group::getAllGroups();
		echo "<tr>";
		echo "<td class='data2'>Church</td><td class='data2'><select id='ovcselect' name='ovcgrp' disabled='disabled'>";
		echo "<option id='ovcselect_all' value='0' selected='selected'>All</option>";
		foreach ($results as $result)
		{
			echo "<option value='".$result->ovcgrpID."'>".$result->church."</option>";
		}
		echo "</select></td>";
		echo "</tr>";

		// nothing new, as above
		$results = Interface::getInterfaces();
		echo "<tr>";
		echo "<td class='data2'>Interface Style</td><td class='data2'><select name='interface'>";
		foreach ($results as $result)
		{
			echo "<option value='".$result->interfaceID."'>".$result->name."</option>";
		}
		echo "</select></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data1'>Password</td><td class='data1'><input type='password' name='password' id='password'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data2'>Confirm Password</td><td class='data2'><input type='password' name='confirmpass' id='confirmpass'></td>";
		echo "</tr>";

		echo "<tr>";

    	echo "<td>";
		echo "<center>";
		//echo "<input class='button' type='button' name='adduser' value='Save' onClick='javascript: user_form_confirm();'>";
		echo "</center>";
		echo "</td>";

   		echo "<td>";
		//echo "<center>";
	    echo "<input class='button' type='button' name='adduser' value='Save' onClick='javascript: user_form_confirm();'>";
		echo "<input class='button' type='button' name='canceladd' value='Cancel' onClick='javascript: document.location=\"".$_SESSION['base_url']."/user/index.php?action=viewAll&clear=clearAll\";'>";
		echo "<input type='hidden' name='action' value='adduser'>";
		//echo "</center>";
		echo "</td>";

    	echo "</tr>";
    	echo "</form>";
		echo "</table>";
	}

	else if ($_REQUEST['action'] == 'adduser')
	{
		// check that the user name is not in the set of active users
		if (User::activeUserExists($_REQUEST['username']))
		{
			$_SESSION['user_msg'] = "Username \"" . $_REQUEST['username'] . "\" is already in use. Please choose another.";
			$_SESSION['user_msg_disp'] = true;
			
			header("Location: ".$_SESSION['base_url']."/user/index.php?action=add");
		}
		else
		{
			// create an instance of the user class
			$u = new User();
			// fill it with the new user info
			$u->setUser($_REQUEST['username'],$_REQUEST['password'],$_REQUEST['firstname'],$_REQUEST['lastname'],$_REQUEST['homeno'],$_REQUEST['workno'],$_REQUEST['cellno'],$_REQUEST['email'],$_REQUEST['ovcgrp'],$_REQUEST['accesslevel'],$_REQUEST['interface']);
			// write to the database
			$u->saveUser();
			// redirect the user to the initial grid edit page with a confirmation message
			//must check if there is a clash or not!!!!!!!!!!!!!!!
			$_SESSION['user_msg'] = "User \"".$_REQUEST['username']."\" added.";
			$_SESSION['user_msg_disp'] = true;
			header("Location: ".$_SESSION['base_url']."/user/index.php?action=viewAll&clear=clearAll");
		}

	}

	else if ($_REQUEST['action'] == 'viewUser')
	{
		$u = new User;
		$u->getUser($_REQUEST['userid']);
		
		echo "<h2>User Management</h2>";
		echo "<div id='top_buttons'>";
		echo "<input class='button' type='button' name='edit_details' id='edit_details' value='Edit Details' onclick='javascript: user_form_edit_activate();'";
		// if user is deleted then disable all the buttons
		if ($u->isActive == '0')
		{
			echo " disabled='disabled'";
		}
		echo ">";

		// if user is deleted then disable all the buttons
		echo "&nbsp;";

		echo "<input class='button' type='button' name='delete' id='delete' value='Delete' onclick='javascript:delete_form_confirm(\"".$u->userName."\");'";
		if ($u->isActive == '0')
		{
			echo " disabled='disabled'";
		}
		echo ">";
		echo "&nbsp;";
		//nick
		echo "<form class='formbuttons' name='delete_form' id='delete_form' action='".$_SESSION['base_url']."/user/index.php' method='post'>";
		//	echo "<td class='button'>";
   		echo "<input type='hidden' name='action' value='deleteuser'>";
		echo "<input type='hidden' name='userid' value='".$_REQUEST['userid']."'>";
  		echo "</form>";

  		// this will need to be layed out properly later. for now, it is just outside the table
		echo "<form class='formbuttons' name='password_form' action='".$_SESSION['base_url']."/user/index.php' method='post'>";
		echo "<input type='hidden' name='action' value='password'>";
		echo "<input type='hidden' name='userid' value='".$_REQUEST['userid']."'>";
		echo "<input class='button' type='submit' id='password_change' name='submit' value='Password'";
		if ($u->isActive == '0')
		{
			echo " disabled='disabled'";
		}
		echo ">";
		echo "</form>";
		echo "&nbsp;";

		echo "<form class='formbuttons' name='nav_help' target='help_window' action='".$_SESSION['base_url']."/help/index.php' method='get'>";
		echo "<input class='button' type='submit' name='help' value='Help' />";
		echo "<input type='hidden' name='chapter' value='useradministration' />";
		echo "<input type='hidden' name='section' value='view'>";
		echo "</form>";

		echo "<form class='formbuttons'>";
		echo "<input class='button' type='button' name='canceladd' id='canceladd' value='Exit' onClick='javascript: document.location=\"".$_SESSION['base_url']."/user/index.php?action=viewAll&clear=clearAll\";'>";
		echo "</form>";
		echo "</div>";
		if ($_SESSION['user_msg_disp'] == true)
		{
			echo "<p class='message'>".$_SESSION['user_msg']."</p>";
			$_SESSION['user_msg_disp'] = false;
		}
		echo "<div id='main_content'";
		echo "<form name='user_form' id='user_form' action='".$_SESSION['base_url']."/user/index.php' method='post'>";
		echo "<input type='hidden' name='userid' value='".$_REQUEST['userid']."'>";
		echo "<input type='hidden' name='action' value='update'>";
		echo "<br />";
    	echo "<table class='form'>";

		echo "<th>";
		if ($u->isActive == 0)
		{
			echo "<img src='".$_SESSION['base_url']."/images/redFlag.gif'>  ";
		}
		echo "".$u->lName.", ".$u->fName."";
		echo "</th>";

		if ($u->isActive == 0)
		{
			echo "<tr>";
			echo "<td class='data2'>";
			$date_arr = explode("-",$u->dateDeleted);
			$dateDeleted = $date_arr[2]."-".$date_arr[1]."-".$date_arr[0];
			echo "<b> <center> Deleted on ".$dateDeleted."</center> </b>";
			echo "</td>";
			echo "</tr>";
		}
		
		echo "<tr>";
		echo "<td class = 'data1'>User Name</td><td class ='data1'><input type='text' name='username' id='username'value='".$u->userName."' readonly='readonly' onkeyup= 'javascript: pass();'></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data2'>First Name</td><td class='data2'><input type='text' name='firstname' id='firstname' value='".$u->fName."' readonly='readonly' onblur= 'javascript: titlecaps();' onkeyup= 'javascript: pass();' ></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data1'>Surname</td><td class='data1'><input type='text' name='lastname' id='lastname' value='".$u->lName."' readonly='readonly' onblur= 'javascript: titlecaps();' onkeyup= 'javascript: pass();'></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data2'>Home Telephone No.</td><td class='data2'><input type='text' name='homeno' id='homeno' value='".$u->homeTel."' readonly='readonly' onkeyup= 'javascript: homephone();'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data1'>Work Telephone No.</td><td class='data1'><input type='text' name='workno' id='workno' value='".$u->workTel."' readonly='readonly'  onkeyup= 'javascript: workphone();'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data2'>Cell No.</td><td class='data2'><input type='text' name='cellno' id='cellno' value='".$u->cellNo."'  readonly='readonly'  onkeyup= 'javascript: cellphone();' ></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data1'>Email Address</td><td class='data1'><input type='text' name='email' id='email' value='".$u->email."' readonly='readonly'></td>";
		echo "</tr>";

		// use the static method in the accessLevel class to get all the names and their values out
		$results = AccessLevel::getAccessLevels();
		echo "<tr>";
		echo "<td class='data1'>User Type</td><td class='data2'><select id='accesslevel' name='accesslevel' onChange='javascript: ovcSelect();' disabled='disabled'>";
		foreach ($results as $result)
		{
			echo "<option value='".$result->accessLevelID."'";
			if ($u->aID == $result->accessLevelID)
			{
				echo " selected='selected'";
			}
			echo ">".$result->name."</option>";
		}
		echo "</select></td>";
		echo "</tr>";

		// now, use the static method in the ovcgrp class to get the ovcgroup names
		//note that this one is slightly different from the add user above because it must correctly set the ovcgrp
		// if a remote site user
		$results = Group::getAllGroups();
		echo "<tr>";
		echo "<td class='data2'>Church</td><td class='data2'>";
		if ($u->aID != '2') // if the user is not a remote site user select the all field
		{
			echo "<select id='ovcselect' name='ovcgrp' disabled='disabled'>";
			echo "<option id='ovcselect_all' value='0' selected='selected'>All</option>";

			foreach ($results as $result)
			{
				echo "<option value='".$result->ovcgrpID."'>".$result->church."</option>";
			}
		}
		else // otherwise disabled the all field and select the correct ovcgrp for him
		{
			echo "<select id='ovcselect' name='ovcgrp' disabled='disabled'>"; // will be disabled initially until edit clicked
			echo "<option id='ovcselect_all' value='0' disabled='disabled''>All</option>";

			foreach ($results as $result)
			{
				echo "<option value='".$result->ovcgrpID."'";
				if ($u->ovcgrpID == $result->ovcgrpID)
				{
					echo " selected='selected'";
				}
				echo ">".$result->church."</option>";
			}
		}
		echo "</select></td>";
		echo "</tr>";

		// nothing new, as above
		$results = Interface::getInterfaces();
		echo "<tr>";
		echo "<td class='data2'>Interface Style</td><td class='data2'><select name='interface' id='interface' disabled='disabled'>";
		foreach ($results as $result)
		{
			echo "<option value='".$result->interfaceID."'";
			if ($u->interfaceID == $result->interfaceID)
			{
				echo " selected='selected'";
			}
			echo ">".$result->name."</option>";
		}
		echo "</select></td>";
		echo "</tr>";

		echo "<tr>";

    	echo "<td>";
		echo "<center>";
		//echo "<input class='button' type='button' name='update' id='update' value='Save' onclick='javascript: user_form_edit_confirm();' disabled='disabled'>";
		echo "</center>";
		echo "</td>";

		echo "<td>";
		//echo "<center>";
   		echo "<input class='button' type='button' name='update' id='update' value='Save' onclick='javascript: user_form_edit_confirm();' disabled='disabled'>";
		echo "<input class='button' type='button' name='discard' id='discard' value='Discard Changes' onClick='javascript: confirm_discard();' disabled='disabled'>";
		//echo "</center>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";
		echo "</form>";
		echo "</div>";
	}

	else if ($_REQUEST['action'] == 'update')
	{
		// create an instance of the user class
		$u = new User();
		// fill it with the info corresponding to the given user id
		$u->getUser($_REQUEST['userid']);
		// set the new values
		$u->fName = $_REQUEST['firstname'];
		$u->lName = $_REQUEST['lastname'];
		$u->aID = $_REQUEST['accesslevel'];
		$u->ovcgrpID = $_REQUEST['ovcgrp'];
		$u->interfaceID = $_REQUEST['interface'];
		$u->homeTel = $_REQUEST['homeno'];
		$u->workTel = $_REQUEST['workno'];
		$u->cellNo = $_REQUEST['cellno'];
		$u->email = $_REQUEST['email'];
		// write to the database
		$u->updateUser();
		// redirect the user to the initial grid edit page with a confirmation message
		$_SESSION['user_msg'] = "User updated.";
		$_SESSION['user_msg_disp'] = true;
		header("Location: ".$_SESSION['base_url']."/user/index.php?action=viewUser&userid=".$_REQUEST['userid']);
	}

	else if ($_REQUEST['action'] == "deleteuser")
	{
		$u = new User();
		$u->getUser($_REQUEST['userid']);
		$u->isActive = '0';
		//set today's date as date deleted
		$u->dateDeleted = date('Y-m-d');
		$u->updateUser();
		// redirect the user to the initial grid select delete page with a confirmation message
		$_SESSION['user_msg'] = $u->userName." deleted.";
		$_SESSION['user_msg_disp'] = true;

		header("Location: ".$_SESSION['base_url']."/user/index.php?action=viewAll&clear=clearAll");
	}

	else if ($_REQUEST['action'] == "password")
	{	
		$u = new User();
		$u->getUser($_REQUEST['userid']);

		echo "<h2>Change Password</h2>";
		echo "<table>";
		echo "<th>".$u->lName.", ".$u->fName."</th>";
		echo "<form name='password_form' id='password_form' action='".$_SESSION['base_url']."/user/index.php' method='post'>";

		echo "<tr>";
		echo "<td class='data1'>New Password</td><td class='data1'><input type='password' id='password' name='password'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data2'>Confirm Password</td><td class='data2'><input type='password' id='confirmpass' name='confirmpass'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='button'>";
		echo "<input type='hidden' name='action' value='changepassword'>";
		echo "<input type='hidden' name='userid' value='".$_REQUEST['userid']."'>";
		echo "<input type='button' name='add' value='Change' onClick='javascript: password_edit_confirm();'>";
		echo "</td>";
		echo "<td class='button'>";
		echo "<input type='button' name='cancelpass' value='Cancel' onClick='javascript:password_form_cancel(\"".$_REQUEST['userid']."\");'>";
		echo "</form>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";
	}

	else if ($_REQUEST['action'] == 'changepassword')
	{
		$u = new User();
		$u->getUser($_REQUEST['userid']);
		$u->setPassword($_REQUEST['password']);
		$u->updateUser();
		
		$_SESSION['user_msg'] = "Password changed.";
		$_SESSION['user_msg_disp'] = true;

		header("Location: ".$_SESSION['base_url']."/user/index.php?action=viewUser&userid=".$_REQUEST['userid']);
	}
	
	else if ($_REQUEST['action'] == 'change_my_password')
	{
		$u = new User();
		$u->getUser($_REQUEST['userid']);

		if (md5($_REQUEST['oldpassword']) != $u->password)
		{
			$_SESSION['mysettings_msg'] = "Password change failed. Wrong old password supplied.";
			$_SESSION['mysettings_msg_disp'] = true;

			header("Location: ".$_SESSION['base_url']."/user/index.php?action=mysettings");
/*
			check that password matches, if so, continue, else redirect with a change failed message
			echo "<script language='javascript'>document.location='".$_SESSION['base_url']."/user/index.php?action=password&msg=Wrong old password given'</script>";
*/
		}
		else
		{
			$u->setPassword($_REQUEST['password']);
			$u->updateUser();
			
			$_SESSION['mysettings_msg'] = "Your password has been changed. Changes will take affect next time you log on.";
			$_SESSION['mysettings_msg_disp'] = true;

			header("Location: ".$_SESSION['base_url']."/user/index.php?action=mysettings");
		}
	}

	else if ($_REQUEST['action'] == 'mysettings')
	{
		$u = new User;
		$u->getUser($_SESSION['userid']);
		//Display user's details

		echo "<h2>My Settings</h2>";

		// check that the message hasn't been displayed before
		if ($_SESSION['mysettings_msg_disp'] == true)
		{
			echo "<p>".$_SESSION['mysettings_msg']."</p>";
			$_SESSION['mysettings_msg_disp'] = false;
		}
		echo "<div id='top_buttons'>";
		echo "<form class='formbuttons' name='nav_help' target='help_window' action='".$_SESSION['base_url']."/help/index.php' method='get'>";
		echo "<input class='button' type='submit' name='help' value='Help' />";
		echo "<input type='hidden' name='chapter' value='mysettings' />";
	//	echo "<input type='hidden' name='section' value=''>";
		echo "</form>";
		echo "</div>";
		echo "<div id='main_content'>";
  		echo "<table>";
    	echo "<th width='250'>";
		echo "Details:";
		echo "</th>";
		//echo "<form name='edit_form' action='".$_SESSION['base_url']."/user/index.php' method='post'>";
		echo "<tr>";
		echo "<td class = 'data1'>User Name</td><td class ='data1'>".$u->userName."</td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data2'>First Name</td><td class='data2'>".$u->fName."</td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data1'>Surname</td><td class='data1'>".$u->lName."</td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data2'>User Type</td><td class='data2'>";
		$al = new AccessLevel();
		$al->getAccessLevel($u->aID);
		echo $al->name."</td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data1'>Access Level Description</td><td class='data1'>".$al->description."</td>";
		echo "</tr>";
    	echo "<tr>";
		echo "<td class='data2'>OVC Group</td><td class='data2'>";
		if ($u->ovcgrpID == '0')
		{
			echo "All";
		}
		else
		{
			$g = new Group();
			$g->getGroup($u->ovcgrpID);
			echo $g->community;
		}
		echo "</td>";
		echo "</tr>";
		echo "</table>";

		echo "<br />";

		echo "<table >";
		echo "<form name='stylesheet_form' action='".$_SESSION['base_url']."/user/index.php' method='post'>";
    	echo "<th width='250'>";
		echo "Interface style:";
		echo "</th>";

		echo "<tr>";
		echo "<td class='data2'>My interface style:</td>";
		echo "<td class='data2' width = '200'>";
		echo "<select name='interface' width = '200'>";
		$results = Interface::getInterfaces();

		foreach ($results as $result)
		{
			echo "<option value='".$result->interfaceID."'";
			if ($u->interfaceID == $result->interfaceID)
			{
				echo " selected='selected'";
			}
			echo ">".$result->name."</option>";
		}
		echo "</select></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td colspan='2'>";
		echo "<input type='hidden' name='action' value='changestyle'>";
			// instead of using the specified userid, we get the user's id from the session var so cunningly set
			// in the login page
		echo "<input type='hidden' name='userid' value='".$_SESSION['userid']."'>";
		echo "<input class='button' type='submit' name='submit' value='Change'>";
			//echo "<input type='button' name='cancelpass' value='Cancel' onClick='javascript: password_own_form_cancel();'>";
		echo "</form>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";

		echo "<br />";

      	//Allow user to change his/her passoword
		echo "<table >";
		echo "<form name='password_form' id='password_form' action='".$_SESSION['base_url']."/user/index.php' method='post'>";
    	echo "<th width = '250'>";
		echo "Change password:";
		echo "</th>";
		echo "<tr>";
		echo "<td class='data1' >Old Password</td><td class='data1' width = '200'><input type='password' id='oldpassword' name='oldpassword' width = '200'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data2'>New Password</td><td class='data2'><input type='password' id='password' name='password'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data1'>Confirm Password</td><td class='data1'><input type='password' id='confirmpass' name='confirmpass'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td colspan='2'>";
		echo "<input type='hidden' name='action' value='change_my_password'>";
			// instead of using the specified userid, we get the user's id from the session var so cunningly set
			// in the login page
		echo "<input type='hidden' name='userid' value='".$_SESSION['userid']."'>";
		echo "<input class='button' type='button' name='add' value='Change' onClick='javascript: password_own_form_confirm();'>";
			//echo "<input type='button' name='cancelpass' value='Cancel' onClick='javascript: password_own_form_cancel();'>";
		echo "</form>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";
		echo "</div>";
	}

	else if ($_REQUEST['action'] == 'changestyle')
	{
		//save new stylesheet in database
    	$u = new User();
		$u->getUser($_REQUEST['userid']);
		$u->interfaceID = $_REQUEST['interface'];
		$u->updateUser();
		//write new stylesheet to curent session
		$_SESSION['stylesheet'] = Interface::getStylePath($u->interfaceID);
		$_SESSION['stylename'] = Interface::getStyleName($u->interfaceID);

		//redirect with message
		$_SESSION['mysettings_msg'] = "Your interface style has been changed to ".$_SESSION['stylename'].".";
		$_SESSION['mysettings_msg_disp'] = true;

		header("Location: ".$_SESSION['base_url']."/user/index.php?action=mysettings");
	}

	else
	{
		echo "<p class='error'> Application Error: Please contact your system administrator.";
	}
?>
Return current item: Care For Kids