var currentChildren = 0;
var oldCollegeCosts = 0
var hidNeededAnnIncome = 0;
var hidAvailAnnIncome = 0;
var hidFuneralExpenses = 0;
var hidMortgage = 0;
var hidCollegeCosts = 0;
var hidRetirement = 0;
var hidSavings = 0;
var hidCurrentLifeInsurance = 0;

var hidIncome = 0;
var hidExpenses = 0;
var hidAssets = 0;

var valueTotalCost = 0;
var valueTotalInflation = 0;
var childCareTotalCost = 0;
var childCareWeeklyCost = 0;
var childCareTotalInflation = 0;
var drivingTotalCost = 0;
var drivingWeeklyCost = 0;
var drivingTotalInflation = 0;
var schoolTotalCost = 0; 
var schoolTotalInflation = 0;
var schoolWeeklyCost = 0;
var cleaningTotalCost = 0; 
var cleaningTotalInflation = 0;
var cleaningWeeklyCost = 0;
var foodTotalCost = 0; 
var foodTotalInflation = 0;
var foodWeeklyCost = 0;
var bookTotalCost = 0; 
var bookTotalInflation = 0;
var bookWeeklyCost = 0;
var yardTotalCost = 0; 
var yardTotalInflation = 0;
var yardWeeklyCost = 0;
var maintenanceTotalCost = 0; 
var maintenanceTotalInflation = 0;
var maintenanceWeeklyCost = 0;
var careTotalCost = 0; 
var careTotalInflation = 0;
var careWeeklyCost = 0;
var other1TotalCost = 0; 
var other1TotalInflation = 0;
var other1WeeklyCost = 0;
var other2TotalCost = 0;
var other2TotalInflation = 0;
var other2WeeklyCost = 0;

var arrIncomeNeeded = new Array();
arrIncomeNeeded[0] = 0;
arrIncomeNeeded[1] = 1;
arrIncomeNeeded[2] = 1.019717;
arrIncomeNeeded[3] = 1.029159;
arrIncomeNeeded[4] = 1.0383337;
arrIncomeNeeded[5] = 1.0472488;
arrIncomeNeeded[6] = 1.0559116;
arrIncomeNeeded[7] = 1.0643292;
arrIncomeNeeded[8] = 1.0725085;
arrIncomeNeeded[9] = 1.0804564;
arrIncomeNeeded[10] = 1.0881793;
arrIncomeNeeded[11] = 1.0956837;
arrIncomeNeeded[12] = 1.1029757;
arrIncomeNeeded[13] = 1.1100612;
arrIncomeNeeded[14] = 1.1169463;
arrIncomeNeeded[15] = 1.1236365;
arrIncomeNeeded[16] = 1.1301374;
arrIncomeNeeded[17] = 1.1364549;
arrIncomeNeeded[18] = 1.1425923;
arrIncomeNeeded[19] = 1.1485567;
arrIncomeNeeded[20] = 1.1543522;
arrIncomeNeeded[25] = 1.1809606;
arrIncomeNeeded[30] = 1.2040108;
arrIncomeNeeded[35] = 1.2239786;
arrIncomeNeeded[40] = 1.2412763;
arrIncomeNeeded[45] = 1.2562609;
arrIncomeNeeded[50] = 1.2692417;

var collegeCosts = new Array();
collegeCosts[1] = 61499;
collegeCosts[2] = 64574;
collegeCosts[3] = 67801;
collegeCosts[4] = 71192;
collegeCosts[5] = 74752;
collegeCosts[6] = 78488;
collegeCosts[7] = 82414;
collegeCosts[8] = 86537;
collegeCosts[9] = 90860;
collegeCosts[10] = 95405;
collegeCosts[11] = 100174;
collegeCosts[12] = 105185;
collegeCosts[13] = 110442;
collegeCosts[14] = 115969;
collegeCosts[15] = 121763;
collegeCosts[16] = 127854;
collegeCosts[17] = 134240;
collegeCosts[18] = 140957;
collegeCosts[19] = 148005;
collegeCosts[20] = 155407;

