<?php


/* Start session */
if($startSession == TRUE){ session_start();}

/* Config file */
include('config.php');

/* Check for submition */
if($_POST['submitID'] == 2){
		
	/* Connect to database & query */
	if($connectDatabase == TRUE){$action=TRUE;include('connect.php');}

	/* sanitize and check info */
	$userName = mysql_real_escape_string($_POST['userName'],$dbc);
	$password = mysql_real_escape_string($_POST['password'],$dbc);

	
	if($message == NULL && $password == NULL){ $message = '<h2>Please enter password.</h2>';}
	if($message == NULL && $password != $_POST['password2']){ $message = '<h2>Passwords do not match.</h2>';}
	if($message == NULL && $_POST['agreement']!= "yes"){ $message = '<h2>You need to agree to our terms of service.</h2>';}

	if($message == NULL  )
	{		
		$userQuery = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `$tableName`
		 WHERE `$userNameField`='$userName'"));
		if($userQuery[0] > 0){
			$message = 'This username already exists. Please select another.';			
		} else {
			/* Add user */
			$addUser = mysql_query("INSERT INTO `$tableName` (`$userNameField`,`$userPasswordField`) 
				VALUES ('$userName','$password')");
			if($addUser)
			{
				/* Disconnect from database */
				if($connectDatabase == TRUE){$action=FALSE;include('connect.php');}
				
				/* Log use in */
				$_SESSION['isLoged'] = 'yes';
				$_SESSION['userName'] = $userName;
				/* add cookies ?*/
				/* expire in 1 hour */
				if($useCookies == TRUE)
				{
					setcookie("isLoged", 'yes', time()+logedInFor, "/", ".$domainName", 1);
					setcookie("userName", $userName, time()+logedInFor, "/", ".$domainName", 1);
				}
	
				/* Redirect to login page */
				header("Location: $loginPage");
				exit();
			} else {
				$message = 'Internal error. Please contact administrator.';
			}
		} 		
	}
	/* Disconnect from database */
	if($connectDatabase == TRUE){$action=FALSE;include('connect.php');}
}
?>
<?php
include('header.php');
?>
<!--
/*
This script was downloaded at:
LightPHPScripts.com
Please support us by visiting
out website and letting people
know of it.
Produced under: LGPL
*/
-->
<?php

/* Display error messages */
if($message != NULL){?>

<table >
  <tr>
    <td><?=$message;?></td>
  </tr>
</table>
<?php } ?>
<form action="<? echo $_SERVER['PHP_SELF'];?>" method="post" name="register" id="register" style="display:inline;">
  <table>
    <tr> 
      <td colspan="2">Please enter registration information: </td>
    </tr>
    <tr> 
      <td width="47%">Username</td>
      <td width="53%"><input name="userName" type="text" id="userName"></td>
    </tr>
    <tr> 
      <td>Password</td>
      <td><input name="password" type="password" id="password"></td>
    </tr>
    <tr>
      <td>Re-enter password</td>
      <td><input name="password2" type="password" id="password2" /></td>
    </tr>
    <tr> 
      <td colspan="2">
          <input type="checkbox" name="agreement" value="yes" checked> I agree to the <a target="_blank" href="/agreement.php">terms of service</a>
          <input name="Submit" type="submit" id="Submit" value="Register">
          <input name="submitID" type="hidden" id="submitID" value="2">
</td>
    </tr>
    <tr>
      <td colspan="2"></td>
    </tr>
  </table>
</form>
<?php
include('footer.php');
?>
