
window.addEvent('load', function(){
	if ($("death-notification-form")) {
		addFormEvents();
		
		
		$("child-details").getElement("textarea").addEvent("focus", function () {
							new Fx.Morph($("child-details").getElement("textarea"), {duration : 500}).start({"height": 100});
							});
		$("child-details").getElement("textarea").addEvent("blur", function () {
							new Fx.Morph($("child-details").getElement("textarea"), {duration : 500}).start({"height": 15});
							}); 	
		
		$("other-contacts").getElement("textarea").addEvent("focus", function () {
							new Fx.Morph($("other-contacts").getElement("textarea"), {duration : 500}).start({"height": 100});
							});
		$("other-contacts").getElement("textarea").addEvent("blur", function () {
							new Fx.Morph($("other-contacts").getElement("textarea"), {duration : 500}).start({"height": 15});
							}); 	
		relationshipOptions($("other-contacts").getElement("input[name=othercontact_relationship_1]").getParent());
		relationshipOptions($("your-details").getElement("input[name=personal_relationship]").getParent());
		
		heights = new Object();
		heights["spouse-details"] = $("spouse-details").getSize().y;
		heights["child-details"] = $("child-details").getSize().y;
		
		$("spouse-details").setStyles({
			"opacity" : 0,
			"height" : 1,
			"display" : "none",
			"visibility" : "hidden"
			});
		$("child-details").setStyles({
			opacity : 0,
			height : 1,
			display : "none",
			visibility : "hidden"
			});
		positionFooter();
		}
	}); 


function openFieldset(id) {
	$(id).setStyles({
		"display" : "block"
		});
	new Fx.Morph($(id), {
		duration : 500,
		onComplete : function () {
			
			$(id).setStyles({
				"height" : "auto"
				});
			positionFooter();
			new Fx.Morph($(id), {duration : 500}).start({"opacity": 1});
			}
		
		}).start({"height": heights[id]});
	
	}
function closeFieldset(id) {
	heights[id] = $(id).getSize().y;
	$(id).setStyles({
		"height" : heights[id]
		});
	new Fx.Morph($(id), {
		duration : 500,
		onComplete : function () {
			
			new Fx.Morph($(id), {
				duration : 500,
				onComplete : function () {
					$(id).setStyles({
						"display" : "none"
						});
					positionFooter();	
					}
				}).start({"height": 1});
			}
		
		}).start({"opacity": 0});
	
	}
	