var privateCollege = new Array();
privateCollege[1] = 146215;
privateCollege[2] = 153521;
privateCollege[3] = 161200;
privateCollege[4] = 169261;
privateCollege[5] = 177723;
privateCollege[6] = 186610;
privateCollege[7] = 195941;
privateCollege[8] = 205741;
privateCollege[9] = 216027;
privateCollege[10] = 226825;
privateCollege[11] = 238161;
privateCollege[12] = 250075;
privateCollege[13] = 262577;
privateCollege[14] = 275705;
privateCollege[15] = 289491;
privateCollege[16] = 303969;
privateCollege[17] = 319165;
privateCollege[18] = 335128;
privateCollege[19] = 351884;
privateCollege[20] = 369474;

$(document).ready(function() {	
	$("#showHideDetails").click(function() {
		if ($("#collegeTable").css("display") == "none") {
			$("#collegeTable").css("display", "inline");
			$(this).html("Hide College Calculator");
		} else {
			$("#collegeTable").css("display", "none");
			$(this).html("Show College Calculator");
		}
	});
	
	$("#numChildren").change(function() {
		if (currentChildren < $("#numChildren").val()) {
			var newChildren = $("#numChildren").val();
			for (i=currentChildren;i<=newChildren;i++) {
				var childRow = "#childRow" + i;
				//$(childRow).css("display", "table-row");
				$(childRow).show();
			}
			currentChildren = newChildren;
			calculateCollegeCosts();
		} else if (currentChildren > $("#numChildren").val()) {
			var newChildren = $("#numChildren").val();			
			for (i=currentChildren;i>newChildren;i--) {
				var childRow = "#childRow" + i;
				//$(childRow).css("display", "none");
				$(childRow).hide();
				var idName = "#yearsUntilCollege" + i;
				$(idName).val("1");
				$("#schoolType" + i).val("public");
			}
			currentChildren = newChildren;
			calculateCollegeCosts();
		}	
		
		if (currentChildren == 0) {
			$('#collegeHeader').hide();
			$('#collegeFooter').hide();
		} else {
			$('#collegeHeader').show();
			$('#collegeFooter').show();
		}
	});	
	
	$("#collegeCosts").keyup(function() {
		if ($("#numChildren").val() != 0) {
			$("#collegeCosts").val(oldCollegeCosts);
			alert("Please change your number of children to 0 in the college costs calculator if you wish to manually enter a college total.");
		} else {
			calculateExpenses();
		}
	});
	
	$(".numValidate").blur(function() {
		if (isNaN(parseInt($(this).val()))) {
			$(this).val('0');
		} else {
			$(this).val(parseInt($(this).val())+'')
		}
	});
	
	$(".numValidate").blur(function() {
		$(this).s							
	});
	
	$(".numValidate").click(function() {
	
		$(this).select();
	});
	
	$("#calculatorTab").click(function() {
		if ($("#calculator").css("display") == "none") {
			$("#value").css({ display: "none", zIndex: 300 });
			$("#calculator").css({ display: "block", zIndex: 100 });
			$("#valueTab").css({ zIndex: 100, background: "#ffffff" });
			$("#calculatorTab").css({ zIndex: 300, background: "#E4EBF1" });
		}
	});

	$("#valueTab").click(function() {
		if ($("#value").css("display") == "none") {
			$("#calculator").css({ display: "none", zIndex: 300 });
			$("#value").css({ display: "block", zIndex: 100 });
			$("#calculatorTab").css({ zIndex: 100, background: "#ffffff" });
			$("#valueTab").css({ zIndex: 300, background: "#E4EBF1" });
		}
	});	
	
	$("#homeValueBtn").click(function() {
		$("#calculator").css("display", "none");
		$("#value").css("display", "block");
	});	
});

