/**
 * Encapsulate the case entity type. See the case database table
 */ 
  
var Case = {
			
	entityName: Entity.CASE,

	handleEvent: function(event, context) {
		var element = Event.element(event);
		elementID = element.id;
		if (elementID.endsWith(Service.SCOPE_SEPARATOR + Widget.CASE_EDITOR + Service.SCOPE_SEPARATOR + 'getproperty')) {
			CasaService.registerGetProperty(Context.CASES);
		} else if (elementID.endsWith(Service.SCOPE_SEPARATOR + Widget.CASE_EDITOR + Service.SCOPE_SEPARATOR + 'upload')) {
			CasaService.fileUpload(Context.CASES);
		} else if (elementID.endsWith(Service.SCOPE_SEPARATOR + Widget.CASE_EDITOR + Service.SCOPE_SEPARATOR + 'registration')) {
			CasaService.qaRegistration(Context.CASES);
		} else if (elementID.endsWith(Service.SCOPE_SEPARATOR + Widget.CASE_EDITOR + Service.SCOPE_SEPARATOR + 'casecancel')) {
			if(confirm("Are you sure you want to cancel this case?")) CasaService.caseCancel(Context.CASES);
		} else if (elementID.endsWith(Service.SCOPE_SEPARATOR + Widget.CASE_EDITOR + Service.SCOPE_SEPARATOR + 'casediscardproperty')) {
			if(confirm("Are you sure you want to discard this case's property?")) CasaService.caseDiscardProperty(Context.CASES);
		} else if (elementID.endsWith(Service.SCOPE_SEPARATOR + Widget.CASE_EDITOR + Service.SCOPE_SEPARATOR + 'multiaddress')) {
			CasaService.multiAddress(Context.CASES);
		} else if (elementID.endsWith(Service.SCOPE_SEPARATOR + Widget.CASE_EDITOR + Service.SCOPE_SEPARATOR + 'multiuprn')) {
			CasaService.multiUPRN(Context.CASES);
		} else if (elementID.endsWith('cancel_case')) {
			CasaService.registerChangeReportStatus(CasaService.REGISTER_CASE_CANCEL);
		} else if (elementID.endsWith('appeal_case')) {
			CasaService.registerChangeReportStatus(CasaService.REGISTER_CASE_APPEAL);
		} else if (elementID.endsWith('remove_case')) {
			alert('Not implemented');
		}
		else if (elementID.endsWith('addToQA')) {
			QAService.addCaseToQA();
		}
		return;
	},
	
	adminSearch: function(context, event){
		var element = Event.element(event);
		if (element.id.endsWith('clear')) {
			$(context + Service.SCOPE_SEPARATOR + Widget.ADMIN_CASE_SEARCHER + Service.SCOPE_SEPARATOR + 'term').value = '';
			$('admincases-lister-case').update('');
			Entity.closeViewer(context, this.entityName);
		} else if (element.id.endsWith('search') || element.id.endsWith('term')) {
			Case.refreshAdminList(context);
		}
	},
	
	search: function(context, event){
		var element = Event.element(event);
		
		var filter = new ListFilters();
		var filterOption = "";
		try {
			filterOption = $(context + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'filter').value;
		} catch(err) {;}
		if (filterOption.length > 0) {
			filter.addFilter('case-status', filterOption);
		}
		
		if (element.id.endsWith('search') || element.id.endsWith('terms')) {
			var searchTerms = $(context + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'terms').value;
			if (searchTerms.trim().length > 0) {
				var term = searchTerms.trim();
				filter.addFilter('case-name', term);
				filter.addFilter('case-uprn', term);
				filter.addFilter('case-postcode', term);
				filter.addFilter('case-posttown', term);
				filter.addFilter('case-case_id', term);
				filter.addFilter('case-rrn', term);
				filter.addFilter('case-related_rrn', term);
			}
		} else if (element.id.endsWith('clear')) {
			$(context + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'terms').value = '';
		}
		Case.refreshList(context, filter);
	},

	/*
	* This method is invoked in order to add a filter based on search criteria
	*/
	refreshAdminList: function(context){
		CasaService.listAdminCases(context);
		Entity.closeViewer(context, this.entityName);
	},

	/*
	* This method is invoked in order to add a filter based on search criteria
	*/
	refreshList: function(context, filter, eventHandler, suppressDirty){
		if(dirty.getDirty(context) != undefined && (suppressDirty == 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 + Widget.LISTER + Service.SCOPE_SEPARATOR + this.entityName;
		if (eventHandler == undefined || eventHandler == null || eventHandler.length == 0) eventHandler = 'Entity.view(\'' + context + '\',\'' + this.entityName + '\',\'@id\')';
		Service.lister(target, context, this.entityName, null, eventHandler, '10', '', filter);
		Entity.closeViewer(context, this.entityName);
	},

	create: function(context){
	},

	edit: function(context, entityID){
	},

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

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

	closeViewer: function(context) {
		$(Context.CASES_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID').value = '';
		$(Context.CASES_NOTES + Service.SCOPE_SEPARATOR + 'instanceID').value = '';
		try {
			Action.refreshPanel(Context.CASES_ACTIONS);
		} catch(err) {;}
		try {
			Note.refreshPanel(Context.CASES_NOTES);
		} catch(err) {;}
	},
	
	validate: function(context){
		var message = '';
		var textMessage = '';
		var textInputElements = $(context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'form').getInputs('text');

  		var idPrefix = 'cases-editor-case-';
 		// Check the build type option (EPC only)
		try {
			var value = $(idPrefix + 'build_type').options.selectedIndex;
			if (value == 0) {
				message += 'Please select a Build Type option.\n'
			}
		} catch(err) {;}
		// Check the data gathering option
		var value = $(idPrefix + 'data_gathering').options.selectedIndex;
		if (value == 0) {
			message += 'Please select a Data Gathering option.\n'
		}
	
		for (i = 0; i < textInputElements.length; i++) {
			if (!$(textInputElements[i]).id.endsWith('-name')) {
				continue;
			} else {
				var value = $(textInputElements[i]).value.trim();
				if (value.length == 0) textMessage += $(textInputElements[i].id).previous('label').innerHTML.replace(':','') + '\n';				
			}
		}
		if(textMessage) message += 'Please enter a value in:\n' + textMessage;
		return message;
	}
	
}
