/**
 * 入力文字数カウントイベント用リスト
 * id:要素ID
 * name:項目名
 * max:最大入力桁数
 * type:1:半角, 2:全角, 3:数字, 4:小数点以下を含む数字
 */
var checkTextList = [];

// 入力エリア 入力文字数カウントイベントの設定
Event.observe(window, "load", function() {
	checkTextList.each(function(text) {
		// 桁数を表示する文字列
		var dsptext = (text.type == 2 ? "全角" : "半角") + " #LEN# / " + Math.floor(text.max / text.type) + " 文字";
		// onkeyupイベント
//		Event.observe(text.id, "keyup", function() {
//			var count = Math.floor(countDoubleByteString(this.value) / text.type);
//			$(text.id + "_count").innerHTML = dsptext.replace("#LEN#", count);
//		});
		// onchangeイベント
//		Event.observe(text.id, "change", function() {
//			var count = Math.floor(countDoubleByteString(this.value) / text.type);
//			$(text.id + "_count").innerHTML = dsptext.replace("#LEN#", count);
//		});
		// onblurイベント
		Event.observe(text.id, 'blur', function() {
			formcheck(text.id, text.name, text.max, text.decimal, text.type);
		});
		// 初期表示
//		$(text.id + "_count").innerHTML = dsptext.replace("#LEN#", Math.floor(countDoubleByteString($F(text.id)) / text.type));
	});
});

/**
 * テキストの入力チェックを行う
 * @param id テキスト・テキストエリアID
 * @param name 項目名
 * @param max 最大入力バイト数
 * @param type:1:半角, 2:全角, 3:数字, 4:小数点以下数字
 */
function formcheck(id, name, max, decimal ,type) {
	var alertStr = "";
	
	var byteCount = countDoubleByteString($F(id));
	if (byteCount > max) {
		alertStr = "「" + name + "」の文字数が" + (byteCount - max) + "文字オーバーしています。";
	}
	if (type == 3) {
		var decimalStr = formDecimalCheck($F(id));
		if (decimalStr.length > 0) {
			alertStr = "「" + name + "」に半角数字以外が含まれています：\n" + decimalStr;
		}
	} else if (type == 4) {
		var decimalStr = formDecimalCheckWithNumberOfDecimals($F(id));
		if (decimalStr.length > 0) {
			alertStr = "「" + name + "」に半角数字以外が含まれています：\n" + decimalStr;
		} else {
		    var str = $F(id);
		    var decimalCount = 0;
		    var decimalIndex = 0;
			for (var i = 0; i < str.length; i++ ) {
				var s = str.substring(i, i + 1);
				if (s == ".") {
					decimalCount++;
					decimalIndex = i;
				}
			}
			if (decimalCount > 1) {
				alertStr = "「" + name + "」の小数点の数が正しくありません：\n" + decimalStr;
			} else {
				var decimalPoint = (str.length -1)- decimalIndex;
				if (decimalPoint > decimal ) {
				alertStr = "「" + name + "」の小数点以下の桁数を超えています：\n" + decimalStr;
				}
			}
		}		
	} else {
		var matchStr = formHalfSizeCheck($F(id));
		if (matchStr.length > 0) {
			alertStr = "「" + name + "」に使用不可の文字（半角カナ・半角記号）が含まれています：\n" + matchStr;
		}
	}
	if (alertStr.length != 0) {
		alert(alertStr);
		$(id).focus();
	}
}

/**
 * 渡された文字列のバイト数を返します
 * @param str 文字列
 * @return バイト数
 */
function countDoubleByteString(str) {
	var c = 0;
	var s2 = str.replace(/\x0D\x0A|\x0D|\x0A/g, "\x0D\x0A");
	for (i = 0; i < s2.length; i++ ) {
		m = escape(s2.substring(i, i + 1));
		if (( m.charAt(0) == "%" ) && ( m.charAt(1) < '0' || m.charAt(1) > '7' )) {
			c++;
		}
		c++;
	}
	return c;
}

/**
 * 渡された文字列に半角カナが含まれていた場合は
 * 含まれていた半角カナを文字列で返します
 * @param str 文字列
 * @return 半角カナ文字列
 */
function formHalfSizeCheck(str) {
	var matchList = "";
	for (var i = 0; i < str.length; i++ ) {
		var s = str.substring(i, i + 1);
		if (s.match(/[\uFF61-\uFF9F]/)) {
			if (matchList.indexOf(s) == -1) {
				matchList += (s + " ");
			}
		}
	}
	return matchList;
}

/**
 * 渡された文字列に英数字以外が含まれていた場合は
 * 含まれていた英数字以外を文字列で返します
 * @param str 文字列
 * @return 英数字以外の文字列
 */
function formDecimalCheck(str) {
	var noDecimalList = "";
	for (var i = 0; i < str.length; i++ ) {
		var s = str.substring(i, i + 1);
		if (s.match(/[0-9]/) == null) {
			if (noDecimalList.indexOf(s) == -1) {
				noDecimalList += (s + " ");
			}
		}
	}
	return noDecimalList;
}

/**
 * 渡された文字列に英数字以外が含まれていた場合は
 * 含まれていた英数字以外を文字列で返します
 * @param str 文字列
 * @return 英数字以外の文字列
 */
function formDecimalCheckWithNumberOfDecimals(str) {
	var noDecimalList = "";
	for (var i = 0; i < str.length; i++ ) {
		var s = str.substring(i, i + 1);
		if (s.match(/[0-9.]/) == null) {
			if (noDecimalList.indexOf(s) == -1) {
				noDecimalList += (s + " ");
			}
		}
	}
	return noDecimalList;
}