// JavaScript Document




function comma_Add(str){
    var dot=0;

    if(-1<str.indexOf('.')){
        dot=str.indexOf('.');
    }else if(-1<str.indexOf('-')){
        dot=str.length-1;
    }else{
        dot=str.length;
    }
    if(-1<str.indexOf('-')){
        for(i=1;i<dot/3;i++){
            str=str.substring(0,dot-i*3+1)+','+str.substring(dot-i*3+1,str.length);
        }
    }else if(!(-1<str.indexOf('-'))){
        for(i=1;i<dot/3;i++){
            str=str.substring(0,dot-i*3)+','+str.substring(dot-i*3,str.length);
        }
    }
    return str;
}


function SetFocus(d){
        d.forms[0].elements[0].focus();
}

/* showRateは使用しないためコメントアウト */
/*
function showRate() {
        parent.table.location = "../loanrate.html";
}
*/

/* 借入利率はリストダウンで選択のためチェックしない */
/* 借入額と年月のみチェックする */
/* && chkRt(f) */
function calc(f) {
        if (!( chkKr(f) && chkYYMM(f) && chkRt(f) )){
                return;
        }

        var Kr = eval(10000 * f.Kari.value);

/* ボーナスまでの月数を６で固定 */
        var ToBn = 6;

/* ボーナスまでの月数も６で固定とするためコメントアウト */
/*      var ToBn = eval(f.ToBonus.selectedIndex); */

/* 利率はテキスト入力を利用する */
        var Rt = eval(1 * f.Rate.value) / 100; 


/*-------------------------------------------------
/*リストダウンをコメント
        for (i = 0; i < f.Rate.options.length; i++) {
                if (f.Rate.options[i].selected == true) {
                 wrk2 = f.Rate.options[i].value;
                 }
        }
        var Rt = 0.01 * eval(wrk2);
*/
/*---------------------------------------------------*/




        var YY = eval(f.YY.selectedIndex);
        var MM = 0;
/* eval(f.MM.selectedIndex); */
        var wrk = "";
        var wrk2 = "";

        for (i = 0; i < f.Bonus.options.length; i++) {
                if (f.Bonus.options[i].selected == true) {
                 wrk = f.Bonus.options[i].value;
                 }
        }
        var Bn = 0.01 * eval(wrk);
        var BnTime = Math.floor((YY * 12 + MM + 6 - ToBn) / 6);
        var result1 = Kr * (1 - Bn) * (Rt / 12) *
                Math.pow((1 + Rt / 12),(YY * 12 + MM)) /
                (Math.pow((1 + Rt / 12),(YY * 12 + MM)) - 1);

        var result2 = Kr * Bn * (Rt / 2) *
                Math.pow((1 + Rt / 2),(BnTime - 1)) *
                (1 + Rt /2 * ToBn /6) /
                (Math.pow((1 + Rt / 2),BnTime) - 1);

        var result3 = Math.floor(result1) * 12 + Math.floor(result2) * 2;
        var result4 = Math.floor(result1) * (YY * 12 + MM) +
                Math.floor(result2) * BnTime;

/*計算結果を表示*/

        f.PayMonth.value = Math.floor(result1);
        f.PayBonus.value = Math.floor(result2);
        f.PayYear.value = Math.floor(result3);
        f.PayTotal.value = Math.floor(result4);

/*--------------------------------------------------------*/
/* 新しいウィンドウで計算結果表示 */
/*--------------------------------------------------------*/
/* しないので、コメントアウト */
/*--------------------------------------------------------*/
/*
        win1=window.open('','anserwin','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,width=320,height=280');
        win1.document.write('<HTML><HEAD><TITLE>ローン試算計算結果ページ</TITLE>');
        win1.document.write('<style type="text/css"><!-- body{margin-left: 0pt; margin-top: 0pt; margin-right:');
        win1.document.write('0pt; margin-bottom: 0pt; font-size: 13px;}');
        win1.document.write('td{font-size: 13px; } --> </style>');
        win1.document.write('</HEAD><BODY><H4>ローン試算計算結果</H4>');
        win1.document.write('● 計算結果は以下の通りとなります。<BR>');
        win1.document.write('● 再計算する時は一旦「閉じる」ボタンでこのウィンドウを閉じてください。<BR>');
        win1.document.write('<TABLE><TR><TD COLSPAN="2"><img src="../html/gifs/trans.gif" width="1" height="8"><BR>');
        win1.document.write('<IMG SRC="../html/sisan/gifs/bt-lo-keisan1.gif" ALT="計算結果" WIDTH="102" HEIGHT="15">');
        win1.document.write('</TD></TR>');
        win1.document.write('<TR><TD BGCOLOR="#FFEEF7" ALIGN="LEFT"><FONT COLOR="#ff0080">●</FONT>毎月の返済額</TD>');
        win1.document.write('<TD BGCOLOR="#FFEEF7" ALIGN="CENTER">：</TD>');
        win1.document.write('<TD BGCOLOR="#FFEEF7" ALIGN="RIGHT">',Math.floor(result1),'円</TD></TR>');
        win1.document.write('<TR><TD><FONT COLOR="#FF0080" ALIGN="LEFT">●</FONT>ボーナス月の加算額</TD><TD ALIGN="CENTER">：</TD>');
        win1.document.write('<TD ALIGN="RIGHT">',Math.floor(result2),'円</TD></TR>');
        win1.document.write('<TR><TD BGCOLOR="#FFEEF7" ALIGN="LEFT"><FONT COLOR="#ff0080">●</FONT>年間返済額</TD>');
        win1.document.write('<TD BGCOLOR="#FFEEF7" ALIGN="CENTER">：</TD>');
        win1.document.write('<TD BGCOLOR="#FFEEF7" ALIGN="RIGHT">',Math.floor(result3),'円</TD></TR>');
        win1.document.write('<TR><TD ALIGN="LEFT"><FONT COLOR="#ff0080">●</FONT>総返済額</TD>');
        win1.document.write('<TD ALIGN="CENTER">：</TD><TD ALIGN="RIGHT">',Math.floor(result4),'円</TD></TR>');
        win1.document.write('</TABLE></BODY></HTML>');
        win1.document.write('<INPUT name="button" type="button" style="border-style:');
        win1.document.write('solid; border-width:1px; border-color:#000099; background:');
        win1.document.write('#FFFFFF;color:#FF0000; cursor:hand" onClick="window.close()"');
        win1.document.write(' value="閉じる">');
*/
}

