/**
* Application event handling
*/

var _DEBUG = false;

// Object to store dirty flags

var dirty  = new Dirty();

// TABS
/**
* Force display of lists when clicking on a tab
*/

function tabSelect(args){
	var context = args.tabId;
	if (context == undefined || context == null || context.length == 0) return;
	var eventHandler = null;
	var entity = '';
	if(!Service.isActiveTab(context))return;
	switch(context) {
		case Context.QA:
		case Context.QACASES:
		case Context.QATASKS:
		case Context.QAREPORTS:
		case Context.QAXML:
			// QA service control
			QAService.controller(context);
			return;
		case Context.APELOLD:
			APService.controller(Context.APAPPLICANTS);
			break;
		case Context.AP:
		case Context.APEXTERNAL:
		case Context.APTASKS:
		case Context.APEXTERNALUSERS:
		case Context.APAPPLICANTS:
		case Context.APCIBSEUSERS:
		// AP service control
		APService.controller(context);
		return;
		case Context.ADDRESSES:
		CasaService.addresses(context);
		return;
		case Context.EAPROFILE:
		CasaService.eaProfile(context);
		return;
		case Context.EAPROFILE:
		CasaService.eaProfile(context);
		return;
		case Context.EAS:
			entity = Entity.EA;
			// Add searcher
			try {
				CasaService.searcherEAs(Context.EAS);
			} catch(err) {;}
		
		break;
		case Context.CASES:
			entity = Entity.CASE;
	
			// Standard framework stuff
	
			try {
				// Request a searcher interface for the case list
				var target = Context.CASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.CASE;
				
				var filterOptions = new FilterOptions();
				filterOptions.addFilter('active', 'Active Cases');
				filterOptions.addFilter('lodged', 'Lodged Cases');
				filterOptions.addFilter('cancelled', 'Cancelled Cases');
				filterOptions.addFilter('all', 'All Cases');
				
				Service.searcher(target, Context.CASES, Widget.FILTER_SEARCHER, Entity.CASE, filterOptions);
			} catch(err) {;}
			break;
		case Context.COMPLAINTS:
		entity = Entity.COMPLAINT;
		break;
		case Context.SURVEYS:
		entity = Entity.SURVEY;
		break;
		case Context.USERS:
		entity = Entity.USER;
		break;
		case Context.ACTIONTYPES:
		entity = Entity.ACTIONTYPE;
		break;
		case Context.ADMIN:
		AdminService.listLocks(Context.ADMIN);
		return;
		case Context.ADMINTOOLS:
		AdminService.listLocks(Context.ADMIN);
		AdminService.listRemoved(Context.ADMIN);
		return;
		case Context.ADDRESS_SEARCH:
		AdminService.addressSearch(context);
		return;
		case Context.ADMINCASES:
			CasaService.searcherAdminCases(Context.ADMINCASES);
		return;
		default:
			try{
				ASWorkbench.tabSelect(args);
			} catch(err){;}
			return;
	}
	try {
		var target = context + Service.SCOPE_SEPARATOR + Widget.LISTER + Service.SCOPE_SEPARATOR + entity;
		if ($(target).getTextContent().length == 0) Entity.list(context, entity, eventHandler);
	} catch(err) {;}

}



function failure() {
	$('message').update('sorry something went wrong');
}

Event.observe(window, 'load', init);

