'Helper function to inject the document-ready-JavaScript for
this widget.
@param field: the Field
@param value: the current value
@param attributes: the HTML attributes for the widget'

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

    ISO = '%Y-%m-%dT%H:%M:%S'
    opts = self.opts
    if ('_id' in attributes):
        selector = attributes['_id']
    else:
        selector = str(field).replace('.', '_')
    settings = current.deployment_settings
    date_format = opts.get('date_format', settings.get_L10n_date_format())
    time_format = opts.get('time_format', settings.get_L10n_time_format())
    separator = opts.get('separator', settings.get_L10n_datetime_separator())
    datetime_format = ('%s%s%s' % (date_format, separator, time_format))
    request = current.request
    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)
    hide_time = opts.get('hide_time', False)
    if hide_time:
        limit = 'Date'
        widget = 'datepicker'
        dtformat = date_format
    else:
        limit = 'DateTime'
        widget = 'datetimepicker'
        dtformat = datetime_format
    now = request.utcnow
    timedelta = datetime.timedelta
    offset = S3DateTime.get_offset_value(current.session.s3.utc_offset)
    if ('min' in opts):
        earliest = opts['min']
    else:
        past = opts.get('past', 876000)
        earliest = (now - timedelta(hours=past))
    if ('max' in opts):
        latest = opts['max']
    else:
        future = opts.get('future', 876000)
        latest = (now + timedelta(hours=future))
    minute_step = opts.get('minute_step', 5)
    if (not hide_time):
        if ((earliest <= now) and (now <= latest)):
            start = now
        elif (now < earliest):
            start = earliest
        elif (now > latest):
            start = latest
        step = (minute_step * 60)
        seconds = (start - start.min).seconds
        rounding = (((seconds + (step / 2)) // step) * step)
        rounded = (start + timedelta(0, (rounding - seconds), (- start.microsecond)))
        if (rounded < earliest):
            rounded = earliest
        elif (rounded > latest):
            rounded = latest
        if offset:
            rounded += timedelta(seconds=offset)
        default = rounded.strftime(dtformat)
    else:
        default = ''
    if offset:
        earliest += timedelta(seconds=offset)
        latest += timedelta(seconds=offset)
    set_min = opts.get('set_min', None)
    set_max = opts.get('set_max', None)
    onclose = 'function(selectedDate){'
    onclear = ''
    if set_min:
        onclose += ("$('#%s').%s('option','minDate',selectedDate)\n" % (set_min, widget))
        onclear += ("$('#%s').%s('option','minDate',null)\n" % (set_min, widget))
    if set_max:
        onclose += ("$('#%s').%s('option','maxDate',selectedDate)" % (set_max, widget))
        onclear += ("$('#%s').%s('option','minDate',null)" % (set_max, widget))
    onclose += '}'
    date_format = settings.get_L10n_date_format().replace('%Y', 'yy').replace('%y', 'y').replace('%m', 'mm').replace('%d', 'dd').replace('%b', 'M')
    time_format = settings.get_L10n_time_format().replace('%p', 'TT').replace('%I', 'hh').replace('%H', 'HH').replace('%M', 'mm').replace('%S', 'ss')
    separator = settings.get_L10n_datetime_separator()
    (pyears, fyears) = (10, 10)
    if (('min' in opts) or ('past' in opts)):
        pyears = abs((earliest.year - now.year))
    if (('max' in opts) or ('future' in opts)):
        fyears = abs((latest.year - now.year))
    year_range = ('%s:%s' % (opts.get('min_year', ('-%s' % pyears)), opts.get('max_year', ('+%s' % fyears))))
    firstDOW = settings.get_L10n_firstDOW()
    getopt = (lambda opt, default: ((opts.get(opt, default) and 'true') or 'false'))
    script = ('$(\'#%(selector)s\').%(widget)s({\n    showSecond:false,\n    firstDay:%(firstDOW)s,\n    min%(limit)s:new    Date(Date.parse(\'%(earliest)s\')),\n    max%(limit)s:new    Date(Date.parse(\'%(latest)s\')),\n    dateFormat:\'%(date_format)s\',\n    timeFormat:\'%(time_format)s\',\n    separator:\'%(separator)s\',\n    stepMinute:%(minute_step)s,\n    showWeek:%(weeknumber)s,\n    showButtonPanel:%(buttons)s,\n    changeMonth:%(month_selector)s,\n    changeYear:%(year_selector)s,\n    yearRange:\'%(year_range)s\',\n    useLocalTimezone:true,\n    defaultValue:\'%(default)s\',\n    onClose:%(onclose)s\n}).one(\'click\',function(){$(this).focus()})\nvar    clear_button=$(\'<button    id="%(selector)s_clear"    class="btn    date-clear-btn"    type="button">%(clear)s</button>\').click(function(){\n    $(\'#%(selector)s\').val(\'\');%(onclear)s;$(\'#%(selector)s\').closest(\'.filter-form\').trigger(\'optionChanged\')\n})\nif($(\'#%(selector)s_clear\').length==0){\n    $(\'#%(selector)s\').after(clear_button)\n}' % dict(selector=selector, widget=widget, date_format=date_format, time_format=time_format, separator=separator, weeknumber=getopt('weeknumber', False), month_selector=getopt('month_selector', False), year_selector=getopt('year_selector', True), buttons=getopt('buttons', True), firstDOW=firstDOW, year_range=year_range, minute_step=minute_step, limit=limit, earliest=earliest.strftime(ISO), latest=latest.strftime(ISO), default=default, clear=current.T('clear'), onclose=onclose, onclear=onclear))
    if (script not in jquery_ready):
        jquery_ready.append(script)
    return
