'Overrides Web2Py\'s register() to add new functionality:
- Checks whether registration is permitted
- Custom Flash styles
- Allow form to be embedded in other pages
- Optional addition of Mobile Phone field to the Register form
- Optional addition of Organisation field to the Register form
- Lookup Domains/Organisations to check for Whitelists
&/or custom Approver
@return: a registration form'

def register(self, next=DEFAULT, onvalidation=DEFAULT, onaccept=DEFAULT, log=DEFAULT, js_validation=True):

    db = current.db
    settings = self.settings
    messages = self.messages
    request = current.request
    session = current.session
    deployment_settings = current.deployment_settings
    T = current.T
    customise = deployment_settings.customise_resource('auth_user')
    if customise:
        customise(request, 'auth_user')
    utable = self.settings.table_user
    utablename = utable._tablename
    passfield = settings.password_field
    if (not deployment_settings.get_security_self_registration()):
        session.error = messages.registration_disabled
        redirect(URL(args=['login']))
    if (self.is_logged_in() and (request.function != 'index')):
        redirect(settings.logged_url)
    if (next == DEFAULT):
        next = (request.vars._next or settings.register_next)
    if (onvalidation == DEFAULT):
        onvalidation = settings.register_onvalidation
    if (onaccept == DEFAULT):
        onaccept = settings.register_onaccept
    if (log == DEFAULT):
        log = messages.register_log
    (labels, required) = s3_mark_required(utable)
    formstyle = deployment_settings.get_ui_formstyle()
    REGISTER = T('Register')
    buttons = [INPUT(_type='submit', _value=REGISTER), A(T('Login'), _href=URL(f='user', args='login'), _id='login-btn', _class='action-lnk')]
    current.response.form_label_separator = ''
    form = SQLFORM(utable, hidden=dict(_next=request.vars._next), labels=labels, separator='', showid=settings.showid, submit_button=REGISTER, delete_label=messages.delete_label, formstyle=formstyle, buttons=buttons)
    form.add_class('auth_register')
    if js_validation:
        self.s3_register_validation()
    for (i, row) in enumerate(form[0].components):
        item = row.element('input', _name=passfield)
        if item:
            field_id = ('%s_password_two' % utablename)
            s3_addrow(form, LABEL(DIV(('%s:' % messages.verify_password), SPAN('*', _class='req'), _for='password_two', _id=(field_id + SQLFORM.ID_LABEL_SUFFIX))), INPUT(_name='password_two', _id=field_id, _type='password', requires=IS_EXPR(('value==%s' % repr(request.vars.get(passfield, None))), error_message=messages.mismatched_password)), '', formstyle, (field_id + SQLFORM.ID_ROW_SUFFIX), position=(i + 1))
    if deployment_settings.get_auth_opt_in_to_email():
        field_id = ('%s_opt_in' % utablename)
        comment = DIV(DIV(_class='tooltip', _title=('%s|%s' % (T('Mailing    list'), T('By    selecting    this    you    agree    that    we    may    contact    you.')))))
        checked = (deployment_settings.get_auth_opt_in_default() and 'selected')
        s3_addrow(form, LABEL(('%s:' % T('Receive    updates')), _for='opt_in', _id=(field_id + SQLFORM.ID_LABEL_SUFFIX)), INPUT(_name='opt_in', _id=field_id, _type='checkbox', _checked=checked), comment, formstyle, (field_id + SQLFORM.ID_ROW_SUFFIX))
    if deployment_settings.get_auth_registration_requests_home_phone():
        for (i, row) in enumerate(form[0].components):
            item = row.element('input', _name='email')
            if item:
                field_id = ('%s_home' % utablename)
                s3_addrow(form, LABEL(('%s:' % T('Home    Phone')), _for='home', _id=(field_id + SQLFORM.ID_LABEL_SUFFIX)), INPUT(_name='home', _id=field_id), '', formstyle, (field_id + SQLFORM.ID_ROW_SUFFIX), position=(i + 1))
    if deployment_settings.get_auth_registration_requests_mobile_phone():
        for (i, row) in enumerate(form[0].components):
            item = row.element('input', _name='email')
            if item:
                field_id = ('%s_mobile' % utablename)
                if deployment_settings.get_auth_registration_mobile_phone_mandatory():
                    mandatory = SPAN('*', _class='req')
                    comment = ''
                else:
                    mandatory = ''
                    comment = DIV(_class='tooltip', _title=('%s|%s' % (deployment_settings.get_ui_label_mobile_phone(), messages.help_mobile_phone)))
                s3_addrow(form, LABEL(('%s:' % deployment_settings.get_ui_label_mobile_phone()), mandatory, _for='mobile', _id=(field_id + SQLFORM.ID_LABEL_SUFFIX)), INPUT(_name='mobile', _id=field_id), comment, formstyle, (field_id + SQLFORM.ID_ROW_SUFFIX), position=(i + 1))
    if deployment_settings.get_auth_registration_requests_image():
        label = self.messages.label_image
        comment = DIV(_class='stickytip', _title=('%s|%s' % (label, (self.messages.help_image % dict(gravatar=A('Gravatar', _target='top', _href='http://gravatar.com'))))))
        field_id = ('%s_image' % utablename)
        widget = SQLFORM.widgets['upload'].widget(current.s3db.pr_image.image, None)
        s3_addrow(form, LABEL(('%s:' % label), _for='image', _id=(field_id + SQLFORM.ID_LABEL_SUFFIX)), widget, comment, formstyle, (field_id + SQLFORM.ID_ROW_SUFFIX))
    if deployment_settings.get_auth_terms_of_service():
        field_id = ('%s_tos' % utablename)
        label = (T('I    agree    to    the    %(terms_of_service)s') % dict(terms_of_service=A(T('Terms    of    Service'), _href=URL(c='default', f='tos'), _target='_blank')))
        label = XML(('%s:' % label))
        s3_addrow(form, LABEL(label, _for='tos', _id=(field_id + SQLFORM.ID_LABEL_SUFFIX)), INPUT(_name='tos', _id=field_id, _type='checkbox'), '', formstyle, (field_id + SQLFORM.ID_ROW_SUFFIX))
    if (settings.captcha != None):
        form[0].insert((-1), DIV('', settings.captcha, ''))
    utable.registration_key.default = key = str(uuid4())
    if form.accepts(request.vars, session, formname='register', onvalidation=onvalidation):
        self.s3_user_register_onaccept(form)
        users = db((utable.id > 0)).select(utable.id, limitby=(0, 2))
        if (len(users) == 1):
            self.s3_approve_user(form.vars)
            current.session.confirmation = self.messages.registration_successful
            admin_group_id = 1
            self.add_membership(admin_group_id, users.first().id)
            if ('language' not in form.vars):
                form.vars.language = T.accepted_language
            user = Storage(utable._filter_fields(form.vars, id=True))
            self.login_user(user)
            self.s3_send_welcome_email(form.vars)
        elif settings.registration_requires_verification:
            if ((not settings.mailer) or (not settings.mailer.settings.server) or (not settings.mailer.send(to=form.vars.email, subject=(messages.verify_email_subject % dict(system_name=deployment_settings.get_system_name())), message=(messages.verify_email % dict(url=('%s/default/user/verify_email/%s' % (current.response.s3.base_url, key))))))):
                current.response.error = messages.email_verification_failed
                return form
            next = URL(c='default', f='message', args=['verify_email_sent'], vars={'email': form.vars.email})
        else:
            approved = self.s3_verify_user(form.vars)
            if approved:
                if ('language' not in form.vars):
                    form.vars.language = T.accepted_language
                user = Storage(utable._filter_fields(form.vars, id=True))
                self.login_user(user)
        self.set_cookie()
        if log:
            self.log_event(log, form.vars)
        if onaccept:
            onaccept(form)
        if (not next):
            next = self.url(args=request.args)
        elif isinstance(next, (list, tuple)):
            next = next[0]
        elif (next and (not (next[0] == '/')) and (next[:4] != 'http')):
            next = self.url(next.replace('[id]', str(form.vars.id)))
        redirect(next)
    return form
