/// <summary>
        /// Sets up the value cell(s) for a specific data label
        ///
        /// </summary>
        /// <returns>void</returns>
        private void SetupGridValueCell(Int32 ARowIndex, PDataLabelRow ADataLabelRow)
        {
            Control cellControl;

            System.Windows.Forms.TextBox TextBoxEditor;
            TtxtPetraDate DateEditor;
            System.Windows.Forms.CheckBox CheckBoxEditor;
            TTxtNumericTextBox TextBoxNumericEditor;
            TTxtCurrencyTextBox TextBoxCurrencyEditor;
            TCmbAutoPopulated LookupValueEditor;
            TtxtAutoPopulatedButtonLabel PartnerKeyEditor;
            SourceGrid.Cells.Views.Cell ValueModel;
            SourceGrid.Cells.Views.Cell SuffixModel;
            PDataLabelValuePartnerRow DataLabelValuePartnerRow;
            PDataLabelValueApplicationRow DataLabelValueApplicationRow;

            // prepare model for the value cells
            ValueModel = new SourceGrid.Cells.Views.Cell();
            ValueModel.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;
            ValueModel.Font = new Font(FLocalDataLabelValuesGrid.Font.FontFamily.Name, FLocalDataLabelValuesGrid.Font.Size, FontStyle.Bold);

            // prepare model for suffix cells (e.g. for currency)
            SuffixModel = new SourceGrid.Cells.Views.Cell();
            SuffixModel.BackColor = FLocalDataLabelValuesGrid.BackColor;
            SuffixModel.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft;

            // In this case the data value rows will only be created once a value is entered
            GetOrCreateDataLabelValueRow(false, ADataLabelRow, out DataLabelValuePartnerRow, out DataLabelValueApplicationRow);

            // initialize cell control
            cellControl = null;

            // Create field, according to specified data type
            // Create character field
            if (ADataLabelRow.DataType == MCommonConstants.OFFICESPECIFIC_DATATYPE_CHAR)
            {
                TextBoxEditor = new System.Windows.Forms.TextBox();
                cellControl = TextBoxEditor;

                if (DataLabelValuePartnerRow != null)
                {
                    TextBoxEditor.Text = DataLabelValuePartnerRow.ValueChar;
                }
                else if (DataLabelValueApplicationRow != null)
                {
                    TextBoxEditor.Text = DataLabelValueApplicationRow.ValueChar;
                }
                else
                {
                    // Default value if no Label data exists for the Partner
                    TextBoxEditor.Text = "";
                }

                // enable save button in editor when cell contents have changed
                TextBoxEditor.TextChanged += new EventHandler(this.ControlValueHasChanged);

                FLocalDataLabelValuesGrid[ARowIndex, 1] = new SourceGrid.Cells.Cell();
                FLocalDataLabelValuesGrid.LinkedControls.Add(new LinkedControlValue((Control)TextBoxEditor, new Position(ARowIndex, 1)));
                FLocalDataLabelValuesGrid[ARowIndex, 1].Tag = TextBoxEditor;
            }
            // Create float field
            else if (ADataLabelRow.DataType == MCommonConstants.OFFICESPECIFIC_DATATYPE_FLOAT)
            {
                TextBoxNumericEditor = new TTxtNumericTextBox();

                if (ADataLabelRow.NumDecimalPlaces == 0)
                {
                    TextBoxNumericEditor.ControlMode = TTxtNumericTextBox.TNumericTextBoxMode.LongInteger;
                    TextBoxNumericEditor.MaxLength = 14;
                }
                else
                {
                    TextBoxNumericEditor.ControlMode = TTxtNumericTextBox.TNumericTextBoxMode.Decimal;
                    TextBoxNumericEditor.DecimalPlaces = ADataLabelRow.NumDecimalPlaces;

                    // limit text length. 14 for number of digits, 5 for decimal and thousands separators
                    TextBoxNumericEditor.MaxLength = 14 + 5 + ADataLabelRow.NumDecimalPlaces;
                }

                TextBoxNumericEditor.NullValueAllowed = true;
                cellControl = TextBoxNumericEditor;

                if (ADataLabelRow.NumDecimalPlaces == 0)
                {
                    if (DataLabelValuePartnerRow != null)
                    {
                        TextBoxNumericEditor.NumberValueLongInt = (long)DataLabelValuePartnerRow.ValueNum;
                    }
                    else if (DataLabelValueApplicationRow != null)
                    {
                        TextBoxNumericEditor.NumberValueLongInt = (long)DataLabelValueApplicationRow.ValueNum;
                    }
                    else
                    {
                        // Default value if no Label data exists for the Partner
                        TextBoxNumericEditor.NumberValueLongInt = null;
                    }
                }
                else
                {
                    if (DataLabelValuePartnerRow != null)
                    {
                        TextBoxNumericEditor.NumberValueDecimal = DataLabelValuePartnerRow.ValueNum;
                    }
                    else if (DataLabelValueApplicationRow != null)
                    {
                        TextBoxNumericEditor.NumberValueDecimal = DataLabelValueApplicationRow.ValueNum;
                    }
                    else
                    {
                        // Default value if no Label data exists for the Partner
                        TextBoxNumericEditor.NumberValueDecimal = null;
                    }
                }

                // enable save button in editor when cell contents have changed
                TextBoxNumericEditor.TextChanged += new EventHandler(this.ControlValueHasChanged);

                FLocalDataLabelValuesGrid[ARowIndex, 1] = new SourceGrid.Cells.Cell();
                FLocalDataLabelValuesGrid.LinkedControls.Add(new LinkedControlValue((Control)TextBoxNumericEditor, new Position(ARowIndex, 1)));
                FLocalDataLabelValuesGrid[ARowIndex, 1].Tag = TextBoxNumericEditor;
            }
            // Create data field
            else if (ADataLabelRow.DataType == MCommonConstants.OFFICESPECIFIC_DATATYPE_DATE)
            {
                DateEditor = new TtxtPetraDate();
                DateEditor.Date = null;
                cellControl = DateEditor;

                if (DataLabelValuePartnerRow != null)
                {
                    if (!DataLabelValuePartnerRow.IsValueDateNull())
                    {
                        DateEditor.Date = DataLabelValuePartnerRow.ValueDate;
                    }
                }
                else if (DataLabelValueApplicationRow != null)
                {
                    if (!DataLabelValueApplicationRow.IsValueDateNull())
                    {
                        DateEditor.Date = DataLabelValueApplicationRow.ValueDate;
                    }
                }

                // enable save button in editor when cell contents have changed
                DateEditor.DateChanged += new TPetraDateChangedEventHandler(this.ControlValueHasChanged);

                FLocalDataLabelValuesGrid[ARowIndex, 1] = new SourceGrid.Cells.Cell();
                FLocalDataLabelValuesGrid.LinkedControls.Add(new LinkedControlValue((Control)DateEditor, new Position(ARowIndex, 1)));
                FLocalDataLabelValuesGrid[ARowIndex, 1].Tag = DateEditor;
            }
            // Create integer field
            else if (ADataLabelRow.DataType == MCommonConstants.OFFICESPECIFIC_DATATYPE_INTEGER)
            {
                TextBoxNumericEditor = new TTxtNumericTextBox();
                TextBoxNumericEditor.ControlMode = TTxtNumericTextBox.TNumericTextBoxMode.Integer;
                TextBoxNumericEditor.NullValueAllowed = true;
                cellControl = TextBoxNumericEditor;

                if (DataLabelValuePartnerRow != null)
                {
                    TextBoxNumericEditor.NumberValueInt = DataLabelValuePartnerRow.ValueInt;
                }
                else if (DataLabelValueApplicationRow != null)
                {
                    TextBoxNumericEditor.NumberValueInt = DataLabelValueApplicationRow.ValueInt;
                }
                else
                {
                    // Default value if no Label data exists for the Partner
                    TextBoxNumericEditor.NumberValueInt = null;
                }

                // enable save button in editor when cell contents have changed
                TextBoxNumericEditor.TextChanged += new EventHandler(this.ControlValueHasChanged);

                FLocalDataLabelValuesGrid[ARowIndex, 1] = new SourceGrid.Cells.Cell();
                FLocalDataLabelValuesGrid.LinkedControls.Add(new LinkedControlValue((Control)TextBoxNumericEditor, new Position(ARowIndex, 1)));
                FLocalDataLabelValuesGrid[ARowIndex, 1].Tag = TextBoxNumericEditor;
            }
            // Create currency field
            else if (ADataLabelRow.DataType == MCommonConstants.OFFICESPECIFIC_DATATYPE_CURRENCY)
            {
                TextBoxCurrencyEditor = new TTxtCurrencyTextBox();
                TextBoxCurrencyEditor.DecimalPlaces = 2;
                TextBoxCurrencyEditor.CurrencyCode = ADataLabelRow.CurrencyCode;
                TextBoxCurrencyEditor.NullValueAllowed = true;
                cellControl = TextBoxCurrencyEditor;

                if (DataLabelValuePartnerRow != null)
                {
                    TextBoxCurrencyEditor.NumberValueDecimal = DataLabelValuePartnerRow.ValueCurrency;
                }
                else if (DataLabelValueApplicationRow != null)
                {
                    TextBoxCurrencyEditor.NumberValueDecimal = DataLabelValueApplicationRow.ValueCurrency;
                }
                else
                {
                    // Default value if no Label data exists for the Partner
                    TextBoxCurrencyEditor.NumberValueDecimal = null;
                }

                // enable save button in editor when cell contents have changed
                TextBoxCurrencyEditor.TextChanged += new EventHandler(this.ControlValueHasChanged);

                FLocalDataLabelValuesGrid[ARowIndex, 1] = new SourceGrid.Cells.Cell();
                FLocalDataLabelValuesGrid.LinkedControls.Add(new LinkedControlValue((Control)TextBoxCurrencyEditor, new Position(ARowIndex, 1)));
                FLocalDataLabelValuesGrid[ARowIndex, 1].Tag = TextBoxCurrencyEditor;
            }
            // Create boolean field
            else if (ADataLabelRow.DataType == MCommonConstants.OFFICESPECIFIC_DATATYPE_BOOLEAN)
            {
                CheckBoxEditor = new System.Windows.Forms.CheckBox();
                cellControl = CheckBoxEditor;

                if (DataLabelValuePartnerRow != null)
                {
                    CheckBoxEditor.Checked = DataLabelValuePartnerRow.ValueBool;
                }
                else if (DataLabelValueApplicationRow != null)
                {
                    CheckBoxEditor.Checked = DataLabelValueApplicationRow.ValueBool;
                }
                else
                {
                    // Default value if no Label data exists for the Partner
                    CheckBoxEditor.Checked = false;
                }

                // enable save button in editor when cell contents have changed
                CheckBoxEditor.CheckedChanged += new EventHandler(this.ControlValueHasChanged);

                FLocalDataLabelValuesGrid[ARowIndex, 1] = new SourceGrid.Cells.Cell();
                FLocalDataLabelValuesGrid.LinkedControls.Add(new LinkedControlValue((Control)CheckBoxEditor, new Position(ARowIndex, 1)));
                FLocalDataLabelValuesGrid[ARowIndex, 1].Tag = CheckBoxEditor;
            }
            // Create partner key field
            else if (ADataLabelRow.DataType == MCommonConstants.OFFICESPECIFIC_DATATYPE_PARTNERKEY)
            {
                PartnerKeyEditor = new TtxtAutoPopulatedButtonLabel();
                PartnerKeyEditor.ASpecialSetting = true;
                PartnerKeyEditor.ButtonText = ADataLabelRow.Text + ':';
                PartnerKeyEditor.ButtonTextAlign = System.Drawing.ContentAlignment.MiddleRight;
                PartnerKeyEditor.ListTable = TtxtAutoPopulatedButtonLabel.TListTableEnum.PartnerKey;
                PartnerKeyEditor.TabStop = false;
                cellControl = PartnerKeyEditor;

                // AutomaticallyUpdateDataSource: very rare, but needed here
                PartnerKeyEditor.AutomaticallyUpdateDataSource = true;

                if (DataLabelValuePartnerRow != null)
                {
                    PartnerKeyEditor.Text = StringHelper.PartnerKeyToStr(DataLabelValuePartnerRow.ValuePartnerKey);
                }
                else if (DataLabelValueApplicationRow != null)
                {
                    PartnerKeyEditor.Text = StringHelper.PartnerKeyToStr(DataLabelValueApplicationRow.ValuePartnerKey);
                }
                else
                {
                    // Default value if no Label data exists for the Partner
                    PartnerKeyEditor.Text = StringHelper.PartnerKeyToStr(0);
                }

                // display partner name linked to partner key
                PartnerKeyEditor.ResetLabelText();

                // enable save button in editor when cell contents have changed
                PartnerKeyEditor.ValueChanged += new TDelegatePartnerChanged(this.PartnerKeyControlValueHasChanged);
                PartnerKeyEditor.TextChanged += new System.EventHandler(this.ControlValueHasChanged);


                FLocalDataLabelValuesGrid[ARowIndex, 0] = new SourceGrid.Cells.Cell();
                FLocalDataLabelValuesGrid.LinkedControls.Add(new LinkedControlValue(PartnerKeyEditor, new Position(ARowIndex, 0)));
                FLocalDataLabelValuesGrid[ARowIndex, 0].Tag = PartnerKeyEditor;
                FLocalDataLabelValuesGrid[ARowIndex, 0].ColumnSpan = 3;
            }
            // Create lookup field
            else if (ADataLabelRow.DataType == MCommonConstants.OFFICESPECIFIC_DATATYPE_LOOKUP)
            {
                // Get the instance of the combobox (created in the actual user interface class)
                LookupValueEditor = new TCmbAutoPopulated();
                LookupValueEditor.Filter = PDataLabelLookupTable.GetCategoryCodeDBName() + " = '" + ADataLabelRow.LookupCategoryCode + "'";
                LookupValueEditor.ListTable = TCmbAutoPopulated.TListTableEnum.DataLabelLookupList;
                LookupValueEditor.InitialiseUserControl();
                cellControl = LookupValueEditor;

                if (DataLabelValuePartnerRow != null)
                {
                    LookupValueEditor.SetSelectedString(DataLabelValuePartnerRow.ValueLookup);
                }
                else if (DataLabelValueApplicationRow != null)
                {
                    LookupValueEditor.SetSelectedString(DataLabelValueApplicationRow.ValueLookup);
                }
                else
                {
                    // Default value if no Label data exists for the Partner
                    LookupValueEditor.Text = "";
                }

                // enable save button in editor when cell contents have changed
                LookupValueEditor.SelectedValueChanged += new EventHandler(this.ControlValueHasChanged);
                LookupValueEditor.TextChanged += new EventHandler(this.ControlValueHasChanged);

                FLocalDataLabelValuesGrid[ARowIndex, 1] = new SourceGrid.Cells.Cell();
                FLocalDataLabelValuesGrid.LinkedControls.Add(new LinkedControlValue((Control)LookupValueEditor, new Position(ARowIndex, 1)));
                FLocalDataLabelValuesGrid[ARowIndex, 1].Tag = LookupValueEditor;
                FLocalDataLabelValuesGrid[ARowIndex, 1].ColumnSpan = 2;
            }

            // perform actions that need to be done for each control
            if (cellControl != null)
            {
                // remember the added control to get the value back lateron
                FGridRowInfo.SetControl(ARowIndex, cellControl);

                // handle focus change when field is entered
                cellControl.Enter += new EventHandler(this.UpdateGridFocusFromExternalControl);

                // set help text for control
                PetraUtilsObject.SetStatusBarText(cellControl, ADataLabelRow.Description);
            }

            // check if value is editable
            if (!ADataLabelRow.Editable)
            {
                FLocalDataLabelValuesGrid[ARowIndex, 1].Editor.EnableEdit = false;
            }
        }
        /// <summary>
        /// ApplyMotivationDetailCodeFilter
        /// </summary>
        private static void ApplyMotivationDetailCodeFilter(GiftBatchTDSAGiftDetailRow ACurrentDetailRow,
            GiftBatchTDS AMainDS,
            Int32 ALedgerNumber,
            TFrmPetraEditUtils APetraUtilsObject,
            TCmbAutoPopulated ACmbKeyMinistries,
            ref TCmbAutoPopulated ACmbMotivationDetailCode,
            TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey,
            Int64 ARecipientKey,
            TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber,
            TextBox ATxtDetailCostCentreCode,
            TextBox ATxtDetailAccountCode,
            TextBox ATxtDetailRecipientKeyMinistry,
            CheckBox AChkDetailTaxDeductible,
            TextBox ATxtDeductibleAccount,
            string AMotivationGroup,
            ref string AMotivationDetail,
            ref bool AMotivationDetailChangedFlag,
            bool AActiveOnly,
            bool ARecipientKeyChangingFlag,
            bool ACreatingNewGiftFlag,
            bool AInEditModeFlag,
            bool ABatchUnpostedFlag,
            bool ATaxDeductiblePercentageEnabledFlag,
            out bool ADoTaxUpdate)
        {
            //FMotivationbDetail will change by next process
            string motivationDetail = AMotivationDetail;

            string AutoPopComment;

            TFinanceControls.ChangeFilterMotivationDetailList(ref ACmbMotivationDetailCode, AMotivationGroup);
            AMotivationDetail = motivationDetail;

            if (AMotivationDetail.Length > 0)
            {
                ACmbMotivationDetailCode.SetSelectedString(AMotivationDetail);
                ACmbMotivationDetailCode.Text = AMotivationDetail;
                ADoTaxUpdate = false;
            }
            else if (ACmbMotivationDetailCode.Count > 0)
            {
                ACmbMotivationDetailCode.SelectedIndex = 0;

                //Force refresh of label
                OnMotivationDetailChanged(ACurrentDetailRow,
                    AMainDS,
                    ALedgerNumber,
                    APetraUtilsObject,
                    ACmbKeyMinistries,
                    ACmbMotivationDetailCode,
                    ATxtDetailRecipientKey,
                    ARecipientKey,
                    AtxtDetailRecipientLedgerNumber,
                    ATxtDetailCostCentreCode,
                    ATxtDetailAccountCode,
                    ATxtDetailRecipientKeyMinistry,
                    AChkDetailTaxDeductible,
                    ATxtDeductibleAccount,
                    AMotivationGroup,
                    ref AMotivationDetail,
                    ref AMotivationDetailChangedFlag,
                    ARecipientKeyChangingFlag,
                    ACreatingNewGiftFlag,
                    AInEditModeFlag,
                    ABatchUnpostedFlag,
                    ATaxDeductiblePercentageEnabledFlag,
                    false,
                    out ADoTaxUpdate,
                    out AutoPopComment);
            }
            else
            {
                ACmbMotivationDetailCode.SelectedIndex = -1;
                //Force refresh of label
                OnMotivationDetailChanged(ACurrentDetailRow,
                    AMainDS,
                    ALedgerNumber,
                    APetraUtilsObject,
                    ACmbKeyMinistries,
                    ACmbMotivationDetailCode,
                    ATxtDetailRecipientKey,
                    ARecipientKey,
                    AtxtDetailRecipientLedgerNumber,
                    ATxtDetailCostCentreCode,
                    ATxtDetailAccountCode,
                    ATxtDetailRecipientKeyMinistry,
                    AChkDetailTaxDeductible,
                    ATxtDeductibleAccount,
                    AMotivationGroup,
                    ref AMotivationDetail,
                    ref AMotivationDetailChangedFlag,
                    ARecipientKeyChangingFlag,
                    ACreatingNewGiftFlag,
                    AInEditModeFlag,
                    ABatchUnpostedFlag,
                    ATaxDeductiblePercentageEnabledFlag,
                    false,
                    out ADoTaxUpdate,
                    out AutoPopComment);
            }

            RetrieveMotivationDetailAccountCode(AMainDS, ALedgerNumber, ATxtDetailAccountCode, ATxtDeductibleAccount,
                AMotivationGroup, AMotivationDetail, ATaxDeductiblePercentageEnabledFlag);

            if ((ATxtDetailRecipientKey.Text == string.Empty) || (Convert.ToInt64(ATxtDetailRecipientKey.Text) == 0))
            {
                ATxtDetailRecipientKey.Text = String.Format("{0:0000000000}", 0);
                RetrieveMotivationDetailCostCentreCode(AMainDS, ALedgerNumber, ATxtDetailCostCentreCode, AMotivationGroup, AMotivationDetail);
            }
        }
        /// <summary>
        /// This function fills the combobox for the key ministry depending on the partnerkey
        /// </summary>
        /// <param name="ACmbMinistry"></param>
        /// <param name="ATxtField"></param>
        /// <param name="APartnerKey"></param>
        /// <param name="ARefreshData"></param>
        public static void GetRecipientData(ref TCmbAutoPopulated ACmbMinistry,
            ref TtxtAutoPopulatedButtonLabel ATxtField,
            System.Int64 APartnerKey,
            Boolean ARefreshData = false)
        {
            GetRecipientData(ref ACmbMinistry, APartnerKey, out FFieldNumber, ARefreshData);

            if (Convert.ToInt64(ATxtField.Text) != FFieldNumber)
            {
                ATxtField.Text = FFieldNumber.ToString();
            }
        }
        /// <summary>
        /// SetKeyMinistryTextBoxInvisible
        /// </summary>
        public static void SetKeyMinistryTextBoxInvisible(GiftBatchTDSAGiftDetailRow ACurrentDetailRow,
            GiftBatchTDS AMainDS,
            Int32 ALedgerNumber,
            TFrmPetraEditUtils APetraUtilsObject,
            TCmbAutoPopulated ACmbKeyMinistries,
            ref TCmbAutoPopulated ACmbMotivationDetailCode,
            TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey,
            Int64 ARecipientKey,
            TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber,
            TextBox ATxtDetailCostCentreCode,
            TextBox ATxtDetailAccountCode,
            TextBox ATxtDetailRecipientKeyMinistry,
            CheckBox AChkDetailTaxDeductible,
            TextBox ATxtDeductibleAccount,
            string AMotivationGroup,
            ref string AMotivationDetail,
            ref bool AMotivationDetailChangedFlag,
            bool AActiveOnly,
            bool ARecipientKeyChangingFlag,
            bool ACreatingNewGiftFlag,
            bool AInEditModeFlag,
            bool ABatchUnpostedFlag,
            bool ATaxDeductiblePercentageEnabledFlag,
            out bool ADoTaxUpdate)
        {
            if (ATxtDetailRecipientKeyMinistry.Visible)
            {
                ApplyMotivationDetailCodeFilter(ACurrentDetailRow,
                    AMainDS,
                    ALedgerNumber,
                    APetraUtilsObject,
                    ACmbKeyMinistries,
                    ref ACmbMotivationDetailCode,
                    ATxtDetailRecipientKey,
                    ARecipientKey,
                    AtxtDetailRecipientLedgerNumber,
                    ATxtDetailCostCentreCode,
                    ATxtDetailAccountCode,
                    ATxtDetailRecipientKeyMinistry,
                    AChkDetailTaxDeductible,
                    ATxtDeductibleAccount,
                    AMotivationGroup,
                    ref AMotivationDetail,
                    ref AMotivationDetailChangedFlag,
                    AActiveOnly,
                    ARecipientKeyChangingFlag,
                    ACreatingNewGiftFlag,
                    AInEditModeFlag,
                    ABatchUnpostedFlag,
                    ATaxDeductiblePercentageEnabledFlag,
                    out ADoTaxUpdate);

                PopulateKeyMinistry(ACurrentDetailRow, ACmbKeyMinistries, ATxtDetailRecipientKey, AtxtDetailRecipientLedgerNumber, false);

                ReconcileKeyMinistryFromTextbox(ACurrentDetailRow,
                    ACmbKeyMinistries,
                    ATxtDetailRecipientKeyMinistry,
                    AInEditModeFlag,
                    ABatchUnpostedFlag);

                //hide the overlay box during editing
                ATxtDetailRecipientKeyMinistry.Visible = false;
            }
            else
            {
                ADoTaxUpdate = false;
            }
        }
        /// <summary>
        /// GetRecipientData
        /// </summary>
        public static void GetRecipientData(GiftBatchTDSAGiftDetailRow ACurrentDetailRow,
            long APartnerKey,
            ref TCmbAutoPopulated ACmbKeyMinistries,
            TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey,
            ref TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber,
            bool AMotivationDetailChangedFlag)
        {
            if (APartnerKey == 0)
            {
                APartnerKey = Convert.ToInt64(ATxtDetailRecipientKey.Text);
            }

            // If this method has been called as a result of a change in motivation detail then txtDetailRecipientKey has not yet been set...
            // but we do know that the recipient must be a Unit.

            // if Family Recipient
            if (!AMotivationDetailChangedFlag && (ATxtDetailRecipientKey.CurrentPartnerClass == TPartnerClass.FAMILY))
            {
                AtxtDetailRecipientLedgerNumber.Text = ACurrentDetailRow.RecipientLedgerNumber.ToString();
                ACmbKeyMinistries.Clear();
                ACmbKeyMinistries.Enabled = false;
            }
            // if Unit Recipient
            else
            {
                TFinanceControls.GetRecipientData(ref ACmbKeyMinistries, ref AtxtDetailRecipientLedgerNumber, APartnerKey, true);

                // enable / disable combo box depending on whether it contains any key ministries
                if ((ACmbKeyMinistries.Table == null) || (ACmbKeyMinistries.Table.Rows.Count == 0))
                {
                    ACmbKeyMinistries.Enabled = false;
                }
                else
                {
                    ACmbKeyMinistries.Enabled = true;
                }
            }
        }
        /// <summary>
        /// Call when the motivation group changes
        /// </summary>
        public static void OnMotivationGroupChanged(GiftBatchTDSAGiftDetailRow AGiftBatchDetail,
            GiftBatchTDS AMainDS,
            Int32 ALedgerNumber,
            TFrmPetraEditUtils APetraUtilsObject,
            TCmbAutoPopulated ACmbKeyMinistries,
            TCmbAutoPopulated ACmbMotivationGroupCode,
            ref TCmbAutoPopulated ACmbMotivationDetailCode,
            TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey,
            Int64 ARecipientKey,
            TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber,
            TextBox ATxtDetailCostCentreCode,
            TextBox ATxtDetailAccountCode,
            TextBox ATxtDetailRecipientKeyMinistry,
            CheckBox AChkDetailTaxDeductible,
            TextBox ATxtDeductibleAccount,
            ref string AMotivationGroup,
            ref string AMotivationDetail,
            ref bool AMotivationDetailChangedFlag,
            bool AActiveOnly,
            bool ACreatingNewGiftFlag,
            bool ARecipientKeyChangingFlag,
            bool AInEditModeFlag,
            bool ABatchUnpostedFlag,
            bool ATaxDeductiblePercentageEnabledFlag,
            out bool ADoTaxUpdate)
        {
            if (!ABatchUnpostedFlag || APetraUtilsObject.SuppressChangeDetection || !AInEditModeFlag || ATxtDetailRecipientKeyMinistry.Visible)
            {
                ADoTaxUpdate = false;
                return;
            }

            AMotivationGroup = ACmbMotivationGroupCode.GetSelectedString();

            if (!ARecipientKeyChangingFlag)
            {
                AMotivationDetail = string.Empty;
            }

            ApplyMotivationDetailCodeFilter(AGiftBatchDetail,
                AMainDS,
                ALedgerNumber,
                APetraUtilsObject,
                ACmbKeyMinistries,
                ref ACmbMotivationDetailCode,
                ATxtDetailRecipientKey,
                ARecipientKey,
                AtxtDetailRecipientLedgerNumber,
                ATxtDetailCostCentreCode,
                ATxtDetailAccountCode,
                ATxtDetailRecipientKeyMinistry,
                AChkDetailTaxDeductible,
                ATxtDeductibleAccount,
                AMotivationGroup,
                ref AMotivationDetail,
                ref AMotivationDetailChangedFlag,
                AActiveOnly,
                ARecipientKeyChangingFlag,
                ACreatingNewGiftFlag,
                AInEditModeFlag,
                ABatchUnpostedFlag,
                ATaxDeductiblePercentageEnabledFlag,
                out ADoTaxUpdate);
        }
        /// <summary>
        /// Call when the key ministry changes
        /// </summary>
        public static void OnKeyMinistryChanged(GiftBatchTDSAGiftDetailRow ACurrentDetailRow, TFrmPetraEditUtils APetraUtilsObject,
            TCmbAutoPopulated ACmbKeyMinistries, TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey, TextBox ATxtDetailRecipientKeyMinistry,
            bool ARecipientKeyChangingFlag, ref bool AInKeyMinistryChangingFlag)
        {
            if ((ACurrentDetailRow == null) || AInKeyMinistryChangingFlag || ARecipientKeyChangingFlag
                || APetraUtilsObject.SuppressChangeDetection || ATxtDetailRecipientKeyMinistry.Visible)
            {
                return;
            }

            string KeyMinistry = ACmbKeyMinistries.GetSelectedDescription();
            string RecipientKey = ACmbKeyMinistries.GetSelectedInt64().ToString();

            try
            {
                AInKeyMinistryChangingFlag = true;

                if (ACmbKeyMinistries.Count == 0)
                {
                    ACmbKeyMinistries.SelectedIndex = -1;

                    if (ATxtDetailRecipientKeyMinistry.Text != string.Empty)
                    {
                        ATxtDetailRecipientKeyMinistry.Text = string.Empty;
                    }
                }
                else
                {
                    // if key ministry has actually changed
                    if ((ATxtDetailRecipientKeyMinistry.Text != KeyMinistry)
                        || (ACurrentDetailRow.RecipientKeyMinistry != KeyMinistry))
                    {
                        ATxtDetailRecipientKeyMinistry.Text = KeyMinistry;
                        ACurrentDetailRow.RecipientKeyMinistry = KeyMinistry;
                    }

                    if (Convert.ToInt64(ATxtDetailRecipientKey.Text) != Convert.ToInt64(RecipientKey))
                    {
                        ATxtDetailRecipientKey.Text = RecipientKey;
                    }
                }
            }
            finally
            {
                AInKeyMinistryChangingFlag = false;
            }
        }
        /// <summary>
        /// Modifies menu items depending on the Recipeint's Partner class
        /// </summary>
        public static void OnRecipientPartnerClassChanged(TPartnerClass? APartnerClass, TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey,
            TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber, out bool ? AEnableRecipientGiftDestination)
        {
            AEnableRecipientGiftDestination = null;

            string ItemText = Catalog.GetString("Open Recipient Gift Destination");

            if ((APartnerClass == TPartnerClass.UNIT) || (APartnerClass == null))
            {
                ATxtDetailRecipientKey.CustomContextMenuItemsVisibility(ItemText, false);
                AtxtDetailRecipientLedgerNumber.CustomContextMenuItemsVisibility(ItemText, false);
                AEnableRecipientGiftDestination = false;
            }
            else if (APartnerClass == TPartnerClass.FAMILY)
            {
                ATxtDetailRecipientKey.CustomContextMenuItemsVisibility(ItemText, true);
                AtxtDetailRecipientLedgerNumber.CustomContextMenuItemsVisibility(ItemText, true);
                AEnableRecipientGiftDestination = true;
            }
        }
        /// <summary>
        /// Call when the recipient key changes
        /// </summary>
        public static void OnRecipientKeyChanged(Int64 APartnerKey,
            String APartnerShortName,
            bool AValidSelection,
            GiftBatchTDSAGiftDetailRow ACurrentDetailRow,
            GiftBatchTDS AMainDS,
            Int32 ALedgerNumber,
            TFrmPetraEditUtils APetraUtilsObject,
            ref TCmbAutoPopulated ACmbKeyMinistries,
            TCmbAutoPopulated ACmbMotivationGroupCode,
            TCmbAutoPopulated ACmbMotivationDetailCode,
            TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey,
            TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber,
            TextBox ATxtDetailCostCentreCode,
            TextBox ATxtDetailAccountCode,
            TextBox ATxtDetailRecipientKeyMinistry,
            CheckBox AChkDetailTaxDeductible,
            TextBox ATxtDeductibleAccount,
            ref string AMotivationGroup,
            ref string AMotivationDetail,
            bool AShowingDetailsFlag,
            ref bool ARecipientKeyChangingFlag,
            bool AInKeyMinistryChangingFlag,
            bool AInEditModeFlag,
            bool ABatchUnpostedFlag,
            bool AMotivationDetailChangedFlag,
            bool ATaxDeductiblePercentageEnabledFlag,
            bool ACreatingNewGiftFlag,
            bool AActiveOnly,
            out bool ADoValidateGiftDestination,
            out bool ADoTaxUpdate)
        {
            ADoValidateGiftDestination = false;
            ADoTaxUpdate = false;

            if (ARecipientKeyChangingFlag || APetraUtilsObject.SuppressChangeDetection || AShowingDetailsFlag)
            {
                return;
            }

            ARecipientKeyChangingFlag = true;
            ATxtDetailRecipientKeyMinistry.Text = string.Empty;

            try
            {
                ACurrentDetailRow.RecipientKey = APartnerKey;
                ACurrentDetailRow.RecipientDescription = APartnerShortName;

                // do not want to update motivation comboboxes if recipient key is being changed due to a new gift or the motivation detail being changed
                if (!AMotivationDetailChangedFlag && !ACreatingNewGiftFlag
                    && TRemote.MFinance.Gift.WebConnectors.GetMotivationGroupAndDetail(APartnerKey, ref AMotivationGroup, ref AMotivationDetail))
                {
                    if (AMotivationGroup != ACmbMotivationGroupCode.GetSelectedString())
                    {
                        // note - this will also update the Motivation Detail
                        ACmbMotivationGroupCode.SetSelectedString(AMotivationGroup);
                    }

                    if (AMotivationDetail != ACmbMotivationDetailCode.GetSelectedString())
                    {
                        ACmbMotivationDetailCode.SetSelectedString(AMotivationDetail);
                    }

                    ACurrentDetailRow.MotivationGroupCode = AMotivationGroup;
                    ACurrentDetailRow.MotivationDetailCode = AMotivationDetail;
                }

                APetraUtilsObject.SuppressChangeDetection = true;

                //Set RecipientLedgerNumber
                if (APartnerKey > 0)
                {
                    ACurrentDetailRow.RecipientLedgerNumber = TRemote.MFinance.Gift.WebConnectors.GetRecipientFundNumber(APartnerKey,
                        ACurrentDetailRow.DateEntered);
                }
                else
                {
                    ACurrentDetailRow.RecipientLedgerNumber = 0;
                }

                if (!AInKeyMinistryChangingFlag)
                {
                    GetRecipientData(ACurrentDetailRow,
                        APartnerKey,
                        ref ACmbKeyMinistries,
                        ATxtDetailRecipientKey,
                        ref AtxtDetailRecipientLedgerNumber,
                        AMotivationDetailChangedFlag);
                    ADoValidateGiftDestination = true;
                }

                if (APartnerKey > 0)
                {
                    RetrieveRecipientCostCentreCode(ACurrentDetailRow, ATxtDetailCostCentreCode);
                }
                else
                {
                    UpdateRecipientKeyText(APartnerKey, ACurrentDetailRow, AMotivationGroup, AMotivationDetail);
                    RetrieveMotivationDetailCostCentreCode(AMainDS, ALedgerNumber, ATxtDetailCostCentreCode, AMotivationGroup, AMotivationDetail);
                }

                if (ATaxDeductiblePercentageEnabledFlag)
                {
                    ADoTaxUpdate = true;
                }
            }
            finally
            {
                ARecipientKeyChangingFlag = false;
                ReconcileKeyMinistryFromCombo(ACurrentDetailRow,
                    ACmbKeyMinistries,
                    ATxtDetailRecipientKeyMinistry,
                    AInEditModeFlag,
                    ABatchUnpostedFlag);

                APetraUtilsObject.SuppressChangeDetection = false;
            }
        }
        /// <summary>
        /// Call when the Motivation Detail changes
        /// </summary>
        public static void OnMotivationDetailChanged(GiftBatchTDSAGiftDetailRow ACurrentDetailRow,
            GiftBatchTDS AMainDS,
            Int32 ALedgerNumber,
            TFrmPetraEditUtils APetraUtilsObject,
            TCmbAutoPopulated ACmbKeyMinistries,
            TCmbAutoPopulated ACmbMotivationDetailCode,
            TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey,
            Int64 ARecipientKey,
            TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber,
            TextBox ATxtDetailCostCentreCode,
            TextBox ATxtDetailAccountCode,
            TextBox ATxtDetailRecipientKeyMinistry,
            CheckBox AChkDetailTaxDeductible,
            TextBox ATxtDeductibleAccount,
            string AMotivationGroup,
            ref string AMotivationDetail,
            ref bool AMotivationDetailChangedFlag,
            bool ARecipientKeyChangingFlag,
            bool ACreatingNewGiftFlag,
            bool AInEditModeFlag,
            bool ABatchUnpostedFlag,
            bool ATaxDeductiblePercentageEnabledFlag,
            bool AAutoPopulatingGift,
            out bool ADoTaxUpdate,
            out string AAutoPopComment)
        {
            ADoTaxUpdate = false;
            AAutoPopComment = null;

            if (!ABatchUnpostedFlag || !AInEditModeFlag || ATxtDetailRecipientKeyMinistry.Visible)
            {
                return;
            }

            Int64 MotivationRecipientKey = 0;
            AMotivationDetail = ACmbMotivationDetailCode.GetSelectedString();

            if (AMotivationDetail.Length > 0)
            {
                AMotivationDetailRow motivationDetail = (AMotivationDetailRow)AMainDS.AMotivationDetail.Rows.Find(
                    new object[] { ALedgerNumber, AMotivationGroup, AMotivationDetail });

                ACmbMotivationDetailCode.RefreshLabel();

                if (motivationDetail != null)
                {
                    RetrieveMotivationDetailAccountCode(motivationDetail,
                        ATxtDetailAccountCode,
                        ATxtDeductibleAccount,
                        ATaxDeductiblePercentageEnabledFlag);

                    MotivationRecipientKey = motivationDetail.RecipientKey;

                    // if motivation detail autopopulation is set to true
                    if (motivationDetail.Autopopdesc)
                    {
                        AAutoPopComment = motivationDetail.MotivationDetailDesc;
                    }

                    // set tax deductible checkbox if motivation detail has been changed by the user (i.e. not a row change)
                    if (!APetraUtilsObject.SuppressChangeDetection || ARecipientKeyChangingFlag)
                    {
                        AChkDetailTaxDeductible.Checked = motivationDetail.TaxDeductible;
                    }

                    if (ATaxDeductiblePercentageEnabledFlag)
                    {
                        if (string.IsNullOrEmpty(motivationDetail.TaxDeductibleAccount))
                        {
                            MessageBox.Show(Catalog.GetString("This Motivation Detail does not have an associated Tax Deductible Account. " +
                                    "This can be added in Finance / Setup / Motivation Details.\n\n" +
                                    "Unless this is changed it will be impossible to assign a Tax Deductible Percentage to this gift."),
                                Catalog.GetString("Incomplete Motivation Detail"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }

                        ADoTaxUpdate = true;
                    }
                }
                else
                {
                    AChkDetailTaxDeductible.Checked = false;
                }
            }

            if (!ACreatingNewGiftFlag && !AAutoPopulatingGift && (MotivationRecipientKey > 0))
            {
                AMotivationDetailChangedFlag = true;
                PopulateKeyMinistry(ACurrentDetailRow,
                    ACmbKeyMinistries,
                    ATxtDetailRecipientKey,
                    AtxtDetailRecipientLedgerNumber,
                    AMotivationDetailChangedFlag,
                    MotivationRecipientKey);
                AMotivationDetailChangedFlag = false;
            }
            else if (ARecipientKey == 0)
            {
                UpdateRecipientKeyText(0, ACurrentDetailRow, AMotivationGroup, AMotivationDetail);
            }

            if (ARecipientKey == 0)
            {
                RetrieveMotivationDetailCostCentreCode(AMainDS, ALedgerNumber, ATxtDetailCostCentreCode, AMotivationGroup, AMotivationDetail);
            }
            else
            {
                string NewCCCode = TRemote.MFinance.Gift.WebConnectors.RetrieveCostCentreCodeForRecipient(ALedgerNumber,
                    ARecipientKey,
                    ACurrentDetailRow.RecipientLedgerNumber,
                    ACurrentDetailRow.DateEntered,
                    AMotivationGroup,
                    AMotivationDetail);

                if (ATxtDetailCostCentreCode.Text != NewCCCode)
                {
                    ATxtDetailCostCentreCode.Text = NewCCCode;
                }
            }
        }
        /// <summary>
        /// Call from ShowDetailsManual after ACurrentDetailRow is known to be non-NULL
        /// </summary>
        public static void FinishShowDetailsManual(GiftBatchTDSAGiftDetailRow ACurrentDetailRow, TCmbAutoPopulated ACmbMotivationDetailCode,
            TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey, TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber,
            TextBox ATxtDetailCostCentreCode, TextBox ATxtDetailAccountCode, ref string AMotivationGroup, ref string AMotivationDetail,
            out bool ? AEnableRecipientGiftDestination)
        {
            AEnableRecipientGiftDestination = null;

            //Record current values for motivation
            AMotivationGroup = ACurrentDetailRow.MotivationGroupCode;
            AMotivationDetail = ACurrentDetailRow.MotivationDetailCode;

            if (ACurrentDetailRow.IsCostCentreCodeNull())
            {
                ATxtDetailCostCentreCode.Text = string.Empty;
            }
            else
            {
                ATxtDetailCostCentreCode.Text = ACurrentDetailRow.CostCentreCode;
            }

            if (ACurrentDetailRow.IsAccountCodeNull())
            {
                ATxtDetailAccountCode.Text = string.Empty;
            }
            else
            {
                ATxtDetailAccountCode.Text = ACurrentDetailRow.AccountCode;
            }

            if (ACurrentDetailRow.IsRecipientKeyNull())
            {
                ATxtDetailRecipientKey.Text = String.Format("{0:0000000000}", 0);
                UpdateRecipientKeyText(0, ACurrentDetailRow, AMotivationGroup, AMotivationDetail);
            }
            else
            {
                ATxtDetailRecipientKey.Text = String.Format("{0:0000000000}", ACurrentDetailRow.RecipientKey);
                UpdateRecipientKeyText(ACurrentDetailRow.RecipientKey, ACurrentDetailRow, AMotivationGroup, AMotivationDetail);
            }

            if (Convert.ToInt64(ATxtDetailRecipientKey.Text) == 0)
            {
                OnRecipientPartnerClassChanged(null, ATxtDetailRecipientKey, AtxtDetailRecipientLedgerNumber, out AEnableRecipientGiftDestination);
            }

            if (Convert.ToInt64(AtxtDetailRecipientLedgerNumber.Text) == 0)
            {
                OnRecipientPartnerClassChanged(ATxtDetailRecipientKey.CurrentPartnerClass,
                    ATxtDetailRecipientKey,
                    AtxtDetailRecipientLedgerNumber,
                    out AEnableRecipientGiftDestination);
            }
        }
        /// <summary>
        /// PopulateKeyMinistry
        /// </summary>
        private static void PopulateKeyMinistry(GiftBatchTDSAGiftDetailRow ACurrentDetailRow,
            TCmbAutoPopulated ACmbKeyMinistries,
            TtxtAutoPopulatedButtonLabel ATxtDetailRecipientKey,
            TtxtAutoPopulatedButtonLabel AtxtDetailRecipientLedgerNumber,
            bool AMotivationDetailChangedFlag,
            long APartnerKey = 0)
        {
            ACmbKeyMinistries.Clear();

            if (APartnerKey == 0)
            {
                APartnerKey = Convert.ToInt64(ATxtDetailRecipientKey.Text);

                if (APartnerKey == 0)
                {
                    return;
                }
            }

            GetRecipientData(ACurrentDetailRow,
                APartnerKey,
                ref ACmbKeyMinistries,
                ATxtDetailRecipientKey,
                ref AtxtDetailRecipientLedgerNumber,
                AMotivationDetailChangedFlag);
        }