1 |
ravilov |
1.1 |
function DateSelector(form, date, before, after) { |
2 |
|
|
this.sDay = form.day || form.elements[0] || null; |
3 |
|
|
this.sMonth = form.month || form.elements[1] || null; |
4 |
|
|
this.sYear = form.year || form.elements[2] || null; |
5 |
|
|
this.yBefore = before || 0; |
6 |
|
|
this.yAfter = after || 0; |
7 |
ravilov |
1.4 |
this.BaseDate = date || null; |
8 |
|
|
if (!this.BaseDate) { |
9 |
|
|
this.BaseDate = new Date(); |
10 |
|
|
this.BaseDate = new Date(this.BaseDate.getYear(), |
11 |
|
|
this.BaseDate.getMonth() + 1, this.BaseDate.getDate()); |
12 |
|
|
} else { |
13 |
|
|
this.BaseDate = new Date(this.BaseDate.getYear(), |
14 |
|
|
this.BaseDate.getMonth() - 1, this.BaseDate.getDate()); |
15 |
|
|
} |
16 |
ravilov |
1.1 |
this.Rebuild = function(d, m, y) { |
17 |
|
|
var i, sel; |
18 |
|
|
// build day selector |
19 |
|
|
if (this.sDay != null) { |
20 |
|
|
sel = d || this.sDay.selectedIndex; |
21 |
|
|
if (sel < 0) sel = this.BaseDate.getDate() - 1; |
22 |
|
|
for (i = 0; i < this.sDay.options.length; i++) |
23 |
|
|
this.sDay.options[i] = null; |
24 |
|
|
for (i = 0; i < this.nDays; i++) |
25 |
|
|
this.sDay.options[i] = new Option(String(i + 1), String(i + 1)); |
26 |
|
|
// this.sDay.options.length = this.nDays; |
27 |
|
|
this.sDay.selectedIndex = Math.min(sel, this.sDay.options.length - 1); |
28 |
|
|
} |
29 |
|
|
// build month selector |
30 |
|
|
if (this.sMonth != null) { |
31 |
ravilov |
1.4 |
sel = (m - 1) || this.sMonth.selectedIndex; |
32 |
ravilov |
1.1 |
if (sel < 0) sel = this.BaseDate.getMonth(); |
33 |
|
|
for (i = 0; i < this.sMonth.options.length; i++) |
34 |
|
|
this.sMonth.options[i] = null; |
35 |
|
|
for (i = 0; i < 12; i++) { |
36 |
|
|
var mon = null; |
37 |
|
|
if (this.Months) mon = this.Months[i]; |
38 |
|
|
mon = mon || String(i + 1); |
39 |
|
|
this.sMonth.options[i] = new Option(mon, String(i + 1)); |
40 |
|
|
} |
41 |
|
|
// this.sMonth.options.length = 12; |
42 |
|
|
this.sMonth.selectedIndex = sel; |
43 |
|
|
} |
44 |
|
|
// build year selector |
45 |
|
|
if (this.sYear != null) { |
46 |
|
|
sel = y || this.sYear.selectedIndex; |
47 |
|
|
if (sel < 0) sel = this.yBefore; |
48 |
|
|
var y = this.BaseDate.getYear(); |
49 |
|
|
if (y < 1900) y += 1900; |
50 |
|
|
for (i = 0; i < this.sYear.options.length; i++) |
51 |
|
|
this.sYear.options[i] = null; |
52 |
|
|
var j = 0; |
53 |
|
|
for (i = y - this.yBefore; i <= y + this.yAfter; i++) |
54 |
|
|
this.sYear.options[j++] = new Option(String(i), String(i)); |
55 |
|
|
// this.sYear.options.length = this.yBefore + 1 + this.yAfter; |
56 |
|
|
this.sYear.selectedIndex = sel; |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
this.Check = function(d, m, y) { |
60 |
|
|
var n = new Array(31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
61 |
ravilov |
1.2 |
if (!m) m = (this.sMonth != null && this.sMonth.options.selectedIndex >= 0) ? |
62 |
|
|
parseInt(this.sMonth.options[this.sMonth.selectedIndex].value) : this.BaseDate.getMonth(); |
63 |
|
|
if (!y) y = (this.sYear != null && this.sYear.options.selectedIndex >= 0) ? |
64 |
ravilov |
1.1 |
parseInt(this.sYear.options[this.sYear.selectedIndex].text) : this.BaseDate.getYear(); |
65 |
|
|
if (y < 1900) y += 1900; |
66 |
|
|
n[1] = ((y % 4 == 0) && (y % 100 != 0)) ? 29 : 28; |
67 |
|
|
this.nDays = n[m - 1]; |
68 |
|
|
} |
69 |
|
|
this.Reset = function() { |
70 |
|
|
var d = this.BaseDate.getDate() - 1; |
71 |
|
|
var m = this.BaseDate.getMonth(); |
72 |
|
|
var y = this.yBefore; |
73 |
|
|
this.Check(d, m, y); |
74 |
|
|
this.Rebuild(d, m, y); |
75 |
|
|
} |
76 |
|
|
this.Today = function() { |
77 |
|
|
var t = new Date(); |
78 |
|
|
var d = t.getDate() - 1; |
79 |
ravilov |
1.4 |
var m = t.getMonth() + 1; |
80 |
ravilov |
1.1 |
var y = this.yBefore; |
81 |
|
|
this.Check(d, m, y); |
82 |
ravilov |
1.4 |
this.Check(d, m, y); |
83 |
ravilov |
1.1 |
this.Rebuild(d, m, y); |
84 |
|
|
} |
85 |
|
|
this.nDays = null; |
86 |
|
|
this.Reset(); |
87 |
|
|
return this; |
88 |
|
|
} |