var COLOR_FIELD_CHANGE = "orange";
var KEY_SEPARATOR      = ",";
var PRM_TXT_CHANGE_K_CODE = "txtChangeKCode";

/**
 * 画面読込時追加処理
 * @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_POSTAL_CODE1			, LEN_POSTAL_CODE1	);
	setMaxLength(PRM_TXT_POSTAL_CODE2			, LEN_POSTAL_CODE2	);
	setMaxLength(PRM_TXT_ADRESS1				, LEN_ADDRESS		);
	setMaxLength(PRM_TXT_ADRESS2				, LEN_ADDRESS		);
	setMaxLength(PRM_TXT_ADRESS3				, LEN_ADDRESS		);
	setMaxLength(PRM_TXT_TEL1					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_TEL2					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_TEL3					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_FAX1					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_FAX2					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_FAX3					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_PHONE1					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_PHONE2					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_PHONE3					, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_EMERGENCY_NAME			, LEN_NAME			);
	setMaxLength(PRM_TXT_EMERGENCY_POSTAL_CODE1	, LEN_POSTAL_CODE1	);
	setMaxLength(PRM_TXT_EMERGENCY_POSTAL_CODE2	, LEN_POSTAL_CODE2	);
	setMaxLength(PRM_TXT_EMERGENCY_ADRESS1		, LEN_ADDRESS		);
	setMaxLength(PRM_TXT_EMERGENCY_ADRESS2		, LEN_ADDRESS		);
	setMaxLength(PRM_TXT_EMERGENCY_ADRESS3		, LEN_ADDRESS		);
	setMaxLength(PRM_TXT_EMERGENCY_TEL1			, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_EMERGENCY_TEL2			, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_EMERGENCY_TEL3			, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_EMERGENCY_PHONE1		, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_EMERGENCY_PHONE2		, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_EMERGENCY_PHONE3		, LEN_PHONE_NUMBER	);
	setMaxLength(PRM_TXT_LICENSE				, LEN_LICENSE		);
	setMaxLength(PRM_TXT_RECAPITULATION			, LEN_RECAPITULATION);
	setMaxLength(PRM_TXT_HEALTH					, LEN_HEALTH		);

	// 初期設定
	if (MODE != 0) {		
		// フォーカス設定(UPDATEの場合)
		setFocus(PRM_TXT_POSTAL_CODE1);
		setFieldsBgColor(PRM_TXT_CHANGE_K_CODE, COLOR_TABLE_BACKGROUND);
	}
	setVisibility("btnNewData", false);
	// モードタイトル
	switch (MODE) {
		case 0:
			setInnerHtml('spnMode', NAM_MODE_INSERT);
			break;
		case 1:
			setInnerHtml('spnMode', NAM_MODE_UPDATE);
			setColor('spnMode', COLOR_MSG_WARNING);
			break;
		default:
	}
	
		// changeイベント設定
		setOnChangeHandler(	   PRM_PLT_GENDER					,	          onChangeFields	);
		setOnChangeHandler(	   PRM_PLT_BIRTH_YEAR				,	          onChangeFields	);
		setOnChangeHandler(	   PRM_PLT_BIRTH_MONTH				,	          onChangeFields	);
		setOnChangeHandler(	   PRM_PLT_BIRTH_DAY				,	          onChangeFields	);
		setOnChangeHandler(	   PRM_TXT_POSTAL_CODE1 			,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_POSTAL_CODE2 			,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_PLT_PREFECTURE 				,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_ADRESS1 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_ADRESS2 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_ADRESS3 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_NAME 			,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_TEL1 			,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_TEL2 			,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_TEL3 			,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_PHONE1 		,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_PHONE2 		,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_PHONE3 		,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_POSTAL_CODE1 	,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_POSTAL_CODE2	,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_PLT_EMERGENCY_PREFECTURE		,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_ADRESS1		,	          onChangeFields	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_ADRESS2		,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_EMERGENCY_ADRESS3		,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_TEL1 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_TEL2 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_TEL3 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_FAX1 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_FAX2 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_FAX3 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_PHONE1 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_PHONE2 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_TXT_PHONE3 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_PLT_BLOOD_TYPE				,	          onChangeFields	);
//		setOnChangeHandler(	   PRM_TXT_LICENSE 					,	          onChangeFields 	);
//		setOnChangeHandler(	   PRM_TXT_RECAPITULATION 			,	          onChangeFields 	);
//		setOnChangeHandler(	   PRM_TXT_HEALTH 					,	          onChangeFields 	);
		setOnChangeHandler(	   PRM_PLT_SALARY_KB				,	          onChangeFields	);
		setOnChangeHandler(	   PRM_PLT_TAX_KB					,	          onChangeFields	);
		setOnChangeHandler(	   PRM_PLT_UNION_KB					,	          onChangeFields	);
		setOnChangeHandler(	   PRM_PLT_PART_KB					,	          onChangeFields	);
		setOnChangeHandler(	   PRM_PLT_PAYMENT_KB				,	          onChangeFields	);
	
	// 変更時処理(初期着色)
	onChangeFields();
}

/**
 * フィールド変更時処理
 * @param 無し
 * @return 無し
 * @throws 実行時例外
 */
