private void setErrorFlag(BaseNode node)
        {
            switch (node.NodeType)
            {
            case MyNodes.EnvelopeGroup:
            case MyNodes.Envelope:
            case MyNodes.AENode:
                break;

            case MyNodes.Root:
                RootNode rNode = node as RootNode;
                rNode.SetError(this.e_isCatagoryError(rNode.Catagory));
                foreach (BaseNode child in node.Nodes)
                {
                    setErrorFlag(child);
                }
                break;

            case MyNodes.AccountType:
                TypeNode tNode = node as TypeNode;
                tNode.SetError(this.e_isTypeError(tNode.Catagory, tNode.TypeID));
                foreach (BaseNode child in node.Nodes)
                {
                    setErrorFlag(child);
                }
                break;

            case MyNodes.Account:
                AccountNode aNode = node as AccountNode;
                aNode.SetError(this.e_isAccountError(aNode.AccountID));
                break;
            }
        }
        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 setErrorFlag(BaseNode node, int accountID, bool error)
        {
            switch (node.NodeType)
            {
            case MyNodes.EnvelopeGroup:
            case MyNodes.Envelope:
            case MyNodes.AENode:
                break;

            case MyNodes.Root:
                RootNode rNode = node as RootNode;
                rNode.SetError(this.e_isCatagoryError(rNode.Catagory));

                foreach (BaseNode child in node.Nodes)
                {
                    if (setErrorFlag(child, accountID, error))
                    {
                        return(true);
                    }
                }
                break;

            case MyNodes.AccountType:
                TypeNode tNode = node as TypeNode;
                tNode.SetError(this.e_isTypeError(tNode.Catagory, tNode.TypeID));

                foreach (BaseNode child in node.Nodes)
                {
                    if (setErrorFlag(child, accountID, error))
                    {
                        return(true);
                    }
                }
                break;

            case MyNodes.Account:
                AccountNode aNode = node as AccountNode;
                if (aNode.AccountID == accountID)
                {
                    aNode.SetError(error);
                    return(true);
                }
                break;
            }
            return(false);
        }
        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);
        }