<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <!-- SECTION 1 -->
    <!--style type="text/css">
        @import "/dojo-release-1.1.0/dijit/themes/tundra/tundra.css";
        @import "/dojo-release-1.1.0/dojo/resources/dojo.css";
    </style-->
    <style type="text/css">
        @import "http://ajax.googleapis.com/ajax/libs/dojo/1.1/dijit/themes/tundra/tundra.css";
        @import "http://ajax.googleapis.com/ajax/libs/dojo/1.1/dojo/resources/dojo.css";
    </style>
    <!-- SECTION 2 -->

<title>Event Calendar</title>
<script type="text/javascript">
var djConfig = {
	isDebug: false
};
</script>
<script type="text/javascript" src="/dojo/dojo.js"></script>
<script type="text/javascript" src="/site_media/js/date.js"></script>
<script type="text/javascript">
dojo.require("mywidgets.widget.Calendar");
dojo.require("mywidgets.widget.Timezones");
dojo.addOnLoad(init);
var oCalendar;

function init(){
	oCalendar = dojo.widget.byId("dojoCalendar");
	oCalendar.setTimeZones(mywidgets.widget.timezones);
	oCalendar.selectedtimezone = dojo.io.cookie.getObjectCookie("DCTZ");
	oCalendar.onSetTimeZone = widgetTimeZoneChanged;
	oCalendar.changeEventTimes = true;
	oCalendar.onEventChanged = widgetEventChanged;
//	oCalendar.setAbleToCreateNew(true);
//	oCalendar.onNewEntry = widgetNewEntry;
	oCalendar.onValueChanged = widgetValueChanged;
	widgetValueChanged(new Date());
}

function loadFollowup(type, data, evt)
{
	dojo.require("dojo.date.serialize");
	var entries = {};
	var l = data.length;
	for (var i=0; i<l; i++)
	{
		var starttime = Date.parse(data[i].fields.next_followup);
		var duration = Date.parse("1:00");
		var hh = duration.getHours();
		var mm = duration.getMinutes();

		var entry = {
				id: data[i].pk,
				starttime: dojo.date.toRfc3339(starttime),
				endtime: dojo.date.toRfc3339(starttime.addHours(hh).addMinutes(mm)),
				allday: true,
				repeated: false,
				title: data[i].fields.name,
				url: "/patient/detail/"+data[i].pk,
				body: data[i].fields.medical_records,
				attributes: {
					Location: "",
					Categories: ""
				},
				type: ["", ""]
			};
		entries['pk'+data[i].pk] = entry;
	}
	oCalendar.setCalendarEntries(entries);
}

function errorFollowup(type, data, evt)
{
	alert("dojo.io.bind error");
}

function widgetValueChanged(dateObj){
	dojo.io.queueBind({
		url: "/get/followup/",
		method: "get",
		mimetype: "text/json",
		preventCache: new Date(),
		content: {
			date: dateObj
		},
		encoding: 'utf-8',
		load: loadFollowup,
		error: errorFollowup
	});
}

function loadEventChanged(type, data, evt)
{
	alert(data);
}

function widgetEventChanged(eventId,eventObject){
	var sReturn = "id " + eventId + "=\n";
	for(var i in eventObject){
		if(typeof(eventObject[i]) != "object"){
			sReturn += i + " = " + eventObject[i] + "\n";
		}else{
			oChildObject = eventObject[i];
			var sChildReturn = "";
			var iNum = 0;
			for(var j in oChildObject){
				if(iNum > 0){
					sChildReturn += ", ";
				}
				sChildReturn += j + ": " + oChildObject[j];
				iNum++;
			}
			sReturn += i + " = " + sChildReturn + "\n";
		}
	}
//	alert(sReturn);
	//Call script to update back-end db
    {% if user.is_authenticated %}
	dojo.io.queueBind({
		url: "/update/followup/",
		method: "post",
		mimetype: "text/json",
		useCache: false,
		content: {
			id: eventObject.id,
			starttime: eventObject.starttime,
			endtime: eventObject.endtime
		},
		encoding: 'utf-8'
//		load: loadEventChanged
//		error: errorFollowup
	});
    {% else %}
    alert("請先登入");
    {% endif %}

	oCalendar.refreshScreen();
}

