<?PHP
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/select.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";
include $server_inc."/invoice_functions.php";  

# start search
if (isset($submitx))
	{
	if (trim($uid)=="0") { header("Location: ".$http_admin."/client_management.php?sid=".$sid); exit;	}
	else { header("Location: ".$http_admin."/client_management.php?sid=".$sid."&uid=".$uid); exit; }
	}

if (isset($submit_search))
	{
	$qct0="select ";
	$qct0.="count(*) ";
	$qct0.="from ";
	$qct0.="user ";
	$qct0.="where ";
	$qct0.="last_name ";
	$qct0.="like '%".addslashes(trim($search_last_name))."%'";

	$ct=mysql_fetch_row(mysql_query($qct0));
	if ($ct[0]<=1)
		{
		$qrs0="select ";
		$qrs0.="uid ";
		$qrs0.="from ";
		$qrs0.="user ";
		$qrs0.="where ";
		$qrs0.="last_name ";
		$qrs0.="like '%".addslashes(trim($search_last_name))."%' ";
		$qrs0.="limit 0, 1";

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

		if ($rs!=true) { header("Location: ".$http_admin."/client_management.php?sid=".$sid); exit; }
		else if ($rs==true) { header("Location: ".$http_admin."/client_management.php?sid=".$sid."&uid=".$rs[0]); exit; }
		}
	else
		{
		$list_all="";
		$qrs0="select ";
		$qrs0.="uid ";
		$qrs0.="from ";
		$qrs0.="user ";
		$qrs0.="where ";
		$qrs0.="last_name ";
		$qrs0.="like '%".addslashes(trim($search_last_name))."%'";
		$row=mysql_query($qrs0);
		while ($rs=mysql_fetch_row($row)) { $list_all.=$rs[0]."|"; }

		header("Location: ".$http_admin."/client_management.php?sid=".$sid."&list_all=".$list_all);
		exit;
		}
	}
