// JavaScript Document

function updateCalc() {
	
	//Set up variables to capture data in form on Income side
	
	//Retirement
	var retirement = parseFloat(document.getElementById("retirement").value);
	
	if (document.getElementById("retirement").value == "" || isNaN(document.getElementById("retirement").value)) {
	
		retirement = 0;
		
		if(isNaN(document.getElementById("retirement").value)) {
				
			alert("Please enter only numbers in the 'Retirement plan distributions' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Social Security
	var benefits = parseFloat(document.getElementById("benefits").value);
	
	if (document.getElementById("benefits").value == "" || isNaN(document.getElementById("benefits").value)) {
	
		benefits = 0;
		
		if(isNaN(document.getElementById("benefits").value)) {
				
			alert("Please enter only numbers in the 'Social Security benefits' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Pension Plan Distributions
	var pension = parseFloat(document.getElementById("pension").value);
	
	if (document.getElementById("pension").value == "" || isNaN(document.getElementById("pension").value)) {
	
		pension = 0;
		
		if(isNaN(document.getElementById("pension").value)) {
				
			alert("Please enter only numbers in the 'Pension plan distributions' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//SEP and Keogh
	var ira = parseFloat(document.getElementById("ira").value);
	
	if (document.getElementById("ira").value == "" || isNaN(document.getElementById("ira").value)) {
	
		ira = 0;
		
		if(isNaN(document.getElementById("ira").value)) {
				
			alert("Please enter only numbers in the 'SEP and Keogh withdrawals' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Annuity payments
	var annuity = parseFloat(document.getElementById("annuity").value);
	
	if (document.getElementById("annuity").value == "" || isNaN(document.getElementById("annuity").value)) {
	
		annuity = 0;
		
		if(isNaN(document.getElementById("annuity").value)) {
				
			alert("Please enter only numbers in the 'Annuity payments' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Personal savings
	var savings = parseFloat(document.getElementById("savings").value);
	
	if (document.getElementById("savings").value == "" || isNaN(document.getElementById("savings").value)) {
	
		savings = 0;
		
		if(isNaN(document.getElementById("savings").value)) {
				
			alert("Please enter only numbers in the 'Personal Savings' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Part-time work salary
	var salary = parseFloat(document.getElementById("salary").value);
	
	if (document.getElementById("salary").value == "" || isNaN(document.getElementById("salary").value)) {
	
		salary = 0;
		
		if(isNaN(document.getElementById("salary").value)) {
				
			alert("Please enter only numbers in the 'Part-time work salary' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Rent or Royalties
	var royalties = parseFloat(document.getElementById("royalties").value);
	
	if (document.getElementById("royalties").value == "" || isNaN(document.getElementById("royalties").value)) {
	
		royalties = 0;
		
		if(isNaN(document.getElementById("royalties").value)) {
				
			alert("Please enter only numbers in the 'Rent or royalties' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Other income
	var otherIncome = parseFloat(document.getElementById("otherIncome").value);
	
	if (document.getElementById("otherIncome").value == "" || isNaN(document.getElementById("otherIncome").value)) {
	
		otherIncome = 0;
		
		if(isNaN(document.getElementById("otherIncome").value)) {
				
			alert("Please enter only numbers in the 'Other Income' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	
	//Set up variables to capture data in form on Expense side
	
	//Rent
	var rent = parseFloat(document.getElementById("rent").value);
	
	if (document.getElementById("rent").value == "" || isNaN(document.getElementById("rent").value)) {
	
		rent = 0;
		
		if(isNaN(document.getElementById("rent").value)) {
				
			alert("Please enter only numbers in the 'Rent/Mortgage payments' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Loan & Auto
	var loan = parseFloat(document.getElementById("loan").value);
	
	if (document.getElementById("loan").value == "" || isNaN(document.getElementById("loan").value)) {
	
		loan = 0;
		
		if(isNaN(document.getElementById("loan").value)) {
				
			alert("Please enter only numbers in the 'Loan & Auto' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Insurance premiums
	var insurance = parseFloat(document.getElementById("insurance").value);
	
	if (document.getElementById("insurance").value == "" || isNaN(document.getElementById("insurance").value)) {
	
		insurance = 0;
		
		if(isNaN(document.getElementById("insurance").value)) {
				
			alert("Please enter only numbers in the 'Insurance premiums' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Groceries
	var groceries = parseFloat(document.getElementById("groceries").value);
	
	if (document.getElementById("groceries").value == "" || isNaN(document.getElementById("groceries").value)) {
	
		groceries = 0;
		
		if(isNaN(document.getElementById("groceries").value)) {
				
			alert("Please enter only numbers in the 'Groceries/Dining Out' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Utilities
	var utilities = parseFloat(document.getElementById("utilities").value);
	
	if (document.getElementById("utilities").value == "" || isNaN(document.getElementById("utilities").value)) {
	
		utilities = 0;
		
		if(isNaN(document.getElementById("utilities").value)) {
				
			alert("Please enter only numbers in the 'Utilites' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Transportation
	var transportation = parseFloat(document.getElementById("transportation").value);
	
	if (document.getElementById("transportation").value == "" || isNaN(document.getElementById("transportation").value)) {
	
		transportation = 0;
		
		if(isNaN(document.getElementById("transportation").value)) {
				
			alert("Please enter only numbers in the 'Transportation' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Entertainment
	var entertainment = parseFloat(document.getElementById("entertainment").value);
	
	if (document.getElementById("entertainment").value == "" || isNaN(document.getElementById("entertainment").value)) {
	
		entertainment = 0;
		
		if(isNaN(document.getElementById("entertainment").value)) {
				
			alert("Please enter only numbers in the 'Entertainment/recreation' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Household services
	var household = parseFloat(document.getElementById("household").value);
	
	if (document.getElementById("household").value == "" || isNaN(document.getElementById("household").value)) {
	
		household = 0;
		
		if(isNaN(document.getElementById("household").value)) {
				
			alert("Please enter only numbers in the 'Household services' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Clothing
	var clothing = parseFloat(document.getElementById("clothing").value);
	
	if (document.getElementById("clothing").value == "" || isNaN(document.getElementById("clothing").value)) {
	
		clothing = 0;
		
		if(isNaN(document.getElementById("clothing").value)) {
				
			alert("Please enter only numbers in the 'Clothing' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Home maintenance
	var maintenance = parseFloat(document.getElementById("maintenance").value);
	
	if (document.getElementById("maintenance").value == "" || isNaN(document.getElementById("maintenance").value)) {
	
		maintenance = 0;
		
		if(isNaN(document.getElementById("maintenance").value)) {
				
			alert("Please enter only numbers in the 'Home maintenance' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Travel
	var travel = parseFloat(document.getElementById("travel").value);
	
	if (document.getElementById("travel").value == "" || isNaN(document.getElementById("travel").value)) {
	
		travel = 0;
		
		if(isNaN(document.getElementById("travel").value)) {
				
			alert("Please enter only numbers in the 'Travel' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Hobbies
	var hobbies = parseFloat(document.getElementById("hobbies").value);
	
	if (document.getElementById("hobbies").value == "" || isNaN(document.getElementById("hobbies").value)) {
	
		hobbies = 0;
		
		if(isNaN(document.getElementById("hobbies").value)) {
				
			alert("Please enter only numbers in the 'Hobbies' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Healthcare
	var healthcare = parseFloat(document.getElementById("healthcare").value);
	
	if (document.getElementById("healthcare").value == "" || isNaN(document.getElementById("healthcare").value)) {
	
		healthcare = 0;
		
		if(isNaN(document.getElementById("healthcare").value)) {
				
			alert("Please enter only numbers in the 'Health-care related expenses' field. It will not be calculated otherwise.");
				
		}
		
	}

	//Gifts
	var gifts = parseFloat(document.getElementById("gifts").value);
	
	if (document.getElementById("gifts").value == "" || isNaN(document.getElementById("gifts").value)) {
	
		gifts = 0;
		
		if(isNaN(document.getElementById("gifts").value)) {
				
			alert("Please enter only numbers in the 'Gifts/charitable contributions' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	//Other Expenses
	var otherExpenses = parseFloat(document.getElementById("otherExpenses").value);
	
	if (document.getElementById("otherExpenses").value == "" || isNaN(document.getElementById("otherExpenses").value)) {
	
		otherExpenses = 0;
		
		if(isNaN(document.getElementById("otherExpenses").value)) {
				
			alert("Please enter only numbers in the 'Other Expenses' field. It will not be calculated otherwise.");
				
		}
		
	}
	
	
	//Set up variables for totals
	var incomeSubtotal = retirement + benefits + pension + ira + annuity + savings + salary + royalties + otherIncome;
	var expenseSubtotal = rent + loan + insurance + groceries + utilities + transportation + entertainment + household + clothing + maintenance + travel + hobbies + healthcare + gifts + otherExpenses;
	var total = incomeSubtotal - expenseSubtotal;
	
	//Output totals
	document.getElementById("incomeSubtotal").value = incomeSubtotal.toFixed(2);
	document.getElementById("expenseSubtotal").value = expenseSubtotal.toFixed(2);
	document.getElementById("total").value = "$" + total.toFixed(2);
	
		
}