footer = '<center>';
footer +="<table width=99% cellpadding=6 cellspacing=1 class=list><tr class=content><td class=bar>";
footer +="《八六子》<br>";
footer +="宋　秦觀 <br>";
footer +="<br>";
footer +="倚危亭　恨如芳草　萋萋&#21079;盡還生 <br>";
footer +="念柳外青驄別後　水邊紅袂分時　愴然暗驚 <br>";
footer +="<br>";
footer +="無端天與娉婷 <br>";
footer +="夜月一簾幽夢　春風十里柔情 <br>";
footer +="怎奈何　歡愉(娛)漸隨流水 <br>";
footer +="素絃聲斷　翠綃香減　<br>";
footer +="那堪片片飛花弄晚　蒙蒙(濛濛)殘雪(雨)籠晴　<br>";
footer +="正銷凝　黃鸝又啼數聲<br>";
footer +="</td></tr></table></center>";

yearid='';
monthid='';

loc = location.toString();
locs = loc.split('/');
locs.reverse();
locy = locs[2];
locmd = locs[1];
locnow = locy+'/'+locmd;
locm = locmd.substr(0,2)-0;
locd = locmd.substring(2)-0;
locymd = locy+'-'+locm+'-'+locd;
docnow = Date.UTC(locy-0, locm-1, locd);
docs = new Array();
raceloc = races.length;
for (i=0;i<races.length;i++){
	if (locymd == races[i].split(',')[0])
		raceloc = i;
}

function nextdoc(){
	nextymd = races[raceloc+1].split(',')[0];
	locs = nextymd.split('-');
	locnext = locs[0]+'/'+zfill(locs[1])+zfill(locs[2]);
	locnext = loc.replace(locnow,locnext);
	location = locnext;
}

function predoc(){
	nextymd = races[raceloc-1].split(',')[0];
	locs = nextymd.split('-');
	locnext = locs[0]+'/'+zfill(locs[1])+zfill(locs[2]);
	locnext = loc.replace(locnow,locnext);
	location = locnext;
}

function israceday(raceday,theyear){
	var day,y,m,d;
	day = raceday.split('-');
	if (theyear) y=theyear;
	else y = day[0];
	m = day[1]-1;
	d = day[2];
	return Date.UTC(y,m,d);
}

function addyr(){
	document.form1.year.value++;
	setyear(document.form1.year.value);
	showcal();
	yearid = setTimeout('addyr()',200);
}
function minusyr(){
	document.form1.year.value--;
	setyear(document.form1.year.value);
	showcal();
	yearid = setTimeout('minusyr()',200);
}
function dnsrc(obj){
	obj.src='/ui/dn'+obj.name;
}
function upsrc(obj){
	obj.src='/ui/up'+obj.name;
}
function whensubmit(){
	document.form1.year.blur();
}
function setmonth(m){
	if (m == '<'){
		year = year-1;
		firstday.setYear(year);
		month = 11;
		m = 11;
		firstday.setMonth(m);
		selecttoday();
	} else if (m=='>'){
		year = (year-1)+2;
		firstday.setYear(year);
		month = 0;
		m = 0;
		firstday.setMonth(m);
		selecttoday();
	}else
	firstday.setMonth(m);
}
function setyear(yr){
	year = yr;
	firstday.setYear(yr);
}

//================ presets ================
	cmonths = new Array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');

	weekdays = new Array('>月','>水','>火','>木','>金','saturday>土','sunday>日');
	weekdays = new Array('>一','>二','>三','>四','>五','saturday>六','sunday>日');

	months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

//================ the program goes here ================
function settoday(){
	firstday = new Date();
	lastday = new Date();
	today = firstday.getDate();
	month = firstday.getMonth();

	year =firstday.getFullYear();
	firstday.setDate(1);

	now = Date.UTC(year, month, today);
}

settoday();

