// 観測所の設定
// 観測所番号？、観測所名、観測開始日、観測終了日(現在も観測している場合はnull)、最初に選択する観測所かどうか
// 最初に選択する観測所が複数ある場合、一番最後のものが選択されます
var Stations = [
	['0943', '宗像', '1976/1/1', null, false],
	['0780', '八幡', '1976/1/1', null, false],
	['0944', '頂吉', '1976/1/1', null, false],
	['1590', '空港北町(北九州空港)', '2006/3/16', null, false],
	['0782', '行橋', '1976/1/1', null, false],
	['0783', '篠栗', '1976/1/1', '2009/10/28', false],
	['47809', '飯塚', '1961/1/1', null, false],
	['0785', '前原', '1976/1/1', null, false],
	['47807', '福岡', '1921/1/1', null, true],
	['1477', '博多(福岡空港)', '2003/1/1', null, false],
	['1141', '太宰府', '1977/3/2', null, false],
	['1046', '添田', '1976/1/1', null, false],
	['0787', '九千部山', '1976/1/1', '2010/3/2', false],
	['1611', '早良脇山', '2010/3/2', null, false],
	['0788', '朝倉', '1976/1/1', null, false],
	['0789', '英彦山', '1976/6/9', null, false],
	['0790', '久留米', '1976/1/1', null, false],
	['0791', '耳納山', '1976/6/1', null, false],
	['0792', '黒木', '1976/1/1', null, false],
	['0945', '柳川', '1976/1/1', null, false],
	['0793', '大牟田', '1976/1/1', null, false],
	['1527', '曽根(旧北九州空港)', '2003/1/1', '2006/3/15', false],
	['0781', '小倉', '1976/1/1', '1977/2/16', false]
];

var CalendarUtil = {
	isLeapYear : function(y) {
		// うるう年は、4で割り切れて100で割り切れない年
		// ただし、400で割り切れる年はうるう年とする
		return ((y % 4) == 0 && (y % 100) != 0) || (y % 400) == 0;
	},

	getLastDate : function(y, m) {
		switch (m) {
		case 1: return 31;
		case 2: return this.isLeapYear(y) ? 29 : 28;
		case 3: return 31;
		case 4: return 30;
		case 5: return 31;
		case 6: return 30;
		case 7: return 31;
		case 8: return 31;
		case 9: return 30;
		case 10: return 31;
		case 11: return 30;
		case 12: return 31;
		default:
			return -1;
		}
	}
};

