<?php
	include 'AdminRedirect.php';
	include 'top.php';
	
	$Text = $pos = $Decoration = $Color =  "";
	$TextErr = $posErr = "";
	$error = 0;
	
	session_start();
	if (!isset($_SESSION['Title'])) {
		$_SESSION['Title'] = TRUE;
	}
	$Title = $_SESSION['Title'];
	session_write_close();

	function StoreText($Text, $Align, $Decoration, $Color)
	{
		$NewText = "";
		session_start();
		
		if ($_SESSION['Title'] == TRUE) {
			$_SESSION['Title'] = FALSE;
			if ($Align == "Left") {
				if ($Decoration == "Plain") {
					$NewText = "<h1 style='text-align: left; color: " .$Color. ";'>" .$Text. "</h1>";
					$_SESSION['Text'] = $NewText;
				}
				else if ($Decoration == "Italics") {
					$NewText = "<h1 style='text-align: left; color: " .$Color. ";'><i>" .$Text. "</i></h1>";
					$_SESSION['Text'] = $NewText;
				}
				else if ($Decoration == "Bold") {
					$NewText = "<h1 style='text-align: left; color: " .$Color. ";'><b>" .$Text. "</b></h1>";
					$_SESSION['Text'] = $NewText;
				}
				else {
					die("<script>alert('Fatal Error'); window.location = 'TextEdit.php';</script>");
				}
			}
			else if ($Align == "Center") {
				if ($Decoration == "Plain") {
					$NewText = "<h1 style='text-align: center; color: " .$Color. ";'>" .$Text. "</h1>";
					$_SESSION['Text'] = $NewText;
				}
				else if ($Decoration == "Italics") {
					$NewText = "<h1 style='text-align: center; color: " .$Color. ";'><i>" .$Text. "</i></h1>";
					$_SESSION['Text'] = $NewText;
				}
				else if ($Decoration == "Bold") {
					$NewText = "<h1 style='text-align: center; color: " .$Color. ";'><b>" .$Text. "</b></h1>";
					$_SESSION['Text'] = $NewText;
				}
				else {
					die("<script>alert('Fatal Error'); window.location = 'TextEdit.php';</script>");
				}
			}
			else if ($Align == "Right") {
				if ($Decoration == "Plain") {
					$NewText = "<h1 style='text-align: right; color: " .$Color. ";'>" .$Text. "</h1>";
					$_SESSION['Text'] = $NewText;
				}
				else if ($Decoration == "Italics") {
					$NewText = "<h1 style='text-align: right; color: " .$Color. ";'><i>" .$Text. "</i></h1>";
					$_SESSION['Text'] = $NewText;
				}
				else if ($Decoration == "Bold") {
					$NewText = "<h1 style='text-align: right; color: " .$Color. ";'><b>" .$Text. "</b></h1>";
					$_SESSION['Text'] = $NewText;
				}
				else {
					die("<script>alert('Fatal Error'); window.location = 'TextEdit.php';</script>");
				}
			}
			else {
				die("<script>alert('Fatal Error 1'); window.location = 'TextEdit.php';</script>");
			}
		}
		else if ($_SESSION['Title'] == FALSE) {
			if ($Align == "Left") {
				if ($Decoration == "Plain") {
					$NewText = "<p style='text-align: left; color: " .$Color.";'>".$Text."</p>";
					$_SESSION['Text'] .= $NewText;
				}
				else if ($Decoration == "Italics") {
					$NewText = "<p style='text-align: left; color: " .$Color. ";'><i>".$Text."</i></p>";
					$_SESSION['Text'] .= $NewText;
				}
				else if ($Decoration == "Bold") {
					$NewText = "<p style='text-align: left; color: " .$Color. ";'><b>".$Text."</b></p>";
					$_SESSION['Text'] .= $NewText;
				}
				else {
					die("<script>alert('Fatal Error'); window.location = 'TextEdit.php';</script>");
				}
			}
			else if ($Align == "Center") {
				if ($Decoration == "Plain") {
					$NewText = "<p style='text-align: center; color: " .$Color.";'>".$Text."</p>";
					$_SESSION['Text'] .= $NewText;
				}
				else if ($Decoration == "Italics") {
					$NewText = "<p style='text-align: center; color: " .$Color. ";'><i>".$Text."</i></p>";
					$_SESSION['Text'] .= $NewText;
				}
				else if ($Decoration == "Bold") {
					$NewText = "<p style='text-align: center; color: " .$Color. ";'><b>".$Text."</b></p>";
					$_SESSION['Text'] .= $NewText;
				}
				else {
					die("<script>alert('Fatal Error'); window.location = 'TextEdit.php';</script>");
				}
			}
			else if ($Align == "Right") {
				if ($Decoration == "Plain") {
					$NewText = "<p style='text-align: right; color: " .$Color.";'>".$Text."</p>";
					$_SESSION['Text'] .= $NewText;
				}
				else if ($Decoration == "Italics") {
					$NewText = "<p style='text-align: right; color: " .$Color. ";'><i>".$Text."</i></p>";
					$_SESSION['Text'] .= $NewText;
				}
				else if ($Decoration == "Bold") {
					$NewText = "<p style='text-align: right; color: " .$Color. ";'><b>".$Text."</b></p>";
					$_SESSION['Text'] .= $NewText;
				}
				else {
					die("<script>alert('Fatal Error'); window.location = 'TextEdit.php';</script>");
				}
			}
			else {
				die("<script>alert('Fatal Error 2'); window.location = 'TextEdit.php';</script>");
			}
		}
		else {
			die("<script>alert('Fatal Error 3'); window.location = 'TextEdit.php';</script>");
		}
		
		session_write_close();
	}
	
	if (!empty($_POST['Next'])) {
		if (empty($_POST['text'])) {
			$TextErr = "Textbox can not be blank";
			$error++;
		}
		else {
			$Text = $_POST['text'];
		}
		
		if (empty($_POST['pos'])) {
			$TextErr = "Please Select An Option";
			$error++;
		}
		else {
			$pos = $_POST['pos'];
		}
		
		if (empty($_POST['Decoration'])) {
			$TextErr = "Please select a text decoration";
			$error++;
		}
		else {
			$Decoration = $_POST['Decoration'];
		}
		
		if (empty($_POST['Color'])) {
			$TextErr = "Please select a font color. White is default";
			$error++;
		}
		else {
			$Color = $_POST['Color'];
		}
		
		if ($error == 0) {
			StoreText($Text, $pos, $Decoration, $Color);
		}
	}
	else if (!empty($_POST['Cancel'])) {
		echo("<script>window.location = 'TextEdit.php';</script>");
	}
	else if (!empty($_POST['submit'])) {
		if (empty($_POST['text'])) {
			$TextErr = "Textbox can not be blank";
			$error++;
		}
		else {
			$Text = $_POST['text'];
		}
		
		if (empty($_POST['pos'])) {
			$posErr = "Please Select An Option";
			$error++;
		}
		else {
			$pos = $_POST['pos'];
		}
		
		if (empty($_POST['Decoration'])) {
			$TextErr = "Please select a text decoration";
			$error++;
		}
		else {
			$Decoration = $_POST['Decoration'];
		}
		
		if (empty($_POST['Color'])) {
			$TextErr = "Please select a font color. White is default";
			$error++;
		}
		else {
			$Color = $_POST['Color'];
		}
		
		if ($error == 0) {
			StoreText($Text, $pos, $Decoration, $Color);
			session_start();
			$_SESSION['PageToEdit'] = "Home";
			session_write_close();
			echo("<script>window.location = 'TextEditConfirm.php';</script>");
		}
	}
	else if (!empty($_POST['restore'])) {
		$Text = "";
		$File = fopen("BackUp/txt/HomeText.txt", "r") or die("<script>alert('Error Restoring HomeText.txt BackUp');</script>");
		$Text = fread($File, filesize("BackUp/txt/HomeText.txt"));
		fclose($File);
		
		$File = fopen("../txt/HomeText.txt", "w") or die("<script>alert('Error Restoring HomeText.txt BackUp 2');</script>");
		fwrite($File, $Text);
		fclose($File);
		
		echo("<script>window.location = 'TextEdit.php';</script>");
	}
