'Widget builder
@param field: the Field
@param value: the current value
@param attributes: the HTML attributes for the widget'

def __call__(self, field, value, **attributes):

    dt = current.calendar.parse_date(value, local=True)
    if dt:
        value = dt.isoformat()
    request = current.request
    settings = current.deployment_settings
    s3 = current.response.s3
    jquery_ready = s3.jquery_ready
    language = current.session.s3.language
    if (language in settings.date_formats):
        if (language in ('prs', 'ps')):
            language = 'fa'
        elif ('-' in language):
            parts = language.split('-', 1)
            language = ('%s-%s' % (parts[0], parts[1].upper()))
        path = os.path.join(request.folder, 'static', 'scripts', 'ui', 'i18n', ('datepicker-%s.js' % language))
        if os.path.exists(path):
            lscript = ('/%s/static/scripts/ui/i18n/datepicker-%s.js' % (request.application, language))
            if (lscript not in s3.scripts):
                s3.scripts.append(lscript)
                script = ('$.datepicker.setDefaults($.datepicker.regional["%s"])' % language)
                jquery_ready.append(script)
    if self.format:
        format = str(self.format)
    else:
        dtfmt = settings.get_L10n_date_format()
        format = dtfmt.replace('%Y', 'yy').replace('%y', 'y').replace('%m', 'mm').replace('%d', 'dd').replace('%b', 'M')
    default = dict(_type='text', value=(((value is not None) and str(value)) or ''))
    attr = StringWidget._attributes(field, default, **attributes)
    widget = INPUT(**attr)
    widget.add_class('date')
    if ('_id' in attr):
        selector = attr['_id']
    else:
        selector = str(field).replace('.', '_')
    now = current.request.utcnow
    past = self.past
    if (past is None):
        past = ''
    else:
        if past:
            past = (now - relativedelta(months=past))
            if (now > past):
                days = (now - past).days
                minDate = ('-%s' % days)
            else:
                days = (past - now).days
                minDate = ('+%s' % days)
        else:
            minDate = '-0'
        past = (',minDate:%s' % minDate)
    future = self.future
    if (future is None):
        future = ''
    else:
        if future:
            future = (now + relativedelta(months=future))
            if (future > now):
                days = (future - now).days
                maxDate = ('+%s' % days)
            else:
                days = (now - future).days
                maxDate = ('-%s' % days)
        else:
            maxDate = '+0'
        future = (',maxDate:%s' % maxDate)
    start_field = self.start_field
    default_interval = self.default_interval
    script = ("$('#%(selector)s').datepicker('option',{yearRange:'c-100:c+100',dateFormat:'%(format)s'%(past)s%(future)s}).one('click',function(){$(this).focus()})" % dict(selector=selector, format=format, past=past, future=future))
    if (script not in jquery_ready):
        jquery_ready.append(script)
    if (start_field and default_interval):
        T = current.T
        i18n = ('\ni18n.interval="%(interval_label)s"\ni18n.btn_1_label="%(btn_first_label)s"\ni18n.btn_2_label="%(btn_second_label)s"\ni18n.btn_3_label="%(btn_third_label)s"\ni18n.btn_4_label="%(btn_fourth_label)s"\ni18n.btn_clear="%(btn_clear)s"\n' % dict(interval_label=T('Interval'), btn_first_label=T('+6    MO'), btn_second_label=T('+1    YR'), btn_third_label=T('+2    YR'), btn_fourth_label=T('+5    YR'), btn_clear=T('Clear')))
        s3.js_global.append(i18n)
        script = ('\n$(\'#%(end_selector)s\').end_date_interval({\nstart_date_selector:"#%(start_selector)s",\ninterval:%(interval)d\n%(default_explicit)s\n})\n' % dict(end_selector=selector, start_selector=start_field, interval=default_interval, default_explicit=(',default_explicit:true' if self.default_explicit else '')))
        if (script not in jquery_ready):
            jquery_ready.append(script)
    return TAG[''](widget, requires=field.requires)
