def comments():
    ' Function accessed by AJAX from discuss() to handle Comments '
    try:
        resourcename = request.args[0]
    except:
        raise HTTP(400)
    try:
        id = request.args[1]
    except:
        raise HTTP(400)
    if (resourcename == 'problem'):
        problem_id = id
        solution_id = None
    elif (resourcename == 'solution'):
        stable = s3db.delphi_solution
        query = (stable.id == id)
        solution = db(query).select(stable.problem_id, limitby=(0, 1)).first()
        if solution:
            problem_id = solution.problem_id
            solution_id = id
        else:
            raise HTTP(400)
    else:
        raise HTTP(400)
    table = s3db.delphi_comment
    field = table.problem_id
    field.default = problem_id
    field.writable = field.readable = False
    sfield = table.solution_id
    if solution_id:
        sfield.default = solution_id
        sfield.writable = sfield.readable = False
    else:
        sfield.label = T('Related to Solution (optional)')
        sfield.requires = IS_EMPTY_OR(IS_ONE_OF(db, 'delphi_solution.id', s3.delphi_solution_represent, filterby='problem_id', filter_opts=(problem_id,)))
    from gluon.tools import Crud
    form = Crud(db).create(table, formname=('delphi_%s/%s' % (resourcename, id)))
    if solution_id:
        comments = db((sfield == solution_id)).select(table.id, table.parent, table.body, table.created_by, table.created_on)
    else:
        comments = db((field == problem_id)).select(table.id, table.parent, table.solution_id, table.body, table.created_by, table.created_on)
    output = UL(_id='comments')
    for comment in comments:
        if (not comment.parent):
            thread = comment_parse(comment, comments, solution_id=solution_id)
            output.append(thread)
    script = "$('#comments').collapsible({xoffset:'-5',yoffset:'50',imagehide:img_path+'arrow-down.png',imageshow:img_path+'arrow-right.png',defaulthide:false})\n$('#delphi_comment_parent__row1').hide()\n$('#delphi_comment_parent__row').hide()\n$('#delphi_comment_body').ckeditor(ck_config)\n$('#submit_record__row input').click(function(){$('#comment-form').hide();$('#delphi_comment_body').ckeditorGet().destroy();return true;})"
    output = DIV(output, DIV(H4(T('New Post'), _id='comment-title'), form, _id='comment-form', _class='clear'), SCRIPT(script))
    return XML(output)
Function accessed by AJAX from discuss() to handle Comments
def comments():
    '  '
    try:
        resourcename = request.args[0]
    except:
        raise HTTP(400)
    try:
        id = request.args[1]
    except:
        raise HTTP(400)
    if (resourcename == 'problem'):
        problem_id = id
        solution_id = None
    elif (resourcename == 'solution'):
        stable = s3db.delphi_solution
        query = (stable.id == id)
        solution = db(query).select(stable.problem_id, limitby=(0, 1)).first()
        if solution:
            problem_id = solution.problem_id
            solution_id = id
        else:
            raise HTTP(400)
    else:
        raise HTTP(400)
    table = s3db.delphi_comment
    field = table.problem_id
    field.default = problem_id
    field.writable = field.readable = False
    sfield = table.solution_id
    if solution_id:
        sfield.default = solution_id
        sfield.writable = sfield.readable = False
    else:
        sfield.label = T('Related to Solution (optional)')
        sfield.requires = IS_EMPTY_OR(IS_ONE_OF(db, 'delphi_solution.id', s3.delphi_solution_represent, filterby='problem_id', filter_opts=(problem_id,)))
    from gluon.tools import Crud
    form = Crud(db).create(table, formname=('delphi_%s/%s' % (resourcename, id)))
    if solution_id:
        comments = db((sfield == solution_id)).select(table.id, table.parent, table.body, table.created_by, table.created_on)
    else:
        comments = db((field == problem_id)).select(table.id, table.parent, table.solution_id, table.body, table.created_by, table.created_on)
    output = UL(_id='comments')
    for comment in comments:
        if (not comment.parent):
            thread = comment_parse(comment, comments, solution_id=solution_id)
            output.append(thread)
    script = "$('#comments').collapsible({xoffset:'-5',yoffset:'50',imagehide:img_path+'arrow-down.png',imageshow:img_path+'arrow-right.png',defaulthide:false})\n$('#delphi_comment_parent__row1').hide()\n$('#delphi_comment_parent__row').hide()\n$('#delphi_comment_body').ckeditor(ck_config)\n$('#submit_record__row input').click(function(){$('#comment-form').hide();$('#delphi_comment_body').ckeditorGet().destroy();return true;})"
    output = DIV(output, DIV(H4(T('New Post'), _id='comment-title'), form, _id='comment-form', _class='clear'), SCRIPT(script))
    return XML(output)