<?php
session_start();
if (isset($_SESSION["login"])) {
} else {
header("Location: index.php?TimeOut=1");
}
?>
<?php
   include "../includes/connection.inc.php";
   $var = ($_GET['resource_type']=="");
   //echo "::$var::<br>";
   $var = (!array_key_exists('resource_type',$_GET));
   //echo "::$var::<br>";
   if(array_key_exists("resource_type",$_GET) && !array_key_exists("resource_name",$_GET))
   {
   	$rt = $_GET['resource_type'];
	//echo "outer if rt<br>";
	  if($rt=="")
	  {
	  	$resourcetypecombo="<select id=\"resource_type\" name=\"resource_type\" onChange=\"updatePage1()\">
          <option>Select An Option</option>
          <option value=\"1\">dyamic contents[pages]</option>
          <option value=\"2\">Files on server</option>
        </select>";
	  }	
	  else if($rt==1) // dynamic contents (pages) is selected
	  {
	  	//echo "in rt=1<br>";
	  	$resourcetypecombo="<select id=\"resource_type\" name=\"resource_type\" onChange=\"updatePage1()\">
          <option>Select An Option</option>
          <option value=\"1\" selected=\"selected\">dyamic contents[pages]</option>
          <option value=\"2\">Files on server</option>
        </select>";
	  	$sql = "select * from pages order by page_name";
	  	$Result = mysql_query($sql);
			$resoucenamecombo = "<option value=\"\">Select A Page</option>";
			while($row = mysql_fetch_array($Result, MYSQL_BOTH))
		 	{
		 		$id=$row[id];
				$page_name = $row[page_name];
				$resoucenamecombo .= "<option value=\"$id\">$page_name</option>";
		 	}
	  }
	  else if($rt==2)
	  {
	  		//echo "in rt=2<br>";
			$resourcetypecombo="<select id=\"resource_type\" name=\"resource_type\" onChange=\"updatePage1()\">
          <option>Select An Option</option>
          <option value=\"1\">dyamic contents[pages]</option>
          <option value=\"2\"  selected=\"selected\">Files on server</option>
        </select>";
	  	$resoucenamecombo = "<option value=\"\">Select A File</option>";
	  	$dh  = opendir("../htmlfiles");
			clearstatcache();
			while (false !== ($filename = readdir($dh))) {
   			$files[] = $filename;
			}
			sort($files);
			$size=count($files);
			for($i=0; $i<$size; $i++)
				{
					$ext = substr(strrchr($files[$i], "."), 1); /* get extension without dot */
					if(is_file("../htmlfiles/$files[$i]") && ($ext=='htm' || $ext=='html'))
	  				{
	  					$resoucenamecombo.="<option value=\"{$files[$i]}\">{$files[$i]}</option>";
	  				}
				}
	  }
	  
   }
   else if(array_key_exists('resource_type',$_GET) && array_key_exists('resource_name',$_GET)) // if resource_type and resource_name selected change HTML contents of WYSIWYG HTML editor
   {
   		//echo "in second if";
		$rt=$_GET['resource_type'];
		if($rt=="")
	  {
	  	$resourcetypecombo="<select id=\"resource_type\" name=\"resource_type\" onChange=\"updatePage1()\">
          <option>Select An Option</option>
          <option value=\"1\">dyamic contents[pages]</option>
          <option value=\"2\">Files on server</option>
        </select>";
	  }	
	  else if($rt==1) // dynamic contents (pages) is selected
	  {
	  	$pageid = $_GET['resource_name'];
	  	//echo "in rt=1<br>";
	  	$resourcetypecombo="<select id=\"resource_type\" name=\"resource_type\" onChange=\"updatePage1()\">
          <option>Select An Option</option>
          <option value=\"1\" selected=\"selected\">dyamic contents[pages]</option>
          <option value=\"2\">Files on server</option>
        </select>";
	  	$sql = "select * from pages order by page_name";
	  	$Result = mysql_query($sql);
			$resoucenamecombo = "<option value=\"\">Select A Page</option>";
			while($row = mysql_fetch_array($Result, MYSQL_BOTH))
		 	{
		 		$id=$row[id];
				$page_name = $row[page_name];
				if($id==$pageid)
					$resoucenamecombo .= "<option value=\"$id\" selected=\"selected\">$page_name</option>";
				else
					$resoucenamecombo .= "<option value=\"$id\">$page_name</option>";
		 	}
		 	$sql  = "select * from pages where id=$pageid";
		 	$Result = mysql_query($sql);
		 	$row = mysql_fetch_array($Result, MYSQL_BOTH);
			$PageAuthorName = $row['author_name'];
			$PageTitle = $row['title'];
		 	$contents = $row['body'];
	  }
	  else if($rt==2)
	  {
	  		$ffilename = $_GET['resource_name'];
	  		//echo "in rt=2<br>";
			$resourcetypecombo="<select id=\"resource_type\" name=\"resource_type\" onChange=\"updatePage1()\">
          <option>Select An Option</option>
          <option value=\"1\">dyamic contents[pages]</option>
          <option value=\"2\"  selected=\"selected\">Files on server</option>
        </select>";
	  	$resoucenamecombo = "<option value=\"\">Select A File</option>";
	  	$dh  = opendir("../htmlfiles");
			clearstatcache();
			while (false !== ($filename = readdir($dh))) {
   			$files[] = $filename;
			}
			sort($files);
			$size=count($files);
			for($i=0; $i<$size; $i++)
				{
					$ext = substr(strrchr($files[$i], "."), 1); /* get extension without dot */
					if(is_file("../htmlfiles/$files[$i]") && ($ext=='htm' || $ext=='html'))
	  				{
						//echo "$ffilename==$files[$i]<br>";
						if($ffilename==$files[$i])
	  						$resoucenamecombo.="<option value=\"{$files[$i]}\" selected=\"selected\">{$files[$i]}</option>";
						else
							$resoucenamecombo.="<option value=\"{$files[$i]}\">{$files[$i]}</option>";
	  				}
				}
			
			$PageAuthorEnabled = "Disabled='Disabled'";  // Disable text field, no use in case of files
			$PageTitleEnabled = "Disabled='Disabled'"; // Disable text field, no use in case of files
			$PageAuthorName = "Disabled, no use...";
			$PageTitle = "Disabled, no use...";
			$contents=file_get_contents("../htmlfiles/$ffilename");
	  }
	  
		
		
   }
	else if(!array_key_exists('resource_type',$_GET))// first time page visited
	{
		//echo "in last if";
		$resourcetypecombo="<select id=\"resource_type\" name=\"resource_type\" onChange=\"updatePage1()\">
          <option>Select An Option</option>
          <option value=\"1\">dyamic contents[pages]</option>
          <option value=\"2\">Files on server</option>
        </select>";
	}
   
