gt: 			if (form.getLocalContext().getaddressIndex() != -1) //FB
context: //#############################################################################
  //#                                                                           #
  //#  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.38 build 2144.16652)
  // Copyright (C) 1995-2005 IMS MAXIMS plc. All rights reserved.
  
  package ims.core.forms.otheraddresses;
  import java.util.ArrayList;
  
  import ims.configuration.gen.ConfigFlag;
  import ims.core.vo.PersonAddress;
  import ims.core.vo.PersonAddressCollection;
  import ims.core.vo.lookups.AddressType;
  import ims.core.vo.lookups.ChannelType;
  import ims.framework.enumerations.DialogResult;
  import ims.framework.enumerations.FormMode;
  import ims.framework.exceptions.PresentationLogicException;
  
  public class Logic extends BaseLogic
  {
  	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
  	{
  		initalize();
  	}
  		
  	private void initalize() 
  	{
  		//update local context
  		if (form.getGlobalContext().Core.getOtherAddressesIsNotNull())
  		{
  			PersonAddressCollection coll = (ims.core.vo.PersonAddressCollection)form.getGlobalContext().Core.getOtherAddresses().clone();
  			PersonAddressCollection collNonHistorical = new PersonAddressCollection();
  			for(int i=0; i<coll.size(); i++)
  			{
  				if(coll.get(i).getAddressType()!=null && !coll.get(i).getAddressType().equals(AddressType.HISTORICAL))
  					collNonHistorical.add(coll.get(i));
  			}
  			form.getLocalContext().setpersonAddressCol(collNonHistorical);
  		
  		}
  		else
  			form.getLocalContext().setpersonAddressCol(null);
  
  		if (form.getLocalContext().getpersonAddressColIsNotNull() && form.getLocalContext().getpersonAddressCol().size() > 0)
  		{			
  			for(PersonAddress pa:form.getLocalContext().getpersonAddressCol())
  			{
  				if(pa.getAddressType()!=null && !pa.getAddressType().equals(AddressType.HISTORICAL))//WDEV-13009 exclude Historical addresses
  				{
  					form.recbrAddress().newRow(pa, pa.getLine1IsNotNull()?pa.getLine1():"");
  					form.recbrAddress().setValue(pa);
  					form.recbrAddress().setEnabled(true);
  				}
  				else if(pa.getAddressType()==null)
  				{
  					form.recbrAddress().newRow(pa, pa.getLine1IsNotNull()?pa.getLine1():"");
  					form.recbrAddress().setValue(pa);
  					form.recbrAddress().setEnabled(true);
  				}
  			}
  		}
  		form.cmbAddressType().removeRow(AddressType.NOKHOME);	
  		
  		form.cmbAddressType().removeRow(AddressType.HISTORICAL);//WDEV-13009 exclude Historical type	
  		populateScreenFromData(form.recbrAddress().getValue());
  
  		form.btnClear().setVisible(true);
  		form.btnNew().setVisible(true);
  		form.btnUpdate().setVisible(true);
  		form.btnOK().setVisible(true);
  				
  		form.setMode(FormMode.VIEW);		
  		
  	}
  
  	protected void onFormModeChanged()
  	{				
  		// Unless using local patient registration, this form should go to view mode only
  		if ((!ims.configuration.gen.ConfigFlag.DOM.LOCAL_PATIENT_REGISTRATION.getValue()))
  		{
  			form.ccAddress().setEnabled(false);
  			form.cmbAddressType().setEnabled(false);			
  			form.txtOtherPhone().setEnabled(false);			
  			form.btnNew().setVisible(false);
  			form.btnUpdate().setVisible(false);
  			form.recbrAddress().setEnabled(true);
  			form.btnOK().setVisible(false);
  			form.btnClear().setVisible(false);//WDEV-14367
  		}
  		else
  		{
  			form.ccAddress().setComponentEnabled(form.getMode().equals(FormMode.EDIT));			
  			form.cmbAddressType().setEnabled(form.getMode().equals(FormMode.EDIT));
  			form.txtOtherPhone().setEnabled(form.getMode().equals(FormMode.EDIT));
  			if (form.getLocalContext().getpersonAddressColIsNotNull())
  			{
  				if (form.getLocalContext().getpersonAddressCol().size()< ConfigFlag.UI.MAXIMUM_NUMBER_OF_OTHERADDRESSES.getValue())
  					form.btnNew().setVisible(true);
  				else
  					form.btnNew().setVisible(false);
  			}
  			form.btnUpdate().setEnabled(form.getMode().equals(FormMode.VIEW));
  			form.btnUpdate().setVisible(true);
  			form.btnUpdate().setEnabled(form.getMode().equals(FormMode.VIEW) && form.recbrAddress().getValue() != null);
  			
  			if (ConfigFlag.UI.MAXIMUM_NUMBER_OF_OTHERADDRESSES.getValue() == 1)
  				form.recbrAddress().setVisible(false);
  			else
  				form.recbrAddress().setEnabled(form.recbrAddress().getValue() != null);
  				
  			form.btnOK().setEnabled(form.getMode().equals(FormMode.EDIT));
  			form.btnClear().setEnabled(form.getMode().equals(FormMode.EDIT));//WDEV-14367
  		}			
  		
  		form.getLocalContext().setFieldsWereCleared(Boolean.FALSE);
  		
  		//WDEV-14613
  		if ((engine.getPreviousNonDialogFormName().equals(form.getForms().Core.Demographics) || engine.getParentDialogFormName().equals(form.getForms().Core.DemographicsDialog))
  				&& form.getGlobalContext().Core.getParentFormModeIsNotNull()
  				&& form.getGlobalContext().Core.getParentFormMode().equals(FormMode.VIEW))
  		{
  			form.btnClear().setVisible(false);
  			form.btnNew().setVisible(false);
  			form.btnUpdate().setVisible(false);
  			form.btnOK().setVisible(false);
  		}
  
  		form.btnCancel().setEnabled(true);
  	}
  	
  	private String[] validateUIRules()
  	{
  		ArrayList<String> errors = new ArrayList<String>();
  		if(form.ccAddress().getValue() == null || form.ccAddress().getValue().getLine1() == null || form.ccAddress().getValue().getLine1().equals(""))
  			errors.add("Address Line 1 is a mandatory field");				
  				
  		if (form.cmbAddressType().getValue() == null)
  			errors.add("Address Type is a mandatory field");
  
  		if (errors.size() == 0)
  			return null;
  		
  		String[] aErrors = new String[errors.size()];
  		errors.toArray(aErrors);
  		
  		return aErrors;
  	}
  		
  	protected void onBtnOKClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		if (form.getMode().equals(FormMode.EDIT))
  		{
  			form.ccAddress().setRequireAdress(true);
  			form.cmbAddressType().setRequired(true);
  			
  			if ( ! form.getLocalContext().getFieldsWereCleared()
  				|| (form.getLocalContext().getFieldsWereCleared() && !AddressFieldsAreEmpty())
  				|| (form.getLocalContext().getFieldsWereCleared() && form.txtOtherPhone().getValue() != null) )
  			{
  				form.getLocalContext().setFieldsWereCleared(false);
  				
  				String[] errors = validateUIRules();
  				if (errors != null)
  				{
  					engine.showErrors(errors);
  					return;
  				}
  			}
  			
  			int nIndex = -1;
  			if (form.getLocalContext().getaddressIndex() != -1)
  			{
  				for (int i = 0 ; i < form.getGlobalContext().Core.getOtherAddresses().size() ; i++)
  				{
  					if (form.getGlobalContext().Core.getOtherAddresses().get(i).equals(form.getLocalContext().getpersonAddress()))
  						nIndex = i;
  				}
  			}
  				
  			if ( ! form.getLocalContext().getFieldsWereCleared())
  			{
  				populateDataFromScreen();
  				//form.getLocalContext().getpersonAddress();
  			}
  			else 
  			{
  				form.getLocalContext().getpersonAddress().setAddressBuildingName("");
  				form.getLocalContext().getpersonAddress().setAddressBuildingNumber("");
  				form.getLocalContext().getpersonAddress().setAddressLocality("");
  				form.getLocalContext().getpersonAddress().setAddressCounty("");
  				form.getLocalContext().getpersonAddress().setAddressPostTown("");
  				form.getLocalContext().getpersonAddress().setAddressStreet("");
  				form.getLocalContext().getpersonAddress().setAddressLine5("");
  				form.getLocalContext().getpersonAddress().setPostCode("");
  				form.getLocalContext().getpersonAddress().setOtherphone("");
  				form.getLocalContext().getpersonAddress().setPhone("");
  				form.getLocalContext().getpersonAddress().setAddressType(null);
  			}
  				
  