/*
 * 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_LCODE				, LEN_LCODE				);
	setMaxLength(PRM_TXT_LNAME				, LEN_LNAME				);
	setMaxLength(PRM_TXT_PRESCRIBED_TIME_HOUR, LEN_PRESCRIBED_TIME_HOUR);
	setMaxLength(PRM_TXT_PRESCRIBED_TIME_MIN, LEN_PRESCRIBED_TIME_MIN);
	/*
	setMaxLength(PRM_TXT_CAUTION_DAY_HOUR	, LEN_CAUTION_DAY_HOUR	);
	setMaxLength(PRM_TXT_CAUTION_DAY_MIN	, LEN_CAUTION_DAY_MIN	);
	setMaxLength(PRM_TXT_CAUTION_TERM_HOUR	, LEN_CAUTION_TERM_HOUR	);
	setMaxLength(PRM_TXT_CAUTION_TERM_MIN	, LEN_CAUTION_TERM_MIN	);
	setMaxLength(PRM_TXT_CAUTION_YEAR_HOUR	, LEN_CAUTION_YEAR_HOUR	);
	setMaxLength(PRM_TXT_CAUTION_YEAR_MIN	, LEN_CAUTION_YEAR_MIN	);
	setMaxLength(PRM_TXT_LIMIT_DAY_HOUR		, LEN_LIMIT_DAY_HOUR	);
	setMaxLength(PRM_TXT_LIMIT_DAY_MIN		, LEN_LIMIT_DAY_MIN		);
	setMaxLength(PRM_TXT_LIMIT_TERM_HOUR	, LEN_LIMIT_TERM_HOUR	);
	setMaxLength(PRM_TXT_LIMIT_TERM_MIN		, LEN_LIMIT_TERM_MIN	);
	setMaxLength(PRM_TXT_LIMIT_YEAR_HOUR	, LEN_LIMIT_YEAR_HOUR	);
	setMaxLength(PRM_TXT_LIMIT_YEAR_MIN		, LEN_LIMIT_YEAR_MIN	);
	setMaxLength(PRM_TXT_RATE_TERM			, LEN_RATE_TERM			);
	setMaxLength(PRM_TXT_RATE_YEAR			, LEN_RATE_YEAR			);
 	*/
	// 登録/更新判定
	if (MODE == 0) {
		// 読取専用設定
		setReadOnly(PRM_PLT_DELETE_FLAG, true);
		// フォーカス設定
		setFocus(PRM_TXT_LCODE);
	} else if (MODE == 1) {
		// 読取専用設定及びフォーカス設定(UPDATEの場合)
		setReadOnly(PRM_TXT_LCODE  , true);
		// フォーカス設定
		setFocus(PRM_TXT_LNAME);
	}
	// 新規登録ボタン利用可否
	if (MODE != 1) {
		setObjectVisibility("btnNewData", false);
		setWidth("btnNewData", 0);
	}
}

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

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();
	
	// 必須チェック
	checkRequired(PRM_TXT_LCODE					, NAM_LCODE					, aryMessage);
	checkRequired(PRM_TXT_LNAME					, NAM_LNAME					, aryMessage);
	checkRequired(PRM_TXT_PRESCRIBED_TIME_HOUR	, NAM_PRESCRIBED_TIME_HOUR	, aryMessage);
	checkRequired(PRM_TXT_PRESCRIBED_TIME_MIN	, NAM_PRESCRIBED_TIME_MIN	, aryMessage);
	/*	
	checkRequired(PRM_TXT_CAUTION_DAY_HOUR		, NAM_CAUTION_DAY_HOUR		, aryMessage);
	checkRequired(PRM_TXT_CAUTION_DAY_MIN		, NAM_CAUTION_DAY_MIN		, aryMessage);
	checkRequired(PRM_TXT_CAUTION_TERM_HOUR		, NAM_CAUTION_TERM_HOUR		, aryMessage);
	checkRequired(PRM_TXT_CAUTION_TERM_MIN		, NAM_CAUTION_TERM_MIN		, aryMessage);
	checkRequired(PRM_TXT_CAUTION_YEAR_HOUR		, NAM_CAUTION_YEAR_HOUR		, aryMessage);
	checkRequired(PRM_TXT_CAUTION_YEAR_MIN		, NAM_CAUTION_YEAR_MIN		, aryMessage);
	checkRequired(PRM_TXT_LIMIT_DAY_HOUR		, NAM_LIMIT_DAY_HOUR		, aryMessage);
	checkRequired(PRM_TXT_LIMIT_DAY_MIN			, NAM_LIMIT_DAY_MIN			, aryMessage);
	checkRequired(PRM_TXT_LIMIT_TERM_HOUR		, NAM_LIMIT_TERM_HOUR		, aryMessage);
	checkRequired(PRM_TXT_LIMIT_TERM_MIN		, NAM_LIMIT_TERM_MIN		, aryMessage);
	checkRequired(PRM_TXT_LIMIT_YEAR_HOUR		, NAM_LIMIT_YEAR_HOUR		, aryMessage);
	checkRequired(PRM_TXT_LIMIT_YEAR_MIN		, NAM_LIMIT_YEAR_MIN		, aryMessage);
	checkRequired(PRM_TXT_RATE_TERM				, NAM_RATE_TERM				, aryMessage);
	checkRequired(PRM_TXT_RATE_YEAR				, NAM_RATE_YEAR				, aryMessage);
 	*/
	// 入力値チェック
	checkCode(PRM_TXT_LCODE	, NAM_LCODE	, aryMessage);
	
	checkNumber(PRM_TXT_PRESCRIBED_TIME_HOUR	, NAM_PRESCRIBED_TIME_HOUR	, aryMessage);
	checkNumber(PRM_TXT_PRESCRIBED_TIME_MIN		, NAM_PRESCRIBED_TIME_MIN	, aryMessage);
	/*
	checkNumber(PRM_TXT_CAUTION_DAY_HOUR		, NAM_CAUTION_DAY_HOUR		, aryMessage);
	checkNumber(PRM_TXT_CAUTION_DAY_MIN			, NAM_CAUTION_DAY_MIN		, aryMessage);
	checkNumber(PRM_TXT_CAUTION_TERM_HOUR		, NAM_CAUTION_TERM_HOUR		, aryMessage);
	checkNumber(PRM_TXT_CAUTION_TERM_MIN		, NAM_CAUTION_TERM_MIN		, aryMessage);
	checkNumber(PRM_TXT_CAUTION_YEAR_HOUR		, NAM_CAUTION_YEAR_HOUR		, aryMessage);
	checkNumber(PRM_TXT_CAUTION_YEAR_MIN		, NAM_CAUTION_YEAR_MIN		, aryMessage);
	checkNumber(PRM_TXT_LIMIT_DAY_HOUR			, NAM_LIMIT_DAY_HOUR		, aryMessage);
	checkNumber(PRM_TXT_LIMIT_DAY_MIN			, NAM_LIMIT_DAY_MIN			, aryMessage);
	checkNumber(PRM_TXT_LIMIT_TERM_HOUR			, NAM_LIMIT_TERM_HOUR		, aryMessage);
	checkNumber(PRM_TXT_LIMIT_TERM_MIN			, NAM_LIMIT_TERM_MIN		, aryMessage);
	checkNumber(PRM_TXT_LIMIT_YEAR_HOUR			, NAM_LIMIT_YEAR_HOUR		, aryMessage);
	checkNumber(PRM_TXT_LIMIT_YEAR_MIN			, NAM_LIMIT_YEAR_MIN		, aryMessage);
	checkNumber(PRM_TXT_RATE_TERM				, NAM_RATE_TERM				, aryMessage);
	checkNumber(PRM_TXT_RATE_YEAR				, NAM_RATE_YEAR				, aryMessage);
 	*/	
	// 項目長チェック
	checkLength(PRM_TXT_LCODE				, LEN_LCODE					, NAM_LCODE					, aryMessage);
	checkLength(PRM_TXT_LNAME				, LEN_LNAME					, NAM_LNAME					, aryMessage);
	checkLength(PRM_TXT_PRESCRIBED_TIME_HOUR, LEN_PRESCRIBED_TIME_HOUR	, NAM_PRESCRIBED_TIME_HOUR	, aryMessage);
	checkLength(PRM_TXT_PRESCRIBED_TIME_MIN	, LEN_PRESCRIBED_TIME_MIN	, NAM_PRESCRIBED_TIME_MIN	, aryMessage);
	/*
	checkLength(PRM_TXT_CAUTION_DAY_HOUR	, LEN_CAUTION_DAY_HOUR		, NAM_CAUTION_DAY_HOUR		, aryMessage);
	checkLength(PRM_TXT_CAUTION_DAY_MIN		, LEN_CAUTION_DAY_MIN		, NAM_CAUTION_DAY_MIN		, aryMessage);
	checkLength(PRM_TXT_CAUTION_TERM_HOUR	, LEN_CAUTION_TERM_HOUR		, NAM_CAUTION_TERM_HOUR		, aryMessage);
	checkLength(PRM_TXT_CAUTION_TERM_MIN	, LEN_CAUTION_TERM_MIN		, NAM_CAUTION_TERM_MIN		, aryMessage);
	checkLength(PRM_TXT_CAUTION_YEAR_HOUR	, LEN_CAUTION_YEAR_HOUR		, NAM_CAUTION_YEAR_HOUR		, aryMessage);
	checkLength(PRM_TXT_CAUTION_YEAR_MIN	, LEN_CAUTION_YEAR_MIN		, NAM_CAUTION_YEAR_MIN		, aryMessage);
	checkLength(PRM_TXT_LIMIT_DAY_HOUR		, LEN_LIMIT_DAY_HOUR		, NAM_LIMIT_DAY_HOUR		, aryMessage);
	checkLength(PRM_TXT_LIMIT_DAY_MIN		, LEN_LIMIT_DAY_MIN			, NAM_LIMIT_DAY_MIN			, aryMessage);
	checkLength(PRM_TXT_LIMIT_TERM_HOUR		, LEN_LIMIT_TERM_HOUR		, NAM_LIMIT_TERM_HOUR		, aryMessage);
	checkLength(PRM_TXT_LIMIT_TERM_MIN		, LEN_LIMIT_TERM_MIN		, NAM_LIMIT_TERM_MIN		, aryMessage);
	checkLength(PRM_TXT_LIMIT_YEAR_HOUR		, LEN_LIMIT_YEAR_HOUR		, NAM_LIMIT_YEAR_HOUR		, aryMessage);
	checkLength(PRM_TXT_LIMIT_YEAR_MIN		, LEN_LIMIT_YEAR_MIN		, NAM_LIMIT_YEAR_MIN		, aryMessage);
	checkLength(PRM_TXT_RATE_TERM			, LEN_RATE_TERM				, NAM_RATE_TERM				, aryMessage);
	checkLength(PRM_TXT_RATE_YEAR			, LEN_RATE_YEAR				, NAM_RATE_YEAR				, aryMessage);
	*/	
	// 時間、分の合計値チェック
	checkTimeValue(PRM_TXT_PRESCRIBED_TIME_HOUR	,PRM_TXT_PRESCRIBED_TIME_MIN	, 8		, NAM_PRESCRIBED_TIME	,aryMessage);
	/*
	checkTimeValue(PRM_TXT_CAUTION_DAY_HOUR		,PRM_TXT_CAUTION_DAY_MIN		, 15	, NAM_CAUTION_DAY		,aryMessage);
	checkTimeValue(PRM_TXT_CAUTION_YEAR_HOUR	,PRM_TXT_CAUTION_YEAR_MIN		, 360	, NAM_CAUTION_YEAR		,aryMessage);
	checkTimeValue(PRM_TXT_LIMIT_DAY_HOUR		,PRM_TXT_LIMIT_DAY_MIN			, 15	, NAM_LIMIT_DAY			,aryMessage);
	checkTimeValue(PRM_TXT_LIMIT_YEAR_HOUR		,PRM_TXT_LIMIT_YEAR_MIN			, 360	, NAM_LIMIT_YEAR		,aryMessage);
	if (getFormValue(PRM_PLT_TERM_TYPE) == "0") {
		// 1週間
		checkTimeValue(PRM_TXT_CAUTION_TERM_HOUR	,PRM_TXT_CAUTION_TERM_MIN	, 15	, NAM_CAUTION_TERM	,aryMessage);
		checkTimeValue(PRM_TXT_LIMIT_TERM_HOUR	,PRM_TXT_LIMIT_TERM_MIN	, 15	, NAM_LIMIT_TERM	,aryMessage);
	} else if (getFormValue(PRM_PLT_TERM_TYPE) == "1") {
		// 2週間
		checkTimeValue(PRM_TXT_CAUTION_TERM_HOUR	,PRM_TXT_CAUTION_TERM_MIN	, 27	, NAM_CAUTION_TERM	,aryMessage);
		checkTimeValue(PRM_TXT_LIMIT_TERM_HOUR	,PRM_TXT_LIMIT_TERM_MIN	, 27	, NAM_LIMIT_TERM	,aryMessage);
	} else if (getFormValue(PRM_PLT_TERM_TYPE) == "2") {
		// 4週間
		checkTimeValue(PRM_TXT_CAUTION_TERM_HOUR	,PRM_TXT_CAUTION_TERM_MIN	, 43	, NAM_CAUTION_TERM	,aryMessage);
		checkTimeValue(PRM_TXT_LIMIT_TERM_HOUR	,PRM_TXT_LIMIT_TERM_MIN	, 43	, NAM_LIMIT_TERM	,aryMessage);
	} else if (getFormValue(PRM_PLT_TERM_TYPE) == "3") {
		// 1ヵ月間
		checkTimeValue(PRM_TXT_CAUTION_TERM_HOUR	,PRM_TXT_CAUTION_TERM_MIN	, 45	, NAM_CAUTION_TERM	,aryMessage);
		checkTimeValue(PRM_TXT_LIMIT_TERM_HOUR	,PRM_TXT_LIMIT_TERM_MIN	, 45	, NAM_LIMIT_TERM	,aryMessage);
	} else if (getFormValue(PRM_PLT_TERM_TYPE) == "4") {
		// 2ヵ月間
		checkTimeValue(PRM_TXT_CAUTION_TERM_HOUR	,PRM_TXT_CAUTION_TERM_MIN	, 81	, NAM_CAUTION_TERM	,aryMessage);
		checkTimeValue(PRM_TXT_LIMIT_TERM_HOUR	,PRM_TXT_LIMIT_TERM_MIN	, 81	, NAM_LIMIT_TERM	,aryMessage);
	} else if (getFormValue(PRM_PLT_TERM_TYPE) == "5") {
		// 3ヵ月間
		checkTimeValue(PRM_TXT_CAUTION_TERM_HOUR	,PRM_TXT_CAUTION_TERM_MIN	, 120	, NAM_CAUTION_TERM	,aryMessage);
		checkTimeValue(PRM_TXT_LIMIT_TERM_HOUR	,PRM_TXT_LIMIT_TERM_MIN	, 120	, NAM_LIMIT_TERM	,aryMessage);
	}
	checkRate(PRM_TXT_RATE_TERM, NAM_RATE_TERM, aryMessage);
	checkRate(PRM_TXT_RATE_YEAR, NAM_RATE_YEAR, aryMessage);
	
	checkCautionTime(PRM_TXT_CAUTION_DAY_HOUR, PRM_TXT_CAUTION_DAY_MIN, PRM_TXT_LIMIT_DAY_HOUR, PRM_TXT_LIMIT_DAY_MIN, NAM_CAUTION_DAY, NAM_LIMIT_DAY, aryMessage);
	checkCautionTime(PRM_TXT_CAUTION_TERM_HOUR, PRM_TXT_CAUTION_TERM_MIN, PRM_TXT_LIMIT_TERM_HOUR, PRM_TXT_LIMIT_TERM_MIN, NAM_CAUTION_TERM, NAM_LIMIT_TERM, aryMessage);
	checkCautionTime(PRM_TXT_CAUTION_YEAR_HOUR, PRM_TXT_CAUTION_YEAR_MIN, PRM_TXT_LIMIT_YEAR_HOUR, PRM_TXT_LIMIT_YEAR_MIN, NAM_CAUTION_YEAR, NAM_LIMIT_YEAR, aryMessage);
	*/
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}

}

