﻿function CalculateThis(form)
{
    if(ValidateForm(form))
    {
        var Rate1RadioButton = Rate1Value(form);
        var Rate2RadioButton = Rate2Value(form);
        var Rate3RadioButton = Rate3Value(form);
        var Rate4RadioButton = Rate4Value(form);
        
        var income1F = parseFloat(CurrencyFormatted(form.income1.value)) * Rate1RadioButton;
        var income2F = parseFloat(CurrencyFormatted(form.income2.value)) * Rate2RadioButton;
        var income3F = parseFloat(CurrencyFormatted(form.income3.value)) * Rate3RadioButton;
        var income4F = parseFloat(CurrencyFormatted(form.income4.value)) * Rate4RadioButton;
        
        var totincome = income1F + income2F + income3F + income4F;
        
        var mptax = parseFloat(CurrencyFormatted(form.ptax.value)) / 12;
        var lunchF = parseFloat(CurrencyFormatted(form.lunch.value)) * 4.33;
        var groceryF = parseFloat(CurrencyFormatted(form.grocery.value)) * 4.33;
        var childF = parseFloat(CurrencyFormatted(form.child.value)) * 4.33;
        var lifeF = parseFloat(CurrencyFormatted(form.life.value)) / 12;
        var tuitionF = parseFloat(CurrencyFormatted(form.tuition.value)) * .75;
        
        var housingT = parseFloat(mptax) + parseFloat(CurrencyFormatted(form.mort.value)) + parseFloat(CurrencyFormatted(form.repair.value)) + parseFloat(CurrencyFormatted(form.dues.value));
        var housingM = housingT;
        var housingN = 0;
        
        var utilitiesT = parseFloat(CurrencyFormatted(form.electric.value)) + parseFloat(CurrencyFormatted(form.phone.value)) + parseFloat(CurrencyFormatted(form.water.value)) + parseFloat(CurrencyFormatted(form.cable.value)) + parseFloat(CurrencyFormatted(form.gas.value));
        var utilitiesM = parseFloat(CurrencyFormatted(form.electric.value)) + parseFloat(CurrencyFormatted(form.phone.value)) + parseFloat(CurrencyFormatted(form.water.value)) + parseFloat(CurrencyFormatted(form.gas.value));
        var utilitiesN = parseFloat(CurrencyFormatted(form.cable.value));

        var automotiveT = parseFloat(CurrencyFormatted(form.carpmt.value)) + parseFloat(CurrencyFormatted(form.carpmt2.value)) + parseFloat(CurrencyFormatted(form.autoins.value)) + parseFloat(CurrencyFormatted(form.autogas.value)) + parseFloat(CurrencyFormatted(form.autorepair.value));	
        var automotiveM = automotiveT;
        var automotiveN = 0;

        var foodT = lunchF + groceryF;
        var foodM = groceryF;
        var foodN = lunchF;

        var debtT = parseFloat(CurrencyFormatted(form.ccard.value)) + parseFloat(CurrencyFormatted(form.deptstore.value)) + parseFloat(CurrencyFormatted(form.ploan.value)) + parseFloat(CurrencyFormatted(form.sloan.value));
        var debtM = debtT;
        var debtN = 0;

        var personalT = parseFloat(CurrencyFormatted(form.support.value)) + childF;
        var personalM = personalT;
        var personalN = 0;

        var servicesT = parseFloat(CurrencyFormatted(form.health.value)) + parseFloat(CurrencyFormatted(form.lawn.value)) + parseFloat(CurrencyFormatted(form.news.value)) + parseFloat(CurrencyFormatted(form.magazine.value)) + parseFloat(CurrencyFormatted(form.cellphone.value)) + parseFloat(CurrencyFormatted(form.dryclean.value)) + parseFloat(CurrencyFormatted(form.hclub.value)) + parseFloat(CurrencyFormatted(form.hair.value)) + parseFloat(CurrencyFormatted(form.isp.value));
        var servicesM = parseFloat(CurrencyFormatted(form.health.value));
        var servicesN = parseFloat(CurrencyFormatted(form.lawn.value)) + parseFloat(CurrencyFormatted(form.news.value)) + parseFloat(CurrencyFormatted(form.magazine.value)) + parseFloat(CurrencyFormatted(form.cellphone.value)) + parseFloat(CurrencyFormatted(form.dryclean.value)) + parseFloat(CurrencyFormatted(form.hclub.value)) + parseFloat(CurrencyFormatted(form.hair.value)) + parseFloat(CurrencyFormatted(form.isp.value));

        var charityT = parseFloat(CurrencyFormatted(form.tithe.value)) + parseFloat(CurrencyFormatted(form.otherdon.value)) + parseFloat(CurrencyFormatted(form.gifts.value));
        var charityM = 0;
        var charityN = parseFloat(CurrencyFormatted(form.tithe.value)) + parseFloat(CurrencyFormatted(form.otherdon.value)) + parseFloat(CurrencyFormatted(form.gifts.value));

        var investmentsT = lifeF + parseFloat(CurrencyFormatted(form.fourohonekay.value)) + parseFloat(CurrencyFormatted(form.stocks.value)) + parseFloat(CurrencyFormatted(form.othersav.value));
        var investmentsM = parseFloat(CurrencyFormatted(form.fourohonekay.value));
        var investmentsN = lifeF + parseFloat(CurrencyFormatted(form.stocks.value)) + parseFloat(CurrencyFormatted(form.othersav.value));

        var otherT = parseFloat(CurrencyFormatted(form.travel.value)) + parseFloat(CurrencyFormatted(form.clothes.value)) + parseFloat(CurrencyFormatted(form.entertain.value)) + parseFloat(CurrencyFormatted(form.medical.value)) + parseFloat(CurrencyFormatted(form.other.value)) + tuitionF;
        var otherM = parseFloat(CurrencyFormatted(form.medical.value));
        var otherN = parseFloat(CurrencyFormatted(form.travel.value)) + parseFloat(CurrencyFormatted(form.clothes.value)) + parseFloat(CurrencyFormatted(form.entertain.value)) + parseFloat(CurrencyFormatted(form.other.value)) + tuitionF;

        var allT = housingT + utilitiesT + automotiveT + foodT + debtT + personalT + servicesT + charityT + investmentsT + otherT;
        var allM = housingM + utilitiesM + automotiveM + foodM + debtM + personalM + servicesM + charityM + investmentsM + otherM;
        var allN = housingN + utilitiesN + automotiveN + foodN + debtN + personalN + servicesN + charityN + investmentsN + otherN;

        var extra = totincome - allT;
        var extra2 = totincome - allM;
        
        PrintCalculations(housingT, housingM, housingN, utilitiesT, utilitiesM, utilitiesN, automotiveT, automotiveM, automotiveN, foodT, foodM, 
                                foodN, debtT, debtM, debtN, personalT, personalM, personalN, servicesT, servicesM, servicesN, charityT, charityM, charityN, 
                                investmentsT, investmentsM, investmentsN, otherT, otherM, otherN, totincome, allT, allM, allN, extra, extra2);  
    }                                                  
}

