/*
 * 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);
	// 項目長設定
	setMaxLength(PRM_TXT_WORK_ON_CODE			, LEN_WORK_ON_CODE);
	setMaxLength(PRM_TXT_WORK_TYPE_NAME			, LEN_WORK_TYPE_NAME);
	setMaxLength(PRM_TXT_CAUSE					, LEN_CAUSE);
	setMaxLength(PRM_TXT_STANDARD_HOLIDAY_RATE	, LEN_STANDARD_HOLIDAY_RATE);
	setMaxLength(PRM_TXT_OFFICIAL_HOLIDAY_RATE	, LEN_OFFICIAL_HOLIDAY_RATE);
	/*
	setMaxLength(PRM_TXT_NUMBER_OF_WORK_ON		, LEN_NUMBER_OF_WORK_ON);
	setMaxLength(PRM_TXT_START_TIME_HOUR		, LEN_HOUR);
	setMaxLength(PRM_TXT_START_TIME_MIN			, LEN_MIN);
	setMaxLength(PRM_TXT_END_TIME_HOUR			, LEN_HOUR);
	setMaxLength(PRM_TXT_END_TIME_MIN			, LEN_MIN);
	*/
	// 登録/更新判定
	if (MODE == 0) {
		// 読取専用設定
		setReadOnly(PRM_PLT_DELETE, true);
		// フォーカス設定
		setFocus(PRM_TXT_WORK_ON_CODE);
		setObjectVisibility("btnNewData", false);
		setWidth("btnNewData", 0);
	} else if (MODE == 1) {
		// 読取専用設定
		setReadOnly(PRM_TXT_WORK_ON_CODE, true);
		// フォーカス設定
		setFocus(PRM_TXT_WORK_TYPE_NAME);
	}
}