function calculateIncome() {
	
	if (isNaN(parseInt($("#neededAnnIncome").val()))) {
		hidNeededAnnIncome = 0;
	} else {
		hidNeededAnnIncome = parseInt($("#neededAnnIncome").val());
	}
	
	if (isNaN(parseInt($("#annIncomeAvail").val()))) {
		hidAvailAnnIncome = 0;
	} else {
		hidAvailAnnIncome = parseInt($("#annIncomeAvail").val());
	}
	
	// Calculate annual income to be replaced 
	var annIncomeReplaced = hidNeededAnnIncome - hidAvailAnnIncome;
	$("#annIncomeReplaced").html(addCommas(annIncomeReplaced));
	
	// calculate the total funds needed based on years
	var yrsFundsNeeded = parseInt($("#yrsFundsNeeded").val());
	var incomeNeededBeforeInflation = annIncomeReplaced * yrsFundsNeeded;
	var totFundsNeeded = Math.round(incomeNeededBeforeInflation * arrIncomeNeeded[yrsFundsNeeded]);
	$("#incomeTotal").html(addCommas(totFundsNeeded));
	hidIncome = totFundsNeeded;
	calculateNeededInsurance();
}

function calculateExpenses() {
	
	if(isNaN(parseInt($("#funeralExpenses").val()))) {
		hidFuneralExpenses = 0;
	} else {
		hidFuneralExpenses = parseInt($("#funeralExpenses").val());
	}
	
	if(isNaN(parseInt($("#mortgage").val()))) {
		hidMortgage = 0;
	} else {
		hidMortgage = parseInt($("#mortgage").val());
	}
	
	if(isNaN(parseInt($("#collegeCosts").val()))) {
		hidCollegeCosts = 0;
	} else {
		hidCollegeCosts = parseInt($("#collegeCosts").val());
	}
	
	var totalCapital = hidFuneralExpenses + hidMortgage + hidCollegeCosts + valueTotalInflation;
	$("#totalCapital").html(addCommas(totalCapital));
	oldCollegeCosts = hidCollegeCosts;
	hidExpenses = totalCapital;
	calculateNeededInsurance();
}

function calculateAssets() {
	if(isNaN(parseInt($("#savings").val()))) {
		hidSavings = 0;
	} else {
		hidSavings = parseInt($("#savings").val());
	}
	
	if(isNaN(parseInt($("#retirement").val()))) {
		hidRetirement = 0;
	} else {
		hidRetirement = parseInt($("#retirement").val());
	}
	
	if(isNaN(parseInt($("#currentLifeInsurance").val()))) {
		hidCurrentLifeInsurance = 0;
	} else {
		hidCurrentLifeInsurance = parseInt($("#currentLifeInsurance").val());
	}
	
	var totalAssets = hidSavings + hidRetirement + hidCurrentLifeInsurance;
	hidAssets = totalAssets;
	$("#totalAssets").html(addCommas(totalAssets));
	calculateNeededInsurance();
}

function calculateNeededInsurance() {
	var neededInsurance = (hidIncome + hidExpenses) - hidAssets;
	$("#insuranceNeeded").html(addCommas(neededInsurance));
}

