/*
 * 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);
	// フォーカス設定
	setFocus(PRM_PLT_HOLIDAY_YEAR);
	// 項目長設定
	setMaxLength(PRM_TXT_CAUSE_COMMENT, LEN_COMMENT);
	// 新規登録、
	if (MODE == 0) {
		setVisibility("btnCancel",false);
	}
	// 更新可能
	if (MODE == 1) {
		setReadOnly(PRM_PLT_HOLIDAY_YEAR, true);
		setReadOnly(PRM_PLT_HOLIDAY_MONTH, true);
		setReadOnly(PRM_PLT_HOLIDAY_DAY, true);
	}
	// 申請中
	if (MODE == 2) {
		setVisibility("btnDraft",false);
		setVisibility("btnRequest",false);
	}
	// 更新不可
	if (MODE == 3) {
		setVisibility("btnDraft",false);
		setVisibility("btnRequest",false);
		setVisibility("btnCancel",false);
	}
	// 差戻更新
	if (MODE == 4) {
		setReadOnly(PRM_PLT_HOLIDAY_YEAR, true);
		setReadOnly(PRM_PLT_HOLIDAY_MONTH, true);
		setReadOnly(PRM_PLT_HOLIDAY_DAY, true);
		setVisibility("btnDraft",false);
	}
	viewDivType();
	viewDivTime();
}

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

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();
	// 日付のチェック
	checkDate(PRM_PLT_HOLIDAY_YEAR, PRM_PLT_HOLIDAY_MONTH, PRM_PLT_HOLIDAY_DAY, NAM_HOLIDAY_DATE, aryMessage);
	// 時間年休
	if (USE_HOUR_HOLIDAY && getFormValue(PRM_PLT_TYPE) == "3") {
		// 改正労働基準法施行日
		checkReformDate(aryMessage);
		// 入力時刻
		checkTime(aryMessage);
	}
	// 充当日
	if (getFormValue(PRM_PLT_SUBSTITUTE_YEAR) != "" 
			|| getFormValue(PRM_PLT_SUBSTITUTE_MONTH) != "" 
			|| getFormValue(PRM_PLT_SUBSTITUTE_DAY) != ""
	) {
		checkDate(PRM_PLT_SUBSTITUTE_YEAR, PRM_PLT_SUBSTITUTE_MONTH, PRM_PLT_SUBSTITUTE_DAY, NAM_SUBSTITUTE_DATE, aryMessage);
	}
	checkLength(PRM_TXT_CAUSE_COMMENT, LEN_COMMENT, NAM_CAUSE_COMMENT, aryMessage)

	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
}

function checkTime(aryMessage) {
	var starthour = getFormValue(PRM_PLT_START_HOUR);
	var startmin = getFormValue(PRM_PLT_START_MIN);
	var endhour = getFormValue(PRM_PLT_END_HOUR);
	var endmin = getFormValue(PRM_PLT_END_MIN);
	var starttime = parseInt(starthour) *60 + parseInt(startmin);
	var endtime = parseInt(endhour) *60 + parseInt(endmin);
	
	if (starttime < 0) {
		if (aryMessage.length == 0) {
			setFocus(PRM_PLT_START_HOUR);
			setMessage("WC0007", NAM_START_TIME);
		}
		setBgColor(PRM_PLT_START_HOUR, COLOR_FIELD_ERROR);
		setBgColor(PRM_PLT_START_MIN, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0007", NAM_START_TIME));
	}
	
	if (endtime < 0) {
		if (aryMessage.length == 0) {
			setFocus(PRM_PLT_END_HOUR);
			setMessage("WC0007", NAM_END_TIME);
		}
		setBgColor(PRM_PLT_END_HOUR, COLOR_FIELD_ERROR);
		setBgColor(PRM_PLT_END_MIN, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0007", NAM_END_TIME));
	}
	
	var differencetime = endtime - starttime;
	
	if (differencetime <= 0 || differencetime != HOUR_UNIT * 60) {
		setBgColor(PRM_PLT_END_HOUR, COLOR_FIELD_ERROR);
		setBgColor(PRM_PLT_END_MIN, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0007", NAM_END_TIME));
	}
	
}

function viewDivType() {
	var reason = getFormValue(PRM_PLT_REASON);
	if (reason == "0") {
		var type = getFormValue(PRM_PLT_TYPE)
		setOption(PRM_PLT_TYPE, ARY_PLT_TYPE);
		setVisibility("divType", true);
		setFormValue(PRM_PLT_TYPE, type);
		viewDivTime();
	} else if (reason == "sub") {
		if (USE_HOUR_HOLIDAY && USE_SUBSTITUTE_HOLIDAY) {
			setOption(PRM_PLT_TYPE, ARY_PLT_TYPE.slice(0, 3));
		}
		setVisibility("divType", true);
		setVisibility("divTime", false);
	} else {
		setVisibility("divType", false);
		setVisibility("divTime", false);
	}
}

function viewDivTime() {
	var type = getFormValue(PRM_PLT_TYPE);
	if (type == "3") {
		setVisibility("divTime", true);
	} else {
		setVisibility("divTime", false);
	}
}

function checkReformDate(aryMessage) {
	yyyy = getFormValue(PRM_PLT_HOLIDAY_YEAR);
	mm = getFormValue(PRM_PLT_HOLIDAY_MONTH);
	dd = getFormValue(PRM_PLT_HOLIDAY_DAY);
	
	var reform =  new Date(2010, 4 - 1, 1);
	var date = new Date(yyyy, mm - 1, dd);
	if (reform.getTime() > date.getTime()) {
		setBgColor(PRM_PLT_HOLIDAY_YEAR, COLOR_FIELD_ERROR);
		setBgColor(PRM_PLT_HOLIDAY_MONTH, COLOR_FIELD_ERROR);
		setBgColor(PRM_PLT_HOLIDAY_DAY, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0023", NAM_HOLIDAY_DATE + ',' + reform.getFullYear()  + "年" + (reform.getMonth() + 1) + "月" + reform.getDate() + "日"));
	}
}

function changeEndTime() {
	var startTime = parseInt(getFormValue(PRM_PLT_START_HOUR)) * 60 + parseInt(getFormValue(PRM_PLT_START_MIN));
	var endTime = startTime + (HOUR_UNIT * 60);
	if (endTime >= 24 * 60) {
		endTime = 24 * 60;
	}
	setFormValue(PRM_PLT_END_HOUR, Math.floor(endTime / 60));
	setFormValue(PRM_PLT_END_MIN, endTime % 60);
}
