<?php 
	require_once('inc/constants.inc.php');
	
	
	if(!isset($_SESSION)){
		session_start();
	}
	
	
	
	$theError = false;
	if(isset($_SESSION['LoggedIn']) && isset($_SESSION['Username']) && $_SESSION['LoggedIn'] == 1){
		//echo "<h1>YES, YOU ARE LOGGED IN</h1>";
		header("Location: index.php");
		
	}
	
	
	if(!empty($_POST['useremail']) && !empty($_POST['password'])){
		
		require_once("inc/class.users.inc.php");
		$users = new users($db);
		if($_POST['whichForm'] == "login"){
			$theResult = $users->createAccount();
		}else if($_POST['whichForm'] == "facebook"){
			$theResult = $users->accountLoginFB();
		}
		
		if($theResult == true){
			header("Location: index.php");
		}else{ //If it couldn't register
			$theError = true;
			$errorMessage = "That email address has already signed up for Share Portal. <a href='login.php'>Click here to login.</a>";
		}
	}else if(isset($_POST['whichForm'])){
		$theError = true;
		$errorMessage = "Please enter an email and a password";
	}
	else{
		$theError = false;
	}
	
	require_once($_SERVER['DOCUMENT_ROOT'] . '/common/constants.php');
?>

<?php
  $pageTitle = "Sign Up";
  $pageDescription = "";
  $pageKeywords = "";
?>

  
<?php	
	include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php"); 
	include_once($_SERVER['DOCUMENT_ROOT'] . "/common/mainNav.php");

?>



	<div id="fb-root"></div>
	<script>
		// Load the SDK Asynchronously
	  (function(d){
		 var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
		 if (d.getElementById(id)) {return;}
		 js = d.createElement('script'); js.id = id; js.async = true;
		 js.src = "//connect.facebook.net/en_US/all.js";
		 ref.parentNode.insertBefore(js, ref);
	   }(document));
	</script>
	
	
    
    <section class="container">
		<div id="blankBuffer" class="carousel-buffer"></div>
		<section class="container" id="mainContent">
			<div class="loginBox">
				<div class="span5 offset3">
					<div class="well">
						<legend>Sign up for Store Portal</legend>
						<?php if($theError): ?>
							<div class="alert alert-error">
								<a class="close" data-dismiss="alert" href="#">x</a><?php echo $errorMessage; ?>
							</div>
						<?php endif;//end if($theError) ?>
						<form method="POST" action="signup.php" name="loginform" id="loginform" class="cf" accept-charset="UTF-8">
							<input type="hidden" name="whichForm" id="whichForm" value="login"/>
							<input type="hidden" name="passMatch" id="passMatch" value="false"/>
							<input class="span4" placeholder="User Email" type="text" name="useremail" id="useremail">
							<input class="span4" placeholder="Password" type="password" name="password" id="password" >
						    <input class="span4" placeholder="Confirm Password" type="password" name="passwordCheck" id="passwordCheck" >
						    <div id="passNote" class="alert alert-error hideObject">Your passwords do not match</div>
							<button class="btn" type="submit">Sign up</button>
							<br><br><div id="completFormAlert" class='alert alert-error hideObject'>Please fill out the form and make sure your passwords match.</div>
						</form>
					</div>
				</div>
				
			</div><br />
            
        </section>
    </section>

	<script>
		$(document).ready(function(){
			$("#passwordCheck").on("input", function(){
				checkPasswords();
			});
			
			$("#signupForm").submit(function(e){
				e.preventDefault();
				var passwordMatch = $("#passMatch").val();
				//console.log(passwordMatch);
				if(passwordMatch == "true") {
				  $(this).unbind("submit").submit();
				}else{
				  checkPasswords();
				  var passwordMatchRecheck = $("#passMatch").val();
				  if(passwordMatchRecheck == "true") {
					$(this).unbind("submit").submit();
				  }else{
					$("#completFormAlert").removeClass("hideObject")
				  }
				}
			});
			
			function checkPasswords(){
			  var firstPassword = $("#password").val();
			  var passCheck = $("#passwordCheck").val();
			  
			  if (firstPassword == passCheck) {
					  $("#passNote").addClass("hideObject");
					  $("#passMatch").val("true");
			  }else{
					  $("#passNote").removeClass("hideObject");
					  $("#passMatch").val("false");
			  }
			}
		});
	</script>
	

<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/footer.php'); ?>
