<?PHP
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/whm_functions.php";
include $server_inc."/client_functions.php";
include $server_inc."/login_functions.php";

if (isset($submit))
	{
	if ($group_status==2)
		{
		$delete="delete ";
		$delete.="from plan_groups ";
		$delete.="where gid='".addslashes(trim($gid))."'";

		mysql_query($delete);
		unset($gid);
		}
	else if (strlen(trim($name))==0) 
		{
		$err=true;
		}
	else if ($gid==0)
		{
		// insert
		$insert="insert into plan_groups set ";
		$insert.="name='".addslashes(trim($name))."', ";
		$insert.="group_status='".addslashes(trim($group_status))."'";

		mysql_query($insert);

		$gid=mysql_insert_id();
		}
	else
		{
		if (!isset($spid))
			{
			$errPP=true;
			}
		else
			{
			while (list ($key, $value) = each ($spid)) 
				{
				@$buffer.=$value."|";
				}

			// update
			$update="update plan_groups set ";
			$update.="name='".addslashes(trim($name))."', ";
			$update.="group_status='".addslashes(trim($group_status))."', ";
			$update.="group_payment='".addslashes(trim($buffer))."' ";
			$update.="where gid='".addslashes(trim($gid))."'";

			mysql_query($update);
			}
		}
	}

if (isset($submit_add))
	{
	if (strlen(trim($name))==0) 
		{
		$err0=true;
		}
	else
		{
		// insert
		$insert="insert into plan_groups set ";
		$insert.="name='".addslashes(trim($name))."', ";
		$insert.="group_status='".addslashes(trim($group_status))."'";

		mysql_query($insert);
		}
	}

include $server_admin_inc."/header.php";

