<?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_level.php');

$arr_news_cates = $obj_class_admin->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="admin.css" />
	<title>新聞類別管理</title>
	<script type="text/javascript" src="scripts/jquery.js"></script>
	<script type="text/javascript">
<!--
var global_bool_processing = false;

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

	if( document.add_news_cate.news_cate_name.value == '' )
	{
		alert('請輸入類別名稱');
		return false;
	}

	if( document.add_news_cate.order.value == '' )
	{
		alert('請輸入優先順序');
		return false;
	}

	if( document.add_news_cate.order.value < 0 || document.add_news_cate.order.value > 255 )
	{
		alert('優先順序必須在0至255之間');
		return false;
	}

	var obj_form_data = new FormData(document.add_news_cate);

	global_bool_processing = true;

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

			if( jdata['ErrCode'] == '1' )
				location.reload();

			global_bool_processing = false;
		}
	});

	return false;
}

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

	var regexp_element_name = /^order_(\d+)$/;

	for( var i=0; i<document.update_news_cates_orders.length; i++ )
	{
		if( document.update_news_cates_orders.elements[i].type == 'text' && ( arr_matches = regexp_element_name.exec(document.update_news_cates_orders.elements[i].name) ) )
		{
			if( document.update_news_cates_orders.elements[i].value == '' )
			{
				alert('請輸入類別編號[' + arr_matches[1] + ']之顯示順序');
				return false;
			}
			else if( isNaN(document.update_news_cates_orders.elements[i].value) || document.update_news_cates_orders.elements[i].value < 0 || document.update_news_cates_orders.elements[i].value > 255 )
			{
				alert('類別編號[' + arr_matches[1] + ']之顯示順序必須在0-255之間');
				return false;
			}
		}
	}

	var obj_form_data = new FormData(document.update_news_cates_orders);

	global_bool_processing = true;

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

			if( jdata['ErrCode'] == '1' )
				location.reload();

			global_bool_processing = false;
		}
	});

	return false;
}

function edit_news_cate(static_uint_news_cate_id)
{
	if( !global_bool_processing )
		window.location = 'edit_news_cate.php?news_cate_id=' + static_uint_news_cate_id;

	return false;
}

function do_delete_news_cate(static_uint_news_cate_id, static_str_news_cate_name)
{
	if( global_bool_processing )
		return false;

	if( !confirm('確定要刪除類別［' + static_str_news_cate_name + '］？') )
		return false;

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_delete_news_cate.php',
		type: 'POST',
		data: 'news_cate_id=' + static_uint_news_cate_id,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ErrCode'] == '1' )
				location.reload();

			global_bool_processing = false;
		}
	});

	return true;
}
//-->
	</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_cate">
<fieldset class="search">
<legend>類別管理</legend>
新增新聞類別：名稱：<input class="search_content" type="text" name="news_cate_name" maxlength="255" /> 優先順序：<input class="search_order" type="text" name="order" value="0" maxlength="3" /><input class="button" type="button" value="確定新增類別" onclick="do_add_news_cate();" /><input class="button" type="button" value="更新優先順序" onclick="do_update_news_cates_orders();" />
</fieldset>
</form>
<?
if( !$arr_news_cates )
	echo("<p>目前並無任何資料</p>\n");
else
{
?>
<form name="update_news_cates_orders">
<table class="data_list"><tbody>
<tr><th>類別名稱</th><th>優先順序</th><th>管理</th></tr>
<?
	foreach( $arr_news_cates as $arr_row )
	{
		echo('<tr><td>' . $arr_row['NewsCateName'] . '</td><td class="order"><input type="text" name="order_' . $arr_row['NewsCateId'] . '" value="' . $arr_row['Order'] . '" size="4" maxlength="3" /></td><td class="function"><input type="button" name="button_submit" value="編輯" onClick="edit_news_cate(' . $arr_row['NewsCateId'] . ');" />&nbsp;<input type="button" name="button_submit" value="刪除" onClick="javascript:do_delete_news_cate(' . $arr_row['NewsCateId'] . ', \'' . $arr_row['NewsCateName'] . '\');" /></td></tr>' . "\n");
	}
?>
</table>
</form>
<?
}
?>
</div>
<? include('_footer.php'); ?>
</body>

</html>