<?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 addon_groups ";
		$delete.="where addon_gid='".addslashes(trim($addon_gid))."'";

		mysql_query($delete);
		unset($gid);

		mysql_query("update plan_specs set addon_gid='0' where addon_gid='".addslashes(trim($addon_gid))."'");
		}
	else if (strlen(trim($name))==0) 
		{
		$err=true;
		}
	else
		{
		if (!isset($spid)) { $errPP=true; } else { $n0=true; }

		if (!isset($skip_pp)) {	if (!isset($xpid)) { $errPx=true; } else { $n1=true; } }
		else { $n1=true; }

		if (isset($n0)&&isset($n1))
			{
			while (list ($key, $value) = each ($spid)) 
				{
				@$buffer.=$value."|";
				}
			unset($value);
			
			if (!isset($skip_pp))
				{
				while (list ($key, $value) = each ($xpid)) 
					{
					@$xbuffer.=$value."|";
					}
				}

			// update
			$update="update addon_groups set ";
			$update.="name='".addslashes(trim($name))."', ";
			$update.="group_status='".addslashes(trim($group_status))."', ";
			if (!isset($skip_pp)) { $update.="group_payment='".addslashes(trim($xbuffer))."', "; }
			$update.="group_addons='".addslashes(trim($buffer))."' ";
			$update.="where addon_gid='".addslashes(trim($addon_gid))."'";

			mysql_query($update);
			}
		}
	}

