//#############################################################################
//#                                                                           #
//#  Copyright (C) <2014>  <IMS MAXIMS>                                       #
//#                                                                           #
//#  This program is free software: you can redistribute it and/or modify     #
//#  it under the terms of the GNU Affero General Public License as           #
//#  published by the Free Software Foundation, either version 3 of the       #
//#  License, or (at your option) any later version.                          # 
//#                                                                           #
//#  This program is distributed in the hope that it will be useful,          #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of           #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
//#  GNU Affero General Public License for more details.                      #
//#                                                                           #
//#  You should have received a copy of the GNU Affero General Public License #
//#  along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Daniel Laffan using IMS Development Environment (version 1.21 build 41015.1700)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.admin.forms.locationservice;

import java.util.List;
import java.util.ArrayList;

import ims.admin.forms.locationservice.GenForm.lyr1Layer.EditContainer.grdServiceRow;
import ims.admin.forms.locationservice.GenForm.lyr1Layer.EditContainer.lyrLocationDetailLayer.tabPageActivityContainer.grdActivityRow;
import ims.admin.forms.locationservice.GenForm.lyr1Layer.EditContainer.lyrLocationDetailLayer.tabPageFunctionContainer.grdServiceFunctionRow;
import ims.admin.helper.NTPFExchequerCodingChecks;
import ims.admin.helper.OrgTreeHelper;
import ims.configuration.AppRight;
import ims.configuration.gen.ConfigFlag;
import ims.core.resource.place.vo.LocationRefVo;
import ims.core.vo.ActivityContextPricingDetailsVo;
import ims.core.vo.ActivityContextPricingDetailsVoCollection;
import ims.core.vo.ActivityVo;
import ims.core.vo.LocShortVo;
import ims.core.vo.LocationServiceMidVo;
import ims.core.vo.LocationServiceMidVoCollection;
import ims.core.vo.LocationServiceVo;
import ims.core.vo.LocationServiceVoCollection;
import ims.core.vo.LocMostVo;
import ims.core.vo.LocationServiceFunctionsVoCollection;
import ims.core.vo.LocationServiceActivityVoCollection;
import ims.core.vo.LocationServiceActivityVo;
import ims.core.vo.LocationServiceFunctionsVo;
import ims.core.vo.OrganisationVo;
import ims.core.vo.OrganisationVoCollection;
import ims.core.vo.ServiceContactVo;
import ims.core.vo.PersonName;
import ims.core.vo.ServiceFunctionVo;
import ims.core.vo.ServiceFunctionVoCollection;
import ims.core.vo.ServiceActivityVo;
import ims.core.vo.ServiceActivityVoCollection;
import ims.core.vo.ServiceShortVo;
import ims.core.vo.ServiceVo;
import ims.core.vo.lookups.ServiceCategory;
import ims.core.vo.lookups.LocationType;
import ims.core.vo.lookups.TaxonomyType;
import ims.core.vo.ServiceShortVoCollection;
import ims.domain.exceptions.ForeignKeyViolationException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.framework.Control;
import ims.framework.FormName;
import ims.framework.MessageButtons;
import ims.framework.MessageIcon;
import ims.framework.controls.TreeNode;
import ims.framework.controls.TreeNodeCollection;
import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Color;
import ims.framework.utils.DecimalFormat;
import ims.framework.utils.TextileString;
import ims.reports.ReportEngine;

public class Logic extends BaseLogic
{
	private final static int	PRICING_NEW		= 1;
	private final static int	PRICING_UPDATE	= 2;
	private final static int	PRICING_VIEW	= 3;

	private void initalize()
	{
		form.getLocalContext().settreMessage("No Organisation/Location/Site found.");
		form.imgLocation().setValue(form.getImages().Admin.Location);
		form.imgLocSite().setValue(form.getImages().Admin.LocationSite);
		form.imgOrganisation().setValue(form.getImages().Admin.Organisation);

		form.pnlLocationService().setValue("Location " + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(true) + " Configuration");

		form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setCaption("Location " + ConfigFlag.UI.DISPLAY_NAME_ACTIVITY.getValue(true));
		form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setCaption("Location " + ConfigFlag.UI.DISPLAY_NAME_FUNCTION.getValue(true));

		form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().setActivityCaption(ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(true) + " " + ConfigFlag.UI.DISPLAY_NAME_ACTIVITY.getValue(true));
		form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().setFunctionCaption(ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(true) + " " + ConfigFlag.UI.DISPLAY_NAME_FUNCTION.getValue(true));

		form.lyr1().Edit().grdService().setServiceCaption(ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(true));

		form.lyr1().Edit().grdService().setReadOnly(false);

		if (ConfigFlag.GEN.SHOW_PROCEDURE_PRICING.getValue())
		{
			form.lyr1().Details().chkHTMLActiveOnly().setValue(true);
			form.lyr1().Details().chkHTMLActiveOnly().setVisible(true);
		}
		else
			form.lyr1().Details().chkHTMLActiveOnly().setVisible(false);

		showActivityFunctionTabs();
	}

	private void showActivityFunctionTabs()
	{

		if (ConfigFlag.UI.LOC_SERVICE_TABS.getValue().equals("ACT"))
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setHeaderVisible(true);

			form.lyr1().Edit().lyrLocationDetail().showtabPageActivity();
		}
		else if (ConfigFlag.UI.LOC_SERVICE_TABS.getValue().equals("ACT-FUNC"))
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setHeaderVisible(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setHeaderVisible(true);

			form.lyr1().Edit().lyrLocationDetail().showtabPageActivity();
		}
		else if (ConfigFlag.UI.LOC_SERVICE_TABS.getValue().equals("FUNC"))
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setHeaderVisible(true);