firstpage = races[0].split(',');
firstpage = firstpage[0].split('-');
lastpage = races[races.length-2].split(',');
lastpage = lastpage[0].split('-');
firstpage = firstpage[0]+'/'+zfill(firstpage[1])+zfill(firstpage[2]);
lastpage = lastpage[0]+'/'+zfill(lastpage[1])+zfill(lastpage[2]);

function gencal(){
	month = firstday.getMonth();
	weekday = firstday.getDay();
	if (weekday==0) weekday = 7;
	days = 30 + (month%2^month<7) + (month==1)*((year%4==0)-2-(year%100==0 && year%1000!=0));
	monthbegin = Date.UTC(year, month, 1);
	monthend = Date.UTC(year, month, days);

	output = '<table cellspacing=2 cellpadding=1 style="margin:0 auto;"><tr><td>';
	output +='<table class=monthborder width=100% cellspacing=0 cellpadding=1><tr>';
		output +='<td>';
		output +='<table class="month" cellspacing=0 width=100%><tr>';
			output +='<td><b>'+months[month]+'</b></td>';
			output +='<td align="right"><b>'+cmonths[month]+'</b></td></tr>';
		output +='</table></td></tr>';
	output +='</table>';
	output +='<table class=calendar id=tabborder cellspacing=1 cellpadding=0><tr>';
	for (day in weekdays){
		output +='<th class='+weekdays[day]+'</th>';
	}
	output +='</tr><tr align=right valign=top>';

	jstart=0;
	jlast=0;
	setstart=1;
	setlast=1;
	inyear = 0;

	for (j=0;j<races.length;j++){
		raceinfo = races[j].split(',');

		curdate = raceinfo[0].split('-');
		if (curdate[0] == year) inyear++;

		if ( monthbegin<=israceday(raceinfo[0]) && setstart){
			jstart = j;
			setstart = 0;
		}
		if ( monthend<=israceday(raceinfo[0]) && setlast){
			jlast = j;
			if (monthend==israceday(raceinfo[0]))
				jlast++;
			setlast = 0;
		}
	}

//alert(races.length+' '+jstart+' '+jlast);

	for (i=-weekday+2;i<=days;i++){
		if (i<=0) output +='<td id=tabborder></td>';
		else {
			cclass = ' class=weekdays';
			dclass = ' class=normal';
			date = Date.UTC(year, month, i);
			day = (i+weekday)%7;

			if      (day==0) cclass = ' class=saturday';
			else if (day==1) cclass = ' class=sunday';

//			if      (date==now) dclass += ' id=today';
			if      (day==2) output += '</tr><tr align=right valign=top>';

			stories = jlast-jstart;
		
				info = '<br>';
			for (j=jstart;j<jlast;j++){
//				info = '';
				raceinfo = races[j].split(',');
				time = '';
				field = '';
				link = '';
				info = time+field;
				if ( date==israceday(raceinfo[0]) ){
					if (raceinfo[1] == '') {
						dclass = ' class=hv';
						course = '';
						link = '<a href="/'+year+'/'+zfill(month+1)+zfill(i)+'/">';
					} else {
						dclass = ' class=st';
						course = '';
						link = raceinfo[1];
					}
					if (raceinfo[2]==''){
						course += '';
						time = '';
					}
					else{
						course += '';
						time = '';
					}
//					time = icon[raceinfo[2]];
					field = link+type[raceinfo[2]]+raceinfo[3]+'" width=16 height=16 border=0 align=top></a>';
					info = time+field;
					break;
				}
			}

			footer = '';
			footer +='<table width=100% cellpadding=6 cellspacing=1 class=list><tr class=content><td class="sidefooter">';
			footer += '這月份有'+stories+'篇文章。<br>';
			footer += '這年共有'+inyear+'篇文章。<br>';
			footer += '全部共有'+races.length+'篇文章<br>';
			if (raceloc>0){
				footer += '<a href=http://www.hay.hk/'+firstpage+'/index.html>第一篇</a>';
				if (raceloc<races.length-1)
					footer += ' <a href=javascript:predoc()>上一篇</a>';
			}
			if (raceloc<races.length-2)
				footer += ' <a href=javascript:nextdoc()>下一篇</a>';
			if (raceloc!=races.length-1)
				footer += ' <a href=http://www.hay.hk/'+lastpage+'/index.html>最後一篇</a>';
			footer +="文章</td></tr></table>";

			birth = new Array();
			birth1 = '<img src=/ui/0.gif width=1 height=16 align=top>';
			birth2 = birth3 = '';
			for (j=0;j<birthday.length;j++){
				birthinfo = birthday[j].split(',');
				hislink = (birthinfo.length==4)?'<a href="'+birthinfo[3]+'">':'';
				
				if ( date == israceday(birthinfo[1],year) ){
					birth1 = hislink + '<img src=/ui/bdcake.gif width=16 height=16 title="這天是 [ ';
					birth2 = ' ] 的生日" align="top" border="0" /></a>';
					birth[birth.length]=birthinfo[0];
				}
			}
			if (birth.length>0)
				birth3 = birth.join("及");
			birthdays = birth1+birth3+birth2;

			death = new Array();
			death1 = '<img src=/ui/0.gif width=1 height=16 align=top>';
			death2 = death3 = '';
			for (j=0;j<deathday.length;j++){
				deathinfo = deathday[j].split(',');
				hislink = (deathinfo.length==4)?'<a href="'+deathinfo[3]+'">':'';
				
				if ( date == israceday(deathinfo[1],year) ){
					death1 = hislink + '<img src=/ui/flower2.gif width=16 height=16 title="'+deathinfo[1].substr(0,4)+'年[ ';
					death2 = ' ] 在這天去世" align="top" border="0" /></a>';
					death[death.length]=deathinfo[0];
				}
			}
			if (death.length>0)
				death3 = death.join("及");
			deathdays = death1+death3+death2;

			if (date==now)
				output += '<td height=100%><table id=today height=100% width=100% cellpadding=0><tr valign=top><td'+dclass+' align=right>'+'<div' +cclass+'>'+ i+'</div> '+info+deathdays+birthdays+'</td></tr></table></td>';
			else {
				if (date==docnow)
					output += '<td height=100%><table id=docnow height=100% width=100% cellpadding=0><tr valign=top><td'+dclass+' align=right>'+'<div' +cclass+'>'+ i+'</div> '+info+deathdays+birthdays+'</td></tr></table></td>';
				else
					output += '<td'+dclass+' style="padding:2px" align=right>'+'<div' +cclass+'>'+ i+'</div>'+info+deathdays+birthdays+'</td>';
			}
		}

	}
	output +=footer+'</tr></table></td></tr></table>';
}

function zfill(num){
	if (num<10) num = '0' +num;
	return num;
}

function showcal(){
	obj = 'calendar';
	gencal();
	if (document.layers){
		document.layers.calendar.document.open();
		document.layers[obj].document.write('<layer>'+output+'</layer>');
		document.calendar.document.close();
		document.calendar.visibility="show";
	}
	else if (document.getElementById){
		document.getElementById(obj).innerHTML = output;
		document.getElementById(obj).style.visibility = 'visible';
	}
}

function selecttoday(){
	if (document.layers){
		setTimeout=('delayselect()',1000);
		return;
	}
	showcal();
	for (i=0;i<document.form1.month.length;i++){
		if (document.form1.month.options[i].value==month){
			document.form1.month.options[i].selected = true;
			break;
		}
	}
	document.form1.year.value=year;
}

function delayselect(){
	showcal();
	for (i=0;i<document.form1.month.length;i++){
		if (document.form1.month.options[i].value==month){
			document.form1.month.options[i].selected = true;
			break;
		}
	}
	document.form1.year.value=year;
}

