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


if(phpversion() <= "4.0.6") { $_POST=$HTTP_POST_VARS; }
if (isset($_POST['x_Submit']))
	{
	if (strlen(trim($_POST['customer_first_name']))==0) { $e0=true; }
	if (strlen(trim($_POST['customer_last_name']))==0) { $e1=true; }
	if (strlen(trim($_POST['customer_address']))==0) { $e2=true; }
	if (strlen(trim($_POST['customer_city']))==0) { $e3=true; }
	if (strlen(trim($_POST['customer_zip']))==0) { $e4=true; }
	if (strlen(trim($_POST['customer_phone']))<10) { $e5=true; }
	if ($_POST['card_select']==0)
		{
		if (strlen(trim($_POST['x_Card_Num']))==0) { $e6=true; }
		if (strlen(trim($_POST['x_Exp_Date_m']))==0||strlen(trim($_POST['x_Exp_Date_y']))==0) { $e7=true; }
		}

	if (!isset($e0)&&!isset($e1)&&!isset($e2)&&!isset($e3)&&!isset($e4)&&!isset($e5)&&!isset($e6)&&!isset($e7))
		{
		$query1="update ";
		$query1.="authnet_master_cc ";
		$query1.="set ";
		$query1.="master_cc='0' ";
		$query1.="where uid='".addslashes(trim($uid))."'";

		mysql_query($query1);

		if ($_POST['card_select']==0)
			{
			$card_info=$_POST['x_Card_Num']."|".$_POST['x_Exp_Date_m'].$_POST['x_Exp_Date_y']."|0|";
			$card_info=e("576cb7f68040520768bf51c75f7f4c84", $card_info);

			$query1="insert into ";
			$query1.="authnet_master_cc ";
			$query1.="set ";
			$query1.="customer_first_name='".addslashes(trim($_POST['customer_first_name']))."', ";
			$query1.="customer_last_name='".addslashes(trim($_POST['customer_last_name']))."', ";
			$query1.="customer_address='".addslashes(trim($_POST['customer_address']))."', ";
			$query1.="customer_city='".addslashes(trim($_POST['customer_city']))."', ";
			$query1.="customer_state='".addslashes(trim($_POST['customer_state']))."', ";
			$query1.="customer_zip='".addslashes(trim($_POST['customer_zip']))."', ";
			$query1.="customer_country='".addslashes(trim($_POST['x_Country']))."', ";
			$query1.="customer_phone='".addslashes(trim($_POST['customer_phone']))."', ";
			$query1.="customer_fax='".addslashes(trim($_POST['customer_fax']))."', ";
			$query1.="customer_email='".addslashes(trim($_POST['customer_email']))."', ";
			$query1.="card_info='".addslashes(trim($card_info))."', ";
			$query1.="uid='".addslashes(trim($uid))."', ";
			$query1.="master_cc='1', ";
			$query1.="ogcreate='".time()."'";

			mysql_query($query1);

			$ccid=mysql_insert_id();

			$query="update ";
			$query.="authnet_batch ";
			$query.="set ";
			$query.="ccid='".addslashes(trim($ccid))."' ";
			$query.="where ";
			$query.="bid='".addslashes(trim($bid))."'";

			mysql_query($query);
			}
		else 
			{
			$query1="update ";
			$query1.="authnet_master_cc ";
			$query1.="set ";
			$query1.="customer_first_name='".addslashes(trim($_POST['customer_first_name']))."', ";
			$query1.="customer_last_name='".addslashes(trim($_POST['customer_last_name']))."', ";
			$query1.="customer_address='".addslashes(trim($_POST['customer_address']))."', ";
			$query1.="customer_city='".addslashes(trim($_POST['customer_city']))."', ";
			$query1.="customer_state='".addslashes(trim($_POST['customer_state']))."', ";
			$query1.="customer_zip='".addslashes(trim($_POST['customer_zip']))."', ";
			$query1.="customer_country='".addslashes(trim($_POST['x_Country']))."', ";
			$query1.="customer_phone='".addslashes(trim($_POST['customer_phone']))."', ";
			$query1.="customer_fax='".addslashes(trim($_POST['customer_fax']))."', ";
			$query1.="customer_email='".addslashes(trim($_POST['customer_email']))."', ";
			$query1.="master_cc='1' ";
			$query1.="where ";
			$query1.="ccid='".addslashes(trim($_POST['card_select']))."'";

			mysql_query($query1);

			$query="update ";
			$query.="authnet_batch ";
			$query.="set ";
			$query.="ccid='".addslashes(trim($_POST['card_select']))."' ";
			$query.="where ";
			$query.="bid='".addslashes(trim($bid))."'";

			mysql_query($query);
			}

		header("Location: ".$http_admin."/view_batch.php?sid=".$sid);
		exit;
		}
	}

