001.
function
positionInfo(object) {
002.
003.
004.
var
p_elm = object;
005.
006.
this
.getElementLeft = getElementLeft;
007.
function
getElementLeft() {
008.
var
x = 0;
009.
var
elm;
010.
if
(
typeof
(p_elm) ==
"object"
){
011.
elm = p_elm;
012.
}
else
{
013.
elm = document.getElementById(p_elm);
014.
}
015.
while
(elm !=
null
) {
016.
x+= elm.offsetLeft;
017.
elm = elm.offsetParent;
018.
}
019.
return
parseInt(x);
020.
}
021.
022.
this
.getElementWidth = getElementWidth;
023.
function
getElementWidth(){
024.
var
elm;
025.
if
(
typeof
(p_elm) ==
"object"
){
026.
elm = p_elm;
027.
}
else
{
028.
elm = document.getElementById(p_elm);
029.
}
030.
return
parseInt(elm.offsetWidth);
031.
}
032.
033.
this
.getElementRight = getElementRight;
034.
function
getElementRight(){
035.
return
getElementLeft(p_elm) + getElementWidth(p_elm);
036.
}
037.
038.
this
.getElementTop = getElementTop;
039.
function
getElementTop() {
040.
var
y = 0;
041.
var
elm;
042.
if
(
typeof
(p_elm) ==
"object"
){
043.
elm = p_elm;
044.
}
else
{
045.
elm = document.getElementById(p_elm);
046.
}
047.
while
(elm !=
null
) {
048.
y+= elm.offsetTop;
049.
elm = elm.offsetParent;
050.
}
051.
return
parseInt(y);
052.
}
053.
054.
this
.getElementHeight = getElementHeight;
055.
function
getElementHeight(){
056.
var
elm;
057.
if
(
typeof
(p_elm) ==
"object"
){
058.
elm = p_elm;
059.
}
else
{
060.
elm = document.getElementById(p_elm);
061.
}
062.
return
parseInt(elm.offsetHeight);
063.
}
064.
065.
this
.getElementBottom = getElementBottom;
066.
function
getElementBottom(){
067.
return
getElementTop(p_elm) + getElementHeight(p_elm);
068.
}
069.
}
070.
071.
function
CalendarControl() {
072.
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 = year+
"-"
+month+
"-"
+day;
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 < 2000) 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.
}
195.
196.
function
calendarDrawTable() {
197.
198.
199.
200.
var
dayOfMonth = 1;
201.
var
validDay = 0;
202.
var
startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
203.
var
daysInMonth = getDaysInMonth(currentYear, currentMonth);
204.
var
css_class =
null
;
205.
206.
var
table =
"<table cellspacing='0' cellpadding='0' border='0' width='200'>"
;
207.
table = table +
"<tr class='header'>"
;
208.
table = table +
" <td colspan='2' class='previous'><a href='javascript:changeCalendarControlMonth(-1);'><</a> <a href='javascript:changeCalendarControlYear(-1);'>«</a></td>"
;
209.
table = table +
" <td colspan='3' class='title'>"
+ months[currentMonth-1] +
"<br>"
+ currentYear +
"</td>"
;
210.
table = table +
" <td colspan='2' class='next'><a href='javascript:changeCalendarControlYear(1);'>»</a> <a href='javascript:changeCalendarControlMonth(1);'>></a></td>"
;
211.
table = table +
"</tr>"
;
212.
table = table +
"<tr><th>อา.</th><th>จ.</th><th>อ.</th><th>พ.</th><th>พฤ.</th><th>ศ.</th><th>ส.</th></tr>"
;
213.
214.
for
(
var
week=0; week < 6; week++) {
215.
table = table +
"<tr>"
;
216.
for
(
var
dayOfWeek=0; dayOfWeek < 7; dayOfWeek++) {
217.
if
(week == 0 && startDayOfWeek == dayOfWeek) {
218.
validDay = 1;
219.
}
else
if
(validDay == 1 && dayOfMonth > daysInMonth) {
220.
validDay = 0;
221.
}
222.
223.
if
(validDay) {
224.
if
(dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
225.
css_class =
'current'
;
226.
}
else
if
(dayOfWeek == 0 || dayOfWeek == 6) {
227.
css_class =
'weekend'
;
228.
}
else
{
229.
css_class =
'weekday'
;
230.
}
231.
232.
table = table +
"<td><a class='"
+css_class+
"' href=\"javascript:setCalendarControlDate("
+currentYear+
","
+currentMonth+
","
+dayOfMonth+
")\">"
+dayOfMonth+
"</a></td>"
;
233.
dayOfMonth++;
234.
}
else
{
235.
table = table +
"<td class='empty'> </td>"
;
236.
}
237.
}
238.
table = table +
"</tr>"
;
239.
}
240.
241.
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>"
;
242.
table = table +
"</table>"
;
243.
244.
return
table;
245.
}
246.
247.
this
.show = show;
248.
function
show(field) {
249.
can_hide = 0;
250.
251.
252.
253.
if
(dateField == field) {
254.
return
;
255.
}
else
{
256.
dateField = field;
257.
}
258.
259.
if
(dateField) {
260.
try
{
261.
var
dateString =
new
String(dateField.value);
262.
var
dateParts = dateString.split(
"-"
);
263.
264.
selectedMonth = parseInt(dateParts[1],10);
265.
selectedDay = parseInt(dateParts[2],10);
266.
selectedYear = parseInt(dateParts[0],10);
267.
}
catch
(e) {}
268.
}
269.
270.
if
(!(selectedYear && selectedMonth && selectedDay)) {
271.
selectedMonth = getCurrentMonth();
272.
selectedDay = getCurrentDay();
273.
selectedYear = getCurrentYear();
274.
}
275.
276.
currentMonth = selectedMonth;
277.
currentDay = selectedDay;
278.
currentYear = selectedYear;
279.
280.
if
(document.getElementById){
281.
282.
calendar = document.getElementById(calendarId);
283.
calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);
284.
285.
setProperty(
'display'
,
'block'
);
286.
287.
var
fieldPos =
new
positionInfo(dateField);
288.
var
calendarPos =
new
positionInfo(calendarId);
289.
290.
var
x = fieldPos.getElementLeft();
291.
var
y = fieldPos.getElementBottom();
292.
293.
setProperty(
'left'
, x +
"px"
);
294.
setProperty(
'top'
, y +
"px"
);
295.
296.
if
(document.all) {
297.
setElementProperty(
'display'
,
'block'
,
'CalendarControlIFrame'
);
298.
setElementProperty(
'left'
, x +
"px"
,
'CalendarControlIFrame'
);
299.
setElementProperty(
'top'
, y +
"px"
,
'CalendarControlIFrame'
);
300.
setElementProperty(
'width'
, calendarPos.getElementWidth() +
"px"
,
'CalendarControlIFrame'
);
301.
setElementProperty(
'height'
, calendarPos.getElementHeight() +
"px"
,
'CalendarControlIFrame'
);
302.
}
303.
}
304.
}
305.
306.
this
.hide = hide;
307.
function
hide() {
308.
if
(dateField) {
309.
setProperty(
'display'
,
'none'
);
310.
setElementProperty(
'display'
,
'none'
,
'CalendarControlIFrame'
);
311.
dateField =
null
;
312.
}
313.
}
314.
315.
this
.visible = visible;
316.
function
visible() {
317.
return
dateField
318.
}
319.
320.
this
.can_hide = can_hide;
321.
var
can_hide = 0;
322.
}
323.
324.
var
calendarControl =
new
CalendarControl();
325.
326.
function
showCalendarControl(textField) {
327.
328.
calendarControl.show(textField);
329.
}
330.
331.
function
clearCalendarControl() {
332.
calendarControl.clearDate();
333.
}
334.
335.
function
hideCalendarControl() {
336.
if
(calendarControl.visible()) {
337.
calendarControl.hide();
338.
}
339.
}
340.
341.
function
setCalendarControlDate(year, month, day) {
342.
calendarControl.setDate(year, month, day);
343.
}
344.
345.
function
changeCalendarControlYear(change) {
346.
calendarControl.changeYear(change);
347.
}
348.
349.
function
changeCalendarControlMonth(change) {
350.
calendarControl.changeMonth(change);
351.
}
352.
353.
document.write(
"<iframe id='CalendarControlIFrame' src='javascript:false;' frameBorder='0' scrolling='no'></iframe>"
);
354.
document.write(
"<div id='CalendarControl'></div>"
);