function init_spacecalc() {
	var s = $("space_calc");
	s.getElements("input").each( function (e) {
		if (e.type == "text") {
			e.addClass("calc");
			e.addEvent("keyup", function () { force_num(this); calc_form(); });
			//e.addEvent("change", function () { calc_form(); });
		}
		
		if (e.type == "checkbox") {
			e.addClass("calc");
			e.addEvent("click", function () { calc_form(); });
		}
	});
}


function calc_form() {
	var p = 0;
	var total = 0;
	var t = 0;
	$$("input.calc").each( function(c) {
		switch (c.type) {
			case "text":
				if (c.value != "") {
					t = c.value.toInt() * $(c.name + "_sqft").value.toInt();
					if (c.hasClass("people")) { p += c.value.toInt(); }
					$("td_"+c.name).set("text", t + " sq ft");
					total += t;
				}
				break;
			case "checkbox":
				if (c.checked == true) {
					if (c.name == "num_dancefloor" && p > 0) {
						peep = ($("people").get("text").toInt() > 0 ? $("people").get("text").toInt() : 0);
						t = Math.round(peep * 0.65) * $(c.name + "_sqft").value.toInt();
						$("td_"+c.name).set("text", t + " sq ft");
						total += t;
					} else {
						t = c.value.toInt() * $(c.name + "_sqft").value.toInt();
						$("td_"+c.name).set("text", t + " sq ft");
						total += t;
					}
				} else {
					$("td_"+c.name).set("text", "0 sq ft");
				}
				break;
		}
	});
	$("total").set("text",total + " sq ft");
	if (p > 0) { $("people").set("text",p); } else { $("people").set("text","0"); }
}

function force_num(obj) {
	//alert("force num... obj.name: "+obj.name);
	if (obj.value.toInt() != obj.value) { obj.value = ""; }
	//else { alert("val: "+obj.value.toInt()); }
}

/***************
DOMREADY
+
EVENT LISTENERS
+
INIT VARS
***************/

window.addEvent('domready', function() {
	init_spacecalc();
});

