<?php require_once('Connections/CCOM.php'); ?>
<?php
session_start();
if(isset($_SESSION['MM_Username'])){
header("Location:index.php?errMsg=2");
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
  $MM_dupKeyRedirect="memberadd.php?errMsg=1";
  
  $loginUsername = $_POST['m_username'];
  $loginEmail= $_POST['m_email'];
  
  $LoginRS__query = sprintf("SELECT m_username FROM memberdata WHERE m_username=%s or m_email=%s", GetSQLValueString($loginUsername, "text"),GetSQLValueString($loginEmail, "text"));
  mysql_select_db($database_CCOM, $CCOM);
  $LoginRS=mysql_query($LoginRS__query, $CCOM) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);

  //if there is a row in the database, the username was found - can not add the requested username
  if($loginFoundUser){
    $MM_qsChar = "?";
    //append the username to the redirect page
    if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
    $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
    header ("Location: $MM_dupKeyRedirect");
    exit;
  }
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO memberdata (m_name, m_nick, m_username, m_passwd, m_sex, m_birthday, m_email, m_url, m_phone, m_cellphone, m_address, m_joinDate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['m_name'], "text"),
                       GetSQLValueString($_POST['m_nick'], "text"),
                       GetSQLValueString($_POST['m_username'], "text"),
                       GetSQLValueString(md5($_POST['m_passwd']), "text"),
                       GetSQLValueString($_POST['m_sex'], "text"),
                       GetSQLValueString($_POST['m_birthday'], "date"),
                       GetSQLValueString($_POST['m_email'], "text"),
                       GetSQLValueString($_POST['m_url'], "text"),
                       GetSQLValueString($_POST['m_phone'], "text"),
                       GetSQLValueString($_POST['m_cellphone'], "text"),
                       GetSQLValueString($_POST['m_address'], "text"),
                       GetSQLValueString($_POST['m_joinDate'], "date"));

  mysql_select_db($database_CCOM, $CCOM);
  $Result1 = mysql_query($insertSQL, $CCOM) or die(mysql_error());

  $insertGoTo = "memberaddfinish.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>網站會員系統</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#99cc00">
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
   <td><table align="left" border="0" cellpadding="0" cellspacing="0" width="720">
	  <tr>
	   <td><img name="memberSystem_r1_c1" src="images/memberSystem_r1_c1.jpg" width="5" height="88" border="0" alt=""></td>
	   <td><img name="memberSystem_r1_c2" src="images/memberSystem_r1_c2.jpg" width="193" height="88" border="0" alt=""></td>
	   <td><img name="memberSystem_r1_c3" src="images/memberSystem_r1_c3.jpg" width="280" height="88" border="0" alt=""></td>
	   <td><table align="left" border="0" cellpadding="0" cellspacing="0" width="222">
	     <tr>
	       <td height="64" align="right" valign="bottom"><table border="0" cellpadding="0" cellspacing="0">
	         <tr>
	           <td><a href="index.php"><img name="memberSystem_r2_c4" src="images/memberSystem_r2_c4.jpg" width="48" height="53" border="0" alt=""></a></td>
	           <td width="11">&nbsp;</td>
	           <td><a href="memberadd.php"><img name="memberSystem_r2_c6" src="images/memberSystem_r2_c6.jpg" width="46" height="53" border="0" alt=""></a></td>
	           </tr>
	         </table></td>
	       </tr>
	     <tr>
	       <td><img name="memberSystem_r3_c4" src="images/memberSystem_r3_c4.jpg" width="222" height="24" border="0" alt=""></td>
	       </tr>
	     </table></td>
	   <td><img name="memberSystem_r1_c11" src="images/memberSystem_r1_c11.jpg" width="20" height="88" border="0" alt=""></td>
	  </tr>
	</table></td>
  </tr>
  <tr>
   <td background="images/memberSystem_r4_c1.jpg"><table width="96%" border="0" align="center" cellpadding="10" cellspacing="0">
     <tr valign="top">
       <td><h1>申請加入會員</h1>
         <?php /*start inputVar script*/ if (isset($_GET['errMsg'])){ ?>
           <?php /*start input_input script*/ if ($_GET['errMsg'] == 1){ ?>
             <table width="100%" border="0" cellspacing="0" cellpadding="4">
               <tr class="head2">
                 <td>錯誤訊息</td>
                 </tr>
               <tr>
                 <td align="center"><strong> </strong>這個帳號或電子郵件已經有人使用！</td>
                 </tr>
             </table>
             <?php } /*end input_input script*/ ?>
           <?php } /*end inputVar script*/ ?>
