<?php
session_start();

if( isset($_SESSION['init_info']) )
{
	if( ( $str_temp = trim($_SESSION['init_info']) ) != '' )
		$str_init_info = $str_temp;

	unset($_SESSION['init_info']);
}

include('includes/check_login.php');

require_once('includes/common.php');

require_once('includes/author.php');
$obj_class_author = new class_author();

$arr_news_cates = $obj_class_author->get_news_cates();
?>
<!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" xml:lang="utf-8" lang="utf-8">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta http-equiv="content-language" content="zh-tw" />
	<meta name="robots" content="none" />
	<link rel="stylesheet" type="text/css" href="author.css" />
	<link rel="stylesheet" type="text/css" href="redactor/redactor.css" />
	<style type="text/css">
.redactor_box {
	width: <?=NEWS_WIDTH?>px;
}
	</style>
	<title>新增新聞</title>
	<script type="text/javascript" src="scripts/jquery.js"></script>
	<script type="text/javascript" src="redactor/redactor.ewmib.js"></script>
	<script type="text/javascript" src="redactor/zh_tw.js"></script>
	<script type="text/javascript" src="scripts/check_date_format.js"></script>
	<script type="text/javascript">
<!--
var global_bool_processing = false;

$(function()
{
	$('#redactor').redactor({
		lang: 'zh_tw', 
		focus: true, 
		imageUpload: 'ajax_do_upload_image.php', 
		minHeight: <?=NEWS_HEIGHT?>
	});
});

function do_add_news()
{
	if( global_bool_processing )
		return false;

	if( document.add_news.date.value == '' )
	{
		alert('請輸入日期');
		return false;
	}

	if( document.add_news.subject.value == '' )
	{
		alert('請輸入新聞標題');
		return false;
	}

	if( document.add_news.author.value == '' )
	{
		alert('請輸入新聞作者');
		return false;
	}

	if( document.add_news.abstract.value == '' )
	{
		alert('請輸入新聞摘要');
		return false;
	}

	if( document.add_news.news_content.value == '' )
	{
		alert('請輸入新聞內容');
		return false;
	}

	if( !check_date_format(document.add_news.date.value) )
	{
		alert('請輸入正確的日期格式(yyyy-mm-dd)');
		return false;
	}

	var obj_form_data = new FormData(document.add_news);

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_add_news.php',
		type: 'POST',
		data: obj_form_data,
		cache: false,
		processData: false,
		contentType: false,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['RedirectURL'] != '' )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrCode'] == '1' )
				history.back();

			global_bool_processing = false;
		}
	});

	return false;
}
//-->
	</script>
</head>

<body<?=( isset($str_init_info) ? ' onLoad="javascript:alert(\'' . $str_init_info . '\');"' : '' )?>>
<? include('_title.php'); ?>
<? include('_menu.php'); ?>
<div id="content">
<h2>新增新聞</h2>
<form name="add_news">
<fieldset class="data">
<legend>新聞內容</legend>
<div class="field"><span class="subject">所屬分類</span><?
if( $arr_news_cates )
{
	echo('<ul>');

	foreach( $arr_news_cates as $arr_row )
		echo('<li><input type="checkbox" name="news_cate_id[]" value="' . $arr_row['NewsCateId'] . '" />' . $arr_row['NewsCateName'] . '</li>');

	echo('</ul>');
}
?></div>
<div class="field"><span class="subject">日期</span><input class="content" type="text" name="date" value="<?=date('Y-m-d')?>" maxlength="10" /></div>
<div class="field"><span class="subject">新聞標題</span><input class="content" type="text" name="subject" maxlength="255" /></div>
<div class="field"><span class="subject">新聞作者</span><input class="content" type="text" name="author" maxlength="255" /></div>
<div class="field"><span class="subject">新聞摘要</span><textarea class="content" name="abstract"></textarea></div>
<div class="field"><span class="subject">新聞內容</span><textarea class="content" id="redactor" name="news_content"></textarea></div>
</fieldset>
<div class="buttons"><input class="previous" type="button" value="回上一頁" onclick="history.back();" /><input class="add_file" id="button_submit" type="button" value="確定新增" onclick="do_add_news();" /></div>
</form>
</div>
<? include('_footer.php'); ?>
</body>

</html>