/*
 * 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_TIME         , COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_ALLOWANCE    , COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_DEDUCTION    , COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_BREAKDOWN    , COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_SUM          , COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_ERR_FIELDS_ID, COLOR_FIELD_ERROR );
	// 項目長設定
	setFieldsMaxLength(ARY_TIME     , LEN_TIME_INT + LEN_TIME_DEC + 1);
	setFieldsMaxLength(ARY_ALLOWANCE, LEN_MONEY);
	setFieldsMaxLength(ARY_DEDUCTION, LEN_MONEY);
	setFieldsMaxLength(ARY_BREAKDOWN, LEN_MONEY + LEN_TIME_DEC + 1);
	setFieldsMaxLength(ARY_SUM      , LEN_MONEY);
	// 編集不可設定
	setFieldsReadOnly(ARY_SUM, true                  );
	setFieldsColor   (ARY_SUM, COLOR_FONT_NORMAL     );
	setFieldsBgColor (ARY_SUM, COLOR_TABLE_BACKGROUND);
	// 変更イベント設定
	setFieldsOnChangeHandler(ARY_ALLOWANCE, onChangeAmount);
	setFieldsOnChangeHandler(ARY_DEDUCTION, onChangeAmount);
	// 可視設定
	if (MODE == 1) {
		setVisibility("divButton", false);
	}
}

/**
 * サーバー通信処理
 * @param objForm フォームオブジェクト
 * @param cmd     コマンド
 * @return 無し
 * @throws 実行時例外
 */
function submitForm(objForm, cmd) {
	// 入力チェック
	if (!validate()) {
		return;
	}
	switch (cmd) {
		case "P1255":
			doSubmitMessage(objForm, cmd, 'QC0001', '');
			break;
		default:
			doSubmitMessage(objForm, cmd, 'QP1253', '');
	}
}

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_TIME     , COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_ALLOWANCE, COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_DEDUCTION, COLOR_FIELD_NORMAL);
	setFieldsBgColor(ARY_BREAKDOWN, COLOR_FIELD_NORMAL);
	// 入力チェック準備
	var aryMessage = new Array();
	// 入力チェック
	for (var i = 0; i < ARY_TIME.length; i++) {
		setZero(ARY_TIME[i]);
		checkDecimal(ARY_TIME[i], LEN_TIME_INT, LEN_TIME_DEC, "勤怠項目", aryMessage);
	}
	for (var i = 0; i < ARY_ALLOWANCE.length; i++) {
		setZero(ARY_ALLOWANCE[i]);
		checkInteger(ARY_ALLOWANCE[i], "支給項目", aryMessage);
	}
	for (var i = 0; i < ARY_DEDUCTION.length; i++) {
		setZero(ARY_DEDUCTION[i]);
		checkInteger(ARY_DEDUCTION[i], "控除項目", aryMessage);
	}
	for (var i = 0; i < ARY_BREAKDOWN.length; i++) {
		setZero(ARY_BREAKDOWN[i]);
		checkDecimal(ARY_BREAKDOWN[i], LEN_MONEY, LEN_TIME_DEC, "内訳項目", aryMessage);
	}
	for (var i = 0; i < ARY_SUM.length; i++) {
		setZero(ARY_SUM[i]);
		checkInteger(ARY_SUM[i], "合計項目", aryMessage);
	}
	// チェック内容確認
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
}

/**
 * 金額変更イベントハンドラ
 * @param e イベントオブジェクト
 * @return 無し
 * @throws 実行時例外
 */
function onChangeAmount(e) {
	for (var i = 0; i < ARY_SUM.length; i++) {
		setFormValue(ARY_SUM[i], "");
	}
}

/**
 * 空白時0円挿入
 * @param w 対象文字列
 * @return 無し
 * @throws 実行時例外
 */
function setZero(objTarget) {
	var targetMonney = getFormValue(objTarget);
	if(targetMonney == ""){
		setFormValue(objTarget, "0");
	}
}
