<?php
$msg="";
$newby=1;
require("includes/.htconfig.php");
require("includes/dbcon.php");
if (!$link) reporterror("connecting to the database");

if(array_key_exists("Submit",$_REQUEST) && $_REQUEST['Submit']=="Please Register Me")
{
	$fisrt_name=trim($_REQUEST['fisrt_name']);
	$middle_name=trim($_REQUEST['middle_name']);
	$last_name=trim($_REQUEST['last_name']);
	$email=trim($_REQUEST['email']);    #Check for validity?
	
	$userSql="select * from enrollee where email = '".$email."'";
	$userRs=mysql_query($userSql);
	$totRows=mysql_num_rows($userRs);	
    if($totRows>0)
	{
		reporterror("enrolling with email $email",$userSql);
		$throw=mysql_fetch_array($userRs);
		reporterror("$email previously enrolled as enrollee ".$throw["enrollee_id"]);
		$err_msg1="SORRY! Your email address, $email, has already been submitted to us for registration.
        If you have the email we sent you at that time, please click on the link provided to continue.
        If you have never registered with us, please contact us at <A href=mailto:contact@reliableid.com>contact@reliableid.com</A> to help us straighten this out.";
	}
	else    #if we don't have a Repeated email address ($totRows>0)
	{		
		$insert_enrollee_sql="insert into enrollee SET curDate=now(),
					fisrt_name='".mysql_escape_string($fisrt_name)."',
					middle_name='".mysql_escape_string($middle_name)."',
					last_name='".mysql_escape_string($last_name)."',
					email='".mysql_escape_string($email)."',username='".mysql_escape_string($email)."',password='fffffffff'";				
					##email='".mysql_escape_string($email)."',				
					##username='".mysql_escape_string($username)."',
					##password='".mysql_escape_string($password)."'";
          
		mysql_query($insert_enrollee_sql);
		$enrollee_id= mysql_insert_id();
		if($enrollee_id)
		{
			loggywoggy("Enrollee $enrollee_id established.");
			$enrollee=mysql_getRow("select * from enrollee where enrollee_id=$enrollee_id");
			$msg="Thank You! <br> Your activation link is being sent to you.<br>Please check your email. ";
	
			$path = UPLOADDIRECTORY."/$enrollee_id";
			if (mkdir($path))
			{
				chmod($path,0767);
	
				$insert_sessions_sql="insert into sessions SET 
						enrollee_id=".$enrollee_id.",
						session_key='".$session_id."',
						session_date=now(),
						ip_address ='".mysql_escape_string($_SERVER['REMOTE_ADDR'])."',				
						browser_name='".mysql_escape_string($_SERVER['HTTP_USER_AGENT'])."',
						operating_system='".mysql_escape_string($_SERVER['HTTP_USER_AGENT'])."'";		
	
				mysql_query($insert_sessions_sql);
				$part="makecert";
				include ("osbr/$osbr.inc.php");
				if ($cert)
				{
					file_put_contents(UPLOADDIRECTORY."/$enrollee_id/cert.pem",strstr($cert,"-----BEGIN CERTIFICATE-----"));
					$txt="Thank-you for registering with Reliable ID!<br>To activate your registration please click <a href='".URL."activation.php'>HERE</a>";
			
					$headers   = "From: ".EMAILFROM."\r\n";
					$headers  .= "X-ReliableID-IP-orig: ".$_SERVER["REMOTE_ADDR"]."\r\n";
					$headers  .= "X-serial: $enrollee_id\r\n";
					$headers  .= "MIME-Version: 1.0\r\n";
					$headers  .= "Content-Type: text/html";
					
					$body = "
					<HTML>
						<BODY>
						<CENTER>
							<TABLE BORDER='1'>
								<TR>
									<TD align='center'>
										<FONT COLOR='MAROON'><B><BIG>$txt</BIG></B></FONT>
						<BR>This email was sent here because someone entered it as part of the process for obtaining a personal digital birth certificate from www.reliableid.com.
						If you did not enter this address with us, please ignore this email.  If you feel that you have been invaded, please let us know.  All we truly know at this point is the address of the computer which entered the registration information.
						Unless someone is trying to annoy you or does not know how to enter his email address, you should not get any more email from us.
									</TD>
								</TR>
							</TABLE>
						</CENTER>
						</BODY>
					</HTML>";
					
					mail($email, "Registration Mail", $body, $headers);
				}
				else
				{
					reporterror("creating the certificate for enrollee $enrollee_id");
					$msg.="<BR>We were unable to generate a provisional certificate for you.  Please try again.";
				}
			}
			else
			{
				reporterror("creating an upload directory at $path");
			}
		}
		else     #enrollee_id
		{
			reporterror("adding a new enrollee",$insert_enrollee_sql);
			$err_msg1="SORRY! We had a problem placing your information into our database.  We should be looking at this soon.  You are welcome to try again on your own.";
		}       #enrollee_id
	}
}   ##REQUEST[submit] == Done

