<?php
include(BASE_URL.'includes/admin/editors/fckeditor/fckeditor.php');
$list = $theme->buildNavArray(true); 

	$options = "<option value='0'>None (Root Item)</option>";
	$list = $theme->buildNavArray(true);
foreach($list as $key => $value) {
	if($key == "") {}
	else {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE id = '$id'";
		$query = $admin->db_query($sql) or die(mysql_error());
		$row = mysql_fetch_array($query);
		$options .= '<option value="'.$value.'"';
		if($value == $row['parent_id']) {
			$options .= ' selected="selected"';
		}
		$options .= '>'.$key.'</option>';
	}
}
for($col = -20; $col <= 20; $col++) {
	$weight .= "\t<option value='$col'";
	if($col == $rec['orderingid']) {
		$weight .= " selected='selected'";
	}
	$weight .= ">$col</option>\n";
}

if($_POST['save']) {
	foreach($_POST as $key => $value) {
		$_POST[$key] = mysql_real_escape_string($value);
	}
	$error = "";
	if($_POST['page-title'] == "") {
		$error[] = "Don't leave the page title blank!";
	}
	if($_POST['menu-title'] == "") {
		$error[] = "Don't leave the menu title blank!";
	}
	$time = time();
	if($error == "") {
		$sql = "INSERT INTO ".DBTABLEPREFIX."content_pages (title, content, orderingid, menutitle, parent_id, time) VALUES ('".$_POST['page-title']."', '".$_POST['content']."', '".$_POST['weight']."', '".$_POST['menu-title']."', '".$_POST['parent-item']."', '$time')";
		$content = $admin->db_query($sql) or die(mysql_error());
		if($content) {
			$output .= $admin->message("success","Woohoo! Your Page \"".$_POST['page-title']."\" was added!", "Click 'View Site' in upper right hand corner to see your page");
		}
	}
	if($error !== "") {
		$list_errors = "<ul class='error-list'>";
		foreach($error as $key => $value) {
			$list_errors .= "<li>".$value."</li>";
		}
		$list_errors .= "</ul></div>";
		$output .= $admin->message("error", "Oops! We encountered ".count($error)." error(s) when trying to add the page.", $list_errors);
	}
}
else {
		$FCKeditor_content = new FCKeditor('content') ;
		$FCKeditor_content->BasePath = THEME_URL.'includes/admin/editors/fckeditor/';
		$FCKeditor_content->Value ='';
		$FCKeditor_content->Width = '100%' ;
		$FCKeditor_content->Height = '350' ;
		$FCKeditor_content_html = $FCKeditor_content->CreateHtml();
	$form = '
						<form action="" method="post">
							<fieldset>
								<h2>Add New Page</h2>
								<div class="form-row">
									<label for="page-title" class="page-title">Page Title:</label><span><input type="text" class="page-title" name="page-title" /></span>
								</div>
								<div class="form-row">
									<label for="menu-title">Menu Title:<span class="small gray">This is what will show up in the menu.</span></label><span><input type="text" name="menu-title" /></span>
								</div>
								<div class="form-row">
									<label for="parent-item">Parent Item</label>
									<span>
										<select name="parent-item">
										'.$options.'
										</select>
									</select>
									</span>
								</div>
								<div class="form-row">
									<label for="weight">Weight <a href="javascript:;" class="helplink" title="<strong style=\'font-size:16px;display:block;\'>Weight</strong>Weight Allows you to manage how items are ordered.<br />For example: If you weight this page 12 and another page 11, in the menu the other page would show before this one.">[Help]</a></label>
									<span>
										<select name="weight">
										'.$weight.'
										</select>
									</span>
								</div>
								<div class="form-row">
									<label class="content" for="content">Content</label>
									<span class="content">
										'.$FCKeditor_content_html.'
									</span>
								</div>
								<div class="form-row form-row-last">
									<label for="save">Save</label>
									<span><input type="submit" name="save" value="Add New Page" class="submit" /></span>
								</div>
							</fieldset>
						</form>
	';
	$output .= $form;
}
$admin->page_info['content']['page_title'] = "Add New Page";
$admin->page_info['content']['page_content']	= $output;
//Display page
include(BASE_URL.'includes/admin/admin.php');
?>