/*
 * 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_CHANGE_K_CODE       , LEN_K_CODE        );
	setMaxLength(PRM_TXT_GENERAL_LIFE_INS    , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_PRIVATE_PENSION     , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_LIFE_INS_DEDUCTION  , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_EARTHQUAKE_INS      , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_LONG_TERM_DAMAGE_INS, LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_DAMAGE_INS_DEDUCTION, LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_SOCIAL_INS_DEDUCTION, LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_MUTUAL_AID_DEDUCTION, LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_SPOUSE_SUM_INCOME   , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_SPOUSE_SP_DEDUCTION , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_HOUSE_LOAN_DEDUCTION, LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_ALLOWANCE_ADJUSTMENT, LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_TAX_FREE_ADJUSTMENT , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_SOC_INS_ADJUSTMENT  , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_TAX_INC_ADJUSTMENT  , LEN_MONEY_AMOUNT  );
	setMaxLength(PRM_TXT_WITHHOLD_SLIP_NOTES , LEN_WITHHOLD_NOTES);
	// チェンジイベントハンドラ設定
	setOnChangeHandler(PRM_TXT_GENERAL_LIFE_INS    , setLifeInsDeduction  );
	setOnChangeHandler(PRM_TXT_PRIVATE_PENSION     , setLifeInsDeduction  );
	setOnChangeHandler(PRM_TXT_EARTHQUAKE_INS      , setDamageInsDeduction);
	setOnChangeHandler(PRM_TXT_LONG_TERM_DAMAGE_INS, setDamageInsDeduction);
		// 金額項目の配列
	aryTargetId = new Array(
		PRM_TXT_GENERAL_LIFE_INS    ,
		PRM_TXT_PRIVATE_PENSION     ,
		PRM_TXT_LIFE_INS_DEDUCTION  ,
		PRM_TXT_EARTHQUAKE_INS      ,
		PRM_TXT_LONG_TERM_DAMAGE_INS,
		PRM_TXT_DAMAGE_INS_DEDUCTION,
		PRM_TXT_SOCIAL_INS_DEDUCTION,
		PRM_TXT_MUTUAL_AID_DEDUCTION,
		PRM_TXT_SPOUSE_SUM_INCOME   ,
		PRM_TXT_SPOUSE_SP_DEDUCTION ,
		PRM_TXT_HOUSE_LOAN_DEDUCTION,
		PRM_TXT_ALLOWANCE_ADJUSTMENT,
		PRM_TXT_TAX_FREE_ADJUSTMENT ,
		PRM_TXT_SOC_INS_ADJUSTMENT  ,
		PRM_TXT_TAX_INC_ADJUSTMENT  
	);
	
	// フォーカス喪失イベントハンドラ設定
//	setFieldsOnBlurHandler(aryTargetId, onLostFocusExtra);
	// フォーカス取得イベントハンドラ設定
	setOnFocusHandler(aryTargetId);
	// 初期表示時カンマ挿入
	setAllInsertComma();
	// フォーカス設定
	setFocus(PRM_TXT_CHANGE_K_CODE);
	
	
}



function chkTargetObject(objTarget) {
	for (var i = 0; i < aryTargetId.length; i++) {
		if (objTarget == getObject(aryTargetId[i])) {
			return true;
		}
		if (i == aryTargetId.length - 1) {
			return false;
		}
	}
}

function onLostFocusExtra(e) {
	var objTarget = getSrcElement(e);
	if (chkTargetObject(objTarget)) {
		if(!objTarget.readOnly) {
			setZero(objTarget);
			insertComma(objTarget);
		}
	}

}


function onFocusExtra(e) {
	var objTarget = getSrcElement(e);
	if (chkTargetObject(objTarget)) {
		if(!objTarget.readOnly) {
			delComma(getObject(objTarget));
			setSpace(getObject(objTarget));
		}
	}

}

/**
 * 全項目カンマ挿入
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function setAllInsertComma() {
	// 初期表示時カンマ挿入
	for (var i = 0; i < aryTargetId.length; i++) {
		insertComma(getObject(aryTargetId[i]));
	}
}

/**
 * 全項目カンマ削除
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function setAllDelComma() {
	// カンマ削除
	for (var i = 0; i < aryTargetId.length; i++) {
		delComma(getObject(aryTargetId[i]));
	}
}

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	// 入力チェック
	var aryMessage = new Array();
	checkNumber (PRM_TXT_GENERAL_LIFE_INS    , NAM_GENERAL_LIFE_INS    , aryMessage);
	checkNumber (PRM_TXT_PRIVATE_PENSION     , NAM_PRIVATE_PENSION     , aryMessage);
	checkNumber (PRM_TXT_LIFE_INS_DEDUCTION  , NAM_LIFE_INS_DEDUCTION  , aryMessage);
	checkNumber (PRM_TXT_EARTHQUAKE_INS      , NAM_EARTHQUAKE_INS      , aryMessage);
	checkNumber (PRM_TXT_LONG_TERM_DAMAGE_INS, NAM_LONG_TERM_DAMAGE_INS, aryMessage);
	checkNumber (PRM_TXT_DAMAGE_INS_DEDUCTION, NAM_DAMAGE_INS_DEDUCTION, aryMessage);
	checkNumber (PRM_TXT_SOCIAL_INS_DEDUCTION, NAM_SOCIAL_INS_DEDUCTION, aryMessage);
	checkNumber (PRM_TXT_MUTUAL_AID_DEDUCTION, NAM_MUTUAL_AID_DEDUCTION, aryMessage);
	checkNumber (PRM_TXT_SPOUSE_SUM_INCOME   , NAM_SPOUSE_SUM_INCOME   , aryMessage);
	checkNumber (PRM_TXT_SPOUSE_SP_DEDUCTION , NAM_SPOUSE_SP_DEDUCTION , aryMessage);
	checkNumber (PRM_TXT_HOUSE_LOAN_DEDUCTION, NAM_HOUSE_LOAN_DEDUCTION, aryMessage);
	checkNumber (PRM_TXT_ALLOWANCE_ADJUSTMENT, NAM_ALLOWANCE_ADJUSTMENT, aryMessage);
	checkNumber (PRM_TXT_TAX_FREE_ADJUSTMENT , NAM_TAX_FREE_ADJUSTMENT , aryMessage);
	checkNumber (PRM_TXT_SOC_INS_ADJUSTMENT  , NAM_SOC_INS_ADJUSTMENT  , aryMessage);
	checkNumber (PRM_TXT_TAX_INC_ADJUSTMENT  , NAM_TAX_INC_ADJUSTMENT  , aryMessage);
	if (aryMessage.length == 0) {
		return true;
	} else {
		// カンマ挿入
		setAllInsertComma();
		showMessage(aryMessage);
		return false;
	}
}

/**
 * 家族情報付加
 * @param 無し
 * @return 無し
 * @throws 無し
 */
