function MsgOkCancel() 
{
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var j=0; j < links.length; j++) {
		if (links[j].className.match("MsgOkCancel")) {
			links[j].onclick = function() {
				var fRet; 
				fRet = confirm('This will delete the item; are you sure?'); 
				if (fRet)
				{
					window.location = this.href;
				}
				return false;
			}
		}
	}
}

function add_section_actions()
{
	// check to see if there are elements we need to add stuff to
	if (document.getElementsByClassName("add_section").length > 0)
	{
		// get the add_section button
		var tbutton = document.getElementsByClassName("add_section")[0];

		// append to the add_section button an onclick method
		tbutton.onclick = function()
		{
			// clone the template
			var section_html = document.getElementById('section_template_block').getElementsByClassName("section_template")[0].cloneNode(true);

			// get the target for the new block
			var sections = document.getElementById('sections').getElementsByTagName("tbody")[0];

			// check how many blocks there are currently, and increment by 1
			var nums = sections.getElementsByClassName("section_template").length;
			var numsections = ((nums==undefined)?0:nums) + 1;

			// give the cloned template an id with the incremented count
			section_html.id = 'section-x' + numsections;

			// make sure that any inputs etc have the correct name (including the incremented count)
			inputs = section_html.getElementsByClassName("sect_input");
			for (var j=0; j < inputs.length; j++)
			{
				var nm = inputs[j].name.length - 10;
				inputs[j].name = 'section-x'+ numsections + inputs[j].name.substring(nm);
			}

			// get the delete button, give it an id (based on the incremented count)
			var delete_button = section_html.getElementsByClassName('delete_section')[0];
			delete_button.id = 'delete_section-x' + numsections;

			// add the onclick event method to the delete section button
			delete_button.onclick = function()
			{
				// get the section number from the id
				var del_id = this.id.substring(16);

				// get the section based on the section number
				var section = document.getElementById( 'section-x' + del_id );

				// remove the section from 'sections'
				sections.removeChild(section);
			}

			// are there add_element buttons?
			if(  section_html.getElementsByClassName("add_element").length > 0 )
			{
				// find the add element button, give it an id (based on the incremented count)
				var add_elem_button = section_html.getElementsByClassName("add_element")[0];
				add_elem_button.id = 'add_element-x' + numsections;

				// add the onclick event method to the add element button
				add_elem_button.onclick = function()
				{
					// get the section number based on the add element button id
					var add_id = this.id.substring(12);

					// clone the element template
					var element_template = document.getElementById("element_template_block").getElementsByClassName("element_template")[0].cloneNode(true);

					// get the target node
					var elements = document.getElementById('section-x' + add_id).getElementsByClassName("elements")[0].getElementsByTagName("tbody")[0];

					// how many elements already in the target node, increment that value
					var nums = elements.getElementsByClassName("element_template").length;
					var numelements = ((nums==undefined)?0:nums) + 1;

					// add an id to the new element based on the section number (from button id), and the incremented element count
					element_template.id = 'section-x'+add_id+'_element-x' + numelements;

					// make sure that any inputs etc have correct names including section number and element number
					inputs = element_template.getElementsByClassName("elem_input");
					for (var j=0; j < inputs.length; j++)
					{
						var nm = inputs[j].name.length - 10;
						inputs[j].name = 'section-x' + add_id + '_element-x' + numelements + inputs[j].name.substring(nm);
					}

					// find the delete element button, give it an id based on the element number
					var delete_element_button = element_template.getElementsByClassName('delete_element')[0];
					delete_element_button.id = 'delete_element-x' + numelements;

					// add the onclick event method to the delete element button
					delete_element_button.onclick = function()
					{
						// get element id from the button id
						var del_id = this.id.substring(15);

						// get the element it refers to
						var element = document.getElementById( 'section-x'+ add_id + '_element-x' + del_id );

						// remove the element from the section
						elements.removeChild(element);
					}

					// now add the completed element instance to the section
					elements.appendChild( element_template );

				}
			}

			// now add the completed section instance to sections
			sections.appendChild( section_html );
		}
	}
}

function add_section_deletes()
{
	// get the target for the new block
	var sections = document.getElementById('sections').getElementsByTagName("tbody")[0];
	
	var delbuttons = document.getElementsByClassName("delete_section");
	
	// check to see if there are elements we need to add stuff to
	if (delbuttons.length > 0)
	{
		var n=delbuttons.length;
		for(var i=0;i<n;i++)
		{
			delbuttons[i].onclick = function()
			{
				// get the section number from the id
				var del_id = this.id.substring(15);

				// get the section based on the section number
				var section = document.getElementById( 'section-' + del_id );

				// remove the section from 'sections'
				sections.removeChild(section);
			}
		}
	}
}


Event.observe(window, 'load', add_section_actions, false);
Event.observe(window, 'load', add_section_deletes, false);
Event.observe(window, 'load', MsgOkCancel, false);