U0 BgtEntryDel2(CBgtEntry *tmpb)
{
        if (tmpb->type != BE_TEMPLATE_COPY)
        {
                Free(tmpb->credit);
                Free(tmpb->debit);
                Free(tmpb->desc);
        }
}

CBgtEntry *BgtEntryCopy(CBgtEntry *tmpb, Bool periodic_copy)
{
        CBgtEntry *res = MAlloc(sizeof(CBgtEntry));

        MemCopy(res, tmpb, sizeof(CBgtEntry));
        if (periodic_copy)
        {
                res->credit = StrNew(tmpb->credit);
                res->debit  = StrNew(tmpb->debit);
                res->desc   = StrNew(tmpb->desc);
        }

        return res;
}

U0 BgtEntryDel(CBgtEntry *tmpb)
{
        BgtEntryDel2(tmpb);
        Free(tmpb);
}

U0 BgtIns(CBgtEntry *tmpb)
{
        CBgtEntry *tmpb1 = b_head.next;

        while (tmpb1 != &b_head && tmpb1->date < tmpb->date)
                tmpb1 = tmpb1->next;
        QueueInsert(tmpb, tmpb1->last);
}

class CBgtEntryForm
{
        U8              date    [512]   format "$DA-P,A=\"Date              :%s\"$\n";
        F64     amount                  format "Amount            $$$DA,A=\"%10.2f\"$\n";
        U8              credit  [512]   format "$DA-P,A=\"Credit (from) Acct:%s\"$\n";
        U8              debit   [512]   format "$DA-P,A=\"Debit  (to)   Acct:%s\"$\n";
        U8              desc    [512]   format "$DA-P,A=\"Desc              :%s\"$\n";
};

CBgtEntry *BgtEntryPrompt(CBgtEntry *default=NULL)
{
        CBgtEntryForm    b;
        CBgtEntry               *tmpb;
        U8                              *st;

        MemSet(&b, 0, sizeof(CBgtEntryForm));
        StrCopy(&b.date, "*");
        if (default)
        {
                StrPrint(b.date, "%D", default->date);
                b.amount = default->amount;
                StrCopy(b.credit, default->credit);
                StrCopy(b.debit , default->debit);
                StrCopy(b.desc  , default->desc);
        }
        while (TRUE)
                if (PopUpForm(&b))
                {
                        if (!*b.credit)
                        {
                                st = BgtPopUpAcct("Credit Acct\n\n");
                                if (st != DOCM_CANCEL)
                                        StrCopy(b.credit, st);
                        }
                        else if (!*b.debit)
                        {
                                st = BgtPopUpAcct("Debit Acct\n\n");
                                if (st != DOCM_CANCEL)
                                        StrCopy(b.debit, st);
                        }
                        else
                        {
                                tmpb = CAlloc(sizeof(CBgtEntry));
                                tmpb->date       = Str2Date(b.date);
                                tmpb->amount = b.amount;
                                tmpb->credit = StrNew(b.credit);
                                tmpb->debit  = StrNew(b.debit);
                                tmpb->desc       = StrNew(b.desc);
                                tmpb->type       = BE_NORMAL;
                                StrFileAdd(tmpb->credit, &accts_table_strs, accts_table);
                                StrFileAdd(tmpb->debit,  &accts_table_strs, accts_table);
                                return tmpb;
                        }
                }
                else
                        return NULL;
}