function Rate1Value(form)
{
    for (var i=0; i < form.rate1.length; i++)
    {
        if (form.rate1[i].checked)
        {
            var rad_val = form.rate1[i].value;
            return parseFloat(rad_val);
        }
    }
}

function Rate2Value(form)
{
    for (var i=0; i < form.rate2.length; i++)
    {
        if (form.rate2[i].checked)
        {
            var rad_val = form.rate2[i].value;
            return parseFloat(rad_val);
        }
    }
}

function Rate3Value(form)
{
    for (var i=0; i < form.rate3.length; i++)
    {
        if (form.rate3[i].checked)
        {
            var rad_val = form.rate3[i].value;
            return parseFloat(rad_val);
        }
    }
}

function Rate4Value(form)
{
    for (var i=0; i < form.rate4.length; i++)
    {
        if (form.rate4[i].checked)
        {
            var rad_val = form.rate4[i].value;
            return parseFloat(rad_val);
        }
    }
}

function PrintCalculations(housingT, housingM, housingN, utilitiesT, utilitiesM, utilitiesN, automotiveT, automotiveM, automotiveN, foodT, foodM, 
                            foodN, debtT, debtM, debtN, personalT, personalM, personalN, servicesT, servicesM, servicesN, charityT, charityM, charityN, 
                            investmentsT, investmentsM, investmentsN, otherT, otherM, otherN, totincome, allT, allM, allN, extra, extra2)
{
    var sb = new StringBuilder();
    
    sb.append("<table>");
    sb.append("    <tr>");
    sb.append("        <td><b>Category</b></td>");
    sb.append("        <td><b>Total Expenses</b></td>");
    sb.append("        <td><b>Mandatory Expenses</b></td>");
    sb.append("        <td><b>Non-essential Expenses</b></td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Housing</b></td>");
    sb.append("        <td>" + CurrencyFormatted(housingT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(housingM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(housingN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Utilities</b></td>");
    sb.append("        <td>" + CurrencyFormatted(utilitiesT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(utilitiesM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(utilitiesN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Automotive</b></td>");
    sb.append("        <td>" + CurrencyFormatted(automotiveT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(automotiveM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(automotiveN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Food</b></td>");
    sb.append("        <td>" + CurrencyFormatted(foodT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(foodM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(foodN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Debt Obligations</b></td>");
    sb.append("        <td>" + CurrencyFormatted(debtT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(debtM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(debtN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Personal Obligations</b></td>");
    sb.append("        <td>" + CurrencyFormatted(personalT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(personalM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(personalN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Services</b></td>");
    sb.append("        <td>" + CurrencyFormatted(servicesT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(servicesM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(servicesN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Charity</b></td>");
    sb.append("        <td>" + CurrencyFormatted(charityT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(charityM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(charityN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Investments</b></td>");
    sb.append("        <td>" + CurrencyFormatted(investmentsT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(investmentsM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(investmentsN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Other</b></td>");
    sb.append("        <td>" + CurrencyFormatted(otherT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(otherM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(otherN) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Totals:</b></td>");
    sb.append("        <td>" + CurrencyFormatted(allT) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(allM) + "</td>");
    sb.append("        <td>" + CurrencyFormatted(allN) + "</td>");
    sb.append("    </tr>");
    sb.append("</table><br><br><br>");
    sb.append("<table");
    sb.append("    <tr>");
    sb.append("        <td align=center><font face=\"Arial\" size=4 color=\"blue\"><b>Summary of Monthly Income</b></font></td>");
    sb.append("    </tr>");
    sb.append("</table><br>");
    sb.append("<table>");
    sb.append("    <tr>");
    sb.append("        <td><b>Total of all<br>Monthly Income</b> (after taxes)</td>");
    sb.append("        <td align=center>=</td>");
    sb.append("        <td>" + CurrencyFormatted(totincome) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Total of all<br>Monthly Expenses</b></td>");
    sb.append("        <td align=center>=</td><td>" + CurrencyFormatted(allT) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Amount Remaining<br>After All Expenses</b></td>");
    sb.append("        <td align=center>=</td>");
    sb.append("        <td>" + CurrencyFormatted(extra) + "</td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><b>Amount Remaining<br>After Mandatory Expenses</b></td>");
    sb.append("        <td align=center>=</td>");
    sb.append("        <td>" + CurrencyFormatted(extra2) + "</td>");
    sb.append("    </tr>");
    sb.append("</table>");
	
	sb.append("<p>");
    sb.append("If the amount remaining after all your expenses are paid is positive, then you are in an excellent financial position. You have extra money to use towards your future goals like paying off debts or investing for your future.");
    sb.append("</p>");
    sb.append("<p>");
    sb.append("If the amount remaining after all your mandatory expenses are paid is positive, while your overall expenses are negative, you are in a good financial position to cut your non-essential expenses and remain within your budget. If the amount remaining after all your mandatory expenses are paid is negative, then you have two choices: You can increase your income to meet your budgetary requirements or you can make some lifestyle changes to reduce your monthly debt.");
    sb.append("</p>");
    sb.append("<p>");
    sb.append("We offer some possible solutions to help you make the most of your money: Trade in a car with high repair bills & bad mpg for a newer more fuel efficient vehicle with a warranty (ya shoulda known THAT one would be first on our list). ");
    sb.append("</p>");
    sb.append("<ol>");
    sb.append("<li>");
    sb.append("Don’t let your finances get “watered down” – instead of buying bottled water, just fill the bottles up with tap water and refrigerate. It tastes just as good as the other stuff that’s a dollar a bottle.");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Don’t be a chicken when it comes to saving money – Buy chicken on sale & freeze it. One or two pieces of chicken are great on the grill, mixed with rice, mixed with vegetables, in a casserole… you can have chicken 3 or 4 meals a week at home and it’s better for you (and cheaper) than red meat. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Cut down on long-distance phone usage and eliminate non-essential phone options such as call-waiting, caller ID, and voice mail. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Electric bills can be shocking – Make sure your ceiling fans are on when you’re in the room, and OFF when you’re not. Having a fan in a room will allow you to keep your thermostat set 4 degrees higher and have the same level of comfort. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Be as conscientious as possible with your electricity and use alternatives whenever possible, like using a swamp cooler instead of the air conditioner. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Get a timer for your water heater. Many electric companies have a “time of use” plan that allows you to take advantage of cheaper rates during “non-peak” hours. Once you sign up for the plan, make the most of it by purchasing a timer for your water heater, so it will only turn on during the non-peak hours.");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Cut your water bill if possible by not watering your lawn as often, or replacing your lawn with low-maintenance ground cover. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Shop for less expensive auto insurance. Consolidate all your insurance (home, auto, and life) into one company that can give you preferred discounts. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Many people think your car will use less gas if you have the windows rolled down instead of using the AC, but that is not true. With your windows rolled down, your car is less aerodynamic and you actually get less mpg than you would if you had the windows rolled up and the AC on full blast. It’s Arizona, so you might as well enjoy that AC!");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Use coupons and value cards when grocery shopping and take advantage of weekly specials. Purchase the best bargains from several stores instead of just shopping at one supermarket. Buy products in bulk whenever possible. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("To reduce your debt obligations, either refinance or consolidate as much of your bank and student loan debt as possible. Often, refinancing will earn you lower interest rates. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Pack a lunch!! Even if you’re not a big fan of leftovers, you WILL be a big fan of the money you’ll save each month packing a lunch to work. On average you can save around $100 each month, not to mention the fact that packing a lunch is usually healthier than going out to eat. It’s an extra bonus that packing your lunch (unless you pack nothing but Twinkies) will help you to eat healthier and lose weight. That’s a pretty great bonus!");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Quit smoking! If you have a pack a day habit, this can save you around $150 a month. Wouldn’t you like to have an extra $1,800 each year? Quitting smoking could also save you money on your insurance and health care, not to mention all of the other health benefits.");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Your best option for reducing child-care expenses is to use the assistance of any relatives living near you. Some relatives may be surprisingly willing to assist you by watching your children either sporadically or permanently. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("You may be able to reduce the amount of health insurance you have through your employer, or increase your deductible to lower the amount deducted from your paycheck. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("If you have a 401K or similar tax-deferred retirement account through your employer, you can eliminate this deduction from your paycheck by closing your account. Keep in mind, however, that this retirement account may be your only supplement to Social Security if you have not made other arrangements. Also, due to the tax savings of 401K accounts, you may pay more in taxes by canceling the account than you would normally through your payroll deduction. Make sure you only take advantage of this option in case of emergencies. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("Remember that the tax code is hundreds of pages long and changes every year. Depending on your particular situation, it may be worth your while to have your taxes done professionally by an accountant. Tax accountants can save you a lot of money through deductions that you might not know are even available to you. ");
    sb.append("</li>");
    sb.append("<li>");
    sb.append("We saved THIS tip for last because it’s our favorite… PLAN YOUR DREAM STAYCATION! The cost of fuel and other expenses has caused vacation planning to become very expensive and stressful. You can still have the dream vacation you want if you plan your STAYcation the Mr. Ed way. Set the date and tell your family about the upcoming trip to “Home Sweet Home”. One night you can pitch a tent in the backyard (or the living room if you live in an apartment), make some smores and take turns telling silly ghost stories. You could also have a “Night at the Theatre” where your kids put together a play, and you can be the audience (just make sure they don’t try to charge ya too much for the tickets!). There are tons of great museums in Arizona, so you could spend a day hopping from one exhibit to the next. If the weather permits, how about a fun hiking trip (don’t forget the sunscreen), or you can break out the slip & slide and see who can set the fastest land speed record! There are tons of fun things you can do right at home… if you have kids, they can probably think of many fun and creative activities. Have fun!");
    sb.append("</li>");
    
    var s = sb.toString();
    
    document.writeln(s);
}

function StringBuilder(value)
{
    this.strings = new Array("");
    this.append(value);            
}

StringBuilder.prototype.append = function(value)
{
    if(value)
    {
        this.strings.push(value);
    }
}

StringBuilder.prototype.toString = function()
{
    return this.strings.join("");
}

function IsNotEmpty(elem)
{
    var str = elem.value;
    var re = /.+/;
    if(!str.match(re))
    {
        alert("Please fill in all required fields.");
        return false;
    }
    else
    {
        return true;
    }
}

function ValidateForm(form)
{
    // for now simply return true
    return true;
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	// make sure we have only two decimal places should there be more passed in
	i = i.toFixed(2);
	var minus = '';
	
	if(isNaN(i)) 
	{ 
	    i = 0.00; 
	}
	
	if(i < 0) 
	{ 
	    minus = '-'; 
	}
	
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	
	s = new String(i);
	
	if(s.indexOf('.') < 0) 
	{ 
	    s += '.00'; 
	}	
	
	if(s.indexOf('.') == (s.length - 2)) 
	{ 
	    s += '0'; 
	}
	
	s = minus + s;
	
	return s;
}
