<?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(phpversion()<="4.0.6") { $_POST=$HTTP_POST_VARS; }
if (isset($_POST['submit']))
	{
	if (trim($coupon_code)=="") { $e0=true; }
	if (!isset($coupon_discount_type)) { $e1=true; }
	else
		{
		if ($coupon_discount_type==1) { if (trim($coupon_discount_percent)=="") { $e2=true; } }
		else if ($coupon_discount_type==2) { if (trim($coupon_discount_whole)=="") { $e3=true; }	}
		}
	
	if (isset($delete)&&isset($aid))
		{
		/*
		Did not work out...
		$xdelete_bit="<img src='".$http_image."/space.gif' width='16' height='1'>Assigned to:<br>";

		$q_="select ";
		$q_.="addon_gid, ";		// 0
		$q_.="name, ";			// 1
		$q_.="group_addons ";	// 2
		$q_.="from ";
		$q_.="addon_groups";

		$ro_=mysql_query($q_);
		while ($r_=mysql_fetch_row($ro_))
			{
			$addon_gid=stripslashes(trim($r_[0]));
			$name=stripslashes(trim($r_[1]));
			$group_addons=stripslashes(trim($r_[2]));
			
			if (trim($group_addons)!="")
				{
				$total_mark=substr_count($group_addons, "|");
				$explode=explode("|", $group_addons);

				for ($i=0; $i<=($total_mark-1); $i++)
					{
					if ($explode[$i]==$aid) { $xdelete_bit.="<img src='".$http_image."/space.gif' width='16' height='1'><a href='".$http_admin."/addon_group_editor.php?sid=".$sid."'>".$name."</a><br>\n";}
					}
				}

			unset($addon_gid);
			unset($name);
			unset($group_addons);
			}
		
		if (strcmp($xdelete_bit, "Assigned to:<br>")==0)
			{
		*/
			mysql_query("delete from addon_specs where aid='".addslashes(trim($aid))."'");
			$deleted=true;
			$done=true;
		/*
			}
		else
			{
			$delete_bit=$xdelete_bit;
			}
		*/
		}

	if (!isset($e0)&&!isset($e1)&&!isset($e2)&&!isset($e3)&&!isset($e4)/*&&!isset($delete_bit)*/&&!isset($deleted))
		{
		if (trim($coupon_expires_m)!=""
			&&trim($coupon_expires_d)!=""
			&&trim($coupon_expires_y)!="")
			{
			$expires=mktime(0, 0, 0, $coupon_expires_m, $coupon_expires_d, $coupon_expires_y);
			if (date("mdY", $expires)==="12311969") { $expires=""; }
			}
		else { $expires=""; }
		
		if (isset($aid)) { $query="update "; }
		else
			{
			$query="insert ";
			$query.="into ";
			}

		$query.="addon_specs ";
		$query.="set ";
		$query.="coupon='1', ";
		$query.="coupon_terms='".addslashes(trim($coupon_terms))."', ";
		$query.="addon_name='Coupon: ".addslashes(trim(strtolower($coupon_code)))."', ";
		$query.="coupon_code='".addslashes(trim(strtolower($coupon_code)))."', ";
		$query.="coupon_discount_type='".addslashes(trim($coupon_discount_type))."', ";
		if ($coupon_discount_type==1)
			{
			$query.="coupon_discount_percent='".addslashes(trim($coupon_discount_percent))."', ";
			$query.="coupon_discount_whole='', ";
			}
		else 
			{
			$query.="coupon_discount_whole='".addslashes(trim($coupon_discount_whole))."', ";
			$query.="coupon_discount_percent='', ";
			}

		$query.="expires='".addslashes(trim($expires))."' ";
		
		if (isset($aid)) { $query.="where aid='".addslashes(trim($aid))."'"; }
		
		mysql_query($query);
		
		if (!isset($aid)) { $aid=mysql_insert_id(); $insert=true;}
		$done=true;
		}
	}

// End Form Handler
include $server_admin_inc."/header.php";