function addFamilyInfo() {
	try {
		var familyInfo       = getFormValue(PRM_HID_FAMILY_INFO        );
		var withholdSlipNote = getFormValue(PRM_TXT_WITHHOLD_SLIP_NOTES);
		var note             = withholdSlipNote + familyInfo;
		if (note.length <= LEN_WITHHOLD_NOTES) {
			setFormValue(PRM_TXT_WITHHOLD_SLIP_NOTES, withholdSlipNote + familyInfo);
		}
	} catch (e) {
		handleException(e);
	}
}

/**
 * 生命保険料控除額設定
 * @param 無し
 * @return 無し
 * @throws 無し
 */
function setLifeInsDeduction() {
	try {
		// カンマ削除
		delComma(getObject(PRM_TXT_GENERAL_LIFE_INS));
		delComma(getObject(PRM_TXT_PRIVATE_PENSION));
		if (
			checkNumberNoMsg(PRM_TXT_GENERAL_LIFE_INS) &&
			checkNumberNoMsg(PRM_TXT_PRIVATE_PENSION )
		) {
			setFormValue(
				PRM_TXT_LIFE_INS_DEDUCTION, calcLifeInsDeduction(
				new Number(getFormValue(PRM_TXT_GENERAL_LIFE_INS)),
				new Number(getFormValue(PRM_TXT_PRIVATE_PENSION ))
			));
			// カンマ挿入
			insertComma(getObject(PRM_TXT_LIFE_INS_DEDUCTION));
			insertComma(getObject(PRM_TXT_GENERAL_LIFE_INS));
			insertComma(getObject(PRM_TXT_PRIVATE_PENSION));
			
		}
	} catch (e) {
		handleException(e);
	}
}

/**
 * 損害保険料控除額設定
 * @param 無し
 * @return 無し
 * @throws 無し
 */
function setDamageInsDeduction() {
	try {
		// カンマ削除
		delComma(getObject(PRM_TXT_EARTHQUAKE_INS));
		delComma(getObject(PRM_TXT_LONG_TERM_DAMAGE_INS));
		if (
			checkNumberNoMsg(PRM_TXT_EARTHQUAKE_INS      ) &&
			checkNumberNoMsg(PRM_TXT_LONG_TERM_DAMAGE_INS)
		) {
			setFormValue(
				PRM_TXT_DAMAGE_INS_DEDUCTION, calcDamageInsDeduction(
				new Number(getFormValue(PRM_TXT_EARTHQUAKE_INS      )),
				new Number(getFormValue(PRM_TXT_LONG_TERM_DAMAGE_INS))
			));
			// カンマ挿入
			insertComma(getObject(PRM_TXT_DAMAGE_INS_DEDUCTION));
			insertComma(getObject(PRM_TXT_EARTHQUAKE_INS));
			insertComma(getObject(PRM_TXT_LONG_TERM_DAMAGE_INS));
		}
	} catch (e) {
		handleException(e);
	}
}

