<?php
/*
UserCake Version: 2.0.2
http://usercake.com
*/
error_reporting(E_ALL);
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}

//Prevent the user visiting the logged in page if he/she is already logged in
if(isUserLoggedIn()) { header("Location: account.php"); die(); }

//Forms posted
if(!empty($_POST))
{
	$errors = array();
	$email = trim($_POST["email"]);
	$username = trim($_POST["username"]);
	$displayname = trim($_POST["displayname"]);
	$password = trim($_POST["password"]);
	$confirm_pass = trim($_POST["passwordc"]);
	$captcha = md5($_POST["captcha"]);
    $telefon = trim($_POST["telefon"]);
    $grad = trim($_POST["grad"]);
	
	
	if ($captcha != $_SESSION['captcha'])
	{
		$errors[] = lang("CAPTCHA_FAIL");
	}
	if(minMaxRange(5,25,$username))
	{
		$errors[] = lang("ACCOUNT_USER_CHAR_LIMIT",array(5,25));
	}
	if(!ctype_alnum($username)){
		$errors[] = lang("ACCOUNT_USER_INVALID_CHARACTERS");
	}
	if(minMaxRange(5,25,$displayname))
	{
		$errors[] = lang("ACCOUNT_DISPLAY_CHAR_LIMIT",array(5,25));
	}

	if(minMaxRange(8,50,$password) && minMaxRange(8,50,$confirm_pass))
	{
		$errors[] = lang("ACCOUNT_PASS_CHAR_LIMIT",array(8,50));
	}
	else if($password != $confirm_pass)
	{
		$errors[] = lang("ACCOUNT_PASS_MISMATCH");
	}
	if(!isValidEmail($email))
	{
		$errors[] = lang("ACCOUNT_INVALID_EMAIL");
	}
    if(!$grad)
    {
        $errors[] = lang("GRAD_OBAVEZAN");
    }
    if(!$email)
    {
        $errors[] = lang("TELEFON_OBAVEZAN");
    }
	//End data validation
	if(count($errors) == 0)
	{	
		//Construct a user object
		$user = new User($username,$displayname,$password,$email,$telefon,$grad);
		
		//Checking this flag tells us whether there were any errors such as possible data duplication occured
		if(!$user->status)
		{
			if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
			if($user->displayname_taken) $errors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
			if($user->email_taken) 	  $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));		
		}
		else
		{
			//Attempt to add the user to the database, carry out finishing  tasks like emailing the user (if required)
			if(!$user->userCakeAddUser())
			{
				if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
				if($user->sql_failure)  $errors[] = lang("SQL_ERROR");
			}
		}
	}
	if(count($errors) == 0) {
		$successes[] = $user->success;
	}
}

require_once("models/header.php");
include("main-nav.php"); ?>

<section>

  <h2>Registracija korisnika</h2>
  <?php echo resultBlock($errors,$successes); ?>
  
  <div id='regbox'>
  <form name='newUser' action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
  
  <p>
  <label>Korisničko ime:</label>
  <input type='text' name='username' value="<?php if(isset($_POST["username"])){echo $_POST["username"];} ?>" autocomplete="off"/>
  </p>
  <p>
  <label>Lozinka:</label>
  <input type='password' name='password' autocomplete="off" />
  </p>
  <p>
  <label>Potvrdi lozinku:</label>
  <input type='password' name='passwordc' autocomplete="off" />
  </p>
  <p>
  <label>Ime i prezime:</label>
  <input type='text' name='displayname' value="<?php if(isset($_POST["displayname"])){echo $_POST["displayname"];} ?>" autocomplete="off"/>
  </p>
  <p>
  <label>Grad:</label>
  <input type='text' name='grad' value="<?php if(isset($_POST["grad"])){echo $_POST["grad"];} ?>" autocomplete="off"/>
  </p>
  <p>
  <label>Email:</label>
  <input type='text' name='email'  value="<?php if(isset($_POST["email"])){echo $_POST["email"];} ?>" autocomplete="off"/>
  </p>
  <p style="margin: 0 0 25px 0;">
  <label>Telefon:</label>
  <input type='text' name='telefon' value="<?php if(isset($_POST["telefon"])){echo $_POST["telefon"];} ?>" autocomplete="off"/>
  </p>
  <p style="margin: 0;">
  <label>Sigurnosni kod:</label>
  <?php echo "<img src='models/captcha.php'>"; ?>
  </p>
  <p>
    <label>Unesite sigurnosni kod:</label>
    <input name='captcha' type='text'>
  </p>
  <p>
    <label>&nbsp;</label>
    <input type='submit' value='Pošalji'/>
  </p>
  
  </form>
  </div>
  </section>
<?php require_once("models/footer.php"); ?>