Location: PHPKode > projects > Care For Kids > careforkids/service/content.inc.php
<script language="javascript" type="text/javascript">
<!--/
		
	function service_form_confirm()
	{
						
		UpperCase();
		var $sCode = document.getElementById("servicecode").value;
		// Checks the service form
		if (document.getElementById("name").value == "")
		{
			alert("Please enter a name for the Service.")
			document.getElementById("name").focus();
		}
		
		else if ($sCode == "" || $sCode.length != 3)
		{
			alert("Please enter a valid Service Code.")
			document.getElementById("servicecode").focus();
		}
		else if (document.getElementById("description").value == "")
		{
			alert("Please enter a description.")
			document.getElementById("description").focus();
		}
		else
		{
			CodeCase();
			document.getElementById("service_form").submit();
			
		}
	
	}
function CodeCase()
{
		
		var $sCode = document.getElementById("servicecode").value;
		document.getElementById("servicecode").value = $sCode.toUpperCase();
}
function UpperCase()
{
var htext, nhtext;
	
	htext= document.getElementById("name").value;
		
	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.
		}
	}
	
	document.getElementById("name").value=nhtext;
	
}

	function cancel_form()
	{
	window.location="index.php?action=viewAll&clear=clearAll";
	}
	function cancel_form()
	{
	window.location="index.php?action=viewAll&clear=clearAll"
	}
	
	function delete_form_confirm(name)
	{
		var is_confirmed = confirm("Delete service "+name+"?");
		if (is_confirmed)
		{
			document.getElementById("delete_form").submit();
		}
	}
	
	function edit_activate()
	{
		document.getElementById("update").disabled = false;
		document.getElementById("edit_details").disabled = true;
		document.getElementById("delete").disabled = true;
		document.getElementById("discard").disabled = false;
		document.getElementById("canceladd").disabled = true;
		
		
		document.getElementById("name").readOnly = false;
		document.getElementById("recipientcode").disabled = false;
		document.getElementById("servicecode").readOnly = false;
		document.getElementById("description").readOnly = 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 viewService section with ...
			<? echo "document.location='".$_SESSION['base_url']."/service/index.php?action=viewService&serviceTypeID=".$_REQUEST['serviceTypeID']."'"; ?>
		} // otherwise do nothing
	}

	function pass()
{
escapeSlash('name');
escapeSlash('description');
}

function escapeSlash(type)

