﻿/// <reference path="~/App_JS/jquery-1.3.2-vsdoc2.js" />
function GiveBackPower() { //還款能力查詢
    var PayByMonth = $("#PayByMonth"); //每月房貸支出 單位:萬元
    var SelfPrepare = $("#SelfPrepare"); //自備款 單位:萬元
    var InterestRate = $("#InterestRate") //貸款年利率 單位:%
    var LoanYear = $("#LoanYear"); //貸款年限 單位:年
    var LoanMonth = LoanYear.val() * 12;
    var sResult = 0;
    var rm = InterestRate.val() / 100 / 12;
    sResult = ((PayByMonth.val() * 10000) * (1 - (1 / Math.pow(1 + rm, LoanMonth))) / rm) + (SelfPrepare.val() * 10000);
    sResult = Math.round(sResult / 10000);
    $("#sResult").html(sResult);
    SetSearchPrice(sResult * 10000);
    HideShowResult();
}

function CalLoan() { //本息平均攤還試算
    var LoanMoney = $("#LoanMoney");
    if (LoanMoney.val() == '') { alert('請輸入貸款總額'); LoanMoney.focus(); return false; }
    var InterestRate = $("#InterestRate");
    if (InterestRate.val() == '') { alert('請輸入貸款年利率'); InterestRate.focus(); return false; }
    var LoanYear = $("#LoanYear");
    if (LoanYear.val() == '') { alert('請輸入貸款年限'); LoanYear.focus(); return false; }
    var LnNetAmt = LoanMoney.val() * 10000;
    var RealInterest = InterestRate.val() / 100 / 12;
    var IPower = Math.pow((1 + RealInterest), 12 * LoanYear.val());
    var avgRatePayBack = (IPower * RealInterest) / (IPower - 1);

    $("#sPayMoney1").html(Math.round(LnNetAmt * avgRatePayBack));

    SetSearchPrice(LoanMoney.val() * 1.3 * 10000);
    HideShowResult();
}

function Section3() { //三階段調息試算
    var i, a, TotalMonth;
    var LoanMoney = $("#LoanMoney").val() * 10000;
    var LoanYear = $("#LoanYear").val();
    var IR1 = $("#InterestRate1").val() * 0.01;
    var IR2 = $("#InterestRate2").val() * 0.01;
    var IR3 = $("#InterestRate3").val() * 0.01;
    var PayMoney1 = 0; var PayMoney2 = 0; var PayMoney3 = 0; var c = 0; var d = 0;
    with (Math) {
        TotalMonth = LoanYear * 12;
        for (i = 1; i < TotalMonth + 1; i++) { a = pow((1 / (1 + IR1 / 12)), i); PayMoney1 += a }
        PayMoney1 = LoanMoney / PayMoney1;

        for (i = 1; i < TotalMonth - 12 + 1; i++) { a = pow((1 / (1 + IR1 / 12)), i); c += a }
        for (i = 1; i < TotalMonth - 12 + 1; i++) { a = pow((1 / (1 + IR2 / 12)), i); PayMoney2 += a }
        PayMoney2 = PayMoney1 / PayMoney2 * c;

        for (i = 1; i < TotalMonth - 24 + 1; i++) { a = pow((1 / (1 + IR2 / 12)), i); d += a }
        for (i = 1; i < TotalMonth - 24 + 1; i++) { a = pow((1 / (1 + IR3 / 12)), i); PayMoney3 += a }
        PayMoney3 = PayMoney2 / PayMoney3 * d;
        $("#sPayMoney1").html(add_comma(Math.floor(PayMoney1)));
        $("#sPayMoney2").html(add_comma(Math.floor(PayMoney2)));
        $("#sPayMoney3").html(add_comma(Math.floor(PayMoney3)));

        SetSearchPrice(LoanMoney * 1.3);
        HideShowResult();
    }
}

function add_comma(num) {
    num = new String(num);
    var numArray = num.split('.');
    if (numArray.length == 2) //有小數點
        num = numArray[0].toString();

    string = new String(num)
    var f_str = string.length % 3;
    var x = Math.floor(string.length / 3);
    if (f_str != 0) {
        var rult = string.substring(0, f_str);
        for (i = 1; i < x + 1; i++)
            rult += ',' + string.substring((f_str + (i - 1) * 3), (f_str + i * 3));
        if (numArray.length == 2) //有小數點
            return rult + '.' + numArray[1].substring(0, 3);
        else //沒小數點
            return rult;
    }
    else {
        f_str = 3;
        var rult = string.substring(0, f_str)
        for (i = 1; i < x; i++)
            rult += ',' + string.substring((f_str + (i - 1) * 3), (f_str + i * 3));
        ;
        if (numArray.length == 2) //有小數點
            return rult + '.' + numArray[1].substring(0, 3);
        else //沒小數點
            return rult;
    }
}

function HideShowResult() {
    var CalculateResult1 = $("#CalculateResult1");
    CalculateResult1.css("display", "none");
    CalculateResult1.css("display", "block");
    CalculateResult1.css("height", "0px");
    CalculateResult1.animate({ height: '590px' }, 1000);
}

function SetSearchPrice(Price) {
    $("#ctl00_ContentPlaceHolder1_txtPriceStart").val(Math.round(Price / 10000 * 0.85));
    $("#ctl00_ContentPlaceHolder1_txtPriceEnd").val(Math.round(Price / 10000 * 1.15));
}
