/*
 * 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_HOLIDAY_YEAR , LEN_YEAR);
	setMaxLength(PRM_TXT_HOLIDAY_NAME , LEN_NAME);
	// 登録/更新判定
	if (MODE == 0) {
		// フォーカス設定
		setFocus(PRM_TXT_HOLIDAY_YEAR);
	} else if (MODE == 1 || MODE == 2) {
		// 読取専用設定及びフォーカス設定(UPDATEの場合)
		setReadOnly(PRM_TXT_HOLIDAY_YEAR	, true);
		setReadOnly(PRM_PLT_HOLIDAY_MONTH	, true);
		setReadOnly(PRM_PLT_HOLIDAY_DAY		, true);
		// フォーカス設定
		setFocus(PRM_TXT_HOLIDAY_NAME);
	}
	// 削除ボタン利用可否
	if (MODE != 2) {
		setObjectVisibility("btnDelete", false);
		setObjectVisibility("btnNewData", false);	
		setWidth("btnDelete", 0);
		setWidth("btnNewData", 0);
	}
}

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

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();
	
	checkRequired(PRM_TXT_HOLIDAY_NAME	, NAM_HOLIDAY_NAME , aryMessage);
	
	if (getFormValue(PRM_TXT_HOLIDAY_YEAR) != "") {
		checkNumber(PRM_TXT_HOLIDAY_YEAR	, NAM_HOLIDAY_YEAR , aryMessage);	
		checkRegLength(PRM_TXT_HOLIDAY_YEAR	, LEN_YEAR	, NAM_HOLIDAY_YEAR, aryMessage);
		checkYear(PRM_TXT_HOLIDAY_YEAR	, NAM_HOLIDAY_YEAR , aryMessage);
		
		checkDate(PRM_TXT_HOLIDAY_YEAR, PRM_PLT_HOLIDAY_MONTH, PRM_PLT_HOLIDAY_DAY, NAM_HOLIDAY_DATE, aryMessage);
	} else {
		var date = new Date();		
		checkDate(date.getFullYear(), PRM_PLT_HOLIDAY_MONTH, PRM_PLT_HOLIDAY_DAY, NAM_HOLIDAY_DATE, aryMessage);		
	}
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
}

function checkDate(yyyy, mm, dd,msg,aryMsg) {
	//年月日チェック
	dayMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	ValidFlag = true;
	yyyy2 = getFormValue(yyyy);
	mm2 = getFormValue(mm);
	dd2 = getFormValue(dd);
	if ((mm2 < 1) || (mm2 > 12)){
		setBgColor(yyyy, COLOR_FIELD_ERROR);
		aryMsg.push(msg + "の月は1--12を入力してください。");
	}
	if ((dd2 < 1) || (dd2 > dayMonth[mm2 - 1])){
		setBgColor(dd, COLOR_FIELD_ERROR);
		aryMsg.push(getMessage("WC0006", msg));
	}
	if (!chkUrudosi(yyyy2) && (mm2 == 2) && (dd2 == 29)){
		setBgColor(dd, COLOR_FIELD_ERROR);
		aryMsg.push(getMessage("WC0006", msg));
	}
}

function chkUrudosi(y) {
	if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
		return true;
	}
	return false;
}

function checkYear(target, rep, aryMessage) {
	var year = 2000;
	if (getFormValue(target) < year) {
		setBgColor(target, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC2026", rep + "," + year));
	}
}