<form method="POST" action="<?php echo $editFormAction; ?>" name="form1" id="form1">
  <table width="100%" border="0" cellspacing="0" cellpadding="4">
             <tr class="head1">
               <td width="100">帳號資料</td>
               <td align="left">&nbsp;</td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">使用帳號</th>
               <td align="left"> <input name="m_username" type="text" class="normalinput" id="m_username">
                 <font color="#FF0000">*</font> <br>
                 請填入5~16個字元的英文字母、數字及符號。</td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">密碼</th>
               <td align="left"> <input name="m_passwd" type="password" class="normalinput" id="m_passwd">
                 <font color="#FF0000">*</font> <br>
                 請填入5~10個字元的英文字母、數字及符號。</td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">確認密碼</th>
               <td align="left"> <input name="m_passwdrecheck" type="password" class="normalinput" id="m_passwdrecheck">
                 <font color="#FF0000">*</font> <br>
                 再輸入一次密碼</td>
             </tr>
             <tr class="head1">
               <td width="100">個人資料</td>
               <td align="left">&nbsp;</td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">真實姓名</th>
               <td align="left"> <input name="m_name" type="text" class="normalinput" id="m_name">
                 <font color="#FF0000">*</font> </td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">暱稱</th>
               <td align="left"> <input name="m_nick" type="text" class="normalinput" id="m_nick">
                 <font color="#FF0000">*</font> </td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">性別</th>
               <td align="left"> <input name="m_sex" type="radio" value="w" checked>
                 女
                   <input name="m_sex" type="radio" value="m">                                                       
                  男 <font color="#FF0000">*</font></td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">生日</th>
               <td align="left"> <input name="m_birthday" type="text" class="normalinput" id="m_birthday">
                 <font color="#FF0000">*</font> <br>
                 為西元格式                           (YYYY - MM - DD) </td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">電子郵件</th>
               <td align="left"> <input name="m_email" type="text" class="normalinput" id="m_email">
                 <font color="#FF0000">*</font> <br>
                 請確定此電子郵件為可使用狀態，以方便未來系統使用，如補寄會員密碼信。</td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">個人網頁</th>
               <td align="left"><input name="m_url" type="text" class="normalinput" id="m_url">
                 <br>
                 請以                           「http://」 為開頭。</td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">電話</th>
               <td align="left"><input name="m_phone" type="text" class="normalinput" id="m_phone"></td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">行動電話</th>
               <td align="left"><input name="m_cellphone" type="text" class="normalinput" id="m_cellphone"></td>
             </tr>
             <tr valign="baseline">
               <th width="100" align="right">住址</th>
               <td align="left"><input name="m_address" type="text" class="normalinput" id="m_address" size="40">
                 <input name="m_joinDate" type="hidden" id="m_joinDate" value="<?php echo date('Y-m-d H:i:s');?>"></td>
             </tr>
             <tr valign="middle">
               <td align="right">&nbsp;</td>
               <td align="left"><font color="#FF0000">*</font> 表示為必填的欄位</td>
             </tr>
             <tr valign="baseline">
               <td colspan="2" align="center"><hr size="1" noshade>
                 <input type="submit" name="Submit2" value="送出申請">
                 <input type="button" name="Submit" value="回上一頁" onclick="window.history.back();"></td>
               </tr>
             <tr valign="baseline">
               <td colspan="2" align="center">&nbsp;</td>
             </tr>
           </table>
           <input type="hidden" name="MM_insert" value="form1">
         </form>
         <p>&nbsp;</p></td>
       <td width="180"><p class="head1">會員資料</p>
         <table width="100%" border="0" cellspacing="0" cellpadding="4">

           <tr>
             <td valign="baseline"><p><strong>填寫資料注意事項： </strong></p>
               <ol>
                 <li class="smalltext"> 請提供您本人正確、最新及完整的資料。 </li>
                 <li class="smalltext"> 在欄位後方出現「*」符號表示為必填的欄位。</li>
                 <li class="smalltext">填寫時請您遵守各個欄位後方的補助說明。</li>
                 <li class="smalltext">關於您的會員註冊以及其他特定資料，本系統不會向任何人出售或出借你所填寫的個人資料。</li>
                 <li class="smalltext">在註冊成功後，除了「使用帳號」外您可以在會員專區內修改您所填寫的個人資料。</li>
               </ol>
               <p><strong>請遵守以下規則： </strong></p>
               <ol class="smalltext">
                 <li class="smalltext"> 遵守政府的各項有關法律法規。</li>
                 <li class="smalltext"> 不得在發佈任何色情非法， 以及危害國家安全的言論。</li>
                 <li class="smalltext">嚴禁連結有關政治， 色情， 宗教， 迷信等違法訊息。</li>
                 <li class="smalltext"> 承擔一切因您的行為而直接或間接導致的民事或刑事法律責任。</li>
                 <li class="smalltext"> 互相尊重， 遵守互聯網絡道德；嚴禁互相惡意攻擊， 漫罵。</li>
                 <li class="smalltext"> 管理員擁有一切管理權力。</li>
               </ol></td>
           </tr>
         </table>
         <p>&nbsp;</p></td>
     </tr>
   </table></td>
  </tr>
  <tr>
   <td><img name="memberSystem_r6_c1" src="images/memberSystem_r6_c1.jpg" width="720" height="10" border="0" alt=""></td>
  </tr>
  <tr>
   <td><div id="siteinfo"></div></td>
  </tr>
</table>
</body>
</html>
