/*
 * 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_S_CODE1, LEN_S_CODE1);
	setMaxLength(PRM_TXT_S_CODE2, LEN_S_CODE2);
	setMaxLength(PRM_TXT_S_CODE3, LEN_S_CODE3);
	setMaxLength(PRM_TXT_S_NAME , LEN_S_NAME );
	//登録/更新判定 
	if (MODE == 0) {
		//フォーカス設定
		setFocus(PRM_TXT_S_CODE1);
	} else if (MODE ==1 || MODE == 2) {
		//読取専用設定及びフォーカス設定（UPDATEの場合）
		setReadOnly(PRM_TXT_S_CODE1, true);
		setReadOnly(PRM_TXT_S_CODE2, true);
		setReadOnly(PRM_TXT_S_CODE3, true);
		//フォーカス設定
		setFocus(PRM_TXT_S_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,'S2107','QC0001','');
		} else if (MODE == 1 || MODE == 2) {
			doSubmitMessage(objForm,'S2108','QC0001','');
		}
	}
}

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();
	checkRequired(PRM_TXT_S_CODE1 , NAM_S_CODE1 , aryMessage);
	checkRequired(PRM_TXT_S_NAME  , NAM_S_NAME , aryMessage);
	
	checkCode(PRM_TXT_S_CODE1 , NAM_S_CODE1	, aryMessage);
	checkCode(PRM_TXT_S_CODE2 , NAM_S_CODE2	, aryMessage);
	checkCode(PRM_TXT_S_CODE3 , NAM_S_CODE3	, aryMessage);	

	checkRegLength(PRM_TXT_S_CODE1 , LEN_S_CODE1 , NAM_S_CODE1	, aryMessage);
	
	if(getFormValue(PRM_TXT_S_CODE2) != ""){	
		checkRegLength(PRM_TXT_S_CODE2 , LEN_S_CODE2 , NAM_S_CODE2	, aryMessage);
	}
	if(getFormValue(PRM_TXT_S_CODE3) != ""){
		checkRequired(PRM_TXT_S_CODE2 , NAM_S_CODE2 , aryMessage);
		checkRegLength(PRM_TXT_S_CODE2 , LEN_S_CODE2 , NAM_S_CODE2	, aryMessage);
		if (checkTargetLength(PRM_TXT_S_CODE2, LEN_S_CODE2)) {
			aryMessage.pop();
		}
		checkRegLength(PRM_TXT_S_CODE3 , LEN_S_CODE3 , NAM_S_CODE3	, aryMessage);
	}
	
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
}	
	
/**
 * 文字桁数確認(規定桁数)
 * @param target     確認対象(StringあるいはObject)
 * @param numLength  確認対象オブジェクト桁数
 * @return 実行可否
 * @throws 実行時例外
 */	
function checkTargetLength(target, numLength) {
	var numTargetLength = getFormValue(target).length 
	if (numLength != numTargetLength) {
		return true;
	} else {
		return false;
	}
}
	
