/*予約スケジュール*/
/* for http://www.after-beat.co.jp/schedule/	*/

/* 全角英数字を半角英数字に置換する */
/*	http://homepage3.nifty.com/aokura/jscript/z2h_word.html	*/
function z2h_word(src) {
	return src.replace(/([Ａ-Ｚａ-ｚ０-９＿／．])/g,
		function ($0) {
			return String.fromCharCode($0.charCodeAt(0) - 65248);
		});
}


/*
	日付移動フォームを生成
	引数：デフォルトの日付 年, 月, 日
*/
function setMoveDateForm(y, m, d, dateStr, corner)
{
	if ((! document.getElementById) || (! Array.prototype.push)) {	//N4等除外。Array.pushでMac IE除外。
		return;
	}
	if (! RegExp) {
			return false;	//正規表現が使えなければ終了。
	}
	
	var defaultDateStr = y + "/"+ m + "/" + d;
	
	document.write(
		'<div id="moveDateBox">' + "\n",
		'<p><form action="#" method="get" id="moveDateForm">' +"\n",
		'<input type="text" name="moveDate" id="moveDate" size="14" maxlength="20" value="',
		defaultDateStr,
		'" /> <input type="submit" value="移動" id="moveDateSubmit"/>' + "\n",
		'</form>' + "\n",
		"</p>\n",
		'<p class="notes" id="dateInputExample">入力例 [' + y + '/' + m + '/' + d + '] ',
		'[' + m + '/' + d + '] [' + d + '] [' + dateStr + '] [' + m + '.' + d + '] [' + m + '-' + d + ']など。</p>'+ "\n",
		"</div>\n"
	);
	
	//入力例設定
	var example = document.getElementById('dateInputExample');
	if (! example) {
		return;
	}
	
	example.onmouseover = function() {
		this.style.position = "absolute";
		this.style.display = "block";
	}
	
	example.onmouseout = function() {
		this.style.display = "none";
	}
	
	example.onmouseout();
	//return false;
	//日付入力フォーム設定
	var moveDate = document.getElementById('moveDate');
	if (! moveDate) {
		return;
	}
	
	moveDate.style.color = "#999";
	moveDate.defaultDateStr = defaultDateStr;
	
	moveDate.onfocus = function() {
		if(this.value == this.defaultDateStr){
			//this.value ='';
			this.style.color = '#000';
		}
	}
	
	moveDate.onblur = function() {
		if(this.value == ''){
			this.value = this.defaultDateStr;
			this.style.color = '#999';
		} else if (this.value == this.defaultDateStr) {
			this.style.color = '#999';
		}
	}
	
	moveDate.setDefaultValue = function() {
		this.value = this.defaultDateStr;
		this.style.color = '#999';
	}
	
	//form設定
	var moveDateForm = document.getElementById('moveDateForm');
	if (! moveDateForm) {
		return;
	}
	
	moveDateForm.example = example;
	
	moveDateForm.onmouseover = function() {
		this.example.onmouseover();
		this.example.style.top = (this.moveDate.offsetHeight + 5) + "px";
	}
	
	moveDateForm.onmouseout = function() {
		this.example.onmouseout();
	}
	
	//formのサブミットハンドラ
	//入力された日付を整形し、指定の日付に移動する。
	moveDateForm.onsubmit = function() {
		var date = this.moveDate.value;	//日付フィールドの値をget
		
		if (date == "") {	//空文字は何もしない。
			return false;
		}
		
		date = z2h_word(date);	//全角英数->半角変換
		date = date.replace(/[\_\.。・年月日\-]/g, '/');	//区切り文に該当しそうな文字をスラッシュに置換。
		date = date.replace(/\/+/g, '/');	//連続するスラッシュを1つに置換。
		
		if (! date.match(/^\d[\d\/]*$/)) {	//数字とスラッシュ以外の文字が含まれていれば、終了。
			this.moveDate.setDefaultValue();
			return false;
		}
		
		dates = date.split("/");	//スラッシュで分解
		
		//デフォルトとして本日の日付を取得しておく。
		var today = new Date();
		var year = today.getFullYear();
		var month = today.getMonth() +1;
		var day = today.getDate();
		var dateStr = "";
		
		if (date.match(/^(\d{4})(\d{2})(\d{2})$/)) {	//20で始まる8桁の数字の場合
			dates[0] = RegExp.$1;	//year
			dates[1] = RegExp.$2;	//month
			dates[2] = RegExp.$3;	//day
		}
		
		
		//[注]parseInt('09');は8進数と解釈されるため、明示的にparseInt('09', 10);とする。
		var len = dates.length;	//分解配列の数
		if (len >= 3) {	//年月日とみなす
			year = parseInt(dates[0], 10);	//整数変換
			month = parseInt(dates[1], 10);
			day = parseInt(dates[2], 10);
		} else if (len == 2) {	//月日とみなす
			month = parseInt(dates[0], 10);
			day = parseInt(dates[1], 10);
		} else if (len == 1) {	//日付のみとみなす。
			day = parseInt(dates[0], 10);
		}
		
		if (year < 1000) {	//西暦が4文字未満ならば、
			if (year < 90) {
				year += 2000;
			} else {
				year += 1900;
			}
		}
		
		if (year > (today.getFullYear() + 1)) {	//年が翌年以上の未来ならば終了。
			this.moveDate.setDefaultValue();
			return false;
		}
		
		if (month > 12) {	//月が12以上ならば終了。
			this.moveDate.setDefaultValue();
			return false;
		}
		
		if (day > 31) {	//日が31以上ならば終了。
			this.moveDate.setDefaultValue();
			return false;
		}
		
		month = ("0" + month).replace(/^.+(\d\d)$/,"$1");	//0パディング
		day = ("0" + day).replace(/^.+(\d\d)$/,"$1");	//0パディング
		
		dateStr = "" + year + month + day;
		location.href = corner + dateStr + ".html";	//指定の日付に移動
		return false;
	}
}

