<?php 
	
	
	require_once($_SERVER['DOCUMENT_ROOT'] . '/admin/inc/constants.inc.php');
	
	if(!isset($_SESSION)){
		session_start();
	}
	
	require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.connector.inc.php");
	require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/sessionStartGeneral.php");
	$db = new connector();
	$theError = false;

	if(isset($loggedIn) && $loggedIn === true){
		if(isset($_SESSION['returnURL'])){
			if($_SESSION['returnURL'] === "/admin/interactions.php" && $_SERVER['REQUEST_URI'] !== "/login" && $_SERVER['REQUEST_URI'] !== "/login.php"){
				header("Location: index.php");
				die;
			}else if($_SESSION['returnURL'] === "/about-rewards.php"){
				header("Location: about-rewards.php");
				die;
			}else{
				//$returnURL = preg_replace('/\?.*/', '', str_replace(".php", "", $_SESSION['returnURL'])) != "/login" ? $_SESSION['returnURL'] : "/account";
				
				$returnURL = $_SESSION['returnURL'];
				
				if(strpos($returnURL, 'login') >= 0){
					header("Location: account.php");
				}else{
					header("Location: " . $returnURL);
				}
				
				die;
			}
			
		}else{
			$httpsVersion = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http';
			// header("Location: " . $httpsVersion . "://" . $_SERVER['SERVER_NAME'] . "/account.php");
			header("Location: account.php");
			
			//echo "<h1>YES, YOU ARE LOGGED IN</h1>";
			die;
		}
		
	}else{
		if(isset($_GET['l']) && $_GET['l'] == "false"){
			$message = "Login to your account to access your account information.";
		}else if(isset($_GET['t']) && $_GET['t'] == "false"){
			$message = "The token you provided is not valid. Please login to access your account information.";
		}else if(isset($_GET['td']) && $_GET['td'] == "false"){
			$message = "The token could not be decoded. Please login to access your account information.";
		}
		
	}
	
	if(isset($_GET['user']) && $_GET['user'] == "y"){
		$message = "That phone number or email address is already registered. Please login to your account to access your account information.";
	}
	
	if(isset($_GET['token']) && $_GET['token'] == "false"){
		$message = "We are having connection issues and can't log you in. Please try again in a minute.";
	}
	
	if(!empty($_POST['username']) && !empty($_POST['password'])){
		require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.brdata.inc.php");
		
		$users = new brdata();
		
		if($_POST['whichForm'] == "login"){
			$theResult = $users->accountLogin();
		}else if($_POST['whichForm'] == "facebook"){
			$theResult = $users->accountLoginFB();
		}
		
		if($theResult === true){
			if(isset($_SESSION['returnURL'])){
				if($_SESSION['returnURL'] === "/about-rewards"){
					header("Location: about-rewards.php");
					die;
				}
				header("Location: " . $_SESSION['returnURL']);
			}else{
				header("Location: index.php");
			}
		}else{ //If it couldn't register
			$theError = true;
			if(isset($theResult['reason'])){
				if($theResult['reason'] == "Unknown error - 000000004"){
					$theMessage = "Please email us at perks@afstores.com to trouble shoot your account";
				}else{
					$theMessage = $theResult['reason'];
				}
			}
		}
	}else if(isset($_POST['whichForm'])){
		$theError = true;
	}
	else{
		$theError = false;
	}
	
	$requestResetExists = false;
	if(!empty($_GET['resetRequest'])){
		$requestResetExists = true;
		$requestReset = $_GET['resetRequest'] === "true" ? true : false;
		
		if(!$requestReset){
			$requestResetError = isset($_GET['reason']) ? $_GET['reason'] : "There was a problem sending the password reset request. Please try again in a few minutes.";
		}
	}
	
	$pageTitle = "Login";
	$pageDescription = "Login page for thaynevalleymarket.com";
	$pageKeywords = "Login page";
	$loggedIn = false;
	
	require_once($_SERVER['DOCUMENT_ROOT'] . '/common/constants.php');
	
	include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php"); 
	include_once($_SERVER['DOCUMENT_ROOT'] . "/common/mainNav.php");
	
?>

    <section class="container">
		<div id="blankBuffer" class="carousel-buffer"></div>
		<section class="row" id="mainContent">
			<div class="loginBox clearfix">
				<div class="span5 clearfix" style="margin:0 auto;float:none">
					<?php
						if($requestResetExists){
							if($requestReset){
					?>
								<div class="alert alert-success">
									<a class="close" data-dismiss="alert" href="#">x</a>Successfully updated your password. Please login with your new password.
								</div>
					<?php
							}else{
					?>
								<div class="alert alert-error">
									<a class="close" data-dismiss="alert" href="#">x</a><?php echo $requestResetError; ?>
								</div>
					<?php
							}
						}//end if($requestResetExists)
						
						if(isset($message) && $message !== false){
					?>
							<div class="alert alert-error">
								<a class="close" data-dismiss="alert" href="#">x</a> <?php echo $message; ?>
							</div>
					<?php
						}
					?>
					<div class="well">
						<legend>Sign in to Valley Market Thayne</legend>
						<?php if($theError): ?>
							<div class="alert alert-error">
								<a class="close" data-dismiss="alert" href="#">x</a><?php echo isset($message) ? $message : "Either your email or password was incorrect.<br>Your Perks account would have to be re-registered using your email address as your username. If you haven’t done this <a href='/register'>please register</a> to access your account.</br>Or <a href='#forgotPassword'>click here</a> to reset your password."; ?>
							</div>
						<?php endif;//end if($theError) ?>
						<?php
							//$redirect = isset($_GET['redirect']) ? $_GET['redirect'] == 'coupons' ? '?redirect=coupons' : '' : '';
							include($_SERVER['DOCUMENT_ROOT'] . '/includes/loginForm.php');
						?>
						<div>
						  <a href="/register.php" class="pull-left">Register</a>
						  <a href="#forgotPassword" class="pull-right" data-toggle="modal" role="button">Forgot password?</a>
						</div>
						<br><br>
					</div>
				</div>
				
				<!--<div class="span5 offset3">
					<div class="well info">
						<p>Don't have an account? <a href="/register">Click here to sign up.</a></p>
					</div>
				</div>-->
			</div><br />
        </section>
    </section>
	
	<script>
		$(document).ready(function(){
			$("#loggedOutContainer, #loginModal").remove();
		});
	</script>

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