#"""mems.ui.form.form_templates
#
#Provides PTL methods for the Form class in form.py.
#"""

from types import StringType
from quixote.html import value_quote, render_tag
 
template _render_start (self, request, action, method='post', name=None):
    render_tag('form', method=method, action=action, name=name)
    "\n"
    self._render_hidden_widgets(request)


template _render_finish (self, request):
    '</form>\n'
    self._render_javascript(request)
    
template _render_sep (self, text, line=1):
    ('<tr><td colspan="3">%s<strong><big>%s</big></strong></td></tr>\n' %
     (line and '<hr>' or '', text))

template _render_error (self, error):
    if error:
        '<font color="red">' + error + '</font><br />\n'
    
template _render_hint (self, hint):
    if hint:
        '<em>' + hint + '</em>\n'

template _render_widget_row (self, request, widget):
    if widget.widget_type == 'hidden':
        return
    title = self.title[widget.name] or ''
    if self.required.get(widget.name):
        title += '&nbsp;*'
    '<tr>'
    '<th colspan="3" align="left">'
    title
    '</th>\n'
    '</tr>'
    '<tr>'
    '<td>&nbsp;&nbsp;</td>'
    '<td>'
    widget.render(request)
    '</td>\n'
    '<td>'
    self._render_error(self.error.get(widget.name))
    self._render_hint(self.hint.get(widget.name))
    '</td>\n'
    '</tr>\n'


template _render_hidden_widgets (self, request):
    for widget in self.widget_order:
        if widget.widget_type == 'hidden':
            widget.render(request)
            self._render_error(self.error.get(widget.name))
            '\n'


template _render_submit_buttons (self, request, ncols=3):
    '<tr><td colspan="%d">\n' % ncols
    for button in self.submit_buttons:
        button.render(request)
        '\n'
    '</td></tr>'


template _render_visible_widgets (self, request):
    for widget in self.widget_order:
        self._render_widget_row(request, widget)


template _render_body (self, request):
    '<table>'
    if filter(None, self.required.values()):
        '<tr><td colspan="3"><b>*</b> = <em>required field</em></td></tr>\n'
    self._render_visible_widgets(request)
    self._render_submit_buttons(request)
    '</table>'


template render (self, request, action_url):
    assert type(action_url) is StringType
    self._render_start(request, action_url, self.method)
    self._render_body(request)
    self._render_finish(request)