var StationUtil = {
	y1 : 0,
	m1 : 0,
	d1 : 0,
	y2 : 0,
	m2 : 0,
	d2 : 0,

	stat : null,
	yy : null,
	mm : null,
	dd : null,
	range1 : null,
	range2 : null,

	initialize : function() {
		this.stat = document.getElementById('b_no');

		this.stat.length = Stations.length;
		var ss = 0;
		for (var i = 0; i < Stations.length; ++i) {
			this.stat.options[i] = new Option(Stations[i][1], Stations[i][0]);
			if (Stations[i][4]) {
				ss = i;
			}
		}
		this.stat.selectedIndex = ss;

		this.yy = document.getElementById('yy');
		this.mm = document.getElementById('mm');
		this.dd = document.getElementById('dd');
		this.range1 = document.getElementById('range1');
		this.range2 = document.getElementById('range2');

		if (window.addEventListener) {
			this.stat.addEventListener("change", StationUtil.changeStation, false);
			this.yy.addEventListener("change", StationUtil.changeYear, false);
			this.mm.addEventListener("change", StationUtil.changeMonth, false);
		} else if (window.attachEvent) {
			this.stat.attachEvent("onchange", StationUtil.changeStation);
			this.yy.attachEvent("onchange", StationUtil.changeYear);
			this.mm.attachEvent("onchange", StationUtil.changeMonth);
		}

		this.changeStation();

		// 前月の1日を選択状態にする
		var d = new Date();
		d.setMonth(d.getMonth() - 1);

		this.yy.value = d.getYear();
		this.mm.value = d.getMonth() + 1;
		this.dd.value = 1;
	},

	setRange : function(sdate, edate) {
		var s = sdate.split('/');
		this.y1 = Number(s[0]);
		this.m1 = Number(s[1]);
		this.d1 = Number(s[2]);

		if (edate != null) {
			var e = edate.split('/');
			this.y2 = Number(e[0]);
			this.m2 = Number(e[1]);
			this.d2 = Number(e[2]);
		} else {
			var d = new Date();
			d.setDate(d.getDate()-1);
			this.y2 = d.getFullYear();
			this.m2 = d.getMonth() + 1;
			this.d2 = d.getDate();
		}
	},

	getYearRange : function() {
		return [this.y2, this.y1];
	},

	getMonthRange : function(y) {
		if (y == this.y1) {
			return [this.m1, 12];
		} else if (y == this.y2) {
			return [1, this.m2];
		} else {
			return [1, 12];
		}
	},

	getDateRange : function(y, m) {
		if (y == this.y1 && m == this.m1) {
			return [this.d1, CalendarUtil.getLastDate(y, m)];
		} else if (y == this.y2 && m == this.m2) {
			return [1, this.d2];
		} else {
			return [1, CalendarUtil.getLastDate(y, m)];
		}
	},

	changeStationImpl : function() {
		var i = this.stat.selectedIndex;
		var s = Stations[i];
		this.setRange(s[2], s[3]);

		var sely = this.yy.value;

		var yr = this.getYearRange();
		this.yy.length = 0;
		var c = 0;
		for (var i = yr[0]; i >= yr[1]; --i) {
			this.yy.options[c] = new Option(i, i, (i == sely), (i == sely));
			++c;
		}

		this.range1.innerHTML = this.y1 + '年' + this.m1 + '月' + this.d1 + '日';
		this.range2.innerHTML = this.y2 + '年' + this.m2 + '月' + this.d2 + '日';

		this.changeYearImpl();
	},

	changeStation : function() {
		StationUtil.changeStationImpl();
	},

	changeYearImpl : function() {
		var sely = Number(this.yy.options[this.yy.selectedIndex].value);
		var mr = this.getMonthRange(sely);

		var selm = this.mm.value;

		this.mm.length = 0;
		var c = 0;
		for (var i = mr[0]; i <= mr[1]; ++i) {
			this.mm.options[c] = new Option(i, i, (i == selm), (i == selm));
			++c;
		}

		this.changeMonth();
	},

	changeYear : function() {
		StationUtil.changeYearImpl();
	},

	changeMonthImpl : function() {
		var sely = Number(this.yy.options[this.yy.selectedIndex].value);
		var selm = Number(this.mm.options[this.mm.selectedIndex].value);
		var dr = this.getDateRange(sely, selm);

		var seld = this.dd.value;
		this.dd.length = 0;
		var c = 0;
		for (var i = dr[0]; i <= dr[1]; ++i) {
			this.dd.options[c] = new Option(i, i, (i == seld), (i == seld));
			++c;
		}
	},

	changeMonth : function() {
		StationUtil.changeMonthImpl();
	},

	init : function() {
		StationUtil.initialize();
	}
};