if (isset($aid))
	{
	$query0="select ";
	$query0.="coupon_code, ";				// 0
	$query0.="coupon_discount_type, ";		// 1
	$query0.="coupon_discount_percent, ";	// 2
	$query0.="coupon_discount_whole, ";		// 3
	$query0.="expires, ";					// 4
	$query0.="coupon_terms ";				// 5
	$query0.="from ";
	$query0.="addon_specs ";
	$query0.="where ";
	$query0.="aid='".addslashes(trim($aid))."' ";
	$query0.="and ";
	$query0.="coupon='1' ";
	$query0.="limit 0, 1";
	
	$rs0=mysql_fetch_row(mysql_query($query0));

	$coupon_code=stripslashes(trim($rs0[0]));
	$coupon_discount_type=stripslashes(trim($rs0[1]));
	$coupon_discount_percent=stripslashes(trim($rs0[2]));
	if ($coupon_discount_percent==0) { $coupon_discount_percent=""; }
	$coupon_discount_whole=stripslashes(trim($rs0[3]));
	if ($coupon_discount_whole==0) { $coupon_discount_whole=""; }
	$expires=stripslashes(trim($rs0[4]));
	if (trim($expires)!=""&&date("mdY", $expires)!="12311969")
		{
		$coupon_expires_m=date("m", $expires);
		$coupon_expires_d=date("d", $expires);
		$coupon_expires_y=date("Y", $expires);
		}
	$coupon_terms=stripslashes(trim($rs0[5]));
	if ($coupon_terms==0) { $coupon_terms=1; }
	}

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'>
	");
