/**
 * Encapsulate the actionType entity type. See the action_type database table
 */ 
 
var ActionType = {
	
	entityName: Entity.ACTIONTYPE,

	create: function(context){
		// Entity types
		var target = context + Service.SCOPE_SEPARATOR + 'entity';
		Service.lister(target, context, Entity.ENTITY, null, null, '', true);
		// Users types
		var target = context + Service.SCOPE_SEPARATOR + 'userType';
		Service.lister(target, context, Entity.USERTYPE, null, null, '', true);
		// Users
		var target = context + Service.SCOPE_SEPARATOR + 'user';
		Service.lister(target, context, Entity.USER, null, null, '', true);
		
		// Users - notification
		var suffix = 'notification';
		var target = context + Service.SCOPE_SEPARATOR + 'notification-user';
		Service.lister(target, context, Entity.USER, 'Notified Users :', null, '', true, null, null, null, null, suffix);
	},

	edit: function(context, entityID){
		selection = new Selection(this.entityName, entityID);
		
		var target = context + Service.SCOPE_SEPARATOR + 'entity';
		Service.lister(target, context, Entity.ENTITY, null, null, '', true, null, null, selection);
		
		var target = context + Service.SCOPE_SEPARATOR + 'userType';
		Service.lister(target, context, Entity.USERTYPE, null, null, '', true, null, null, selection);
		
		var target = context + Service.SCOPE_SEPARATOR + 'user';
		Service.lister(target, context, Entity.USER, null, null, '', true, null, null, selection);

		var suffix = 'notification';
		var selection = new Selection(this.entityName, entityID, 'action_type_notification');
		var target = context + Service.SCOPE_SEPARATOR + 'notification-user';
		Service.lister(target, context, Entity.USER, 'Notified Users :', null, '', true, null, null, selection, null, suffix);
  },

	view: function(context, entityID) {
		// Entity types supported by the collection
		var filter = new ListFilter(this.entityName, entityID);
		
		var target = context + Service.SCOPE_SEPARATOR + 'entity';
		Service.lister(target, context, Entity.ENTITY, null, null, '', true, filter, Widget.FLAT_LIST);

		var target = context + Service.SCOPE_SEPARATOR + 'userType';
		Service.lister(target, context, Entity.USERTYPE, null, null, '', true, filter, Widget.FLAT_LIST);

		var target = context + Service.SCOPE_SEPARATOR + 'user';
		Service.lister(target, context, Entity.USER, null, null, '', true, filter, Widget.FLAT_LIST);

		var filter = new ListFilter(this.entityName, entityID, 'action_type_notification');
		var target = context + Service.SCOPE_SEPARATOR + 'notification-user';
		Service.lister(target, context, Entity.USER, 'Notified Users :', null, '', true, filter, Widget.FLAT_LIST);
  },
		
	
	closeEditor: function(context) {
		var target = context + Service.SCOPE_SEPARATOR + 'entity';
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + 'userType';
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + 'user';
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + 'notification-user';
		$(target).update('');
	},

	closeViewer: function(context) {
		var target = context + Service.SCOPE_SEPARATOR + 'entity';
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + 'userType';
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + 'user';
		$(target).update('');
		var target = context + Service.SCOPE_SEPARATOR + 'notification-user';
		$(target).update('');
	},
	
	validate: function(context){
		var message = '';
		var textMessage = '';
		var selectMessage = '';
		var nameMessage = ''

		var prefix = context + Service.SCOPE_SEPARATOR + Widget.LISTER + Service.SCOPE_SEPARATOR;
		// Must have values in all editor text fields
		var textInputElements = $(context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'form').getInputs('text');		
		for (i = 0; i < textInputElements.length; i++) {
			if ($(textInputElements[i]).value.length == 0){
				textMessage += $(textInputElements[i].id).previous('label').innerHTML.replace(':','') + '\n';
			}
		}
		// Check select elements
		var excludedSelect = context + Service.SCOPE_SEPARATOR + Widget.LISTER + Service.SCOPE_SEPARATOR + Entity.USER + Service.SCOPE_SEPARATOR + 'select[]';
		var selectElements = $(context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'form').getElementsByClassName('fieldSelect');
		for (i = 0; i < selectElements.length; i++) {
			if ($(selectElements[i]).id != excludedSelect && $(selectElements[i]).value == ''){
				selectMessage += $(selectElements[i]).previous('h3').innerHTML.replace(':','') + '\n';			
			}
		}	
		
		if (textMessage) message += 'Please enter a value in: \n' + textMessage;
		if (selectMessage) message += 'Please make a selection in: \n' + selectMessage;
		if (nameMessage) message += 'Name must be selected in: \n' + nameMessage;
		
		return message;
	}
	
}
