<?php /*SVN-1324(5.0.09)*/
include_once("../../includes/var.php");
include_once("../../libs/utils.php");
include_once("../../libs/functions.php");

header("Content-Type: text/html;charset=".CS_ENCODING);

$sid = (isset($_GET["sid"])) ? $_GET["sid"] : "";
$mode = (isset($_GET["mode"])) ? $_GET["mode"] : "";
$id = (isset($_GET["id"])) ? $_GET["id"] : "";
$act = (isset($_GET["act"])) ? $_GET["act"] : "";
$mutp = (isset($_GET["mutp"])) ? $_GET["mutp"] : "";
switch ($mutp) {
	case "R": $mdir = "manage"; break;
	case "B": $mdir = "modules"; break;
	default: echo "System path error!"; return; break;
}

$parary = createsidobject($sid);
if (empty($parary)) {
	echo "System session error!";
	return;
}
$dbo = $parary["dbo"];$ln = $parary["ln"];$user = $parary["user"];
include_once("../../classes/class.Locale_DB.php");
$lo = new Locale($ln, $dbo, 1, $mode);

$insl = get_cw_index_setup($sid, $mode, TYPE_FETCH_ARRAY);
if (empty($insl[0]["MODNAME"])) {
	$mask_data = array();$mask_file = "mask.ini";
	if (file_exists($mask_file)) $mask_data = parse_ini_file($mask_file, true);
	$insl[0]["SATYPE"] = $mask_data["SATYPE"];
	$insl[0]["SANEW"] = $mask_data["SANEW"];
	$insl[0]["SAEDIT"] = $mask_data["SAEDIT"];
	$insl[0]["SADEL"] = $mask_data["SADEL"];
	$insl[0]["SASCRT"] = $mask_data["SASCRT"];
	$insl[0]["MODNAME"] = $mask_data["MODNAME"];
	$insl[0]["PGNEW"] = $mask_data["PGNEW"];
	$insl[0]["PGDEL"] = $mask_data["PGDEL"];
	$insl[0]["PGSCRT"] = $mask_data["PGSCRT"];
	$insl[0]["PGPNT"] = $mask_data["PGPNT"];
	$insl[0]["FETYPE"] = $mask_data["FETYPE"];
}

// 5A Query
$fast_fs = get_five_a_set($dbo, $id, TYPE_FETCH_ASSOC);
$str_sort = get_five_a_sort_str($dbo, $id);

$data = array();$calendar = "";