/**
 * サーバー通信処理
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function submitForm(objForm) {
	// 入力チェック
	if (validate()) {
		// サーバー通信(入力エラー無の場合)
		if (MODE == 0) {
			doSubmitMessage(objForm, 'S2537', 'QC0001', '');
		} else if (MODE == 1) {
			doSubmitMessage(objForm, 'S2538', 'QC0001', '');
		}
	}
}

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();

	checkRequired(PRM_TXT_WORK_ON_CODE			, NAM_WORK_ON_CODE			, aryMessage);
	checkRequired(PRM_TXT_WORK_TYPE_NAME		, NAM_WORK_TYPE_NAME		, aryMessage);
	checkRequired(PRM_TXT_CAUSE					, NAM_CAUSE					, aryMessage);
	checkRequired(PRM_TXT_STANDARD_HOLIDAY_RATE	, NAM_STANDARD_HOLIDAY_RATE	, aryMessage);
	checkRequired(PRM_TXT_OFFICIAL_HOLIDAY_RATE	, NAM_OFFICIAL_HOLIDAY_RATE	, aryMessage);
	/*
	checkRequired(PRM_TXT_NUMBER_OF_WORK_ON		, NAM_NUMBER_OF_WORK_ON		, aryMessage);
	checkRequired(PRM_TXT_START_TIME_HOUR		, NAM_START_TIME_HOUR		, aryMessage);
	checkRequired(PRM_TXT_START_TIME_MIN		, NAM_START_TIME_MIN		, aryMessage);
	checkRequired(PRM_TXT_END_TIME_HOUR			, NAM_END_TIME_HOUR			, aryMessage);
	checkRequired(PRM_TXT_END_TIME_MIN			, NAM_END_TIME_MIN			, aryMessage);
	*/
	checkCode(PRM_TXT_WORK_ON_CODE, NAM_WORK_ON_CODE, aryMessage);

	checkNumber(PRM_TXT_STANDARD_HOLIDAY_RATE	, NAM_STANDARD_HOLIDAY_RATE	, aryMessage);
	checkNumber(PRM_TXT_OFFICIAL_HOLIDAY_RATE	, NAM_OFFICIAL_HOLIDAY_RATE	, aryMessage);
	/*
	checkNumber(PRM_TXT_NUMBER_OF_WORK_ON		, NAM_NUMBER_OF_WORK_ON		, aryMessage);
	checkNumber(PRM_TXT_START_TIME_HOUR			, NAM_START_TIME_HOUR		, aryMessage);
	checkNumber(PRM_TXT_START_TIME_MIN			, NAM_START_TIME_MIN		, aryMessage);
	checkNumber(PRM_TXT_END_TIME_HOUR			, NAM_END_TIME_HOUR			, aryMessage);
	checkNumber(PRM_TXT_END_TIME_MIN			, NAM_END_TIME_MIN			, aryMessage);
	*/
	var officialHoliday = getFormValue(PRM_PLT_OFFICIAL_HOLIDAY);
	if ((officialHoliday == 1 && getFormValue(PRM_CKB_STANDARD_WEEK_HOLIDAY_SUN) == 1)
			|| (officialHoliday == 2 && getFormValue(PRM_CKB_STANDARD_WEEK_HOLIDAY_MON) == 1)
			|| (officialHoliday == 3 && getFormValue(PRM_CKB_STANDARD_WEEK_HOLIDAY_TUE) == 1)
			|| (officialHoliday == 4 && getFormValue(PRM_CKB_STANDARD_WEEK_HOLIDAY_WED) == 1)
			|| (officialHoliday == 5 && getFormValue(PRM_CKB_STANDARD_WEEK_HOLIDAY_THU) == 1)
			|| (officialHoliday == 6 && getFormValue(PRM_CKB_STANDARD_WEEK_HOLIDAY_FRI) == 1)
			|| (officialHoliday == 7 && getFormValue(PRM_CKB_STANDARD_WEEK_HOLIDAY_SAT) == 1)
	) {
		// 法定休日と所定休日が同じ曜日の場合
		if (aryMessage.length == 0) {
			setFocus(PRM_PLT_OFFICIAL_HOLIDAY);
			setMessage("WC0009", NAM_OFFICIAL_HOLIDAY);
		}
		setBgColor(PRM_PLT_OFFICIAL_HOLIDAY, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0009", NAM_OFFICIAL_HOLIDAY));
	}

	checkNumberMinValue(PRM_TXT_STANDARD_HOLIDAY_RATE, 125, NAM_STANDARD_HOLIDAY_RATE, aryMessage);
	checkNumberMinValue(PRM_TXT_OFFICIAL_HOLIDAY_RATE, 135, NAM_OFFICIAL_HOLIDAY_RATE, aryMessage);
	/*
	checkNumberValue(PRM_TXT_NUMBER_OF_WORK_ON, 0, 31, NAM_NUMBER_OF_WORK_ON, aryMessage);

	checkTimeValue(PRM_TXT_START_TIME_HOUR	, PRM_TXT_START_TIME_MIN, 24, NAM_START_TIME, aryMessage);
	checkTimeValue(PRM_TXT_END_TIME_HOUR	, PRM_TXT_END_TIME_MIN	, 24, NAM_END_TIME	, aryMessage);
	*/
	checkDate(PRM_PLT_START_DATE_YEAR, PRM_PLT_START_DATE_MONTH, PRM_PLT_START_DATE_DAY, NAM_START_DATE, aryMessage);
	/*
	if (aryMessage.length == 0) {
		if ((getFormValue(PRM_TXT_START_TIME_HOUR) * 60 + parseIntDecimal(getFormValue(PRM_TXT_START_TIME_MIN))) > (getFormValue(PRM_TXT_END_TIME_HOUR) * 60 + parseIntDecimal(getFormValue(PRM_TXT_END_TIME_MIN)))) {
			setFocus(PRM_TXT_END_TIME_HOUR);
			setMessage("WC2028", NAM_START_TIME + "," + NAM_END_TIME);
			setBgColor(PRM_TXT_START_TIME_HOUR	, COLOR_FIELD_ERROR);
			setBgColor(PRM_TXT_START_TIME_MIN	, COLOR_FIELD_ERROR);
			setBgColor(PRM_TXT_END_TIME_HOUR	, COLOR_FIELD_ERROR);
			setBgColor(PRM_TXT_END_TIME_MIN		, COLOR_FIELD_ERROR);
			aryMessage.push(getMessage("WC2028", NAM_START_TIME + "," + NAM_END_TIME));
		}
	}
	*/
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
}

/**
 * 値確認
 * @param target		確認対象
 * @param minValue		最大値
 * @param maxValue		最小値
 * @param rep			変換文字列
 * @param aryMessage	エラーメッセージ格納配列
 * @return
 */
function checkNumberValue(target, minValue, maxValue, rep, aryMessage) {
	if (checkNumberNoMsg(target)) {
		if (getFormValue(target) < minValue
				|| maxValue < getFormValue(target)
		) {
			if (aryMessage.length == 0) {
				setFocus(target);
				setMessage("WC2027", rep + "," + minValue + "," + maxValue);
			}
			setBgColor(target, COLOR_FIELD_ERROR);
			aryMessage.push(getMessage("WC2027", rep + "," + minValue + "," + maxValue));
		}
	}
}

/**
 * 最小値確認
 * @param target		確認対象
 * @param minValue		最小値
 * @param rep			変換文字列
 * @param aryMessage	エラーメッセージ格納配列
 * @return
 */
function checkNumberMinValue(target, minValue, rep, aryMessage) {
	if (checkNumberNoMsg(target)) {
		if (getFormValue(target) < minValue) {
			if (aryMessage.length == 0) {
				setFocus(target);
				setMessage("WC2026", rep + "," + minValue);
			}
			setBgColor(target, COLOR_FIELD_ERROR);
			aryMessage.push(getMessage("WC2026", rep + "," + minValue));
		}
	}
}

/**
 * 10進数の整数を返す
 * @param target 対象文字列
 */
function parseIntDecimal(target) {
	return parseInt(target, 10);
}
