/*
 * 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 Affero General Public License
 * as published by the Free Software Foundation, either version 3
 * 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * 登録/削除確認メッセージ。
 */
var MSG_DIFFERENCE_REQUEST_CHECK	= "TMW0230";
var MSG_HOLIDAY_REQUIRED_CHECK		= "TMW0275";
var MSG_HOLIDAY_WARNING_CHECK		= "TMW0276";

/**
 * 画面読込時追加処理
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function onLoadExtra() {
	setOnChangeHandler("pltEditHolidayType", checkEditHolidayType);
	checkEditHolidayType();
	setOnChangeHandler("pltSearchHolidayType", checkSearchHolidayType);
	checkSearchHolidayType();
	var holidayType = getFormValue("pltEditHolidayType");
	if(holidayType == 1){
		setOnChangeHandler("pltEditHolidayRange1", checkEditHolidayRange1);
		checkEditHolidayRange1();
	}
	setOnChangeHandler("pltEditStatusSpecial", changeHolidayType);
	setOnChangeHandler("pltEditSpecialOther", changeHolidayType);

	// 未申請
	if (jsStatusApplication == "Unclaimed"){
		setDisabled("btnDelete", true);
	}
	// 未承認
	if (jsStatusApplication == "Unauthorized"){
		setDisabled("btnRegist", true);
		setDisabled("btnDraft", true);
		setDisabled("btnDelete", true);
	}
	// 有効日(編集)モード確認
	if (modeActivateDate == MODE_ACTIVATE_DATE_FIXED) {
		// 有効日編集不可
		setDisabled("pltEditStartYear", true);
		setDisabled("pltEditStartMonth", true);
		setDisabled("pltEditStartDay", true);
		setDisabled("pltEditEndYear", true);
		setDisabled("pltEditEndMonth", true);
		setDisabled("pltEditEndDay", true);
		// 休暇種別編集可
		setDisabled("pltEditHolidayType", false);
		setDisabled("pltEditStatusWithPay", false);
		setDisabled("pltEditStatusSpecial", false);
		setDisabled("pltEditSpecialOther", false);
		setDisabled("pltEditSpecialAbsence", false);
		// 登録ボタン利用可
		setDisabled("btnRegist", false);
		setDisabled("btnDraft", false);
		var startDay = getFormValue("pltEditStartDay");
		var endDay = getFormValue("pltEditEndDay");
		if( startDay != endDay ){
//			setFormValue("pltEditHolidayRange1", 1);
			setDisabled("pltEditHolidayRange1", true);
//			setFormValue("pltEditHolidayRange2", 1);
			setDisabled("pltEditHolidayRange2", true);
		} else {
			setDisabled("pltEditHolidayRange1", false);
			setDisabled("pltEditHolidayRange2", false);
		}
	} else {
		// 有効日編集可
		setDisabled("pltEditStartYear", false);
		setDisabled("pltEditStartMonth", false);
		setDisabled("pltEditStartDay", false);
		setDisabled("pltEditEndYear", false);
		setDisabled("pltEditEndMonth", false);
		setDisabled("pltEditEndDay", false);
		// 休暇種別編集不可
		setDisabled("pltEditHolidayType", true);
		setDisabled("pltEditStatusWithPay", true);
		setDisabled("pltEditStatusSpecial", true);
		setDisabled("pltEditSpecialOther", true);
		setDisabled("pltEditSpecialAbsence", true);
		setDisabled("pltEditHolidayRange1", true);
		setDisabled("pltEditHolidayRange2", true);
		// 登録ボタン利用不可
		setDisabled("btnRegist", true);
		setDisabled("btnDraft", true);
	}
	// 有効日(編集)モード確認
	if (jsSearchModeActivateDate  == MODE_ACTIVATE_DATE_FIXED) {
		// 有効日編集不可
		setDisabled("pltSearchYear", true);
		setDisabled("pltSearchMonth", true);
		// 休暇種別編集可
		setDisabled("pltSearchHolidayType", false);
		setDisabled("pltSearchStatus", false);
		setDisabled("pltSearchStatusWithPay", false);
		setDisabled("pltSearchStatusSpecial", false);
		setDisabled("pltSearchSpecialOther", false);
		setDisabled("pltSearchSpecialAbsence", false);
		setDisabled("pltSearchHolidayRange", false);
		setDisabled("pltSearchHolidayRange1", false);
		setDisabled("pltSearchHolidayRange2", false);
		// 登録ボタン利用可
		setDisabled("btnSearch", false);
	} else {
		// 有効日編集可
		setDisabled("pltSearchYear", false);
		setDisabled("pltSearchMonth", false);
		// 休暇種別編集不可
		setDisabled("pltSearchHolidayType", true);
		setDisabled("pltSearchStatus", true);
		setDisabled("pltSearchStatusWithPay", true);
		setDisabled("pltSearchStatusSpecial", true);
		setDisabled("pltSearchSpecialOther", true);
		setDisabled("pltSearchSpecialAbsence", true);
		setDisabled("pltSearchHolidayRange", true);
		setDisabled("pltSearchHolidayRange1", true);
		setDisabled("pltSearchHolidayRange2", true);
		// 登録ボタン利用不可
		setDisabled("btnSearch", true);
	}
	if( modeCardEdit == MODE_CARD_EDIT_EDIT ){
		// 有効日編集不可
		setDisabled("pltEditStartYear", true);
		setDisabled("pltEditStartMonth", true);
		setDisabled("pltEditStartDay", true);
		setDisabled("pltEditEndYear", true);
		setDisabled("pltEditEndMonth", true);
		setDisabled("pltEditEndDay", true);
		// 休暇種別編集不可
		setDisabled("pltEditHolidayType", true);
		setDisabled("pltEditStatusWithPay", true);
		setDisabled("pltEditStatusSpecial", true);
		setDisabled("pltEditSpecialOther", true);
		setDisabled("pltEditSpecialAbsence", true);
		setDisabled("pltEditHolidayRange1", true);
		setDisabled("pltEditHolidayRange2", true);
		// 時間指定プルダウン利用不可
		setDisabled("pltEditStartHour", true);
		setDisabled("pltEditStartMinute", true);
		setDisabled("pltEditEndHour", true);
		setDisabled("pltEditEndMinute", true);
		// 休暇理由編集不可
		setDisabled("txtEditRequestReason", true);
		// 日付決定ボタン利用不可
		setDisabled("btnHolidayDate", true);
		// 承認系ボタン利用可
		setDisabled("btnRegist", false);
		setDisabled("btnDraft", false);
		setDisabled("btnDelete", false);
	}
}

/**
 * 付与区分のチェック処理
 * @param 無し
 * @return 無し
 */
