<?
if(!defined('IN_BANDTOPIA')) {
	header("Location: http://www.band-topia.com/index.php");
	die('you should not be here');
}

/***********************************************************/
/* Login Process
/***********************************************************/
if($action == 'logout') {
	session_destroy();
	header("Location: index.php?action=main");
	die("Error on page");
} elseif(isset($_SESSION['HTTP_USER_AGENT']) && isset($_SESSION['lskejd']) && $action == 'login') {
	$action = $prev_action;
	header("Location: index.php?action={$action}&prev_action={$prev_action}");
	die("Error on page");
} else {
	if(isset($_POST['post_form']) && isset($_POST['bt_login_btn']) && $action == 'login') {
		/*******************************************************/
		/* Verify user login information
		 /*******************************************************/
		$userObj = new UserObj();
		$userObj -> UserName = $_POST['bt_username'];
		$userObj -> GetUser();
		if($userObj -> UserId == '') {
			$template -> assign_block_vars('page_error', array());
			$template -> assign_block_vars('page_error.error_msg', array('ERROR_MSG' => 'The username you provided does not exist or has been deactivated. <br> Please try again.'));
		} else {
			if(!($userObj -> verifyPassword($_POST['bt_password']))) {
				$template -> assign_block_vars('page_error', array());
				$template -> assign_block_vars('page_error.error_msg', array('ERROR_MSG' => 'The password you provided does not match our records. <br> Please try again.'));
			} else {
				/*******************************************************/
				/* Register User Session Information
				 /*******************************************************/
				$_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);
				$string = $_SERVER['HTTP_USER_AGENT'];
				$string .= 'h8er89pkd3!';
				$lskejd = md5($string);

				$_SESSION['lskejd'] = $lskejd;

				$_SESSION['username'] = $userObj -> UserName;
				$_SESSION['UserFullName'] = $userObj -> UserFullName;
				$_SESSION['UserId'] = $userObj -> UserId;
				$_SESSION['UserTypeId'] = $userObj -> UserTypeId;
				$_SESSION['UserProfileId'] = $userObj->ProfileId;

				if($prev_action == 'login') {
					$prev_action = 'main';
				}
				switch ($userObj -> UserTypeId)
				{
					case 2:
					case 4:
						header('Location: /admin/index.php');
						die('Error Redirecting to Promotion');
						break;
					default:
						header('Location: /cr/index.php');
						die('Error Redirecting to UserPage');
						break;
				}

				header("Location: index.php?action={$prev_action}");
				die("Error on page");
			}
		}
	} elseif(!$allowAccess) {
		header("Location: index.php?action=login&prev_action={$action}");
		die("Error on page");
	}
}
?>