<?
	include("connect.php");
	include("security.php");
	$type1 = 1;
	include("pagepermission.php");

	$firstname = $_POST["firstname"];
	$lastname = $_POST["lastname"];
	$gender = "Male";
	$birthdate = rand(1,29)."-".rand(1,12)."-".rand(1980,2000);
	$city = $_POST["city"];
	$country = $_POST["country"];
	$username = $_POST["username"];
	$password = $_POST["password"];
	if($password=="")
	{
		$password = "password";
	}
	$email = $_POST["email"];
	$phone = $_POST["phoneno"];


	if($_POST["add"]!="")
	{
		$qrysel = "select * from registration where username='".$_REQUEST['username']."'";
		$rsqrysel = mysql_query($qrysel);
		$totalrows = mysql_affected_rows();
		if($totalrows>0)
		{
			header("location: message.php?msg=76");
			exit;
		}
		else
		{
			$qryins = "Insert into registration (username,firstname,lastname,sex,birth_date,city,country,phone,password,terms_condition,privacy,account_status,register_date,admin_user_flag,email) values('".$username."','".$firstname."','".$lastname."','".$gender."','".$birthdate."','".$city."','".$country."','".$phone."','".$password."','1','1','1',NOW(),'1','".$email."')";
			mysql_query($qryins) or die(mysql_error());
			header("location: message.php?msg=75");
		}
	}
	
	if($_POST["edit"]!="")
	{
		$id = $_POST["editrecord"];
		$qrysel1 = "select * from registration where (username='".$_REQUEST['username']."') and id!='".$id."'";
		$rsqrysel1 = mysql_query($qrysel1);
		$totalavailable = mysql_num_rows($rsqrysel1);
		if($totalavailable>0)
		{
			header("location: message.php?msg=76");
			exit;
		}
		else
		{
			$qryupd = "update registration set username='".$username."',firstname='".$firstname."',lastname='".$lastname."',sex='".$gender."',birth_date='".$birthdate."',city='".$city."',country='".$country."',phone='".$phone."',password='".$password."',email='".$email."' where id='".$id."'";
			mysql_query($qryupd) or die(mysql_error());
			header("location: message.php?msg=77");
		}
	}
	
	if($_GET["deleterecord"]!="")
	{
		$delid = $_GET["deleterecord"];
		$qrydel = "update registration set user_delete_flag='d' where id='".$delid."'";
		mysql_query($qrydel) or die(mysql_error());
		header("location: message.php?msg=78");
	}

	if($_GET["editid"]!="" || $_GET["delid"]!="")
	{
		if($_GET["editid"]!="") { $id=$_GET["editid"]; }
		else{ $id = $_GET["delid"]; }

		$qryreg = "select * from registration where id='".$id."'";
		$resreg = mysql_query($qryreg);
		$obj = mysql_fetch_object($resreg);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?=$lng_characset;?>" />
<title></title>
<link rel="stylesheet" href="main.css" type="text/css">
</head>
<script language="javascript">
	function Check(f1)
	{
		if(document.f1.firstname.value=="")
		{
			alert("请输入姓!!!");
			document.f1.firstname.focus();
			return false;
		}

		if(document.f1.lastname.value=="")
		{
			alert("请输入名!!!");
			document.f1.lastname.focus();
			return false;
		}

/*		if(document.f1.country.value=="none")
		{
			alert("Please select coutnry!!!");
			document.f1.country.focus();
			return false;
		}
*/
		if(document.f1.username.value=="")
		{
			alert("请输入会员名!!!");
			document.f1.username.focus();
			return false;
		}
		if(f1.username.value.length<3)
		{
			alert("会员名长度不足!");
			f1.username.focus();
			f1.username.select();
			return false;
		}
/*		if(document.f1.password.value=="")
		{
			alert("Please enter password!!!");
			document.f1.password.focus();
			return false;
		}
		if(f1.password.value.length<6)
		{
			alert("Password is too short!");
			f1.password.focus();
			f1.password.select();
			return false;
		}
		if(document.f1.cnfpassword.value=="")
		{
			alert("Please enter confirm password!!!");
			document.f1.cnfpassword.focus();
			return false;
		}
		if(document.f1.password.value!=document.f1.cnfpassword.value)
		{
			alert("Password Mismatch!!!");
			document.f1.password.focus();
			return false;
		}*/
		if(document.f1.email.value=="")
		{
			alert("请输入电子邮件!!!");
			document.f1.email.focus();
			return false;
		}
		if(document.f1.cnfemail.value=="")
		{
			alert("请再次输入电子邮件!!!");
			document.f1.cnfemail.focus();
			return false;
		}
		if(document.f1.email.value!=document.f1.cnfemail.value)
		{
			alert("电子邮件不一致!");
			f1.cnfemail.focus();
			f1.cnfemail.select();
			return false;		
		}
		else
		{
			if(!validate_email(document.f1.email.value,"请输入合法的电子邮件地址"))
				{
					document.f1.email.select();
					return false;
				}
		}
/*		if(document.f1.phoneno.value=="")
		{
			alert("Please enter phone number!!!");
			document.f1.phoneno.focus();
			return false;
		}
*/	}
	function validate_email(field,alerttxt){
		with (field){
			var value;
			value = document.f1.email.value;
			apos=value.indexOf("@");
			dotpos=value.lastIndexOf(".");
			if (apos<1||dotpos-apos<2){
				alert(alerttxt);return false;
			}else{
				return true;
			}
		}
	}
	
	function ConfirmDelete(id)
	{
		if(confirm("Are you sure to delete this member!!!"))
		{
			window.location.href='addbiddinguser.php?deleterecord=' + id;
		}
	}
</script>
<body>
<TABLE cellSpacing=10 cellPadding=0  border=0 width="650">
		<TR>
			<TD class="H1"><? if($_GET["editid"]!=""){ ?>编辑机器人 <? }elseif($_GET["delid"]!=""){ ?>删除机器人 <? }else{ ?>添加机器人<? } ?></TD>
		</TR>
		<TR>
			<TD background="images/vdots.gif"><IMG height=1 
			  src="images/spacer.gif" width=1 border=0></TD>
		</TR>
	<? if($msg){ ?>
		<tr>
			<td bgcolor="#F1F0EF" style='color:#FF0000;' align="center"><?=$msg?></td>
		</tr>
	<? } ?>
<tr><td>
<!-- changeble part starts -->
<form action="" method="post" name="f1" onsubmit="return Check(this);">
<TABLE cellSpacing=2 cellPadding=1 width="650" border=0>
   <TBODY>
     <TR align="right"> 
       <TD colSpan=3 class="a">* 必填项</TD>
     </TR>
     <TR> 
        <TD class=th-d colSpan=3>基本信息</TD>
     </TR>
     <TR> 
       <TD width="182" align=right><IMG height=5 src="images/spacer.gif" width=1 border=0></TD>
       <TD width="458"> </TD>
     </TR>
   	 <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;姓 :&nbsp;</td>									
 	  <td><input type="text" name="firstname" size="20" value="<?=$obj->firstname!=""?$obj->firstname:"";?>"></td>
	 </tr>
   	 <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;名 :&nbsp;</td>									
 	  <td><input type="text" name="lastname" size="20" value="<?=$obj->lastname!=""?$obj->lastname:"";?>"></td>
	 </tr>
<?php /*?>   	 <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;Gender :&nbsp;</td>									
 	  <td>
	  	<select name="gender">
			<option value="Male">Male</option>
			<option <?=$obj->sex=="Female"?"selected":"";?> value="Female">Female</option>
		</select>
	  </td>
	 </tr>
   	 <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;Birth Date :&nbsp;</td>									
 	  <td>
	  		<select name="bdate">
				<option value="none">DD</option>
				<?
					for($i=1;$i<=31;$i++)
					{
				?>
				  <option <?=substr($obj->birth_date,0,2)==$i?"selected":"";?> value="<?=$i<=9?"0".$i:$i;?>"><?=$i<=9?"0".$i:$i;?></option>
				<?
					}
				?>
			</select>
			<select name="bmonth">
				<option value="none">MM</option>
				<?
					for($i=1;$i<=12;$i++)
					{
				?>
				  <option <?=substr($obj->birth_date,3,5)==$i?"selected":"";?> value="<?=$i<=9?"0".$i:$i;?>"><?=$i<=9?"0".$i:$i;?></option>
				<?
					}
				?>
			</select>
			<select name="byear">
				<option value="none">YYYY</option>
				<?
					for($i=1960;$i<=2020;$i++)
					{
				?>
				  <option <?=substr($obj->birth_date,6)==$i?"selected":"";?>  value="<?=$i<=9?"0".$i:$i;?>"><?=$i<=9?"0".$i:$i;?></option>
				<?
					}
				?>
			</select>
	  </td>
	 </tr>
   	 <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;City :&nbsp;</td>									
 	  <td><input type="text" name="city" size="20" value="<?=$obj->city!=""?$obj->city:"";?>"></td>
	 </tr>
<?php */?>   	 <tr>
	  <td class="normal" align="right">&nbsp;国家 :&nbsp;</td>									
 	  <td>
	  	<select name="country" style="width: 200px;">
			<option value="none">--</option>
	  		<?
				$qrycou = "select * from countries order by printable_name";
				$rescou = mysql_query($qrycou);
				while($objcou = mysql_fetch_object($rescou))
				{
			?>	
				<option <?=$objcou->countryId==$obj->country?"selected":"";?> value="<?=$objcou->countryId;?>"><?=$objcou->printable_name;?></option>
			<?
				}
			?>
		</select>
		</td>
	 </tr>
   	 <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;会员名 :&nbsp;</td>									
 	  <td><input type="text" name="username" size="20" maxlength="16" value="<?=$obj->username!=""?$obj->username:"";?>"></td>
	 </tr>
     <tr>
	  <td class="normal" align="right">&nbsp;密码 :&nbsp;</td>	 
	  <td><input type="password" name="password" size="20" value="<?=$obj->password!=""?$obj->password:"";?>"></td>
	</tr>
<?php /*?>     <tr>
	  <td class="normal" align="right">&nbsp;Confirm Password :&nbsp;</td>	 
	  <td><input type="password" name="cnfpassword" size="20" value="<?=$obj->password!=""?$obj->password:"";?>"></td>
	</tr>
<?php */?>     <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;电子邮件 :&nbsp;</td>	  
 	  <td><input type="text" name="email" size="20" value="<?=$obj->email!=""?$obj->email:"";?>"></td>
	</tr>
     <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;确认电子邮件 :&nbsp;</td>	  
 	  <td><input type="text" name="cnfemail" size="20" value="<?=$obj->email!=""?$obj->email:"";?>"></td>
	</tr>
<?php /*?>     <tr>
	  <td class="normal" align="right"><font class="a">*</font>&nbsp;Phone No :&nbsp;</td>	  
 	  <td><input type="text" name="phoneno" size="20" value="<?=$obj->phone!=""?$obj->phone:"";?>"></td>
	</tr>
<?php */?>	<tr>
	 <td>&nbsp;</td><td>&nbsp;</td>
	</tr>
	<TR> 
     <TD align="right">&nbsp;</TD>
     <TD>
	 	<? if($_GET["editid"]!="") { ?>
	 	<INPUT type="submit" value="编辑" name="edit" class="bttn">
		<input type="hidden" value="<?=$_GET["editid"];?>" name="editrecord" />
		<? } elseif($_GET["delid"]!="") { ?>
	 	<INPUT type="button" value="删除" name="delete" class="bttn" onclick="ConfirmDelete('<?=$_GET["delid"];?>')">
		<? }else { ?>
	 	<INPUT type="submit" value="添加" name="add" class="bttn">
		<? } ?>
	 </TD>
    </TR>
</TBODY>
</TABLE>
</form>
</td>
</tr>
</TABLE>
</body>
</html>
