<?php require_once('Connections/TOM.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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])) {
// Password Checks
$pwdOK = true;
// trim leading and trailing spaces
$_POST['password'] = trim($_POST['password']);
// if less than 6 characters, create an alert and set flag to false
if (strlen($_POST['password']) < 6) {
	$error['pwd_length'] = 'Your password must be at least 6 characters';
	$pwdOK = false;
	}
// if no match, create an alert and set flag to false
	if ($_POST['password'] != trim($_POST['conf_pwd'])){
	$error['pwd'] = 'Your passwords don\'t match';
	$pwdOK = false;
	}
// if password OK, encrypt it
if ($pwdOK) {
	$_POST['password'] = sha1($_POST['password']);
	}
  $MM_dupKeyRedirect="newuser.php";
  $loginUsername = $_POST['username'];
  $LoginRS__query = sprintf("SELECT username FROM users WHERE username=%s", GetSQLValueString($loginUsername, "text"));
  mysql_select_db($database_TOM, $TOM);
  $LoginRS=mysql_query($LoginRS__query, $TOM) 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){
 $error['username']= "$loginUsername is already in use. Please choose a different username.";
  }
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if (!$error) {
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "register")) {
  $insertSQL = sprintf("INSERT INTO users (email, username, password) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['username'], "text"),
                       GetSQLValueString($_POST['password'], "text"));

  mysql_select_db($database_TOM, $TOM);
  $Result1 = mysql_query($insertSQL, $TOM) or die(mysql_error());
}
}
?><!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=iso-8859-1" />
<title>New User Registration</title>
<link href="admin.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div align="center">
  <h1 align="left">GC1BVEA - Register New User </h1>
</div>
<h3>OK, so you have decided to try for this cache. The first step is to get registered into the database so that I can send you your information. <span class="warning"><img src="publickey.jpg" alt="public key" width="178" height="241" class="floatRight" /><br />
</span><em>Please remember your password so that you can get back into the site.</em> </h3>
<?php
if ($error) {
	echo '<ul>';
	foreach ($error as $alert) {
		echo "<li class='warning'>$alert</li>\n";
		}
	echo '</ul>';
	}
?>
<form id="register" name="register" method="POST" action="<?php echo $editFormAction; ?>">
  <p>
    <label for="username">Geocaching <br />
    Username:</label>
    <br />
    <input type="text" name="username" id="username" />
  </p>
  <p>
    <label for="password">Password:<br />
    </label>
    <input type="password" name="password" id="password" />
  </p>
  <p>
    <label for="conf_pwd">Confirm Password:</label>
    <br />
    <input type="password" name="conf_pwd" id="conf_pwd" />
  </p>
  <p>
    <label for="email">Email:<br />
    </label>
    <input value="<?php if (isset($_POST['email'])) {
echo $_POST['email'];} ?>" name="email" type="text" class="mediumbox" id="email" />
  </p>
  <p>
    <input name="register" type="submit" id="register" value="Register new user" />
  </p>
  <p>&nbsp;</p>
  <input type="hidden" name="MM_insert" value="register">
</form>
<p>&nbsp;</p>
</body>
</html>