// HTML読み込み後に読み込むように、イベントリスナに追加する
if (window.addEventListener) {
	// 一般的なブラウザの場合
	window.addEventListener("load", StationUtil.init, false);
} else if (window.attachEvent) {
	// IEの場合
	window.attachEvent("onload", StationUtil.init);
}

	// 過去データの初期値の設定を先月の1日とする
	function initial(){
/*		var today = new Date();
		var initYear = today.getYear();
		var initMonth = today.getMonth();

		if (initYear < 2000) {
			 initYear += 1900;
		}

//		selectbox(initYear);

		var sel = document.getElementById("yy");

		for (var i = 0;i < sel.options.length; i++) {
			if (sel.options[i].text == initYear) {
				if (initMonth == 0) {
				// 1月の場合は前年（12月）とする
					sel.selectedIndex = i + 1;
				} else {
					sel.selectedIndex = i;
				}
				break;
			}
		}

		if (initMonth == 0) {
		// 1月の場合は（前年の）12月とする
			document.past.month.options[initMonth + 11].selected=true;
		} else {
			document.past.month.options[initMonth - 1].selected=true;
		}*/

		selectType();
	}

	// セレクトボックス（年）を自動的に作成する
	function selectbox(initYear) {
		var selectYear ="";
		tempYear = initYear;

		selectYear += "<select name=\"year\" id=\"yy\" onChange=\"setDay(this)\">\n";

		for (var j = tempYear;j >= 1971; j--) {
				selectYear += "<option value=" + j + ">" + j + "</option>\n";
		}

		selectYear += "</select>年";

		// 書き出しを行う
		document.getElementById("select").innerHTML = selectYear;

	}

	// 地点の値（地上orアメダス）によって年のリストを変更する
	function setYear(obj){
		obj = obj.form;
		var bno = document.getElementById('b_no');
		var no = bno.options[bno.selectedIndex].value.length;

		var _today = new Date();
		var _initYear = _today.getYear();

		if (_initYear < 2000) {
			 _initYear += 1900;
		}

		var obs_n= _initYear - 1971 + 1;
		var ame_n= _initYear - 1976 + 1;
		
		if (no == 4) {					// アメダス官署の場合
		// 地点名が4桁の場合
			obj.year.length = ame_n;
		} else {						// 気象台や旧測候所の場合
		// 地点名がその他（5桁）の場合
			var op = document.past.year.options;

			for (yyyy = 1975; yyyy >= 1971; yyyy--) {
				if (op.length < obs_n) {
					op.length++;
					op[op.length - 1].text = yyyy;
					op[op.length - 1].value = yyyy
				}
			}
		}
	}

	// 年と月の値によって日のリストを決定する
	function setDay(obj){
		obj = obj.form;
		var year = parseInt(obj.year.options[obj.year.selectedIndex].value);
		var month = parseInt(obj.month.options[obj.month.selectedIndex].value);
		var lastday = monthday(year,month);
		var itemnum = obj.day.length;
		if (lastday - 1 < obj.day.selectedIndex) {
			obj.day.selectedIndex = lastday - 1;
		}
		obj.day.length = lastday;
		for (cnt = itemnum + 1;cnt <= lastday;cnt++) {
			obj.day.options[cnt - 1].text = cnt;
		}
	}
	function monthday(year, month){
		var lastday = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
			lastday[1] = 29;
		}
		return lastday[month - 1];
	}

	// 選択する要素に合わせて年月日の選択可否を設定する
	function selectType() {
		switch (document.past.elm.selectedIndex) {
		case 0:
			all_sel();
			break;
		case 1:
			no_sel_d();
			break;
		case 2:
			no_sel_md();
			break;
		case 3:
		case 4:
			no_sel_ymd();
			break;
		default:
		}
	}

	// 選択可能
	function enabledSel(obj) {
		obj.disabled = false;
		obj.style.backgroundColor = 'transparent';
	}

	// 選択不可
	function disabledSel(obj) {
		obj.disabled = true;
		obj.style.backgroundColor = 'silver';
	}

	// 年月日を選択しない
	function no_sel_ymd(){
		disabledSel(document.getElementById('yy'));
		disabledSel(document.getElementById('mm'));
		disabledSel(document.getElementById('dd'));
	}

	// 月日を選択しない
	function no_sel_md(){
		enabledSel(document.getElementById('yy'));
		disabledSel(document.getElementById('mm'));
		disabledSel(document.getElementById('dd'));
	}

	// 日を選択しない
	function no_sel_d(){
		enabledSel(document.getElementById('yy'));
		enabledSel(document.getElementById('mm'));
		disabledSel(document.getElementById('dd'));
	}

	// 全て選択する
	function all_sel(){
		enabledSel(document.getElementById('yy'));
		enabledSel(document.getElementById('mm'));
		enabledSel(document.getElementById('dd'));
	}

	// 要素と地点（地上orアメダス）によってジャンプ先のURLを設定する
	function jump(){
		var point = document.getElementById('b_no');

		var idx = point.selectedIndex;
		var p = point[idx].value;
		var pt = point[idx].text;

		document.getElementById('b_ch').value = pt;

		var elm = document.getElementById('el').value;
		var frm = document.getElementById('past');
		if (p.length == 4) {
			switch (elm) {
			case 'hourly':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/hourly_a1.php';
				break;
			case 'daily':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php';
				break;
			case 'monthly':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/monthly_a1.php';
				break;
			case 'normal':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/nml_amd_ym.php';
				break;
			case 'rank':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/rank_a.php';
				break;
			default:
				return;
			}
		} else {
			switch (elm) {
			case 'hourly':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/hourly_s1.php';
				break;
			case 'daily':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/daily_s1.php';
				break;
			case 'monthly':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/monthly_s1.php';
				break;
			case 'normal':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/nml_sfc_ym.php';
				break;
			case 'rank':
				frm.action = 'http://www.data.jma.go.jp/obd/stats/etrn/view/rank_s.php';
				break;
			default:
				return;
			}
		}

		frm.submit();
	}

// HTML読み込み後に読み込むように、イベントリスナに追加する
if (window.addEventListener) {
	// 一般的なブラウザの場合
	window.addEventListener("load", initial, true);
} else if (window.attachEvent) {
	// IEの場合
	window.attachEvent("onload", initial);
}
