aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/smart_date.coffee
blob: 48aa1c2f9d3a1c0fc19dc01ffc23440f528bf473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
window.legalDaysPerMonth =
  false: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  true:  [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

window.correctDay = (dateValues) ->
  [day, month, year] = dateValues
  return day unless day > 0 && month > 0 && year > 0

  legallyMaximumDay = legalDaysPerMonth[isLeapYear(year)][month - 1]
  Math.min day, legallyMaximumDay

window.isLeapYear = (year) ->
  (year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0))

window.smartCorrectDate = ->
  allSelectors = $(@).parent().children('select')
  allVals      = allSelectors.map (index, sel) ->
    parseInt($(sel).val())
  correctedDay = correctDay allVals
  daySelector  = allSelectors.first()
  $(daySelector).val(correctedDay)

$ ->
  $(document).on 'change', '.smart_date select', smartCorrectDate