'create a html review form using the available data
@param s3ocrdict: output of self.__s3ocrxml2dict()
@param s3ocrdata: output of self.__importjob2data()
@return: html review form'

def __create_review_form(self, s3ocrdict, s3ocrdata):

    ptablecontent = []
    fieldnum = 1
    request = current.request
    T = current.T
    r = self.r
    setuuid = self.setuuid
    if r.component:
        request_args = request.get('args', ['', ''])
        record_id = request_args[0]
        component_name = request_args[1]
        urlprefix = ('%s/%s/%s' % (request.function, record_id, component_name))
    else:
        urlprefix = request.function
    for resourcename in s3ocrdict['$resource_seq']:
        resource = s3ocrdict[resourcename]
        ptablecontent.append(TR(TD(DIV(resourcename, _class='resource_name'), _colspan='4'), _class='titletr'))
        ctablecontent = []
        for fieldname in resource['$field_seq']:
            field = resource[fieldname]
            comment = (field.comment if field.comment else '')
            try:
                ocrdata = s3ocrdata[resourcename][fieldname]
                if ocrdata:
                    condition = (isinstance(ocrdata, str) or isinstance(ocrdata, int))
                    if condition:
                        value = str(ocrdata)
                    elif isinstance(ocrdata, date):
                        value = date.strftime(ocrdata, '%Y-%m-%d')
                    elif isinstance(ocrdata, datetime):
                        value = datetime.strftime(ocrdata, '%Y-%m-%d    %H:%M:%S')
                    else:
                        value = unicodedata.normalize('NFKD', ocrdata).encode('ascii', 'ignore')
                else:
                    value = ''
            except KeyError:
                value = ''
            name = ('%s-%s' % (resourcename, fieldname))
            if field.has_options:
                if (field.type == 'multiselect'):
                    if (field.options.count <= MAX_FORM_OPTIONS_LIMIT):
                        options = []
                        optct = 1
                        try:
                            value = value.split('|')[1:(-1)]
                        except:
                            value = [str(value)]
                        chk = (lambda m, n: ('on' if (str(m) in n) else None))
                        for option in field.options.list:
                            options.append(TD(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': fieldname, 'value': option.value}), _style='border:    solid    #333    1px;'), _style='text-align:center;'), TD(INPUT(_id=('%s-%s' % (name, optct)), _value=option.value, _type='checkbox', _class=('field-%s' % fieldnum), _name=name, value=chk(option.value, value))), TD(LABEL(option.label, _for=('%s-%s' % (name, optct)))))
                            optct += 1
                        input_area = TABLE(TR(options), _class=('field-%s' % fieldnum))
                    else:
                        for line in xrange(1, 3):
                            ctablecontent.append(TR(TD(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': fieldname, 'sequence': line}), _style='border:    solid    #333    1px;'), _style='text-align:center;    padding:5px;', _colspan='4')))
                        options = []
                        optct = 1
                        chk = (lambda m, n: ('on' if (str(m) in n) else None))
                        for option in field.options.list:
                            options.append(TR(TD(INPUT(_id=('%s-%s' % (name, optct)), _value=option.value, _type='checkbox', _class=('field-%s' % fieldnum), _name=name, value=chk(option.value, value))), TD(LABEL(option.label, _for=('%s-%s' % (name, optct))))))
                            optct += 1
                        input_area = TABLE(options, _class=('field-%s' % fieldnum))
                elif (field.type == 'boolean'):
                    options = []
                    optct = 1
                    chk = (lambda m, n: (m if (str(m) == str(n)) else None))
                    for option in [Storage({'value': 'yes', 'label': T('Yes')}), Storage({'value': 'no', 'label': T('No')})]:
                        options.append(TD(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': fieldname, 'value': option.value}), _style='border:    solid    #333    1px;'), _style='text-align:center;'), TD(INPUT(_id=('%s-%s' % (name, optct)), _value=option.value, _type='radio', _class=('field-%s' % fieldnum), _name=name, value=chk(option.value, value))), TD(LABEL(option.label, _for=('%s-%s' % (name, optct)))))
                        optct += 1
                    input_area = TABLE(TR(options), _class=('field-%s' % fieldnum))
                elif (field.options.count <= MAX_FORM_OPTIONS_LIMIT):
                    options = []
                    optct = 1
                    chk = (lambda m, n: (m if (str(m) == str(n)) else None))
                    for option in field.options.list:
                        options.append(TD(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': fieldname, 'value': option.value}), _style='border:    solid    #333    1px;'), _style='text-align:center;'), TD(INPUT(_id=('%s-%s' % (name, optct)), _value=option.value, _type='radio', _class=('field-%s' % fieldnum), _name=name, value=chk(option.value, value))), TD(LABEL(option.label, _for=('%s-%s' % (name, optct)))))
                        optct += 1
                    input_area = TABLE(TR(options), _class=('field-%s' % fieldnum))
                else:
                    for line in xrange(1, 3):
                        ctablecontent.append(TR(TD(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': fieldname, 'sequence': line}), _style='border:    solid    #333    1px;'), _style='text-align:center;    padding:5px;', _colspan='4')))
                    options = []
                    optct = 1
                    chk = (lambda m, n: (m if (str(m) == str(n)) else None))
                    for option in field.options.list:
                        options.append(TR(TD(INPUT(_id=('%s-%s' % (name, optct)), _value=option.value, _type='radio', _class=('field-%s' % fieldnum), _name=name, value=chk(option.value, value))), TD(LABEL(option.label, _for=('%s-%s' % (name, optct))))))
                        optct += 1
                    input_area = TABLE(options, _class=('field-%s' % fieldnum))
            elif (field.type in ['string', 'integer', 'double']):
                for line in xrange(1, (field.lines + 1)):
                    ctablecontent.append(TR(TD(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': fieldname, 'sequence': line}), _style='border:    solid    #333    1px;'), _style='text-align:center;    padding:5px;', _colspan='4')))
                input_area = INPUT(_id=('%s-id' % name.replace('-', '_')), _class=('field-%s' % fieldnum), _value=value, _name=name)
            elif (field.type == 'date'):
                subsec = {'DD': 1, 'MO': 2, 'YYYY': 3}
                imglist = []
                for sec in ['YYYY', 'MO', 'DD']:
                    imglist.append(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': field, 'sequence': subsec[sec]}), _style='border:    solid    #333    1px;'))
                ctablecontent.append(TR(TD(imglist, _style='text-align:center;    padding:5px;', _colspan='4')))
                try:
                    value = value.strftime('%Y-%m-%d')
                except AttributeError:
                    try:
                        value = datetime.strptime(value, '%Y-%m-%d')
                        value = value.strftime('%Y-%m-%d')
                    except ValueError:
                        value = ''
                input_area = INPUT(_id=('%s-id' % name.replace('-', '_')), _class=('field-%s    date' % fieldnum), _value=value, _name=name)
            elif (field.type == 'datetime'):
                subsec = {'HH': 1, 'MM': 2, 'DD': 3, 'MO': 4, 'YYYY': 5}
                imglist = []
                for eachsec in ['YYYY', 'MO', 'DD', 'HH', 'MM']:
                    imglist.append(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': fieldname, 'sequence': subsec[eachsec]}), _style='border:    solid    #333    1px;'))
                ctablecontent.append(TR(TD(imglist, _style='text-align:center;    padding:5px;', _colspan='4')))
                try:
                    value = value.strftime('%Y-%m-%d    %H:%M:%S')
                except AttributeError:
                    try:
                        value = datetime.strptime(value, '%Y-%m-%d    %H:%M:%S')
                        value = value.strftime('%Y-%m-%d    %H:%M:%S')
                    except ValueError:
                        value = ''
                input_area = INPUT(_id=('%s-id' % name.replace('-', '_')), _class=('field-%s    datetime' % fieldnum), _value=value, _name=name)
            elif (field.type == 'textbox'):
                for line in xrange(1, (field.lines + 1)):
                    ctablecontent.append(TR(TD(IMG(_src=URL(request.application, r.prefix, ('%s/upload.pdf' % urlprefix), args='image', vars={'setuuid': setuuid, 'resource_table': resourcename, 'field_name': fieldname, 'sequence': line}), _style='border:    solid    #333    1px;'), _style='text-align:center;    padding:5px;', _colspan='4')))
                input_area = TEXTAREA(value, _class=('field-%s' % fieldnum), _name=name)
            else:
                input_area = SPAN()
            ctablecontent.append(TR(TD(TABLE(TR(TD(field.label)), TR(TD(SPAN(_id=('%s-error' % name), _style='font-size:    12px;    font-weight:bold;    color:    red;', _class='error-span')))), _class='label', _style='vertical-align:    top;'), TD(input_area, _class='infield'), TD(comment, _class='comment', _style='vertical-align:    top;'), TD(TAG['BUTTON'](T('clear'), _name=('button-%s' % fieldnum), _class='clrbutton'), TAG['BUTTON'](T('ignore'), _name=('ignore-%s' % name), _class='ignore-button'), _class='clear', _style='vertical-align:    top;'), _class='fieldtr'))
            ctablecontent.append(TR(TD(_colspan='4', _style='border:    solid    #999    3px;')))
            fieldnum += 1
        ptablecontent.extend(ctablecontent)
    ptablecontent.append(TR(TD(TAG['button'](T('Submit'), _class='submit-button', _style='width:    70px;    height:    20px;'), _colspan='4', _style='text-align:center;    padding:    5px;')))
    output = FORM(TABLE(ptablecontent, _class='ptable'), _id='ocr-review-form')
    return output