			form.lyr1().Edit().lyrLocationDetail().showtabPageFunction();
		}
		else if (ConfigFlag.UI.LOC_SERVICE_TABS.getValue().equals("CON"))
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().setHeaderVisible(true);

			form.lyr1().Edit().lyrLocationDetail().showtabPageContact();
		}
		else if (ConfigFlag.UI.LOC_SERVICE_TABS.getValue().equals("CON-ACT"))
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().setHeaderVisible(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setHeaderVisible(true);

			form.lyr1().Edit().lyrLocationDetail().showtabPageContact();
		}
		else if (ConfigFlag.UI.LOC_SERVICE_TABS.getValue().equals("CON-FUNC"))
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().setHeaderVisible(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setHeaderVisible(true);

			form.lyr1().Edit().lyrLocationDetail().showtabPageContact();
		}
		else if (ConfigFlag.UI.LOC_SERVICE_TABS.getValue().equals("ALL"))
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().setHeaderVisible(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setHeaderVisible(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setHeaderVisible(true);

			form.lyr1().Edit().lyrLocationDetail().showtabPageContact();
		}
		else if (ConfigFlag.UI.LOC_SERVICE_TABS.getValue().equals("NONE"))
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().setHeaderVisible(false);
			form.lyr1().Edit().lyrLocationDetail().hide();
		}

	}

	private void populateListControl()
	{
		OrganisationVoCollection voColl = domain.listOrganisation(null);

		new OrgTreeHelper(form.getImages().Admin.Organisation, form.getImages().Admin.LocationSite, form.getImages().Admin.Location).populateOrgTree(form.treHeader(), voColl);
		form.treHeader().expandAll();
	}

	public void open()
	{
		clearInstanceControls();
		populateListControl();

		form.setMode(FormMode.VIEW);
	}

	private void populateLocationServices(TreeNode node, LocShortVo voLocation)
	{
		LocationServiceMidVoCollection voLocationServices;
		voLocationServices = domain.listLocationServiceLite(voLocation);
		TreeNode childServiceNode = null;
		clearServiceNodes(node);
		for (int i = 0; i < voLocationServices.size(); i++)
		{
			if (voLocationServices.get(i).getServiceIsNotNull())
			{
				childServiceNode = node.getNodes().add(voLocationServices.get(i), (voLocationServices.get(i).getService().getServiceNameIsNotNull() ? voLocationServices.get(i).getService().getServiceName() : ""));
				childServiceNode.setTooltip(formatLocationServicevoTooltip(voLocationServices.get(i)));
				childServiceNode.setExpandedImage(voLocationServices.get(i).getIsActiveIsNotNull() ? (voLocationServices.get(i).getIsActive().booleanValue() ? form.getImages().Admin.Service : form.getImages().Admin.ServiceInactive) : form.getImages().Admin.Service);
				childServiceNode.setCollapsedImage(voLocationServices.get(i).getIsActiveIsNotNull() ? (voLocationServices.get(i).getIsActive().booleanValue() ? form.getImages().Admin.Service : form.getImages().Admin.ServiceInactive) : form.getImages().Admin.Service);
				childServiceNode.setSelectedImage(voLocationServices.get(i).getIsActiveIsNotNull() ? (voLocationServices.get(i).getIsActive().booleanValue() ? form.getImages().Admin.Service : form.getImages().Admin.ServiceInactive) : form.getImages().Admin.Service);
				childServiceNode.sortChildrenByText();
				childServiceNode.setExpanded(false);
			}
		}

		node.setExpanded(true);

		form.lyr1().Details().chkHTMLActiveOnly().setValue(true);
		form.lyr1().Details().htmDetails().setHTML(getDetailsAsTextile(node));

	}

	private void populateServiceActivities(TreeNode node, LocationServiceVoCollection voLocationServices)
	{
		TreeNode childActivityNode = null;
		TreeNode childFunctionNode = null;

		clearServiceNodes(node);

		for (int i = 0; i < voLocationServices.size(); i++)
		{
			if (voLocationServices.get(i).getServiceIsNotNull())
			{
				node.setValue(voLocationServices.get(i));
				for (int j = 0; j < voLocationServices.get(i).getActivities().size(); j++)
				{
					childActivityNode = node.getNodes().add(voLocationServices.get(i).getActivities().get(j), (voLocationServices.get(i).getActivities().get(j).getServiceActivityIsNotNull() ? voLocationServices.get(i).getActivities().get(j).getServiceActivity().getActivity().toString() : ""));
					childActivityNode.setExpandedImage(voLocationServices.get(i).getActivities().get(j).getIsActiveIsNotNull() ? (voLocationServices.get(i).getActivities().get(j).getIsActive().booleanValue() ? form.getImages().Admin.Activity : form.getImages().Admin.ActivityInactive) : form.getImages().Admin.Activity);
					childActivityNode.setCollapsedImage(voLocationServices.get(i).getActivities().get(j).getIsActiveIsNotNull() ? (voLocationServices.get(i).getActivities().get(j).getIsActive().booleanValue() ? form.getImages().Admin.Activity : form.getImages().Admin.ActivityInactive) : form.getImages().Admin.Activity);
					childActivityNode.setSelectedImage(voLocationServices.get(i).getActivities().get(j).getIsActiveIsNotNull() ? (voLocationServices.get(i).getActivities().get(j).getIsActive().booleanValue() ? form.getImages().Admin.Activity : form.getImages().Admin.ActivityInactive) : form.getImages().Admin.Activity);
					childActivityNode.setEnabled(false);
				}
				for (int j = 0; j < voLocationServices.get(i).getFunctions().size(); j++)
				{
					childFunctionNode = node.getNodes().add(voLocationServices.get(i).getFunctions().get(j), (voLocationServices.get(i).getFunctions().get(j).getServiceFunctionIsNotNull() ? voLocationServices.get(i).getFunctions().get(j).getServiceFunction().getFunction().getText() : ""));
					childFunctionNode.setExpandedImage(voLocationServices.get(i).getFunctions().get(j).getIsActiveIsNotNull() ? (voLocationServices.get(i).getFunctions().get(j).getIsActive().booleanValue() ? form.getImages().Admin.Activity : form.getImages().Admin.ActivityInactive) : form.getImages().Admin.Activity);
					childFunctionNode.setCollapsedImage(voLocationServices.get(i).getFunctions().get(j).getIsActiveIsNotNull() ? (voLocationServices.get(i).getFunctions().get(j).getIsActive().booleanValue() ? form.getImages().Admin.Activity : form.getImages().Admin.ActivityInactive) : form.getImages().Admin.Activity);
					childFunctionNode.setSelectedImage(voLocationServices.get(i).getFunctions().get(j).getIsActiveIsNotNull() ? (voLocationServices.get(i).getFunctions().get(j).getIsActive().booleanValue() ? form.getImages().Admin.Activity : form.getImages().Admin.ActivityInactive) : form.getImages().Admin.Activity);
					childFunctionNode.setEnabled(false);

				}
				node.sortChildrenByText();
				if (null != childActivityNode)
					childActivityNode.setExpanded(true);
				if (null != childFunctionNode)
					childFunctionNode.setExpanded(true);
			}
		}

		node.setExpanded(true);
		form.lyr1().Details().htmDetails().setHTML(getDetailsAsTextile(node.getParent()));
	}

	private void clearServiceNodes(TreeNode node)
	{
		TreeNodeCollection coll = node.getNodes();
		List<TreeNode> removes = new ArrayList<TreeNode>();
		for (int i = 0; coll != null && i < coll.size(); i++)
		{
			TreeNode childNode = coll.get(i);
			if (!(childNode.getValue() instanceof LocationRefVo))
				removes.add(childNode);
		}
		for (int i = 0; i < removes.size(); i++)
		{
			coll.remove(removes.get(i));
		}
	}

	public void clearInstanceControls()
	{
		// clear search
		form.lyr1().Edit().txtServiceName().setValue(null);
		form.lyr1().Edit().grdService().getRows().clear();
		// clear contact
		form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtForename().setValue(null);
		form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtMiddleName().setValue(null);
		form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtSurname().setValue(null);
		form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtContactNumber().setValue(null);
		form.lyr1().Edit().lyrLocationDetail().tabPageContact().cmbTitle().setValue(null);
		form.lyr1().Edit().lyrLocationDetail().tabPageContact().qmbPrinter().clear();
		// clear functions
		form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().clear();
		// clear activities
		form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRows().clear();

	}

	private boolean checkServiceExists(ServiceShortVo voServiceShort, LocationServiceVo voLocationService)
	{
		ServiceVo voService = new ServiceVo();
		voService.setID_Service(voServiceShort.getID_Service());
		voLocationService.setService(voService);
		return (domain.listLocationService(voLocationService).size() > 0);
	}

	private boolean populateDataFromScreen()
	{
		// check a service is selected
		if (form.lyr1().Edit().grdService().getValue() == null)
		{
			engine.showMessage("Please search and select a service from the Service List before saving, or Cancel.");
			return false;
		}

		// gather locationservice info
		LocationServiceVo voLocService = null;

		if (form.getLocalContext().getSelectedRecordLocationServiceIsNotNull())
		{
			voLocService = form.getLocalContext().getSelectedRecordLocationService();
		}
		else
		{
			voLocService = new LocationServiceVo();

			// Location
			LocShortVo voLocation = null;
			if (voLocService.getLocationIsNotNull())
				voLocation = voLocService.getLocation();
			else
				voLocation = form.getLocalContext().getSelectedRecordLocation();
			voLocService.setLocation(voLocation);

			if (checkServiceExists(form.lyr1().Edit().grdService().getValue(), voLocService))
			{
				engine.showMessage("Service already added to " + (voLocService.getLocationIsNotNull() ? voLocService.getLocation().getName() : ""));
				return false;
			}
		}

		// service
		// get full serviceVo here
		ServiceVo voService = domain.getService(form.getLocalContext().getSelectedRecordServiceId());
		voLocService.setService(voService);

		// contact
		ServiceContactVo voServiceCon = null;
		if (voLocService.getContactIsNotNull())
		{
			voServiceCon = voLocService.getContact();
		}
		else
		{
			voServiceCon = new ServiceContactVo();
		}

		voServiceCon.setContactNumber(form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtContactNumber().getValue());

		// person name
		PersonName name = null;
		if ((voLocService.getContactIsNotNull()) && (voLocService.getContact().getNameIsNotNull()))
			name = voLocService.getContact().getName();
		else
			name = new PersonName();

		name.setForename(form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtForename().getValue());
		name.setMiddleName(form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtMiddleName().getValue());
		name.setSurname(form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtSurname().getValue());
		name.setTitle(form.lyr1().Edit().lyrLocationDetail().tabPageContact().cmbTitle().getValue());
		voServiceCon.setName(name);

		voLocService.setContact(voServiceCon);

		voLocService.setIsActive(Boolean.valueOf(form.lyr1().Edit().grdService().getSelectedRow().getActive()));

		voLocService.setActivities(populateDataFromScreenActivity(voLocService));

		// functions
		voLocService.setFunctions(populateDataFromScreenFunctions());

		voLocService.setDefaultPrinter(form.lyr1().Edit().lyrLocationDetail().tabPageContact().qmbPrinter().getValue());

		form.getLocalContext().setSelectedRecord(voLocService);
		form.getLocalContext().setSelectedRecordLocationService(voLocService);

		return true;
	}

	private LocationServiceActivityVoCollection populateDataFromScreenActivity(LocationServiceVo voLocSer)
	{
		LocationServiceActivityVoCollection voColl = null;

		if (form.getLocalContext().getSelectedRecordLocationServiceIsNotNull() && form.getLocalContext().getSelectedRecordLocationService().getActivitiesIsNotNull())
			voColl = form.getLocalContext().getSelectedRecordLocationService().getActivities();
		else
			voColl = new LocationServiceActivityVoCollection();

		grdActivityRow row;
		for (int i = 0; i < form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRows().size(); i++)
		{
			row = form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRows().get(i);
			int j = inActivityList(row, voColl);
			boolean bCopyPricings = true;
			if ((row.getSelect()) && (j == -1))
			{
				LocationServiceActivityVo voLocServiceAct = new LocationServiceActivityVo();
				voLocServiceAct.setServiceActivity(row.getValue());
				voLocServiceAct.setIsActive(Boolean.valueOf(row.getActive()));
				voLocServiceAct.setLocationService(voLocSer);

				voColl.add(voLocServiceAct);
			}
			else if ((row.getSelect()) && (j >= 0))
			{
				// update active flag
				voColl.get(j).setIsActive(Boolean.valueOf(row.getActive()));
			}
			else if ((!row.getSelect()) && (j >= 0) && (j < (voColl.size())))
			{
				voColl.remove(j);
				bCopyPricings = false;
			}

			// Need to find this Activity / Proc on the pricing tab
			// Then get all its pricings and setActivityContextPricings()
			if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().isVisible() && bCopyPricings)
			{
				for (int l = 0; l < form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getNodes().size(); l++)
				{
					TreeNode rootNode = form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getNodes().get(l);
					LocationServiceActivityVo lsaVo = (LocationServiceActivityVo) rootNode.getValue();
					for (int m = 0; voColl != null && m < voColl.size(); m++)
					{
						if (voColl.get(m).getID_LocationServiceActivity().equals(lsaVo.getID_LocationServiceActivity()))
							voColl.get(m).setActivityContextPricings(lsaVo.getActivityContextPricings());
					}
				}
			}
		}

		return voColl;
	}

	private LocationServiceFunctionsVoCollection populateDataFromScreenFunctions()
	{

		LocationServiceFunctionsVoCollection voColl = null;

		if (form.getLocalContext().getSelectedRecordLocationServiceIsNotNull() && form.getLocalContext().getSelectedRecordLocationService().getFunctionsIsNotNull())
			voColl = form.getLocalContext().getSelectedRecordLocationService().getFunctions();
		else
			voColl = new LocationServiceFunctionsVoCollection();

		grdServiceFunctionRow row;
		for (int i = 0; i < form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().size(); i++)
		{
			row = form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().get(i);
			int j = inFunctionList(row.getValue(), voColl);
			if ((row.getSelect()) && (j == -1))
			{
				LocationServiceFunctionsVo voLocServiceFunc = new LocationServiceFunctionsVo();
				voLocServiceFunc.setServiceFunction(row.getValue());
				voLocServiceFunc.setIsActive(Boolean.valueOf(row.getActive()));
				voColl.add(voLocServiceFunc);
			}
			else if ((!row.getSelect()) && (j >= 0) && (j < (voColl.size())))
			{
				voColl.remove(j);
			}

		}

		return voColl;
	}

	private int inFunctionList(ServiceFunctionVo value, LocationServiceFunctionsVoCollection voVollFunc)
	{
		if (voVollFunc != null && voVollFunc.size() > 0)
		{
			for (int i = 0; i < voVollFunc.size(); i++)
			{
				if (voVollFunc.get(i).getServiceFunction().equals(value))
					return i;
			}
		}
		return -1;
	}

	private int inActivityList(grdActivityRow row, LocationServiceActivityVoCollection voVollAct)
	{
		if (voVollAct != null && voVollAct.size() > 0)
		{
			for (int i = 0; i < voVollAct.size(); i++)
			{
				if (voVollAct.get(i).getServiceActivity().equals(row.getValue()))
				{
					return i;
				}

			}
		}
		return -1;
	}

	private void populateScreenFromData()
	{
		// clear out instance data for next edit
		clearInstanceControls();
	}

	private String getDetailsAsTextile(TreeNode node)
	{
		StringBuilder out = new StringBuilder(5000);
		out = getFormattedNodeDetails(node, out);
		return new TextileString(out.toString()).toString();
	}

	private StringBuilder getFormattedNodeDetails(TreeNode node, StringBuilder out)
	{
		if (node.getValue() instanceof LocShortVo)
		{
			LocShortVo vo = (LocShortVo) node.getValue();
			out = formatLocationDetails(out, vo);
			if (node.isExpanded())
			{
				TreeNodeCollection treeNodeColl = node.getNodes();
				for (int i = 0; i < treeNodeColl.size(); i++)
				{
					out = getFormattedNodeDetails(treeNodeColl.get(i), out);
				}
			}
		}
		else if (node.getValue() instanceof LocationServiceVo)
		{
			out = formatServiceDetails(out, (LocationServiceVo) node.getValue());
		}
		return out;
	}

	private StringBuilder formatLocationDetails(StringBuilder out, LocShortVo vo)
	{
		// Location
		out.append(makeTextile("<CENTER>*Location/" + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(true) + " Detail*</CENTER>", "SlateBlue", "tahoma", "14"));
		out.append(makeNewLine(1));
		out.append(makeTextile("*Location: *", "DarkRed", "tahoma", "12"));
		out.append(makeTextile((vo.getNameIsNotNull() ? vo.getName() : ""), "black", "tahoma", "12"));
		out.append(makeNewLine(1));

		if (vo.getAddressIsNotNull())
		{
			out.append(makeNewLine(1));
			out.append(makeTextile("*Address: *", "DarkRed", "tahoma", "12"));
			out.append(makeTextile((vo.getAddressIsNotNull() ? vo.getAddress().toDisplayStringForMultiLineTextBox() : ""), "black", "tahoma", "12"));
			out.append(makeNewLine(1));
		}
		return out;
	}

	private StringBuilder formatServiceDetails(StringBuilder out, LocationServiceVo vo)
	{
		out.append(makeTextile("<CENTER>*______________________________________________________*</CENTER>", "SlateBlue", "tahoma", "12"));
		// Service
		out.append(makeNewLine(2));
		out.append(makeTextile("<CENTER>*" + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(true) + ": *", "DarkRed", "tahoma", "12"));
		if (vo.getServiceIsNotNull())
		{
			out.append(makeTextile((vo.getService().getServiceNameIsNotNull() ? vo.getService().getServiceName() : ""), (vo.getIsActiveIsNotNull() ? (vo.getIsActive().booleanValue() ? "black" : "red") : "black"), "tahoma", "12"));
			out.append("</CENTER>");
			out.append(makeNewLine(1));
			out.append(makeTextile("*Description: *", "SlateBlue", "tahoma", "12"));
			out.append(makeTextile((vo.getService().getServiceDescriptionIsNotNull() ? vo.getService().getServiceDescription() : ""), "black", "tahoma", "12"));
			out.append(makeNewLine(1));
		}
		else
			out.append("</CENTER>");
		// Contact
		if (vo.getContactIsNotNull())
		{
			out.append(makeNewLine(1));
			out.append(makeTextile("*Contact: *", "SlateBlue", "tahoma", "12"));
			out.append(makeTextile(vo.getContactIsNotNull() ? vo.getContact().toString() : "", "black", "tahoma", "12"));

		}
		// WDEV-2895
		if (vo.getDefaultPrinterIsNotNull())
		{
			out.append(makeNewLine(2));
			out.append(makeTextile("*Default Printer: *", "SlateBlue", "tahoma", "12"));
			out.append(makeTextile(vo.getDefaultPrinterIsNotNull() ? vo.getDefaultPrinter() : "", "black", "tahoma", "12"));
			out.append(makeNewLine(2));
		}

		// Activity
		if (vo.getActivitiesIsNotNull() && vo.getActivities().size() > 0)
		{
			out.append(makeTextile("*" + ConfigFlag.UI.DISPLAY_NAME_ACTIVITY.getValue(true) + "*", "SlateBlue", "tahoma", "12"));
			out.append(makeNewLine(2));
			vo.getActivities().sort();
			for (int j = 0; j < vo.getActivities().size(); j++)
			{
				if (vo.getActivitiesIsNotNull())
				{
					out.append(makeTextile("<li>   " + (vo.getActivities().get(j).getServiceActivity().getActivityIsNotNull() ? vo.getActivities().get(j).getServiceActivity().getActivity().toString() : ""), vo.getActivities().get(j).getIsActiveIsNotNull() ? (vo.getActivities().get(j).getIsActive().booleanValue() ? "black" : "red") : "black", "tahoma", "12"));
					// WDEV-2590
					if (vo.getActivities().get(j).getServiceActivityIsNotNull() && vo.getActivities().get(j).getServiceActivity().getTaxonomyMapIsNotNull())
					{
						for (int k = 0; k < vo.getActivities().get(j).getServiceActivity().getTaxonomyMap().size(); k++)
						{
							if (vo.getActivities().get(j).getServiceActivity().getTaxonomyMap().size() > 1)
								out.append("<br>");

							out.append(makeTextile("* Taxonomy: *", "SlateBlue", "tahoma", "12"));
							out.append(makeTextile((vo.getActivities().get(j).getServiceActivity().getTaxonomyMap().get(k).getTaxonomyNameIsNotNull() ? vo.getActivities().get(j).getServiceActivity().getTaxonomyMap().get(k).getTaxonomyName().toString() : ""), "black", "tahoma", "12"));
							out.append(makeTextile("* Code: *", "SlateBlue", "tahoma", "12"));
							out.append(makeTextile((vo.getActivities().get(j).getServiceActivity().getTaxonomyMap().get(k).getTaxonomyCodeIsNotNull() ? vo.getActivities().get(j).getServiceActivity().getTaxonomyMap().get(k).getTaxonomyCode() : ""), "black", "tahoma", "12"));
						}
					}
					out.append("</li>");

					if (ConfigFlag.GEN.SHOW_PROCEDURE_PRICING.getValue())
					{
						// Pricings
						StringBuffer sb = new StringBuffer();
						for (int m = 0; m < vo.getActivities().get(j).getActivityContextPricings().size(); m++)
						{
							if ((vo.getActivities().get(j).getActivityContextPricings().get(m).getIsActiveIsNotNull() && vo.getActivities().get(j).getActivityContextPricings().get(m).getIsActive() && vo.getActivities().get(j).getActivityContextPricings().get(m).getEffectiveTo() == null && form.lyr1().Details().chkHTMLActiveOnly().getValue()) || form.lyr1().Details().chkHTMLActiveOnly().getValue() == false)
							{
								sb.append(makeTextileRightAlign(createPricingString(vo.getActivities().get(j).getActivityContextPricings().get(m)).toString(), "black", "tahoma", "10"));
								sb.append("<br>");
							}
						}
						out.append(sb);
					}

					out.append("<BR>");
				}
			}
		}

		// //Functions
		if (vo.getFunctionsIsNotNull() && vo.getFunctions().size() > 0)
		{
			out.append(makeNewLine(1));
			out.append(makeTextile("*" + ConfigFlag.UI.DISPLAY_NAME_FUNCTION.getValue(true) + "*", "SlateBlue", "tahoma", "12"));
			out.append(makeNewLine(2));
			for (int j = 0; j < vo.getFunctions().size(); j++)
			{
				if (vo.getFunctions().get(j).getServiceFunctionIsNotNull())
					out.append(makeTextile("<li>   " + (vo.getFunctions().get(j).getServiceFunction().getFunctionIsNotNull() ? vo.getFunctions().get(j).getServiceFunction().getFunction().getText() : "") + "</li>", vo.getFunctions().get(j).getIsActiveIsNotNull() ? (vo.getFunctions().get(j).getIsActive().booleanValue() ? "black" : "red") : "black", "tahoma", "12"));
			}
			out.append(makeNewLine(1));
		}
		return out;
	}

	private String formatLocationServicevoTooltip(LocationServiceMidVo voLocService)
	{
		StringBuilder out = new StringBuilder(1000);
		out.append(makeTextile("*" + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(true) + ": *", "SlateBlue", "tahoma", "12"));
		out.append(makeTextile((voLocService.getService().getServiceNameIsNotNull() ? voLocService.getService().getServiceName() : ""), (voLocService.getIsActiveIsNotNull() ? (voLocService.getIsActive().booleanValue() ? "black" : "red") : "black"), "tahoma", "12"));
		out.append(makeNewLine(1));
		out.append(makeTextile("*Description: *", "SlateBlue", "tahoma", "12"));
		out.append(makeTextile((voLocService.getService().getServiceDescriptionIsNotNull() ? voLocService.getService().getServiceDescription() : ""), "black", "tahoma", "12"));
		out.append(makeNewLine(1));
		if (voLocService.getContactIsNotNull())
		{
			out.append(makeNewLine(1));
			out.append(makeTextile("*Contact Details: *", "SlateBlue", "tahoma", "12"));
			out.append(makeTextile((voLocService.getContact().getNameIsNotNull() ? voLocService.getContact().getName().toString() : ""), "black", "tahoma", "12"));
			if (voLocService.getContact().getContactNumberIsNotNull())
			{
				out.append(makeNewLine(1));
				out.append(makeTextile("*Contact Number: *", "SlateBlue", "tahoma", "12"));
				out.append(makeTextile(voLocService.getContact().getContactNumber(), "black", "tahoma", "12"));
			}
		}

		return new TextileString(out.toString()).toString();
	}

	private void removeService()
	{
		if (form.getLocalContext().getSelectedRecordLocationServiceIsNotNull())
		{
			if ((form.treHeader().getSelectedNode().getValue() != null) && (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo))
			{
				LocationServiceVo voLocSer = (LocationServiceVo) form.treHeader().getSelectedNode().getValue();
				try
				{
					try
					{
						domain.removeLocationService(voLocSer);
						if (form.treHeader().getSelectedNode().getParent() != null)
						{
							try
							{
								onTreHeaderTreeViewSelectionChanged(form.treHeader().getSelectedNode().getParent());
							}
							catch (PresentationLogicException pe)
							{
								engine.showMessage(pe.getMessage());
							}
						}
					}
					catch (ForeignKeyViolationException fe)
					{
						if (form.getLocalContext().getSelectedRecordLocationService().getServiceIsNotNull())
							engine.showMessage("Cannot remove " + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue() + " : " + (form.getLocalContext().getSelectedRecordLocationService().getService().getServiceNameIsNotNull() ? form.getLocalContext().getSelectedRecordLocationService().getService().getServiceName() : "") + ". It is already referenced.");
						else
							engine.showMessage("Cannot remove this" + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue() + " as it is already referenced.");
					}
				}
				catch (StaleObjectException se)
				{
					engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
					populateLocationServices(form.treHeader().getSelectedNode().getParent(), (LocShortVo) form.treHeader().getSelectedNode().getParent().getValue());
				}
			}
		}
	}

	private void removeActivity()
	{
		if (form.getLocalContext().getSelectedRecordLocSerActIsNotNull())
		{
			if ((form.treHeader().getSelectedNode().getParent().getValue() != null) && (form.treHeader().getSelectedNode().getParent().getValue() instanceof LocationServiceVo))
			{
				LocationServiceVo voLocSer = (LocationServiceVo) form.treHeader().getSelectedNode().getParent().getValue();

				// remove activity
				if (voLocSer.getActivitiesIsNotNull() && form.getLocalContext().getSelectedRecordLocSerActIsNotNull())
				{
					for (int i = 0; i < voLocSer.getActivities().size(); i++)
					{
						if (voLocSer.getActivities().get(i).equals(form.getLocalContext().getSelectedRecordLocSerAct()))
						{
							// found, so remove
							voLocSer.getActivities().remove(i);
							break;
						}

					}
				}
				try
				{
					try
					{
						try
						{
							form.getLocalContext().setSelectedRecordLocationService(domain.saveLocationService(voLocSer));
							if (form.treHeader().getSelectedNode().getParent().getParent() != null)
							{
								try
								{
									onTreHeaderTreeViewSelectionChanged(form.treHeader().getSelectedNode());
									form.treHeader().setValue(null);
								}
								catch (PresentationLogicException pe)
								{
									engine.showMessage(pe.getMessage());
								}
							}
						}
						catch (UniqueKeyViolationException ue)
						{
							engine.showMessage(ue.getMessage());
						}
					}
					catch (ForeignKeyViolationException fe)
					{
						engine.showMessage(fe.getMessage());
					}
				}
				catch (StaleObjectException se)
				{
					engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
					populateLocationServices(form.treHeader().getSelectedNode().getParent(), (LocShortVo) form.treHeader().getSelectedNode().getParent().getValue());
				}
			}
		}
	}

	private void removeFunction()
	{
		if (form.getLocalContext().getSelectedRecordLocSerFuncIsNotNull())
		{
			if ((form.treHeader().getSelectedNode().getParent().getValue() != null) && (form.treHeader().getSelectedNode().getParent().getValue() instanceof LocationServiceVo))
			{
				LocationServiceVo voLocSer = (LocationServiceVo) form.treHeader().getSelectedNode().getParent().getValue();

				// remove function
				if (voLocSer.getFunctionsIsNotNull() && form.getLocalContext().getSelectedRecordLocSerFuncIsNotNull())
				{
					for (int i = 0; i < voLocSer.getFunctions().size(); i++)
					{
						if (voLocSer.getFunctions().get(i).equals(form.getLocalContext().getSelectedRecordLocSerFunc()))
						{
							// found, so remove
							voLocSer.getFunctions().remove(i);
							break;
						}

					}
				}
				try
				{
					try
					{
						try
						{
							form.getLocalContext().setSelectedRecordLocationService(domain.saveLocationService(voLocSer));
							if (form.treHeader().getSelectedNode().getParent().getParent() != null)
							{
								try
								{
									onTreHeaderTreeViewSelectionChanged(form.treHeader().getSelectedNode());
									form.treHeader().setValue(null);
								}
								catch (PresentationLogicException pe)
								{
									engine.showMessage(pe.getMessage());
								}
							}
						}
						catch (UniqueKeyViolationException ue)
						{
							engine.showMessage(ue.getMessage());
						}
					}
					catch (ForeignKeyViolationException fe)
					{
						engine.showMessage(fe.getMessage());
					}
				}
				catch (StaleObjectException se)
				{
					engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
					populateLocationServices(form.treHeader().getSelectedNode().getParent(), (LocShortVo) form.treHeader().getSelectedNode().getParent().getValue());
				}
			}
		}
	}

	private void updateInstance()
	{
		clearInstanceControls();

		if (form.treHeader().getSelectedNode() != null && form.treHeader().getSelectedNode().getValue() != null && form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo)
		{
			LocationServiceVo voLocService = (LocationServiceVo) form.treHeader().getSelectedNode().getValue();
			populateScreenFromDataContact(voLocService);
			if (voLocService.getServiceIsNotNull() && voLocService.getService().getServiceNameIsNotNull())
			{
				form.lyr1().Edit().txtServiceName().setValue(voLocService.getService().getServiceName());

				grdServiceRow row = null;
				row = form.lyr1().Edit().grdService().getRows().newRow();
				row.setValue(voLocService.getService());
				row.setService(voLocService.getService().getServiceName());
				row.setActive(voLocService.getIsActiveIsNotNull() ? voLocService.getIsActive().booleanValue() : true); // default
																														// to
																														// true
				row.setTooltip(formatServicevoTooltip(voLocService.getService()));
				row.setSelect(true);

				form.lyr1().Edit().grdService().setValue(voLocService.getService());

				if (voLocService.getService().getID_ServiceIsNotNull())
				{
					populateScreenDataServiceFunction(voLocService.getService().getID_Service());
					populateScreenDataServiceActivity(voLocService.getService().getID_Service());
				}

				if (ConfigFlag.GEN.SHOW_PROCEDURE_PRICING.getValue())
					form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(true);
				else
					form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(false);

				form.setMode(FormMode.EDIT);

				if (voLocService.getLocationIsNotNull() && voLocService.getLocation().getTreatingHospIsNotNull() && !voLocService.getLocation().getTreatingHosp())
					form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(false);

				// override for editing service grid.
				// allow edit for active flag if no active Activities and
				// Functions
				if (activeActivities())
				{
					form.lyr1().Edit().grdService().setEnabled(true);
					row.setReadOnly(false);
					row.setActiveReadOnly(true);
					row.setSelectReadOnly(true);
					row.setTooltip("Active " + ConfigFlag.UI.DISPLAY_NAME_ACTIVITY.getValue(true) + " associated with this " + voLocService.toString());
				}
				else if (activeFunctions())
				{
					form.lyr1().Edit().grdService().setEnabled(true);
					row.setReadOnly(false);
					row.setActiveReadOnly(true);
					row.setSelectReadOnly(true);
					row.setTooltip("Active " + ConfigFlag.UI.DISPLAY_NAME_FUNCTION.getValue(true) + " associated with this " + voLocService.toString());
				}
				else
				{
					form.lyr1().Edit().grdService().setEnabled(true);
					row.setReadOnly(false);
					row.setActiveReadOnly(false);
					row.setSelectReadOnly(true);
				}
			}
		}
		else
			form.setMode(FormMode.EDIT);

		// override for updating a service
		form.lyr1().Edit().txtServiceName().setEnabled(false);
		form.lyr1().Edit().btnSearch().setEnabled(false);
		form.lyr1().Edit().pnlServiceList().setEnabled(false);

		form.getContextMenus().getLocationServiceActivityGridUPDATE_PRICINGItem().setVisible(false);
		form.getContextMenus().getLocationServiceActivityGridNEW_PRICEItem().setVisible(false);

		form.lyr1().showEdit();

	}

	private boolean activeActivities()
	{
		if (form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRows() != null)
		{
			for (int i = 0; i < form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRows().size(); i++)
			{
				if (form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRows().get(i).getActive())
					return true;
			}
		}
		return false;
	}

	private boolean activeFunctions()
	{
		if (form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows() != null)
		{
			for (int i = 0; i < form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().size(); i++)
			{
				if (form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().get(i).getActive())
					return true;
			}
		}
		return false;
	}

	private void populateScreenFromDataContact(LocationServiceVo voLocService)
	{
		if (voLocService.getContactIsNotNull())
		{
			if (voLocService.getContact().getNameIsNotNull())
			{
				form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtForename().setValue(voLocService.getContact().getName().getForenameIsNotNull() ? voLocService.getContact().getName().getForename() : null);
				form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtSurname().setValue(voLocService.getContact().getName().getSurnameIsNotNull() ? voLocService.getContact().getName().getSurname() : null);
				form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtMiddleName().setValue(voLocService.getContact().getName().getMiddleNameIsNotNull() ? voLocService.getContact().getName().getMiddleName() : null);
				form.lyr1().Edit().lyrLocationDetail().tabPageContact().cmbTitle().setValue(voLocService.getContact().getName().getTitleIsNotNull() ? voLocService.getContact().getName().getTitle() : null);
			}
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().txtContactNumber().setValue(voLocService.getContact().getContactNumberIsNotNull() ? voLocService.getContact().getContactNumber() : null);
		}
		// WDEV-2895
		if (voLocService.getDefaultPrinter() != null)
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().qmbPrinter().newRow(voLocService.getDefaultPrinter(), voLocService.getDefaultPrinter());
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().qmbPrinter().setValue(voLocService.getDefaultPrinter());
		}
	}

	public void newInstance()
	{
		clearInstanceControls();
		form.getContextMenus().getGenericGridAddItem().setVisible(false);
		form.getLocalContext().setSelectedRecordLocationService(null); // new
																		// instance
		form.lyr1().showEdit();
		form.lyr1().Edit().txtServiceName().setFocus();
		form.setMode(FormMode.EDIT);

		if (ConfigFlag.GEN.SHOW_PROCEDURE_PRICING.getValue())
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(true);
		else
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(false);
	}

	private String makeTextile(String stringin, String colour, String font, String fontsize)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<span style=\"color:");
		sb.append(colour);
		sb.append(";font-family:");
		sb.append(font);
		sb.append(";font-size:");
		sb.append(fontsize);
		sb.append(";\">");
		sb.append(stringin);
		sb.append("</span>");
		return sb.toString();
	}

	private String makeTextileRightAlign(String stringin, String colour, String font, String fontsize)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<span style=\"color:");
		sb.append(colour);
		sb.append(";font-family:");
		sb.append(font);
		sb.append(";align:centre");
		sb.append(";font-size:");
		sb.append(fontsize);
		sb.append(";\">");
		sb.append(stringin);
		sb.append("</span>");
		return sb.toString();
	}

	private String makeNewLine(int numLines)
	{
		StringBuffer ret = new StringBuffer();
		ret.append("");

		for (int i = 0; i < numLines; i++)
		{
			ret.append("<br>");
		}
		return ret.toString();
	}

	private void setScreenViewMode()
	{
		form.setMode(FormMode.VIEW);

		if (ConfigFlag.GEN.SHOW_PROCEDURE_PRICING.getValue())
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(true);
		else
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(false);

		form.lyr1().showDetails();

		form.treHeader().setValue(null);
		form.getContextMenus().hideAllGenericGridMenuItems();//wdev-7076
		if (form.treHeader().getSelectedNode() != null)
			populateScreenFromData();
	}

	public boolean save()
	{

		if (populateDataFromScreen())
		{
			LocationServiceVo voLocService = form.getLocalContext().getSelectedRecordLocationService();

			try
			{
				String[] arrErrors;
				arrErrors = voLocService.validate(validateRules(voLocService));
				if (arrErrors != null)
				{
					engine.showErrors("Validation Errors", arrErrors);
					return false;
				}

				try
				{
					try
					{
						form.getLocalContext().setSelectedRecordLocationService(domain.saveLocationService(voLocService));
						// refresh
						Boolean bTreatingHosp = false;
						if (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo)
						{
							form.treHeader().getSelectedNode().setValue(form.getLocalContext().getSelectedRecordLocationService());
							LocationServiceVoCollection lsVoColl = new LocationServiceVoCollection();
							lsVoColl.add(form.getLocalContext().getSelectedRecordLocationService());
							populateServiceActivities(form.treHeader().getSelectedNode(), lsVoColl);

						}
						else if (form.treHeader().getSelectedNode().getValue() instanceof LocShortVo)
						{
							bTreatingHosp = ((LocShortVo) form.treHeader().getSelectedNode().getValue()).getTreatingHosp();
							populateLocationServices(form.treHeader().getSelectedNode(), (LocShortVo) form.treHeader().getSelectedNode().getValue());
						}
						managePricingMenuOptions(bTreatingHosp);
					}
					catch (UniqueKeyViolationException ue)
					{
						engine.showMessage(ims.configuration.gen.ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue() + " : " + ue.getMessage());
						return false;
					}
				}
				catch (ForeignKeyViolationException fe)
				{
					engine.showMessage(fe.getMessage());
					open();
					return false;
				}

			}
			catch (StaleObjectException e)
			{
				engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
				open();
				return false;
			}
			setScreenViewMode();
			return true;
		}

		return false;
	}

	private String[] validateRules(LocationServiceVo voLocService) {
		
		if (voLocService == null || !voLocService.getServiceIsNotNull())
			return null;
		if (!voLocService.getService().getIsActiveIsNotNull() || !voLocService.getService().getIsActive().booleanValue())
			return new String[]{"Service "+voLocService.getService().getServiceName()+" is not active"};
		return null;
	}

	private void searchService(String searchText)
	{
		if (form.getLocalContext().getSelectedRecordLocation().getType().equals(LocationType.PATHOLOGYLABORATORY))
			populateListControl(domain.listServiceExcludeCategory(searchText, ServiceCategory.RADIOLOGY_MODALITY));
		else if (form.getLocalContext().getSelectedRecordLocation().getType().equals(LocationType.CLINICALIMAGINGDEPARTMENT))
			populateListControl(domain.listServiceExcludeCategory(searchText, ServiceCategory.PATHOLOGY_DISCIPLINE));
		else
			populateListControl(domain.listServices(searchText));
	}

	/**
	 * populates the screen from data from specified Vo Collection
	 * 
	 * @param ServiceVoCollection
	 * @return void
	 */
	public void populateListControl(ServiceShortVoCollection voColl)
	{
		form.lyr1().Edit().grdService().getRows().clear();

		if (voColl != null)
		{
			grdServiceRow row;
			for (int i = 0; i < voColl.size(); i++)
			{
				ServiceShortVo voService = voColl.get(i);
				// check service compatibility to Location

				if (voService.getServiceNameIsNotNull())
				{
					row = form.lyr1().Edit().grdService().getRows().newRow();
					row.setValue(voColl.get(i));
					row.setService(voColl.get(i).getServiceName());
					row.setTooltip(formatServicevoTooltip(voService));
				}
			}
		}
	}

	private String formatServicevoTooltip(ServiceShortVo voService)
	{
		String out = makeTextile("*Service *", "SlateBlue", "tahoma", "14");
		out += makeTextile("*Name: *", "SlateBlue", "tahoma", "12");
		out += makeTextile(voService.getServiceName(), "black", "tahoma", "12");
		out += makeNewLine(1);
		out += makeTextile("*Description:*", "SlateBlue", "tahoma", "12");
		out += makeTextile(voService.getServiceDescriptionIsNotNull() ? voService.getServiceDescription() : "", "black", "tahoma", "12");
		out += makeNewLine(1);
		return new TextileString(out).toString();
	}

	private void populateScreenDataServiceActivity(Integer voServiceId)
	{
		form.getLocalContext().setSelectedRecordServiceId(voServiceId);

		ServiceActivityVoCollection voColl = domain.listServiceActivity(voServiceId);

		// location service activities
		form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRows().clear();
		if ((voColl != null) && (voColl.size() > 0))
		{
			// load service activities
			grdActivityRow row;

			for (int i = 0; i < voColl.size(); i++)
			{
				if (voColl.get(i).getIsActiveIsNotNull() && voColl.get(i).getIsActive().booleanValue())
				{
					row = form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRows().newRow();
					row.setserviceActivityId(voColl.get(i).getActivity().getID_Activity());
					row.setActivity(voColl.get(i).getActivity().toString());
					row.setValue(voColl.get(i));
					row.setSelect(false);

					if (voColl.get(i).getActivityIsNotNull() && voColl.get(i).getTaxonomyMapIsNotNull())
						row.setTooltip(setToolTipTaxonomy(voColl.get(i), voColl.get(i).getActivity()));
				}
			}

			// show linked location service activities
			if (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo)
			{
				form.lyr1().Edit().txtServiceName().setEnabled(false);
				form.lyr1().Edit().btnSearch().setEnabled(false);
				// this is an update of with a single servicefunction
				form.getLocalContext().setSelectedRecordLocationService((LocationServiceVo) form.treHeader().getSelectedNode().getValue());
			}
			else if (form.treHeader().getSelectedNode().getValue() instanceof LocShortVo)
			{
				// this is an update/addition of services
				if (form.treHeader().getSelectedNode().getNodes().size() > 0)
				{
					form.getLocalContext().setSelectedRecordLocationService(null);
					// find locationservice
					for (int j = 0; j < form.treHeader().getSelectedNode().getNodes().size(); j++)
					{
						if (form.treHeader().getSelectedNode().getNodes().get(j).getValue() instanceof LocationServiceVo)
						{
							LocationServiceVo voLocServ = (LocationServiceVo) form.treHeader().getSelectedNode().getNodes().get(j).getValue();
							if (voLocServ.getService().equals(form.lyr1().Edit().grdService().getValue()))
							{
								// found
								form.getLocalContext().setSelectedRecordLocationService(voLocServ);
								break;
							}
						}
					}
				}
			}

			if (form.getLocalContext().getSelectedRecordLocationService() != null)
			{
				// select linked location service actions
				if (form.getLocalContext().getSelectedRecordLocationService().getActivitiesIsNotNull())
				{
					for (int i = 0; i < form.getLocalContext().getSelectedRecordLocationService().getActivities().size(); i++)
					{
						row = form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().getRowByValue(form.getLocalContext().getSelectedRecordLocationService().getActivities().get(i).getServiceActivity());
						if (row != null)
						{
							row.setActive(form.getLocalContext().getSelectedRecordLocationService().getActivities().get(i).getIsActiveIsNotNull() ? form.getLocalContext().getSelectedRecordLocationService().getActivities().get(i).getIsActive().booleanValue() : true); // default
																																																																			// to
																																																																			// true
							row.setSelectBackColor(Color.LightGray);
							row.setSelectReadOnly(true);
							row.setSelect(true);
						}
					}
				}
			}

		}
	}

	private void populateScreenDataServiceFunction(Integer voServiceId)
	{
		form.getLocalContext().setSelectedRecordServiceId(voServiceId);

		ServiceFunctionVoCollection voColl = domain.listServiceFunction(voServiceId);

		// location service functions
		form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().clear();
		if ((voColl != null) && (voColl.size() > 0))
		{
			// load servicefunctions
			grdServiceFunctionRow row;
			for (int i = 0; i < voColl.size(); i++)
			{
				row = form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().newRow();
				row.setservicefunctionId(Integer.valueOf(voColl.get(i).getFunction().getId()));
				row.setFunction(voColl.get(i).getFunction().getText());
				row.setValue(voColl.get(i));
				row.setSelect(false);
			}

			// show linked location service functions
			if (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo)
			{
				form.lyr1().Edit().txtServiceName().setEnabled(false);
				form.lyr1().Edit().btnSearch().setEnabled(false);
				// this is an update of with a single servicefunction
				form.getLocalContext().setSelectedRecordLocationService((LocationServiceVo) form.treHeader().getSelectedNode().getValue());
			}
			else if (form.treHeader().getSelectedNode().getValue() instanceof LocShortVo)
			{
				// this is an update/addition of services
				if (form.treHeader().getSelectedNode().getNodes().size() > 0)
				{
					form.getLocalContext().setSelectedRecordLocationService(null);
					// find locationservice
					for (int j = 0; j < form.treHeader().getSelectedNode().getNodes().size(); j++)
					{
						if (form.treHeader().getSelectedNode().getNodes().get(j).getValue() instanceof LocationServiceVo)
						{
							LocationServiceVo voLocServ = (LocationServiceVo) form.treHeader().getSelectedNode().getNodes().get(j).getValue();
							if (voLocServ.getService().equals(form.lyr1().Edit().grdService().getValue()))
							{
								// found
								form.getLocalContext().setSelectedRecordLocationService(voLocServ);
								break;
							}
						}
					}
				}
			}

			if (form.getLocalContext().getSelectedRecordLocationService() != null)
			{
				// select linked location service functions
				if (form.getLocalContext().getSelectedRecordLocationService().getFunctionsIsNotNull())
				{
					for (int i = 0; i < form.getLocalContext().getSelectedRecordLocationService().getFunctions().size(); i++)
					{
						row = getFunctionrow(form.getLocalContext().getSelectedRecordLocationService().getFunctions().get(i).getServiceFunction().getFunction().getId());
						if (row != null)
							row.setSelect(true);
					}
				}
			}

		}
	}

	private grdServiceFunctionRow getFunctionrow(int serviceFunctionId)
	{
		for (int i = 0; i < form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().size(); i++)
		{
			if (form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().get(i).getservicefunctionId().intValue() == serviceFunctionId)
				return form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().getRows().get(i);
		}
		return null;
	}

	public void updateControlsState()
	{
		if (form.getMode().equals(FormMode.VIEW))
		{
			// form.pnlDetails().setVisible(true);
			form.lyr1().Details().htmDetails().setVisible(true);
			form.lyr1().Edit().pnlServiceList().setVisible(false);
			form.lyr1().Edit().lyrLocationDetail().hide();
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().setVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().setEnabled(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().setReadOnly(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().setVisible(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().setEnabled(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().setReadOnly(true);
			form.lyr1().Edit().grdService().setVisible(false);
			form.lyr1().Edit().txtServiceName().setVisible(false);
			form.lyr1().Edit().btnSearch().setVisible(false);
		}
		else
		{
			// form.pnlDetails().setVisible(false);
			form.lyr1().Details().htmDetails().setVisible(false);
			form.lyr1().Edit().pnlServiceList().setVisible(true);
			showActivityFunctionTabs();
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().setVisible(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().setEnabled(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageFunction().grdServiceFunction().setReadOnly(false);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().setVisible(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().setEnabled(true);
			form.lyr1().Edit().lyrLocationDetail().tabPageActivity().grdActivity().setReadOnly(false);
			form.lyr1().Edit().grdService().setVisible(true);
			form.lyr1().Edit().txtServiceName().setVisible(true);
			form.lyr1().Edit().btnSearch().setVisible(true);
			form.lyr1().Edit().txtServiceName().setEnabled(true);
			form.lyr1().Edit().btnSearch().setEnabled(true);
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().chkActivePricing().setValue(true);
			populateActivitiesTreeFromData();

		}
	}

	protected void onGrdServiceGridCheckBoxClicked(int column, grdServiceRow row, boolean isChecked) throws PresentationLogicException
	{
		// clearout all other selections and set current selection
		for (int i = 0; i < form.lyr1().Edit().grdService().getRows().size(); i++)
		{
			if (!row.getValue().equals(form.lyr1().Edit().grdService().getRows().get(i).getValue()))
			{
				form.lyr1().Edit().grdService().getRows().get(i).setSelect(false);
				form.lyr1().Edit().grdService().getRows().get(i).setActive(false);
			}
			else
				form.lyr1().Edit().grdService().getRows().get(i).setActive(isChecked);
		}

		if (row.getValue() != null)
		{
			ServiceShortVo voShortService = row.getValue();
			form.lyr1().Edit().grdService().setValue(row.getValue());
			if (voShortService.getID_ServiceIsNotNull())
			{
				populateScreenDataServiceFunction(voShortService.getID_Service());
				populateScreenDataServiceActivity(voShortService.getID_Service());
			}
		}

	}

	protected void onFormDialogClosed(FormName formName, DialogResult result) throws PresentationLogicException
	{
		if (formName.equals(form.getForms().Admin.ActivityContextPricingDetails) && result.equals(DialogResult.OK))
		{
			LocationServiceActivityVo lsaVo = form.getLocalContext().getSelectedLocationServiceActivity();
			if (lsaVo == null)
				return;

			if (!NTPFExchequerCodingChecks.validatePricingIsUnique(form.getLocalContext().getSelectedLocationServiceActivity().getActivityContextPricings(), form.getGlobalContext().Admin.getActivityContextPricingDetail()))
				return;

			if (form.getGlobalContext().Admin.getLSAPricingAction().equals(Integer.valueOf(PRICING_NEW)))
			{
				lsaVo.getActivityContextPricings().add(form.getGlobalContext().Admin.getActivityContextPricingDetail());
				populatePricing(form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode(), lsaVo.getActivityContextPricings());

			}
			else if (form.getGlobalContext().Admin.getLSAPricingAction().equals(Integer.valueOf(PRICING_UPDATE)))
			{
				// Replace the object in the collection
				for (int i = 0; i < lsaVo.getActivityContextPricings().size(); i++)
				{
					if (lsaVo.getActivityContextPricings().get(i).equals(form.getGlobalContext().Admin.getActivityContextPricingDetail()))
					{
						lsaVo.getActivityContextPricings().set(i, form.getGlobalContext().Admin.getActivityContextPricingDetail());
						break;
					}
				}
				populatePricing(form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getParent(), lsaVo.getActivityContextPricings());
			}

			form.getLocalContext().setSelectedLocationServiceActivity(lsaVo);

			if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode() != null)
			{
				if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getValue() instanceof ActivityContextPricingDetailsVo)
					form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getParent().setValue(lsaVo);
				else if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getValue() instanceof LocationServiceActivityVo)
					form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().setValue(lsaVo);
			}

			if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode() == null)//wdev-7106
				form.getContextMenus().hideAllLocationServiceActivityGridMenuItems();

			form.lyr1().Edit().lyrLocationDetail().showtabPagePricing();
		}
		if (formName.equals(form.getForms().Admin.ProcedurePriceIncrease) && result.equals(DialogResult.OK))
		{
			populateLocationServices(form.treHeader().getSelectedNode(), (LocShortVo) form.treHeader().getSelectedNode().getValue());
			managePricingMenuOptions(((LocShortVo) form.treHeader().getSelectedNode().getValue()).getTreatingHosp());
		}

		if (formName.equals(form.getForms().Admin.NTPFBatchPriceSelectionDialog) && result.equals(DialogResult.OK))
		{
			engine.open(form.getForms().Admin.ProcedurePriceIncrease);
		}

		if (formName.equals(form.getForms().Core.NewPriceBatchDialog) && result.equals(DialogResult.OK))
		{
			form.getGlobalContext().NTPF.setConfirmPriceIncreases(false);
			engine.open(form.getForms().Admin.ProcedurePriceIncrease, new Object[]{FormMode.EDIT});
		}
	}

	protected void onFormModeChanged()
	{
		updateControlsState();
	}

	protected void onBtnSearchClick() throws PresentationLogicException
	{
		searchService(form.lyr1().Edit().txtServiceName().getValue());
	}

	protected void onTreHeaderTreeViewSelectionChanged(TreeNode node) throws PresentationLogicException
	{
		if (node == null)
			open();
		else
		{
			if (node.getValue() instanceof OrganisationVo)
			{
				form.getLocalContext().setSelectedRecordOrganisation((OrganisationVo) node.getValue());
				form.getLocalContext().setSelectedRecordLocation(null);
				form.getLocalContext().setSelectedRecordLocationService(null);
				form.getLocalContext().setSelectedRecordLocSerFunc(null);
				form.getLocalContext().setSelectedRecordLocSerAct(null);
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
				form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
				form.getContextMenus().getGenericGridViewItem().setVisible(false);
				form.getContextMenus().getGenericGridConfirmItem().setVisible(false);
				form.getContextMenus().getGenericGridReplaceItem().setVisible(false);

				form.lyr1().Details().htmDetails().setHTML("");
			}
			else if (node.getValue() instanceof LocShortVo)
			{
				form.getLocalContext().setSelectedRecordOrganisation(null);
				form.getLocalContext().setSelectedRecordLocation((LocShortVo) node.getValue());
				form.getLocalContext().setSelectedRecordLocationService(null);
				form.getLocalContext().setSelectedRecordLocSerFunc(null);
				form.getLocalContext().setSelectedRecordLocSerAct(null);
				form.getContextMenus().getGenericGridAddItem().setText("Add " + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(true));
				form.getContextMenus().getGenericGridAddItem().setVisible(true);
				form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
				form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
				form.getContextMenus().getGenericGridViewItem().setVisible(false);

				form.getGlobalContext().Admin.setSelectedLocShortVo((LocShortVo) node.getValue());

				populateLocationServices(node, (LocShortVo) node.getValue());

				managePricingMenuOptions(((LocShortVo) node.getValue()).getTreatingHosp());

			}
			else if (node.getValue() instanceof LocationServiceMidVo)
			{
				form.getLocalContext().setSelectedRecordOrganisation(null);
				form.getLocalContext().setSelectedRecordLocation(null);

				LocShortVo locationVo = ((LocationServiceMidVo) node.getValue()).getLocation();
				ServiceVo serviceVo = ((LocationServiceMidVo) node.getValue()).getService();

				LocationServiceVoCollection voLocationService = domain.listServicesForLocation(locationVo, serviceVo);
				populateServiceActivities(node, voLocationService);

				form.getLocalContext().setSelectedRecordLocationService((LocationServiceVo) node.getValue());
				form.getLocalContext().setSelectedRecordLocSerFunc(null);
				form.getLocalContext().setSelectedRecordLocSerAct(null);
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridRemoveItem().setText("Remove " + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(false));
				form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
				form.getContextMenus().getGenericGridUpdateItem().setText("Edit " + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(false));
				form.getContextMenus().getGenericGridUpdateItem().setVisible(true);
				form.getContextMenus().getGenericGridViewItem().setVisible(false);
				form.getContextMenus().getGenericGridConfirmItem().setVisible(false);
				form.getContextMenus().getGenericGridReplaceItem().setVisible(false);

				populateActivitiesTreeFromData();
			}

			else if (node.getValue() instanceof LocationServiceVo)
			{
				form.getLocalContext().setSelectedRecordOrganisation(null);
				form.getLocalContext().setSelectedRecordLocation(null);

				LocShortVo locationVo = ((LocationServiceVo) node.getValue()).getLocation();
				ServiceVo serviceVo = ((LocationServiceVo) node.getValue()).getService();

				LocationServiceVoCollection voLocationService = domain.listServicesForLocation(locationVo, serviceVo);
				populateServiceActivities(node, voLocationService);

				form.getLocalContext().setSelectedRecordLocationService((LocationServiceVo) node.getValue());
				form.getLocalContext().setSelectedRecordLocSerFunc(null);
				form.getLocalContext().setSelectedRecordLocSerAct(null);
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridRemoveItem().setText("Remove " + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(false));
				form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
				form.getContextMenus().getGenericGridUpdateItem().setText("Edit " + ConfigFlag.UI.DISPLAY_NAME_SERVICE.getValue(false));
				form.getContextMenus().getGenericGridUpdateItem().setVisible(true);
				form.getContextMenus().getGenericGridViewItem().setVisible(false);
				form.getContextMenus().getGenericGridConfirmItem().setVisible(false);
				form.getContextMenus().getGenericGridReplaceItem().setVisible(false);

				populateActivitiesTreeFromData();
			}

			else if (node.getValue() instanceof LocationServiceFunctionsVo)
			{
				form.getLocalContext().setSelectedRecordOrganisation(null);
				form.getLocalContext().setSelectedRecordLocation(null);
				form.getLocalContext().setSelectedRecordLocationService(null);
				form.getLocalContext().setSelectedRecordLocSerFunc((LocationServiceFunctionsVo) node.getValue());
				form.getLocalContext().setSelectedRecordLocSerAct(null);
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				form.getContextMenus().getGenericGridRemoveItem().setText("Remove " + ConfigFlag.UI.DISPLAY_NAME_FUNCTION.getValue(false));
				form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
				form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
				form.getContextMenus().getGenericGridViewItem().setVisible(false);
				form.getContextMenus().getGenericGridConfirmItem().setVisible(false);
				form.getContextMenus().getGenericGridReplaceItem().setVisible(false);
			}
			else if (node.getValue() instanceof LocationServiceActivityVo)
			{
				form.getLocalContext().setSelectedRecordOrganisation(null);
				form.getLocalContext().setSelectedRecordLocation(null);
				form.getLocalContext().setSelectedRecordServiceId(null);
				form.getLocalContext().setSelectedRecordLocSerFunc(null);
				form.getLocalContext().setSelectedRecordLocSerAct((LocationServiceActivityVo) node.getValue());
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				form.getContextMenus().getGenericGridRemoveItem().setText("Remove " + ConfigFlag.UI.DISPLAY_NAME_ACTIVITY.getValue(false));
				form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
				form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
				form.getContextMenus().getGenericGridViewItem().setVisible(false);
				form.getContextMenus().getGenericGridConfirmItem().setVisible(false);
				form.getContextMenus().getGenericGridReplaceItem().setVisible(false);
			}

			populateScreenFromData();
		}

	}

	private void managePricingMenuOptions(Boolean isTreating)
	{
		boolean hasRight = engine.hasRight(AppRight.CAN_ACCESS_PRICE_ADJUSTMENT);

		if (ConfigFlag.GEN.SHOW_PROCEDURE_PRICING.getValue() && isTreating && hasRight)
		{
			form.getContextMenus().getGenericGridReplaceItem().setVisible(false);
			if (form.treHeader().getSelectedNode() != null && form.treHeader().getSelectedNode().getNodes().size() > 0)
			{
				form.getContextMenus().getGenericGridReplaceItem().setVisible(true);
				form.getContextMenus().getGenericGridReplaceItem().setText("Input Price Adjs");
				form.getContextMenus().getGenericGridReplaceItem().setIcon(form.getImages().Admin.PricingEuro);
			}

			form.getContextMenus().getGenericGridConfirmItem().setVisible(false);
			if (domain.countBatches(form.getGlobalContext().Admin.getSelectedLocShortVo()) > 0)
			{
				form.getContextMenus().getGenericGridConfirmItem().setVisible(true);
				form.getContextMenus().getGenericGridConfirmItem().setText("Confirm Price Adjs");
				form.getContextMenus().getGenericGridConfirmItem().setIcon(form.getImages().Admin.PricingEuro);
			}
		}
		else
		{
			form.getContextMenus().getGenericGridReplaceItem().setVisible(false);
			form.getContextMenus().getGenericGridConfirmItem().setVisible(false);
		}
	}

	protected void onTreHeaderTreeViewNodeExpandCollapse(TreeNode node) throws PresentationLogicException
	{
		if (node.getNodes().size() == 0)
		{
			if (node.getValue() instanceof LocMostVo)
				populateLocationServices(node, (LocMostVo) node.getValue());
		}
	}

	protected void onBtnCancelClick() throws PresentationLogicException
	{
		setScreenViewMode();
	}

	protected void onBtnSaveClick() throws PresentationLogicException
	{
		save();
	}

	protected void onContextMenuItemClick(int menuItemID, Control sender) throws PresentationLogicException
	{
		if (sender.equals(form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing()))
		{
			setHospitalSpecialtyProcedure();
			switch (menuItemID)
			{
				case GenForm.ContextMenus.LocationServiceActivityGrid.NEW_PRICE :
					menuActivityNewPriceClick();
				break;
				case GenForm.ContextMenus.LocationServiceActivityGrid.UPDATE_PRICING :
					menuActivityUpdatePriceClick();
				break;
				case GenForm.ContextMenus.LocationServiceActivityGrid.VIEW_PRICING :
					menuActivityViewPriceClick();
				break;
			}
		}

		switch (menuItemID)
		{
			case GenForm.ContextMenus.GenericGrid.Add :
				newInstance();
			break;
			case GenForm.ContextMenus.GenericGrid.Update :
				updateInstance();
			break;
			case GenForm.ContextMenus.GenericGrid.Remove :
			{
				if (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo)
				{
					removeService();
				}
				else if (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceFunctionsVo)
					removeFunction();
				else if (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceActivityVo)
					removeActivity();
				break;
			}
			case GenForm.ContextMenus.GenericGrid.Confirm :
				activatePrices();
			break;
			case GenForm.ContextMenus.GenericGrid.Replace :
				priceIncrease();
			break;
			default :
			break;
		}
	}

	private void activatePrices()
	{
		form.getGlobalContext().NTPF.setConfirmPriceIncreases(true);

		engine.open(form.getForms().Admin.NTPFBatchPriceSelectionDialog);
	}

	private void priceIncrease()
	{
		if (form.getContextMenus().getGenericGridConfirmItem().isVisible() && form.getContextMenus().getGenericGridConfirmItem().isEnabled())
		{
			StringBuffer sb = new StringBuffer();
			sb.append("There are unconfirmed batches associated with this Hospital. Do you want to continue?");

			engine.showMessage(sb.toString(), "Unconfirmed Batches", MessageButtons.OKCANCEL, MessageIcon.WARNING);
		}
		else
			engine.open(form.getForms().Core.NewPriceBatchDialog);
	}

	private void menuActivityViewPriceClick()
	{
		TreeNode node = form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode();
		if (node == null)
			return;

		if (node.getValue() instanceof ActivityContextPricingDetailsVo)
		{
			form.getGlobalContext().Admin.setActivityContextPricingDetail((ActivityContextPricingDetailsVo) node.getValue());
			form.getGlobalContext().Admin.setLSAPricingAction(Integer.valueOf(PRICING_VIEW));

			engine.open(form.getForms().Admin.ActivityContextPricingDetails);
		}
	}

	private void menuActivityUpdatePriceClick()
	{
		TreeNode node = form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode();
		if (node == null)
			return;

		if (node.getParent() == null)
			return;

		form.getLocalContext().setSelectedLocationServiceActivity((LocationServiceActivityVo) node.getParent().getValue());
		form.getGlobalContext().Admin.setActivityContextPricingDetail((ActivityContextPricingDetailsVo) node.getValue());

		form.getGlobalContext().Admin.setLSAPricingAction(Integer.valueOf(PRICING_UPDATE));
		form.getGlobalContext().Admin.setActivityContextPricingDetailCollection(((LocationServiceActivityVo) node.getParent().getValue()).getActivityContextPricings());

		engine.open(form.getForms().Admin.ActivityContextPricingDetails);
	}

	private void menuActivityNewPriceClick()
	{
		ActivityContextPricingDetailsVo acpVo = new ActivityContextPricingDetailsVo();
		form.getGlobalContext().Admin.setActivityContextPricingDetail(acpVo);
		form.getGlobalContext().Admin.setLSAPricingAction(Integer.valueOf(PRICING_NEW));

		LocationServiceActivityVo voLocServiceActivity = (LocationServiceActivityVo) form.getLocalContext().getSelectedLocationServiceActivity();

		if (form.treHeader().getSelectedNode().getParent().getValue() != null)
		{
			// Check Hospital is Active and has an Exchequer code
			LocMostVo voHospital = (LocMostVo) form.treHeader().getSelectedNode().getParent().getValue();
			String strMessage = NTPFExchequerCodingChecks.ValidHospital(voHospital);
			if (strMessage != "")
			{
				engine.showMessage(strMessage);
				return;
			}
		}

		if ((form.treHeader().getSelectedNode().getValue() != null) && (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo))
		{
			LocationServiceVo voLocService = (LocationServiceVo) form.treHeader().getSelectedNode().getValue();
			String strMessage = NTPFExchequerCodingChecks.ValidHospitalSpecialty(voLocService);
			if (strMessage != "")
			{
				engine.showMessage(strMessage);
				return;
			}
		}

		String strMessage = NTPFExchequerCodingChecks.ValidHospitalSpecialtyProcedure(voLocServiceActivity);
		if (strMessage != "")
		{
			engine.showMessage(strMessage);
			return;
		}

		if (voLocServiceActivity != null)
		{
			form.getGlobalContext().Admin.setActivityContextPricingDetailCollection(voLocServiceActivity.getActivityContextPricings());

			engine.open(form.getForms().Admin.ActivityContextPricingDetails);
		}
	}

	protected void onFormOpen() throws ims.framework.exceptions.PresentationLogicException
	{
		initalize();
		open();
	}

	protected void onQmbPrinterTextSubmited(String value) throws PresentationLogicException
	{
		form.lyr1().Edit().lyrLocationDetail().tabPageContact().qmbPrinter().clear();

		ReportEngine re = domain.getReportEngine();
		String[] printers = re.listPrinters(value);

		for (int i = 0; i < printers.length; i++)
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().qmbPrinter().newRow(printers[i], printers[i]);
		}
		if (printers.length == 1)
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().qmbPrinter().setValue(printers[0]);
		}
		else if (printers.length > 1)
		{
			form.lyr1().Edit().lyrLocationDetail().tabPageContact().qmbPrinter().showOpened();
		}

	}

	protected void onGrdServiceFunctionGridCheckBoxClicked(int column, grdServiceFunctionRow row, boolean isChecked) throws PresentationLogicException
	{
		row.setActive(row.getSelect());
	}

	protected void onGrdActivityGridCheckBoxClicked(int column, grdActivityRow row, boolean isChecked) throws PresentationLogicException
	{
		// if a service is marked as seleced we need to update the content on
		// the Pricing tab......
		// but need to know if anything has been edited on the pricing
		// tab......if so need to force a save I think.

		if (column == 2)
		{
			// Activities changed so cant change pricing till on DB and Pricing
			// tab refreshed....
			// so disable the Pricing tab .............
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(false);

		}

		if (column == 3)
		{
			// Activities changed so cant change pricing till on DB and Pricing
			// tab refreshed....
			// so disable the Pricing tab .............
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().setHeaderVisible(false);

			if (isChecked)
			{
				row.setActive(true);
				return;
			}
			// check for activity pricing
			if (!isChecked && activityPricingFor(row.getValue()))
			{
				row.setActive(true);
				return;
			}
		}
	}

	private boolean activityPricingFor(ServiceActivityVo voSerAct)
	{
		String sMessage = "";

		if (form.getLocalContext().getSelectedRecordLocationService() != null)
		{
			// select linked location service actions
			if (form.getLocalContext().getSelectedRecordLocationService().getActivitiesIsNotNull())
			{
				for (int i = 0; i < form.getLocalContext().getSelectedRecordLocationService().getActivities().size(); i++)
				{
					if (form.getLocalContext().getSelectedRecordLocationService().getActivities().get(i).getServiceActivity().equals(voSerAct))
					{
						sMessage = "The " + ConfigFlag.UI.DISPLAY_NAME_ACTIVITY.getValue(false) + "\r\n\r\n";

						sMessage += voSerAct.getActivity().toString() + "\r\n";
						engine.showMessage(sMessage + "\r\nhas pricing associated.\r\n\r\nThe " + ConfigFlag.UI.DISPLAY_NAME_ACTIVITY.getValue(false) + " cannot be made inactive.");
						return true;
					}
				}
			}
		}
		return false;
	}

	protected String setToolTipTaxonomy(ServiceActivityVo voActiv, ActivityVo vo)
	{
		String out;
		out = "";

		if (voActiv != null)
		{
			for (int j = 0; j < voActiv.getTaxonomyMap().size(); j++)
			{
				if (voActiv.getTaxonomyMap().get(j).getTaxonomyCodeIsNotNull() && voActiv.getTaxonomyMap().get(j).getTaxonomyNameIsNotNull())
				{
					if (voActiv.getActivity().getID_Activity().equals(vo.getID_Activity()))
					{
						out += makeTextile("Taxonomy: ", "SlateBlue", "tahoma", "12");
						out += makeTextile((voActiv.getTaxonomyMap().get(j).getTaxonomyNameIsNotNull() ? voActiv.getTaxonomyMap().get(j).getTaxonomyName().toString() : ""), "black", "tahoma", "12");
						out += makeTextile(" Code: ", "SlateBlue", "tahoma", "12");
						out += makeTextile((voActiv.getTaxonomyMap().get(j).getTaxonomyCodeIsNotNull() ? voActiv.getTaxonomyMap().get(j).getTaxonomyCode() : "") + "<br>", "black", "tahoma", "12");
					}
				}
			}
		}

		return out;
	}

	@Override
	protected void onChkActivePricingValueChanged() throws PresentationLogicException
	{
		populateActivitiesTreeFromData();

		if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode() == null)//wdev-7106
			form.getContextMenus().hideAllLocationServiceActivityGridMenuItems();
	}

	private void populateActivitiesTreeFromData()
	{
		if (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo)
		{
			LocationServiceActivityVoCollection voCollLocServiceActivities = null;
			LocationServiceActivityVo voLocServiceActivity = null;
			LocationServiceActivityVo nodeToSelectLocationServiceActivityVo = null;

			if (form.getMode().equals(FormMode.EDIT))
			{
				// Create the collection from the screen contents and not the DB
				for (int k = 0; k < form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getNodes().size(); k++)
				{
					if (voCollLocServiceActivities == null)
						voCollLocServiceActivities = new LocationServiceActivityVoCollection();

					if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getNodes().get(k).getValue() != null)
						voCollLocServiceActivities.add((LocationServiceActivityVo) form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getNodes().get(k).getValue());
				}
				
				if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode() != null && form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getValue() != null)
				{
					if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getValue() instanceof LocationServiceActivityVo)
						nodeToSelectLocationServiceActivityVo = (LocationServiceActivityVo) form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getValue();
				}
			}
			else
				voCollLocServiceActivities = domain.listLocationServiceActivity((LocationServiceVo) form.treHeader().getSelectedNode().getValue());

			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().clear();

			for (int i = 0; voCollLocServiceActivities != null && i < voCollLocServiceActivities.size(); i++)
			{
				voLocServiceActivity = voCollLocServiceActivities.get(i);

				if ((voLocServiceActivity.getServiceActivityIsNotNull()) && (voLocServiceActivity.getServiceActivity().getActivityIsNotNull()) && (voLocServiceActivity.getIsActiveIsNotNull())// wdev-3966
						&& (voLocServiceActivity.getIsActive()))
				{
					StringBuffer sb = new StringBuffer();
					sb.append(voLocServiceActivity.getServiceActivity().getActivity().getName());
					for (int x = 0; voLocServiceActivity.getServiceActivity().getTaxonomyMapIsNotNull() && x < voLocServiceActivity.getServiceActivity().getTaxonomyMap().size(); x++)
					{
						if (voLocServiceActivity.getServiceActivity().getTaxonomyMap().get(x).getTaxonomyName().equals(TaxonomyType.EXCH))
						{
							sb.append("  ");
							sb.append(voLocServiceActivity.getServiceActivity().getTaxonomyMap().get(x).getTaxonomyCode());
						}
					}
					TreeNode rootNode = form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getNodes().add(voLocServiceActivity, sb.toString());

					rootNode.setCollapsedImage(form.getImages().Admin.Activity);
					rootNode.setExpandedImage(form.getImages().Admin.Activity);

					populatePricing(rootNode, voLocServiceActivity.getActivityContextPricings());
				}
			}
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().expandAll();
			form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().setValue(nodeToSelectLocationServiceActivityVo);
		}
	}

	private void populatePricing(TreeNode rootNode, ActivityContextPricingDetailsVoCollection voCollActivityContextPricing)
	{
		voCollActivityContextPricing.sort();

		if (rootNode.getNodes().size() > 0)
			rootNode.getNodes().clear();

		setHospitalSpecialtyProcedure();

		TreeNode pricingNode;
		for (int j = 0; j < voCollActivityContextPricing.size(); j++)
		{
			if ((voCollActivityContextPricing.get(j).getIsActiveIsNotNull() && voCollActivityContextPricing.get(j).getIsActive().booleanValue() && voCollActivityContextPricing.get(j).getEffectiveTo() == null && form.lyr1().Edit().lyrLocationDetail().tabPagePricing().chkActivePricing().getValue()) || form.lyr1().Edit().lyrLocationDetail().tabPagePricing().chkActivePricing().getValue() == false)
			{
				StringBuffer sb = createPricingString(voCollActivityContextPricing.get(j));
				pricingNode = rootNode.getNodes().add(voCollActivityContextPricing.get(j), sb.toString());
				pricingNode.setCollapsedImage(voCollActivityContextPricing.get(j).getCurrency().getImage() == null ? form.getImages().Core.help1 : voCollActivityContextPricing.get(j).getCurrency().getImage());
				pricingNode.setExpandedImage(voCollActivityContextPricing.get(j).getCurrency().getImage() == null ? form.getImages().Core.help1 : voCollActivityContextPricing.get(j).getCurrency().getImage());
				if (voCollActivityContextPricing.get(j).getEffectiveToIsNotNull())
					pricingNode.setTextColor(Color.Gray);

				pricingNode.setTooltip(createPricingTooltip(voCollActivityContextPricing.get(j)));
			}
		}
	}

	private StringBuffer createPricingString(ActivityContextPricingDetailsVo voActivityContextPricing)
	{
		StringBuffer sb = new StringBuffer();

		if ((voActivityContextPricing.getIsActiveIsNotNull()) && (voActivityContextPricing.getIsActive().booleanValue()))
			sb.append("Context:");
		else
			sb.append("Pre-active Context:");

		sb.append(voActivityContextPricing.getContext().toString());
		sb.append(", Price:");
		sb.append(voActivityContextPricing.getPriceCategoryIsNotNull() ? DecimalFormat.format(voActivityContextPricing.getPrice(), 0, 2) : "");
		sb.append(", Length of Stay:");
		sb.append(voActivityContextPricing.getNormLengthStayIsNotNull() ? voActivityContextPricing.getNormLengthStay().toString() : "");

		if (voActivityContextPricing.getEffectiveFromIsNotNull())
		{
			sb.append(", From:");
			sb.append(voActivityContextPricing.getEffectiveFrom().toString());
		}
		if (voActivityContextPricing.getEffectiveFromSetIsNotNull())
		{
			sb.append(", Set:");
			sb.append(voActivityContextPricing.getEffectiveFromSet().toString());
		}
		if (voActivityContextPricing.getEffectiveToIsNotNull())
		{
			sb.append(", To:");
			sb.append(voActivityContextPricing.getEffectiveTo().toString());
		}
		if (voActivityContextPricing.getEffectiveToSetIsNotNull())
		{
			sb.append(", Set:");
			sb.append(voActivityContextPricing.getEffectiveToSet().toString());
		}
		if (voActivityContextPricing.getCreatingBatchIsNotNull() && voActivityContextPricing.getIsPreActiveIsNotNull() && voActivityContextPricing.getIsPreActive().booleanValue())
		{
			sb.append(", Batch:");
			sb.append(voActivityContextPricing.getCreatingBatch().getBatchName());
		}

		return sb;
	}

	protected String createPricingTooltip(ActivityContextPricingDetailsVo voPricing)
	{
		StringBuffer strTooltip = new StringBuffer();
		strTooltip.append("<font color=\"#FF0000\"><b>Pricing</b></font>&nbsp;<br><br>");

		if ((voPricing.getIsActiveIsNotNull()) && (!voPricing.getIsActive().booleanValue()))
			strTooltip.append("*** Pre-active Context ***<br><br>");

		strTooltip.append("Context: " + (voPricing.getContextIsNotNull() ? voPricing.getContext().toString() : ""));
		strTooltip.append("<br><br>");
		strTooltip.append("Category: " + (voPricing.getPriceCategoryIsNotNull() ? voPricing.getPriceCategory().toString() : ""));
		strTooltip.append("<br><br>");
		strTooltip.append("Currency: " + (voPricing.getCurrencyIsNotNull() ? voPricing.getCurrency().toString() : ""));
		strTooltip.append("<br><br>");
		strTooltip.append("Price: " + (voPricing.getPriceIsNotNull() ? voPricing.getPrice().toString() : ""));
		strTooltip.append("<br><br>");
		strTooltip.append("Normal length of stay: " + (voPricing.getNormLengthStayIsNotNull() ? voPricing.getNormLengthStay().toString() : ""));
		strTooltip.append("<br><br>");
		strTooltip.append("Effective from: " + (voPricing.getEffectiveFromIsNotNull() ? voPricing.getEffectiveFrom().toString() : ""));
		strTooltip.append("<br><br>");
		strTooltip.append("Effective from set: " + (voPricing.getEffectiveFromSetIsNotNull() ? voPricing.getEffectiveFromSet().toString() : ""));
		strTooltip.append("<br><br>");
		strTooltip.append("Effective to: " + (voPricing.getEffectiveToIsNotNull() ? voPricing.getEffectiveTo().toString() : ""));
		strTooltip.append("<br><br>");
		strTooltip.append("Effective to set: " + (voPricing.getEffectiveToSetIsNotNull() ? voPricing.getEffectiveToSet().toString() : ""));
		strTooltip.append("<br><br>");
		if (voPricing.getIsActiveIsNotNull() && voPricing.getIsActive().booleanValue())
			strTooltip.append("Status: Active");
		else if (voPricing.getIsPreActiveIsNotNull() && voPricing.getIsPreActive().booleanValue())
			strTooltip.append("Status: Pre - Active");

		return strTooltip.toString();
	}

	private void setHospitalSpecialtyProcedure()
	{
		// Issue 2.12.9
		if ((form.treHeader().getSelectedNode().getValue() != null) && (form.treHeader().getSelectedNode().getValue() instanceof LocationServiceVo))
		{
			LocationServiceVo voSpecialty = (LocationServiceVo) form.treHeader().getSelectedNode().getValue();
			form.getGlobalContext().Admin.setCurrentSpecialty(voSpecialty.getService().getServiceName());
		}

		if (form.treHeader().getSelectedNode().getParent().getValue() != null)
		{
			LocMostVo voHospital = (LocMostVo) form.treHeader().getSelectedNode().getParent().getValue();
			form.getGlobalContext().Admin.setCurrentHospital(voHospital.getName());
		}

		if (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode() != null)
		{
			if ((form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getValue() != null) && (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getParent() == null))
			{

				LocationServiceActivityVo voProcedure = (LocationServiceActivityVo) form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getValue();
				if ((voProcedure.getServiceActivityIsNotNull()) && (voProcedure.getServiceActivity().getActivityIsNotNull()))
					form.getGlobalContext().Admin.setCurrentProcedure(voProcedure.getServiceActivity().getActivity().getName());
			}
			else if ((form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getValue() != null) && (form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getParent() != null))
			{
				LocationServiceActivityVo voProcedure = (LocationServiceActivityVo) form.lyr1().Edit().lyrLocationDetail().tabPagePricing().trePricing().getSelectedNode().getParent().getValue();

				if ((voProcedure.getServiceActivityIsNotNull()) && (voProcedure.getServiceActivity().getActivityIsNotNull()))
					form.getGlobalContext().Admin.setCurrentProcedure(voProcedure.getServiceActivity().getActivity().getName());
			}
		}
	}

	@Override
	protected void onTrePricingTreeViewSelectionChanged(TreeNode node) throws PresentationLogicException
	{
		if (node == null)
			return;

		form.getContextMenus().getLocationServiceActivityGridUPDATE_PRICINGItem().setVisible(false);
		form.getContextMenus().getLocationServiceActivityGridNEW_PRICEItem().setVisible((node.getValue() instanceof LocationServiceActivityVo) && form.getMode().equals(FormMode.EDIT));

		if (node.getValue() instanceof ActivityContextPricingDetailsVo)
		{
			ActivityContextPricingDetailsVo voACP = (ActivityContextPricingDetailsVo) node.getValue();
			if (voACP.getEffectiveTo() == null || (voACP.getEffectiveToIsNotNull() && voACP.getIsPreActiveIsNotNull() && voACP.getIsPreActive().booleanValue()))
				form.getContextMenus().getLocationServiceActivityGridUPDATE_PRICINGItem().setVisible((node.getValue() instanceof ActivityContextPricingDetailsVo) && form.getMode().equals(FormMode.EDIT));

			if (!voACP.getID_ActivityContextPricingDetailsIsNotNull())
			{
				form.getContextMenus().getLocationServiceActivityGridUPDATE_PRICINGItem().setText("No Update until saved");
				form.getContextMenus().getLocationServiceActivityGridUPDATE_PRICINGItem().setEnabled(false);
			}
			else
			{
				form.getContextMenus().getLocationServiceActivityGridUPDATE_PRICINGItem().setText("Update Pricing");
				form.getContextMenus().getLocationServiceActivityGridUPDATE_PRICINGItem().setEnabled(true);
			}
		}
		form.getContextMenus().getLocationServiceActivityGridVIEW_PRICINGItem().setVisible(node.getValue() instanceof ActivityContextPricingDetailsVo);

		if (node.getValue() instanceof LocationServiceActivityVo)
			form.getLocalContext().setSelectedLocationServiceActivity((LocationServiceActivityVo) node.getValue());

		if (node.getValue() instanceof ActivityContextPricingDetailsVo)
			form.getLocalContext().setSelectedLocationServiceActivity((LocationServiceActivityVo) node.getParent().getValue());
	}

	@Override
	protected void onMessageBoxClosed(int messageBoxId, DialogResult result) throws PresentationLogicException
	{
		if (result.equals(DialogResult.OK))
			engine.open(form.getForms().Core.NewPriceBatchDialog);
	}

	@Override
	protected void onChkHTMLActiveOnlyValueChanged() throws PresentationLogicException
	{
		if (form.treHeader().getSelectedNode() != null)
		{
			if (form.treHeader().getSelectedNode().getParent() == null)
				form.lyr1().Details().htmDetails().setHTML("");
			else if (form.treHeader().getSelectedNode().getParent().getValue() instanceof OrganisationVo)
				form.lyr1().Details().htmDetails().setHTML(getDetailsAsTextile(form.treHeader().getSelectedNode()));
			else
				form.lyr1().Details().htmDetails().setHTML(getDetailsAsTextile(form.treHeader().getSelectedNode().getParent()));
		}
	}
}
