// JavaScript Document

	function handleError(msg)
		{
			var errDiv = document.getElementById("divError");
			var errCtl = document.getElementById("ctlError");
			var n;

			deleteChildren(errCtl);
			errDiv.style.display = "";
			
			n = document.createTextNode(msg);
			crrCtl.appendChild(n);
		}

		function hideError()
		{
			var errDiv = document.getElementById("divError");
			errDiv.style.display = "none";
		}

		function deleteChildren(elm)
		{
			var i;

			if (elm)
			{
				if (elm.childNodes)
				{
					for (i=elm.childNodes.length-1; i>=0; i--)
						elm.removeChild(elm.childNodes[i]);
				}
			}
		}

		function formatMoney(cur, decplaces)
		{
			var s1 = String(cur);
			var decimal = s1.indexOf(".");
			var places = (-1 != decimal) ? decimal : s1.length;
			var s2 = "";
			var s3 = (-1 != decimal) ? s1.substr(decimal + 1) : "";
			var i;
		
			if (0 != decplaces)
				s3 = formatDecimal("." + s3, decplaces);
			else
				s3 = "";
		
			for (i=(places-1); i>=0; i--)
			{
				s2 = s1.substr(i, 1) + s2;
				if ((0 != i) && (0 == ((places - i) % 3)))
					s2 = "," + s2;
			}
		
			return "$" + s2 + s3;
		}

		function formatDecimal(dec, places)
		{
			var s = String(dec);
			var i = s.indexOf(".");
			var j, k;
		
			if (-1 == i)
			{
				s += ".";
				i = s.length - 1;
			}
		
			j = (s.length - 1) - i;
			if (j < places)
			{
				for (k=j; k<places; k++)
					s += "0";
			}
			else
				s = s.substr(0, s.length - (j - places));
		
			return s;
		}

        // function doCalculate(elm) {
		function doCalculate() {
		    var frm = document.getElementById("calc-form");
            // replaced button element with clickable image so need to get form directly.  TLS 7/22/2011
            // elm.form;
			var rowContribution = document.getElementById("rowContribution");
			var rowMonthlySavings = document.getElementById("rowMonthlySavings");
			var rowYearlySavings = document.getElementById("rowYearlySavings");
			var ctlContribution = document.getElementById("ctlContribution");
			var ctlMonthlySavings = document.getElementById("ctlMonthlySavings");
			var ctlYearlySavings = document.getElementById("ctlYearlySavings");

			var employees, fringerate, hours, payrolltax, contribution, savings, yearlysavings;
			var n;

			if (doValidate(frm))
			{
				rowContribution.style.display = "";
				ctlContribution.style.display = "";
				rowMonthlySavings.style.display = "";
				rowYearlySavings.style.display = "";

				employees = parseInt(frm.lngEmployees.value);
				fringerate = parseInt(frm.intFringe.value);
				hours = parseInt(frm.intHours.value) / 12;
				payrolltax = parseFloat(frm.intPayrollTax.value) * .01;
				contribution = employees * fringerate * hours;
				savings = contribution * payrolltax;
				yearlysavings = savings * 12;

				contribution = Math.round(contribution * 100) / 100;
				savings = Math.round(savings * 100) / 100;
				yearlysavings = Math.round(yearlysavings * 100) / 100;

				contribution = formatMoney(contribution, 2);
				savings = formatMoney(savings, 2);
				yearlysavings = formatMoney(yearlysavings, 2);
				deleteChildren(ctlContribution);
				deleteChildren(ctlMonthlySavings);
				deleteChildren(ctlYearlySavings);

				n = document.createTextNode(contribution);
				ctlContribution.appendChild(n);

				n = document.createTextNode(savings);
				ctlMonthlySavings.appendChild(n);

				n = document.createTextNode(yearlysavings);
				ctlYearlySavings.appendChild(n);

				// frm.ActionButton.value = "Recalculate My Savings";
			}
			else
			{
				rowContribution.style.display = "none";
				ctlContribution.style.display = "none";
				rowMonthlySavings.style.display = "none";
				rowYearlySavings.style.display = "none";

				// frm.ActionButton.value = "Calculate My Savings";
			}
			
		}

		function v_stripCurrency(elm)
		{
			var stripped = elm.value;
			if ("$" == stripped.substr(0,1))
			{
				stripped = stripped.substr(1);
				elm.value = stripped;
			}

			return (0 != String(parseFloat(stripped)));
		}

		function v_percent(elm)
		{
			var stripped = elm.value;
			if ("%" == stripped.substr(stripped.length - 1,1))
			{
				stripped = stripped.substr(0, stripped.length - 1);
				elm.value = stripped;
			}

			stripped = parseFloat(stripped);
			return ((stripped >= 0) && (stripped <= 100));
		}
