/*
 * 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);
	// 一覧背景色設定
	setTableColor("list");
	// 登録ボタン利用可否
	if (MODE == 0) {
		setVisibility("btnRegist", false);
	} else if (MODE == 1) {
		setInnerHtml('btnRegist', NAM_1ST_APPROVAL);
	} else if (MODE == 2) {
		setInnerHtml('btnRegist', NAM_2ND_APPROVAL);
	}
	checkPltYearMonth();
}

function doAllBoxChecked(check) {
	var id = document.getElementsByName(PRM_CKB_DATE);
	for(i = 0; i < id.length; i++) {
		id[i].checked = check;
	}
}

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

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();
	
	checkBoxRequired(PRM_CKB_DATE, NAM_APPROVED, aryMessage);
	
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
}

/**
 * 必須確認（チェックボックス）
 * @param objForm フォームオブジェクト
 * @param target     確認対象(StringあるいはObject)
 * @param rep        変換文字列
 * @param aryMessage エラーメッセージ格納配列
 * @return 無し
 * @throws 実行時例外
 */
function checkBoxRequired(target, rep, aryMessage) {
	// checkboxをチェック
	var cnt = 0;
	var check = document.getElementsByName(PRM_CKB_DATE);
	for(var i = 0; i < check.length; i++){
		if(check[i].checked)
			cnt++;
	}
	if (cnt == 0) {
		aryMessage.push(getMessage("WC2021", rep));
	}
}

function submitMonth(objForm, cmd, process) {

	var pltYear = getFormValue(PRM_PLT_YEAR);
	var pltMonth = getFormValue(PRM_PLT_MONTH);
	
	var year = eval(pltYear);
	var month = eval(pltMonth);
	
	var isExist = false;
	
	var aryYearOptions = getObject(PRM_PLT_YEAR).options;
	
	setDisabled("btnFormer", false);
	setDisabled("btnNext", false);
	
	if (process == "former") {
		if (month == 1) {
			year--;
			month = 12;
		} else {
			month--;
		}
		if (month == 12) {
			for ( var int = 0; int < aryYearOptions.length; int++) {
				var op = eval(aryYearOptions[int].value);
				if (op == year) {
					isExist = true;
					break;
				}
			}
		} else {
			isExist = true;
		}
		setDisabled("btnFormer", !isExist);
	} else if (process == "next") {
		if (month == 12) {
			year++;
			month = 1;
		} else {
			month++;
		}
		if (month == 1) {
			for ( var int = 0; int < aryYearOptions.length; int++) {
				var op = eval(aryYearOptions[int].value);
				if (op == year) {
					isExist = true;
					break;
				}
			}
		} else {
			isExist = true;
		}
		setDisabled("btnNext", !isExist);
	}
	if (!isExist) {
		year = eval(pltYear);
		month = eval(pltMonth);
	}
	setFormValue(PRM_PLT_YEAR, year);
	setFormValue(PRM_PLT_MONTH, month);
	doSubmit(document.form, cmd);
}

function checkPltYearMonth() {
	
	var month = getFormValue(PRM_PLT_MONTH);
	var year = getFormValue(PRM_PLT_YEAR);
	var aryYearOptions = getObject(PRM_PLT_YEAR).options;
	var isNon = true;
	if (month == "1") {
		for ( var int = 0; int < aryYearOptions.length; int++) {
			var op = eval(aryYearOptions[int].value);
			if (op == eval(year) -1) {
				isNon = false;
				break;
			}
		}
		setDisabled("btnFormer", isNon);
	} else if (month == "12") {
		for ( var int = 0; int < aryYearOptions.length; int++) {
			var op = eval(aryYearOptions[int].value);
			if (op == eval(year) +1) {
				isNon = false;
				break;
			}
		}
		setDisabled("btnNext", isNon);
	}
	
}