switch ($act) {
	case "qryui":
		if (empty($_POST["SDCNAME"])) {
			$output["result"] = "false";
			$output["message"] = "No data can be qryui!";
			echo json_encode(array(0 => $output));
			return;
		}

		$sql = "select CD.SDCNAME, CD.MENUNAME, CD.QRYID, CD.QTABLE, CD.QFIELD, QD.QFNAME, QD.QEDITTYPE ".
			   "from WEBCLDCFGD as CD ".
			   "left join QUERYFILED as QD on QD.QUERYNAME = CD.QRYID and QD.SERNO = 1 ".
			   "where SDCNAME = '".$_POST["SDCNAME"]."' ".
			   "order by CD.SERNO; ";
		$res = $dbo->execute($sql);
		$error = trim($dbo->getMessage());
		if ($error) {
			//--------WEB_SYSTEM_LOG---------
			$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
				" N'".$_POST["SDCNAME"]."', ".
				" N'".$_SERVER["REMOTE_ADDR"]."', ".
				" N'".$user["EMPNO"]."', ".
				" N'".$mode."', ".
				" N'".$act."_error', ".
				" N'".sql_sim_str($sql, 3000)."', ".
				" N'".sql_sim_str($error, 200)."'; ";
			$log_res = $dbo->execute($sys_log_sql);
			$LOG = fetch_query_result($log_res, TYPE_FETCH_ARRAY);
			$output["result"] = "false";
			$output["message"] = $lo->text("WRITE_FAILED")."\n".$LOG[0]["LOGETABLE"]."\n(".$LOG[0]["LASTUPD"].")";
			echo json_encode(array(0 => $output));
			return;
			//--------WEB_SYSTEM_LOG end---------
		}
		$data = fetch_query_result($res, TYPE_FETCH_ARRAY);

		if (empty($data)) {
			$output["result"] = "false";
			$output["message"] = "No data can be qryui!";
			echo json_encode(array(0 => $output));
			return;
		} else {
			for ($i = 0; $i < count($data); $i++) {
				$data[$i]["FNAME"] = $lo->text($data[$i]["QTABLE"]."_".$data[$i]["QFIELD"]);
			}
		}

		//--------WEB_SYSTEM_LOG---------
		$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
			" N'".$_POST["SDCNAME"]."', ".
			" N'".$_SERVER["REMOTE_ADDR"]."', ".
			" N'".$user["EMPNO"]."', ".
			" N'".$mode."', ".
			" N'".$act."', ".
			" N'".sql_sim_str($sql, 3000)."', ".
			" N'' ";
		$log_res = $dbo->execute($sys_log_sql);
		//--------WEB_SYSTEM_LOG end---------

		$output["result"] = "true";
		$output["message"] = "";
		$output["data"] = $data;
		echo json_encode(array(0 => $output));

		return;
		break;

	case "cldvi":
		$sql = "exec dbo.PROC_CHANGE_VIEW_WEBCALENDAR '".$user["ERI"]."'; ";
		if (!preg_match("/WHERE/i", strtoupper($fast_fs["FROMSQL"][0]))) $query_where = "WHERE ".$fast_fs["KEYERI"][0]." <> ''";
		if (!empty($_POST["SDCNAME"])) $query_where .= " AND ".$fast_fs["KEYERI"][0]." = '".$_POST["SDCNAME"]."'";
		$sql .= "SELECT ".$fast_fs["KEYERI"][0].",".implode(",", $fast_fs["QFNAME"])." ".$fast_fs["FROMSQL"][0]." ".$query_where." ORDER BY ".$str_sort."; ";
		$res = $dbo->execute($sql);
		$error = trim($dbo->getMessage());
		if ($error) {
			//--------WEB_SYSTEM_LOG---------
			$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
				" N'".$_POST["SDCNAME"]."', ".
				" N'".$_SERVER["REMOTE_ADDR"]."', ".
				" N'".$user["EMPNO"]."', ".
				" N'".$mode."', ".
				" N'".$act."_error', ".
				" N'".sql_sim_str($sql, 3000)."', ".
				" N'".sql_sim_str($error, 200)."'; ";
			$log_res = $dbo->execute($sys_log_sql);
			$LOG = fetch_query_result($log_res, TYPE_FETCH_ARRAY);
			$output["result"] = "false";
			$output["message"] = $lo->text("WRITE_FAILED")."\n".$LOG[0]["LOGETABLE"]."\n(".$LOG[0]["LASTUPD"].")";
			echo json_encode(array(0 => $output));
			return;
			//--------WEB_SYSTEM_LOG end---------
		}
		$ret = fetch_query_result($res, TYPE_FETCH_ARRAY);

		$sql = "";
		if (isset($ret) && !empty($ret)) {
			for ($i = 0; $i < count($ret); $i++) {
				if (isset($ret[$i]["MTABLE"]) && trim($ret[$i]["MTABLE"]) != "") {
					$qrystr = "";
					if (!empty($_POST["SDCNAME"]) && $_POST["SDCNAME"] == $ret[$i]["SDCNAME"]) {
						$sql2 = "select CD.SDCNAME, CD.MENUNAME, CD.QRYID, CD.QTABLE, CD.QFIELD, QD.QFNAME, QD.QEDITTYPE ".
								"from WEBCLDCFGD as CD ".
								"left join QUERYFILED as QD on QD.QUERYNAME = CD.QRYID and QD.SERNO = 1 ".
								"where SDCNAME = '".$ret[$i]["SDCNAME"]."' ".
								"order by CD.SERNO; ";
						$res = $dbo->execute($sql2);
						$error = trim($dbo->getMessage());
						if ($error) {
							//--------WEB_SYSTEM_LOG---------
							$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
								" N'".$_POST["SDCNAME"]."', ".
								" N'".$_SERVER["REMOTE_ADDR"]."', ".
								" N'".$user["EMPNO"]."', ".
								" N'".$mode."', ".
								" N'".$act."_error', ".
								" N'".sql_sim_str($sql2, 3000)."', ".
								" N'".sql_sim_str($error, 200)."'; ";
							$log_res = $dbo->execute($sys_log_sql);
							$LOG = fetch_query_result($log_res, TYPE_FETCH_ARRAY);
							$output["result"] = "false";
							$output["message"] = $lo->text("WRITE_FAILED")."\n".$LOG[0]["LOGETABLE"]."\n(".$LOG[0]["LASTUPD"].")";
							echo json_encode(array(0 => $output));
							return;
							//--------WEB_SYSTEM_LOG end---------
						}
						$qryset = fetch_query_result($res, TYPE_FETCH_ARRAY);
						if (!empty($qryset)) {
							for ($j = 0; $j < count($qryset); $j++) {
								$text = $qryset[$j]["QTABLE"]."_".$qryset[$j]["QFIELD"];
								$field = $qryset[$j]["QTABLE"].".".$qryset[$j]["QFIELD"];
								$qtype = (isset($_POST["T_".$text])) ? $_POST["T_".$text] : "EDNORMAL";
								$fdflg = (isset($_POST["F_".$text])) ? $_POST["F_".$text] : 0;
								if ($fdflg == "0") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." >= ".(((is_numeric($_POST["B_".$text])) && $qtype == "EDFLOAT") ? $_POST["B_".$text] : "'".$_POST["B_".$text]."'");
									}
									if (isset($_POST["E_".$text]) && trim($_POST["E_".$text]) != "") {
										$qrystr .= " AND ".$field." <= ".(((is_numeric($_POST["E_".$text])) && $qtype == "EDFLOAT") ? $_POST["E_".$text] : "'".$_POST["E_".$text]."'");
									}
								} else if ($fdflg == "1") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." LIKE '%".$_POST["B_".$text]."%'";
									}
								} else if ($fdflg == "2") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." LIKE '".$_POST["B_".$text]."%'";
									}
								} else if ($fdflg == "3") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." LIKE '%".$_POST["B_".$text]."'";
									}
								} else if ($fdflg == "4") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." = ".(((is_numeric($_POST["B_".$text])) && $qtype == "EDFLOAT") ? $_POST["B_".$text] : "'".$_POST["B_".$text]."'");
									}
								} else if ($fdflg == "5") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." <> ".(((is_numeric($_POST["B_".$text])) && $qtype == "EDFLOAT") ? $_POST["B_".$text] : "'".$_POST["B_".$text]."'");
									}
								} else if ($fdflg == "6") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." < ".(((is_numeric($_POST["B_".$text])) && $qtype == "EDFLOAT") ? $_POST["B_".$text] : "'".$_POST["B_".$text]."'");
									}
								} else if ($fdflg == "7") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." <= ".(((is_numeric($_POST["B_".$text])) && $qtype == "EDFLOAT") ? $_POST["B_".$text] : "'".$_POST["B_".$text]."'");
									}
								} else if ($fdflg == "8") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." > ".(((is_numeric($_POST["B_".$text])) && $qtype == "EDFLOAT") ? $_POST["B_".$text] : "'".$_POST["B_".$text]."'");
									}
								} else if ($fdflg == "9") {
									if (isset($_POST["B_".$text]) && trim($_POST["B_".$text]) != "") {
										$qrystr .= " AND ".$field." >= ".(((is_numeric($_POST["B_".$text])) && $qtype == "EDFLOAT") ? $_POST["B_".$text] : "'".$_POST["B_".$text]."'");
									}
								}
							}
						}
					}
					if (trim($ret[$i]["WEFUNC"]) <> "") {
						$sql2 = "exec dbo.".trim($ret[$i]["WEFUNC"])." '".$ret[$i]["SDCNAME"]."','".$user["ERI"]."'; ";
						$res = $dbo->execute($sql2);
						$error = trim($dbo->getMessage());
						if ($error) {
							//--------WEB_SYSTEM_LOG---------
							$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
								" N'".$_POST["SDCNAME"]."', ".
								" N'".$_SERVER["REMOTE_ADDR"]."', ".
								" N'".$user["EMPNO"]."', ".
								" N'".$mode."', ".
								" N'".$act."_error', ".
								" N'".sql_sim_str($sql2, 3000)."', ".
								" N'".sql_sim_str($error, 200)."'; ";
							$log_res = $dbo->execute($sys_log_sql);
							$LOG = fetch_query_result($log_res, TYPE_FETCH_ARRAY);
							$output["result"] = "false";
							$output["message"] = $lo->text("WRITE_FAILED")."\n".$LOG[0]["LOGETABLE"]."\n(".$LOG[0]["LASTUPD"].")";
							echo json_encode(array(0 => $output));
							return;
							//--------WEB_SYSTEM_LOG end---------
						}
						$restr = fetch_query_result($res, TYPE_FETCH_ARRAY);
						if (isset($restr[0]["SQLSTR"]) && trim($restr[0]["SQLSTR"]) <> "") {
							if (!empty($sql)) {
								$sql .= " UNION ";
							}
							$sql .= $restr[0]["SQLSTR"].$qrystr;
						}
					} else {
						if (!empty($sql)) {
							$sql .= " UNION ";
						}
						$csql = "SELECT ISNULL((SELECT TOP(1) (CASE ISNULL(DATA_TYPE,'') ".
																  "WHEN 'datetime' THEN 'DATETIME' ".
																  "ELSE 'NVARCHAR' ".
															  "END) AS CBFDTP ".
											   "FROM INFORMATION_SCHEMA.COLUMNS ".
											   "WHERE TABLE_NAME = '".$ret[$i]["MTABLE"]."' ".
												 "AND COLUMN_NAME = '".$ret[$i]["BTIMEFD"]."'),'') AS BTYPE,".
									   "ISNULL((SELECT TOP(1) (CASE ISNULL(DATA_TYPE,'') ".
																  "WHEN 'datetime' THEN 'DATETIME' ".
																  "ELSE 'NVARCHAR' ".
															  "END) AS CBFDTP ".
											   "FROM INFORMATION_SCHEMA.COLUMNS ".
											   "WHERE TABLE_NAME = '".$ret[$i]["MTABLE"]."' ".
												 "AND COLUMN_NAME = '".$ret[$i]["ETIMEFD"]."'),'') AS ETYPE;";
						$res = $dbo->execute($csql);
						$error = trim($dbo->getMessage());
						if ($error) {
							//--------WEB_SYSTEM_LOG---------
							$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
								" N'".$_POST["SDCNAME"]."', ".
								" N'".$_SERVER["REMOTE_ADDR"]."', ".
								" N'".$user["EMPNO"]."', ".
								" N'".$mode."', ".
								" N'".$act."_error', ".
								" N'".sql_sim_str($csql, 3000)."', ".
								" N'".sql_sim_str($error, 200)."'; ";
							$log_res = $dbo->execute($sys_log_sql);
							$LOG = fetch_query_result($log_res, TYPE_FETCH_ARRAY);
							$output["result"] = "false";
							$output["message"] = $lo->text("WRITE_FAILED")."\n".$LOG[0]["LOGETABLE"]."\n(".$LOG[0]["LASTUPD"].")";
							echo json_encode(array(0 => $output));
							return;
							//--------WEB_SYSTEM_LOG end---------
						}
						$tpary = fetch_query_result($res, TYPE_FETCH_ARRAY);
						if (empty($tpary[0]["BTYPE"])) $btime = "''";
						else {
							if ($tpary[0]["BTYPE"] == "DATETIME") $btime = "CONVERT(NVARCHAR(5),".$ret[$i]["BTIMEFD"].",108)";
							else $btime = "ISNULL(".$ret[$i]["BTIMEFD"].",'')";
						}
						if (empty($tpary[0]["ETYPE"])) $etime = "''";
						else {
							if ($tpary[0]["ETYPE"] == "DATETIME") $etime = "CONVERT(NVARCHAR(5),".$ret[$i]["ETIMEFD"].",108)";
							else $etime = "ISNULL(".$ret[$i]["ETIMEFD"].",'')";
						}
						$sql .= "SELECT ERI, ".
								"		'".$ret[$i]["SDCNAME"]."' AS SDCNAME, ".
								"		CONVERT(NVARCHAR(50), ".str_replace("|", "+", ((isset($ret[$i]["TITLEFDS"])) ? $ret[$i]["TITLEFDS"] : "''")).") AS TITLE, ".
								"		CONVERT(NVARCHAR(10), ".((isset($ret[$i]["BDATEFD"]) && trim($ret[$i]["BDATEFD"]) != "") ? $ret[$i]["BDATEFD"] : "''").", 121) AS BDATE, ".
								"		CONVERT(NVARCHAR(10), ".((isset($ret[$i]["EDATEFD"]) && trim($ret[$i]["EDATEFD"]) != "") ? $ret[$i]["EDATEFD"] : "''").", 121) AS EDATE, ".
								"		".$btime." AS BTIME, ".$etime." AS ETIME, ".
								"		'".((isset($ret[$i]["SHCODE"])) ? trim($ret[$i]["SHCODE"]) : "")."' AS SHCODE, ".
								"		'".((isset($ret[$i]["BCOLOR"])) ? trim($ret[$i]["BCOLOR"]) : "")."' AS BCOLOR, ".
								"		'".((isset($ret[$i]["FCOLOR"])) ? trim($ret[$i]["FCOLOR"]) : "")."' AS FCOLOR ".
								"FROM ".$ret[$i]["MTABLE"]." ".
								"WHERE ERI <> '' AND (ERI = TOPERI OR TOPERI IS NULL)";
						$sql .= $qrystr;
					}
				}
			}
		}

		if (!empty($sql)) {
			$res = $dbo->execute($sql);
			$error = trim($dbo->getMessage());
			if ($error) {
				//--------WEB_SYSTEM_LOG---------
				$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
					" N'".$_POST["SDCNAME"]."', ".
					" N'".$_SERVER["REMOTE_ADDR"]."', ".
					" N'".$user["EMPNO"]."', ".
					" N'".$mode."', ".
					" N'".$act."_error', ".
					" N'".sql_sim_str($sql, 3000)."', ".
					" N'".sql_sim_str($error, 200)."'; ";
				$log_res = $dbo->execute($sys_log_sql);
				$LOG = fetch_query_result($log_res, TYPE_FETCH_ARRAY);
				$output["result"] = "false";
				$output["message"] = $lo->text("WRITE_FAILED")."\n".$LOG[0]["LOGETABLE"]."\n(".$LOG[0]["LASTUPD"].")";
				echo json_encode(array(0 => $output));
				return;
				//--------WEB_SYSTEM_LOG end---------
			}
			$red = fetch_query_result($res, TYPE_FETCH_ARRAY);
			if (isset($red) && !empty($red)) {
				$c = 0;
				for ($i = 0; $i < count($red); $i++) {
					if ($red[$i]["SDCNAME"] == "" || $red[$i]["TITLE"] == "" || !isset($red[$i]["TITLE"]) || $red[$i]["BDATE"] == "" ||
						$red[$i]["EDATE"] == "" || $red[$i]["BCOLOR"] == "" || $red[$i]["FCOLOR"] == "") continue;
					if ($red[$i]["BDATE"].$red[$i]["BTIME"] == $red[$i]["EDATE"].$red[$i]["ETIME"] || $red[$i]["BTIME"] == "" || $red[$i]["ETIME"] == "") {
						$start_time = $red[$i]["BDATE"];
						$end_time = $red[$i]["EDATE"];
					} else {
						$start_time = $red[$i]["BDATE"]."T".$red[$i]["BTIME"];
						$end_time = $red[$i]["EDATE"]."T".$red[$i]["ETIME"];
					}

					// $data[$i]["id"] = $red[$i]["CODE"];
					$data[$c]["title"] = $red[$i]["TITLE"];
					$data[$c]["start"] = $start_time;
					$data[$c]["end"] = $end_time;
					$data[$c]["eri"] = $red[$i]["ERI"];
					$data[$c]["sdc"] = $red[$i]["SDCNAME"];
					$data[$c]["color"] = "#".$red[$i]["BCOLOR"]; // 事件的框的顏色
					$data[$c]["textColor"] = "#".$red[$i]["FCOLOR"]; // 事件的字的顏色
					$c++;
				}
			}
		}
		//echo json_encode($data);
		$output["result"] = "true";
		$output["message"] = "";
		$output["data"] = (empty($data)) ? NULL : $data;
		echo json_encode(array(0 => $output));

		return;
		break;

	case "view":
		// Default action, no break;
	default:
		$sql = "exec dbo.PROC_CHANGE_VIEW_WEBCALENDAR '".$user["ERI"]."'; ";
		if (!preg_match("/WHERE/i", strtoupper($fast_fs["FROMSQL"][0]))) $query_where = "WHERE ".$fast_fs["KEYERI"][0]." <> ''";
		$sql .= "SELECT ".$fast_fs["KEYERI"][0].",".implode(",", $fast_fs["QFNAME"])." ".$fast_fs["FROMSQL"][0]." ".$query_where." ORDER BY ".$str_sort."; ";
		$res = $dbo->execute($sql);
		$error = trim($dbo->getMessage());
		if ($error) {
			//--------WEB_SYSTEM_LOG---------
			$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
				" N'".$id."', ".
				" N'".$_SERVER["REMOTE_ADDR"]."', ".
				" N'".$user["EMPNO"]."', ".
				" N'".$mode."', ".
				" N'error', ".
				" N'".sql_sim_str($sql, 3000)."', ".
				" N'".sql_sim_str($error, 200)."'; ";
			$log_res = $dbo->execute($sys_log_sql);
			//--------WEB_SYSTEM_LOG end---------
			echo "SQL: ".$sql."<br>".$error."<br>";
		}
		$ret = fetch_query_result($res, TYPE_FETCH_ARRAY);
		$sql = "";
		if (isset($ret) && !empty($ret)) {
			for ($i = 0; $i < count($ret); $i++) {
				if (isset($ret[$i]["MTABLE"]) && trim($ret[$i]["MTABLE"]) != "") {
					if (trim($ret[$i]["WEFUNC"]) <> "") {
						$sql2 = "exec dbo.".trim($ret[$i]["WEFUNC"])." '".$ret[$i]["SDCNAME"]."','".$user["ERI"]."'; ";
						$res = $dbo->execute($sql2);
						$error = trim($dbo->getMessage());
						if ($error) {
							//--------WEB_SYSTEM_LOG---------
							$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
								" N'".$id."', ".
								" N'".$_SERVER["REMOTE_ADDR"]."', ".
								" N'".$user["EMPNO"]."', ".
								" N'".$mode."', ".
								" N'error', ".
								" N'".sql_sim_str($sql2, 3000)."', ".
								" N'".sql_sim_str($error, 200)."'; ";
							$log_res = $dbo->execute($sys_log_sql);
							//--------WEB_SYSTEM_LOG end---------
							echo "SQL: ".$sql2."<br>".$error."<br>";
						}
						$restr = fetch_query_result($res, TYPE_FETCH_ARRAY);
						if (isset($restr[0]["SQLSTR"]) && trim($restr[0]["SQLSTR"]) <> "") {
							if (!empty($sql)) {
								$sql .= " UNION ";
							}
							$sql .= $restr[0]["SQLSTR"];
						}
					} else {
						if (!empty($sql)) {
							$sql .= " UNION ";
						}
						$csql = "SELECT ISNULL((SELECT TOP(1) (CASE ISNULL(DATA_TYPE,'') ".
																  "WHEN 'datetime' THEN 'DATETIME' ".
																  "ELSE 'NVARCHAR' ".
															  "END) AS CBFDTP ".
											   "FROM INFORMATION_SCHEMA.COLUMNS ".
											   "WHERE TABLE_NAME = '".$ret[$i]["MTABLE"]."' ".
												 "AND COLUMN_NAME = '".$ret[$i]["BTIMEFD"]."'),'') AS BTYPE,".
									   "ISNULL((SELECT TOP(1) (CASE ISNULL(DATA_TYPE,'') ".
																  "WHEN 'datetime' THEN 'DATETIME' ".
																  "ELSE 'NVARCHAR' ".
															  "END) AS CBFDTP ".
											   "FROM INFORMATION_SCHEMA.COLUMNS ".
											   "WHERE TABLE_NAME = '".$ret[$i]["MTABLE"]."' ".
												 "AND COLUMN_NAME = '".$ret[$i]["ETIMEFD"]."'),'') AS ETYPE;";
						$res = $dbo->execute($csql);
						$error = trim($dbo->getMessage());
						if ($error) {
							//--------WEB_SYSTEM_LOG---------
							$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
								" N'".$id."', ".
								" N'".$_SERVER["REMOTE_ADDR"]."', ".
								" N'".$user["EMPNO"]."', ".
								" N'".$mode."', ".
								" N'error', ".
								" N'".sql_sim_str($csql, 3000)."', ".
								" N'".sql_sim_str($error, 200)."'; ";
							$log_res = $dbo->execute($sys_log_sql);
							//--------WEB_SYSTEM_LOG end---------
							echo "SQL: ".$csql."<br>".$error."<br>";
						}
						$tpary = fetch_query_result($res, TYPE_FETCH_ARRAY);
						if (empty($tpary[0]["BTYPE"])) $btime = "''";
						else {
							if ($tpary[0]["BTYPE"] == "DATETIME") $btime = "CONVERT(NVARCHAR(5),".$ret[$i]["BTIMEFD"].",108)";
							else $btime = "ISNULL(".$ret[$i]["BTIMEFD"].",'')";
						}
						if (empty($tpary[0]["ETYPE"])) $etime = "''";
						else {
							if ($tpary[0]["ETYPE"] == "DATETIME") $etime = "CONVERT(NVARCHAR(5),".$ret[$i]["ETIMEFD"].",108)";
							else $etime = "ISNULL(".$ret[$i]["ETIMEFD"].",'')";
						}
						$sql .= "SELECT ERI, ".
								"		'".$ret[$i]["SDCNAME"]."' AS SDCNAME, ".
								"		CONVERT(NVARCHAR(50), ".str_replace("|", "+", ((isset($ret[$i]["TITLEFDS"])) ? $ret[$i]["TITLEFDS"] : "''")).") AS TITLE, ".
								"		CONVERT(NVARCHAR(10), ".((isset($ret[$i]["BDATEFD"]) && trim($ret[$i]["BDATEFD"]) != "") ? $ret[$i]["BDATEFD"] : "''").", 121) AS BDATE, ".
								"		CONVERT(NVARCHAR(10), ".((isset($ret[$i]["EDATEFD"]) && trim($ret[$i]["EDATEFD"]) != "") ? $ret[$i]["EDATEFD"] : "''").", 121) AS EDATE, ".
								"		".$btime." AS BTIME, ".$etime." AS ETIME, ".
								"		'".((isset($ret[$i]["SHCODE"])) ? trim($ret[$i]["SHCODE"]) : "")."' AS SHCODE, ".
								"		'".((isset($ret[$i]["BCOLOR"])) ? trim($ret[$i]["BCOLOR"]) : "")."' AS BCOLOR, ".
								"		'".((isset($ret[$i]["FCOLOR"])) ? trim($ret[$i]["FCOLOR"]) : "")."' AS FCOLOR ".
								"FROM ".$ret[$i]["MTABLE"]." ".
								"WHERE ERI <> '' AND (ERI = TOPERI OR TOPERI IS NULL)";
					}
				}
			}
		}
		if (!empty($sql)) {
			$res = $dbo->execute($sql);
			$error = trim($dbo->getMessage());
			if ($error) {
				//--------WEB_SYSTEM_LOG---------
				$sys_log_sql ="EXEC PROC_WEB_SYSTEM_LOG ".
					" N'".$id."', ".
					" N'".$_SERVER["REMOTE_ADDR"]."', ".
					" N'".$user["EMPNO"]."', ".
					" N'".$mode."', ".
					" N'error', ".
					" N'".sql_sim_str($sql, 3000)."', ".
					" N'".sql_sim_str($error, 200)."'; ";
				$log_res = $dbo->execute($sys_log_sql);
				//--------WEB_SYSTEM_LOG end---------
				echo "SQL: ".$sql."<br>".$error."<br>";
			}
			$red = fetch_query_result($res, TYPE_FETCH_ARRAY);
			if (isset($red) && !empty($red)) {
				$c = 0;
				for ($i = 0; $i < count($red); $i++) {
					if ($red[$i]["SDCNAME"] == "" || $red[$i]["TITLE"] == "" || !isset($red[$i]["TITLE"]) || $red[$i]["BDATE"] == "" ||
						$red[$i]["EDATE"] == "" || $red[$i]["BCOLOR"] == "" || $red[$i]["FCOLOR"] == "") continue;
					if ($red[$i]["BDATE"].$red[$i]["BTIME"] == $red[$i]["EDATE"].$red[$i]["ETIME"] || $red[$i]["BTIME"] == "" || $red[$i]["ETIME"] == "") {
						$start_time = $red[$i]["BDATE"];
						$end_time = $red[$i]["EDATE"];
					} else {
						$start_time = $red[$i]["BDATE"]."T".$red[$i]["BTIME"];
						$end_time = $red[$i]["EDATE"]."T".$red[$i]["ETIME"];
					}

					// $data[$i]["id"] = $red[$i]["CODE"];
					$data[$c]["title"] = cs_convert_encode($red[$i]["TITLE"], "utf-8", "big5");
					$data[$c]["start"] = $start_time;
					$data[$c]["end"] = $end_time;
					$data[$c]["eri"] = $red[$i]["ERI"];
					$data[$c]["sdc"] = $red[$i]["SDCNAME"];
					$data[$c]["color"] = "#".$red[$i]["BCOLOR"]; // 事件的框的顏色
					$data[$c]["textColor"] = "#".$red[$i]["FCOLOR"]; // 事件的字的顏色
					$c++;
				}
			}
		}
		$calendar = json_encode($data);
		break;
}

include_once("../../classes/general/class.CSTemplate.php");
$cst = new CSTemplate();

$page = "detail.html";
$cst->set("SID", $sid);
$cst->set("mode", $mode);
$cst->set("user", $user);
$cst->set("locale_obj", $lo);
$cst->set("id", $id);
$cst->set("mutp", $mutp);
$cst->set("mdir", $mdir);
$cst->set("calendar", $calendar);
$cst->set("insl", $insl[0]);
$cst->show($page);
?>