// This calculator is based on an example from
//  JavaScript: The Definitive Guide, 3rd Edition.
// That book and the original example were Written by David Flanagan.
// They are Copyright (c) 1996, 1997, 1998 O'Reilly & Associates.
// Modifications and improvements are Copyright 2003 iMaxSales.net
// This example is provided WITHOUT WARRANTY either expressed or implied.
// You may study, use, modify, and distribute it for any purpose,
// as long as this notice is retained.

function calcVals() {

    // Get the user's input from the form. Assume it is all valid.
    // Convert interest from a percentage to a decimal, and convert from
    // an annual rate to a monthly rate. Convert payment period in years
    // to the number of monthly payments.
    
	var hourly = document.calc.hourly.value;
	var fica_percent = document.calc.fica_percent.value;
	var statetax_percent = document.calc.statetax_percent.value;
	var fedtax_percent = document.calc.fedtax_percent.value;
	var workers_comp_rate = document.calc.workers_comp_rate.value;
	var benefit_percent = document.calc.benefit_percent.value;
	var recruiting_percent = document.calc.recruiting_percent.value;
	var admin_percent = document.calc.admin_percent.value;
	var salary = document.calc.salary.value;
	var bloehr_rate = document.calc.bloehr_rate.value;

    // Check that the result is a finite number. If so, display the results
    if (!isNaN(hourly) &&
        (hourly != Number.POSITIVE_INFINITY) &&
        (hourly != Number.NEGATIVE_INFINITY)) {
        hourly = round_decimal(hourly);
        
        document.calc.hourly.value = hourly;
        
        var annual = hourly * 2080;
        document.calc.annual.value = annual;
        
        var fica = round(fica_percent * annual / 100);
        document.calc.fica.value = fica;
        
        var statetax = round(statetax_percent * document.calc.annual.value / 100);
        document.calc.statetax.value = statetax;
        
        var fedtax = round(fedtax_percent * document.calc.annual.value / 100);
        document.calc.fedtax.value = fedtax;
        
        var workers_comp = round(workers_comp_rate * document.calc.annual.value / 100);
        document.calc.workers_comp.value = workers_comp;
        
        var benefit = round(benefit_percent * document.calc.annual.value / 100);
        document.calc.benefit.value = benefit;
        
        var recruiting = round(recruiting_percent * document.calc.annual.value / 100);
        document.calc.recruiting.value = recruiting;
        
        var admin = round(admin_percent * document.calc.annual.value / 100);
        document.calc.admin.value = admin;
        
        var salary = (annual + 
						fica +
						statetax +
						fedtax +
						workers_comp +
						benefit +
						recruiting +
						admin );
						
        document.calc.salary.value = salary;
        
        var actual_hourly = salary / 2080;
        
        var hourly_savings = round_decimal(actual_hourly - bloehr_rate);
        document.calc.hourly_savings.value = hourly_savings;
        
        var annual_savings = round(hourly_savings * 2080);
        document.calc.annual_savings.value = annual_savings;
        
        var ten_year_savings = annual_savings * 10;
        document.calc.ten_year_savings.value = ten_year_savings;
        
        var profit = ten_year_savings * 20;
        document.calc.profit.value = profit;
        								
    }
    // Otherwise, the user's input was probably invalid, so don't
    // display anything.
    else {
        document.calc.profit.value = "";
    }

    return true;
}

// This simple method rounds a number to two decimal places.
function round(x) {
	return Math.ceil(Math.round(x));
}

function round_decimal(x) {
	return (Math.round(x * 100))/100;
}


