/* Child Support Calculator
	   Version 2.1 (JS + IEFix)
	   Tobias Zwick, January 2008
	   CueBlocks.
	 */

	var msie = ((navigator.appVersion.indexOf("MSIE")!= -1) && !window.opera)? true : false; 
	 
	var gross;
	// automatic calculation on change after both buttons are clicked
	var auto = false;

	function SetStep(step) {
		if(step == 2) {
			CalculateSupport();

			document.getElementById("c_btn2").style.display = "none";
			document.getElementById("c_note").style.display = "block";
			document.getElementById("c_note2").style.display = "block";
			auto = true;
		}
		if(step == 1) {
			CalculateAdjustedIncome();

			// show section
			var hide;
			for(var i=0; hide=document.getElementById("c_hide"+i+""); ++i) {
				if(msie)
					hide.style.display = "block";
				else
					hide.style.display = "table-row";
			}
			// show calculate button
			document.getElementById("c_btn1").style.display = "none";
			document.getElementById("c_btn2").style.display = "inline";
		}
		if(step == 0) {

			// hide "c_hide"
			var hide;
			for(var i=0; hide=document.getElementById("c_hide"+i+""); ++i) 
				hide.style.display = "none";
			// show calculate button
			document.getElementById("c_btn1").style.display = "inline";
			document.getElementById("c_btn2").style.display = "none";
		}
	}

	function CheckNum(element) {
		var id = element.getAttribute("id");

		var number = parseInt(element.value);
		if(isNaN(number)) number = "";
		element.value = number;

		if(auto) {
			if(id != "c_children") CalculateAdjustedIncome();
			CalculateSupport();
		}
	}

	function CalculateAdjustedIncome() {
		// get from input fields
		var income = document.getElementById("c_income").value;
		var paid = document.getElementById("c_paid").value;
		var tax = document.getElementById("c_tax").value;	

		// calculate
		gross = income - paid - tax;

		// write output
		document.getElementById("c_gross").firstChild.nodeValue = "$" + Math.round(gross);
	}

	function CalculateSupport() {
		// get from input fields
		var children = document.getElementById("c_children").value;

		// calculate support percentage
		var percentage = 0;
		if(children == 1) percentage = 0.17;
		if(children == 2) percentage = 0.25;
		if(children == 3) percentage = 0.29;
		if(children == 4) percentage = 0.31;
		if(children >= 5) percentage = 0.35;

		// amount of support the non-custodial parent has to pay
		var support = gross * percentage;

		// weeks per year:
		var weeks = 52.177457;
		var support_biweekly = support / weeks * 2;

		// write output
		document.getElementById("c_support").firstChild.nodeValue = "$" + Math.round(support) + " per year, or";
		document.getElementById("c_support_biweekly").firstChild.nodeValue = "$" + Math.round(support_biweekly) + " biweekly";
	}


	window.onload = function(){
		SetStep(0);
	}