function onChangeFields() {
	try {
		if (MODE == 0) {
			return;
		}
		checkChangedValue(	   PRM_PLT_GENDER					,	          PRM_HID_GENDER				);
		checkChangedValue(	   PRM_PLT_BIRTH_YEAR				,	          PRM_HID_BIRTH_YEAR			);
		checkChangedValue(	   PRM_PLT_BIRTH_MONTH				,	          PRM_HID_BIRTH_MONTH			);
		checkChangedValue(	   PRM_PLT_BIRTH_DAY				,	          PRM_HID_BIRTH_DAY				);
		checkChangedValue(	   PRM_TXT_POSTAL_CODE1 			,	          PRM_HID_POSTAL_CODE1 			);
		checkChangedValue(	   PRM_TXT_POSTAL_CODE2 			,	          PRM_HID_POSTAL_CODE2 			);
		checkChangedValue(	   PRM_PLT_PREFECTURE 				,	          PRM_HID_PREFECTURE 			);
		checkChangedValue(	   PRM_TXT_ADRESS1 					,	          PRM_HID_ADRESS1 				);
		checkChangedValue(	   PRM_TXT_ADRESS2 					,	          PRM_HID_ADRESS2 				);
		checkChangedValue(	   PRM_TXT_ADRESS3 					,	          PRM_HID_ADRESS3 				);
		checkChangedValue(	   PRM_TXT_EMERGENCY_NAME 			,	          PRM_HID_EMERGENCY_NAME 		);
		checkChangedValue(	   PRM_TXT_EMERGENCY_TEL1 			,	          PRM_HID_EMERGENCY_TEL1 		);
		checkChangedValue(	   PRM_TXT_EMERGENCY_TEL2 			,	          PRM_HID_EMERGENCY_TEL2 		);
		checkChangedValue(	   PRM_TXT_EMERGENCY_TEL3 			,	          PRM_HID_EMERGENCY_TEL3 		);
		checkChangedValue(	   PRM_TXT_EMERGENCY_PHONE1 		,	          PRM_HID_EMERGENCY_PHONE1 		);
		checkChangedValue(	   PRM_TXT_EMERGENCY_PHONE2 		,	          PRM_HID_EMERGENCY_PHONE2 		);
		checkChangedValue(	   PRM_TXT_EMERGENCY_PHONE3 		,	          PRM_HID_EMERGENCY_PHONE3 		);
		checkChangedValue(	   PRM_TXT_EMERGENCY_POSTAL_CODE1 	,	          PRM_HID_EMERGENCY_POSTAL_CODE1);
		checkChangedValue(	   PRM_TXT_EMERGENCY_POSTAL_CODE2	,	          PRM_HID_EMERGENCY_POSTAL_CODE2);
		checkChangedValue(	   PRM_PLT_EMERGENCY_PREFECTURE		,	          PRM_HID_EMERGENCY_PREFECTURE 	);
		checkChangedValue(	   PRM_TXT_EMERGENCY_ADRESS1		,	          PRM_HID_EMERGENCY_ADRESS1		);
		checkChangedValue(	   PRM_TXT_EMERGENCY_ADRESS2		,	          PRM_HID_EMERGENCY_ADRESS2 	);
		checkChangedValue(	   PRM_TXT_EMERGENCY_ADRESS3		,	          PRM_HID_EMERGENCY_ADRESS3 	);
		checkChangedValue(	   PRM_TXT_TEL1 					,	          PRM_HID_TEL1 					);
		checkChangedValue(	   PRM_TXT_TEL2 					,	          PRM_HID_TEL2 					);
		checkChangedValue(	   PRM_TXT_TEL3 					,	          PRM_HID_TEL3 					);
		checkChangedValue(	   PRM_TXT_FAX1 					,	          PRM_HID_FAX1 					);
		checkChangedValue(	   PRM_TXT_FAX2 					,	          PRM_HID_FAX2 					);
		checkChangedValue(	   PRM_TXT_FAX3 					,	          PRM_HID_FAX3 					);
		checkChangedValue(	   PRM_TXT_PHONE1 					,	          PRM_HID_PHONE1 				);
		checkChangedValue(	   PRM_TXT_PHONE2 					,	          PRM_HID_PHONE2 				);
		checkChangedValue(	   PRM_TXT_PHONE3 					,	          PRM_HID_PHONE3 				);
		checkChangedValue(	   PRM_PLT_BLOOD_TYPE				,	          PRM_HID_BLOOD_TYPE			);
//		checkChangedValue(	   PRM_TXT_LICENSE 					,	          PRM_HID_LICENSE 				);
//		checkChangedValue(	   PRM_TXT_RECAPITULATION 			,	          PRM_HID_RECAPITULATION 		);
//		checkChangedValue(	   PRM_TXT_HEALTH 					,	          PRM_HID_HEALTH 				);
		checkChangedValue(	   PRM_PLT_SALARY_KB				,	          PRM_HID_SALARY_KB				);
		checkChangedValue(	   PRM_PLT_TAX_KB					,	          PRM_HID_TAX_KB				);
		checkChangedValue(	   PRM_PLT_UNION_KB					,	          PRM_HID_UNION_KB				);
		checkChangedValue(	   PRM_PLT_PART_KB					,	          PRM_HID_PART_KB				);
		checkChangedValue(	   PRM_PLT_PAYMENT_KB				,	          PRM_HID_PAYMENT_KB			);
	
	} catch (e) {
		handleException(e);
	}
}