include $server_admin_inc."/header.php";

if (isset($ccid))
	{
	$query="select ";
	$query.="customer_first_name, ";	// 0
	$query.="customer_last_name, ";		// 1
	$query.="customer_address, ";		// 2
	$query.="customer_city, ";			// 3
	$query.="customer_state, ";			// 4
	$query.="customer_zip, ";			// 5
	$query.="customer_country, ";		// 6
	$query.="customer_phone, ";			// 7
	$query.="customer_fax, ";			// 8
	$query.="customer_email, ";			// 9
	$query.="card_info, ";				// 10
	$query.="uid, ";					// 11
	$query.="count(*) ";				// 12
	$query.="from ";
	$query.="authnet_master_cc ";
	$query.="where ccid='".addslashes(trim($ccid))."' ";
	$query.="group by customer_first_name";

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

	$customer_first_name=stripslashes(trim($rs[0]));
	$customer_last_name=stripslashes(trim($rs[1]));
	$customer_address=stripslashes(trim($rs[2]));
	$customer_city=stripslashes(trim($rs[3]));
	$customer_state=stripslashes(trim($rs[4]));
	$customer_zip=stripslashes(trim($rs[5]));
	$customer_country=stripslashes(trim($rs[6]));
	$customer_phone=stripslashes(trim($rs[7]));
	$customer_fax=stripslashes(trim($rs[8]));
	$customer_email=stripslashes(trim($rs[9]));
	$card_info=stripslashes(trim($rs[10]));
	$uid=stripslashes(trim($rs[11]));
	$count=stripslashes(trim($rs[12]));

	if (isset($mb))
		{
		$q=mysql_fetch_row(mysql_query("select uid, oid, description from authnet_batch where bid='".addslashes(trim($bid))."'"));
		$uid=stripslashes(trim($q[0]));
		$oid=stripslashes(trim($q[1]));
		$batch_desc=stripslashes(trim($q[2]));
		}
	}


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'>
	<form action='".$PHP_SELF."' method='POST'>
	<input type='hidden' name='sid' value='".$sid."'>
	".((isset($c))?"<input type='hidden' name='c' value='".$c."'>":"")."
	".((isset($oid))?"<input type='hidden' name='oid' value='".$oid."'>":"")."
	".((isset($uid))?"<input type='hidden' name='uid' value='".$uid."'>":"")."
	".((isset($bid))?"<input type='hidden' name='bid' value='".$bid."'>":"")."
	".((isset($mb))?"<input type='hidden' name='mb' value='5'>":"")."
	<table width='100%' cellpadding='2' cellspacing='0' border='0'>
	");