function calculateCollegeCosts() {
	var i = 1;
	var totalCollegeCosts = 0;
	
	for (i=1;i<=currentChildren;i++) {
		var idName = "#yearsUntilCollege" + i;
		var yearsUntilCollege = parseInt($(idName).val());
		
		if ($("#schoolType" + i).val() == "public") {
			var collegeTotal = collegeCosts[yearsUntilCollege];
		} else {
			var collegeTotal = privateCollege[yearsUntilCollege];
		}
		
		idName = "#collegeTotal" + i;
		$(idName).html(addCommas(collegeTotal));
		totalCollegeCosts += collegeTotal;
	}
	
	$("#collegeCosts").val(totalCollegeCosts);
	$("#totalCollegeCosts").html(addCommas(totalCollegeCosts));
	calculateExpenses();
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function resetChildServicesNOY() {
	$("#childCareNOY").val($("#childServicesNOY").val());
	$("#drivingNOY").val($("#childServicesNOY").val());
	$("#schoolNOY").val($("#childServicesNOY").val());
}

function resetOtherServicesNOY() {
	$("#cleaningNOY").val($("#otherServicesNOY").val());
	$("#foodNOY").val($("#otherServicesNOY").val());
	$("#bookNOY").val($("#otherServicesNOY").val());
	$("#yardNOY").val($("#otherServicesNOY").val());
	$("#maintenanceNOY").val($("#otherServicesNOY").val());
	$("#careNOY").val($("#otherServicesNOY").val());
	$("#other1NOY").val($("#otherServicesNOY").val());
	$("#other2NOY").val($("#otherServicesNOY").val());
}

function calculateValue() {
	if ($("#childCareNOY").val() != '0') {
		childCareWeeklyCost = parseInt($("#childCareHPW").val()) * parseInt($("#childCareHR").val());
		$("#childCareWeeklyCost").html(addCommas(childCareWeeklyCost));
		childCareTotalCost = parseInt($("#childCareNOY").val()) * childCareWeeklyCost * 52;
		$("#childCareTotalCost").html(addCommas(childCareTotalCost));
		childCareTotalInflation = arrIncomeNeeded[parseInt($("#childCareNOY").val())] * childCareTotalCost;
	}
	
	if ($("#drivingNOY").val() != '0') {
		drivingWeeklyCost = parseInt($("#drivingHPW").val()) * parseInt($("#drivingHR").val());
		$("#drivingWeeklyCost").html(addCommas(drivingWeeklyCost));
		drivingTotalCost = parseInt($("#drivingNOY").val()) * drivingWeeklyCost * 52;
		$("#drivingTotalCost").html(addCommas(drivingTotalCost));
		drivingTotalInflation = arrIncomeNeeded[parseInt($("#drivingNOY").val())] * drivingTotalCost;
	}
	
	if ($("#schoolNOY").val() != '0') {
		schoolWeeklyCost = parseInt($("#schoolHPW").val()) * parseInt($("#schoolHR").val());
		$("#schoolWeeklyCost").html(addCommas(schoolWeeklyCost));
		schoolTotalCost = parseInt($("#schoolNOY").val()) * schoolWeeklyCost * 52;
		$("#schoolTotalCost").html(addCommas(schoolTotalCost));
		schoolTotalInflation = arrIncomeNeeded[parseInt($("#schoolNOY").val())] * schoolTotalCost;
	}
	
	if ($("#cleaningNOY").val() != '0') {
		cleaningWeeklyCost = parseInt($("#cleaningHPW").val()) * parseInt($("#cleaningHR").val());
		$("#cleaningWeeklyCost").html(addCommas(cleaningWeeklyCost));
		cleaningTotalCost = parseInt($("#cleaningNOY").val()) * cleaningWeeklyCost * 52;
		$("#cleaningTotalCost").html(addCommas(cleaningTotalCost));
		cleaningTotalInflation = arrIncomeNeeded[parseInt($("#cleaningNOY").val())] * cleaningTotalCost;
	}
	
	if ($("#foodNOY").val() != '0') {
		foodWeeklyCost = parseInt($("#foodHPW").val()) * parseInt($("#foodHR").val());
		$("#foodWeeklyCost").html(addCommas(foodWeeklyCost));
		foodTotalCost = parseInt($("#foodNOY").val()) * foodWeeklyCost * 52;
		$("#foodTotalCost").html(addCommas(foodTotalCost));
		foodTotalInflation = arrIncomeNeeded[parseInt($("#foodNOY").val())] * foodTotalCost;
	}
	
	if ($("#bookNOY").val() != '0') {
		bookWeeklyCost = parseInt($("#bookHPW").val()) * parseInt($("#bookHR").val());
		$("#bookWeeklyCost").html(addCommas(bookWeeklyCost));
		bookTotalCost = parseInt($("#bookNOY").val()) * bookWeeklyCost * 52;
		$("#bookTotalCost").html(addCommas(bookTotalCost));
		bookTotalInflation = arrIncomeNeeded[parseInt($("#bookNOY").val())] * bookTotalCost;
	}
	
	if ($("#yardNOY").val() != '0') {	
		yardWeeklyCost = parseInt($("#yardHPW").val()) * parseInt($("#yardHR").val());
		$("#yardWeeklyCost").html(addCommas(yardWeeklyCost));
		yardTotalCost = parseInt($("#yardNOY").val()) * yardWeeklyCost * 52;
		$("#yardTotalCost").html(addCommas(yardTotalCost));
		yardTotalInflation = arrIncomeNeeded[parseInt($("#yardNOY").val())] * yardTotalCost;
	}
	
	if ($("#maintenanceNOY").val() != '0') {
		maintenanceWeeklyCost = parseInt($("#maintenanceHPW").val()) * parseInt($("#maintenanceHR").val());
		$("#maintenanceWeeklyCost").html(addCommas(maintenanceWeeklyCost));
		maintenanceTotalCost = parseInt($("#maintenanceNOY").val()) * maintenanceWeeklyCost * 52;
		$("#maintenanceTotalCost").html(addCommas(maintenanceTotalCost));
		maintenanceTotalInflation = arrIncomeNeeded[parseInt($("#maintenanceNOY").val())] * maintenanceTotalCost;
	}
	
	if ($("#careNOY").val() != '0') {
		careWeeklyCost = parseInt($("#careHPW").val()) * parseInt($("#careHR").val());
		$("#careWeeklyCost").html(addCommas(careWeeklyCost));
		careTotalCost = parseInt($("#careNOY").val()) * careWeeklyCost * 52;
		$("#careTotalCost").html(addCommas(careTotalCost));
		careTotalInflation = arrIncomeNeeded[parseInt($("#careNOY").val())] * careTotalCost;
	}
	
	if ($("#other1NOY").val() != '0') {
		other1WeeklyCost = parseInt($("#other1HPW").val()) * parseInt($("#other1HR").val());
		$("#other1WeeklyCost").html(addCommas(other1WeeklyCost));
		other1TotalCost = parseInt($("#other1NOY").val()) * other1WeeklyCost * 52;
		$("#other1TotalCost").html(addCommas(other1TotalCost));
		other1TotalInflation = arrIncomeNeeded[parseInt($("#other1NOY").val())] * other1TotalCost;
	}
	
	if ($("#other2NOY").val() != '0') {
		other2WeeklyCost = parseInt($("#other2HPW").val()) * parseInt($("#other2HR").val());
		$("#other2WeeklyCost").html(addCommas(other2WeeklyCost));
		other2TotalCost = parseInt($("#other2NOY").val()) * other2WeeklyCost * 52;
		$("#other2TotalCost").html(addCommas(other2TotalCost));
		other2TotalInflation = arrIncomeNeeded[parseInt($("#other2NOY").val())] * other2TotalCost;
	}
	
	valueTotalCost = childCareTotalCost + drivingTotalCost + schoolTotalCost + cleaningTotalCost + foodTotalCost + bookTotalCost + yardTotalCost + maintenanceTotalCost + careTotalCost + other1TotalCost + other2TotalCost;
	$("#valueTotal").html(addCommas(valueTotalCost));
	
	valueTotalInflation = childCareTotalInflation + drivingTotalInflation + schoolTotalInflation + cleaningTotalInflation + foodTotalInflation + bookTotalInflation + yardTotalInflation + maintenanceTotalInflation + careTotalInflation + other1TotalInflation + other2TotalInflation;
	valueTotalInflation = Math.round(valueTotalInflation);
	$("#valueTotalInflation").html(addCommas(valueTotalInflation));
	
	$("#homeValueTotal").val(valueTotalInflation);
	calculateExpenses();
}


	
