/*
 * MosP - Mind Open Source Project    http://www.mosp.jp/
 * Copyright (C) MIND Co., Ltd.       http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/**
 * 画面読込時追加処理
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function onLoadExtra() {
	//項目背景色設定
//	setFieldsBgColor(ARY_FIELDS_ID    , COLOR_FIELD_NORMAL);
//	setFieldsBgColor(ARY_ERR_FIELDS_ID, COLOR_FIELD_ERROR);
	setFieldsBgColorByName(ARY_FIELDS_ID	, COLOR_FIELD_NORMAL);
	setFieldsBgColorByName(ARY_ERR_FIELDS_ID, COLOR_FIELD_ERROR);
	//項目長設定
	setMaxLength(PRM_TXT_CF_DAYS	, LEN_CF_DAYS);
//	setMaxLength(PRM_TXT_STANDARD	, LEN_STANDARD);
//	setMaxLength(PRM_TXT_DAYS 		, LEN_DAYS);
//	setMaxLength(PRM_TXT_INFO 		, LEN_INFO);
	setMaxLengthByName(PRM_TXT_STANDARD	, LEN_STANDARD);
	setMaxLengthByName(PRM_TXT_DAYS		, LEN_DAYS);
	setMaxLengthByName(PRM_TXT_INFO		, LEN_INFO);
	//フォーカス設定
	setFocus(PRM_PLT_CF_MONTH);
}	
/**
 * サーバー通信処理
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function submitForm(objForm) {
	// 入力チェック
	if (validate()) {
			doSubmitMessage(objForm,'S2708','QC0001','');
	}
}

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
//	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	setFieldsBgColorByName(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();
	
	checkRequired(PRM_TXT_CF_DAYS	, NAM_CF_DAYS	, aryMessage);
	checkNumber(PRM_TXT_CF_DAYS		, NAM_CF_DAYS	, aryMessage);
	
	var aryStantard = document.getElementsByName(PRM_TXT_STANDARD);
	var aryDays 	= document.getElementsByName(PRM_TXT_DAYS);
	var aryInfo 	= document.getElementsByName(PRM_TXT_INFO);
	
	for(var i = 0; i < 20; i++){
		var k = i + 1;
		if(aryStantard[i].value != ""){
				checkNum(aryStantard[i]						, k + "番目の" + NAM_STANDARD	, aryMessage);
				checkLen(aryStantard[i]	, LEN_STANDARD		, k + "番目の" + NAM_STANDARD	, aryMessage);
			
				checkReq(aryDays[i]							, k + "番目の" + NAM_DAYS 		, aryMessage);
				checkLen(aryDays[i]		, LEN_DAYS 			, k + "番目の" + NAM_DAYS		, aryMessage);
				checkNum(aryDays[i]							, k + "番目の" + NAM_DAYS		, aryMessage);
			
				checkReq(aryInfo[i] 						, k + "番目の" + NAM_INFO		, aryMessage);
				checkLen(aryInfo[i]		, LEN_INFO 			, k + "番目の" + NAM_INFO		, aryMessage);
		} else {
			if(aryDays[i].value != ""){
				checkReq(aryStantard[i]						, k + "番目の" + NAM_STANDARD 	, aryMessage);
				checkNum(aryStantard[i]						, k + "番目の" + NAM_STANDARD 	, aryMessage);
				checkLen(aryStantard[i]	, LEN_STANDARD 		, k + "番目の" + NAM_STANDARD	, aryMessage);

				checkLength(aryDays[i]	, LEN_DAYS 			, k + "番目の" + NAM_DAYS		, aryMessage);
				checkNum(aryDays[i]							, k + "番目の" + NAM_DAYS 		, aryMessage);

				checkReq(aryInfo[i]							, k + "番目の" + NAM_INFO		, aryMessage);
				checkLen(aryInfo[i]		, LEN_INFO			, k + "番目の" + NAM_INFO		, aryMessage);
			} 
			else if(aryInfo[i].value != ""){
				checkReq(aryStantard[i]						, k + "番目の" + NAM_STANDARD 	, aryMessage);
				checkNum(aryStantard[i]						, k + "番目の" + NAM_STANDARD 	, aryMessage);
				checkLen(aryStantard[i]	, LEN_STANDARD 		, k + "番目の" + NAM_STANDARD	, aryMessage);

				checkReq(aryDays[i]							, k + "番目の" + NAM_DAYS 		, aryMessage);
				checkLen(aryDays[i]		, LEN_DAYS 			, k + "番目の" + NAM_DAYS		, aryMessage);
				checkNum(aryDays[i]							, k + "番目の" + NAM_DAYS 		, aryMessage);
			
				checkLen(aryInfo[i] 	, LEN_INFO 			, k + "番目の" + NAM_INFO		, aryMessage);
			}
		}
	}
	
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
}
	
/**
 * 文字列タイプ確認(半角数字)
 * @param target     確認対象(String)
 * @param rep        変換文字列
 * @param aryMessage エラーメッセージ格納配列
 * @return 無し
 * @throws 実行時例外
 */
function checkNum(target, rep, aryMessage) {
	if (target.value.match(/[^0-9]+/)) {
		setBgColor(target, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0003", rep));
	}
}

/**
 * 文字桁数確認
 * @param target     確認対象(StringあるいはObject)
 * @param numLength  確認対象オブジェクト桁数
 * @param rep        変換文字列
 * @param aryMessage エラーメッセージ格納配列
 * @return 無し
 * @throws 実行時例外
 */
function checkLen(target, numLength, rep, aryMessage) {
	if (numLength < target.value.length) {
		setBgColor(target, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0004", rep + SEPARATOR_MESSAGE + numLength));
	}
}

/**
 * 必須確認
 * @param target     確認対象(StringあるいはObject)
 * @param rep        変換文字列
 * @param aryMessage エラーメッセージ格納配列
 * @return 無し
 * @throws 実行時例外
 */
function checkReq(target, rep, aryMessage) {
	if (target.value == "") {
		setBgColor(target, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0001", rep));
	}
}

/**
 * フィールド背景色設定
 * @param targetId 設定対象
 * @param strColor 設定色
 * @return 無し
 * @throws 実行時例外
 */
function setFieldsBgColorByName(target, strColor) {
	for (var i = 0; i < target.length; i++) {
		var aryTarget = document.getElementsByName(target[i]);
		for (var j = 0; j < aryTarget.length; j++) {
			setBgColor(aryTarget[j], strColor);
		}
	}
}

/**
 * フィールド長設定
 * @param target    設定対象
 * @param numLength フィールド長
 * @return 無し
 * @throws 実行時例外
 */
function setMaxLengthByName(target, numLength) {
	var	aryTarget = document.getElementsByName(target);
	for (var i = 0; i < aryTarget.length; i++) {
		setMaxLength(aryTarget[i], numLength);
	}
}
