﻿// START CAPITAL GAIN

// Code calculator by Max Lawn at BT Financial Corporation
// Use of this calculator is granted if the reference to the maker remains intact
// Alterations may be made with comments to describe your additions
// CVS: $Id: capitalGains.htm,v 1.1.1.1 2006/10/17 17:48:43 nin Exp $

function calculate() {
    bnum = document.getElementById('bnum').value;
    bprice = document.getElementById('bprice').value;
    bdate = document.getElementById('bdate').value;
    snum = document.getElementById('snum').value;
    sprice = document.getElementById('sprice').value;
    sdate = document.getElementById('sdate').value;
    tax = document.getElementById('tax').value;

    galoss = snum * (sprice - bprice);
    galoss = galoss * 100;
    galoss = Math.round(galoss) / 100;


    b = bdate.split("/");
    s = sdate.split("/");

    if (s[2] >= b[2]) {
        if (s[2] - b[2] >= 2)
            displayL(galoss, tax);
        else if (s[0] < b[0])
            displayS(galoss, tax)
        else if (s[0] > b[0]) {
            if (s[2] > b[2])
                displayL(galoss, tax);
            else
                displayS(galoss, tax);
        }
        else if (s[0] == b[0]) {
            if (s[1] > b[1])
                displayL(galoss, tax);
            else
                displayS(galoss, tax);
        }
    }
    else {
        document.getElementById('sgl').value = '------------';
        document.getElementById('lgl').value = '------------';
        document.getElementById('ltax').value = '------------';
        document.getElementById('stax').value = '------------';
    }
}

function displayL(galoss, tax) {
    document.getElementById('lgl').value = '$' + galoss;
    document.getElementById('sgl').value = '------------';
    if (tax == .15)
        totax = galoss * tax;
    else
        totax = galoss * .2;
    totax = totax * 100;
    totax = Math.round(totax) / 100;
    document.getElementById('ltax').value = '$' + totax;
    document.getElementById('stax').value = '------------';

}

function displayS(galoss, tax) {
    document.getElementById('sgl').value = '$' + galoss;
    document.getElementById('lgl').value = '------------';
    totax = galoss * tax;
    totax = totax * 100;
    totax = Math.round(totax) / 100;
    document.getElementById('stax').value = '$' + totax;
    document.getElementById('ltax').value = '------------';
}

// END CAPITAL GAIN

// START LOAN INTERST

function calculate_loaninterest() {
    var amount;
    var rate;
    var months;
    var payment;
    var y;
    var q;
    var hit, i;
    var temp1, temp2;
    var lookFor = new Array(4);

    lookFor[0] = "$";
    lookFor[1] = ",";
    lookFor[2] = "%";
    lookFor[3] = " ";

    /* ----- process the YEARS variable ----- */

    if (document.getElementById('years').value != "") {
        months = document.getElementById('years').value;

        for (i = 0; i < lookFor.length; i++) {
            hit = months.indexOf(lookFor[i]);
            while (hit > -1) {
                temp1 = months.substring(0, hit);
                temp2 = months.substring(hit + 1, months.length);
                months = temp1 + temp2;
                hit = months.indexOf(lookFor[i]);
            }
        }
        months = months * 12;
    }
    else
        months = 0;

    /* ----- process the RATE variable ----- */

    if (document.getElementById('rate').value != "") {
        rate = document.getElementById('rate').value;

        for (i = 0; i < lookFor.length; i++) {
            hit = rate.indexOf(lookFor[i]);
            while (hit > -1) {
                temp1 = rate.substring(0, hit);
                temp2 = rate.substring(hit + 1, rate.length);
                rate = temp1 + temp2;
                hit = rate.indexOf(lookFor[i]);
            }
        }
        rate = rate * 1.0;

    }
    else
        rate = 0;

    /* ----- process the PAYMENT variable ----- */


    if (document.getElementById('payment').value != "") {
        payment = document.getElementById('payment').value;

        for (i = 0; i < lookFor.length; i++) {
            hit = payment.indexOf(lookFor[i]);
            while (hit > -1) {
                temp1 = payment.substring(0, hit);
                temp2 = payment.substring(hit + 1, payment.length);
                payment = temp1 + temp2;
                hit = payment.indexOf(lookFor[i]);
            }
        }
        payment = payment * 1.0;
    }
    else
        payment = 0;

    /* ----- process the AMOUNT variable ----- */

    if (document.getElementById('amount').value != "") {

        amount = document.getElementById('amount').value;

        for (i = 0; i < lookFor.length; i++) {
            hit = amount.indexOf(lookFor[i]);
            while (hit > -1) {
                temp1 = amount.substring(0, hit);
                temp2 = amount.substring(hit + 1, amount.length);
                amount = temp1 + temp2;
                hit = amount.indexOf(lookFor[i]);
            }
        }

        amount = amount * 1.0;
    }
    else
        amount = 0;

    /* ----- perform the rate simulation now ----- */

    if (payment == 0 && months != 0 && rate != 0 && amount != 0) {
        y = rate / 1200;
        q = Math.pow((1 + y), months);
        payment = -1 * (amount * y * q) / (1 - q);
        payment = Math.round(payment * 100) / 100;
        document.getElementById('payment').value = "" + payment;
    } else if (amount == 0 && months != 0 && payment != 0 && rate != 0) {
        y = rate / 1200;
        q = Math.pow((1 + y), months);
        amount = -1 * (payment * (1 - q)) / (y * q);
        amount = Math.round(amount);
        document.getElementById('amount').value = "" + amount;
        document.getElementById('payment').value = "";
        calculate_loaninterest();
    } else if (rate == 0 && payment != 0 && months != 0 && amount != 0) {
        for (i = 1.0; i < 50; i = i + 0.25) {
            y = i / 1200;
            q = Math.pow((1 + y), months);
            test = -1 * (amount * y * q) / (1 - q);
            test = Math.round(test * 100) / 100;
            if (test >= payment) {
                document.getElementById('rate').value = i;
                document.getElementById('payment').value = "";
                calculate_loaninterest();
                return;
            }
        }
        alert("Sorry, could not determine from this data!");
    } else if (months == 0 && rate != 0 && payment != 0 && amount != 0) {
        for (i = 3; i < 600; i++) {
            y = rate / 1200;
            q = Math.pow((1 + y), i);
            test = -1 * (amount * y * q) / (1 - q);
            test = Math.round(test * 100) / 100;
            if (test <= payment) {
                document.getElementById('years').value = "" + Math.round(i / 12 * 100) / 100;
                document.getElementById('payment').value = "";
                calculate_loaninterest();
                return;
            }
        }
        alert("Sorry, could not determine from this data!");

    } else {
        alert("You need to fill in all but one of the fields!");
    }
}
function clear1() {
    document.getElementById('amount').value = "";
}
function clear2() {
    document.getElementById('rate').value = "";
}
function clear3() {
    document.getElementById('years').value = "";
}
function clear4() {
    document.getElementById('payment').value = "";
}


// END LOAN INTERST

// START SAVINGS

function savings_calc2() {
    var amount;
    var rate, newrate;
    var months;
    var y;
    var q, check;
    var hit, i;

    amount = document.getElementById('amount').value;
    months = document.getElementById('months').value;
    rate = document.getElementById('rate').value;
    
    for (i = 0; i < months; i++) {
        y = (amount * (rate * .01));
        amount = eval(amount) + eval(y);

    }

    amount = Math.round(amount * 100) / 100

    document.getElementById('final').value = amount;




} //end Function
// END SAVINGS