<?php

	$inited = 	@file_exists(SA_DIR_AUTH);
	$hub = 	$CONFIG->getNodeVal("setup.hub")=="true";

	if ($inited || $hub) {

		if ($hub) {
			$auth = new Auth();
			$auth->check();
		}

		$page_title = $CONFIG->getNodeVal("setup.product_title");
		$need_flash_title = $LANG->lookup("Flash and Javascript Required");
		$need_flash_description = $LANG->lookup("Flash and Javascript Required Description");
		$need_flash_download = $LANG->lookup("Download Flash");
		$help_title = $LANG->lookup("Help Documentation");
		$help_description = markdown(@file_get_contents(SA_DIR_COREPATH."/lang/".$CONFIG->getNodeVal("setup.language").".wiki.txt"));
		$help_link = $LANG->lookup("Visit Support Site");

?>
<!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" lang="en" xml:lang="en">
	<head>
		<title><?php echo $page_title; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" type="text/css" href="<?php echo SA_DIR_COREPATH; ?>/css/itdradmin.css?v=<?php echo SA_PRODUCT_VERSION; ?>" media="all" />
		<script type="text/javascript" src="<?php echo SA_DIR_COREPATH; ?>/js/flashsite.js?v=<?php echo SA_PRODUCT_VERSION; ?>"></script>
		<script type="text/javascript">
			FlashSite.force(true);
			FlashSite.setSwfId("loader");
			FlashSite.setContainerId("container");
			FlashSite.setLoaderSwf("admin-loader.swf?v=<?php echo SA_PRODUCT_VERSION; ?>");
			FlashSite.setNamespace("<?php echo SA_NAMESPACE; ?>");
			FlashSite.setResizeStyle("ForceSize");
			FlashSite.setResizeMetrics(1000, 500);
			FlashSite.setSwfColor("#FFFFFF");
			FlashSite.setTitleFunction = function (title) {
				if (!title) title = "";
				title = "<?php echo $page_title; ?>: " + title;
				document.title = title;
			}
			FlashSite.start();
		</script>
	</head>
	<body>
		<div id="container">
			<div id="content">
				<div class="PanelBox">
					<div class="Top">
						<h2 title="<?php echo $need_flash_title; ?>"><?php echo $need_flash_title; ?></h2>
					</div>
					<div class="Body">
						<div class="WikiText"><?php echo $need_flash_description; ?></div>
					</div>
					<div class="Foot">
						<a class="OptionButton" href="http://get.adobe.com/flashplayer/"><?php echo $need_flash_download; ?></a>
					</div>
				</div>
				<div class="PanelBox">
					<div class="Top">
						<h2 title="<?php echo $help_title; ?>"><?php echo $help_title; ?></h2>
					</div>
					<div class="Body">
						<div class="WikiText"><?php echo $help_description; ?></div>
					</div>
					<div class="Foot">
						<a class="OptionButton" href="http://intothedarkroom.com/resources/"><?php echo $help_link; ?></a>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
<?php

	} else {

		/* get all the text stuff
			*/
		$page_title = $LANG->lookup("Initial Setup");
		$page_description = $LANG->lookup("Intial Page Description");
		$page_footer = $LANG->lookup("Intial Page Footer");
		$email_address_label = $LANG->lookup("Your Primary Email Address");
		$secret_question_label = $LANG->lookup("Your Secret Question");
		$secret_question_answer_label = $LANG->lookup("Secret Question Answer");
		$username_label = $LANG->lookup("Choose a Username");
		$password_label = $LANG->lookup("Choose a Password");
		$password_again_label = $LANG->lookup("Verify Password");
		$submit_title = $LANG->lookup("Initialize");
		/* this might be a submit call, lets pull any post vars in
			*/
		$form_error = "";
		$form_errors = array();
		$email_address = isset($_POST["email_address"]) ? strtolower($_POST["email_address"]) : "";
		$secret_question = isset($_POST["secret_question"]) ? $_POST["secret_question"] : "";
		$secret_question_answer = isset($_POST["secret_question_answer"]) ? strtolower($_POST["secret_question_answer"]) : "";
		$submit = isset($_POST["submit"]) ? $_POST["submit"] : "";
		$username = isset($_POST["username"]) ? $_POST["username"] : "";
		$password = isset($_POST["password"]) ? $_POST["password"] : "";
		$password_again = isset($_POST["password_again"]) ? $_POST["password_again"] : "";
		/* if it's a submit, search for errors
			*/
		if (strlen($submit)) {
			if (strlen($email_address)==0) $form_errors[] = $LANG->lookup("You must supply a valid Email Address");
			if (strlen($secret_question)==0) $form_errors[] = $LANG->lookup("You must supply a secret question");
			if (strlen($secret_question_answer)==0) $form_errors[] = $LANG->lookup("You must supply the answer to the secret question");
			if (strlen($username)<6) $form_errors[] = $LANG->lookup("Username must be a minimum of 6 characters");
			if (strlen($password)<6) $form_errors[] = $LANG->lookup("Password must be a minimum of 6 characters");
			if ($password!=$password_again) $form_errors[] = $LANG->lookup("Passwords do not match");
			/* if there are no errors, we should be good to go, create the account info
				and then redirect to yourself to start her up!
				auto exits and redirects
				*/
			if (count($form_errors)==0) {
				require_once(SA_DIR_COREPATH."/php/classes/simpleadmin/Gateway.php");
				$gateway = new Gateway();
				$gateway->createUser($username, $password, $email_address, $secret_question, $secret_question_answer);
				/* redirect to the new admin
					*/
				$path = Func::getBaseUrl() . "/" . SA_DIR_INDEXPATH . "?/admin/";
				header("Location: $path");
				exit;
			}
		}
		/* consolidate any errors
			*/
		foreach ($form_errors as $str) $form_error .= "<p>$str</p>";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
	<head>
		<title><?php echo $page_title; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" type="text/css" href="<?php echo SA_DIR_COREPATH; ?>/css/itdradmin.css?v=<?php echo SA_PRODUCT_VERSION; ?>" media="all" />
	</head>
	<body>
		<div id="container">
			<div id="content">
				<div class="PanelBox">
					<div class="Top">
						<h2 title="<?php echo $page_title; ?>"><?php echo $page_title; ?></h2>
					</div>
					<div class="Body">
						<div class="Description"><?php echo $page_description; ?></div>
							<form action="?/admin/" method="post">
							<div class="formtitle"><?php echo $email_address_label; ?></div>
							<input type="text" name="email_address"  value="<?php echo $email_address; ?>" />
							<div class="formtitle"><?php echo $secret_question_label; ?></div>
							<input type="text" name="secret_question"  value="<?php echo $secret_question; ?>" />
							<div class="formtitle"><?php echo $secret_question_answer_label; ?></div>
							<input type="text" name="secret_question_answer"  value="<?php echo $secret_question_answer; ?>" />
							<div class="formtitle"><?php echo $username_label; ?></div>
							<input type="text" name="username"  value="<?php echo $username; ?>" />
							<div class="formtitle"><?php echo $password_label; ?></div>
							<input type="password" name="password"  value="" />
							<div class="formtitle"><?php echo $password_again_label; ?></div>
							<input type="password" name="password_again"  value="" />
							<input class="InputButton" type="submit" name="submit" value="<?php echo $submit_title; ?>" />
							<?php if ($form_error!="") { ?><div class="formerror"><?php echo $form_error; ?></div><?php } ?>
						</form>
					</div>
					<div class="Foot">
						<div class="Description"><?php echo $page_footer; ?></div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
<?php

	}

?>