/**
 * Encapsulate the EA entity type. See the ea database table
 */ 
  
var EntityEA = {
			
	entityName: 'entityEA',

	handleEvent: function(event, context, entity) {
		var element = Event.element(event);

		if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'edit')) {
			Entity.edit(context, entity);
			return;
		} else if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'save')) {
			EntityEA.save(context, entity);
			return;
		} else if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'cancel')) {
			EntityEA.cancel(context, entity);
			return;
		} else if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'close')) {
			Entity.closeEditor(context, entity);
			return;
		}
		return;
	},

	create: function(context) {
	},

	edit: function(context, entityID){
	},

	view: function(context, entityID) {
		try {
			Action.refreshPanel(Context.EAS_ACTIONS);
		} catch(err) {;}
		try {
			Note.refreshPanel(Context.EAS_NOTES);
		} catch(err) {;}
		
		// 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);
		
	},
	
	
	save: function(context, entity, onSuccessFunction, onFailureFunction) {
		document.body.style.cursor = 'wait';
		// Call the entity type's validation method
		try {
			var call = entity.ucFirst() + '.validate(\'' + context + '\')';
			var message = eval(call);
			if (message.length > 0) {
				document.body.style.cursor = '';
				alert(message);
				return;
			}
		} catch(err) {
			document.body.style.cursor = '';
			alert('error validating form data for: ' + entity + ' ' + err);
			return;
		}
		
		var target = context + Service.SCOPE_SEPARATOR + this.entityName;
		var data = $(context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'form').serialize();
		
		switch (context) {
			case ApplicationContext.REGISTER:
				Service.editor(target, context, this.entityName, Service.EDITOR_SAVE, null, data, null, ApplicationWidget.EA_REGISTER, 'ApplicationService.contentSuccess');
				break;
				
			case ApplicationContext.REGISTERCONFIRM:
				Service.editor(target, context, this.entityName, Service.EDITOR_SAVE, null, data, null, ApplicationWidget.EA_REGISTERCONFIRM, 'ApplicationService.contentSuccess');
				break;
				
			case ApplicationContext.FORGOTTENPASSWORD:
				Service.editor(target, context, this.entityName, Service.EDITOR_SAVE, null, data, null, ApplicationWidget.EA_FORGOTTENPASSWORD, 'ApplicationService.contentSuccess');
				var target = context + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + entity;
				$(target).update('');
				break;
				
			default:
				Service.editor(target, context, this.entityName, Service.EDITOR_SAVE, null, data);
				break;
		}
	},

	
	cancel: function(context) {
		var target = context + Service.SCOPE_SEPARATOR + entity;
		var idElement = context + Service.SCOPE_SEPARATOR + Widget.EDITOR + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'id';
		var entityID = $F(idElement);
		Service.editor(target, context, this.entityName, Service.EDITOR_CANCEL, entityID, null);
	},
	
	
	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) {
		switch (context) {
			case ApplicationContext.FORGOTTENPASSWORD:
				var message = '';
				var textMessage = '';
				var prefix = context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR;
				var emailValue = $(prefix + 'email').value;
				
				if (emailValue == '') textMessage += 'Email\n';
				if (textMessage) message += 'Please enter a value in:\n' + textMessage;
				
				return message;

			case ApplicationContext.REGISTERCONFIRM:
				// When confirming registration, the user is asked for their login details, so just run the standard user
				// validation
				var message = '';
				var textMessage = '';
				var passwordMessage = '';
				var prefix = context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR;
				
				var usernameValue = $(prefix + 'user_name').value;
				var passwordValue = $(prefix + 'password').value;
				var passwordConfirmValue = $(prefix + 'confirmPassword').value;
				
				if (usernameValue == '') textMessage += 'Desired Username\n';
				if (passwordValue == '') textMessage += 'Desired Password\n';
				if (passwordConfirmValue == '') textMessage += 'Confirm Password\n';
				
				if (message.length == 0 && (passwordValue != passwordConfirmValue)) {
					passwordMessage = 'Password and confirm password fields do not match';
				}

				if (textMessage) message += 'Please enter a value in:\n' + textMessage;
				if (passwordMessage) message += passwordMessage;
				return message;

			default: 
				var message = '';
				var textMessage = '';
				//var mandatoryTextFields = new Array('first_name', 'last_name', 'email', 'company', 'postcode', 'insurer', 'insurancepolicyno', 'insuranceeffectivedate', 'insuranceexpirydate', 'insurancepilimit');
				var mandatoryTextFields = new Array('first_name', 'last_name', 'email', 'company');
				var textInputElements = $(context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'form').getInputs('text');
		
				for (i = 0; i < textInputElements.length; i++) {
					for (var key in mandatoryTextFields) {
						if (typeof mandatoryTextFields[key] == "function") continue;
						
						if (textInputElements[i].id.endsWith(Service.SCOPE_SEPARATOR + mandatoryTextFields[key]) && textInputElements[i].value.length == 0) {
							textMessage += $(textInputElements[i].id).previous('label').firstChild.nodeValue.replace(':','') + '\n';
							break;
						}
					}
				}
		
				if (textMessage) message += 'Please enter a value in:\n' + textMessage + '\n';
				
				// EULA Checkbox
				if (!$(context + Service.SCOPE_SEPARATOR + 'editor' + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'eula').checked) message += 'You must agree to the terms and conditions of the EULA\n';
		
				// Certificate Number
				var certificateNumber = $(context + Service.SCOPE_SEPARATOR + 'editor' + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'certificate_number').value;
				if (certificateNumber.length != 0) {
					if (certificateNumber.length != 10) message += 'Your certificate number must be 10 characters long: 4 letters followed by 6 digits.\n';
					else {
						switch (certificateNumber.substr(0, 4)) {
							//L.D temp change for test cases.
							case 'TEST':
							case 'LCEA':
							case 'BREC':
							case 'ECMK':
							case 'STRO':
							case 'NHER':
							case 'EES/':
								break;
							default:
								message += 'Your certificate number does not belong to one of the schemes we support.  We support the following schemes: CIBSE, BRE, ECMK, NES, Elmhurst and Stroma.\n';
							break;
						}
					}
				}
				return message;
		}
	}
}
