} //dg_Detail_MouseClick()

        /*
         * Special menu case where the transaction has been created by another Source
         */
        private Boolean OfferDeleteMenu(ref DetailMenuStruct myDetailStr, ref ContextMenuStrip myMenu, ref ToolStripMenuItem mySubMenu)
        {
            // Local Variables
            Boolean RetVal = false;

            if (myDetailStr.Source.Length > 0 && myDetailStr.Journal.Length == 0)
            {
                mySubMenu          = new ToolStripMenuItem("Delete the Transactions that came from '" + myDetailStr.Source + "'");
                mySubMenu.Tag      = myDetailStr;
                mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                myMenu.Items.Add(mySubMenu);
                RetVal = true;
            }
            return(RetVal);
        } //OfferDeleteMenu()
        } // dg_Summary_CellClick()

        public static void dg_SummarySystemMenuItem_Click(object sender, MouseEventArgs e)
        {
            //
            // Purpose: A generic menu click event for the Detail Right-Click
            //

            // Local Variables
            ToolStripMenuItem ts_From     = (ToolStripMenuItem)sender;
            DetailMenuStruct  myDetailStr = (DetailMenuStruct)ts_From.Tag;

            switch (ts_From.Text)
            {
            case "Check 45 Day Rule":
                String mySql     = "Exec sp_Check_45_Day_Rule " + myDetailStr.TranID.ToString();
                String myMessage = SystemLibrary.SQLSelectString(mySql);
                if (myMessage.Length == 0)
                {
                    myMessage = "45 Day Rule checked and applied";
                }
                ((ProcessFundClosedTransactions)myDetailStr.myParentForm).LoadSummary();
                MessageBox.Show(myMessage, ts_From.Text);
                break;

            case "Move Franking Credit to another Sub-Fund(s)":
            case "Move Dividend to another Sub-Fund(s)":
            case "Apportion Interest to another Sub-Fund(s)":
                ProcessJournals frm = new ProcessJournals();
                frm.FromParent(myDetailStr.myParentForm, myDetailStr.TranID, myDetailStr.TranType, myDetailStr.EffectiveDate, myDetailStr.AccountID, myDetailStr.AccountType, myDetailStr.ParentFundID, myDetailStr.FundID, myDetailStr.PortfolioID, myDetailStr.Description, myDetailStr.Amount, myDetailStr.crncy);
                frm.Show();
                break;

            case "Reverse Interest Accrual Entry":
                ProcessJournals frm1 = new ProcessJournals();
                frm1.FromParent(myDetailStr.myParentForm, "Reverse Accrual", myDetailStr.TranID, myDetailStr.TranType, myDetailStr.EffectiveDate, myDetailStr.AccountID, myDetailStr.AccountType, myDetailStr.Description, myDetailStr.Amount, myDetailStr.crncy, myDetailStr.ParentFundID, myDetailStr.FundID, myDetailStr.PortfolioID);
                frm1.Show();
                break;

            case "General Journal":
            default:
                ProcessJournals frm2 = new ProcessJournals();
                frm2.FromParent(myDetailStr.myParentForm, "General Journal", myDetailStr.TranID, myDetailStr.TranType, myDetailStr.EffectiveDate, myDetailStr.AccountID, myDetailStr.AccountType, myDetailStr.Description, myDetailStr.Amount, myDetailStr.crncy, myDetailStr.ParentFundID, myDetailStr.FundID, myDetailStr.PortfolioID);
                frm2.Show();
                break;
            }
        } //dg_SummarySystemMenuItem_Click()
        } //dg_Summary_MouseClick()

        private void dg_Summary_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // Show the popup menu
            try
            {
                if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    Point  myLocation = new Point(this.Location.X + CXLocation, this.Location.Y + CYLocation);
                    String JournalID  = SystemLibrary.ToString(dg_Summary.Rows[e.RowIndex].Cells["JournalID"].Value);

                    ContextMenuStrip  myMenu    = new ContextMenuStrip();
                    ToolStripMenuItem mySubMenu = new ToolStripMenuItem();

                    DetailMenuStruct myDetailStr = new DetailMenuStruct();
                    myDetailStr.TranID        = SystemLibrary.ToInt32(dg_Summary.Rows[e.RowIndex].Cells["TranID"].Value);
                    myDetailStr.FundID        = SystemLibrary.ToInt32(dg_Summary.Rows[e.RowIndex].Cells["FundID"].Value);
                    myDetailStr.ParentFundID  = SystemLibrary.ToInt32(dg_Summary.Rows[e.RowIndex].Cells["ParentFundID"].Value);
                    myDetailStr.JournalID     = SystemLibrary.ToInt32(dg_Summary.Rows[e.RowIndex].Cells["JournalID"].Value);
                    myDetailStr.AccountID     = SystemLibrary.ToInt32(dg_Summary.Rows[e.RowIndex].Cells["AccountID"].Value);
                    myDetailStr.TranType      = SystemLibrary.ToString(dg_Summary.Rows[e.RowIndex].Cells["TranType"].Value);
                    myDetailStr.EffectiveDate = Convert.ToDateTime(dg_Summary.Rows[e.RowIndex].Cells["EffectiveDate"].Value);
                    myDetailStr.RecordDate    = Convert.ToDateTime(dg_Summary.Rows[e.RowIndex].Cells["RecordDate"].Value);
                    myDetailStr.Description   = SystemLibrary.ToString(dg_Summary.Rows[e.RowIndex].Cells["Description"].Value);
                    myDetailStr.AccountType   = SystemLibrary.ToString(dg_Summary.Rows[e.RowIndex].Cells["AccountType"].Value);
                    myDetailStr.Amount        = SystemLibrary.ToDouble(dg_Summary.Rows[e.RowIndex].Cells["Amount"].Value);
                    myDetailStr.crncy         = SystemLibrary.ToString(dg_Summary.Rows[e.RowIndex].Cells["crncy"].Value);
                    myDetailStr.PortfolioID   = SystemLibrary.ToString(dg_Summary.Rows[e.RowIndex].Cells["PortfolioID"].Value);
                    myDetailStr.myParentForm  = this;

                    // Create Menu
                    if (myDetailStr.TranType.ToUpper() == "FRANKING CREDIT")
                    {
                        mySubMenu          = new ToolStripMenuItem("Check 45 Day Rule");
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_SummarySystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);
                        if (myDetailStr.FundID != myDetailStr.ParentFundID)
                        {
                            mySubMenu          = new ToolStripMenuItem("Move Franking Credit to another Sub-Fund(s)");
                            mySubMenu.Tag      = myDetailStr;
                            mySubMenu.MouseUp += new MouseEventHandler(dg_SummarySystemMenuItem_Click);
                            myMenu.Items.Add(mySubMenu);
                        }
                        else
                        {
                            mySubMenu          = new ToolStripMenuItem("General Journal");
                            mySubMenu.Tag      = myDetailStr;
                            mySubMenu.MouseUp += new MouseEventHandler(dg_SummarySystemMenuItem_Click);
                            myMenu.Items.Add(mySubMenu);
                        }
                    }
                    else if (myDetailStr.TranType.ToUpper() == "DIVIDEND")
                    {
                        if (myDetailStr.FundID != myDetailStr.ParentFundID)
                        {
                            mySubMenu          = new ToolStripMenuItem("Move Dividend to another Sub-Fund(s)");
                            mySubMenu.Tag      = myDetailStr;
                            mySubMenu.MouseUp += new MouseEventHandler(dg_SummarySystemMenuItem_Click);
                            myMenu.Items.Add(mySubMenu);
                        }
                        else
                        {
                            mySubMenu          = new ToolStripMenuItem("General Journal");
                            mySubMenu.Tag      = myDetailStr;
                            mySubMenu.MouseUp += new MouseEventHandler(dg_SummarySystemMenuItem_Click);
                            myMenu.Items.Add(mySubMenu);
                        }
                    }
                    else if (myDetailStr.TranType.ToUpper() == "INTEREST")
                    {
                        if (myDetailStr.AccountType.ToUpper() == "ACCRUAL")
                        {
                            mySubMenu          = new ToolStripMenuItem("Reverse Interest Accrual Entry");
                            mySubMenu.Tag      = myDetailStr;
                            mySubMenu.MouseUp += new MouseEventHandler(dg_SummarySystemMenuItem_Click);
                            myMenu.Items.Add(mySubMenu);
                        }
                        if (myDetailStr.FundID != myDetailStr.ParentFundID)
                        {
                            mySubMenu = new ToolStripMenuItem("Apportion Interest to another Sub-Fund(s)");
                        }
                        else
                        {
                            mySubMenu = new ToolStripMenuItem("General Journal");
                        }
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_SummarySystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);
                    }
                    else
                    {
                        mySubMenu          = new ToolStripMenuItem("General Journal");
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_SummarySystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);
                    }

                    // Show the Menu
                    myMenu.Show(myLocation);
                }
            }
            catch { }
        } // dg_Summary_CellClick()
        } // dg_Detail_CellClick()

        public static void dg_DetailSystemMenuItem_Click(object sender, MouseEventArgs e)
        {
            //
            // Purpose: A generic menu click event for the Detail Right-Click
            //

            // Local Variables
            ToolStripMenuItem ts_From     = (ToolStripMenuItem)sender;
            DetailMenuStruct  myDetailStr = (DetailMenuStruct)ts_From.Tag;
            String            Reason      = "Deleted because {your text here}";

            if (ts_From.Text.StartsWith("Delete the Transactions that came from"))
            {
                if (SystemLibrary.InputBox(ts_From.Text, "You are about to remove a transactions sourced from '" + myDetailStr.Source + "'.\r\n" +
                                           "You will not be able to reverse this.\r\n\r\n" +
                                           "Press [Ok] to continue the deletion, or [Cancel] to abort.",
                                           ref Reason, ((ProcessUnallocatedTransactions)myDetailStr.myParentForm).validate_SaveAs, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    SystemLibrary.SQLExecute("Exec sp_RemoveTransaction " + myDetailStr.TranID.ToString() + ",'" + Reason + "'");
                    ((ProcessUnallocatedTransactions)myDetailStr.myParentForm).RefreshData();
                    MessageBox.Show("Transaction removed.");
                }
            }
            else if (ts_From.Text.Equals("Transfer Funds to another account of the same type"))
            {
                // Local Variables
                String mySql = "Select t.AccountID, a.AccountType, t.Amount, t.crncy, f.ParentFundID, t.PortfolioID " +
                               "From    Transactions t, " +
                               "        Accounts a, " +
                               "        Fund f " +
                               "Where   t.TranID = " + myDetailStr.TranID.ToString() + " " +
                               "And     a.AccountID = t.AccountID " +
                               "And     f.FundID = t.FundID";

                DataTable dt_Journal = SystemLibrary.SQLSelectToDataTable(mySql);

                if (dt_Journal.Rows.Count > 0)
                {
                    Int32  AccountID    = SystemLibrary.ToInt32(dt_Journal.Rows[0]["AccountID"]);
                    String AccountType  = SystemLibrary.ToString(dt_Journal.Rows[0]["AccountType"]);
                    Double Amount       = SystemLibrary.ToDouble(dt_Journal.Rows[0]["Amount"]);
                    String crncy        = SystemLibrary.ToString(dt_Journal.Rows[0]["crncy"]);
                    Int32  ParentFundID = SystemLibrary.ToInt32(dt_Journal.Rows[0]["ParentFundID"]);
                    Int32  PortfolioID  = SystemLibrary.ToInt32(dt_Journal.Rows[0]["PortfolioID"]);

                    ProcessJournals frm2 = new ProcessJournals();
                    frm2.FromParent(myDetailStr.myParentForm, "Between Accounts", myDetailStr.TranID, myDetailStr.TranType,
                                    myDetailStr.EffectiveDate, AccountID, AccountType, myDetailStr.Description,
                                    Amount, crncy, ParentFundID, myDetailStr.FundID, PortfolioID.ToString());
                    frm2.Show();
                }
            }
            else if (ts_From.Text.Equals("Create Other Side of Transfer to another account of the same type"))
            {
                // Local Variables
                String mySql = "Select t.AccountID, a.AccountType, t.Amount, t.crncy, f.ParentFundID, t.PortfolioID " +
                               "From    Transactions t, " +
                               "        Accounts a, " +
                               "        Fund f " +
                               "Where   t.TranID = " + myDetailStr.TranID.ToString() + " " +
                               "And     a.AccountID = t.AccountID " +
                               "And     f.FundID = t.FundID";

                DataTable dt_Journal = SystemLibrary.SQLSelectToDataTable(mySql);

                if (dt_Journal.Rows.Count > 0)
                {
                    Int32  AccountID    = SystemLibrary.ToInt32(dt_Journal.Rows[0]["AccountID"]);
                    String AccountType  = SystemLibrary.ToString(dt_Journal.Rows[0]["AccountType"]);
                    Double Amount       = SystemLibrary.ToDouble(dt_Journal.Rows[0]["Amount"]);
                    String crncy        = SystemLibrary.ToString(dt_Journal.Rows[0]["crncy"]);
                    Int32  ParentFundID = SystemLibrary.ToInt32(dt_Journal.Rows[0]["ParentFundID"]);
                    Int32  PortfolioID  = SystemLibrary.ToInt32(dt_Journal.Rows[0]["PortfolioID"]);

                    ProcessJournals frm2 = new ProcessJournals();
                    frm2.FromParent(myDetailStr.myParentForm, "Between Accounts - Other Half", myDetailStr.TranID, myDetailStr.TranType,
                                    myDetailStr.EffectiveDate, AccountID, AccountType, myDetailStr.Description,
                                    Amount, crncy, ParentFundID, myDetailStr.FundID, PortfolioID.ToString());
                    frm2.Show();
                }
            }
            else if (ts_From.Text.Equals("Manually Apportion Ticket Charges to sub accounts"))
            {
                // Local Variables
                String mySql = "Select t.AccountID, a.AccountType, t.Amount, t.crncy, f.ParentFundID, t.PortfolioID " +
                               "From    Transactions t, " +
                               "        Accounts a, " +
                               "        Fund f " +
                               "Where   t.TranID = " + myDetailStr.TranID.ToString() + " " +
                               "And     a.AccountID = t.AccountID " +
                               "And     f.FundID = t.FundID";

                DataTable dt_Journal = SystemLibrary.SQLSelectToDataTable(mySql);

                if (dt_Journal.Rows.Count > 0)
                {
                    Int32  AccountID    = SystemLibrary.ToInt32(dt_Journal.Rows[0]["AccountID"]);
                    String AccountType  = SystemLibrary.ToString(dt_Journal.Rows[0]["AccountType"]);
                    Double Amount       = SystemLibrary.ToDouble(dt_Journal.Rows[0]["Amount"]);
                    String crncy        = SystemLibrary.ToString(dt_Journal.Rows[0]["crncy"]);
                    Int32  ParentFundID = SystemLibrary.ToInt32(dt_Journal.Rows[0]["ParentFundID"]);
                    Int32  PortfolioID  = SystemLibrary.ToInt32(dt_Journal.Rows[0]["PortfolioID"]);

                    ProcessJournals frm2 = new ProcessJournals();
                    frm2.FromParent(myDetailStr.myParentForm, "Between Accounts - Manually Apportion Ticket Charges to sub accounts", myDetailStr.TranID, myDetailStr.TranType,
                                    myDetailStr.EffectiveDate, AccountID, AccountType, myDetailStr.Description,
                                    Amount, crncy, ParentFundID, myDetailStr.FundID, PortfolioID.ToString());
                    frm2.Show();
                }
            }
            else
            {
                switch (myDetailStr.TranType.ToUpper())
                {
                case "INTEREST":
                    InterestApportion frm = new InterestApportion();
                    frm.FromParent(myDetailStr.myParentForm, myDetailStr.TranID);
                    frm.Show();
                    break;

                case "STOCK LOAN FEE":
                    StockLoanFeeApportion frm1 = new StockLoanFeeApportion();
                    frm1.FromParent(myDetailStr.myParentForm, myDetailStr.TranID);
                    frm1.Show();
                    break;

                case "TICKET CHARGES":
                    String myMessage = SystemLibrary.SQLSelectString("Exec sp_ApportionTicketCharges " + myDetailStr.TranID.ToString() + " ");
                    ((ProcessUnallocatedTransactions)myDetailStr.myParentForm).RefreshData();
                    MessageBox.Show(myMessage, "Apportion Ticket Charges");
                    break;

                case "TRANSFER":
                    if (myDetailStr.Description.ToUpper() == "FUTURES MARGIN" || myDetailStr.Description.ToUpper() == "Day Profit + Future comm and charges".ToUpper())
                    {
                        if (ts_From.Text.StartsWith("Explain Future Margins movements for "))
                        {
                            FuturesExplainWireTransfer frm_Explain = new FuturesExplainWireTransfer();
                            frm_Explain.FromParent((ProcessUnallocatedTransactions)myDetailStr.myParentForm, myDetailStr.FundID, myDetailStr.EffectiveDate);
                            frm_Explain.Show();
                        }
                        else if (ts_From.Text.StartsWith("DO THIS FIRST - Update MISSING Future Margin Definition for "))
                        {
                            FutureMargins frm_FutMargin = new FutureMargins();
                            frm_FutMargin.FromParent(myDetailStr.myParentForm, true);
                            frm_FutMargin.Show();
                        }
                        else
                        {
                            SystemLibrary.SQLExecute("Exec sp_ReapplyMargin " + myDetailStr.FundID.ToString() + ", '" + myDetailStr.RecordDate.ToString("dd-MMM-yyyy") + "' ");
                            ((ProcessUnallocatedTransactions)myDetailStr.myParentForm).RefreshData();
                            MessageBox.Show("Futures Margins Reapplied");
                        }
                    }
                    break;
                }
            }
        } //dg_DetailSystemMenuItem_Click()
        } //OfferDeleteMenu()

        private void dg_Detail_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // Show the popup menu
            try
            {
                if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    Point  myLocation = new Point(this.Location.X + CXLocation, this.Location.Y + CYLocation);
                    String JournalID  = SystemLibrary.ToString(dg_Detail.Rows[e.RowIndex].Cells["JournalID"].Value);

                    ContextMenuStrip  myMenu    = new ContextMenuStrip();
                    ToolStripMenuItem mySubMenu = new ToolStripMenuItem();

                    DetailMenuStruct myDetailStr = new DetailMenuStruct();
                    myDetailStr.TranID        = SystemLibrary.ToInt32(dg_Detail.Rows[e.RowIndex].Cells["TranID"].Value);
                    myDetailStr.FundID        = SystemLibrary.ToInt32(dg_Detail.Rows[e.RowIndex].Cells["FundID"].Value);
                    myDetailStr.TranType      = SystemLibrary.ToString(dg_Detail.Rows[e.RowIndex].Cells["TranType"].Value);
                    myDetailStr.EffectiveDate = Convert.ToDateTime(dg_Detail.Rows[e.RowIndex].Cells["EffectiveDate"].Value);
                    myDetailStr.RecordDate    = Convert.ToDateTime(dg_Detail.Rows[e.RowIndex].Cells["RecordDate"].Value);
                    myDetailStr.Description   = SystemLibrary.ToString(dg_Detail.Rows[e.RowIndex].Cells["Description"].Value);
                    myDetailStr.Source        = SystemLibrary.ToString(dg_Detail.Rows[e.RowIndex].Cells["Source"].Value);
                    myDetailStr.Journal       = SystemLibrary.ToString(dg_Detail.Rows[e.RowIndex].Cells["Journal"].Value);
                    myDetailStr.myParentForm  = this;

                    // Create Menu
                    if (myDetailStr.TranType.ToUpper() == "INTEREST" && JournalID.Length == 0)
                    {
                        mySubMenu          = new ToolStripMenuItem("Apportion Interest Paid to sub accounts");
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);

                        OfferDeleteMenu(ref myDetailStr, ref myMenu, ref mySubMenu);
                    }
                    else if (myDetailStr.TranType.ToUpper() == "STOCK LOAN FEE" && JournalID.Length == 0)
                    {
                        mySubMenu          = new ToolStripMenuItem("Apportion Stock Loan Fee to sub accounts");
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);

                        OfferDeleteMenu(ref myDetailStr, ref myMenu, ref mySubMenu);
                    }
                    else if (myDetailStr.TranType.ToUpper() == "TICKET CHARGES" && JournalID.Length == 0)
                    {
                        mySubMenu          = new ToolStripMenuItem("Apportion Ticket Charges to sub accounts based on number of trades");
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);

                        myMenu.Items.Add("-");
                        mySubMenu          = new ToolStripMenuItem("Manually Apportion Ticket Charges to sub accounts");
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);

                        OfferDeleteMenu(ref myDetailStr, ref myMenu, ref mySubMenu);
                    }
                    else if (myDetailStr.TranType.ToUpper() == "TRANSFER" && JournalID.Length > 0 &&
                             (myDetailStr.Description.ToUpper() == "FUTURES MARGIN" || myDetailStr.Description.ToUpper() == "Day Profit + Future comm and charges".ToUpper())
                             )
                    {
                        /*
                         * See if there are any missing entries from the FuturesMargin Table
                         * sp_CreateMarginTransactionForTradeOutstanding
                         */
                        String myMissingTicker = SystemLibrary.SQLSelectString("Exec sp_MissingFutureMargins");
                        if (myMissingTicker.Length > 0)
                        {
                            mySubMenu          = new ToolStripMenuItem("DO THIS FIRST - Update MISSING Future Margin Definition for " + myMissingTicker);
                            mySubMenu.Tag      = myDetailStr;
                            mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                            myMenu.Items.Add(mySubMenu);
                        }

                        mySubMenu          = new ToolStripMenuItem("Rebuild Future Margins for " + myDetailStr.RecordDate.ToString("dd-MMM-yyyy"));
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);
                        mySubMenu          = new ToolStripMenuItem("Explain Future Margins movements for " + myDetailStr.EffectiveDate.ToString("dd-MMM-yyyy"));
                        mySubMenu.Tag      = myDetailStr;
                        mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                        myMenu.Items.Add(mySubMenu);
                    }
                    else if (myDetailStr.TranType.ToUpper() == "TRANSFER" && JournalID.Length == 0 &&
                             myDetailStr.Description.Length > 0
                             )
                    {
                        // This is a Transfer sent by the source. See if it matches a Capital movement.
                        String oldSql = "Select	count(*) " +
                                        "From	Transactions t1, "+
                                        "		Transactions t2, "+
                                        "		Fund f1, "+
                                        "		Fund f2 "+
                                        "Where	t1.TranID = "+ myDetailStr.TranID.ToString() + " " +
                                        "And		f1.FundID = t1.FundID "+
                                        "And		f2.ParentFundID = f1.ParentFundID "+
                                        "And		f2.FundID = t2.FundID "+
                                        "And		t2.AccountID = t1.AccountID "+
                                        "And		t2.EffectiveDate = t1.EffectiveDate "+
                                        "And		t2.Amount = t1.Amount "+
                                        "And		t2.TranType = 'Capital' "+
                                        "And		t1.TranType = 'Transfer' "+
                                        "And     isNull(t1.Reconcilled,'N') = 'N' " +
                                        "And     isNull(t2.Reconcilled,'N') = 'N' ";

                        //               "						And		t2.Amount = t1.Amount  " +
                        String mySql = "Select Count(*) " +
                                       "From	Transactions t1 "+
                                       "Where	t1.TranID = "+ myDetailStr.TranID.ToString() + " " +
                                       "And     isNull(t1.Reconcilled,'N') = 'N'  " +
                                       "And		t1.TranType = 'Transfer'  "+
                                       "And		t1.Amount = (	Select	sum(t2.Amount) "+
                                       "						From	Transactions t2,  "+
                                       "								Fund f1,  "+
                                       "								Fund f2  "+
                                       "						Where	f1.FundID = t1.FundID  "+
                                       "						And		f2.ParentFundID = f1.ParentFundID  "+
                                       "						And		f2.FundID = t2.FundID  "+
                                       "						And		t2.AccountID = t1.AccountID  "+
                                       "						And		t2.EffectiveDate = t1.EffectiveDate  "+
                                       "						And		t2.TranType = 'Capital'  "+
                                       "						And     isNull(t2.Reconcilled,'N') = 'N'  "+
                                       "					)";
                        if (SystemLibrary.SQLSelectInt32(mySql) > 0)
                        {
                            // Found a Fund Capital inflow/outflow that matches this record, so offer a new window to process this
                            JournalCapitalMovement JCP = new JournalCapitalMovement();
                            JCP.FromParent(this, myDetailStr.TranID, SystemLibrary.ToDouble(dg_Detail.Rows[e.RowIndex].Cells["Amount"].Value));
                            JCP.ShowDialog();
                            RefreshData();

                            /*
                             * if (MessageBox.Show(this, "Found a Fund Capital inflow/outflow that matches this record.\r\n\r\n" +
                             *                        "Do you want to match these against each other?", myDetailStr.Description, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                             * {
                             *  mySql = "exec sp_Map_Capital_Movement " + myDetailStr.TranID.ToString() + " ";
                             *  SystemLibrary.SQLExecute(mySql);
                             *  RefreshData();
                             *  MessageBox.Show("Fund Capital inflow/outflow Reapplied");
                             * }
                             */
                        }
                        else
                        {
                            // See if there is another 'Cash' account that the funds could be transferred to.
                            // I have chosen not to offer Linked Accounts like the Futures Account as I assume that comes electronically?
                            mySql = "Select  Count(*) " +
                                    "From	Accounts, "+
                                    "		Transactions "+
                                    "Where	Transactions.TranID = "+ myDetailStr.TranID.ToString() + " " +
                                    "And		Accounts.FundID = Transactions.FundID "+
                                    "And		Accounts.AccountID <> Transactions.AccountID "+
                                    "And		Accounts.AccountType = (Select	a.AccountType "+
                                    "								From	Accounts a "+
                                    "								Where	a.FundID = Transactions.FundID "+
                                    "								And		a.AccountID = Transactions.AccountID "+
                                    "							   ) "+
                                    "And		isNull(Accounts.Linked_AccountID,-123456) <> Transactions.AccountID ";         // No linked account
                            if (SystemLibrary.SQLSelectInt32(mySql) > 0)
                            {
                                mySubMenu          = new ToolStripMenuItem("Transfer Funds to another account of the same type");
                                mySubMenu.Tag      = myDetailStr;
                                mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                                myMenu.Items.Add(mySubMenu);
                                mySubMenu          = new ToolStripMenuItem("Create Other Side of Transfer to another account of the same type");
                                mySubMenu.Tag      = myDetailStr;
                                mySubMenu.MouseUp += new MouseEventHandler(dg_DetailSystemMenuItem_Click);
                                myMenu.Items.Add(mySubMenu);
                            }

                            OfferDeleteMenu(ref myDetailStr, ref myMenu, ref mySubMenu);
                        }
                    }
                    else
                    {
                        if (OfferDeleteMenu(ref myDetailStr, ref myMenu, ref mySubMenu))
                        {
                            myMenu.Show(myLocation);
                        }
                        return;
                    }

                    // Show the Menu
                    myMenu.Show(myLocation);
                }
            }
            catch { }
        } // dg_Detail_CellClick()