<?php require('includes/config.php');

//if logged in redirect to registration page
if( $user->is_logged_in() ){ header('Location: memberpage.php'); }

//if form has been submitted process it
if(isset($_POST['submit'])){

	//very basic validation
//	if (empty($_POST["name"])) {
//		$error[] = 'Input your first name';
//	} else {
//		$stmt = $db->prepare('SELECT name FROM registration WHERE name = :name');
//		$stmt->execute(array(':name' => $_POST['name']));
//		$row = $stmt->fetch(PDO::FETCH_ASSOC);
//
//		if(!empty($row['name'])){
//			$error[] = 'Username provided is already in use.';
//		}
//
//	}
//

	if (empty($_POST["surname"])) {
		$error[] = 'Surname can not empty';
		}
	if(strlen($_POST['phone']) < 8){
		$error[] = 'Phone is too short.';
		}
	if (empty($_POST["email"])) {
		$error[] = 'Provide your email to activated the registration';
		}

	if(strlen($_POST['position']) < 1){
		$error[] = 'Please Fill your Position.';
		}

	if (empty($_POST["depfak"])) {
		$error[] = 'Departement or Faculty can not empty';
		}

	if (empty($_POST["organization"])) {
		$error[] = 'Organization can not empty';
		}

	if (empty($_POST["postal"])) {
		$error[] = 'Please input your postal address';
		}

	if (empty($_POST["city"])) {
		$error[] = 'Please input your City';
		}

	if (empty($_POST["stateprov"])) {
		$error[] = 'Please input your State/Province';
		}

	if (empty($_POST["zipcode"])) {
		$error[] = 'Please input your ZIP Code';
		}
	if (empty($_POST["country"])) {
		$error[] = 'Please input your Country';
		}
		

	//email validation
	if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
	    $error[] = 'Please enter a valid email address';
	} else {
		$stmt = $db->prepare('SELECT email FROM registration WHERE email = :email');
		$stmt->execute(array(':email' => $_POST['email']));
		$row = $stmt->fetch(PDO::FETCH_ASSOC);

		if(!empty($row['email'])){
			$error[] = 'Email provided is already in use.';
		}

	}


	//if no errors have been created carry on
	if(!isset($error)){

		//hash the phone

		//create the activasion code
		$activasion = md5(uniqid(rand(),true));

		try {

			//insert into database with a prepared statement
			$stmt = $db->prepare('INSERT INTO registration (salutation, name,surname,feetype, email,phone,position, depfak, organization, postal, city, stateprov, zipcode, country, presentationtype, additional, activation) VALUES 
			(:salutation, :name, :surname, :feetype, :email, :phone, :position, :depfak, :organization, :postal, :city, :stateprov, :zipcode, :country, :presentationtype, :additional, :activation)');
			$stmt->execute(array(
				':salutation' => $_POST['salutation'],
				':name' => $_POST['name'],
				':surname' => $_POST['surname'],
				':feetype' => $_POST['feetype'],
				':email' => $_POST['email'],
				':phone' => $_POST['phone'],
				':position' => $_POST['position'],
				':depfak' => $_POST['depfak'],
				':organization' => $_POST['organization'],
				':postal' => $_POST['postal'],
				':city' => $_POST['city'],
				':stateprov' => $_POST['stateprov'],
				':zipcode' => $_POST['zipcode'],
				':country' => $_POST['country'],
				':presentationtype' => $_POST['presentationtype'],
				':additional' => $_POST['additional'],

				':activation' => $activasion
			));
			$id = $db->lastInsertId('memberID');

			//send email
			$to = $_POST['email'];
			$subject = "Registration Confirmation";
			$body = "<p>Thank you for registering to our International Conference on Integrated Coastal Management and Marine Biotechnology.</p><br>
			Your Registration Code: <h3><b>KODE</b></h3><br>
			<p>To confirm your registration, please click on this link: <a href='".DIR."int/icmmbt/reg/activate.php?x=$id&y=$activasion'>".DIR."int/icmmbt/reg/activate.php?x=$id&y=$activasion</a></p><br><br>
			<p>Regards, Conference Secretariat</p>";

			$mail = new Mail();
			$mail->setFrom(SITEEMAIL);
			$mail->addAddress($to);
			$mail->subject($subject);
			$mail->body($body);
			$mail->send();

			//redirect to index page
			header('Location: index.php?action=joined');
			exit;

		//else catch the exception and show the error.
		} catch(PDOException $e) {
		    $error[] = $e->getMessage();
		}

	}

}

//define page title
$title = 'CCMRS';

