<?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/admin.php');
$obj_class_admin = new class_admin();

$arr_news_cates = $obj_class_admin->get_news_cates();

$uint_image_width = NEWS_BANNER_WIDTH;
$uint_image_height = NEWS_BANNER_HEIGHT;
?>
<!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_banner()
{
	if( global_bool_processing )
		return false;

	if( document.add_banner.news_cate_id.value < '0' )
	{
		alert('請選擇新聞分類');
		return false;
	}

	if( document.add_banner.img_file.value == '' )
	{
		alert('請選擇廣告圖檔');
		return false;
	}

	if( document.add_banner.slogan.value == '' )
	{
		alert('請輸入廣告標題');
		return false;
	}

	var obj_form_data = new FormData(document.add_banner);

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_add_banner.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;
}

function slogan_img(obj_input_file_this)
{
	if( ( obj_img = document.getElementById('img_file_sample') ) != null )
	{
		if( obj_input_file_this.files != null )
			obj_img.src = window.URL.createObjectURL(obj_input_file_this.files[0]);
		else
			obj_img.src = obj_input_file_this.value;
	}
}
//-->
	</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_banner">
<fieldset class="data">
<legend>廣告內容</legend>
<div class="field"><span class="subject">新聞分類</span><select class="content" name="news_cate_id"><option value="-1">請選擇↓</option><?
if( $arr_news_cates )
{
	foreach( $arr_news_cates as $arr_row )
		echo('<option value="' . $arr_row['NewsCateId'] . '">' . $arr_row['NewsCateName'] . '</option>');
}
?></select></div>
<div class="field"><span class="subject">廣告圖檔（<?=NEWS_BANNER_WIDTH?>x<?=NEWS_BANNER_HEIGHT?>像素JPG圖形檔）</span>
<input class="content" type="file" name="img_file" onchange="javascript:slogan_img(this);" />
<img class="content" id="img_file_sample" src="../images/banners/no_image_300x250.png" width="<?=$uint_image_width?>" height="<?=$uint_image_height?>" /></div>
<div class="field"><span class="subject">廣告標題</span><input class="content" type="text" name="slogan" maxlength="255" /></div>
<div class="field"><span class="subject">廣告連結</span><input class="content" type="text" name="url" maxlength="255" /></div>
<div class="field"><span class="subject">廣告狀態</span><select class="content" name="active"><option value="0">隱藏</option><option value="1" selected>顯示</option></select></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_banner();" /></div>
</form>
</div>
<? include('_footer.php'); ?>
</body>

</html>