echo("
	<table width='725' cellpadding='0' cellspacing='0' border='0'>
		<tr>
			<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
			<td width='99%' align='left' valign='top'>
			<table width='100%' cellpadding='4' cellspacing='1' border='0'>
	");
if (isset($err0))
	{
	echo("
				<tr>
					<td colspan='5' align='left' valign='top'><img src='".$http_images."/error_arrow.gif'> You must enter a group name to continue.</td>
				</tr>
				<tr>
					<td colspan='5' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='4'></td>
				</tr>
		");
	}
echo("
			<form action='".$PHP_SELF."' method='POST'>
			<input type='hidden' name='sid' value='".trim($sid)."'>
				<tr>
					<td colspan='2' align='left' valign='middle'>&nbsp;<input type='text' size='40' maxlength='255' name='name' ".$input_style."></td>
					<td align='left' valign='middle'>&nbsp;<select name='group_status' ".$input_style.">
						<option value='1'".((isset($group_status))?"".(($group_status=="1")?" selected":"")."":"").">Public View</option>
						<option value='0'".((isset($group_status))?"".(($group_status=="0")?" selected":"")."":"").">Hidden Group</option>
					</select></td>
					<td colspan='2' align='center' valign='middle'><input ".$button_style." type='submit' name='submit_add' value='Add New Group'></td>
				</tr>
			</form>
	<tr>
		<td colspan='5' align='left' valign='top'><hr color='#F0EFED'></td>
	</tr>
	");
if (isset($err))
	{
	echo("
				<tr>
					<td colspan='5' align='left' valign='top'><img src='".$http_images."/error_arrow.gif'> <font color='#990000'>You must enter a group name to continue.</font></td>
				</tr>
		");
	}
if (isset($errPP))
	{
	echo("
				<tr>
					<td colspan='5' align='left' valign='top'><img src='".$http_images."/error_arrow.gif'> <font color='#990000'>You must check at least one payment processor for each group to continue.</font></td>
				</tr>
		");
	}
echo("
				<tr>
					<td colspan='5' align='left' valign='top'><img src='".$http_images."/error_arrow.gif'> Deleting a group will set all packages in the group to undefined when using the <a href='".$http_admin."/plan_edit.php?sid=".$sid."'>Hosting Plan Editor</a>.</td>
				</tr>
				<tr>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>#</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Group Name</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Group Status</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Delete Group</b></td>
					<td align='center' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Edit Group</b></td>
				</tr>
		");
	$y=1;
	$x=1;
	$row=mysql_query("select gid, name, group_status, group_payment from plan_groups order by group_status desc, gid asc");
	while ($rs=mysql_fetch_row($row))
		{
		if ($y==1) {$bgcolor="#F2F4F6"; $y=0;} else {$bgcolor="#ECEEF0"; $y=1;}
		$name=stripslashes(trim($rs[1]));
		$group_status=stripslashes(trim($rs[2]));
		$group_payment=stripslashes(trim($rs[3]));
		echo("
			<form action='".$PHP_SELF."' method='POST'>
			<input type='hidden' name='sid' value='".trim($sid)."'>
			<input type='hidden' name='gid' value='".trim($rs[0])."'>
				<tr>
					<td align='center' valign='middle' bgcolor='".$bgcolor."'>".$x.").</td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;<input type='text' size='40' maxlength='255' name='name' ".str_replace("#ffffff", $bgcolor, $input_style)." value='".((isset($name))?"".$name."":"")."'></td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;<select name='group_status' ".$input_style.">
						<option value='1'".((isset($group_status))?"".(($group_status=="1")?" selected":"")."":"").">Public View</option>
						<option value='0'".((isset($group_status))?"".(($group_status=="0")?" selected":"")."":"").">Hidden Group</option>
					</select></td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'><input type='checkbox' name='group_status' value='2'>Check to Delete</td>
					<td align='center' valign='middle' bgcolor='".$bgcolor."'><input ".$button_style." type='submit' name='submit' value='Edit'></td>
				</tr>
				<tr>
					<td colspan='1' align='left' valign='middle'></td>
					<td colspan='4' align='left' valign='middle' bgcolor='".$bgcolor."'>
					<table width='100%' cellpadding='0' cellspacing='0' border='0'>
						<tr>
							<td>&nbsp;<b>Un</b>check the payment processors that do not apply to this group.</td>
						</tr>
					</table>
					<table width='100%' cellpadding='0' cellspacing='0' border='1' bordercolor='#E9E9EB'>
	");	
						if ($group_payment=="")
							{
							$rbx=mysql_query("select pid from payment_process where status='1'");
							while ($rb=mysql_fetch_row($rbx)) { $cd.=$rb[0]."|"; }
							mysql_query("update plan_groups set group_payment='".addslashes(trim($cd))."' where gid='".addslashes(trim($rs[0]))."'");

							unset($cd);

							$gpx=mysql_fetch_row(mysql_query("select group_payment from plan_groups where gid='".addslashes(trim($rs[0]))."'"));
							$group_payment=$gpx[0];
							}

						list($p1, $p2, $p3, $p4, $p5, $p6)=split("[|]", $group_payment);

						$rt=0;
						$rspl=mysql_query("select pid, name from payment_process where status='1'");
						while ($rsp=mysql_fetch_row($rspl))
							{
							if ($rt==0)
								{
								echo("
									<tr>
										<td><input type='checkbox' name='spid[".$rsp[0]."]' value='".$rsp[0]."'".(($rsp[0]==$p1||$rsp[0]==$p2||$rsp[0]==$p3||$rsp[0]==$p4||$rsp[0]==$p5||$rsp[0]==$p6)?" checked":"")."> ".stripslashes(trim($rsp[1]))."</td>
									");
								$rt=1;
								}
							else if ($rt==1)
								{
								echo "<td><input type='checkbox' name='spid[".$rsp[0]."]' value='".$rsp[0]."'".(($rsp[0]==$p1||$rsp[0]==$p2||$rsp[0]==$p3||$rsp[0]==$p4||$rsp[0]==$p5||$rsp[0]==$p6)?" checked":"")."> ".stripslashes(trim($rsp[1]))."</td>";
								$rt=2;
								}
							else if ($rt==2)
								{
								echo "<td><input type='checkbox' name='spid[".$rsp[0]."]' value='".$rsp[0]."'".(($rsp[0]==$p1||$rsp[0]==$p2||$rsp[0]==$p3||$rsp[0]==$p4||$rsp[0]==$p5||$rsp[0]==$p6)?" checked":"")."> ".stripslashes(trim($rsp[1]))."</td>";
								$rt=3;
								}
							else
								{
								echo("
										<td><input type='checkbox' name='spid[".$rsp[0]."]' value='".$rsp[0]."'".(($rsp[0]==$p1||$rsp[0]==$p2||$rsp[0]==$p3||$rsp[0]==$p4||$rsp[0]==$p5||$rsp[0]==$p6)?" checked":"")."> ".stripslashes(trim($rsp[1]))."</td>
									</tr>
									");
								$rt=0;
								}
							
							}

						// finish out building the HTML tables if needed.
						if ($rt==1||$rt==2||$rt==3)
							{
							echo("
									<td colspan='
								");
							if ($rt==1)	{ echo "3"; }
							else if ($rt==2)	{ echo "2"; }
							else if ($rt==3)	{ echo "1"; }
							echo("
									'>&nbsp;</td>
								</tr>
								");
							}
echo("		
					</table>
					</td>
				</tr>
				<tr>
					<td colspan='5'><img src='".$http_images."/space.gif' width='1' height='8'></td>
				</tr>	
			</form>
			");
		$x++;
		}
echo("
			</table>
			</td>
		</tr>
	</table>
	");
include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>