//毎月返済額計算メインルーチン
function calc(){
    //入力チェックへ
    if (!(Acheck() && Bcheck() && Icheck() && YMcheck() && Hcheck())){
    return;
    }

    //基本部分
    var Am = document.hensai.Amount.value * 10000;		//借入元金
    var Bk = document.hensai.Bmount.value * 10000;		//ボーナス返済元金
    var Bd = document.hensai.Boma.selectedIndex;	//ボーナスまでの月数
    var Rr = document.hensai.Interest.value / 100;//借入利率
    var Yy = document.hensai.YY.selectedIndex;	//借入年数
    var Mm = document.hensai.MM.selectedIndex;	//借入月数
    var Hr = document.hensai.Horitsu.value / 100;	//保証料率
    var Nn = Math.floor(Yy * 12 + Mm);							//毎月支払回数
    var Bn = Math.floor((Nn + 6 - Bd) / 6);			//ボーナス支払回数
    var Mk = Am - Bk;								//毎月返済元金部分
    var Pm = Math.floor((Mk * Rr / 12 * Math.pow((1 + Rr /12),Nn))/(Math.pow((1 + Rr / 12),Nn)-1));	//毎月支払額
    var Pb = Math.floor((Bk * Rr / 2 * Math.pow((1 + Rr /2),Bn))/(Math.pow((1 + Rr / 2),Bn)-1));	//ボーナス月加算額
    var Py = Pm * 12 + Pb * 2;						//年間返済額
    var all = Pm * Nn + Pb * Bn;					//総返済額
    if (Bk == 0){var Bnk = 0;}
    else {var Bnk = Bn;}							//ボーナス支払回数
    var Hm = Math.floor((Mk * Hr / 12 * Math.pow((1 + Hr /12),Nn))/(Math.pow((1 + Hr / 12),Nn)-1));	//毎月払い元金に対する保証料算出
    var Hb = Math.floor((Bk * Hr / 2 * Math.pow((1 + Hr /2),Bn))/(Math.pow((1 + Hr / 2),Bn)-1));	//ボーナス払い元金に対する保証料算出
    if (Hr == 0){var Hg = 0;}
    else{var Hg = Hm * Nn + Hb * Bn - Am;}

    //出力エリアに結果出力

    document.hensai.PayM.value = Math.floor(Pm);		//毎月返済額
    document.hensai.PayB.value = Math.floor(Pb);		//ボーナス加算額
    document.hensai.Msuu.value = Math.floor(Nn);		//毎月支払回数
    document.hensai.Bsuu.value = Math.floor(Bnk);		//ボーナス支払回数
    document.hensai.PayY.value = Math.floor(Py);		//年間返済額
    document.hensai.PayALL.value = Math.floor(all);	//総返済額
    document.hensai.Hcharge.value = Math.floor(Hg);	//保証料

//メインルーチンここまで
}

//入力エリアのクリア
function ClearIP(){
    document.hensai.Amount.value = "";
    document.hensai.Bmount.value = "";
    document.hensai.Boma.selectedIndex = 0;
    document.hensai.Interest.value = "";
    document.hensai.YY.selectedIndex = 0;
    document.hensai.MM.selectedIndex = 0;
    document.hensai.Horitsu.value = "";
    document.hensai.Amount.focus();
}

//出力エリアのクリア
function ClearOP(){
    document.hensai.PayM.value = "";
    document.hensai.PayB.value = "";
    document.hensai.Msuu.value = "";
    document.hensai.Bsuu.value = "";
    document.hensai.PayY.value = "";
    document.hensai.PayALL.value = "";
    document.hensai.Hcharge.value = "";
}

//入力が数値かチェック
function NumCheck(n){
    var i = 0;
    for (i = 0; i < n.length; i++){
        var s = n.substring(i, i + 1);
        if (s == "0"  ||  s == "1" ||  s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" || s == "8" || s == "9" || s == "."){
            continue;
        }
        else{
            return false;
        }
    }
    return true;
}

//借入元金入力チェック
function Acheck(){
    var gankin = document.hensai.Amount.value;
    if (gankin == ""){
        alert("お借り入れ金額が入力されていません");
        document.hensai.Amount.focus();
        return false;
    }

    if (!NumCheck(gankin) || gankin < 1 || gankin > 99999){
        alert("お借り入れ金額には1〜99999までの整数を入力してください");
        document.hensai.Amount.focus();
        return false;
    }
    return true;
}

//ボーナス元金額入力チェック
function Bcheck(){
    var bonus = document.hensai.Bmount.value;
    var gankin = document.hensai.Amount.value;
    var bg = bonus / gankin;

    if (bonus == ""){
        alert("ボーナス返済元金が入力されていません");
        document.hensai.Bmount.focus();
        return false;
    }

    if (bg > 1){
        alert("ボーナス返済元金はお借入れ金額より少なくしてください");
        document.hensai.Bmount.focus();
        return false;
    }

    if (!NumCheck(bonus) || bonus < 0 || bonus > 99999){
        alert("ボーナス返済元金には0〜99999までの整数を入力してください");
        document.hensai.Bmount.focus();
        return false;
    }
    return true;
}    

//借入利率入力チェック
function Icheck(){
    var rate = document.hensai.Interest.value;
    if (rate == ""){
        alert("お借り入れ利率が入力されていません");
        document.hensai.Interest.focus();
        return false;
    }

    if (!NumCheck(rate) || rate < 0.001 || rate > 18.000){
        alert("お借り入れ利率には0.001〜18.000までの数値を入力してください");
        document.hensai.Interest.focus();
        return false;
    }
    return true;
}    

//借入期間入力チェック
function YMcheck(){
    var year = document.hensai.YY.selectedIndex;
    var month = document.hensai.MM.selectedIndex;
    if (year == 0){
        alert("ご返済期間は1年0カ月〜35年0カ月の範囲で入力してください");
        document.hensai.YY.focus();
        return false;
    }

    if (year == 35 && month != 0){
        alert("ご返済期間は1年0カ月〜35年0カ月の範囲で入力してください");
        document.hensai.MM.focus();
        return false;
    }
    return true;
}
        
//保証料率入力チェック
function Hcheck(){
    var hrate = document.hensai.Horitsu.value;
    if (hrate == ""){
        alert("保証料率が入力されていません");
        document.hensai.Horitsu.focus();
        return false;
    }

    if (!NumCheck(hrate) || hrate < 0 || hrate > 18.000){
        alert("保証料率には0.000〜18.000までの数値を入力してください");
        document.hensai.Horitsu.focus();
        return false;
    }
    return true;
}    