if (isset($submit_add))
	{
	if (strlen(trim($name))==0) 
		{
		$err0=true;
		}
	else
		{
		// insert
		$insert="insert into addon_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 Addon 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 addon for each group to continue.</font></td>
				</tr>
		");
	}
if (isset($errPx))
	{
	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 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 addon_gid, name, group_status, group_payment, group_addons from addon_groups order by group_status desc, addon_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]));
		$group_addons=stripslashes(trim($rs[4]));
		echo("
			<form action='".$PHP_SELF."' method='POST'>
			<input type='hidden' name='sid' value='".trim($sid)."'>
			<input type='hidden' name='addon_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='4' cellspacing='0' border='0'>
						<tr>
							<td>&nbsp;<b>Un</b>check the addons that do not apply to this group.</td>
						</tr>
					</table>
					<table width='100%' cellpadding='2' cellspacing='0' border='1' bordercolor='#E9E9EB'>
	");	
				if ($group_addons=="")
					{
					$xrbx=mysql_query("select aid from addon_specs");
					while ($xrb=mysql_fetch_row($xrbx)) { $xcd.=$xrb[0]."|"; }
					mysql_query("update addon_groups set group_addons='".addslashes(trim($xcd))."' where addon_gid='".addslashes(trim($rs[0]))."'");

					unset($xcd);

					$xgpx=mysql_fetch_row(mysql_query("select group_addons from addon_groups where addon_gid='".addslashes(trim($rs[0]))."'"));
					$group_addons=$xgpx[0];
					}
						$gg=1;
						$rt=0;
						$rspl=mysql_query("select aid, addon_name, addon_show_description, addon_description from addon_specs");
						while ($rsp=mysql_fetch_row($rspl))
							{
							if ($rt==0)
								{
								echo("
									<tr>
										<td><input type='checkbox' name='spid[".$rsp[0]."]' value='".$rsp[0]."'
									");
								$tok=strtok($group_addons, "|");
								while ($tok) 
									{
									if ($rsp[0]==$tok) { echo " checked"; }
									$tok=strtok("|");
									}
								echo ">";
								if ($rsp[2]==1)
									{
									$rsp[3]=preg_replace("/[^A-Za-z0-9 ]/", "", $rsp[3]);
									$rsp[1]=preg_replace("/[^A-Za-z0-9 ]/", "", $rsp[1]);
									echo "<A HREF='javascript:void(0)' class='cleanlink3' onmouseover=\"return overlib('".$rsp[3]."', CAPTION, '".stripslashes($rsp[1])."');\" onmouseout='nd();'>";
									}
								echo stripslashes(trim($rsp[1]));
								if ($rsp[2]==1)
									{
									echo "</a>";
									}
								echo "</td>";
								$rt=1;
								}
							else if ($rt==1)
								{
								echo "<td><input type='checkbox' name='spid[".$rsp[0]."]' value='".$rsp[0]."'";
								$tok=strtok($group_addons, "|");
								while ($tok) 
									{
									if ($rsp[0]==$tok) { echo " checked"; }
									$tok=strtok("|");
									}
								echo ">";
								if ($rsp[2]==1)
									{
									echo "<A HREF='javascript:void(0)' class='cleanlink3' onmouseover=\"return overlib('".$rsp[3]."', CAPTION, '".stripslashes($rsp[1])."');\" onmouseout='nd();'>";
									}
								echo stripslashes(trim($rsp[1]));
								if ($rsp[2]==1)
									{
									echo "</a>";
									}
								echo "</td>";
								$rt=2;
								}
							else if ($rt==2)
								{
								echo "<td><input type='checkbox' name='spid[".$rsp[0]."]' value='".$rsp[0]."'";
								$tok=strtok($group_addons, "|");
								while ($tok) 
									{
									if ($rsp[0]==$tok) { echo " checked"; }
									$tok=strtok("|");
									}
								echo ">";
								if ($rsp[2]==1)
									{
									echo "<A HREF='javascript:void(0)' class='cleanlink3' onmouseover=\"return overlib('".$rsp[3]."', CAPTION, '".stripslashes($rsp[1])."');\" onmouseout='nd();'>";
									}
								echo stripslashes(trim($rsp[1]));
								if ($rsp[2]==1)
									{
									echo "</a>";
									}
								echo "</td>";
								$rt=3;
								}
							else
								{
								echo("
										<td><input type='checkbox' name='spid[".$rsp[0]."]' value='".$rsp[0]."'
									");
								$tok=strtok($group_addons, "|");
								while ($tok) 
									{
									if ($rsp[0]==$tok) { echo " checked"; }
									$tok=strtok("|");
									}
								echo "> ";
								if ($rsp[2]==1)
									{
									echo "<A HREF='javascript:void(0)' class='cleanlink3' onmouseover=\"return overlib('".$rsp[3]."', CAPTION, '".stripslashes($rsp[1])."');\" onmouseout='nd();'>";
									}
								echo stripslashes(trim($rsp[1]));
								if ($rsp[2]==1)
									{
									echo "</a>";
									}
								echo "</td>\n</tr>";
								$rt=0;
								}
							$gg++;
							}

						// 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>
								");
							}
						if ($gg==1)
							{
							echo("
								<tr>
									<td>&nbsp;<b>No Addons Available.</b> Please <a href='".$http_admin."/addon_build.php?sid=".$sid."'>Click Here</a> to use the <a href='".$http_admin."/addon_build.php?sid=".$sid."'>\"Add New Addon\"</a> tool to create an addon now.</td>
								</tr>
								");
							}
				echo "</table>";
// --------------------------------------------------------------------------------
		$ct0=mysql_fetch_row(mysql_query("select count(*) from plan_specs where addon_gid='".addslashes(trim($rs[0]))."'"));
		if ($ct0[0]<=0)
			{
			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 addon_groups set group_payment='".addslashes(trim($cd))."' where addon_gid='".addslashes(trim($rs[0]))."'");

				unset($cd);

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

				echo("
					<table width='100%' cellpadding='0' cellspacing='0' border='0'>
						<tr>
							<td><hr color='#ffffff' noshade></td>
						</tr>
					</table>
					<table width='100%' cellpadding='2' 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='2' cellspacing='0' border='1' bordercolor='#E9E9EB'>
					");
						unset($gg);
						unset($rt);
						unset($tok);

						$gg=1;
						$rt=0;
						$row_p=mysql_query("select pid, name from payment_process where status='1'");
						while ($rs_p=mysql_fetch_row($row_p))
							{
							if ($rt==0)
								{
								echo("
									<tr>
										<td><input type='checkbox' name='xpid[".$rs_p[0]."]' value='".$rs_p[0]."'
									");
								$tok=strtok($group_payment, "|");
								while ($tok) 
									{
									if ($rs_p[0]==$tok) { echo " checked"; }
									$tok=strtok("|");
									}
								echo "> ".stripslashes(trim($rs_p[1]))."</td>";
								$rt=1;
								}
							else if ($rt==1)
								{
								echo "<td><input type='checkbox' name='xpid[".$rs_p[0]."]' value='".$rs_p[0]."'";
								$tok=strtok($group_payment, "|");
								while ($tok) 
									{
									if ($rs_p[0]==$tok) { echo " checked"; }
									$tok=strtok("|");
									}
								echo "> ".stripslashes(trim($rs_p[1]))."</td>";
								$rt=2;
								}
							else if ($rt==2)
								{
								echo "<td><input type='checkbox' name='xpid[".$rs_p[0]."]' value='".$rs_p[0]."'";
								$tok=strtok($group_payment, "|");
								while ($tok) 
									{
									if ($rs_p[0]==$tok) { echo " checked"; }
									$tok=strtok("|");
									}
								echo "> ".stripslashes(trim($rs_p[1]))."</td>";
								$rt=3;
								}
							else
								{
								echo("
										<td><input type='checkbox' name='xpid[".$rs_p[0]."]' value='".$rs_p[0]."'
									");
								$tok=strtok($group_payment, "|");
								while ($tok) 
									{
									if ($rs_p[0]==$tok) { echo " checked"; }
									$tok=strtok("|");
									}
								echo ("
									> ".stripslashes(trim($rs_p[1]))."</td>
									</tr>
									");
								$rt=0;
								}
							$gg++;
							}

						// 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>";
			}
		else
			{
			$row_b=mysql_query("select package_name from plan_specs where addon_gid='".addslashes(trim($addon_gid))."'");
			while ($rs_b=mysql_fetch_row($row_b)) { @$list.=stripslashes($rs_b[0])."<br>"; }
				echo("
					<input type='hidden' name='skip_pp' value='1'>
					<table width='100%' cellpadding='6' cellspacing='0' border='0'>
						<tr>
							<td>&nbsp;<A HREF='javascript:void(0)' class='cleanlink3' onmouseover=\"return overlib('".$list."', CAPTION, 'Group is tied to:');\" onmouseout='nd();'>This group is tied to at least one hosting package and cannot be ordered outside of the hosting package.</a></td>
						</tr>
					</table>
					");
			unset($list);
			}
// --------------------------------------------------------------------------------
echo("		
					</td>
				</tr>
				<tr>
					<td colspan='5'><img src='".$http_images."/space.gif' width='1' height='8'></td>
				</tr>	
			</form>
			");
		$x++;
		}
if ($x==1)
	{
	echo("		
		<tr>
			<td colspan='5' align='left' valign='top'>No addon groups found in the database.</td>
		</tr>
		");
	}
echo("
			</table>
			</td>
		</tr>
	</table>
	");
include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>