/**
 * 生命保険料控除額算出
 * @param generalLifeIns 一般の生命保険料額
 * @param privatePension 個人年金保険料額
 * @return 生命保険料控除額
 * @throws 実行時例外
 */
function calcLifeInsDeduction(generalLifeIns, privatePension) {
	return calcLifeInsDeductionEach(generalLifeIns) +
			calcLifeInsDeductionEach(privatePension);
}

/**
 * 生命保険料控除額算出(個別)
 * @param premium 保険料
 * @return 控除額
 * @throws 実行時例外
 */
function calcLifeInsDeductionEach(premium) {
	// 25000円以下→支払った保険料の全額
	if (premium <= 25000) {
		return premium;
	}
	// 25001円〜50000円→支払った保険料の金額÷2+12500
	if (premium <= 50000) {
		return Math.ceil(premium / 2) + 12500;
	}
	// 50001円〜100000円→支払った保険料の金額÷4+25000
	if (premium <= 100000) {
		return Math.ceil(premium / 4) + 25000;
	}
	// 100001円以上→一律50000
	return 50000;
}

/**
 * 損害保険料控除額算出
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function calcDamageInsDeduction(earthQuakeIns, longTermDamageIns) {
	var damageInsDeduction = 0;
	// 地震保険料の金額(最高50000円)
	if (earthQuakeIns <= 50000) {
		damageInsDeduction += earthQuakeIns;
	} else {
		damageInsDeduction += 50000;
	}
	// 旧長期損額保険の金額
	if (longTermDamageIns <= 10000) {
		// 10000円以下→支払った保険料の全額
		damageInsDeduction += longTermDamageIns;
	} else if (longTermDamageIns <= 20000) {
		// 10001円〜20000円→支払った保険料の金額÷2+5000
		damageInsDeduction += Math.ceil(longTermDamageIns / 2) + 5000;
	} else {
		// 20001円以上→一律15000
		damageInsDeduction += 15000;
	}
	// 損害保険料控除額(最高50000円)
	if (damageInsDeduction <= 50000) {
		return damageInsDeduction;
	}
	return 50000;
}

/**
 * カンマ挿入
 * @param sourceStr 対象文字列
 * @return 無し
 * @throws 実行時例外
 */
function insertComma(objTarget) {
	var destStr = getFormValue(objTarget);
	var tmpStr = "";
	while (destStr != (tmpStr = destStr.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2"))) {
		destStr = tmpStr;
	}
	setFormValue(objTarget, destStr);
}



/**
 * カンマ削除
 * @param w 対象文字列
 * @return 無し
 * @throws 実行時例外
 */
function delComma(objTarget) {

	var targetMonney = getFormValue(objTarget);
	setFormValue(objTarget, targetMonney.replace(/,/g,""));
}

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

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


/**
 * サーバー通信処理
 * @param objForm フォームオブジェクト
 * @param cmd     コマンド
 * @param msg     メッセージコード
 * @param rep     置換文字列
 * @return 無し
 * @throws 実行時例外
 */
function submitForm(objForm, cmd, msg, rep) {
	// カンマ削除
	setAllDelComma();
	// 入力チェック
	if (validate()) {
		// サーバー通信
		submitMessageForm(objForm, cmd, msg, rep);
	}
}

/**
 * サーバー通信処理
 * @param objForm フォームオブジェクト
 * @param cmd     コマンド
 * @param msg     メッセージコード
 * @param rep     置換文字列
 * @return 無し
 * @throws 実行時例外
 */
function submitMessageForm(objForm, cmd, code, rep) {
	if (!confirm(getMessage(code, rep))) {
		// カンマ挿入
		setAllInsertComma();
		return;
	}
	// submit
	doSubmit(objForm, cmd);
}

/**
 * key設定付きsubmit処理
 * @param objForm フォームオブジェクト
 * @param cmd     コマンド
 * @param key1    キー1
 * @param key2    キー2
 * @return 無し
 * @throws 実行時例外
 */
function doSubmitKey(objForm, cmd, key1, key2) {
	// カンマ削除
	setAllDelComma();
	objForm.key1.value = key1;
	objForm.key2.value = key2;
	doSubmit(objForm, cmd);
}
