////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Javascript support functions for the functionality described in (and supported by) com.infofoundry.struts.actions.EditDbAction
// See com.infofoundry.struts.actions.EditDbAction javadoc for further documentation
// Designed to work in the following environment:
//    1) Struts-based, using the <html:...> and <logic:...> tag libraries
//    2) EditDbAction's core fields are included, keyed by 'name' (that's what Struts' <html:hidden> tag likes) 
//			- THE FIELD IS NAMED IN A NAME-BASED MANNER
//			- action: ('update', 'add', 'cancel' and 'delete' are supported
//			- actionTargetProperty: the property name in struts' form-bean element
//		   	- actionTargetIndex: if the actionTargetProperty is an array, the index into the array of the object of interest
//			- actionTargetBelongsToList: used for the 'add' action -- the list to which the new member belongs
//    3) Indexed lists are created by Struts' <logic:iterate> and <html:...> tags.  (Really, they just need to follow the right naming convention, but we won't get into that here.)
//    4) View/Edit is toggled by style="display:block/none".  
//			- THE BLOCK IS NAMED IN AN ID-BASED MANNER
//			- See 'toggleIndexedEditMode' for indexed elements' naming convention
//			- See 'toggleEditMode' for singleton elements' naming convention
//	  5) Each object is of class EditBeanWrapper.  This means that it has the bean-field "isEditMode"
//			- This field needs to be included (at the very least, as a <html:hidden> element for each object
//			- It can also be used to "start with" edit instead of view mode
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Front-end functions (call from an on-click)
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	// Toggle to 'edit' block for the specified property[index]
	// Set 'isEditMode' to true
	function editIndexedElement(propertyRoot, index) {
		toggleIndexedEditMode(true, propertyRoot, index);
	}

	// Toggle to 'view' block for the specified property[index]
	// Set 'isEditMode' to false
	function cancelIndexedEdit(formName, propertyRoot, index) {
//		toggleIndexedEditMode(false, propertyRoot, index);	
		setActionTargetsForIndexedElement(propertyRoot, index);
		submitFormWithAction(formName, "cancel");	
	}

	// Set the core fields needed for EditDbAction
	// Submit the form with action='update'
	function saveIndexedElement(formName, propertyRoot, index) {
		setActionTargetsForIndexedElement(propertyRoot, index);
		submitFormWithAction(formName, "update");
	}

	// Set the core fields needed for EditDbAction
	// Submit the form with action='delete'
	function deleteIndexedElement(formName, propertyRoot, index) {
		setActionTargetsForIndexedElement(propertyRoot, index);
		submitFormWithAction(formName, "delete");
	}

	// Inserts a singleton into the specified list (including database insert)
	// Done via form submit
	function insertListElement(formName, elementProperty, listProperty) {
		setHiddenProperty("actionTargetProperty", elementProperty);
		setHiddenProperty("actionTargetBelongsToList", listProperty);
		submitFormWithAction(formName, "add");
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Support functions.  Shouldn't need to be called by the user.  In a real language, these would be private
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	// toEdit: boolean; true to show edit, false to show view
	// index: integer, index of an html:iterate ../ tag
	// propertyRoot: base of property naming scheme
	function toggleIndexedEditMode(toEdit, propertyRoot, index) {
		var editHideShowDomId = propertyRoot + "Edit[" + index + "]";
		var viewHideShowDomId = propertyRoot + "View[" + index + "]";
		var isEditModeDomName = propertyRoot + "[" + index + "].isEditMode";
		toggleEditModeCommon(toEdit, editHideShowDomId, viewHideShowDomId, isEditModeDomName);
	}
	
	function toggleEditMode(toEdit, propertyRoot) {
		var editHideShowDomId = propertyRoot + "Edit";
		var viewHideShowDomId = propertyRoot + "View";
		var isEditModeDomName = propertyRoot + ".isEditMode";
		toggleEditModeCommon(toEdit, editHideShowDomId, viewHideShowDomId);
	}
	
	function toggleEditModeCommon(toEdit, editHideShowDomId, viewHideShowDomId, isEditModeDomName) {
		var editDomElt = document.getElementById(editHideShowDomId);
		var viewDomElt = document.getElementById(viewHideShowDomId);
		var editModeDomElt = (document.getElementsByName(isEditModeDomName))[0];
		if (toEdit) {
			editDomElt.style.display = "block";
			viewDomElt.style.display = "none";
			editModeDomElt.value = true;
		} else {
			editDomElt.style.display = "none";
			viewDomElt.style.display = "block";
			editModeDomElt.value = false;
		}
	}
		
	function submitFormWithAction(formName, action) {
		var formDomElt = document.forms[formName];
		var actionDomElt = formDomElt.elements["action"];
		actionDomElt.value = action;
		formDomElt.submit();		
	}
	
	function setActionTargetsForIndexedElement(propertyRoot, index) {
		setHiddenProperty("actionTargetProperty", propertyRoot);
		setHiddenProperty("actionTargetIndex", index);
	}
	
	
	// Sets a struts-style hidden property's value (the property is the html element's 'name')
	function setHiddenProperty(propertyName, propertyValue) {
		var propertyDomElt = (document.getElementsByName(propertyName))[0];
		propertyDomElt.value = propertyValue;
	}