function init(){
	['busy'].each(Element.hide);
	document.body.style.cursor = "";
	$('message').update('');

	//Initialise the Apel Stands frameworkd
	try{
		ASWorkbench.init();	
	}catch(err){;}
	
	// QA PROCESS
	
	// QA CASES CONTEXT
	
	// Look for qa case events
	try {
		Event.observe(Context.QACASES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.QACASE, 'click',  function(event) {
			Qacase.handleEvent(event, Context.QACASES);
			Event.stop(event);
		});
	} catch(err) {;}
	
	try {
		Event.observe(Context.QACASES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.QACASE, 'click', function(event){
			QAService.refreshCaseList(Context.QACASES);
			Event.stop(event);
		});
	} catch(err) {;}

	try{
		Event.observe(Context.QACASES + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.QACASE, 'click',  function(event) {
			Qacase.create(Context.QACASES, Entity.QACASE);
			Event.stop(event);
		});
	} catch(err) {;}
	
	
	// Look for search events

	try {
		Event.observe(Context.QACASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.QACASE, 'click', function(event){
			// Got some search criteria
			var element = Event.element(event);
			if (element.id.endsWith('search') || element.id.endsWith('clear')) {
				QAService.caseSearch(Context.QACASES, event);				
			}
		});
		Event.observe(Context.QACASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.QACASE, 'keypress', function(event){
			// Got some search criteria
			if (event.keyCode == Event.KEY_RETURN) {
				var element = Event.element(event);
				if (element.id.endsWith('terms')) {
					QAService.caseSearch(Context.QACASES, event);
				}
				Event.stop(event);
			}
		});
	} catch(err) {;}
	
	//If none of the above QACase events have handled the event try here
	try{		
		Event.observe(Context.QACASES, 'click', function(event){
			Qacase.handleEvent(event,Context.QACASES);
		});
	}catch(err){;}
	
	// AP context
	//try {
	//	Event.observe(Context.AP, 'click',  function(event) {
	//		APService.handleEvent(event, Context.APEXTERNAL);
	//		Event.stop(event);
	//	});
	//} catch(err) {;}
	
	// AP External context
	try {
		Event.observe(Context.APEXTERNAL, 'click',  function(event) {
			APService.handleEvent(event, Context.APEXTERNAL);
			//Event.stop(event);
		});
	} catch(err) {;}
	
	// AP tasks context
	try {
		Event.observe(Context.APTASKS, 'click',  function(event) {
			APService.handleEvent(event, Context.APTASKS);
			var element = Event.element(event);	
			if (element.id.endsWith('-file')) {
				Event.stop(event);
			}
		});
	} catch(err) {;}
	
	
	/*L.D 
	 * Need to capture the next two events. This allows the pi form to be replaced
	 */
	try {
		Event.observe(Context.APTASKS, 'mousedown',  function(event) {
			APService.handleEvent(event, Context.APTASKS);
		});
	} catch(err) {;}
	
	try {
		Event.observe(Context.APTASKS, 'mouseup',  function(event) {
			APService.handleEvent(event, Context.APTASKS);
		});
	} catch(err) {;}
	
	
	
	
	
	

	try {
		Event.observe(Context.APTASKS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.TASK, 'click', function(event){
	
			// Refresh the tasks of a CIBSE user
			APService.refreshTaskList(Context.APTASKS);
			
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.APEXTERNAL + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.TASK, 'click', function(event){
			// Refresh the tasks of an external user
			APService.refreshTaskList(Context.APEXTERNAL);
			Event.stop(event);
		});
	} catch(err) {;}
	

	try {
		Event.observe(Context.APAPPLICANTS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.USER, 'click', function(event){
			// Refresh the applicants list and clear the viewer
			APService.refreshUserList(Context.APAPPLICANTS);
			Event.stop(event);
		});
	} catch(err) {;}
	
	//L.D Handle events for apel form in applicants context
	try {
		Event.observe(Context.APAPPLICANTS, 'click', function(event){
			APService.handleEvent(event, Context.APAPPLICANTS);
		});
	} catch(err) {;}
	
	try {
		Event.observe(Context.APAPPLICANTS + '-comments-form', 'keypress', function(event){
			// Auto-save on space
			if (event.charCode == 32) {
				APService.autoSaveComments(Context.APAPPLICANTS);
			}
		});
	} catch(err) {;}
/*
	try {
		Event.observe(Context.APAPPLICANTS + '-comments-form', 'click', function(event){
			APService.autoSaveComments(Context.APAPPLICANTS);
		});
	} catch(err) {;}
*/
	try {
		Event.observe(Context.APEXTERNALUSERS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.USER, 'click', function(event){
			// Refresh the external users list and clear the viewer
			APService.refreshUserList(Context.APEXTERNALUSERS);
			Event.stop(event);
		});
	} catch(err) {;}
	
	try {
		Event.observe(Context.APCIBSEUSERS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.USER, 'click', function(event){
			// Refresh the external users list and clear the viewer
			APService.refreshUserList(Context.APCIBSEUSERS);
			Event.stop(event);
		});
	} catch(err) {;}
	
	try {

		// REPORTS CONTEXT
		Event.observe(Context.REPORTS, 'click',  function(event) {
			Reports.handleEvent(event);
			Event.stop(event);
		});
	} catch(err) {;}

	// PROFILE CONTEXT
	try {
		
		// Password support
		Event.observe(Context.PROFILE + Service.SCOPE_SEPARATOR + 'password', 'click',  function(event) {
			AdminService.handlePasswordEvent(event, Context.PROFILE);
			Event.stop(event);
		});
	} catch(err) {;}

	// ADMIN CONTEXT
	try {
		Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'access', 'click',  function(event) {
			AdminService.handleAccessEvent(event, Context.ADMIN);
			Event.stop(event);
		});
		
		Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'access', 'keypress', function(event){
			var code = (event.keyCode ? event.keyCode : event.which);
			if (code == Event.KEY_RETURN) {
				var username = $(Context.ADMIN + Service.SCOPE_SEPARATOR + 'username').value;
				var password = $(Context.ADMIN + Service.SCOPE_SEPARATOR + 'password').value;
				var target = Context.ADMIN  + Service.SCOPE_SEPARATOR + 'access';
				AdminService.login(target, Context.ADMIN, username, password);
				Event.stop(event);
			}
		});		
		
		Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'removed', 'click',  function(event) {
			AdminService.handleRemovedEvent(event, Context.ADMIN);
			Event.stop(event);
		});
		
		Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'removed' + Service.SCOPE_SEPARATOR + 'controls', 'click',  function(event) {
			AdminService.handleRemovedEvent(event, Context.ADMIN);
			Event.stop(event);
		});

		Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + 'lock', 'click',  function(event) {
			AdminService.listLocks(Context.ADMIN);
			Event.stop(event);
		});

		Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'removeall' + Service.SCOPE_SEPARATOR + 'lock', 'click',  function(event) {
			AdminService.removeAllLocks(Context.ADMIN);
			Event.stop(event);
		});

		Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'removeselected' + Service.SCOPE_SEPARATOR + 'lock', 'click',  function(event) {
			AdminService.removeSelectedLocks(Context.ADMIN);
			Event.stop(event);
		});

		Event.observe(Context.ADMIN_EAS, 'click',  function(event) {
			CasaService.handleEAAdminEvent(event, Context.ADMIN);
			Event.stop(event);
		});

	} catch(err) {;}




	// GLOBAL CONTEXT
	
	//Language.loadOptions();

	Event.observe('message', 'click', function(){
		$('message').update('');
	});

	try {

		// SOFTWARE DOWNLOADS CONTEXT
		Event.observe(Context.DOWNLOADS, 'click',  function(event) {
			var element = Event.element(event);
			if (element.id != 'shop' && element.id != 'mail.key' && element.id != 'message') {
				CasaService.handleEvent(event, Context.DOWNLOADS);
				Event.stop(event);
			}
		});
	} catch(err) {;}

	// QA CONTEXT
	try {
		Event.observe(Context.QATASKS, 'click', function(event){
			var element = Event.element(event);
			var elementID = element.id;
			QAService.handleEvent(event,Context.QATASKS)
		//	Event.stop(event);
			// The block below is no longer supported because download features on the Case interface
			/*
			if (elementID == 'qa-xml-download') {
				alert('hit');
				asaService.caseXML(Context.QA);
			}
			*/
		});
	} catch(err) {;}

	try {
		Event.observe(Context.QA , 'click', function(event){
			var element = Event.element(event);
			var elementID = element.id;
			context = Names.getContext(elementID);
			if(context==Context.QA){
				QAService.handleEvent(event,Context.QA)
			}
		//	Event.stop(event);
			// The block below is no longer supported because download features on the Case interface
			/*
			if (elementID == 'qa-xml-download') {
				alert('hit');
				asaService.caseXML(Context.QA);
			}
			*/
		});
	} catch(err) {;}

	// EAS_ACTIONS CONTEXT

	try {
		Event.observe(Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click', function(event){
			// Get the currently selected ea
			var relatedEntityField = Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'entity';
			var relatedEntity = $(relatedEntityField).value;
			var relatedEntityIDField = Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID';
			var relatedEntityID = $(relatedEntityIDField).value;
	  		var filter = new ListFilter(relatedEntity, relatedEntityID);
			Entity.refreshList(Context.EAS_ACTIONS, Entity.ACTION, null, false, null, filter);
			Event.stop(event);
		});

		Event.observe(Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click',  function(event) {
			var instanceIDField = Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID'; 
			var instanceID = $(instanceIDField).value;
			if (instanceID.length > 0) Entity.create(Context.EAS_ACTIONS, Entity.ACTION);
			else alert('Please select an Energy Assessor');
			Event.stop(event);
		});
	
		Event.observe(Context.EAS_ACTIONS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click',  function(event) {
			Entity.handleEvent(event, Context.EAS_ACTIONS, Entity.ACTION);
			Event.stop(event);
		});
	} catch(err) {;}
	
	// EAS_NOTES CONTEXT

	try {
		Event.observe(Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click', function(event){
			// Get the currently selected ea
			var relatedEntityField = Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'entity';
			var relatedEntity = $(relatedEntityField).value;
			var relatedEntityIDField = Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'instanceID';
			var relatedEntityID = $(relatedEntityIDField).value;
	  		var filter = new ListFilter(relatedEntity, relatedEntityID);
			Entity.refreshList(Context.EAS_NOTES, Entity.NOTE, null, false, null, filter);
			Event.stop(event);
		});

		Event.observe(Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click',  function(event) {
			var instanceIDField = Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'instanceID'; 
			var instanceID = $(instanceIDField).value;
			if (instanceID.length > 0) Entity.create(Context.EAS_NOTES, Entity.NOTE);
			else alert('Please select an Energy Assessor');
			Event.stop(event);
		});
	
		Event.observe(Context.EAS_NOTES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click',  function(event) {
			Entity.handleEvent(event, Context.EAS_NOTES, Entity.NOTE);
			Event.stop(event);
		});
	} catch(err) {;}
	
	// CASES_ACTIONS CONTEXT

	try {
		Event.observe(Context.CASES_ACTIONS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click', function(event){
			// Get the currently selected case
			var relatedEntityField = Context.CASES_ACTIONS + Service.SCOPE_SEPARATOR + 'entity';
			var relatedEntity = $(relatedEntityField).value;
			var relatedEntityIDField = Context.CASES_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID';
			var relatedEntityID = $(relatedEntityIDField).value;
	  		var filter = new ListFilter(relatedEntity, relatedEntityID);
			Entity.refreshList(Context.CASES_ACTIONS, Entity.ACTION, null, false, null, filter);
			Event.stop(event);
		});

		Event.observe(Context.CASES_ACTIONS + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click',  function(event) {
			var instanceIDField = Context.CASES_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID'; 
			var instanceID = $(instanceIDField).value;
			if (instanceID.length > 0) Entity.create(Context.CASES_ACTIONS, Entity.ACTION);
			else alert('Please select a Case');			
			Event.stop(event);
		});
	
		Event.observe(Context.CASES_ACTIONS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click',  function(event) {
			Entity.handleEvent(event, Context.CASES_ACTIONS, Entity.ACTION);
			Event.stop(event);
		});
	} catch(err) {}
	
	// CASES_NOTES CONTEXT

	try {
		Event.observe(Context.CASES_NOTES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click', function(event){
			// Get the currently selected case
			var relatedEntityField = Context.CASES_NOTES + Service.SCOPE_SEPARATOR + 'entity';
			var relatedEntity = $(relatedEntityField).value;
			var relatedEntityIDField = Context.CASES_NOTES + Service.SCOPE_SEPARATOR + 'instanceID';
			var relatedEntityID = $(relatedEntityIDField).value;
	  		var filter = new ListFilter(relatedEntity, relatedEntityID);
			Entity.refreshList(Context.CASES_NOTES, Entity.NOTE, null, false, null, filter);
			Event.stop(event);
		});

		Event.observe(Context.CASES_NOTES + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click',  function(event) {
			var instanceIDField = Context.CASES_NOTES + Service.SCOPE_SEPARATOR + 'instanceID'; 
			var instanceID = $(instanceIDField).value;
			if (instanceID.length > 0) Entity.create(Context.CASES_NOTES, Entity.NOTE);
			else alert('Please select a Case');
			Event.stop(event);
		});
	
		Event.observe(Context.CASES_NOTES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click',  function(event) {
			Entity.handleEvent(event, Context.CASES_NOTES, Entity.NOTE);
			Event.stop(event);
		});
	} catch(err) {;}

	// COMPLAINTS_ACTIONS CONTEXT

	try {
		Event.observe(Context.COMPLAINTS_ACTIONS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click', function(event){
			// Get the currently selected complaint
			var relatedEntityField = Context.COMPLAINTS_ACTIONS + Service.SCOPE_SEPARATOR + 'entity';
			var relatedEntity = $(relatedEntityField).value;
			var relatedEntityIDField = Context.COMPLAINTS_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID';
			var relatedEntityID = $(relatedEntityIDField).value;
	  		var filter = new ListFilter(relatedEntity, relatedEntityID);
			Entity.refreshList(Context.COMPLAINTS_ACTIONS, Entity.ACTION, null, false, null, filter);
			Event.stop(event);
		});

		Event.observe(Context.COMPLAINTS_ACTIONS + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click',  function(event) {
			var instanceIDField = Context.COMPLAINTS_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID'; 
			var instanceID = $(instanceIDField).value;
			if (instanceID.length > 0) Entity.create(Context.COMPLAINTS_ACTIONS, Entity.ACTION);
			else alert('Please select a Complaint');						
			Event.stop(event);
		});
	
		Event.observe(Context.COMPLAINTS_ACTIONS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click',  function(event) {
			Entity.handleEvent(event, Context.COMPLAINTS_ACTIONS, Entity.ACTION);
			Event.stop(event);
		});
	} catch(err) {}

	// COMPLAINTS_NOTES CONTEXT

	try {
		Event.observe(Context.COMPLAINTS_NOTES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click', function(event){
			// Get the currently selected complaint
			var relatedEntityField = Context.COMPLAINTS_NOTES + Service.SCOPE_SEPARATOR + 'entity';
			var relatedEntity = $(relatedEntityField).value;
			var relatedEntityIDField = Context.COMPLAINTS_NOTES + Service.SCOPE_SEPARATOR + 'instanceID';
			var relatedEntityID = $(relatedEntityIDField).value;
	  		var filter = new ListFilter(relatedEntity, relatedEntityID);
			Entity.refreshList(Context.COMPLAINTS_NOTES, Entity.NOTE, null, false, null, filter);
			Event.stop(event);
		});

		Event.observe(Context.COMPLAINTS_NOTES + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click',  function(event) {
			var instanceIDField = Context.COMPLAINTS_NOTES + Service.SCOPE_SEPARATOR + 'instanceID'; 
			var instanceID = $(instanceIDField).value;
			if (instanceID.length > 0) Entity.create(Context.COMPLAINTS_NOTES, Entity.NOTE);
			else alert('Please select a Complaint');
			Event.stop(event);
		});
	
		Event.observe(Context.COMPLAINTS_NOTES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click',  function(event) {
			Entity.handleEvent(event, Context.COMPLAINTS_NOTES, Entity.NOTE);
			Event.stop(event);
		});
	} catch(err) {;}

	// SURVEYS_ACTIONS CONTEXT

	try {
		Event.observe(Context.SURVEYS_ACTIONS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click', function(event){
			// Get the currently selected survey
			var relatedEntityField = Context.SURVEYS_ACTIONS + Service.SCOPE_SEPARATOR + 'entity';
			var relatedEntity = $(relatedEntityField).value;
			var relatedEntityIDField = Context.SURVEYS_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID';
			var relatedEntityID = $(relatedEntityIDField).value;
	  		var filter = new ListFilter(relatedEntity, relatedEntityID);
			Entity.refreshList(Context.SURVEYS_ACTIONS, Entity.ACTION, null, false, null, filter);
			Event.stop(event);
		});

		Event.observe(Context.SURVEYS_ACTIONS + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click',  function(event) {
			var instanceIDField = Context.SURVEYS_ACTIONS + Service.SCOPE_SEPARATOR + 'instanceID'; 
			var instanceID = $(instanceIDField).value;
			if (instanceID.length > 0) Entity.create(Context.SURVEYS_ACTIONS, Entity.ACTION);
			else alert('Please select a Survey Result');									
			Event.stop(event);
		});
	
		Event.observe(Context.SURVEYS_ACTIONS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.ACTION, 'click',  function(event) {
			Entity.handleEvent(event, Context.SURVEYS_ACTIONS, Entity.ACTION);
			Event.stop(event);
		});
	} catch(err) {}

	// SURVEYS_NOTES CONTEXT

	try {
		Event.observe(Context.SURVEYS_NOTES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click', function(event){
			// Get the currently selected survey
			var relatedEntityField = Context.SURVEYS_NOTES + Service.SCOPE_SEPARATOR + 'entity';
			var relatedEntity = $(relatedEntityField).value;
			var relatedEntityIDField = Context.SURVEYS_NOTES + Service.SCOPE_SEPARATOR + 'instanceID';
			var relatedEntityID = $(relatedEntityIDField).value;
	  		var filter = new ListFilter(relatedEntity, relatedEntityID);
			Entity.refreshList(Context.SURVEYS_NOTES, Entity.NOTE, null, false, null, filter);
			Event.stop(event);
		});

		Event.observe(Context.SURVEYS_NOTES + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click',  function(event) {
			var instanceIDField = Context.SURVEYS_NOTES + Service.SCOPE_SEPARATOR + 'instanceID'; 
			var instanceID = $(instanceIDField).value;
			if (instanceID.length > 0) Entity.create(Context.SURVEYS_NOTES, Entity.NOTE);
			else alert('Please select a Survey Result');
			Event.stop(event);
		});
	
		Event.observe(Context.SURVEYS_NOTES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.NOTE, 'click',  function(event) {
			Entity.handleEvent(event, Context.SURVEYS_NOTES, Entity.NOTE);
			Event.stop(event);
		});
	} catch(err) {;}

	// EAS CONTEXT
	

	// Look for search events

	try {
		Event.observe(Context.EAS + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.EA, 'click', function(event){
			var element = Event.element(event);
			if (element.id.endsWith('search') || element.id.endsWith('clear') || element.id.endsWith('ea') || element.id.endsWith('name')) Ea.search(Context.EAS, event);
		});
	
		Event.observe(Context.EAS + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.EA, 'keypress', function(event){
			// Got some search criteria
			if (event.keyCode == Event.KEY_RETURN) {
				var element = Event.element(event);
				if (element.id.endsWith('terms')) Ea.search(Context.EAS, event);
				Event.stop(event);
			}
		});
	} catch(err) {;}

	try {
		Event.observe(Context.EAS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.EA, 'click', function(event){
			Ea.refreshList(Context.EAS);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.EAS + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.EA, 'click',  function(event) {			
			Entity.create(Context.EAS, Entity.EA);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.EAS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.EA, 'click',  function(event) {		
			Ea.handleEvent(event, Context.EAS);
			//Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.EAS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + "special" + Service.SCOPE_SEPARATOR + Entity.EA, 'click',  function(event) {			
			Entity.handleEvent(event, Context.EAS, Entity.EA);
		});
		
	} catch(err) {;}

	// ADMINCASES CONTEXT
	
	// Look for admin case events
	try {
		Event.observe(Context.ADMINCASES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.CASE, 'click',  function(event) {
			Case.handleEvent(event, Context.ADMINCASES);
			Event.stop(event);
		});
	} catch(err) {;}
	
	try {
		Event.observe(Context.ADMINCASES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.CASE, 'click', function(event){
			Case.refreshAdminList(Context.ADMINCASES);
			Event.stop(event);
		});
	} catch(err) {;}

	
	// Look for search events

	try {
		Event.observe(Context.ADMINCASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.CASE, 'click', function(event){
			// Got some search criteria
			var element = Event.element(event);
			if (element.id.endsWith('search') || element.id.endsWith('clear') || element.id.endsWith('ea') || element.id.endsWith('uprn') || element.id.endsWith('rrn')|| element.id.endsWith('case')) Case.adminSearch(Context.ADMINCASES, event);
		});
	
		Event.observe(Context.ADMINCASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.CASE, 'keypress', function(event){
			// Got some search criteria
			if (event.keyCode == Event.KEY_RETURN) {
				var element = Event.element(event);
				if (element.id.endsWith('terms')) Case.adminSearch(Context.ADMINCASES, event);
				Event.stop(event);
			}
		});
	} catch(err) {;}


	// CASES CONTEXT

	// Look for case editor events
	try {
		Event.observe(Context.CASES + Service.SCOPE_SEPARATOR + Entity.CASE, 'click', function(event){
			Case.handleEvent(event, Context.CASES);
		});
	} catch(err) {;}
	
	try {
		Event.observe(Context.CASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.CASE, 'click', function(event){
			// Got some search criteria
			var element = Event.element(event);
			if (element.id.endsWith('search') || element.id.endsWith('clear')) Case.search(Context.CASES, event);
			Event.stop(event);
		});
	
		Event.observe(Context.CASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + Entity.CASE, 'keypress', function(event){
			// Got some search criteria
			if (event.keyCode == Event.KEY_RETURN) {
				var element = Event.element(event);
				if (element.id.endsWith('terms')) Case.search(Context.CASES, event);
				Event.stop(event);
			}
		});
	} catch(err) {;}

	try {
		Event.observe(Context.CASES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.CASE, 'click', function(event){
			//Entity.refreshList(Context.CASES, Entity.CASE);
			Case.refreshList(Context.CASES);
			try {
				$('cases-searcher-case-terms').value = '';	
			} catch(err) {;}	
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.CASES + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.CASE, 'click',  function(event) {			
			Entity.create(Context.CASES, Entity.CASE);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.CASES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.CASE, 'click',  function(event) {
			Entity.handleEvent(event, Context.CASES, Entity.CASE);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.CASES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + "special" + Service.SCOPE_SEPARATOR + Entity.CASE, 'click',  function(event) {			
			Entity.handleEvent(event, Context.CASES, Entity.CASE);
		});
	} catch(err) {;}
	
	// ADDRESSES CONTEXT
	
	// Look for address manage events
	try {
		Event.observe(Context.ADDRESSES, 'click', function(event){
			CasaService.handleEvent(event, Context.ADDRESSES);
		});
	} catch(err) {;}
	

	// COMPLAINTS CONTEXT

	try {
		Event.observe(Context.COMPLAINTS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.COMPLAINT, 'click', function(event){
			Entity.refreshList(Context.COMPLAINTS, Entity.COMPLAINT);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.COMPLAINTS + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.COMPLAINT, 'click',  function(event) {			
			Entity.create(Context.COMPLAINTS, Entity.COMPLAINT);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.COMPLAINTS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.COMPLAINT, 'click',  function(event) {		
			Entity.handleEvent(event, Context.COMPLAINTS, Entity.COMPLAINT);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.COMPLAINTS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + "special" + Service.SCOPE_SEPARATOR + Entity.COMPLAINT, 'click',  function(event) {			
			Entity.handleEvent(event, Context.COMPLAINTS, Entity.COMPLAINT);
		});
	} catch(err) {;}
	
	// SURVEYS CONTEXT

	try {
		Event.observe(Context.SURVEYS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.SURVEY, 'click', function(event){
			Entity.refreshList(Context.SURVEYS, Entity.SURVEY);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.SURVEYS + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.SURVEY, 'click',  function(event) {			
			Entity.create(Context.SURVEYS, Entity.SURVEY);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.SURVEYS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.SURVEY, 'click',  function(event) {		
			Entity.handleEvent(event, Context.SURVEYS, Entity.SURVEY);
			Event.stop(event);
		});
	} catch(err) {;}

	try {
		Event.observe(Context.SURVEYS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + "special" + Service.SCOPE_SEPARATOR + Entity.SURVEY, 'click',  function(event) {			
			Entity.handleEvent(event, Context.SURVEYS, Entity.SURVEY);
		});
	} catch(err) {;}

	// ACTION TYPES
	try {
		Event.observe(Context.ACTIONTYPES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.ACTIONTYPE, 'click', function(event){
			Entity.refreshList(Context.ACTIONTYPES, Entity.ACTIONTYPE);
			Event.stop(event);
		});

		Event.observe(Context.ACTIONTYPES + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.ACTIONTYPE, 'click',  function(event) {
			Entity.create(Context.ACTIONTYPES, Entity.ACTIONTYPE);
			Event.stop(event);
		});

		Event.observe(Context.ACTIONTYPES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.ACTIONTYPE, 'click',  function(event) {
			Entity.handleEvent(event, Context.ACTIONTYPES, Entity.ACTIONTYPE);
			Event.stop(event);
		});
	} catch(err) {;}

	// USERS
	try {
		Event.observe(Context.USERS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + Entity.USER, 'click', function(event){
			var context = Context.USERS;
			var entity = Entity.USER;
			Entity.refreshList(context, entity);
			Event.stop(event);
		});

		Event.observe(Context.USERS + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + Entity.USER, 'click',  function(event) {
			Entity.create(Context.USERS, Entity.USER);
			Event.stop(event);
		});

		Event.observe(Context.USERS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + Entity.USER, 'click',  function(event) {
			Entity.handleEvent(event, Context.USERS, Entity.USER);
			Event.stop(event);
		});
	} catch(err) {;}

}