function checkEditHolidayType() {
	var holidayType = getFormValue("pltEditHolidayType");
	// 時間指定プルダウン利用不可
	setDisabled("pltEditStartHour", true);
	setDisabled("pltEditStartMinute", true);
	setDisabled("pltEditEndHour", true);
	setDisabled("pltEditEndMinute", true);
	if(holidayType == 1){
		setObjectVisibility("pltEditStatusWithPay", true);
		setObjectVisibility("pltEditStatusSpecial", false);
		setObjectVisibility("pltEditSpecialOther", false);
		setObjectVisibility("pltEditSpecialAbsence", false);
		setObjectVisibility("pltEditHolidayRange1", true);
		setObjectVisibility("pltEditHolidayRange2", false);
		var holidayRange1 = getFormValue("pltEditHolidayRange1");
		if(holidayRange1 == 4){
			// 時間指定プルダウン利用可
			setDisabled("pltEditStartHour", false);
			setDisabled("pltEditStartMinute", false);
			setDisabled("pltEditEndHour", false);
			setDisabled("pltEditEndMinute", false);
		}
	} else if(holidayType == 2){
		setObjectVisibility("pltEditStatusWithPay", false);
		setObjectVisibility("pltEditStatusSpecial", true);
		setObjectVisibility("pltEditSpecialOther", false);
		setObjectVisibility("pltEditSpecialAbsence", false);
		setObjectVisibility("pltEditHolidayRange1", false);
		setObjectVisibility("pltEditHolidayRange2", true);
	} else if(holidayType == 3){
		setObjectVisibility("pltEditStatusWithPay", false);
		setObjectVisibility("pltEditStatusSpecial", false);
		setObjectVisibility("pltEditSpecialOther", true);
		setObjectVisibility("pltEditSpecialAbsence", false);
		setObjectVisibility("pltEditHolidayRange1", false);
		setObjectVisibility("pltEditHolidayRange2", true);
	} else if(holidayType == 4){
		setObjectVisibility("pltEditStatusWithPay", false);
		setObjectVisibility("pltEditStatusSpecial", false);
		setObjectVisibility("pltEditSpecialOther", false);
		setObjectVisibility("pltEditSpecialAbsence", true);
		setObjectVisibility("pltEditHolidayRange1", false);
		setObjectVisibility("pltEditHolidayRange2", true);
	}
	changeHolidayType();
}