if (isset($c))
	{
	echo("
			<tr>
				<td colspan='2' align='left' valign='middle'>[&nbsp;<a href='".$http_admin."/view_batch.php?sid=".$sid."&c=".$c."'>Go Back</a>&nbsp;]&nbsp;&nbsp;<b>
		");
	if ($c==0) { echo "Open Batch Editor";}
	else if ($c==1) { echo "Completed Batch History";}
	else if ($c==2) { echo "Declined Batch History";}
	else if ($c==3) { echo "Error Batch History";}
	echo ("
		</b></td>
			</tr>
			<tr>
				<td colspan='2' align='left' valign='middle'><hr color='#F0EFED'></td>
			</tr>
		");
	}

echo "</table>";

if (isset($e0))
	{
	echo("
			<table width='85%' cellpadding='2' cellspacing='0' border='0' align='center'>
				<tr>
					<td><font color='#C60C08'><b>ERROR!</b></font> Please enter the card holders first name to continue.</td>
				</tr>
			</table>
			");
	}

if (isset($e1))
	{
	echo("
			<table width='85%' cellpadding='2' cellspacing='0' border='0' align='center'>
				<tr>
					<td><font color='#C60C08'><b>ERROR!</b></font> Please enter the card holders last name to continue.</td>
				</tr>
			</table>
			");
	}

if (isset($e2))
	{
	echo("
		<table width='85%' cellpadding='2' cellspacing='0' border='0' align='center'>
			<tr>
				<td><font color='#C60C08'><b>ERROR!</b></font> Please enter the card holders address to continue.</td>
			</tr>
		</table>
		");
	}

if (isset($e3))
	{
	echo("
		<table width='85%' cellpadding='2' cellspacing='0' border='0' align='center'>
			<tr>
				<td><font color='#C60C08'><b>ERROR!</b></font> Please enter the card holders city to continue.</td>
			</tr>
		</table>
		");
	}

if (isset($e4))
	{
	echo("
		<table width='85%' cellpadding='2' cellspacing='0' border='0' align='center'>
			<tr>
				<td><font color='#C60C08'><b>ERROR!</b></font> Please enter the card holders zip code to continue.</td>
			</tr>
		</table>
		");
	}

if (isset($e5))
	{
	echo("
		<table width='85%' cellpadding='2' cellspacing='0' border='0' align='center'>
			<tr>
				<td><font color='#C60C08'><b>ERROR!</b></font> Please enter the card holders phone number to continue.</td>
			</tr>
		</table>
		");
	}

if (isset($e6))
	{
	echo("
		<table width='85%' cellpadding='2' cellspacing='0' border='0' align='center'>
			<tr>
				<td><font color='#C60C08'><b>ERROR!</b></font> Please enter the card number to continue.</td>
			</tr>
		</table>
		");
	}

if (isset($e7))
	{
	echo("
		<table width='85%' cellpadding='2' cellspacing='0' border='0' align='center'>
			<tr>
				<td><font color='#C60C08'><b>ERROR!</b></font> Please enter the card expiration dates to continue.</td>
			</tr>
		</table>
		");
	}

echo("
	<table width='100%' cellpadding='2' cellspacing='0' border='0'>
		<tr>
			<td colspan='2' align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Credit Card Personal Information</b><td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>First Name:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='40' maxlength='50' type='text' name='customer_first_name' value='".((isset($customer_first_name))?"".$customer_first_name."":"")."'></td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Last Name:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='40' maxlength='50' type='text' name='customer_last_name' value='".((isset($customer_last_name))?"".$customer_last_name."":"")."'></td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Address:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='40' maxlength='60' type='text' name='customer_address' value='".((isset($customer_address))?"".$customer_address."":"")."'></td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>City:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='40' maxlength='40' type='text' name='customer_city' value='".((isset($customer_city))?"".$customer_city."":"")."'></td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>State/Province:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='40' maxlength='40' type='text' name='customer_state' value='".((isset($customer_state))?"".$customer_state."":"")."'><BR>( 2 Digits State/Province Abbreviation )</td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Zip Code:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='20' maxlength='20' type='text' name='customer_zip' value='".((isset($customer_zip))?"".$customer_zip."":"")."'></td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Country:</td>
			<td width='65%' align='left' valign='top'>".display_country($customer_country, $input_style)."</td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Phone:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='15' maxlength='15' type='text' name='customer_phone' value='".((isset($customer_phone))?"".$customer_phone."":"")."'></td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Fax:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='15' maxlength='15' type='text' name='customer_fax' value='".((isset($customer_fax))?"".$customer_fax."":"")."'></td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Email Address:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='40' maxlength='255' type='text' name='customer_email' value='".((isset($customer_email))?"".$customer_email."":"")."'></td>
		</tr>
		<tr>
			<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='10'><td>
		</tr>
		<tr>
			<td colspan='2' align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Credit Card Information</b><td>
		</tr>
	");

if ($count>=1)
	{
	echo("
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Use Exsiting Card:</td>
			<td width='65%' align='left' valign='top'><select name='card_select' ".$input_style.">
			<option value='0'>No, Enter New Card Information Below</option>
		");
	$query3="select ";
	$query3.="ccid, ";
	$query3.="card_info, ";
	$query3.="master_cc ";
	$query3.="from ";
	$query3.="authnet_master_cc ";
	$query3.="where ";
	$query3.="uid='".addslashes(trim($uid))."' ";

	$row3=mysql_query($query3);
	while ($rs3=mysql_fetch_row($row3))
		{
		list($card_num, $card_exp, $card_code)=explode("|", d("576cb7f68040520768bf51c75f7f4c84", $rs3[1]));

		echo "<option value='".$rs3[0]."'".(($rs3[2]==1)?" selected":"").">".substr($card_num, 0, 2)."**********".substr($card_num, -4)."</option>\n";
		}

	unset($x_Card_Num);
	unset($x_Exp_Date_y);
	unset($x_Exp_Date_m);
	unset($x_Card_Code);
	echo("
				</select></td>
			</tr>
		");
	}
echo("
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>New Card Number:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='40' maxlength='22' type='text' name='x_Card_Num'></td>
		</tr>
		<tr>
			<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>New Card Expiration:</td>
			<td width='65%' align='left' valign='top'><input ".$input_style." size='2' maxlength='2' type='text' name='x_Exp_Date_m'>&nbsp;/&nbsp;<input ".$input_style." size='2' maxlength='2' type='text' name='x_Exp_Date_y'>&nbsp;<font color='#990000'>ex. MM/YY</font></td>
		</tr>
		<tr>
			<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='10'><td>
		</tr>
	");
if ($c!=1)
	{
	echo("
			<tr>
				<td width='35%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'></td>
				<td width='65%' align='left' valign='top'><input ".$button_style." type='submit' name='x_Submit' value='Continue'></td>
			</tr>
			<tr>
				<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='10'><td>
			</tr>
		");
	}
echo("
	</table>
	</form>
			</td>
		</tr>
	</table>
	");

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