<?
/*
Author		: Yaw
Version		: 1.01
Date		: 29th March 2002
Function	: Group Management

Revised by	: Louis
Version		: Final
Date		: 7 June 2002
*/ 
session_start();
require("auth.php");
unset($msg);

######################################################
# Determine if Access Level is adequate to view page #
######################################################
if ($Transmarco["accesslevel"] < $adminlevel) {
	authfail();
} else 	{
	// Subsequently when users decide on a sorting preference, store their preference into a session
	if (($Transmarco["usergroupsort"] != "") && ($sortorder != "")) {
		$Transmarco["usergroupsort"] = $sortorder;
	}
	else {
		$Transmarco["usergroupsort"] = "groupcode";
	}
	###################################################
	# Deactivate a Group code                         #
	###################################################
	if ($action == 'Deactivate') {
		$sql = "update grouptable set groupactive='N' where groupid=$id";
		db_query($sql);
		$msg = "<br><b>Group Deactivated</b><br>";
	}
	#################################################
	# Activating a Group code                       #
	#################################################
	if ($action == 'Activate') {
		$sql = "update grouptable set groupactive='Y' where groupid=$id";
		db_query($sql);
		$msg = "<br><b>Group Activated</b><br>";
	}
	###################################################
	# Editing a Group code                            #
	###################################################
	if (($action == 'Edit') && ($submitted == 'yes')) {
		// Initializing Msg Text
		$msg = "";

		// Checking if Group Description is blank when User is editing a group
		if (trim($groupdesc) != "") {
			$groupdesc = magiccode(ucwords($groupdesc));
			$sql = "update grouptable set groupdesc='$groupdesc', poolid='$poolid' where groupid=$id";
			db_query($sql);
			$msg = "<br><b>Group Record Successfully Updated!</b>";
		} else {
			$msg = "<br>Group Description Must Be Filled In";
		}
	}
	##################################################
	# Add a Group code                               #
	##################################################
	if (($action == 'Add') && ($submitted == 'yes')) {
		// Initializing Msg Text
		$msg = "";

		// Checking if Group Code is valid when User is adding a group, and if valid, force both characters to uppercase
		if (ereg ("([a-zA-Z]{2})", $groupcode)) {
			// Checking to see the the existing Group Code already exists in the database
			$groupcode = magiccode(strtoupper($groupcode));
			$sql = "select * from grouptable where groupcode='$groupcode'";
			$result = db_query($sql);
			if (db_numrows($result) >= 1) {
				$msg = $msg . "<br>The Group Code keyed in already exists in the database!";
			}
		} else {
			$msg = $msg . "<br>Invalid Group Code Entered";
		}

		// Checking if Group Description is blank when User is adding a group
		if (trim($groupdesc) != "" ) {
			$groupdesc = magiccode(ucwords($groupdesc));
		} else {
			$msg = $msg . "<br>Invalid Group Description Entered";
		}

		// Check if Pool Link got selected or not
		if ($poolid != "") {
			$poolid = magiccode($poolid);
		} else {
			$msg = $msg . "<br>You need to select a pool!";
		}

		// If no messages are stored up until this point, all data entered is valid, Proceed with insertion
		if ($msg == "") {
			$newid = magiccode(find_new_id("groupid","grouptable"));
			$sql = "insert into grouptable values ('$newid','$groupcode','$groupdesc','$poolid','Y')";
			db_query($sql);
			$msg = "<br><b>Group " . $groupcode . " Created<br>";
			unset($groupid);
			unset($groupcode);
			unset($groupdesc);
			unset($poolid);
		} else {
			$msg = $msg . "<br><b>Creation of New Group Failed!</b><br>";
		}
	}
?>
<html>
<head>
<title>Group Management Form</title>
<meta content="text/html; charset=windows-1252" http-equiv=Content-Type>
<meta content=en-us http-equiv=Content-Language>
<? include($style); ?>
</head>
<body bgcolor=#ffffff leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<p>&nbsp;</p>
<table width="680" border="0" cellspacing="0" cellpadding="8" align="center">
<tr valign="top"><td align="center"><font class="sub"><b>EXISTING GROUP CODES</b></font><br><? echo $msg?></td></tr>
</table>
<table class="bigbox" border="0" cellspacing="0" cellpadding="0" align="center">
<tr> 
    <td width="16" height="16" valign="top" align="left"><img src="<? echo $images ?>a.gif" width="16" height="18"></td>
    <td width="363"><img src="<? echo $images ?>shim.gif" width="16" height="16"></td>
    <td width="17" height="16" align="right" valign="top"><img src="<? echo $images ?>b.gif" width="16" height="16"></td>
</tr>
<tr> 
    <td width="16">&nbsp;</td>
    <td valign="top" class="bgline"> 
      <table cellpadding="5" cellspacing="1" border="0" align="center">
      <tr class="highlights"> 
          <td><a href="<?=$docroot?>/<?=$groupmgt?><? echo "?sortorder=groupcode"?>"><b>Group Code</b></a></td>
          <td><b>Group Description</b></td>
          <td><a href="<?=$docroot?>/<?=$groupmgt?><? echo "?sortorder=poolid"?>"><b>Pool Code</b></a></td>
          <td><b>Action</b></td>
      </tr>
		<? 
		//  Generating Existing Group Codes
		$sql = "select * from grouptable order by $Transmarco[usergroupsort]";
		$result = db_query($sql);
		if (db_numrows($result) >= 1) {
			for($x = 0; $x < db_numrows($result); $x++){
				$row = db_fetch_array($result,1);
				$groupid = $row["groupid"];
				$groupcode = $row["groupcode"];
				$groupdesc = $row["groupdesc"];
				$poolid = $row["poolid"];
				$groupactive = $row["groupactive"];
		?>
        <tr class="tdfill"> 
          <td><?=$groupcode?></td>
          <td><?=$groupdesc?></td>
          <td>
			<?
			$sql = "select poolcode from pool where poolid = $poolid";
			$rsult = mysql_query($sql);
			if (mysql_num_rows($rsult) >= 1) {
				$rs = mysql_fetch_array($rsult,1);
				echo $rs["poolcode"];
			}
			?></td>
          <td>
			<a href="<?=$docroot?>/<?=$groupmgt?><? echo "?action=Edit&id=" . $groupid ?>">Edit</a>
			<?
			if ($groupactive == "Y") { ?>
				<a href="<?=$docroot?>/<?=$groupmgt?><? echo "?action=Deactivate&id=" . $groupid ?>">Deactivate</a>
			<?
			} else { ?>
				<a href="<?=$docroot?>/<?=$groupmgt?><? echo "?action=Activate&id=" . groupid ?>">Activate</a>
			<?
			} ?>
		  </td>
        </tr>
		<? }
		}?>
      </table>
    </td>
    <td width="17" align="right">&nbsp;</td>
  </tr>
  <tr> 
    <td width="16" valign="bottom" align="left"><img src="<?=$images?>c.gif" width="16" height="18"></td>
    <td width="363">&nbsp;</td>
    <td width="17" align="right" valign="bottom"><img src="<?=$images?>d.gif" width="16" height="16"></td>
  </tr>
</table>
<p align="center"><a href="<?=$docroot?>/<?=$groupmgt?><? echo "?action=Add" ?>">Add New Group Code</a></p>
<?
if (($action == 'Edit') || ($action == 'Add')){ 
	// Prefilling the fields in the form with values from db
	if ($action=='Edit') {
		$sql = "select * from grouptable where groupid = $id";
		$result = db_query($sql);
		if (db_numrows($result) >= 1) {
			$row = db_fetch_array($result,1);
			$groupcode = $row["groupcode"];
			$groupdesc = $row["groupdesc"];
			$poolid = $row["poolid"];
		}
	}
?>
<form method="post" action="<?=$PHP_SELF?>" name="form">
<table width="680" border="0" cellspacing="0" cellpadding="8" align="center">
<tr valign="top"> 
	<td align="center"><font class="sub"><b>
        <?=strtoupper($action)?> GROUP CODE</b></font></td>
</tr>
</table>
<table width="600" border="0" cellspacing="0" cellpadding="0" bgcolor="#EEEEEE" align="center">
<tr> 
	<td width="16" height="16" valign="top" align="left"><img src="<?=$images?>a.gif" width="16" height="18"></td>
	<td width="563"><img src="<?=$images?>shim.gif" width="16" height="16"></td>
	<td width="17" height="16" align="right" valign="top"><img src="<?=$images?>b.gif" width="16" height="16"></td>
</tr>
<tr> 
	<td width="23">&nbsp;</td>
	<td width="563">
		<table>
		<tr><td>Group Code</td>
		<? if ($action == 'Add') {?>
			<td><input type="text" name="groupcode" value="" maxlength="2" size="2"> * The System will input this in uppercase</td>
		<? } else {	?>
			<td><?=$groupcode?></td>
		<? } ?>
		</tr>
		<tr><td>Group Description</td>
		<? if ($action == 'Add') {?>
			<td><input type="text" name="groupdesc" value="" maxlength="200" size="65">*</td>
		<? } else {	?>
			<td><input type="text" name="groupdesc" value="<?=$groupdesc?>" maxlength="200" size="65">*</td>
		<? } ?>			
		</tr>
		<tr><td>Pool ID</td>
		<?
		if ($action == 'Add') { ?>
			<td><select name="poolid" value="">Select One
			<?
			$sql = "select * from pool where editflag = '1' order by poolcode";
			$result = db_query($sql);
			for ($x = 0; $x < db_numrows($result); $x++) {
				$rs = db_fetch_array($result, 1);
			?>
				<option value="<?=$rs["poolid"]?>"><?=$rs["poolcode"]?></option>
			<?
			} ?>
			</select></td>
		<?
		} else { ?>
			<td><select name="poolid" value="">Select One
			<?
			$sql = "select * from pool where editflag = '1' order by poolcode";
			$result = db_query($sql);
			for ($x = 0; $x < db_numrows($result); $x++) {
				$rs = db_fetch_array($result, 1);
				if ($poolid == $rs["poolid"]) {
			?>
				<option value="<?=$rs["poolid"]?>" selected><?=$rs["poolcode"]?></option>
			<?	} else { ?>
				<option value="<?=$rs["poolid"]?>"><?=$rs["poolcode"]?></option>
			<?	}
			}?>
			</select></td>
		<? } ?>
		</tr>
		</table></td>
	<td width="17">&nbsp;</td>
</tr>
<tr> 
	<td width="23">&nbsp;</td>
		<input type="hidden" name="submitted" value="yes">
	<? if ($action == "Edit") { ?>
		<input type="hidden" name=id value=<?=$id?>>
	<? } ?>
	<td width="563" align="center"><input type="submit" name="action" value="<?=ucwords($action)?>">&nbsp;<input type="reset" name="Reset" value="Clear">&nbsp;<input type="submit" name="action" value="Cancel"></td>
	<td width="17">&nbsp;</td>
</tr>
<tr> 
	<td width="16" valign="bottom" align="left"><img src="<?=$images?>c.gif" width="16" height="18"></td>
	<td width="563">&nbsp;</td>
	<td width="17" align="right" valign="bottom"><img src="<?=$images?>d.gif" width="16" height="16"></td>
</tr>
</table>
</form>
<? } ?>
</body>
</html>
<? } ?>