/**
 * 付与区分のチェック処理
 * @param 無し
 * @return 無し
 */
function checkSearchHolidayType() {
	var holidayType = getFormValue("pltSearchHolidayType");
	if(holidayType == 1){
		setObjectVisibility("pltSearchStatus", false);
		setObjectVisibility("pltSearchStatusWithPay", true);
		setObjectVisibility("pltSearchStatusSpecial", false);
		setObjectVisibility("pltSearchSpecialOther", false);
		setObjectVisibility("pltSearchSpecialAbsence", false);
		setObjectVisibility("pltSearchHolidayRange", false);
		setObjectVisibility("pltSearchHolidayRange1", true);
		setObjectVisibility("pltSearchHolidayRange2", false);
	} else if(holidayType == 2){
		setObjectVisibility("pltSearchStatus", false);
		setObjectVisibility("pltSearchStatusWithPay", false);
		setObjectVisibility("pltSearchStatusSpecial", true);
		setObjectVisibility("pltSearchSpecialOther", false);
		setObjectVisibility("pltSearchSpecialAbsence", false);
		setObjectVisibility("pltSearchHolidayRange", false);
		setObjectVisibility("pltSearchHolidayRange1", false);
		setObjectVisibility("pltSearchHolidayRange2", true);
	} else if(holidayType == 3){
		setObjectVisibility("pltSearchStatus", false);
		setObjectVisibility("pltSearchStatusWithPay", false);
		setObjectVisibility("pltSearchStatusSpecial", false);
		setObjectVisibility("pltSearchSpecialOther", true);
		setObjectVisibility("pltSearchSpecialAbsence", false);
		setObjectVisibility("pltSearchHolidayRange", false);
		setObjectVisibility("pltSearchHolidayRange1", false);
		setObjectVisibility("pltSearchHolidayRange2", true);
	} else if(holidayType == 4){
		setObjectVisibility("pltSearchStatus", false);
		setObjectVisibility("pltSearchStatusWithPay", false);
		setObjectVisibility("pltSearchStatusSpecial", false);
		setObjectVisibility("pltSearchSpecialOther", false);
		setObjectVisibility("pltSearchSpecialAbsence", true);
		setObjectVisibility("pltSearchHolidayRange", false);
		setObjectVisibility("pltSearchHolidayRange1", false);
		setObjectVisibility("pltSearchHolidayRange2", true);
	} else {
		setObjectVisibility("pltSearchStatus", true);
		setObjectVisibility("pltSearchStatusWithPay", false);
		setObjectVisibility("pltSearchStatusSpecial", false);
		setObjectVisibility("pltSearchSpecialOther", false);
		setObjectVisibility("pltSearchSpecialAbsence", false);
		setObjectVisibility("pltSearchHolidayRange", true);
		setObjectVisibility("pltSearchHolidayRange1", false);
		setObjectVisibility("pltSearchHolidayRange2", false);
		
	}
}

/**
 * 休暇種別(範囲)のチェック処理
 * @param 無し
 * @return 無し
 */
function checkEditHolidayRange1() {
	var holidayRange1 = getFormValue("pltEditHolidayRange1");
	if(holidayRange1 == 4){
		// 時間指定プルダウン利用可
		setDisabled("pltEditStartHour", false);
		setDisabled("pltEditStartMinute", false);
		setDisabled("pltEditEndHour", false);
		setDisabled("pltEditEndMinute", false);
	} else {
		// 時間指定プルダウン利用不可
		setDisabled("pltEditStartHour", true);
		setDisabled("pltEditStartMinute", true);
		setDisabled("pltEditEndHour", true);
		setDisabled("pltEditEndMinute", true);
	}
}

