/**
 * Encapsulate the EA entity type. See the ea database table
 */ 
  
var Ea = {
			
	entityName: Entity.EA,

	handleEvent: function(event, context) {
		var element = Event.element(event);
		elementID = element.id;
		if (elementID.endsWith('credit_ea')) {
			Ea.creditEA();
		}
		return;
	},
	
	search: function(context, event){
		var element = Event.element(event);
		if (element.id.endsWith('clear')) {
			$(context + Service.SCOPE_SEPARATOR + Widget.EA_SEARCHER + Service.SCOPE_SEPARATOR + 'term').value = '';
			Entity.list(context, Entity.EA, null);
			Entity.closeViewer(context, this.entityName);
		} else if (element.id.endsWith('search') || element.id.endsWith('term')) {
			Ea.refreshList(context);
		}
	},
	
	refreshList: function(context){
		var termID = context + Service.SCOPE_SEPARATOR + Widget.EA_SEARCHER + Service.SCOPE_SEPARATOR + 'term';
		var term = $(termID).value;
		if (term.length == 0) {
			Entity.refreshList(context, Entity.EA);
		} else {
			CasaService.listEAs(context);			
		}
		Entity.closeViewer(context, this.entityName);
	},

	creditEA: function() {
		var idElement = Context.EAS + Service.SCOPE_SEPARATOR + Widget.EA_VIEWER + Service.SCOPE_SEPARATOR + Entity.EA + Service.SCOPE_SEPARATOR + 'id';
		var entityID = $F(idElement);
		
		var creditIDPrefix = Context.EAS + Service.SCOPE_SEPARATOR + Service.BUTTONS + Service.SCOPE_SEPARATOR + Entity.EA + Service.SCOPE_SEPARATOR;
		var countFieldID = creditIDPrefix + 'credit_ea_count';
		var reasonFieldID = creditIDPrefix + 'credit_ea_reason';
		var creditCount = $(countFieldID).value;
		var reason = $(reasonFieldID).value;
		// Validate
		if (creditCount.search(/^[0-9]+$/) != 0) {
			alert('The amount of credits to refund must be a positive integer.');
			exit;
		}
		if (reason.trim().length == 0) {
			alert('Please supply a reason for the refund.');
			exit;
		}
		var option = "";
		try {
			var radioID = creditIDPrefix + 'credit_ea_radio';
			option = getRadioValue(radioID);
		} catch(err) {;}
		if (option == null) option = "";
		// Confirm
		var message = 'Are you sure you want to refund ' + creditCount;
		if (creditCount == 1) {
			message += ' credit to this EA\'s ' + option + ' account'
		} else {
		 	message += ' credits to this EA\'s ' + option + ' account';
		}
		if (confirm(message)) {
			CasaService.eaCredit(entityID, creditCount, reason, option);		
		}
	},
	
	create: function(context){
	},

	edit: function(context, entityID){
	},

	view: function(context, entityID) {
		
		// Actions: load the actions div which by default is not visible
		var filter = new ListFilter(this.entityName, entityID);
		$(Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID').value = entityID;
		var target = Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'lister' + Service.SCOPE_SEPARATOR + Entity.ACTION;
		var eventHandler = 'Entity.view(\'' + Context.EAS_ACTIONS + '\',\'' + Entity.ACTION + '\',\'@id\')';
		Service.lister(target, Context.EAS_ACTIONS, Entity.ACTION, null, eventHandler, '', false, filter);

		// Notes: load the notes div which by default is not visible
		var filter = new ListFilter(this.entityName, entityID);
		$(Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'instanceID').value = entityID;
		var target = Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'lister' + Service.SCOPE_SEPARATOR + Entity.ACTION;
		var eventHandler = 'Entity.view(\'' + Context.EAS_NOTES + '\',\'' + Entity.ACTION + '\',\'@id\')';
		Service.lister(target, Context.EAS_NOTES, Entity.ACTION, null, eventHandler, '', false, filter);

		try {
			Action.refreshPanel(Context.EAS_ACTIONS);
		} catch(err) {;}
		try {
			Note.refreshPanel(Context.EAS_NOTES);
		} catch(err) {;}
		
	},
	
	
	closeEditor: function(context) {
	},

	closeViewer: function(context) {
		$(Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID').value = '';
		$(Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'instanceID').value = '';
		try {
			Action.refreshPanel(Context.EAS_ACTIONS);
		} catch(err) {;}
		try {
			Note.refreshPanel(Context.EAS_NOTES);
		} catch(err) {;}
	},
	
	validate: function(context){
		var message = '';
		var textMessage = '';
		// Must have values in all descriptor text fields
		var textInputElements = $(context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'form').getInputs('text');
		for (i = 0; i < textInputElements.length; i++) {
			// Ignore the directory because it may not be set
			if ($(textInputElements[i].id.endsWith(Service.SCOPE_SEPARATOR + 'directory'))) continue;
			if ($(textInputElements[i]).value.length == 0) textMessage += $(textInputElements[i].id).previous('label').innerHTML.replace(':','') + '\n';
		}
		if(textMessage) message += 'Please enter a value in:\n' + textMessage;
		return message;
	}
	
}
