
// the Journey System will include this javascript
// in every form that it is managing
//
// the FormManager
// + maintains a list of edited fields in a hidden inputfield 'edited'
// + maintains which button triggerd the submit in a hiddenfield 'action'
// if these fields are not already present on the form they will be created


function FormManager(formId)
{
	this.form = document.getElementById(formId);
	this.editedObjects = new Array();

	this.edited = function (domElement) {
		//alert("edited");
		this.addEditedElement(domElement);
	}

	this.shouldSave = function () {
		return (this.editedObjects.length > 0);
	}

	// 'private function' to remember which dom elements were edited
	this.addEditedElement = function (domElement) {
		for ( var i = this.editedObjects.length-1; i > -1; i-- )
			if ( this.editedObjects[i] == domElement )
				return;
		
		this.editedObjects[this.editedObjects.length] = domElement;
	}

	// submit the form and remember which button triggered the submit
	this.submit = function (button) {
		//alert("should save = "+this.shouldSave());
		this.setEdited();
		this.setAction(button.id);
		this.form.submit();
	}

	// 'private function' to remember which button triggered action
	this.setAction = function (action) {
		// in this form find the input field named 'action' (vs. with id=action)
		// we use this field to inform the server of which button was clicked
		var actionField = this.getField('action');
		
		if ( !actionField )
		{
			//action field not found: create such a hidden input field in this form
			actionField = document.createElement('input');
			actionField.setAttribute('type', 'hidden');
			actionField.setAttribute('name', 'action');
			this.form.appendChild(actionField);
		}

		actionField.value = action;
	}

	// 'private function' fille the 'edited' field with names of fields that were edited
	this.setEdited = function () {
		// in this form find the input field named 'edited' (vs. with id=edited)
		// we use this field to inform the server of which fields were edited
		var editedField = this.getField('edited');
		
		if ( !editedField )
		{
			//edited field not found: create such a hidden input field in this form
			editedField = document.createElement('input');
			editedField.setAttribute('type', 'hidden');
			editedField.setAttribute('name', 'edited');
			this.form.appendChild(editedField);
		}
		
		editedField.value = "";
		for (var i = 0; i < this.editedObjects.length; i++)
		{
			if ( i > 0)
				editedField.value += ",'"+this.editedObjects[i].name+"'";
			else
				editedField.value = "'"+this.editedObjects[i].name+"'";
		}
		//alert("edited: "+editedField.value);
	}

	// in 'this' form find the field with a given name
	this.getField = function (name) {
		var fields = document.getElementsByName(name);
		var field = null, parentField = null, length;
		
		// find the one in this form
		for ( var i = 0; fields && i < fields.length; i++ )
		{
			field = fields[i];
			parentField = field;

			while ( parentField && parentField != this.form )
					parentField = parentField.parentNode;
			
			if ( parentField ) return field;
		}

		return null;
	}
}