?>
<!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>
<link href="./style/design.css" rel="stylesheet" type="text/css" />

<style type="text/css">
<!--
.style1 {font-size: 0.8em}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Register Page</title>
<script language="JavaScript" src="includes/js/common.js"></script>
<script language="JavaScript">
function validate()
{
	var is_valid=true
	is_valid = blank_check( document.frm.fisrt_name.value, "PLEASE INPUT YOUR GIVEN NAME" )
		if( is_valid == false ) { document.frm.fisrt_name.focus(); return is_valid }

	is_valid = blank_check( document.frm.last_name.value, "PLEASE INPUT YOUR SURNAME" )
		if( is_valid == false ) { document.frm.last_name.focus(); return is_valid }

	is_valid = blank_check( document.frm.email.value, "PLEASE INPUT AN EMAIL ADDRESS" )	
    if( is_valid == false ) { document.frm.email.focus(); return is_valid }
	
	is_valid = email_check( document.frm.email.value, "PLEASE INPUT A VALID EMAIL ADDRESS" )
    if( is_valid == false ) { document.frm.email.focus(); return is_valid }	

	return is_valid
}
</script>
<?
$part="header";
include ("osbr/$osbr.inc.php");
?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td align="center" valign="top">		
<?php
include_once("includes/header.php");
?>
<?php
if( !$msg )
{
$part="form";
$other="name=\"frm\" onSubmit=\"return validate()\"";
include ("osbr/$osbr.inc.php");
?>
	<table width="40%" border="0" align="center" cellpadding="0" cellspacing="0">	 
	 <tr>
	   <td width="30%" valign="top">
		<div class="bodySubHeader">
		<span class="bsht03">User Registration</span>		
		</div>
		</td>
	 </tr> 
	</table>	

	<table width="40%" border="0" align="center" cellpadding="0" cellspacing="0" class="DarkLightBox">
	 <tr>
	   <td height="30" align="center" valign="top" colspan="3" class="ErrorMessage"><strong><?=$err_msg1?></strong></td>	   
	 </tr>
	 <tr>
	   <td height="30" align="left" valign="top" class="browseEventContents"><strong>First Name:</strong><?=$redstar?></td>
	   <td height="30" align="left" valign="top">&nbsp;</td>
	   <td height="30" align="left" valign="top">
	   <?
	   echo inputBox("fisrt_name")."</td>";
	   ?>
	 </tr>
	 <tr>
	   <td height="30" align="left" valign="top" class="browseEventContents"><strong>Middle Name:</strong></td>
	   <td height="30" align="left" valign="top">&nbsp;</td>
	   <td height="30" align="left" valign="top">
	   <?
	   echo inputBox("middle_name")."</td>";
	   ?>
	 </tr>
	 <tr>
	   <td height="30" align="left" valign="top" class="browseEventContents"><strong>Surname:</strong><font color="red"><strong>*</strong></font></td>
	   <td height="30" align="left" valign="top">&nbsp;</td>
	   <td height="30" align="left" valign="top">
	   <?
	   echo inputBox("last_name")."</td>";
	   ?>
	 </tr>
	   <td height="30" align="left" valign="top" class="browseEventContents"><strong>E-mail:</strong><font color="red"><strong>*</strong></font></td>
	   <td height="30" align="left" valign="top">&nbsp;</td>
	   <td height="30" align="left" valign="top">
	   <?
	   echo inputBox("email")."</td>";
	   ?>
	 </tr> 
	 <tr>
	   <td height="35" align="left" valign="bottom">&nbsp;</td>
	   <td width="30" height="35" align="left" valign="bottom">&nbsp;</td>
	   <td height="35" align="left" valign="bottom"><input name="Submit" type="submit" value="Please Register Me" /></td>
	 </tr>
	</table>
</form>
<?php
} ##!$msgs
if( !empty($_POST) )
{
?>
	<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">		 
	 <tr>
	   <td height="30" align="center" valign="top"><strong><br /><br /><?=$msg?></strong></td>	   
	 </tr>
	</table>
<?php
}
?>
<?php
if (strlen($bugsy) > 0) echo $bugsy;
include_once("includes/footer.php");
?>
		</td>
	</tr>
</table>
</body>
</html>