/**
 * 変更確認処理
 * @param strTargetId    変更確認対象オブジェクトID
 * @param strBaseValueId 元値所持オブジェクトID
 * @return 無し
 * @throws 実行時例外
 */
 
function checkChangedValue(strTargetId, strBaseValueId) {
	var color = getBgColor(strTargetId);
	if (color == COLOR_FIELD_ERROR) {
		return;
	}
	setBgColor(strTargetId, COLOR_FIELD_NORMAL);
	if (getFormValue(strTargetId) != getFormValue(strBaseValueId)) {
		setBgColor(strTargetId, COLOR_FIELD_CHANGE);
	} 
}

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

/**
 * 入力チェック
 * @param 無し
 * @return チェック結果(true：OK、false：NG)
 * @throws 実行時例外
 */
function validate() {
	// フィールド背景色設定
	setFieldsBgColor(ARY_FIELDS_ID, COLOR_FIELD_NORMAL);
	onChangeFields();
	// 入力チェック
	var aryMessage = new Array();
	if (MODE == 1) {
		setFieldsBgColor(PRM_TXT_CHANGE_K_CODE, COLOR_TABLE_BACKGROUND);
	} else {
		checkRequired(PRM_TXT_CHANGE_K_CODE, NAM_K_CODE, aryMessage);
		checkCode    (PRM_TXT_CHANGE_K_CODE, NAM_K_CODE, aryMessage);
	}
	if (getFormValue(PRM_TXT_POSTAL_CODE1) != "") {
		checkRegLength(PRM_TXT_POSTAL_CODE1	, LEN_POSTAL_CODE1	, NAM_POSTAL1, aryMessage);
		checkRequired(PRM_TXT_POSTAL_CODE2						, NAM_POSTAL2, aryMessage);
	}
	if (getFormValue(PRM_TXT_POSTAL_CODE2) != "") {
		checkRegLength(PRM_TXT_POSTAL_CODE2	, LEN_POSTAL_CODE2	, NAM_POSTAL2, aryMessage);
		checkRequired(PRM_TXT_POSTAL_CODE1						, NAM_POSTAL1, aryMessage);
		checkRegLength(PRM_TXT_POSTAL_CODE1	, LEN_POSTAL_CODE1	, NAM_POSTAL1, aryMessage);
		if (checkTargetLength(PRM_TXT_EMERGENCY_POSTAL_CODE1, LEN_POSTAL_CODE1)) {
			aryMessage.pop();
		}	
	}
	if (getFormValue(PRM_TXT_EMERGENCY_POSTAL_CODE1) != "") {
		checkRegLength(PRM_TXT_EMERGENCY_POSTAL_CODE1	, LEN_POSTAL_CODE1	, NAM_EMERGENCY_POSTAL1	, aryMessage);
		checkRequired(PRM_TXT_EMERGENCY_POSTAL_CODE2						, NAM_EMERGENCY_POSTAL2, aryMessage);
	}
	if (getFormValue(PRM_TXT_EMERGENCY_POSTAL_CODE2) != "") {
		checkRegLength(PRM_TXT_EMERGENCY_POSTAL_CODE2	, LEN_POSTAL_CODE2	, NAM_EMERGENCY_POSTAL2	, aryMessage);
		checkRequired(PRM_TXT_EMERGENCY_POSTAL_CODE1						, NAM_EMERGENCY_POSTAL1, aryMessage);
		checkRegLength(PRM_TXT_EMERGENCY_POSTAL_CODE1	, LEN_POSTAL_CODE1	, NAM_EMERGENCY_POSTAL1	, aryMessage);
		if (checkTargetLength(PRM_TXT_EMERGENCY_POSTAL_CODE1, LEN_POSTAL_CODE1)) {
			aryMessage.pop();
		}
	}
	
	checkTextAreaLength(PRM_TXT_LICENSE			, LEN_LICENSE			, NAM_LICENSE			, aryMessage);
	checkTextAreaLength(PRM_TXT_RECAPITULATION	, LEN_RECAPITULATION	, NAM_RECAPITULATION	, aryMessage);
	checkTextAreaLength(PRM_TXT_HEALTH			, LEN_HEALTH			, NAM_HEALTH			, aryMessage);
	
	checkNumber(PRM_TXT_TEL1					, NAM_TEL1				, aryMessage);
	checkNumber(PRM_TXT_TEL2					, NAM_TEL2				, aryMessage);
	checkNumber(PRM_TXT_TEL3					, NAM_TEL3				, aryMessage);
	checkNumber(PRM_TXT_FAX1					, NAM_FAX1				, aryMessage);
	checkNumber(PRM_TXT_FAX2					, NAM_FAX2				, aryMessage);
	checkNumber(PRM_TXT_FAX3					, NAM_FAX3				, aryMessage);
	checkNumber(PRM_TXT_PHONE1					, NAM_PHONE1			, aryMessage);
	checkNumber(PRM_TXT_PHONE2					, NAM_PHONE2			, aryMessage);
	checkNumber(PRM_TXT_PHONE3					, NAM_PHONE3			, aryMessage);
	checkNumber(PRM_TXT_EMERGENCY_TEL1			, NAM_EMERGENCY_TEL1	, aryMessage);
	checkNumber(PRM_TXT_EMERGENCY_TEL2			, NAM_EMERGENCY_TEL2	, aryMessage);
	checkNumber(PRM_TXT_EMERGENCY_TEL3			, NAM_EMERGENCY_TEL3	, aryMessage);
	checkNumber(PRM_TXT_EMERGENCY_PHONE1		, NAM_EMERGENCY_PHONE1	, aryMessage);
	checkNumber(PRM_TXT_EMERGENCY_PHONE2		, NAM_EMERGENCY_PHONE2	, aryMessage);
	checkNumber(PRM_TXT_EMERGENCY_PHONE3		, NAM_EMERGENCY_PHONE3	, aryMessage);
	checkNumber(PRM_TXT_POSTAL_CODE1			, NAM_POSTAL1			, aryMessage);
	checkNumber(PRM_TXT_POSTAL_CODE2			, NAM_POSTAL2			, aryMessage);
	checkNumber(PRM_TXT_EMERGENCY_POSTAL_CODE1	, NAM_EMERGENCY_POSTAL1	, aryMessage);
	checkNumber(PRM_TXT_EMERGENCY_POSTAL_CODE2	, NAM_EMERGENCY_POSTAL2	, aryMessage);
	
	checkDate(PRM_PLT_BIRTH_YEAR, PRM_PLT_BIRTH_MONTH, PRM_PLT_BIRTH_DAY,
				NAM_BIRTH_DATE, aryMessage);

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

function checkTextAreaLength(target, numLength, rep, aryMessage) {
	var numTargetLength = getFormValue(target).length 
	if (numLength < numTargetLength) {
		if (aryMessage.length == 0) {
			setFocus(target);
			setMessage("WC2025", rep + ","  + numLength);
		}
		setBgColor(target, COLOR_FIELD_ERROR);
		aryMessage.push(getMessage("WC2025", rep + SEPARATOR_MESSAGE + numLength));
	}
}

function checkDate(yyyy, mm, dd,msg,aryMsg) {
	//年月日チェック
	dayMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	ValidFlag = true;
	yyyy2 = getFormValue(yyyy);
	mm2 = getFormValue(mm);
	dd2 = getFormValue(dd);
	if ((mm2 < 1) || (mm2 > 12)){
		setBgColor(yyyy, COLOR_FIELD_ERROR);
		aryMsg.push(msg + "の月は1--12を入力してください。");
	}
	if ((dd2 < 1) || (dd2 > dayMonth[mm2 - 1])){
		setBgColor(dd, COLOR_FIELD_ERROR);
		aryMsg.push(getMessage("WC0006", msg));
	}
	if (!chkUrudosi(yyyy2) && (mm2 == 2) && (dd2 == 29)){
		setBgColor(dd, COLOR_FIELD_ERROR);
		aryMsg.push(getMessage("WC0006", msg));
	}
}

function chkUrudosi(y) {
	if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
		return true;
	}
	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;
	}
}