function widgetNewEntry(eventObject){
	var sReturn = "";
	for(var i in eventObject){
		if(typeof(eventObject[i]) != "object"){
			sReturn += i + " = " + eventObject[i] + "\n";
		}else{
			oChildObject = eventObject[i];
			var sChildReturn = "";
			var iNum = 0;
			for(var j in oChildObject){
				if(iNum > 0){
					sChildReturn += ", ";
				}
				sChildReturn += j + ": " + oChildObject[j];
				iNum++;
			}
			sReturn += i + " = " + sChildReturn + "\n";
		}
	}
	alert(sReturn);
	//Call script to add to back-end db
	oCalendar.refreshScreen();
}

function widgetTimeZoneChanged(){
	//Setting cookie
	if(oCalendar.selectedtimezone == ""){
		dojo.io.cookie.deleteCookie("DCTZ");
	}else{
		dojo.io.cookie.setObjectCookie("DCTZ",oCalendar.selectedtimezone,3650);
	}
}

function setLocale(sLocale){
	oCalendar.lang = sLocale;
	oCalendar._preInitUI(new Date(oCalendar.value));
}
</script>
<link rel="stylesheet" type="text/css" href="/site_media/css/arrow_bullet_list_menu.css" />
<link rel="stylesheet" type="text/css" href="/site_media/css/css-liquid-layout-21-fixed-fluid.css" />
</head>
<body>
<div id="maincontainer">

<div id="topsection">

<img src="/site_media/css/media/logo.gif" alt="NTUH CyberKnife" height="70" width="100%"/>

<div class="innertube"><div align="right">
    {% if user.is_authenticated %}
    {{ user.username }} <a href=/accounts/logout/>登出</a>
    {% else %}
    <a href=/accounts/login/>登入</a>
    {% endif %}
</div>

</div></div>

<div id="contentwrapper">
<div id="contentcolumn" style="height:400px; background-color:#cccccc;">
<!--div class="innertube"><b>Content Column: <em>Fluid</em></b> <script type="text/javascript">filltext(45)</script></div-->
	<div id="dojoCalendar" dojoType="mywidgets:calendar"></div>
</div>
</div>

<div id="leftcolumn">
<!--div class="innertube"><b>Left Column: <em>20%</em></b> <script type="text/javascript">filltext(20)</script></div-->

<div class="arrowlistmenu">
<h3 class="headerbar">NTUHCK</h3>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/admin/" target="_blank">管理</a></li>
<li><a href="ftp://ADM:chu0230@intra.mc.ntu.edu.tw/ADM/kf/9697orthtb.xls" target="_blank">事前審查案件</a></li>
</ul>
<h3 class="headerbar">Event</h3>
<ul>
<li><a href="/calendar/">Calendar</a></li>
<li><a href="/calendar/followup/">Followup Calendar</a></li>
<li><a href="/record/weekly/" target="_blank">每週病人數紀錄</a></li>
<li><a href="/timetable/scheduling/" target="_blank">排程時間表</a></li>
</ul>
<h3 class="headerbar">Patient</h3>
<ul>
<li><a href="/patient/list/">List</a></li>
<li><a href="/patient/search/">Search</a></li>
</ul>
<h3 class="headerbar">Treatment</h3>
<ul>
<li><a href="/treatment/finished/">Finished</a></li>
<li><a href="/treatment/unfinished/">Unfinished</a></li>
</ul>
</div>

</div>

<div id="footer">
<script type="text/javascript"><!--
google_ad_client = "pub-0455212153132729";
/* 728x90, 已建立 2008/2/13 */
google_ad_slot = "2397228825";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

</div>

</body>
</html>
