/** window.onload defer */
Event.domReady.add(function() {
  tekitoku.onload();
});

/** new getElementById */
function $(id) {
	return document.getElementById(id);
}


/** new function */
function objtekitoku(){};
var tekitoku = new objtekitoku();
tekitoku = {

	/** 使用日数取得 */
	funcGetUsedDay: function(dateF, dateT) {
		var diffYY = dateT.substr(0, 4) - dateF.substr(0, 4);
		var diffMM = dateT.substr(4, 2) - dateF.substr(4, 2);
		var diffDD = dateT.substr(6, 2) - dateF.substr(6, 2);
		var usedDay = diffYY * 12 * 30 + diffMM * 30 + diffDD + 1;
		window.status += ",使用日数:" + usedDay;
		return usedDay;
	},
	/** 妥当性検証:日付チェック */
	funcValidDate: function(inDate) {
		var inYY = new Number(inDate.substr(0, 4));
		var inMM = new Number(inDate.substr(4, 2));
		var inDD = new Number(inDate.substr(6, 2));
		var chkDate = new Date(inYY, inMM - 1, inDD);
		//alert(inDate +":"+ inYY +"/"+ inMM +"/"+ inDD +"@"+ chkDate.getFullYear() +"=="+ inYY +"&&"+ (chkDate.getMonth() + 1) +"=="+ inMM +"&&"+ chkDate.getDate() +"=="+ inDD);
		if (chkDate.getFullYear() == inYY && chkDate.getMonth() == inMM - 1 && chkDate.getDate() == inDD) {
			return true;
		} else {
			return false;
		}
	},
	/** HTML:エラーメッセージ表示 */
	funcHtmlError: function(msg) {
		$("error").innerHTML = msg;
	},
	/** HTML:結果表示 */
	funcHtmlAppendRetList: function(retList) {
		// 結果表示
		elementOl = document.createElement("ol");
		elementOl.setAttribute("id", "retList");
		for (var i = 0; i < retList.length; i++) {
			elementList = document.createElement("li");
			elementText = document.createTextNode(retList[i]);
			elementList.appendChild(elementText);
			elementOl.appendChild(elementList);
		}
		if ($("retList")) {
			$("pitatoku").removeChild($("pitatoku").lastChild);
		}
		$("pitatoku").appendChild(elementOl);
	},
	/** HTML:結果表示削除 */
	funcHtmlResetRetList: function() {
		var retList = new Array();
		retList.push("１日単価: (計算中)円");
		retList.push("１旬単価: (計算中)円");
		retList.push("使用日数: (計算中)日");
		retList.push("使用旬数: (計算中)旬");
		retList.push("使用金額: (計算中)円");
		retList.push("払戻金額: (計算中)円");
		this.funcHtmlAppendRetList(retList);
	},
	
	/** ---- ---- ---- ---- */
	/** アクション */
	funcAction: function(teiki, dateF, dateT) {
		if (!teiki.match(/^\d+$/)) {
			this.funcHtmlError("[error] 6ヶ月定期代の金額が数値以外が入力されています。正しく入力してください。");
			return;
		}
		if (!dateF.match(/^\d{8}$/)) {
			this.funcHtmlError("[error] ご利用期間(開始日)が正しくありません。YYYY/MM/DD形式で入力してください。");
			return;
		}
		if (!dateT.match(/^\d{8}$/)) {
			this.funcHtmlError("[error] ご利用期間(終了日)が正しくありません。YYYY/MM/DD形式で入力してください。");
			return;
		}
		if (!this.funcValidDate(dateF)) {
			this.funcHtmlError("[error] ご利用期間(開始日)が正しくありません。存在する日付を入力してください。[" + dateF + "]");
			return;
		}
		if (!this.funcValidDate(dateT)) {
			this.funcHtmlError("[error] ご利用期間(終了日)が正しくありません。存在する日付を入力してください。[" + dateT + "]");
			return;
		}
		if (parseInt(dateF) > parseInt(dateT)) {
			this.funcHtmlError("[error] ご利用期間の開始日と終了日が逆転しています。" + "[" + dateF + "][" + dateT + "]");
			return;
		}
		// 日単価
		var unitDay  = Math.ceil(teiki / 180);
		// 旬単価
		var unitShun = unitDay * 10;
		// 使用日数
		var usedDay = this.funcGetUsedDay(dateF, dateT);
		// 使用旬数
		var usedShun = Math.ceil(usedDay / 10);
		// 使用金額
		var usedKngk = usedShun * unitShun;
		// 払戻金額
		var rtnKngk = teiki - usedKngk - 210;
		// 結果表示
		$("calc1").innerHTML = "１日単価: " + unitDay + "円";
		$("calc2").innerHTML = "１旬単価: " + unitShun + "円";
		$("calc3").innerHTML = "使用日数: " + usedDay + "日";
		$("calc4").innerHTML = "使用旬数: " + usedShun + "旬";
		$("calc5").innerHTML = "使用金額: " + usedKngk + "円";
		$("calc6").innerHTML = "払戻金額: " + rtnKngk + "円";
		// 結果表示
		var retList = new Array();
		retList.push("１日単価: " + unitDay + "円");
		retList.push("１旬単価: " + unitShun + "円");
		retList.push("使用日数: " + usedDay + "日");
		retList.push("使用旬数: " + usedShun + "旬");
		retList.push("使用金額: " + usedKngk + "円");
		retList.push("払戻手数料: 210円");
		if (rtnKngk >= 0) {
			retList.push("払戻金額: " + rtnKngk + "円");
		} else {
			retList.push("払戻金額: 0円(" + rtnKngk + "円)");
		}
		this.funcHtmlAppendRetList(retList);
		this.funcHtmlError("[success] 正常に計算結果が表示されました。");
	},

	/** プレアクション */
	funcPreAction: function() {
		this.funcHtmlResetRetList();
		var teiki = $("teiki").value.replace(/,/g, '');
		var dateF = $("dateF").value.replace(/\//g, '');
		var dateT = $("dateT").value.replace(/\//g, '');
		/** JR定期代算出 */
		this.funcAction(teiki, dateF, dateT);
	},

	/** ---- ---- ---- ---- */
	observe: function(elm, type, func) {
		if (elm.addEventListener) {
			elm.addEventListener(type, func, false);
		} else {type
			elm['on' + type] = func;
		}
	},

	onload: function() {
		// onfocus
		this.observe($("teiki"), "focus", function(){this.value = this.value.replace(/,/g, '');this.select()});
		this.observe($("dateF"), "focus", function(){this.value = this.value.replace(/\//g, '');this.select()});
		this.observe($("dateT"), "focus", function(){this.value = this.value.replace(/\//g, '');this.select()});
		// onblur
		this.observe($("teiki"), "blur", function(){this.value = this.value.replace(/(\d)(\d{3})(?!\d)/g, "$1,$2")});
		this.observe($("dateF"), "blur", function(){this.value = this.value.replace(/(\d{4})(\d{2})(\d{2})/g, "$1/$2/$3")});
		this.observe($("dateT"), "blur", function(){this.value = this.value.replace(/(\d{4})(\d{2})(\d{2})/g, "$1/$2/$3")});
		// onchange
		this.observe($("teiki"), "change", function(){tekitoku.funcPreAction(1)});
		this.observe($("dateF"), "change", function(){tekitoku.funcPreAction(1)});
		this.observe($("dateT"), "change", function(){tekitoku.funcPreAction(1)});
		// init value
		$("teiki").value = "42,340";
		$("dateF").value = "2011/09/01";
		$("dateT").value = "2011/12/20";
		tekitoku.funcPreAction(1);
	}
};