//include header template
require('layout/header.php');
?>
<!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=utf-8" />
<title>Registration Page</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {color: #0000FF}
-->
</style>
</head>

<!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=utf-8" />
<title>Registration Page</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {color: #0000FF}
-->
</style>
</head>

<body>
<div class="container">
  <div class="header"></div>
  <div class="content">
			<form role="form" method="post" action="" autocomplete="off">
				<?php
				//check for any errors
				if(isset($error)){
					foreach($error as $error){
						echo '<p class="bg-danger">'.$error.'</p>';
					}
				}

				//if action is joined show sucess
				if(isset($_GET['action']) && $_GET['action'] == 'joined'){
					echo "<h2 class='bg-success'>Registration successful, please check your email to activate your account.</h2>";
				}
				?>

<table align="center">
          <tr valign="baseline">
            <td width="35%" align="right" nowrap="nowrap">* Salutation:</td>
            <td width="65%">
              <input type='radio' value="Mr." name='salutation' checked="checked" />Mr. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type='radio' value="Ms."  name='salutation' />Ms. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type='radio' value="Mrs."  name='salutation' />Mrs. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type='radio' value="Prof."  name='salutation' />Prof. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type='radio' value="Dr."  name='salutation' />Dr.            </td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* First Name:</td>
            <td><input type="text" name="name" id="name"  placeholder="First Name" value="<?php if(isset($error)){ echo $_POST['name']; } ?>" tabindex="1" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* Surname:</td>
            <td><input type="text" name="surname" id="surname"  placeholder="Surname" value="<?php if(isset($error)){ echo $_POST['surname']; } ?>" tabindex="2" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* Fee Type:</td>
            <td>
            	<input  name='feetype' type='radio' value="Full" checked="checked" />
           	  Full (IDR) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           	<input type='radio' value="Student"  name='feetype' />Student (IDR)</td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* Email:</td>
            <td><input type="email" name="email" id="email"  placeholder="Email Address" value="<?php if(isset($error)){ echo $_POST['email']; } ?>" tabindex="3"></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Phone :</td>
            <td><input type="phone" name="phone" id="phone"  placeholder="Phone" value="<?php if(isset($error)){ echo $_POST['phone']; } ?>" tabindex="4" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* Position Held :</td>
            <td><input type="position" name="position" id="position" placeholder="Position Held" value="<?php if(isset($error)){ echo $_POST['position']; } ?>" tabindex="5" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* Departement/Faculty :</td>
            <td><input type="depfak" name="depfak" id="depfak" placeholder="Departement/Faculty" value="<?php if(isset($error)){ echo $_POST['depfak']; } ?>" tabindex="6" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* Organization :</td>
            <td><input type="organization" name="organization" id="organization" placeholder="Organization" value="<?php if(isset($error)){ echo $_POST['organization']; } ?>" tabindex="7" /></td>
          </tr>
          <tr valign="baseline">
            <td align="right" valign="middle" nowrap="nowrap">* Postal Address :</td>
            <td><textarea name="postal" id="postal" placeholder="Postal Address"cols="38" tabindex="8"><?php if(isset($error)){ echo $_POST['postal']; } ?></textarea></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* City :</td>
            <td><input type="city" name="city" id="city" placeholder="City" value="<?php if(isset($error)){ echo $_POST['city']; } ?>" tabindex="9"  /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* State/Province :</td>
            <td><input type="stateprov" name="stateprov" id="stateprov" placeholder="State/Province" value="<?php if(isset($error)){ echo $_POST['stateprov']; } ?>" tabindex="10" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* Zip Code :</td>
            <td><input type="zipcode" name="zipcode" id="zipcode" placeholder="ZIP Code" value="<?php if(isset($error)){ echo $_POST['zipcode']; } ?>" tabindex="11" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">* Country :</td>
            <td><input type="country" name="country" id="country" placeholder="Country" value="<?php if(isset($error)){ echo $_POST['country']; } ?>" tabindex="12" /></td>
          </tr>
          <tr valign="baseline">
            <td align="right">* Presentation Type  :</td>
            <td>
              <select name="presentationtype" tabindex="13">
               <option value='I Will Not Present'>I Will Not Present</option>
               <option value='Oral'>Oral</option>
                <option value='Poster'>Poster</option>
              </select>           </td>
          </tr>
          <tr valign="baseline">
            <td align="right" valign="middle" nowrap="nowrap">Additonal Information / <br>Instructions :</td>
            <td><textarea name="additional" id="additional" placeholder="Additonal Information / Instructions"cols="38" tabindex="14"><?php if(isset($error)){ echo $_POST['additional']; } ?></textarea></td>
          </tr>

          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" name="submit" value="Register"  tabindex="15" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
        </table>
			</form>
</div>
  <div class="nelayan"></div>
  <div class="footer"><a href="http://pksplipb.or.id/int/icmmbt/">| Home</a> | <a href="http://pkspl.ipb.ac.id">About</a> | <a href="mailto:icmbt2016@pksplipb.or.id">Contact Us</a> | <a href="admin/">Admin</a> | </div>
</div>
</body>
</html>

<?php
//include header template
//require('layout/footer.php');
?>
