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

// Start Form Handler

if (isset($submit))
	{
	$update="update plan_specs set ";
	$update.="com_type='".addslashes(trim($com_type))."', ";
	$update.="com_rate_onetime='".addslashes(trim($com_rate_onetime))."', ";
	$update.="com_rate_month='".addslashes(trim($com_rate_month))."' ";
	$update.="where pid='".addslashes(trim($pid))."' ";

	mysql_query($update);

	$done=true;
	}

// End Form Handler

include $server_admin_inc."/header.php";
$query="select ";
$query.="plan_specs.package_name, ";		// 0
$query.="plan_groups.name, ";				// 1
$query.="plan_specs.com_type, ";			// 2
$query.="plan_specs.com_rate_month, ";		// 3
$query.="plan_specs.com_rate_onetime, ";	// 4
$query.="plan_specs.monthly_cost, ";		// 5
$query.="plan_specs.quarterly_cost, ";		// 6
$query.="plan_specs.semi_annual_cost, ";	// 7
$query.="plan_specs.annual_cost ";			// 8
$query.="from ";
$query.="plan_specs, ";
$query.="plan_groups ";
$query.="where ";
$query.="plan_specs.pid='".addslashes(trim($pid))."' ";
$query.="and ";
$query.="plan_specs.gid=plan_groups.gid";

$rs=mysql_fetch_row(mysql_query($query));

$monthly_cost=stripslashes(trim($rs[5]));
$quarterly_cost=stripslashes(trim($rs[6]));
$semi_annual_cost=stripslashes(trim($rs[7]));
$annual_cost=stripslashes(trim($rs[8]));

if ($monthly_cost!="-1") { $base=$monthly_cost; }
else if ($quarterly_cost!="-1") { $base=$quarterly_cost; }
else if ($semi_annual_cost!="-1") { $base=$semi_annual_cost; }
else if ($annual_cost!="-1") { $base=$annual_cost; }
$com_monthly=$rs[3];

// ok, all done - Plan added
if (isset($done))
	{
	echo("
		<table width='725' cellpadding='2' cellspacing='0' border='0'>
			<tr>
				<td align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'>The affiliate plan was updated successfully!</td>
			</tr>
			<tr>
				<td align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'><a href='".$http_admin."/affiliate_plan_edit.php?sid=".trim($sid)."'>Go back to the edit menu</a>.</td>
			</tr>
			<tr>
				<td align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Make edits to the affiliate plan for <b>".$package_name."</b> below:</td>
			</tr>
			<tr>
				<td align='left' valign='top'><hr align='left' valign='top' color='#F0EFED' width='725'></td>
			</tr>
		</table>
		");
	}

echo("
<form action='".$PHP_SELF."' method='POST' name='form1'>
<input type='hidden' name='pid' value='".trim($pid)."'>
<input type='hidden' name='sid' value='".trim($sid)."'>
<table width='700' border='0' cellspacing='1' cellpadding='2'>
	<tr>
		<td colspan='2'><img src='".$http_images."/menu_arrow.gif'><b>Manage Affiliate Plan</b></td>
	</tr>
	<tr>
		<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Package Name:</td>
		<td width='75%' align='left' valign='top'>".$rs[1].": ".$rs[0]."</td>
	</tr>
	<tr>
		<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Commission Type:</td>
		<td width='75%' align='left' valign='middle'>
		<table width='100%' cellpadding='0' cellspacing='0'>
			<tr>
				<td width='5%' align='left' valign='middle'><input type='radio' name='com_type' value='1'".(($rs[2]==1)?" checked":"")." onclick='document.form1.com_rate_month.disabled=true; document.form1.com_rate_onetime.disabled=false;'></td>
				<td width='30%' align='left' valign='middle'>Enable One Time Payout</td>
				<td width='65%' align='left' valign='middle'></td>
			</tr>
			<tr>
				<td width='5%' align='left' valign='middle'><input type='radio' name='com_type' value='2'".(($rs[2]==2)?" checked":"")." onclick='document.form1.com_rate_onetime.disabled=true; document.form1.com_rate_month.disabled=false;'></td>
				<td width='30%' align='left' valign='middle'>Enable Monthly Percentage</td>
				<td width='65%' align='left' valign='middle'>".affiliate_month_percentage($currency, $currency_type, $base, $com_monthly)."</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>One Time Payout Rate:</td>
		<td width='75%' align='left' valign='top'>".$currency." <input ".$input_style." size='10' maxlength='15' type='text' name='com_rate_onetime' value='".(($rs[4]==0)?"":"".$rs[4]."")."'".(($rs[2]==2)?" disabled":"").">".$currency_type."</td>
	</tr>
	<tr>
		<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Monthly Payout Rate:</td>
		<td width='75%' align='left' valign='top'><input ".$input_style." size='5' maxlength='15' type='text' name='com_rate_month' value='".(($rs[3]==0)?"":"".$rs[3]."")."'".(($rs[2]==1)?" disabled":"")."> %&nbsp;&nbsp;&nbsp;ex. 20</td>
	</tr>
	<tr>
		<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'></td>
		<td width='75%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'></td>
	</tr>
	<tr>
		<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'></td>
		<td width='75%' align='left' valign='top'><input ".$button_style." type='submit' name='submit' value='Update Affiliate Plan'></td>
	</tr>
</table>
</form>
	");
// End Main Process
mysql_close($dblink);
include $server_admin_inc."/footer.php";
?>