function clearInArea(f){
        f.Kari.value = "";
        f.Rate.selectedIndex = 0;
        f.Bonus.selectedIndex = 0;
/*      f.ToBonus.selectedIndex = 0; */
        f.YY.selectedIndex = 0;
/*      f.MM.selectedIndex = 0; */
        SetFocus(this.document);
}

function clearOutArea(f){
        f.PayMonth.value = "";
        f.PayBonus.value = "";
        f.PayYear.value = "";
        f.PayTotal.value = "";
}

function chkNum(t) {
        var i = 0;
        for(i = 0; i <  t.length; i++) {
                var s = t.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 chkKr(f) {
        var kingaku = f.Kari.value;
        if (kingaku == ""){
                alert("お借入れ希望額を入力してください");
                f.Kari.focus();
                return false;
        }

        if (!chkNum(kingaku) || kingaku < 1 || kingaku > 99999){
                alert ("1～99999までの整数を入力してください");
                f.Kari.focus();
                return false;
        }
        return true;
}


function chkRt(f) {
        var rate = f.Rate.value;
        if (rate == ""){
                alert ("利率を入力してください");
                f.Rate.focus();
                return false;
        }
         if (!chkNum(rate) || rate < 0.001 || rate > 15.000){
                alert ("0.001～15.000までの数値を入力してください");
                f.Rate.focus();
                return false;
        }
        return true;
}


function chkYYMM(f) {
        var year = f.YY.selectedIndex;
/*      var month= f.MM.selectedIndex; */
        if (year == 0){
                alert("返済期間は1年0カ月～35年0カ月の範囲で入力してください");
                f.YY.focus();
                return false;
        }
/*
        if (year == 35 && month != 0){
                alert ("返済期間は1年0カ月～35年0カ月の範囲で入力してください");
                f.MM.focus();
                return false;
        }
*/
        return true;
}