function setLimitTermTime() {
	var termType = getFormValue(PRM_PLT_TERM_TYPE);
	if (termType == "0") {
		setFormValue(PRM_TXT_LIMIT_TERM_HOUR, "15");
	} else if (termType == "1") {
		setFormValue(PRM_TXT_LIMIT_TERM_HOUR, "27");
	} else if (termType == "2") {
		setFormValue(PRM_TXT_LIMIT_TERM_HOUR, "43");
	} else if (termType == "3") {
		setFormValue(PRM_TXT_LIMIT_TERM_HOUR, "45");
	} else if (termType == "4") {
		setFormValue(PRM_TXT_LIMIT_TERM_HOUR, "81");
	} else if (termType == "5") {
		setFormValue(PRM_TXT_LIMIT_TERM_HOUR, "120");
	}
}

function checkRate(target, rep, aryMessage){
	var rate = 125;
	if (getFormValue(target) < 125) {
		setBgColor(target, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC2026", rep + "," + rate));
	}
}

function checkCautionTime(hour, min, hour2, min2, rep, rep2, aryMessage){
	var cautionTime = getFormValue(hour) * 60 + parseInt(getFormValue(min));
	var limitTime = getFormValue(hour2) * 60 + parseInt(getFormValue(min2));
	if (limitTime < cautionTime) {
		setBgColor(hour, COLOR_FIELD_ERROR);
		setBgColor(min, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC2028", rep + "," + rep2));
	}
}
