var DIR = "http://www.drs-avenue.net/blogparts";	// 設置ディレクトリ
var HTML = "http://www.drs-avenue.net/blogparts/";	// 説明ページ
var NEWS_CGI = "news.cgi";	// ニュースCGI
var NEWS_NUM = 3;	// ニュース数
var STYLE_CSS = "dacal.css";	// カレンダーstyleのcssファイル名

var id_calender = "dacalender";
var prefix = "dacal_";
var id_news		= prefix+"news";
var id_date		= prefix+"date";
var id_age		= prefix+"age";
var id_submit	= prefix+"submit";
var id_reset	= prefix+"reset";

var id_s_year	= prefix+"s_year";
var id_s_month	= prefix+"s_month";
var id_g_year	= prefix+"g_year";
var id_g_month	= prefix+"g_month";
var id_gengou	= prefix+"gengou";

var base = Array(0, 1988, 1925, 1911, 1867);
var day_max = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var datename = Array("日", "月", "火", "水", "木", "金", "土");
var today = new Date();

function newstext(text)	{document.getElementById(id_news).contentWindow.document.body.innerHTML = text;}
function BGerror(id)	{document.getElementById(id).style.background = "FFA0A0";}
function BGnormal(id)	{document.getElementById(id).style.background = "FFFFFF";}
function BGreset()
{
	BGnormal(id_s_year);
	BGnormal(id_s_month);
	BGnormal(id_g_year);
	BGnormal(id_g_month);
	BGnormal(id_age);
}

function setdate(date)
{
	document.getElementById(id_date).innerHTML =
		date.getDate() + "日 " +
		datename[date.getDay()] + "曜";
}

function dacal_init()
{
	document.getElementById(id_s_year).value = today.getFullYear();
	document.getElementById(id_s_month).value = today.getMonth()+1;
	setdate(today);
	changeS();
}

function setnews(year, month)
{
	var rand = Math.floor(Math.random()*NEWS_NUM);
	document.getElementById(id_news).src = DIR+"/"+NEWS_CGI + "?y="+year + "&m="+month + "&r="+rand;
}

function setage(year, month)
{
	var elem = document.getElementById(id_age)
	var age = today.getFullYear() - year;
	if (today.getMonth() < month)
		age--;
	elem.value = age;
}

function changeS()
{
	var year	= parseInt(document.getElementById(id_s_year).value);
	var month	= parseInt(document.getElementById(id_s_month).value);
	var err = "";
	if (isNaN(year))			{BGerror(id_s_year);	err += "西暦年が数値ではありません<br>";}
	if (isNaN(month))			{BGerror(id_s_month);	err += "西暦月が数値ではありません<br>";}
	if (1 > month || month > 12){BGerror(id_s_month);	err += "西暦月の値の範囲が不正です<br>";}
	if (err)	return newstext(err);

	var gengou = document.getElementById(id_gengou);

	var date = year*10000 + month*100 + today.getDate();
		 if (date < 18680125)	gengou.value = 0;
	else if (date < 19120730)	gengou.value = 4;
	else if (date < 19261225)	gengou.value = 3;
	else if (date < 19890108)	gengou.value = 2;
	else						gengou.value = 1;

	document.getElementById(id_g_year).value = year - base[gengou.value];
	document.getElementById(id_g_month).value = month;

	make_calender(year, month);
}

function changeG()
{
	var year	= parseInt(document.getElementById(id_g_year).value);
	var month	= parseInt(document.getElementById(id_g_month).value);
	var err = "";
	if (isNaN(year))			{BGerror(id_g_year);	err += "元号年が数値ではありません";}
	if (isNaN(month))			{BGerror(id_g_month);	err += "元号月が数値ではありません";}
	if (1 > month || month > 12){BGerror(id_g_month);	err += "元号月の値の範囲が不正です";}
	if (err)	return newstext(err);

	var gengou = document.getElementById(id_gengou);
	year += base[gengou.value];
	document.getElementById(id_s_year).value = year;
	document.getElementById(id_s_month).value = month;

	make_calender(year, month);
}

function changeA()
{
	var age = parseInt(document.getElementById(id_age).value);
	if (isNaN(age))	{BGerror(id_age);	return newstext("年齢の値が数値ではありません");}

	document.getElementById(id_s_year).value = today.getFullYear() - age;
	document.getElementById(id_s_month).value = today.getMonth()+1;
	changeS();
}