function changeHolidayType() {
	// リクエスト送信
	doSubmit(document.form, "TM1598");
}

/**
 * 追加チェックを行う。<br>
 * @param aryMessage エラーメッセージ格納配列
 * @param event イベント
 * @return 無し
 */
function checkExtra(aryMessage, event) {
	// チェックボックス必須確認
	checkBoxRequired("ckbSelect", aryMessage);
}

/**
 * オブジェクトの表示・非表示
 * @param テーブルid
 * @param  num
 * @return 無し
 * @throws 実行時例外
 */
function setObjectVisibility(target , isVisible) {
	var objTarget = getObject(target);
	if (objTarget == null) {
		return;
	}
	if (isVisible) {
		objTarget.style.display = "inline";
	} else {
		objTarget.style.display = "none";
	}
}

/**
 * 入力チェックを行った後、更新系確認メッセージを出し、リクエストを送信する。<br>
 * データ登録、更新時等に用いる。<br>
 * @param event          イベントオブジェクト
 * @param validateTarget 入力チェック対象(null：チェックを行わない、""：全体をチェック)
 * @param objExtraCheck  追加チェック関数オブジェクト(null：追加チェック無し)
 * @param cmd            コマンド
 * @return 無し
 */
function submitDiffcheckRegist(event, validateTarget, objExtraCheck, cmd) {
	var differenceMessage = "";
	if( jsModeDifferenceRequest1 == "on" ){
		var holidayType = getFormValue("pltEditHolidayType");
		var flg = 0;
		if(holidayType == 1){
			if ( getFormValue("pltEditHolidayRange1") == "2" || getFormValue("pltEditHolidayRange1") == "3" ) {
				flg = 1;
			}
		} else {
			if ( getFormValue("pltEditHolidayRange2") == "2" || getFormValue("pltEditHolidayRange2") == "3" ) {
				flg = 1;
			}
		}
		if( flg == 1 ){
			differenceMessage = getMessage(MSG_DIFFERENCE_REQUEST_CHECK, jsModeRequestDate1);
		}
	}
	var continueMessage = "";
	if (jsHolidayContinue == "0" || jsHolidayContinue == "1") {
		// 申請開始日
		var startDate = new Date(getFormValue("pltEditStartYear"), getFormValue("pltEditStartMonth") - 1, getFormValue("pltEditStartDay"));
		// 申請終了日
		var endDate = new Date(getFormValue("pltEditEndYear"), getFormValue("pltEditEndMonth") - 1, getFormValue("pltEditEndDay"));
		// 申請日数
		var requestDay = ((endDate - startDate) / 86400000) + 1;
		if (requestDay < jsHolidayRemainDay) {
			if (jsHolidayContinue == "0") {
				// 必須
				continueMessage = getMessage(MSG_HOLIDAY_REQUIRED_CHECK, "");
			} else if (jsHolidayContinue == "1") {
				// 警告
				continueMessage = getMessage(MSG_HOLIDAY_WARNING_CHECK, "");
			}
		}
	}
	if (differenceMessage.length != 0 || continueMessage.length != 0) {
		// 入力チェック
		if (validate(validateTarget, objExtraCheck, event)) {
			// 更新系確認メッセージ
			if (confirm(differenceMessage + "\n" + continueMessage)) {
				// リクエスト送信
				doSubmit(document.form, cmd);
			}
		}
	} else {
		submitRegist(event, validateTarget, objExtraCheck, cmd);
	}
}
/**
 * 追加チェックを行う。<br>
 * @param aryMessage エラーメッセージ格納配列
 * @param event イベント
 * @return 無し
 */
function checkDateExtra(aryMessage, event) {
	checkDate("pltEditStartYear", "pltEditStartMonth", "pltEditStartDay", aryMessage);
	checkDate("pltEditEndYear", "pltEditEndMonth", "pltEditEndDay", aryMessage);	
}