{
var inValidChars = "<>;\"\'";
var Char;
var $new="";
   
 if ( type == "name")
	{
	var $check = document.getElementById("name").value;
	}
else if ( type == "description")
	{
	var $check = document.getElementById("description").value;
	}

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

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

<?php
	include_once($_SESSION['base_dir']."/classes/service.inc.php");
	
	// Sort 2d Array
	// Limitation - Must edit 2nd last line of function - array_multisort to have correct no of keys at end
	// 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_STRING;
		}
		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]]);
		return $arr;
	}
	
	if ($_REQUEST['action'] == 'viewAll')
	// display a grid view of OVCgroups, allowing the user to sort by columns or select a ovcgroup to edit
	{
  		// Main heading and top button row
		echo "<h2>Service Type Administration</h2>";
		echo "<div id='top_buttons'>";
		echo "<form class='formbuttons' name='nav_add' action='".$_SESSION['base_url']."/service/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='servicemanagement' />";
		//echo "<input type='hidden' name='section' value='search'>";
		echo "</form>";

		echo "</div>";

		if ($_SESSION['service_msg_disp'] == true)
		{
			echo "<p class='message'>".$_SESSION['service_msg']."</p>";
			$_SESSION['service_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['serviceStart']);
			unset($_SESSION['serviceFiltered']);
			unset($_SESSION['serviceSorted']);
			unset($_SESSION['serviceSortBy']);
			unset($_SESSION['serviceNoShown']);
			$_SESSION['serviceShowAll'] = false;
		}
		if (!isset($_SESSION['serviceStart']))
		{
			$_SESSION['serviceStart'] = '0';
			$_SESSION['serviceDepth'] = '0';
		}
		if (!isset($_SESSION['serviceNoShown']))
		{
			$_SESSION['serviceNoShown'] = '10';
		}
		if ($_REQUEST['direction'] == 'next')
		{
			unset($_SESSION['serviceSorted']);
			unset($_SESSION['serviceSortBy']);
			$_SESSION['serviceStart'] = $_SESSION['serviceDepth'];
		}
		if ($_REQUEST['direction'] == 'previous')
		{
			unset($_SESSION['serviceSorted']);
			unset($_SESSION['serviceSortBy']);
			$_SESSION['serviceDepth'] = $_SESSION['serviceStart'];
			$_SESSION['serviceDepth']--;
		}
		// if no sorting stipulated then set defaults - House Case Number Ascending
		if (!isset($_SESSION['serviceSortBy']))
		{
			$_SESSION['serviceSortBy'] = "Name";
			$_SESSION['serviceLastSortBy'] = "";
			$_SESSION['serviceOrder'] = "ASC";
			$_REQUEST['sortType'] = "str";
		}
		$results = Service::getAllServices();
		// 2d array structure
		$details = array(array("ServiceID" => "","Name" => "","Recipient" => "","ServiceCode" => "","Description" => "","isActive" => ""));
		$count = 0; 
		// Populating a 2d array using household info
		foreach($results as $result)
		{
			$details['ServiceID'][$count] = $result->serviceTypeID;
			$details['Name'][$count] = $result->name;
			if ($result->recipientCode == 'OC')
			{
				$details['Recipient'][$count] = "Single Child";
			}
			else if ($result->recipientCode == 'HH')
			{
				$details['Recipient'][$count] = "Household";			
			}
			else if ($result->recipientCode == 'CG')
			{
				$details['Recipient'][$count] = "Caregiver";			
			}
			$details['ServiceCode'][$count] = $result->recipientCode.$result->serviceCode;
			$details['Description'][$count] = $result->description;
			$details['isActive'][$count] = $result->isActive;
			$count++;
		}
		$details = TwoDSort($details,'Name','asc','str');
		// If filtering has been done, replace $details (which is all info) with filtered info
		if (isset($_SESSION['serviceFiltered']))
		{
			$details = $_SESSION['serviceFiltered'];
		}
		$_SESSION['serviceDetails'] = $details;
		if (count($details['Name']) != 0)
		{
			// Form for filtering choices
			echo "<div id='search'>";
			echo "<form class='formbuttons' name='filtering' action='".$_SESSION['base_url']."/service/index.php' method='get'>";
			echo "Search for ";
			echo "<input type='text' name='filter' value=''>";
			echo " in ";
			echo "<SELECT name='category'>";
			echo "<OPTION SELECTED value='Name'> Name";
			echo "<OPTION value='Recipient'> Recipient";
			echo "<OPTION value='ServiceCode'> Service Code";
			echo "<OPTION value='Description'> Description";
			echo "</SELECT>";
			echo " &nbsp; &nbsp; Show:  ";
			if ($_SESSION['serviceShowAll'])
			{
				echo "<input type='checkbox' name='showAll' checked='true'>";
			}
			else
			{
				echo "<input type='checkbox' name='showAll'>";
			}
			echo " Removed Services (<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['serviceNoShown'])
				{
					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']."/service/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>";
	
			echo "<div id='main_content'>";
			// Start of child info grid
			echo "<table class='grid'>";
			echo "<tr>";
			echo "<th width='130'>";
			echo "<a href='".$_SESSION['base_url']."/service/index.php?action=sorting&sortBy=Name&sortType=str'>Name </a>";
			if ($_SESSION['serviceSortBy'] == 'Name')
			{
				if ($_SESSION['serviceOrder'] == '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='120'>";
			echo "<a href='".$_SESSION['base_url']."/service/index.php?action=sorting&sortBy=ServiceCode&sortType=str"."'>Service Code </a>";
			if ($_SESSION['serviceSortBy'] == 'ServiceCode')
			{
				if ($_SESSION['serviceOrder'] == '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='160'>";
			echo "<a href='".$_SESSION['base_url']."/service/index.php?action=sorting&sortBy=Recipient&sortType=str"."'>Recipient </a>";
			if ($_SESSION['serviceSortBy'] == 'Recipient')
			{
				if ($_SESSION['serviceOrder'] == '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='380'>";
			echo "<a href='".$_SESSION['base_url']."/service/index.php?action=sorting&sortBy=Description&sortType=str"."'>Description </a>";
			if ($_SESSION['serviceSortBy'] == 'Description')
			{
				if ($_SESSION['serviceOrder'] == '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['serviceSorted']))
			{
				// Beginning of hitcounter logic for determining depth
				$hitCounter = 0;
				while ($hitCounter < $_SESSION['serviceNoShown'])
				{
					// Only want to display Active users
					if (!$_SESSION['serviceShowAll'])
					{
						if ($details['isActive'][$_SESSION['serviceDepth']] == 1)
						{
							$hitCounter++;
						}
						if ($_REQUEST['direction'] == 'previous')
						{
							$_SESSION['serviceDepth']--;
						}
						else
						{
							$_SESSION['serviceDepth']++;
						}
					}
					// Viewing all households so don't need to match check. Depth is same as number of records 
					else
					{
						$hitCounter = $_SESSION['serviceNoShown'];
						if ($_REQUEST['direction'] == 'previous')
						{
							$_SESSION['serviceDepth'] = $_SESSION['serviceDepth'] - $_SESSION['serviceNoShown'];
						}
						else
						{
							$_SESSION['serviceDepth'] = $_SESSION['serviceDepth'] + $_SESSION['serviceNoShown'];
						}
					}
					if ($_SESSION['serviceDepth'] == count($details['Name']))
					{
						$hitCounter = $_SESSION['serviceNoShown'];
					}
				}
			}
			// If viewing all households and on last page then depth is simply total number of records
			if (($_SESSION['serviceShowAll']) && ($_SESSION['serviceDepth'] > count($details['Name'])))
			{
				$_SESSION['serviceDepth'] = count($details['Name']);
			}
			// 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['serviceDepth']++;
				$placeholder = $_SESSION['serviceDepth'];
				$_SESSION['serviceDepth'] = $_SESSION['serviceStart'];
				$_SESSION['serviceStart'] = $placeholder;
			}
			// Sets record set to sorted set just before outputting. After outputting sets it back to original again.
			if (isset($_SESSION['serviceSorted']))
			{
				$details = $_SESSION['serviceSorted'];
			}
			// Outputing Details
			for ($i=$_SESSION['serviceStart'];$i<$_SESSION['serviceDepth'];$i++)
			{
				if (!$_SESSION['serviceShowAll'])
				{			
					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']."/service/index.php?action=viewService&serviceTypeID=".$details['ServiceID'][$i]."'>";
						echo $details['Name'][$i]."</a>";
						echo "</td>";
						echo "<td class='data$col'>";
						echo $details['ServiceCode'][$i];
						echo "</td>";
						echo "<td class='data$col'>";
						echo $details['Recipient'][$i];
						echo "</td>";
						echo "<td class='data$col'>";
						echo $details['Description'][$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']."/service/index.php?action=viewService&serviceTypeID=".$details['ServiceID'][$i]."'>";
					echo $details['Name'][$i]."</a>";
					echo "</td>";
					echo "<td class='data$col'>";
					echo $details['ServiceCode'][$i];
					echo "</td>";
					echo "<td class='data$col'>";
					echo $details['Recipient'][$i];
					echo "</td>";
					echo "<td class='data$col'>";
					echo $details['Description'][$i];
					echo "</td>";
					echo "</tr>";		
				}
			}
			echo "</table>";
			
			// Returns $details to 'orginal' pre-sorted state
			if (isset($_SESSION['serviceFiltered']))
			{
				$details = $_SESSION['serviceFiltered'];
			}
			else
			{
				$details = $_SESSION['serviceDetails'];
			} 
	
			// 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['serviceShowAll'])
			{
				$checker = $_SESSION['serviceStart'];
				do
				{
					if ($checker == 0)
					{
						$firstPage = true;
					}
					$checker--;
					if ($details['isActive'][$checker] == 1)
					{
						$firstPage = false;
						$checker = -1;
					}
				}
				while ($checker >= 0);
			}
			else
			{
				if ($_SESSION['serviceStart'] == 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['serviceShowAll'])
			{
				$lastPage = true;
				$checker = $_SESSION['serviceDepth'];
				do
				{
					if ($checker == count($details['Name']))
					{
						$checker++;
					}
					else
					{
						if ($details['isActive'][$checker] == 1)
						{
							$lastPage = false;
							$checker = count($details['Name']);
						}
						$checker++;
					}
				}
				while ($checker < count($details['Name']));
			}
			else
			{
				if ($_SESSION['serviceDepth'] >= count($details['Name']))
				{
					$lastPage = true;
				}
			}
	
			echo "<table>";
			echo "<tr>";
			echo "<td class='grid_previous'>";
			// If so disables left tab
			if ($firstPage)
			{
				$_SESSION['serviceStartingRecord'] = 1;
			}
			else
			{
				echo "<a href='".$_SESSION['base_url']."/service/index.php?action=viewAll&direction=previous'> Previous </a>";
			}
			echo "</td>";
			echo "<td class='grid_records'>";
			// Checking size of record set
			if ($_SESSION['serviceShowAll'])
			{
				$totalRecords = count($details['Name']);
			}
			else
			{
				$totalRecords = 0;
				for ($j=0;$j<count($details['Name']);$j++)
				{
					if ($details['isActive'][$j] == 1)
					{
						$totalRecords++;
					}
				}
			}		
			// Checks if direction is previous and NOT the first page (Important) 
			if (($_REQUEST['direction'] == 'previous') && (!$firstPage))
			{
				$_SESSION['serviceStartingRecord'] = $_SESSION['serviceStartingRecord'] - $_SESSION['serviceNoShown'];
			}
			// Direction forward
			if ($_REQUEST['direction'] == 'next')
			{
				$_SESSION['serviceStartingRecord'] = $_SESSION['serviceStartingRecord'] + $_SESSION['serviceNoShown'];
			}	
			echo "Showing ".$_SESSION['serviceStartingRecord']." - ";
			// If last page then top end is obviously the total numbers of records
			if (!$lastPage)
			{
				echo $_SESSION['serviceStartingRecord'] + $_SESSION['serviceNoShown'] - 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']."/service/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 services on record";
			echo "Children can be entered using the 'Add New' Button above";
			echo "</div>";
		}
	}

	else if ($_REQUEST['action'] == 'filtered')
	{
		$filtered = array(array("ServiceID" => "","Name" => "","Recipient" => "","ServiceCode" => "","Description" => "","isActive" => ""));
		$_SESSION['serviceNoShown'] = $_REQUEST['numberRecords'];
		$c = 0;
		$clear = "";
		$match = false;
		unset($_SESSION['serviceStart']);
		unset($_SESSION['serviceSorted']);
		unset($_SESSION['serviceSortBy']);
		for ($i=0;$i<count($_SESSION['serviceDetails']['Name']);$i++)
		{
			// Take substring of chosen categoty at the length of the filter word and compare it to the filter choice
			if (strncasecmp($_SESSION['serviceDetails'][$_REQUEST['category']][$i],$_REQUEST['filter'],strlen($_REQUEST['filter'])) == 0)
			{
				$filtered['ServiceID'][$c] = $_SESSION['serviceDetails']['ServiceID'][$i];
				$filtered['Name'][$c] = $_SESSION['serviceDetails']['Name'][$i];
				$filtered['Recipient'][$c] = $_SESSION['serviceDetails']['Recipient'][$i];
				$filtered['ServiceCode'][$c] = $_SESSION['serviceDetails']['ServiceCode'][$i];
				$filtered['Description'][$c] = $_SESSION['serviceDetails']['Description'][$i];
				$filtered['isActive'][$c] = $_SESSION['serviceDetails']['isActive'][$i];
				$c++;
				$match = true;
			}
		}
		if (!$match)
		{
			$_SESSION['service_msg_disp'] = true;
			$_SESSION['service_msg'] = "Your search did not return any results - showing all records.";
			$clear = 'clearAll';
		}
		$_SESSION['serviceFiltered'] = $filtered;
		// To check track of whether or not the Show Removed Children box has been checked
		if (isset($_REQUEST['showAll']))
		{
			 $_SESSION['serviceShowAll'] = true;
		}
		else
		{
			$_SESSION['serviceShowAll'] = false;
		}
		echo "<script language = 'javascript'>document.location='".$_SESSION['base_url']."/service/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['serviceLastSortBy'] = $_SESSION['serviceSortBy'];
		$_SESSION['serviceSortBy'] = $_REQUEST['sortBy'];
		// if lastsortby and sortby are the same, then flip the value of the ASC/DESC
		if ($_SESSION['serviceLastSortBy'] == $_SESSION['serviceSortBy'])
		{
			if ($_SESSION['serviceOrder'] == "ASC")
			{
				$_SESSION['serviceOrder'] = "DESC";
			}
			else $_SESSION['serviceOrder'] = "ASC";
		}
		else
		{
			// otherwise, the sort must be ASC
			$_SESSION['serviceOrder'] = "ASC";
		}
		$sorted = array(array("ServiceID" => "","Name" => "","Recipient" => "","ServiceCode" => "","Description" => "","isActive" => ""));
		$c = 0;
		// Creating smaller array out just the records in the current page section (e.g 8 - 15)
		for ($i=$_SESSION['serviceStart'];$i<$_SESSION['serviceDepth'];$i++)
		{
			$sorted['Service'][$i] = $_SESSION['serviceDetails']['Service'][$i];
			$sorted['Name'][$i] = $_SESSION['serviceDetails']['Name'][$i];
			$sorted['Recipient'][$i] = $_SESSION['serviceDetails']['Recipient'][$i];
			$sorted['ServiceCode'][$i] = $_SESSION['serviceDetails']['ServiceCode'][$i];
			$sorted['Description'][$i] = $_SESSION['serviceDetails']['Description'][$i];
			$sorted['isActive'][$i] = $_SESSION['serviceDetails']['isActive'][$i];
		}

		$sorted = TwoDSort($sorted,$_SESSION['serviceSortBy'],$_SESSION['serviceOrder'],$_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 childSorted starts as a copy
		// of the full 2d array and then gets the current page records overwritten with sorted ones.
		$_SESSION['serviceSorted'] = $_SESSION['serviceDetails'];
		for ($i=$_SESSION['serviceStart'];$i<$_SESSION['serviceDepth'];$i++)
		{
			$_SESSION['serviceSorted']['Service'][$i] = $sorted['Service'][$c];
			$_SESSION['serviceSorted']['Name'][$i] = $sorted['Name'][$c];
			$_SESSION['serviceSorted']['Recipient'][$i] = $sorted['Recipient'][$c];
			$_SESSION['serviceSorted']['ServiceCode'][$i] = $sorted['ServiceCode'][$c];
			$_SESSION['serviceSorted']['Description'][$i] = $sorted['Description'][$c];
			$_SESSION['serviceSorted']['isActive'][$i] = $sorted['isActive'][$c];
			$c++;
		}
		echo "<script language = 'javascript'>document.location='".$_SESSION['base_url']."/service/index.php?action=viewAll&sortBy=sorting';</script>";
	}
		
	else if ($_REQUEST['action'] == 'add')
	{
		// Add a new Service

		echo "<h2>Add a Service</h2>";

		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='servicemanagement' />";
		echo "<input type='hidden' name='section' value='add'>";
		echo "</form>";
		echo "</div>";

		echo "<div id='main_content'>";
			echo "<table class= 'form'>";
		echo "<form name='add_service_form' id='service_form' action='".$_SESSION['base_url']."/service/index.php' method='post'>";
		echo "<th>Service Details</th>";
		echo "<tr>";
		echo "<td class='data1'>Name</td><td class='data1'><input type='text'  name='name' id='name' onblur= 'javascript:UpperCase();' onkeyup= 'javascript: pass();'></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data2'>Recipient Code</td><td class='data2'><select id='recipientcode' name='recipientcode' >";
			echo "<option value='CG'";
			if ($_REQUEST['recipientcode'] == 'CG' || $_REQUEST['recipientcode'] == '' )
			{
			echo "selected='selected'";
			}
			echo ">";
			echo "CG";
			echo "</option>";
			echo "<option value='HH'"; 
			if ($_REQUEST['recipientcode'] == 'HH'  )
			{
			echo "selected='selected'";
			}
			echo ">";
			echo "HH";
			echo "</option>";
			echo "<option value='OC'";
			if ($_REQUEST['recipientcode'] == 'OC'  )
			{
			echo "selected='selected'";
			}
			echo ">";
			echo "OC";
			echo "</option>";
			
			
		echo "</select></td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td class='data1'>Service Code </td><td class='data1'><input type='text' name='servicecode' id='servicecode' onblur= 'javascript:CodeCase();' onkeydown= 'javascript:CodeCase();'></td>";
		echo "</tr>";
		
		echo "<tr>";
		echo "<td valign='top' class='data2'>Description </td><td class='data2'><textarea name='description' id='description' rows= '5' cols= '37' onkeyup= 'javascript: pass();' ></textarea>";
		echo "</tr>";
		echo "<tr>";
    	echo "<td colspan='1'>";
		echo "</td>";
		echo "<td colspan='1'>";
		echo "<input class='button' type='button' name='addservice' value='Save' onClick='javascript: service_form_confirm();'>";
		echo "<input class='button' type='button' name='cancel' value='Cancel' onclick='javascript: cancel_form();'>";
		echo "<input type='hidden' name='action' value='addService'>";
		echo "</td>";
		echo "</tr>";
    	echo "</form>";
		echo "</table>";
		echo "</div>";
	}

	else if ($_REQUEST['action'] == 'addService')
	{
		// create an instance of the service class
		$s = new Service;
		//formatting today's date to insert into mySql
		
		if ($s->serviceExists($_REQUEST['recipientcode'],$_REQUEST['servicecode']) == 1)
		{
			echo "<script language = 'javascript'>alert(\"Service Code ".$_REQUEST['recipientcode'].$_REQUEST['servicecode']." has already been used. ".'\n'." Please enter a new Service Code.\");</script>";
			echo "<script language = 'javascript'>document.location='".$_SESSION['base_url']."/service/index.php?action=add';</script>";
		}
		else
		{
			$date= date ("Y-m-d");
			$_SESSION['service_msg_disp'] = true;
			$_SESSION['service_msg'] = "Service ".$_REQUEST['name']."has been added";
			// fill it with the new service info
			$s->setService($_REQUEST['recipientcode'],$_REQUEST['name'],$_REQUEST['description'],$_REQUEST['servicecode'],$date);
			// write to the database
			$s->saveService();
			// redirect the user to the initial grid edit page with a confirmation message
			$_SESSION['service_msg_disp'] = true;
			$_SESSION['service_msg'] = "Service ".$_REQUEST['name']." has been added";
			echo "<script language = 'javascript'>document.location='".$_SESSION['base_url']."/service/index.php?action=viewAll&clear=clearAll';</script>";
		}
	}
	
	else if ($_REQUEST['action'] == 'viewService')
	{
		$s = new Service;
		$s->getService($_REQUEST['serviceTypeID']);
		$_SESSION['serviceID'] = $_REQUEST['serviceTypeID'];
		
		echo "<h2>Update Service</h2>";
		
		echo "<div id='top_buttons'>";
		
		echo "<input class='button' type='button' name='edit_details' id='edit_details' value='Edit Details' onclick='javascript: edit_activate();'";
		if ($s->isActive == 0)
		{
			echo "disabled='disabled'";
		}
		echo ">";
		echo "&nbsp;";
		
		echo "<input class='button' type='button' name='delete' id='delete' value='Delete' onclick='javascript:delete_form_confirm(\"".$s->name."\");'";
		if ($s->isActive == 0)
		{
			echo "disabled='disabled'";
		}
		echo ">";
		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='servicemanagement' />";
		echo "<input type='hidden' name='section' value='view'>";
		echo "</form>";

		echo "<input class='button' type='button' name='canceladd' id='canceladd' value='Exit' onClick='javascript: document.location=\"".$_SESSION['base_url']."/service/index.php?action=viewAll&clear=clearAll\";'>";
		echo "&nbsp;";
		echo "<form class='formbuttons' name='delete_form' id='delete_form' action='".$_SESSION['base_url']."/service/index.php' method='post'>";
		
   		echo "<input type='hidden' name='action' value='delete'>";
  		echo "</form>";
		
		echo "<form name='service_form' id='service_form' action='".$_SESSION['base_url']."/service/index.php' method='post'>";
		
		echo "<input type='hidden' name='serviceTypeID' value='".$_REQUEST['serviceTypeID']."'>";
		echo "<input type='hidden' name='action' value='update'>";
		echo "</div>";

		echo "<div id='main_content'>";
		echo "<table class= 'form'>";
		echo "<th>";
		if ($s->isActive == 0)
		{
			echo "<img src='".$_SESSION['base_url']."/images/redFlag.gif'>  ";
		}
		echo "".$s->name."";
		echo "</th>";
		
		if ($s->isActive == 0)
		{
			echo "<tr>";
			echo "<td class='data2'>";
			$date_arr = explode("-",$s->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'>Name</td><td class ='data1'><input type='text' name='name' id='name' value='".$s->name."' readonly='readonly' onblur= 'javascript:UpperCase();' onkeyup= 'javascript: pass();' ></td>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data2'>Recipient Code</td><td class='data2'><select id='recipientcode' name='recipientcode' disabled='disabled'>";
			echo "<option value='CG'";
			if ($s->rCode == 'CG')
			{
				echo " selected='selected'";
			}
			echo ">";
			echo "CG";
			echo "</option>";
			echo "<option value='HH'";
			
			if ($s->rCode == 'HH')
			{
				echo " selected='selected'";
			}
			echo ">";
			echo "HH";
			echo "</option>";
			echo "<option value='OC'";
			if ($s->rCode == 'OC')
			{
				echo " selected='selected'";
			}
			echo ">";
			echo "OC";
			echo "</option>";			
		echo "</select></td>";
		echo "</tr>";
		echo "</tr>";
		echo "<tr>";
		echo "<td class='data1'>Service Code</td><td class='data1'><input type='text' name='servicecode' id='servicecode' value='".$s->sCode."' readonly='readonly' onblur= 'javascript:CodeCase();' onkeydown= 'javascript:CodeCase();'></td>";
		echo "</tr>";
		echo "<td class='data1'>Description</td><td class='data1'><textarea name='description' id='description' readonly='readonly' rows= '5' cols= '37' onkeyup= 'javascript: pass();' >".$s->desc. "</textarea></td>";
		echo "</tr>";
		echo "<tr>";
    	echo "<td>";
		echo "</td>";
		echo "<td>";
		echo "<input class='button' type='button' name='update' id='update' value='Save' onclick='javascript: service_form_confirm();' disabled='disabled'>";
		echo "<input class='button' type='button' name='discard' id='discard' value='Discard Changes' onClick='javascript: confirm_discard();' disabled='disabled'>";
    	echo "</form>";
   		echo "</td>";
		echo "</tr>";
		echo "</table>";
		echo "</div>";
	}

	else if ($_REQUEST['action'] == 'delete')
	{
		$date= date ("Y-m-d");
		// create an instance of the Service class
		$s = new Service();
		// fill it with the info corresponding to the given Group id
		$s->getService($_SESSION['serviceID']);
		$s->isActive = 0;
		$s->dateDeleted = $date;
		// write to the database
		$s->updateService();
		// redirect the user to the initial grid edit page with a confirmation message
		$_SESSION['service_msg_disp'] = true;
		$_SESSION['service_msg'] = "Service ".$s->name." has been deleted";
		echo "<script language = 'javascript'>document.location='".$_SESSION['base_url']."/service/index.php?action=viewAll&clear=clearAll&msg=service updated.';</script>";
	}

	else if ($_REQUEST['action'] == 'update')
	{
		// create an instance of the Service class
		$s = new Service();
		// fill it with the info corresponding to the given Group id
		$s->getService($_REQUEST['serviceTypeID']);
		// set the new values
		$s->name = $_REQUEST['name'];
		$s->rCode = $_REQUEST['recipientcode'];
		$s->sCode = $_REQUEST['servicecode'];
		$s->desc = $_REQUEST['description'];
		// write to the database
		$s->updateService();
		// redirect the user to the initial grid edit page with a confirmation message
		$_SESSION['service_msg_disp'] = true;
		$_SESSION['service_msg'] = "Service ".$s->name." has been updated";
		echo "<script language = 'javascript'>document.location='".$_SESSION['base_url']."/service/index.php?action=viewAll&clear=clearAll&msg=service updated.';</script>";
	}

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