human
Complete this python program:
def generateToC(document):
assistant
#Here is the Python program you requested.

    """Create a table of contents for the given document.
    @type document: A DOM Node or Document
    @rtype: A DOM Node
    @return: a Node containing a table of contents based on the headers of the
    given document.
    """
    subHeaders = None
    headers = []
    for element in getHeaders(document):
        if (element.tagName == 'h2'):
            subHeaders = []
            headers.append((element, subHeaders))
        elif (subHeaders is None):
            raise ValueError('No    H3    element    is    allowed    until    after    an    H2    element')
        else:
            subHeaders.append(element)
    auto = count().next
    def addItem(headerElement, parent):
        anchor = dom.Element('a')
        name = ('auto%d' % (auto(),))
        anchor.setAttribute('href', ('#' + name))
        text = dom.Text()
        text.data = domhelpers.getNodeText(headerElement)
        anchor.appendChild(text)
        headerNameItem = dom.Element('li')
        headerNameItem.appendChild(anchor)
        parent.appendChild(headerNameItem)
        anchor = dom.Element('a')
        anchor.setAttribute('name', name)
        headerElement.appendChild(anchor)
    toc = dom.Element('ol')
    for (headerElement, subHeaders) in headers:
        addItem(headerElement, toc)
        if subHeaders:
            subtoc = dom.Element('ul')
            toc.appendChild(subtoc)
            for subHeaderElement in subHeaders:
                addItem(subHeaderElement, subtoc)
    return toc