/*
 * 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.
 */
var KEY_SEPARATOR      = ",";

/**
 * 画面読込時追加処理
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function onLoadExtra() {
	// 項目背景色設定
	setFieldsBgColor(ARY_FIELDS_ID,     COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_ERR_FIELDS_ID, COLOR_FIELD_ERROR);
	// 項目長設定
	setMaxLength(PRM_TXT_START_HOUR				, LEN_HOUR);
	setMaxLength(PRM_TXT_START_MIN				, LEN_MIN);
	setMaxLength(PRM_TXT_LATE_HOUR				, LEN_HOUR);
	setMaxLength(PRM_TXT_LATE_MIN				, LEN_MIN);
	setMaxLength(PRM_TXT_LATE_COMMENT			, LEN_COMMENT);
	setMaxLength(PRM_TXT_QUIT_HOUR				, LEN_HOUR);
	setMaxLength(PRM_TXT_QUIT_MIN				, LEN_MIN);
	setMaxLength(PRM_TXT_WORK_HOUR				, LEN_HOUR);
	setMaxLength(PRM_TXT_WORK_MIN				, LEN_MIN);
	setMaxLength(PRM_TXT_BREAK_HOUR				, LEN_HOUR);
	setMaxLength(PRM_TXT_BREAK_MIN				, LEN_MIN);
	setMaxLength(PRM_TXT_LEAVE_EARLY_HOUR		, LEN_HOUR);
	setMaxLength(PRM_TXT_LEAVE_EARLY_MIN		, LEN_MIN);
	setMaxLength(PRM_TXT_LEAVE_EARLY_COMMENT	, LEN_COMMENT);
	setMaxLength(PRM_TXT_OVER_HOUR				, LEN_HOUR);
	setMaxLength(PRM_TXT_OVER_MIN				, LEN_MIN);
	setMaxLength(PRM_TXT_WORK_ON_HOLIDAY_HOUR	, LEN_HOUR);
	setMaxLength(PRM_TXT_WORK_ON_HOLIDAY_MIN	, LEN_MIN);
	setMaxLength(PRM_TXT_LATE_NIGHT_HOUR		, LEN_HOUR);
	setMaxLength(PRM_TXT_LATE_NIGHT_MIN			, LEN_MIN);
	
	// フォーカス設定(EDITの場合)
	setFocus(PRM_TXT_START_HOUR);
	// モードタイトル
	setModeLabel();
/*	switch (MODE) {
		case 0:
			setInnerHtml('spnMode', NAM_MODE_INSERT);
			break;
		case 1:
			setInnerHtml('spnMode', NAM_MODE_EDIT);
			break;
		case 2:
			setInnerHtml('spnMode', NAM_MODE_APPROVED);
			setColor('spnMode', COLOR_MSG_WARNING);
			break;		
		default:
	}
 */	
	// 登録ボタン利用可否
	if (MODE == 2) {
		setVisibility("btnRegist", false);
		setWidth("btnRegist", 0);
		setVisibility("btnDelete", false);
		setWidth("btnDelete", 0);
		setVisibility("btnCalc", false);
	}
	// 入力ボタン利用可否
	setVisibility("btnStart", false);
	setVisibility("btnQuit", false);	
	setVisibility("btnHoliday", false);
	// 下書き
	if (!isDraft) {
		setVisibility("btnDraft", false);
	}
	// 時間外警告
	setOverTimeBgColor();
}

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

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

	// 入力チェック
	var aryMessage = new Array();
	
	checkRequired(PRM_TXT_START_HOUR	, NAM_START_TIME_HOUR	, aryMessage);
	checkRequired(PRM_TXT_START_MIN		, NAM_START_TIME_MIN	, aryMessage);
	checkRequired(PRM_TXT_LATE_HOUR		, NAM_LATE_TIME_HOUR	, aryMessage);
	checkRequired(PRM_TXT_LATE_MIN		, NAM_LATE_TIME_MIN		, aryMessage);
	
	checkRequired(PRM_TXT_QUIT_HOUR				, NAM_QUIT_TIME_HOUR		, aryMessage);
	checkRequired(PRM_TXT_QUIT_MIN				, NAM_QUIT_TIME_MIN			, aryMessage);
	checkRequired(PRM_TXT_WORK_HOUR				, NAM_WORKING_TIME_HOUR		, aryMessage);
	checkRequired(PRM_TXT_WORK_MIN				, NAM_WORKING_TIME_MIN		, aryMessage);
	checkRequired(PRM_TXT_BREAK_HOUR			, NAM_INTERVAL_TIME_HOUR	, aryMessage);
	checkRequired(PRM_TXT_BREAK_MIN				, NAM_INTERVAL_TIME_MIN		, aryMessage);
	checkRequired(PRM_TXT_LEAVE_EARLY_HOUR		, NAM_LEAVE_EARLY_TIME_HOUR	, aryMessage);
	checkRequired(PRM_TXT_LEAVE_EARLY_MIN		, NAM_LEAVE_EARLY_TIME_MIN	, aryMessage);
	checkRequired(PRM_TXT_OVER_HOUR				, NAM_OVER_TIME_HOUR		, aryMessage);
	checkRequired(PRM_TXT_OVER_MIN				, NAM_OVER_TIME_MIN			, aryMessage);
	checkRequired(PRM_TXT_WORK_ON_HOLIDAY_HOUR	, NAM_WORK_ON_HOLIDAY_HOUR	, aryMessage);
	checkRequired(PRM_TXT_WORK_ON_HOLIDAY_MIN	, NAM_WORK_ON_HOLIDAY_MIN	, aryMessage);
	checkRequired(PRM_TXT_LATE_NIGHT_HOUR		, NAM_LATE_NIGHT_HOUR		, aryMessage);
	checkRequired(PRM_TXT_LATE_NIGHT_MIN		, NAM_LATE_NIGHT_MIN		, aryMessage);
	
	checkNumber  (PRM_TXT_START_HOUR	, NAM_START_TIME_HOUR	, aryMessage);
	checkNumber  (PRM_TXT_START_MIN		, NAM_START_TIME_MIN	, aryMessage);
	checkNumber  (PRM_TXT_LATE_HOUR		, NAM_LATE_TIME_HOUR	, aryMessage);
	checkNumber  (PRM_TXT_LATE_MIN		, NAM_LATE_TIME_MIN		, aryMessage);
	
	checkNumber  (PRM_TXT_QUIT_HOUR				, NAM_QUIT_TIME_HOUR		, aryMessage);
	checkNumber  (PRM_TXT_QUIT_MIN				, NAM_QUIT_TIME_MIN			, aryMessage);
	checkNumber  (PRM_TXT_WORK_HOUR				, NAM_WORKING_TIME_HOUR		, aryMessage);
	checkNumber  (PRM_TXT_WORK_MIN				, NAM_WORKING_TIME_MIN		, aryMessage);
	checkNumber  (PRM_TXT_BREAK_HOUR			, NAM_INTERVAL_TIME_HOUR	, aryMessage);
	checkNumber  (PRM_TXT_BREAK_MIN				, NAM_INTERVAL_TIME_MIN		, aryMessage);
	checkNumber  (PRM_TXT_LEAVE_EARLY_HOUR		, NAM_LEAVE_EARLY_TIME_HOUR	, aryMessage);
	checkNumber  (PRM_TXT_LEAVE_EARLY_MIN		, NAM_LEAVE_EARLY_TIME_MIN	, aryMessage);
	checkNumber  (PRM_TXT_OVER_HOUR				, NAM_OVER_TIME_HOUR		, aryMessage);
	checkNumber  (PRM_TXT_OVER_MIN				, NAM_OVER_TIME_MIN			, aryMessage);
	checkNumber  (PRM_TXT_WORK_ON_HOLIDAY_HOUR	, NAM_WORK_ON_HOLIDAY_HOUR	, aryMessage);
	checkNumber  (PRM_TXT_WORK_ON_HOLIDAY_MIN	, NAM_WORK_ON_HOLIDAY_MIN	, aryMessage);
	checkNumber  (PRM_TXT_LATE_NIGHT_HOUR		, NAM_LATE_NIGHT_HOUR		, aryMessage);
	checkNumber  (PRM_TXT_LATE_NIGHT_MIN		, NAM_LATE_NIGHT_MIN		, aryMessage);

	//時間、分の合計値チェック
	checkTimeValue(PRM_TXT_START_HOUR			,PRM_TXT_START_MIN			, 24 , NAM_START_TIME			,aryMessage);
	checkTimeValue(PRM_TXT_QUIT_HOUR			,PRM_TXT_QUIT_MIN			, 48 , NAM_QUIT_TIME			,aryMessage);
	checkTimeValue(PRM_TXT_WORK_HOUR			,PRM_TXT_WORK_MIN			, 48 , NAM_WORKING_TIME			,aryMessage);
	checkTimeValue(PRM_TXT_LATE_HOUR			,PRM_TXT_LATE_MIN			, 24 , NAM_LATE_TIME			,aryMessage);
	checkTimeValue(PRM_TXT_BREAK_HOUR			,PRM_TXT_BREAK_MIN			, 24 , NAM_INTERVAL_TIME		,aryMessage);
	checkTimeValue(PRM_TXT_LEAVE_EARLY_HOUR		,PRM_TXT_LEAVE_EARLY_MIN	, 48 , NAM_LEAVE_EARLY_TIME		,aryMessage);
	checkTimeValue(PRM_TXT_OVER_HOUR			,PRM_TXT_OVER_MIN			, 48 , NAM_OVER_TIME			,aryMessage);
	checkTimeValue(PRM_TXT_WORK_ON_HOLIDAY_HOUR	,PRM_TXT_WORK_ON_HOLIDAY_MIN, 48 , NAM_WORK_ON_HOLIDAY		,aryMessage);
	checkTimeValue(PRM_TXT_LATE_NIGHT_HOUR		,PRM_TXT_LATE_NIGHT_MIN		, 10 , NAM_LATE_NIGHT			,aryMessage);
		
	var start 	= -1;
	var quit 	= -1;
	if (checkNumberNoMsg(PRM_TXT_START_HOUR) && checkNumberNoMsg(PRM_TXT_START_MIN)) {
		start 	= getFormValue(PRM_TXT_START_HOUR) * 60 + eval(getFormValue(PRM_TXT_START_MIN));
	}
	if (checkNumberNoMsg(PRM_TXT_QUIT_HOUR) && checkNumberNoMsg(PRM_TXT_QUIT_MIN)) {
		quit 	= getFormValue(PRM_TXT_QUIT_HOUR) * 60 + eval(getFormValue(PRM_TXT_QUIT_MIN));
	}
	
	if (start > -1 && quit > -1) {
		if (quit < start) {
			setBgColor(PRM_TXT_START_HOUR, COLOR_FIELD_ERROR);
			setBgColor(PRM_TXT_START_MIN, COLOR_FIELD_ERROR);
			setBgColor(PRM_TXT_QUIT_HOUR, COLOR_FIELD_ERROR);
			setBgColor(PRM_TXT_QUIT_MIN, COLOR_FIELD_ERROR);
			aryMessage.push(getMessage("WC2028", NAM_START_TIME + "," + NAM_QUIT_TIME));
		}
	}
	
	var breakHour = getFormValue(PRM_TXT_BREAK_HOUR);
	var breakMin  = getFormValue(PRM_TXT_BREAK_MIN);
	
	if (start > -1 && quit > -1 && checkNumberNoMsg(PRM_TXT_BREAK_HOUR) && checkNumberNoMsg(PRM_TXT_BREAK_MIN)) {
		if ((quit - start) < (breakHour * 60 + eval(breakMin))) {
			setBgColor(PRM_TXT_BREAK_HOUR, COLOR_FIELD_ERROR);
			setBgColor(PRM_TXT_BREAK_MIN, COLOR_FIELD_ERROR);
			aryMessage.push(getMessage("WC2028", NAM_INTERVAL_TIME + "," + NAM_WORKING_TIME));
		}
	}	
	
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
}
