<?php
session_start();
include("header.php");
include("leftSideMenu.php");
include("../config.php");
include("FCKeditor/fckeditor.php");

$sucessMessage  = "";
$description    = "";
$pageName       = "";

if(isset($_GET['editid'])){

  $pageId             = $_GET['editid'];
  $sqlGetPageDetails  = "SELECT * FROM menus WHERE id = '".$pageId."'";
  $queryPageDetails   = mysql_query($sqlGetPageDetails);
  $resultPageDetails  = mysql_fetch_row($queryPageDetails);
  $pageName           = $resultPageDetails[2];
  $description        = $resultPageDetails[3];
}

if(isset($_POST['addPage'])){

  $menuType = "left";
  $pageName = $_POST['name'];
  $pageDesc = $_POST['page_description'];
  $date     = date('d-m-Y');

  if(isset($_POST['updatePage'])){
    $pageId = $_POST['updatePage'];
    $sqlInsertPage = "UPDATE menus SET `page_name`='".$pageName."',`page_description`='".$pageDesc."',`last_modified`='".$date."' WHERE id = '".$pageId."'";
    $msg = "Page Updated Successfully";
  }else{
    $sqlInsertPage = "INSERT INTO menus (`menu_type`,`page_name`,`page_description`,`last_modified`) values ('".$menuType."','".$pageName."','".$pageDesc."','".$date."')";
    $msg = "Page Added Successfully";
  }
  $fireQuery = mysql_query($sqlInsertPage);
  if($fireQuery){
    $sucessMessage = $msg;
  }
}

?>
<style>
.error{
  color:red;
}
</style>

<script type="text/javascript">
function checkAddPage(){
  var pageName = document.getElementById("name").value;
  if(pageName == ''){
    document.getElementById("addPageError").innerHTML = "Please Enter Page Name";
    return false;
  }else{
    return true;
  }
}
</script>

<div id="main">
	<div class="full_w">
		<div class="h_title">Top Menu Management</div>
		<h2>Add Page to Top Menu</h2>

    <form action="" method="post" name="addLeftMenuPage" id="addLeftMenuPage">
			<span class="error" id="addPageError" style="padding-left:0px;"><?php echo $sucessMessage;?></span>

			<div class="element">
				<label for="name">Page Name <span class="red">*</span></label>
				<input id="name" name="name" type="text" size="30" value="<?php echo $pageName;?>" />
			</div>

			<div class="element">
        <label for="page_description">Page Description </label>
				<?php			
          $oFCKeditor = new FCKeditor('page_description');
          $oFCKeditor->BasePath = 'FCKeditor/';
          $oFCKeditor->Value = $description;
          $oFCKeditor->Width  = '100%';
          $oFCKeditor->Height = '500';
          $oFCKeditor->Create();
        ?>
			</div>

			<div class="entry">
				<div class="sep"></div>
				<input type="hidden" name="addPage" value="true">
        <?php
        if(isset($_GET['editid'])){
        ?>
          <input type="hidden" name="updatePage" value="<?php echo $_GET['editid'];?>">
          <button type="submit" class="button add" onclick="return checkAddPage()">Update Page</button>
        <?php
        }else{
        ?>
          <button type="submit" class="button add" onclick="return checkAddPage()">Add Page</button>
        <?php
        }
        ?>
				
			</div>
		</form>
			
		<div class="entry">
			<div class="sep"></div>
		</div>
	
	</div>

</div>
<div class="clear"></div>
</div>

<?php include("footer.php"); ?>