///////////////////////////////////////////////////////////////////////
        //   Internal Events
        ///////////////////////////////////////////////////////////////////////
        private void AccountTLV_AfterSelect(object sender, TreeViewEventArgs e)
        {
            int accountID  = -1;
            int envelopeID = -1;

            MyNodes nodeType = (this.accTLV.FocusedNode as BaseNode).NodeType;

            switch (nodeType)
            {
            case MyNodes.AENode:
                AENode aeNode = this.accTLV.FocusedNode as AENode;
                accountID  = aeNode.AccountID;
                envelopeID = aeNode.EnvelopeID;
                break;

            case MyNodes.Account:
                accountID = (this.accTLV.FocusedNode as AccountNode).AccountID;
                break;

            case MyNodes.Envelope:
                envelopeID = (this.accTLV.FocusedNode as EnvelopeNode).EnvelopeID;
                break;

            case MyNodes.Root:
            case MyNodes.AccountType:
            case MyNodes.EnvelopeGroup:
                break;
            }

            if (accountID != selectedAccountID || envelopeID != selectedEnvelopeID)
            {
                OnSelectedAccountEnvelopeChanged(new SelectedAccountEnvelopeChangedEventArgs(accountID, envelopeID));
            }
        }
        private void handleThisEnvelopeNode(EnvelopeNode envNode)
        {
            int envelopeID = envNode.EnvelopeID;

            foreach (var item in DBquery.getSubEnvelopeBalances(envelopeID))
            {
                AENode node = new AENode(item.ID, envelopeID, item.Name, item.SubBalance);
                node.ImageId = (int)NodeImage.Bank;
                envNode.Nodes.Add(node);
            }
        }
        private void handleThisAccountNode(AccountNode accNode)
        {
            int accountID = accNode.AccountID;

            if (accNode.Catagory == SpclAccountCat.ACCOUNT)
            {
                foreach (var item in DBquery.getSubAccountBalances(accountID))
                {
                    AENode node = new AENode(accountID, item.ID, item.Name, item.SubBalance);
                    node.ImageId = (int)NodeImage.Envelope;
                    accNode.Nodes.Add(node);
                }
            }

            this.setErrorFlag(accNode);
        }
        private bool updateBalanceRecurse(BaseNode pNode, int accountID, int envelopeID, decimal newAmount)
        {
            switch (pNode.NodeType)
            {
            case MyNodes.Root:
            case MyNodes.AccountType:
            case MyNodes.EnvelopeGroup:
                foreach (BaseNode child in pNode.Nodes)
                {
                    if (updateBalanceRecurse(child, accountID, envelopeID, newAmount))
                    {
                        return(true);
                    }
                }
                break;

            case MyNodes.Account:
                AccountNode aNode = pNode as AccountNode;
                if (aNode.AccountID == accountID)
                {
                    if (envelopeID == SpclEnvelope.NULL)
                    {
                        aNode.setBalance(newAmount);
                        return(true);
                    }
                    else
                    {
                        foreach (BaseNode child in pNode.Nodes)
                        {
                            if (updateBalanceRecurse(child, accountID, envelopeID, newAmount))
                            {
                                return(true);
                            }
                        }

                        // If we get here the AENode was not found so handle this Account Node.
                        bool open = aNode.Expanded;

                        aNode.Nodes.Clear();
                        this.handleThisAccountNode(aNode);

                        aNode.Expanded = open;
                        return(true);
                    }
                }
                break;

            case MyNodes.Envelope:
                EnvelopeNode eNode = pNode as EnvelopeNode;
                if (eNode.EnvelopeID == envelopeID)
                {
                    if (accountID == SpclAccount.NULL)
                    {
                        eNode.setBalance(newAmount);
                        return(true);
                    }
                    else
                    {
                        foreach (BaseNode child in pNode.Nodes)
                        {
                            if (updateBalanceRecurse(child, accountID, envelopeID, newAmount))
                            {
                                return(true);
                            }
                        }

                        // If we get here the AENode was not found so handle this Account Node.
                        bool open = eNode.Expanded;

                        eNode.Nodes.Clear();
                        this.handleThisEnvelopeNode(eNode);

                        eNode.Expanded = open;
                        return(true);
                    }
                }
                break;

            case MyNodes.AENode:
                AENode aeNode = pNode as AENode;
                if (aeNode.EnvelopeID == envelopeID && aeNode.AccountID == accountID)
                {
                    if (newAmount == 0.00m)
                    {
                        // If we get here delete this node because it is zero balance.
                        // To do that in a simple way just return false and the previous iteration
                        // will think we didn't find the aeNode and do a update of all the aeNode.
                        // which will be the same as deleting it here.
                        return(false);
                    }
                    else
                    {
                        aeNode.setBalance(newAmount);
                    }

                    return(true);
                }
                break;
            }
            return(false);
        }