function make_calender(year, month)
{
	month -= 1;
	var elem = document.getElementById(id_calender);
	var date = new Date(year, month, 1);
	var TODAY = today.getDate();

	if (month == 1)	// 閏年
	{
		if (year % 400 == 0)		day_max[month] = 29;
		else if (year % 100 == 0)	;
		else if (year % 4 == 0)		day_max[month] = 29;
	}

	var html = "<table>";

	html += "<tr><th class='d0'>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th class='d6'>土</th></tr>";

	html += "<tr>";
	var d;
	var day = 1;
	for (d = 0; d < date.getDay(); d++)	html += "<td class='d"+d+"'></td>";
	for (; d < 7; d++, day++)
	{
		if (day == TODAY)	html += "<td class='d"+d+"'><span class='today'>"+day+"</span></td>";
		else				html += "<td class='d"+d+"'>"+day+"</td>";
	}
	html += "</tr>";

	for (; day <= day_max[month];)
	{
		html += "<tr>";
		for (d = 0; d < 7; d++, day++)
		{
			if (day <= day_max[month]) {
				if (day == TODAY)	html += "<td class='d"+d+"'><span class='today'>"+day+"</span></td>";
				else				html += "<td class='d"+d+"'>"+day+"</td>";
			} else					html += "<td class='d"+d+"'></td>";
		}
		html += "</tr>";
	}

	html += "</table>";
	elem.innerHTML = html;

	setnews(year, month);
	setage(year, month);
	var date = new Date(year, month, TODAY);
	setdate(date);
}


var currentform = 0;
function inputfocus(n)	{currentform = n;}

function changesubmit(n)
{
	BGreset();
	if (currentform == 1)
		changeS();
	else if (currentform == 2)
		changeG();
	else if (currentform == 3)
		changeA();
	return false;
}

function formreset()
{
	BGreset();
	document.getElementById(id_age).value = 0;
	changeA();
	return false;
}

document.write('<link href="'+DIR+'/'+STYLE_CSS+'" rel="stylesheet" type="text/css" media="all" />');
document.write("<div class='dacal'><div class='title'><a href='"+HTML+"'>&nbsp;</a></div><div class='subtitle'><a href='"+HTML+"'>明治以降、年代別で人にまつわる医療の歴史も合わせて掲載！</a></div>");
document.write("<form action='.' onsubmit='return changesubmit();' onreset='return formreset();'><div class='s'>");
document.write("西暦<input id='"+id_s_year+"' name='"+id_s_year+"' size='4' maxlength='4' onfocus='inputfocus(1);' />年");
document.write("<input id='"+id_s_month+"' name='"+id_s_month+"' size='2' maxlength='2' onfocus='inputfocus(1);' />月");
document.write("<span id='"+id_date+"'>日 曜日</span></div>");
document.write("<div class='g'><select id='"+id_gengou+"' name='"+id_gengou+"' onfocus='inputfocus(2);'>");
document.write("<option value='1'>平成</option><option value='2'>昭和</option>");
document.write("<option value='3'>大正</option><option value='4'>明治</option>");
document.write("<option value='0'></option></select>");
document.write("<input id='"+id_g_year+"' name='"+id_g_year+"' size='4' maxlength='4' onfocus='inputfocus(2);' />年");
document.write("<input id='"+id_g_month+"' name='"+id_g_month+"' size='2' maxlength='2' onfocus='inputfocus(2);' />月</div>");
document.write("<div id='"+id_calender+"'></div>");
document.write("<div class='a'><input id='"+id_age+"' name='"+id_age+"' size='3' maxlength='3' onfocus='inputfocus(3);' />歳");
document.write("<input type='submit' value='' id='"+id_submit+"'><input type='reset' value='' id='"+id_reset+"'></div>");
document.write("<div class='news'><iframe id='"+id_news+"' frameborder='0' marginheight='0' marginwidth='0' scrolling='no'></iframe></div>");
document.write("</form><div class='footer'><a href='http://www.drs-avenue.net/'>Powered&nbsp;by&nbsp;Doctor's&nbsp;Avenue</a></div></div>");
dacal_init();