?>
<!DOCTYPE html>
<html>
	<head>
    	<link rel="stylesheet" type="text/css" href="body.css">
        <link rel="stylesheet" type="text/css" href="AdminSideBar.css">
        <style>
			.Buttons {width: 100%; height: 100%; border: none; background-color: black; color: white;}
			.Buttons:hover {background-color: white; color: black;}
			.error {color: red;}
		</style>
    </head>
    <body class="background">
    	<div class="sidebar">
            <div class="infoContainer"><a href="AccountManager.php">Account Manager</a></div>
            <div class="infoContainer"><a href="TextEdit.php">Page Text Editor</a></div>
            <div class="infoContainer"><a href="APhotoManager.php">Photos</a></div>
            <div class="infoContainer"><a href="AReview.php">Review Manager</a></div>
            <div class="infoContainer"><a href="ALogOut.php">LogOut</a></div>
        </div>
        <div class="bodyContainer">
        	<form method="post" action="<?php htmlspecialchars($_SERVER['PHP_SELF']);?>">
            	<span class="error">* <?php echo($TextErr);?></span>
                <?php
					if ($Title == TRUE) {
						echo("<h1 style='text-align: center;'>Home Text Title</h1>");
					}
					else {
						echo("<h1 style='text-align: center;'>Home Text Paragraph</h1>");
					}
				?>
                <textarea cols="160" rows="30" name="text"></textarea>
                <div style="position: relative; float: left; width: 49.5%; height: 56px; border: 2px solid white; overflow: auto;">
                	<div style="position: relative; float: left; width: 32.5%; overflow: auto; text-align: center;">
                    	<p>Left<input type="radio" name="pos" value="Left" /></p>
                    </div>
                    <div style="position: relative; float: left; width: 32.5%; overflow: auto; text-align: center;">
                    	<p>Center<input type="radio" name="pos" value="Center" checked="checked" /></p>
                    </div>
                    <div style="position: relative; float: left; width: 32.5%; overflow: auto; text-align: center;">
                    	<p>Right<input type="radio" name="pos" value="Right" /></p>
                    </div>
                </div>
                <div style="float: left; width: 49.5%; height: 56px; overflow: auto; border: 2px solid white;">
                	<div style="position: relative; float: left; width: 25%; overflow: auto; text-align: center;">
                    	<p>Plain<input type="radio" name="Decoration" value="Plain" checked="checked" /></p>
                    </div>
                    <div style="position: relative; float: left; width: 25%; overflow: auto; text-align: center;">
                    	<p>Italics<input type="radio" name="Decoration" value="Italics" /></p>
                    </div>
                    <div style="position: relative; float: left; width: 25%; overflow: auto; text-align: center;">
                    	<p>Bold<input type="radio" name="Decoration" value="Bold" /></p>
                    </div>
                    <div style="position: relative; float: left; width: 25%; overflow: auto; text-align: center;">
                		<select name="Color" style="background-color: black; color: white; width: 90%;">
                    		<option value="white" style="color: white;">White</option>
                        	<option value="blue" style="color: blue;">Blue</option>
                        	<option value="red" style="color: red;">Red</option>
                        	<option value="green" style="color: green;">Green</option>
                    	</select>
                    </div>
                </div>
                <div style="clear: both; margin: 0px auto; margin-bottom: 10px; margin-top: 5px; width: 400px; height: 35px; overflow: auto; border: 2px solid white;">
                	<div style="position: relative; float: left; width: 24.6%; height: 100%; border-left: 0.5px solid white; background-color: black; overflow: auto">
                    	<input type="submit" name="restore" value="Restore" class="Buttons">
                    </div>
                	<div style="position: relative; float: left; width: 24.6%; height: 100%; border-left: 0.5px solid white; border-right: 0.5px solid white; background-color: black; overflow: auto">
                    	<input type="submit" name="Cancel" value="Cancel" class="Buttons">
                    </div>
                	<div style="position: relative; float: left; width: 24.6%; height: 100%; border-left: 0.5px solid white; border-right: 0.5px solid white; background-color: black; overflow: auto">
                    	<input type="submit" name="Next" value="Next" class="Buttons">
                    </div>
                    <div style="position: relative; float: left; width: 24.6%; height: 100%; border-left: 0.5px solid white; background-color: black; overflow: auto">
                    	<input type="submit" name="submit" value="Finish" class="Buttons">
                    </div>
                </div>
            </form>
        </div>
        <div class="sidebarRight">
        	<div class="infoContainer"><a href="HomeTextEditor.php">Home Text Edit</a></div>
            <div class="infoContainer"><a href="AboutTextEditor.php">About Text Edit</a></div>
        </div>
    </body>
</html>