<? 
/*
Author		: Sue
Version		: 1.01
Date			: 25th March 2002
Function		: Registration 
*/ 

session_start();
require("auth.php");

unset($error);
unset ($errmsg);
if ($Transmarco["accesslevel"] < $adminlevel) {
	authfail();
} else 	{ 


if ($submit == "Cancel"){
	header("Location: " . $docroot . "/" . $welcome);
}

if ($submit == "Submit"){
	//do form check for unfilled required fields
	if($firstname=="" || $surname=="" || $email=="" || $login=="" || $password=="" || $passwordconf ==""){
		$error=1;
		$errmsg.="<li>Blanks detected in mandatory fields</li>";
	}

	if(HasIllegalChars($login)){
		$error=1;
		$errmsg.="<li>Your Login can only contain alphabets, numbers, underscore '_', fullstop '.' or dash '-'</li>";
	}

	if(HasIllegalChars2($firstname)){
		$error=1;
		$errmsg.="<li>Your Firstname can only contain alphabets</li>";
	}

	if(HasIllegalChars2($surname)){
		$error=1;
		$errmsg.="<li>Your Surname can only contain alphabets</li>";
	}

	if ($email != "") {
		if(!isValidEmail($email)){
			$error=1;
			$errmsg.="<li>Your Email Address is not valid</li>";
		}
	}

	// Check if user exist
	$login=magiccode($login);

	$sql = "select * from usersession where login='$login'";
	$result = db_query($sql);
	if (mysql_num_rows($result) >= 1) {
		$error=1;
		$errmsg.= "<li>This login is already in the database</li>";
	}

	if ($password!=$passwordconf) {
		$error=1;
		$errmsg.= "<li>The passwords does not match</li>";
	}

	//NO errors write the data to database
	if ($error!=1){
		$hashpassword = md5($password);
		$SID = session_id();
		$todaydate = date("Y-m-d H:i:s");

		$newuserid=find_new_id("userid","usersession");
		$newudid = find_new_id("userdetailid","userdetail");
		
		$newuserid=magiccode($newuserid);
		$newudid=magiccode($newudid);
		$login=magiccode($login);
		$hashpassword=magiccode($hashpassword);
		$SID=magiccode($SID);
		$todaydate=magiccode($todaydate);		
		$email=magiccode($email);
		$firstname=magiccode($firstname);
		$surname=magiccode($surname);

		$sql = "insert into usersession (userid,login,password,SID,acctcreated,accesslevel) values('$newuserid','$login','$hashpassword','$SID','$todaydate','500')";
		db_query($sql); //write into usersession table
		
		// For specific fields only, user $sql="insert into userdetail (colname1, colname2, colname3 ... colnameN) values ($value1, $value2, $value3 ... $valueN)";
		$sql="insert into userdetail (userdetailid,userid,email,firstname,surname,active) values('$newudid','$newuserid','$email','$firstname','$surname','yes')";
		db_query($sql);
        
		$errmsg = "<b>User successfully created.<br>Create more user or click 'Cancel' to exit.</b>";
		//header("Location: " . $docroot . "/" . $index);
		unset($firstname);
		unset($surname);
		unset($email);
		unset($login);
	}
}
?>

<HTML><HEAD><TITLE>Registration Form</TITLE>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type>
<META content=en-us http-equiv=Content-Language>
<? include($style); ?>
</HEAD>

<body>
<form method="post" action="<?php echo $PHP_SELF?>" name="form">
  <p>&nbsp;</p>
  <table width="400" border="0" cellspacing="0" cellpadding="8" align="center">
    <tr valign="top"> 
      <td><font class="sub"><b>User Registration Form</b></font></td>
    </tr>
  </table>
  <table class="bigbox" width="400" border="0" cellspacing="0" cellpadding="0" align="center">
    <tr> 
      <td width="16" height="16" valign="top" align="left"><img src="<? echo $images ?>a.gif" width="16" height="18"></td>
      <td width="363"><img src="<? echo $images ?>shim.gif" width="16" height="16"></td>
      <td width="17" height="16" align="right" valign="top"><img src="<? echo $images ?>b.gif" width="16" height="16"></td>
    </tr>
    <tr> 
      <td width="16">&nbsp;</td>
      <td width="363" valign="top" >
        <p>Create your user profile below. All fields marked <b>*</b> are required. 
        </p>
        <p>
          <?if(isset($error)){?>
          <b>ERROR:</b> Please complete the required fields with errors listed below.<br>
          <?}?>
          <? if(isset($errmsg)) { ?>
        </p>
        <ul>
          <? echo $errmsg; ?>
        </ul>
        <? } ?>
        <table width="100%" border="0" cellspacing="0" cellpadding="2">
          <tr valign="baseline"> 
            <td width="23">&nbsp;</td>
            <td width="132"> 
              <p><b>first name:</b></p>
            </td>
            <td width="199"> 
              <input type="text" name="firstname" size="20" maxlength="64" value="<? echo $firstname ?>">
              <b>*</b></td>
          </tr>
          <tr valign="baseline"> 
            <td width="23">&nbsp;</td>
            <td width="132"><b>surname:</b><br>
            </td>
            <td width="199"> 
              <input type="text" name="surname" size="20" maxlength="64" value="<? echo $surname ?>">
              <b>*</b></td>
          </tr>
          <tr valign="baseline"> 
            <td width="23">&nbsp;</td>
            <td width="132"><b>e-mail address:</b></td>
            <td width="199">
              <input type="text" name="email" size="20" maxlength="64" value="<? echo $email ?>">
              <b>*</b></td>
          </tr>
          <tr valign="baseline"> 
            <td width="23">&nbsp;</td>
            <td width="132">&nbsp;</td>
            <td width="199">&nbsp;</td>
          </tr>
          <tr valign="baseline"> 
            <td width="23">&nbsp;</td>
            <td width="132"><b>login name:</b></td>
            <td width="199"><b> 
              <input type="text" name="login" size="15" maxlength="12" value="<? echo $login ?>">
              </b><b>*</b></td>
          </tr>
          <tr valign="baseline"> 
            <td width="23">&nbsp;</td>
            <td width="132"><b>password:</b></td>
            <td width="199"><b> 
              <input type="password" name="password" size="15" maxlength="12">
              </b><b>*</b></td>
          </tr>
          <tr valign="baseline"> 
            <td width="23">&nbsp;</td>
            <td width="132"><b>confirm password:</b></td>
            <td width="199"><b> 
              <input type="password" name="passwordconf" size="15" maxlength="12">
              </b><b>*</b></td>
          </tr>
          <tr valign="baseline"> 
            <td width="23">&nbsp;</td>
            <td width="132">&nbsp;</td>
            <td width="199"><b> 
              <input type="submit" name="submit" value="Submit">
              <input type="reset" name="Reset" value="Clear">
              <input type="submit" name="submit" value="Cancel">
              </b></td>
          </tr>
        </table>
        
      </td>
      <td width="17" align="right">&nbsp;</td>
    </tr>
    <tr> 
      <td width="16" valign="bottom" align="left"><img src="<? echo $images ?>c.gif" width="16" height="18"></td>
      <td width="363">&nbsp;</td>
      <td width="17" align="right" valign="bottom"><img src="<? echo $images ?>d.gif" width="16" height="16"></td>
    </tr>
  </table>
  <input type="hidden" name="error" value="<?echo $error?>">
</form>
<?
	} 
	?>
</BODY>
</HTML>