# end search
# --------------------------------------------------------------------------------------------------------------------
# start client entry
if (isset($submit))
	{
	if (strlen(trim($first_name))==0) {$err0=true;} else {$n0=true;}
	if (strlen(trim($last_name))==0) {$err1=true;} else {$n1=true;}
	if (strlen(trim($street_address_1))==0) {$err2=true;} else {$n2=true;}
	if (strlen(trim($city))==0) {$err3=true;} else {$n3=true;}
	if (strlen(trim($state))==0) {$err4=true;} else {$n4=true;}
	if (strlen(trim($zip_code))==0) {$err5=true;} else {$n5=true;}
	if (strlen(trim($country))==0) {$err6=true;} else {$n6=true;}
	if (strlen(trim($phone))==0) {$err7=true;} else {$n7=true;}

	if (isset($uid)&&strlen(trim($password))==0&&strlen(trim($password2))==0) { $n10=true; }
	else { if (strcmp($password, $password2)!=0||strlen(trim($password))==0) {$err10=true;} else {$n10=true;} }

	if (!isset($uid))
		{
		if (strlen(trim($email))==0) {$err8=true;} else {$n8=true;}
		if (strlen(trim($username))==0) {$err9=true;} else {$n9=true;}
		if (check_user($username)==1) {$err14=true;} else {$n14=true;}
		if (check_email($email)==1) {$err15=true;} else {$n15=true;}
		}
	else { $n8=true; $n9=true; $n14=true; $n15=true; }
	if (trim($domain_name)=="") {$err16=true;} else {$n16=true;}

	if (isset($n0)&&isset($n1)&&isset($n2)&&isset($n3)
		&&isset($n4)&&isset($n5)&&isset($n6)&&isset($n7)
		&&isset($n8)&&isset($n9)&&isset($n10)&&isset($n14)
		&&isset($n15)&&isset($n16))
		{
		if (isset($uid))
			{
			$update="update user set ";
			$update.="first_name='".addslashes(trim($first_name))."', ";
			$update.="last_name='".addslashes(trim($last_name))."', ";
			$update.="organization_name='".addslashes(trim($organization_name))."', ";
			$update.="street_address_1='".addslashes(trim($street_address_1))."', ";
			$update.="street_address_2='".addslashes(trim($street_address_2))."', ";
			$update.="city='".addslashes(trim($city))."', ";
			$update.="state='".addslashes(trim($state))."', ";
			$update.="zip_code='".addslashes(trim($zip_code))."', ";
			$update.="country='".addslashes(trim($country))."', ";
			$update.="phone='".addslashes(trim($phone))."', ";
			$update.="fax='".addslashes(trim($fax))."', ";
			if (strlen(trim($password))!=0) { $update.="password='".md5(strtolower(trim($password)))."', "; }
			$update.="advert='".addslashes(trim($advert))."', ";
			$update.="advert_other='".addslashes(trim($advert_other))."', ";
			$update.="sid='".create_sid()."',";
			$update.="ogcreate='".time()."',";
			$update.="tos='1' ";
			$update.="where uid='".addslashes(trim($uid))."'";

			mysql_query($update);
			
			list($pid, $payment_term)=split("[|]", $xpid);

			$nq="insert into hosting_order set ";
			$nq.="ogcreate='".time()."', ";
			$nq.="uid='".addslashes(trim($uid))."', ";
			$nq.="pid='".addslashes(trim($pid))."', ";
			$nq.="domain_name='".addslashes(trim($domain_name))."', ";
			$nq.="domain_expire='".addslashes(trim($domain_expire))."', ";
			if (isset($resolved)&&trim($resolved)!="") 
				{ 
				$nq.="resolved='1', "; 
				$nq.="resolved_time='".time()."', "; 
				}
			else { $nq.="resolved='0', "; }
			if (isset($cont_resolve)&&trim($cont_resolve)!="") { $nq.="cont_resolve='1', "; }
			else { $nq.="cont_resolve='0', "; }
			$nq.="payment_term='".addslashes(trim($payment_term))."', ";
			$nq.="next_due_date='".time()."'";
			
			mysql_query($nq);

			$oid=mysql_insert_id();
			
			create_new_invoice_shell($oid);
		
			$nq2="update ";
			$nq2.="hosting_order ";
			$nq2.="set ";
			$nq2.="next_due_date='".create_next_due(time(), convert_payment_term_to_number($payment_term))."' ";
			$nq2.="where ";
			$nq2.="oid='".addslashes(trim($oid))."'";
			
			mysql_query($nq2);

			header("Location: ".$http_admin."/client_view.php?sid=".$sid."&oid=".$oid."&status=99");
			exit;
			}
		else
			{
			// put order in system
			// insert user
			$insert="insert into user set ";
			$insert.="first_name='".addslashes(trim($first_name))."', ";
			$insert.="last_name='".addslashes(trim($last_name))."', ";
			if (strlen(trim($organization_name))!=0) { $insert.="organization_name='".addslashes(trim($organization_name))."', "; }
			$insert.="street_address_1='".addslashes(trim($street_address_1))."', ";
			if (strlen(trim($street_address_2))!=0) { $insert.="street_address_2='".addslashes(trim($street_address_2))."', "; }
			$insert.="city='".addslashes(trim($city))."', ";
			$insert.="state='".addslashes(trim($state))."', ";
			$insert.="zip_code='".addslashes(trim($zip_code))."', ";
			$insert.="country='".addslashes(trim($country))."', ";
			$insert.="phone='".addslashes(trim($phone))."', ";
			$insert.="fax='".addslashes(trim($fax))."', ";
			$insert.="email='".addslashes(trim($email))."', ";
			$insert.="username='".addslashes(trim($username))."', ";
			$insert.="password='".md5(strtolower(trim($password)))."', ";
			$insert.="advert='OTHER', ";
			$insert.="advert_other='Manually Created', ";
			$insert.="tos='1',";
			$insert.="sid='".create_sid()."',";
			$insert.="ogcreate='".time()."'";

			mysql_query($insert);

			$uid=mysql_insert_id();
			
			list($pid, $payment_term)=split("[|]", $xpid);
   
			$nq="insert into hosting_order set ";
			$nq.="ogcreate='".time()."', ";
			$nq.="uid='".addslashes(trim($uid))."', ";
			$nq.="pid='".addslashes(trim($pid))."', ";
			$nq.="domain_name='".addslashes(trim($domain_name))."', ";
			$nq.="domain_expire='".addslashes(trim($domain_expire))."', ";
			if (isset($resolved)&&trim($resolved)!="") 
				{ 
				$nq.="resolved='1', "; 
				$nq.="resolved_time='".time()."', "; 
				}
			else { $nq.="resolved='0', "; }
			if (isset($cont_resolve)&&trim($cont_resolve)!="") { $nq.="cont_resolve='1', "; }
			else { $nq.="cont_resolve='0', "; }
			$nq.="payment_term='".addslashes(trim($payment_term))."', ";

			# $next_due_date=create_next_due(time(), convert_payment_term_to_number($payment_term));
			# $nq.="next_due_date='".$next_due_date."'";
			$nq.="next_due_date='".time()."'";
			
			mysql_query($nq);

			$oid=mysql_insert_id();

			create_new_invoice_shell($oid);

			$nq2="update ";
			$nq2.="hosting_order ";
			$nq2.="set ";
			$nq2.="next_due_date='".create_next_due(time(), convert_payment_term_to_number($payment_term))."' ";
			$nq2.="where ";
			$nq2.="oid='".addslashes(trim($oid))."'";
			
			mysql_query($nq2);

			header("Location: ".$http_admin."/client_view.php?sid=".$sid."&oid=".$oid."&status=99");
			exit;
			}
		}
	}
