/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 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_I_CODE, LEN_I_CODE);
	setMaxLength(PRM_TXT_I_NAME, LEN_I_NAME);
	//登録/更新判定 
	if (MODE == 0) {
		//フォーカス設定
		setFocus(PRM_TXT_I_CODE);
	} else if (MODE ==1) {
		//読取専用設定及びフォーカス設定（UPDATEの場合）
		setReadOnly(PRM_TXT_I_CODE, true);
		//フォーカス設定
		setFocus(PRM_TXT_I_NAME);
	}
	//削除ボタン、新規登録ボタン利用可否
	if (MODE != 1) {
		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,'P9032','QC0001','');
		} else if (MODE == 1 || MODE == 2) {
			doSubmitMessage(objForm,'P9033','QC0001','');
		}
	}
}

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	for (var i = 0; i < ARY_FIELDS_ID.length; i++) {
		var aryFieldsId = document.getElementsByName(ARY_FIELDS_ID[i]);
		for (var j = 0; j < aryFieldsId.length; j++) {
			setBgColor(aryFieldsId[j], COLOR_FIELD_NORMAL);
		}
	}
	// 入力チェック
	var aryMessage = new Array();
	checkRequired(PRM_TXT_I_CODE, NAM_I_CODE, aryMessage);
	checkRequired(PRM_TXT_I_NAME, NAM_I_NAME, aryMessage);

	checkCode(PRM_TXT_I_CODE, NAM_I_CODE, aryMessage);

	checkLength(PRM_TXT_I_CODE, LEN_I_CODE, NAM_I_CODE, aryMessage);
	checkLength(PRM_TXT_I_NAME, LEN_I_NAME, NAM_I_NAME, aryMessage);

	var aryImportField = document.getElementsByName(PRM_HID_I_FIELD);
	var aryImportOrder = document.getElementsByName(PRM_PLT_I_ORDER);
	for (var i = 0; i < aryImportField.length; i++) {
		if (getFormValue(aryImportField[i]) == "KCODE"
			|| getFormValue(aryImportField[i]) == "SIMEI"
			|| getFormValue(aryImportField[i]) == "FURIGANA"
			|| getFormValue(aryImportField[i]) == "SCODE"
			|| getFormValue(aryImportField[i]) == "YCODE"
			|| getFormValue(aryImportField[i]) == "TOKYU"
			|| getFormValue(aryImportField[i]) == "JCODE"
			|| getFormValue(aryImportField[i]) == "NYDATE"
			|| getFormValue(aryImportField[i]) == "KNKUBUN"
			|| getFormValue(aryImportField[i]) == "HIZUKE"
			|| getFormValue(aryImportField[i]) == "SYJIKOKU"
			|| getFormValue(aryImportField[i]) == "KINMU"
			|| getFormValue(aryImportField[i]) == "TAJIKOKU"
			|| getFormValue(aryImportField[i]) == "CALCNEN"
			|| getFormValue(aryImportField[i]) == "CALCTUKI"
			|| getFormValue(aryImportField[i]) == "PI001"
			|| getFormValue(aryImportField[i]) == "PI002"
			|| getFormValue(aryImportField[i]) == "PI003"
			|| getFormValue(aryImportField[i]) == "PT001"
			|| getFormValue(aryImportField[i]) == "PT002"
			|| getFormValue(aryImportField[i]) == "PT003"
			|| getFormValue(aryImportField[i]) == "PT004"
			|| getFormValue(aryImportField[i]) == "PT005"
			|| getFormValue(aryImportField[i]) == "PT006"
			|| getFormValue(aryImportField[i]) == "PT011"			
		) {
			checkRequired(aryImportOrder[i], NAM_I_ORDER[i], aryMessage);
		}
	}

	if (aryMessage.length == 0) {
		var count = 0;
		var aryPrmOrder = new Array();
		var aryNamOrder = new Array();
		for (var i = 0; i < aryImportOrder.length; i++) {
			if (getFormValue(aryImportOrder[i]) != "") {
				count++;
				aryPrmOrder.push(aryImportOrder[i]);
				aryNamOrder.push(NAM_I_ORDER[i]);
			}
		}
		for (var i = 1; i <= count; i++) {
			for (var j = 0; j < aryPrmOrder.length; j++) {
				if (getFormValue(aryPrmOrder[j]) == i) {
					aryPrmOrder.splice(j, 1);
					aryNamOrder.splice(j, 1);
					break;
				}
			}
		}
		if (aryPrmOrder.length > 0) {
			for (var i = 0; i < aryPrmOrder.length; i++) {
				setBgColor(aryPrmOrder[i], COLOR_FIELD_ERROR);
				if (getFormValue(aryPrmOrder[i]) > count) {
					if (aryMessage.length == 0) {
						setFocus(aryPrmOrder[i]);
						setMessage("WC2028", aryNamOrder[i] + "," + count);
					}
					aryMessage.push(getMessage("WC2028", aryNamOrder[i] + "," + count));
				} else {
					if (aryMessage.length == 0) {
						setFocus(aryPrmOrder[i]);
						setMessage("WC0009", aryNamOrder[i]);
					}
					aryMessage.push(getMessage("WC0009", aryNamOrder[i]));
				}
			}
		}
	}

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