/*
 * 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_THIS_SUPPLY	, LEN_PAID_HOLIDAY	);
	setMaxLength(PRM_TXT_LAST_SUPPLY	, LEN_PAID_HOLIDAY	);
	setMaxLength(PRM_TXT_THIS_WASTE		, LEN_PAID_HOLIDAY	);
	setMaxLength(PRM_TXT_LAST_WASTE		, LEN_PAID_HOLIDAY	);
	setReadOnly(PRM_TXT_TOTAL_REMAIN 	, true);
	setReadOnly(PRM_TXT_THIS_REAMIN  	, true);
	setReadOnly(PRM_TXT_LAST_REMAIN  	, true);
	// 一覧背景色設定
	setTableColor("list");
	// 登録/更新判定
	if (MODE == 0) {
		setVisibility("btnRegist", false);
	}
}

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

/**
 * フィールド変更
 * @param 無し
 * @return 
 * @throws 実行時例外
 */
function makeValue(hidRemain, txtSupply, txtWaste, txtRemain){
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	if (validate()) {
		var remain = parseFloat(getFormValue(hidRemain));
		var supply	= parseInt(getFormValue(txtSupply));	
		var waste = parseFloat(getFormValue(txtWaste));	
		var result = remain + supply - waste;
		if (result < 0)	{
			result = 0;
			waste = remain + supply;
			setFormValue(txtWaste, waste);
			alert("破棄数が保有数を超えました。保有数は0になります。");
		}
		setFormValue(txtRemain, result);
		var lastRemain = parseFloat(getFormValue(PRM_TXT_LAST_REMAIN));	
		var thisRemain = parseFloat(getFormValue(PRM_TXT_THIS_REAMIN));
		setFormValue(PRM_TXT_TOTAL_REMAIN, lastRemain + thisRemain);
	}
}

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();
	// 必須確認
	checkRequired(PRM_TXT_THIS_SUPPLY	, NAM_THIS_SUPPLY	, aryMessage);
	checkRequired(PRM_TXT_LAST_SUPPLY	, NAM_LAST_SUPPLY	, aryMessage);
	checkRequired(PRM_TXT_THIS_WASTE	, NAM_THIS_WASTE	, aryMessage);
	checkRequired(PRM_TXT_LAST_WASTE	, NAM_LAST_WASTE	, aryMessage);
	// 数値チェック
	checkNumber(PRM_TXT_THIS_SUPPLY		, NAM_THIS_SUPPLY	, aryMessage);
	checkNumber(PRM_TXT_LAST_SUPPLY		, NAM_LAST_SUPPLY	, aryMessage);
	checkDecimal(PRM_TXT_THIS_WASTE , LEN_INTEGER , LEN_DECIMAL , NAM_THIS_WASTE , aryMessage);
	checkDecimal(PRM_TXT_LAST_WASTE , LEN_INTEGER , LEN_DECIMAL , NAM_LAST_WASTE , aryMessage);
	
	// 半日入力のチェック
	checkHalfDays(PRM_TXT_THIS_WASTE, NAM_THIS_WASTE, aryMessage);
	checkHalfDays(PRM_TXT_LAST_WASTE, NAM_LAST_WASTE, aryMessage);
	
	if (aryMessage.length == 0) {
		return true;
	} else {
		showMessage(aryMessage);
		return false;
	}
	
}

function checkHalfDays(target, rep, aryMessage) {
	var inputDays = parseFloat(getFormValue(target)).toFixed(LEN_DECIMAL);
	var numInputDays = Math.floor(inputDays);
	var halfDays = inputDays - numInputDays;
	if (halfDays > 0 && halfDays.toString() != "0.5") {
		setBgColor(target, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC0005", rep));
	}
	
}