# end client entry

# general variables
if (isset($uid))
	{
	$qrs4="select ";
	$qrs4.="first_name, ";			// 0
	$qrs4.="last_name, ";			// 1
	$qrs4.="organization_name, ";	// 2
	$qrs4.="street_address_1, ";	// 3
	$qrs4.="street_address_2, ";	// 4
	$qrs4.="city, ";				// 5
	$qrs4.="state, ";				// 6
	$qrs4.="zip_code, ";			// 7
	$qrs4.="country, ";				// 8
	$qrs4.="phone, ";				// 9
	$qrs4.="fax, ";					// 10
	$qrs4.="email, ";				// 11
	$qrs4.="username ";				// 12
	$qrs4.="from ";		
	$qrs4.="user ";
	$qrs4.="where ";
	$qrs4.="uid='".addslashes(trim($uid))."'";

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

	$first_name=stripslashes(trim($rs[0]));
	$last_name=stripslashes(trim($rs[1]));
	$organization_name=stripslashes(trim($rs[2]));
	$street_address_1=stripslashes(trim($rs[3]));
	$street_address_2=stripslashes(trim($rs[4]));
	$city=stripslashes(trim($rs[5]));
	$state=stripslashes(trim($rs[6]));
	$zip_code=stripslashes(trim($rs[7]));
	$country=stripslashes(trim($rs[8]));
	$phone=stripslashes(trim($rs[9]));
	$fax=stripslashes(trim($rs[10]));
	$email=stripslashes(trim($rs[11]));
	$username=stripslashes(trim($rs[12]));
	}
# end general variables

# start GUI
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'>
	");
include $server_admin_tools."/client_management_select_inc.php";
include $server_admin_tools."/client_management_errors_inc.php";
include $server_admin_tools."/client_management_client_form_inc.php";
echo("
			</td>
		</tr>
		<tr>
			<td colspan='2'><img src='".$http_images."/space.gif' width='1' height='6'></td>
		</tr>
	</table>
	");

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