if (isset($done))
	{
	echo("
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/menu_arrow.gif'><b>The coupon was <font color='#990000'>".((isset($insert))?"added":"".((isset($deleted))?"deleted":"updated")."")." successfully!</font><b></td>
				</tr>
				<tr>
					<td align='left' valign='middle'><hr color='#F3F3F3'></td>
				</tr>
		");
	if (!isset($deleted))
		{
		echo("
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/error_arrow.gif'><a href='".$http_admin."/coupon_build.php?sid=".$sid."&aid=".$aid."'>Continue with edit</a></td>
				</tr>
			");
		}
	echo("
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/error_arrow.gif'><a href='".$http_admin."/coupon_edit.php?sid=".$sid."'>Work with another coupon</a></td>
				</tr>
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/error_arrow.gif'><a href='".$http_admin."/coupon_build.php?sid=".$sid."'>Add new coupon</a></td>
				</tr>
			</table>
			</td>
			</tr>
		</table>
		");

	include $server_admin_inc."/footer.php";
	mysql_close($dblink);
	die();
	}

echo("
			<form action='".$PHP_SELF."' method='POST'>
			<input type='hidden' name='sid' value='".$sid."'>
			".((isset($aid))?"<input type='hidden' name='aid' value='".$aid."'>":"")."
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/menu_arrow.gif'><b>Coupon Manager</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/coupon_edit.php?sid=".$sid."'>Back to the Editor</a>]</td>
				</tr>
				<tr>
					<td align='left' valign='middle'><hr color='#F3F3F3'></td>
				</tr>
			</table>
	");

if (isset($e0))
	{
	echo("
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/error_arrow.gif'><font color='#990000'>Please enter a coupon code to continue.</font></td>
				</tr>
			</table>
		");
	}

if (isset($e1))
	{
	echo("
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/error_arrow.gif'><font color='#990000'>Please choose a coupon discount type to continue.</font></td>
				</tr>
			</table>
		");
	}

if (isset($e2))
	{
	echo("
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/error_arrow.gif'><font color='#990000'>Please choose a coupon discount percentage to continue.</font></td>
				</tr>
			</table>
		");
	}

if (isset($e3))
	{
	echo("
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/error_arrow.gif'><font color='#990000'>Please choose a whole dollar coupon discount to continue.</font></td>
				</tr>
			</table>
		");
	}


if (isset($delete_bit))
	{
	echo("
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='middle'><img src='".$http_images."/error_arrow.gif'><font color='#990000'>Please remove this coupon from associated hosting groups before you delete.</font></td>
				</tr>
				<tr>
					<td align='left' valign='middle'>".$delete_bit."</td>
				</tr>
			</table>
		");
	}

if (isset($e0)||isset($e1)||isset($e2)||isset($e3)||isset($delete_bit))
	{
	echo("
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td colspan='2' align='left' valign='middle'><img src='".$http_images."/space.gif' width='1' height='4'></td>
				</tr>
			</table>
		");
	}

echo("
			<table width='100%' cellpadding='4' cellspacing='0' border='0'>
				<tr>
					<td width='25%' align='left' valign='top'>Discount Type:</td>
					<td width='75%' align='left' valign='top'><input type='radio' name='coupon_terms' value='1'".((isset($coupon_terms))?"".(($coupon_terms==1)?" checked":"")."":" checked")."> One Time Discount<br><input type='radio' name='coupon_terms' value='2'".((isset($coupon_terms))?"".(($coupon_terms==2)?" checked":"")."":"")."> Monthly Discount</td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='top'>Enter Coupon Code:</td>
					<td width='75%' align='left' valign='middle'><input ".$input_style." type='text' name='coupon_code' size='40' maxlength='255' value='".((isset($coupon_code))?"".$coupon_code."":"")."'> <font color='#990000' size='1'><br>&nbsp;* AutoPilot will convert to all lowercase</font></td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='middle'>Discount Details:</td>
					<td width='75%' align='left' valign='middle'>
					<table width='100%' cellpadding='0' cellspacing='0' border='0'>
						<tr>
							<td width='28%' align='left' valign='middle'><input type='radio' name='coupon_discount_type' value='1'".((isset($coupon_discount_type))?"".(($coupon_discount_type==1)?" checked":"")."":" checked")."> Percentage</td>
							<td width='72%' align='left' valign='middle'>.&nbsp;<input ".$input_style." type='text' name='coupon_discount_percent' size='5' maxlength='10' value='".((isset($coupon_discount_percent))?"".$coupon_discount_percent."":"")."'>&nbsp;%&nbsp;&nbsp;&nbsp;<font color='#990000'>ex. 10</font></td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='middle'></td>
					<td width='75%' align='left' valign='middle'>
					<table width='100%' cellpadding='0' cellspacing='0' border='0'>
						<tr>
							<td width='28%' align='left' valign='middle'><input type='radio' name='coupon_discount_type' value='2'".((isset($coupon_discount_type))?"".(($coupon_discount_type==2)?" checked":"")."":"")."> Whole Dollar Amount</td>
							<td width='72%' align='left' valign='middle'>$&nbsp;<input ".$input_style." type='text' name='coupon_discount_whole' size='5' maxlength='10' value='".((isset($coupon_discount_whole))?"".$coupon_discount_whole."":"")."'></td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='middle'>Coupon Expiration Date:</td>
					<td width='75%' align='left' valign='middle'><input ".$input_style." type='text' name='coupon_expires_m' size='2' maxlength='2' value='".((isset($coupon_expires_m))?"".$coupon_expires_m."":"")."'>&nbsp;/&nbsp;<input ".$input_style." type='text' name='coupon_expires_d' size='2' maxlength='2' value='".((isset($coupon_expires_d))?"".$coupon_expires_d."":"")."'>&nbsp;/&nbsp;<input ".$input_style." type='text' name='coupon_expires_y' size='4' maxlength='4' value='".((isset($coupon_expires_y))?"".$coupon_expires_y."":"")."'>&nbsp;&nbsp;&nbsp;<font color='#990000' size='1'>ex. mm/dd/yyyy</font></td>
				</tr>
	");
if (isset($aid))
	{
	echo("
				<tr>
					<td width='25%' align='left' valign='top'>Delete this Coupon:</td>
					<td width='75%' align='left' valign='middle'><input type='checkbox' name='delete' value='1'> Check here to delete this coupon now.</td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='top'></td>
					<td width='75%' align='left' valign='middle'><font color='#990000'><b>Warning:</b> You must first delete this coupon from any<br>hosting groups that it may be assigned to.</font></td>
				</tr>
		");
	}
echo("
				<tr>
					<td colspan='2' align='left' valign='middle'><img src='".$http_images."/space.gif' width='1' height='4'></td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='middle'></td>
					<td width='75%' align='left' valign='middle'><input ".$button_style." type='submit' name='submit' value='Continue'></td>
				</tr>
				<tr>
					<td colspan='2' align='left' valign='middle'><img src='".$http_images."/space.gif' width='1' height='12'></td>
				</tr>
			</table>
			
			</td>
			</tr>
		</table>
		");

include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>