/**
 * Encapsulate the action entity type. See the action database table
 * The action entity does not have it's own context and instances are managed with the
 * context of a related entity type, e.g. product. This related type must be known and in
 * the case of edit operations the related instance ID is also required.
 */ 
 
var Action = {

	entityName: Entity.ACTION,
	
	/**
	* Collect the instance ID the entity type to which the action is being applied
	*/
	getData: function(context, entity) {
		var data = '';
		var instanceIDField = context + Service.SCOPE_SEPARATOR + 'instanceID';
		data = '&' + 'instanceID=' + $(instanceIDField).value;
		return data;
	},

	create: function(context){
		// Action types - filter by related entity type
		var relatedEntityField = context + Service.SCOPE_SEPARATOR + 'entity';
		var relatedEntity = $(relatedEntityField).value;
		var filter = new ListFilter(relatedEntity, '0');
		var target = context + Service.SCOPE_SEPARATOR + 'actionType';
		Service.lister(target, context, Entity.ACTIONTYPE, null, null, '1', false, filter);
	},

	edit: function(context, entityID){
		// Action types - filter by related entity type and related entityID
		var relatedEntityField = context + Service.SCOPE_SEPARATOR + 'entity';
		var relatedEntity = $(relatedEntityField).value;
		var relatedEntityIDField = context + Service.SCOPE_SEPARATOR + 'instanceID';
		var relatedEntityID = $(relatedEntityIDField).value;
  		var filter = new ListFilter(relatedEntity, relatedEntityID);
		var selection = new Selection(this.entityName, entityID);
		var target = context + Service.SCOPE_SEPARATOR + 'actionType';
		Service.lister(target, context, Entity.ACTIONTYPE, null, null, '1', false, filter, null, selection);
	},

	view: function(context, entityID) {		
		var filter = new ListFilter(this.entityName, entityID);
		var target = context + Service.SCOPE_SEPARATOR + 'actionType';
		Service.lister(target, context, Entity.ACTIONTYPE, null, null, '', false, filter, Widget.FLAT_LIST);
 	},
		
	closePanel: function(context) {
		if(dirty.getDirty(context) != undefined){
			var answer = false;
			var dirtyID = dirty.getDirty(context);
			if (dirtyID > 0) answer = Entity.cancel(context, this.entityName);
			else answer = Entity.closeEditor(context, this.entityName);
			if (!answer) return;
		}
		
		var target = context;
		$(target).setStyle({'display': 'none'});
		var target = context + Service.SCOPE_SEPARATOR + this.entityName;
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + Service.BUTTONS + Service.SCOPE_SEPARATOR + this.entityName;
		$(target).update('');
		var relatedEntityField = context + Service.SCOPE_SEPARATOR + 'entity';
		var relatedEntity = $(relatedEntityField).value;
		var relatedEntityIDField = context + Service.SCOPE_SEPARATOR + 'instanceID';
		var relatedEntityID = $(relatedEntityIDField).value;
  		var filter = new ListFilter(relatedEntity, relatedEntityID);
		Entity.refreshList(context, this.entityName, null, false, null, filter);

	},

	refreshPanel: function(context) {
		if(dirty.getDirty(context) != undefined){
			var answer = false;
			var dirtyID = dirty.getDirty(context);
			if (dirtyID > 0) answer = Entity.cancel(context, this.entityName);
			else answer = Entity.closeEditor(context, this.entityName);
			if (!answer) return;
		}
		var target = context + Service.SCOPE_SEPARATOR + this.entityName;
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + Service.BUTTONS + Service.SCOPE_SEPARATOR + this.entityName;
		$(target).update('');
		var relatedEntityField = context + Service.SCOPE_SEPARATOR + 'entity';
		var relatedEntity = $(relatedEntityField).value;
		var relatedEntityIDField = context + Service.SCOPE_SEPARATOR + 'instanceID';
		var relatedEntityID = $(relatedEntityIDField).value;
  		var filter = new ListFilter(relatedEntity, relatedEntityID);
		Entity.refreshList(context, this.entityName, null, false, null, filter);
	},
	
	closeEditor: function(context) {
		var target = context + Service.SCOPE_SEPARATOR + 'actionType';
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + 'action';
		$(target).update('');
		Action.refreshPanel(context);
	},

	closeViewer: function(context) {
		var target = context + Service.SCOPE_SEPARATOR + 'actionType';
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + 'action';
		$(target).update('');
	},
	
	validate: function(context){
		var message = '';
		return message;
	}
	
}
