001.
002.
003.
function
positionInfo(object) {
004.
005.
var
p_elm = object;
006.
007.
this.getElementLeft = getElementLeft;
008.
function
getElementLeft() {
009.
var
x = 0;
010.
var
elm;
011.
if
(typeof(p_elm) ==
"object"
){
012.
elm = p_elm;
013.
}
else
{
014.
elm = document.getElementById(p_elm);
015.
}
016.
while
(elm != null) {
017.
x+= elm.offsetLeft;
018.
elm = elm.offsetParent;
019.
}
020.
return
parseInt(x);
021.
}
022.
023.
this.getElementWidth = getElementWidth;
024.
function
getElementWidth(){
025.
var
elm;
026.
if
(typeof(p_elm) ==
"object"
){
027.
elm = p_elm;
028.
}
else
{
029.
elm = document.getElementById(p_elm);
030.
}
031.
return
parseInt(elm.offsetWidth);
032.
}
033.
034.
this.getElementRight = getElementRight;
035.
function
getElementRight(){
036.
return
getElementLeft(p_elm) + getElementWidth(p_elm);
037.
}
038.
039.
this.getElementTop = getElementTop;
040.
function
getElementTop() {
041.
var
y = 0;
042.
var
elm;
043.
if
(typeof(p_elm) ==
"object"
){
044.
elm = p_elm;
045.
}
else
{
046.
elm = document.getElementById(p_elm);
047.
}
048.
while
(elm != null) {
049.
y+= elm.offsetTop;
050.
elm = elm.offsetParent;
051.
}
052.
return
parseInt(y);
053.
}
054.
055.
this.getElementHeight = getElementHeight;
056.
function
getElementHeight(){
057.
var
elm;
058.
if
(typeof(p_elm) ==
"object"
){
059.
elm = p_elm;
060.
}
else
{
061.
elm = document.getElementById(p_elm);
062.
}
063.
return
parseInt(elm.offsetHeight);
064.
}
065.
066.
this.getElementBottom = getElementBottom;
067.
function
getElementBottom(){
068.
return
getElementTop(p_elm) + getElementHeight(p_elm);
069.
}
070.
}
071.
072.
function
CalendarControl() {
073.
074.
var
calendarId =
'CalendarControl'
;
075.
var
currentYear = 0;
076.
var
currentMonth = 0;
077.
var
currentDay = 0;
078.
079.
var
selectedYear = 0;
080.
var
selectedMonth = 0;
081.
var
selectedDay = 0;
082.
083.
var
months = [
'มกราคม'
,
'กุมภาพันธ์'
,
'มีนาคม'
,
'เมษายน'
,
'พฤษภาคม'
,
'มิถุนายน'
,
'กรกฏาคม'
,
'สิงหาคม'
,
'กันยายน'
,
'ตุลาคม'
,
'พฤศจิกายน'
,
'ธันวาคม'
];
084.
var
dateField = null;
085.
086.
function
getProperty(p_property){
087.
var
p_elm = calendarId;
088.
var
elm = null;
089.
090.
if
(typeof(p_elm) ==
"object"
){
091.
elm = p_elm;
092.
}
else
{
093.
elm = document.getElementById(p_elm);
094.
}
095.
if
(elm != null){
096.
if
(elm.style){
097.
elm = elm.style;
098.
if
(elm[p_property]){
099.
return
elm[p_property];
100.
}
else
{
101.
return
null;
102.
}
103.
}
else
{
104.
return
null;
105.
}
106.
}
107.
}
108.
109.
function
setElementProperty(p_property, p_value, p_elmId){
110.
var
p_elm = p_elmId;
111.
var
elm = null;
112.
113.
if
(typeof(p_elm) ==
"object"
){
114.
elm = p_elm;
115.
}
else
{
116.
elm = document.getElementById(p_elm);
117.
}
118.
if
((elm != null) && (elm.style != null)){
119.
elm = elm.style;
120.
elm[ p_property ] = p_value;
121.
}
122.
}
123.
124.
function
setProperty(p_property, p_value) {
125.
setElementProperty(p_property, p_value, calendarId);
126.
}
127.
128.
function
getDaysInMonth(year, month) {
129.
return
[31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
130.
}
131.
132.
function
getDayOfWeek(year, month, day) {
133.
var
date
=
new
Date
(year,month-1,day)
134.
return
date
.getDay();
135.
}
136.
137.
this.clearDate = clearDate;
138.
function
clearDate() {
139.
dateField.value =
''
;
140.
hide();
141.
}
142.
143.
this.setDate = setDate;
144.
function
setDate(year, month, day) {
145.
if
(dateField) {
146.
if
(month < 10) {month =
"0"
+ month;}
147.
if
(day < 10) {day =
"0"
+ day;}
148.
149.
var
dateString = month+
"-"
+day+
"-"
+year;
150.
dateField.value = dateString;
151.
hide();
152.
}
153.
return
;
154.
}
155.
156.
this.changeMonth = changeMonth;
157.
function
changeMonth(change) {
158.
currentMonth += change;
159.
currentDay = 0;
160.
if
(currentMonth > 12) {
161.
currentMonth = 1;
162.
currentYear++;
163.
}
else
if
(currentMonth < 1) {
164.
currentMonth = 12;
165.
currentYear--;
166.
}
167.
168.
calendar = document.getElementById(calendarId);
169.
calendar.innerHTML = calendarDrawTable();
170.
}
171.
172.
this.changeYear = changeYear;
173.
function
changeYear(change) {
174.
currentYear += change;
175.
currentDay = 0;
176.
calendar = document.getElementById(calendarId);
177.
calendar.innerHTML = calendarDrawTable();
178.
}
179.
180.
function
getCurrentYear() {
181.
var
year =
new
Date
().getYear();
182.
if
(year < 1900) year += 1900;
183.
return
year;
184.
}
185.
186.
function
getCurrentMonth() {
187.
return
new
Date
().getMonth() + 1;
188.
}
189.
190.
function
getCurrentDay() {
191.
return
new
Date
().
getDate
();
192.
}
193.
194.
function
calendarDrawTable() {
195.
196.
var
dayOfMonth = 1;
197.
var
validDay = 0;
198.
var
startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
199.
var
daysInMonth = getDaysInMonth(currentYear, currentMonth);
200.
var
css_class = null;
201.
202.
var
table =
"<table cellspacing='0' cellpadding='0' border='0'>"
;
203.
table = table +
"<tr class='header'>"
;
204.
table = table +
" <td colspan='2' class='previous'><a href='javascript:changeCalendarControlMonth(-1);'><</a> <a href='javascript:changeCalendarControlYear(-1);'>«</a></td>"
;
205.
table = table +
" <td colspan='3' class='title'>"
+ months[currentMonth-1] +
"<br>"
+ currentYear +
"</td>"
;
206.
table = table +
" <td colspan='2' class='next'><a href='javascript:changeCalendarControlYear(1);'>»</a> <a href='javascript:changeCalendarControlMonth(1);'>></a></td>"
;
207.
table = table +
"</tr>"
;
208.
table = table +
"<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr>"
;
209.
210.
for
(
var
week=0; week < 6; week++) {
211.
table = table +
"<tr>"
;
212.
for
(
var
dayOfWeek=0; dayOfWeek < 7; dayOfWeek++) {
213.
if
(week == 0 && startDayOfWeek == dayOfWeek) {
214.
validDay = 1;
215.
}
else
if
(validDay == 1 && dayOfMonth > daysInMonth) {
216.
validDay = 0;
217.
}
218.
219.
if
(validDay) {
220.
if
(dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
221.
css_class =
'current'
;
222.
}
else
if
(dayOfWeek == 0 || dayOfWeek == 6) {
223.
css_class =
'weekend'
;
224.
}
else
{
225.
css_class =
'weekday'
;
226.
}
227.
228.
table = table +
"<td><a class='"
+css_class+
"' href=\"javascript:setCalendarControlDate("
+currentYear+
","
+currentMonth+
","
+dayOfMonth+
")\">"
+dayOfMonth+
"</a></td>"
;
229.
dayOfMonth++;
230.
}
else
{
231.
table = table +
"<td class='empty'> </td>"
;
232.
}
233.
}
234.
table = table +
"</tr>"
;
235.
}
236.
237.
table = table +
"<tr class='header'><th colspan='7' style='padding: 3px;'><a href='javascript:clearCalendarControl();'>Clear</a> | <a href='javascript:hideCalendarControl();'>Close</a></td></tr>"
;
238.
table = table +
"</table>"
;
239.
240.
return
table;
241.
}
242.
243.
this.show = show;
244.
function
show(field) {
245.
can_hide = 0;
246.
247.
248.
249.
if
(dateField == field) {
250.
return
;
251.
}
else
{
252.
dateField = field;
253.
}
254.
255.
if
(dateField) {
256.
try {
257.
var
dateString =
new
String(dateField.value);
258.
var
dateParts = dateString.split(
"-"
);
259.
260.
selectedMonth = parseInt(dateParts[0],10);
261.
selectedDay = parseInt(dateParts[1],10);
262.
selectedYear = parseInt(dateParts[2],10);
263.
} catch(e) {}
264.
}
265.
266.
if
(!(selectedYear && selectedMonth && selectedDay)) {
267.
selectedMonth = getCurrentMonth();
268.
selectedDay = getCurrentDay();
269.
selectedYear = getCurrentYear();
270.
}
271.
272.
currentMonth = selectedMonth;
273.
currentDay = selectedDay;
274.
currentYear = selectedYear;
275.
276.
if
(document.getElementById){
277.
278.
calendar = document.getElementById(calendarId);
279.
calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);
280.
281.
setProperty(
'display'
,
'block'
);
282.
283.
var
fieldPos =
new
positionInfo(dateField);
284.
var
calendarPos =
new
positionInfo(calendarId);
285.
286.
var
x = fieldPos.getElementLeft();
287.
var
y = fieldPos.getElementBottom();
288.
289.
setProperty(
'left'
, x +
"px"
);
290.
setProperty(
'top'
, y +
"px"
);
291.
292.
if
(document.all) {
293.
setElementProperty(
'display'
,
'block'
,
'CalendarControlIFrame'
);
294.
setElementProperty(
'left'
, x +
"px"
,
'CalendarControlIFrame'
);
295.
setElementProperty(
'top'
, y +
"px"
,
'CalendarControlIFrame'
);
296.
setElementProperty(
'width'
, calendarPos.getElementWidth() +
"px"
,
'CalendarControlIFrame'
);
297.
setElementProperty(
'height'
, calendarPos.getElementHeight() +
"px"
,
'CalendarControlIFrame'
);
298.
}
299.
}
300.
}
301.
302.
this.hide = hide;
303.
function
hide() {
304.
if
(dateField) {
305.
setProperty(
'display'
,
'none'
);
306.
setElementProperty(
'display'
,
'none'
,
'CalendarControlIFrame'
);
307.
dateField = null;
308.
}
309.
}
310.
311.
this.visible = visible;
312.
function
visible() {
313.
return
dateField
314.
}
315.
316.
this.can_hide = can_hide;
317.
var
can_hide = 0;
318.
}
319.
320.
var
calendarControl =
new
CalendarControl();
321.
322.
function
showCalendarControl(textField) {
323.
324.
calendarControl.show(textField);
325.
}
326.
327.
function
clearCalendarControl() {
328.
calendarControl.clearDate();
329.
}
330.
331.
function
hideCalendarControl() {
332.
if
(calendarControl.visible()) {
333.
calendarControl.hide();
334.
}
335.
}
336.
337.
function
setCalendarControlDate(year, month, day) {
338.
calendarControl.setDate(year, month, day);
339.
}
340.
341.
function
changeCalendarControlYear(change) {
342.
calendarControl.changeYear(change);
343.
}
344.
345.
function
changeCalendarControlMonth(change) {
346.
calendarControl.changeMonth(change);
347.
}
348.
349.
document.write(
"<iframe id='CalendarControlIFrame' src='javascript:;' frameBorder='0' scrolling='no'></iframe>"
);
350.
document.write(
"<div id='CalendarControl'></div>"
);