function setUpGivingForm (sender) {
	$("Offering").onkeyup = updateFields;
	$("Relocation").onkeyup = updateFields;
	$("Missions").onkeyup = updateFields;
	$("Special").onkeyup = updateFields;
	$("Other").onkeyup = updateFields;
	$("Name").onkeyup = updateFields;

	$("Offering").onblur = reformatField;
	$("Relocation").onblur = reformatField;
	$("Missions").onblur = reformatField;
	$("Special").onblur = reformatField;
	$("Other").onblur = reformatField;

	var placeholders = new Array();
	var inputs = document.getElementsByTagName("input");

	for (var i = 0; i < inputs.length; i++) {
		var next = inputs[i];
		if (next.className.indexOf("Placeholder") + 1) {
			next.onfocus = focusPlaceholder;
			next.onblur = blurPlaceholder;
		}
	}
}

function updateFields () {
	var offering = parseFloat($("Offering").value);
	var relocation = parseFloat($("Relocation").value);
	var missions = parseFloat($("Missions").value);
	var special = parseFloat($("Special").value);
	var other = parseFloat($("Other").value);

	if (String(offering) == "NaN") offering = 0.0;
	if (String(relocation) == "NaN") relocation = 0.0;
	if (String(missions) == "NaN") missions = 0.0;
	if (String(special) == "NaN") special = 0.0;
	if (String(other) == "NaN") other = 0.0;

	var total = offering + relocation + missions + special + other;
	$("Total").value = moneyFormat(total);
	$("Amount").value = moneyFormat(total);

	var name = $("Name").value;
	if (name == "Specify Other Here") name = "(Other Not Specified)";

	var items = new Array();
	if (offering > 0) items.push("Offering " + moneyFormat(offering));
	if (relocation > 0) items.push("Relocation " + moneyFormat(relocation));
	if (missions > 0) items.push("Missions " + moneyFormat(missions));
	if (special > 0) items.push("Special " + moneyFormat(special));
	if (other > 0) items.push(name + " " + moneyFormat(other));

	$("Item").value = items.join(" / ");
}

function reformatField () {
	var current = this.value;
	if (String(parseFloat(current)) == "NaN") current = "0.0";
	this.value = moneyFormat(parseFloat(current));
}

function moneyFormat (number) {
	var repr = new String(number);
	var parts = repr.split(".");
	
	if (parts.length == 1) return parts[0] + ".00";
	else { 
		if (parts[1].length < 2) {
			return parts[0] + "." + parts[1] + "0";
		} else return parts[0] + "." + parts[1].substr(0, 2);
	}
	return repr;
}

function focusPlaceholder () {
	var current = this.value;
	var placeholder = this.getAttribute("value");

	if (current == placeholder) {
		this.value = "";
		this.className = this.className.replace("Placeholder", "");
	}
}

function blurPlaceholder () {
	var current = this.value;
	var placeholder = this.getAttribute("value");

	if (current == "") {
		this.value = placeholder;
		this.className = this.className + " Placeholder";
	}
}

Event.observe(window, "load", function (event) {
  if (!event) event = window.event;
  setUpGivingForm(event);
});
