//-----------------------------------------------------------------
// 関数名: paymain
// 内  容: メイン処理
// 引  数: -
// 戻り値: -
//-----------------------------------------------------------------
function paymain() {

	// ご希望購入額の値取得
	var hope = document.getElementById("hope").value;

	// 返済期間の値取得
	var pay_period = document.getElementById("pay_period").value;

	// 金利の値取得
	var rate1_input = document.getElementById("rate1_input").value;

	// 入力チェック(ご希望購入額)
	if (hope == "") {
		alert("借入額を入力して下さい。");
		return false;
	}

	if((chkNum(hope)) == false) {
		alert("借入額は半角数字を入力して下さい。");
		return false;
	}
	
	// 入力チェック(金利)
	if (rate1_input == "") {
		alert("金利を入力して下さい。");
		return false;
	}

	if((chkNum(rate1_input)) == false) {
		alert("金利には半角数字を入力して下さい。");
		return false;
	}

	// 計算処理
	money(hope,pay_period,rate1_input);
}

//-----------------------------------------------------------------
// 関数名: money
// 内  容: 元金均等の計算
// 引  数: -
// 戻り値: -
//-----------------------------------------------------------------
function money(hope,pay_period,rate1_input) {
	
	// 月々の返済
	var return_month = "";
	
	// 総支払額
	var return_all = "";
	
	// 借入額の変換
	var hope_num = hope * 10000;
	
	// 金利の返還(%を数字に変換)
	var rate1_input_num = rate1_input / 100;
	
	// 期間の変換(日に変換)
	var pay_period_month = pay_period * 365;

	//毎月返済分の算出
	return_month  = hope_num * (1 + rate1_input_num) / pay_period_month * 30;
	
	//総支払額の算出
	return_all = hope_num * (1 + rate1_input_num);
	
	// カンマ処理
	var return_month_comma = addComma((return_month).toFixed(0));
	var return_all_comma = addComma((return_all).toFixed(0));
	
	// 計算金額の表示
	document.getElementById("pay_month").value = return_month_comma;
	document.getElementById("pay_all").value = return_all_comma;
}

//-----------------------------------------------------------------
// 関数名: chkNum
// 内  容: 対象の文字が数値のみか判断する
// 引  数: str (チェックする文字列)
// 戻り値: boolean
//-----------------------------------------------------------------
function chkNum (str) {
	
	// 値がなかったら抜ける
	if (str == "") {
		return true;
	}
	
	// 数値を抜いて他に文字が残ればエラー
	if (str.match( /[^0-9]+/ ) != null) {
		return false;
	}
	
	return true;
}

//-----------------------------------------------------------------
// 関数名: dataclear
// 内  容: データクリア処理
// 引  数: -
// 戻り値: -
//-----------------------------------------------------------------
function dataclear(){
	document.getElementById("hope").value = "";
	document.getElementById("pay_period").value = 35;
	document.getElementById("rate1_input").value = "";
	document.getElementById("pay_month").value = "";
	document.getElementById("pay_all").value = "";
}

//-----------------------------------------------------------------
// 関数名: addComma
// 内  容: カンマ追加処理
// 引  数: num(金額)
// 戻り値: str(カンマをつけた金額)
//-----------------------------------------------------------------
function addComma(value){
    var i;
    for(i = 0; i < value.length/3; i++){
        value = value.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2");
    }
    return value;
}