function addFormEvents() {
	positionFooter();
	$("child-details").getElement("input[value^=New]").removeEvents();
	
	$("child-details").getElement("input[value^=New]").addEvent("click", function () {
		var dl = $("child-details").getElement("tbody").getElements("tr")[0];
		var item = dl.clone().setOpacity(0).inject($("child-details").getElement("tbody"), "bottom");
		if (item.getElement("ul")) {
			item.getElement("ul").setOpacity(0)
			}
		//new Element("tr").set("html", dl.get("html")).setOpacity(0).inject($("child-details").getElement("tbody"), "bottom");
		item.getElements("td").each(
			function (t) {
				if (t.getElement("input")) {
					t.getElement("input").name = t.getElement("input").name.replace(/[0-9]/, dl.getParent().getElements("tr").length)
					}
				if (t.getElement("select")) {
					t.getElement("select").name = t.getElement("select").name.replace(/[0-9]/, dl.getParent().getElements("tr").length)
					}
				if (t.getElement("textarea")) {
					t.getElement("textarea").name = t.getElement("textarea").name.replace(/[0-9]/, dl.getParent().getElements("tr").length)
					t.getElement("textarea").addEvent("focus", function () {
						new Fx.Morph(t.getElement("textarea"), {duration : 500}).start({"height": 100});
						});
					t.getElement("textarea").addEvent("blur", function () {
						new Fx.Morph(t.getElement("textarea"), {duration : 500}).start({"height": 15});
						}); 
					}
				if (t.getElement("a")) {
					t.getElement("a").removeProperty("style");
					t.getElement("a").addEvent("click", function (ev) {
						if (t.getParent().getParent().getElements("tr").length > 1) {
							ev.stop();
							new Fx.Morph(t.getParent(), {
												duration : 500,
												onComplete : function () {
													t.getParent().dispose();
													styleTables();
													positionFooter();
													}
												}).start({"opacity": 0});
								}					
							});
						 
					}
					positionFooter();
					
				}
			);
		item.removeProperty("class");
		item.setOpacity(0);
		
		styleTables();
		new Fx.Morph(item, {duration : 500,onComplete : function () {}}).start({"opacity": 1});
		});
	
	
	$("other-contacts").getElement("input[value^=New]").addEvent("click", function () {
		var dl = $("other-contacts").getElement("tbody").getElements("tr")[0];
		var item = dl.clone().setOpacity(0).inject($("other-contacts").getElement("tbody"), "bottom");
		item.getElement("ul").setOpacity(0)
		//new Element("tr").set("html", dl.get("html")).setOpacity(0).inject($("other-contacts").getElement("tbody"), "bottom");
		item.getElements("td").each(
			function (t) {
				if (t.getElement("input")) {
					t.getElement("input").name = t.getElement("input").name.replace(/[0-9]/, dl.getParent().getElements("tr").length)
					if (t.getElement("input").name.indexOf("othercontact_relationship") != -1) {
						relationshipOptions(t.getElement("input").getParent());
						}
					}
				if (t.getElement("select")) {
					t.getElement("select").name = t.getElement("select").name.replace(/[0-9]/, dl.getParent().getElements("tr").length)
					}
				if (t.getElement("textarea")) {
					t.getElement("textarea").name = t.getElement("textarea").name.replace(/[0-9]/, dl.getParent().getElements("tr").length)
					t.getElement("textarea").addEvent("focus", function () {
						new Fx.Morph(t.getElement("textarea"), {duration : 500}).start({"height": 100});
						});
					t.getElement("textarea").addEvent("blur", function () {
						new Fx.Morph(t.getElement("textarea"), {duration : 500}).start({"height": 15});
						}); 
					}
				if (t.getElement("a")) {
					t.getElement("a").removeProperty("style");
					t.getElement("a").addEvent("click", function (ev) {
						if (t.getParent().getParent().getElements("tr").length > 1) {
							ev.stop();
							new Fx.Morph(t.getParent(), {
												duration : 500,
												onComplete : function () {
													t.getParent().dispose();
													styleTables();
													positionFooter();
													}
												}).start({"opacity": 0});
								}					
							});
					}
					positionFooter();
				}
			);
		item.removeProperty("class");
		item.setOpacity(0);
		
		styleTables();
		new Fx.Morph(item, {duration : 500,onComplete : function () {}}).start({"opacity": 1});
		});
	
	
	
	$$("textarea").each(
		function (el) {
			el.addEvent("focus", function (t) {
				t.target.addClass("active");
				});
			}
		);
	$$("textarea").each(
		function (el) {
			el.addEvent("blur", function (t) {
				t.target.removeClass("active");
				});
			}
		);
	$$("input").each(
		function (el) {
			el.addEvent("focus", function (t) {
				t.target.addClass("active");
				});
				
			}
		);
	$$("input").each(
		function (el) {
			el.addEvent("blur", function (t) {
				t.target.removeClass("active");
				});
			}
		);
	}
	
function relationshipOptions (el) {

		if (el.getElement("ul").getStyle("display") == "none") {
			el.getElement("ul").setOpacity(0);
			el.getElement("ul").setStyle("display", "block");
			}
		
		el.getElement("input").addEvent("click", function () {
			if (!el.getElement("input").getProperty("readonly")) {
				el.setStyle("position", "relative");
				new Fx.Morph(el.getElement("ul"), {duration:200}).start({"opacity": 1});
				}
			});	
		
		el.getElement("input").addEvent("focus", function () {
			if (el.getElement("input").getProperty("readonly")) {
				el.setStyle("position", "relative");
				new Fx.Morph(el.getElement("ul"), {duration:200}).start({"opacity": 1});
				}
			});
		
		el.getElement("input").addEvent("blur", function () {
			el.setStyle("position", "relative");
			new Fx.Morph(el.getElement("ul"), {duration:200}).start({"opacity": 0});
			});
			
		el.getElements("li").each(
			function (e) {
				e.addEvent("click", function () {
						var val = arguments[0].target.get("text");
						//new Fx.Tween(arguments[0].target.getParent().getParent().getElement("ul"), {duration:200}).start("opacity", 0);
						if (val.toLowerCase() == "other") {
							arguments[0].target.getParent().getParent().getElement("input").value = "";
							arguments[0].target.getParent().getParent().getElement("input").removeProperty("readonly");
							arguments[0].target.getParent().getParent().getElement("input").focus();
							}
						else {
							arguments[0].target.getParent().getParent().getElement("input").value = val;
							arguments[0].target.getParent().getParent().getElement("input").setProperty("readonly", "readonly");
							}
					
						} // end function
					);// end addEvent
				}// end function
			);// end each
		
	}