// チェックボックスのnameを配列化
var chs = new Array(
                'ch1','ch2','ch3','ch4','ch5','ch6','ch7','ch8','ch9','ch10',
                'ch11','ch12','ch13','ch14','ch15','ch16','ch17','ch18','ch19',
                'ch20','ch21','ch22','ch23','ch24','ch25'
            );

// フォームのリセット
function initvalue() {
    var totalstr = document.getElementById("totalstr");
    var totalstr2 = document.getElementById("totalstr2");
    var totalstr3 = document.getElementById("totalstr3");
    totalstr.innerHTML  = '0分';
    totalstr2.innerHTML = '0円';
    totalstr3.innerHTML = '5,500円';
    
    // 全てのチェックボックスを初期化
    for(var i=0; i<chs.length; i++) {
        document.estimateForm.elements[chs[i]].checked = false;
    }
}

// 合計所要時間を計算する
function calc() {
    var totaltime = 0;
    var price = 0;
    var hour = 0;
    var min = 0;
    var i;
    // 予算(ラジオボタンは8個有り)
    var budget = 0;
    // 予算オーバーフラグ
    var over = false;

    // 所要時間
    var totalstr = document.getElementById("totalstr");
    // 料金
    var totalstr2 = document.getElementById("totalstr2");
    // 設定予算
    var totalstr3 = document.getElementById("totalstr3");

    // 予算チェックボックスを走査して値を得る
    for(i=0; i<8; i++) {
        if(document.estimateForm.budget[i].checked == true) {
            budget = eval(document.estimateForm.budget[i].value);
        }
    }
    
    // 設定予算の表示用にコンマ追加
    var num;
    if(budget > 0) {
        num = numberFormat(budget);
        totalstr3.innerHTML = num +'円';
    }
    
    // 全てのチェックボックスを走査して値の合計を出す
    for(i=0; i<chs.length; i++) {
        if(document.estimateForm.elements[chs[i]].checked == true) {
            totaltime += eval(document.estimateForm.elements[chs[i]].value);
        }
    }
    
    
    // 60分以内は分表示
    // 料金は常に予算内となる
    if(totaltime > 0 & totaltime < 60) {
        totalstr.innerHTML = totaltime +'分';
        totalstr2.innerHTML = '5,500円';
    
    // 60分以上は0時間0分のフォーマットに
    } else if(totaltime >= 60 ) { 
        hour = Math.floor(totaltime / 60)  +'時間';
        min = totaltime % 60;
        if(min == 0) {
            min = '';
        } else {
            min += '分';
        }
        totalstr.innerHTML = hour + min;
        
        // 1時間
        totalstr2.innerHTML = '5,500円';
        
        // 1時間半以内
        if(totaltime > 60 & totaltime <= 90) {
            if(budget >= 8250) {
                totalstr2.innerHTML = '8,250円';
            } else {
                over = true;
            }
        
        // 2時間以内
        } else if(totaltime > 90 & totaltime <= 120) {
            if(budget >= 11000) {
                totalstr2.innerHTML = '11,000円';
            } else {
                over = true;
            }
        
        // 2時間半以内
        } else if(totaltime > 120 & totaltime <= 150) {
            if(budget >= 13750) {
                totalstr2.innerHTML = '13,750円';
            } else {
                over = true;
            }
        
        // 3時間以内
        } else if(totaltime > 150 & totaltime <= 180) {
            if(budget >= 16500) {
                totalstr2.innerHTML = '16,500円';
            } else {
                over = true;
            }

        // 3時間半以内
        } else if(totaltime > 180 & totaltime <= 210) {
            if(budget >= 19250) {
                totalstr2.innerHTML = '19,250円';
            } else {
                over = true;
            }
        
        // 4時間以内
        } else if(totaltime > 210 & totaltime <= 240) {
            if(budget >= 22000) {
                totalstr2.innerHTML = '22,000円';
            } else {
                over = true;
            }

        // 4時間半以内
        } else if(totaltime > 240 & totaltime <= 270) {
            if(budget >= 24750) {
                totalstr2.innerHTML = '24,750円';
            } else {
                over = true;
            }
            
        // 5時間以内
        } else if(totaltime > 270 & totaltime <= 300) {
            if(budget >= 27500) {
                totalstr2.innerHTML = '27,500円';
            } else {
                over = true;
            }
        
        // 5時間半以内
        } else if(totaltime > 300 & totaltime <= 330) {
            if(budget >= 30250) {
                totalstr2.innerHTML = '30,250円';
            } else {
                over = true;
            }
            
        // 6時間以内
        } else if(totaltime > 330 & totaltime <= 360) {
            if(budget >= 33000) {
                totalstr2.innerHTML = '33,000円';
            } else {
                over = true;
            }
        
        // 6時間半以内
        } else if(totaltime > 360 & totaltime <= 390) {
            if(budget >= 35750) {
                totalstr2.innerHTML = '35,750円';
            } else {
                over = true;
            }
            
        // 7時間以内
        } else if(totaltime > 390 & totaltime <= 420) {
            if(budget >= 38500) {
                totalstr2.innerHTML = '38,500円';
            } else {
                over = true;
            }
        
        // 7時間半以内
        /*
        } else if(totaltime > 420 & totaltime <= 450) {
            if(budget >= 41250) {
                totalstr2.innerHTML = '41,250円';
            } else {
                over = true;
            }
        */
        
        // 8時間以内
        } else if(totaltime > 420 & totaltime <= 480) {
            if(budget >= 42000) {
                totalstr2.innerHTML = '42,000円';
            } else {
                over = true;
            }

        // それ以上
        } else if(totaltime > 480) {
            totalstr.innerHTML = '設定外';
            totalstr2.innerHTML = '設定外';
        }
    }
    
    if(over == true) totalstr2.innerHTML = '予算オーバー';
}


// ナンバーフォーマット
function numberFormat(str) {
    /**
     * thanx 'Web系雑記'
     * http://webdev.seesaa.net/article/22769178.html
     */
    var num = new String(str).replace(/,/g, "");
    while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
    return num;
}