?>
<html>
<head>
<link rel="stylesheet" href="main.css" type="text/css" />
<script language="javascript">
function updatePage1()
{
	control = document.getElementById('resource_type');
	rt = control.value;
	document.form1.action = 'EditPage.php?resource_type='+rt;
	document.form1.submit();
}
function updatePage2()
{
	control_one = document.getElementById('resource_type');
	control_two = document.getElementById('resource_name');
	rt = control_one.value;
	rn = control_two.value;
	document.form1.action = 'EditPage.php?resource_type='+rt+'&resource_name='+rn;
	document.form1.submit();
}
</script>
</head>
<?php
include("FCKeditor/fckeditor.php") ;
?>
<body >
<form  name="form1"action="EditPageAction.php" method="post">
<table align="center" class="maintable">
  <tr><td colspan="2"><?php include "header.php"; ?></td></tr>
  <tr>
    <td id='menuPanel' width="14%" ><?php include "includes/menu.inc.php"; ?>      &nbsp;</td>
    <td width="86%"><div id='mainpanel'><table width="99%" border="0">
      <tr>
          <td><p>Select Type </td><td width="34%"><label>
        <?=$resourcetypecombo?>
        </label></td>
        <td width="15%">Select A Name :          </td>
        <td width="29%"><div id="uniqness">
          <select id="resource_name"  name="resource_name" onChange="updatePage2()">
		  <?=$resoucenamecombo?>
          </select>
          </div></td>
      </tr>
      <tr>
        <td>Page Author Name: </td>
        <td colspan="3"><label>
          <input type="text" id="author" name="author" value="<?=$PageAuthorName?>" <?=$PageAuthorEnabled?> >
        [optional]</label></td>
      </tr>
      <tr>
        <td width="22%">Enter Page Title : </td>
        <td colspan="3"><label>
          <input type="text" id="title" name="title" value="<?=$PageTitle?>" <?=$PageTitleEnabled?>>
        </label></td>
      </tr>
      <tr>
        <td colspan="4"><?php
		$oFCKeditor = new FCKeditor('pageBody') ;
		$oFCKeditor->BasePath = 'FCKeditor/';
		$oFCKeditor->Value = $contents;
		$oFCKeditor->Height = '400' ;
		$oFCKeditor->Create() ;
	?></td>
        </tr>
      <tr>
        <td colspan="4"><label>
          <div align="center">
            <input type="submit" name="Submit" value="Submit">
            </div>
        </label></td>
      </tr>
    </table></div></td>
  </tr><tr><td colspan="2"><?php include "footer.php"; ?></td></tr>
</table>

</form>

</body>
</html>