// js calendar by jaewon // last_modified : 2009/08/06 // Usage : // // var calendar_div; var calendar_obj; var calendar_tt; // ex) calendar(event, 'object_name', 'YYYYMMDD') // fmt option이 없으면 기본으로 설정. // YYYY : 년 // MM : 월 // DD : 일 var calendar_fmt = "YYYY-MM-DD"; // 숨겨야 되는 select element 저장변수(Array) var hidden_select; // element의 x, y, width, height를 가져옴. (select외에 object를 인수로 사용해도 됨 ex:div) function get_pos(selectObj) { var pos = new Array(); var pos_x = 0, pos_y = 0; // while에서 offsetParent를 찾아가는 방식이라 // 다른 지정자에 인수로 넘어온 object를 넘겨줌. obj = selectObj; // 좌표계산 while(obj.offsetParent) { pos_y += parseInt(obj.offsetTop); pos_x += parseInt(obj.offsetLeft); obj = obj.offsetParent; } pos_x += parseInt(obj.offsetLeft); pos_y += parseInt(obj.offsetTop); // 좌표저장 pos.x = pos_x; pos.y = pos_y; pos.x2 = pos_x + selectObj.offsetWidth; pos.y2 = pos_y + selectObj.offsetHeight; return pos; } // IE와 Maxton에서 select elements를 숨김 // 달력 레이어의 좌표를 인수로 넘겨서 해당 좌표내에 select element가 있는지 체크하는 방식 function hide_select(x, y, x2, y2) { var selects = document.getElementsByTagName('select'); var hidden_count = 0; x = parseInt(x); y = parseInt(y); x2 = parseInt(x2); y2 = parseInt(y2); // X축이 겹치는지 체크하기 위한 변수 var x_cross = false; // Y축이 겹치는지 체크하기 위한 변수 var y_cross = false; // 숨겨야 할 select elements를 배열로 등록함. hidden_select = new Array(); for(var i=0; i= selects[i].pos.x) || (x <= selects[i].pos.x2 && x2 >= selects[i].pos.x2)) x_cross = true; else x_cross = false; // Y축이 달력 영역에 포함되어 있는지 체크 if((y <= selects[i].pos.y && y2 >= selects[i].pos.y) || (y <= selects[i].pos.y2 && y2 >= selects[i].pos.y2)) y_cross = true; else y_cross = false; // X축이나 Y축이 달력 레이어와 겹쳐지면 해당 select element를 숨김 if(x_cross == true && y_cross == true) { selects[i].style.visibility = "hidden"; hidden_select[hidden_count++] = i; } } } // IE와 Maxton에서 숨겨진 select elements를 다시 표시해줌. function show_select() { var selects = document.getElementsByTagName('select'); // hide_select function에서 숨긴 select elements를 다시 보여줌. for(var i=0; i= 4)? 2 : 0; margin_left = (i % 4 == 0)? 0 : 2; html += "
"+String(year+i)+"
"; } prev_year = (year - i); next_year = (year + i); html += "
이전
"; html += "
다음
"; return html; } // 달력 레이어에 달력을 출력함. function calendar_draw(set_date) { var days_arr = new Array(31,28,31,30,31,30,31,31,30,31,30,31); var html = ""; var calendar_date, calendar_t; // 달력을 입력할 input box의 숫자만 검출한 값의 자릿수가 6자리 YYYYMM 이거나 8자리 YYYYMMDD라면 해당 년월에 맞게 달력을 표시함 if(set_date && (set_date.length == 6 || set_date.length == 8)) { calendar_date = set_date; var y = calendar_date.substring(0, 4); var m = calendar_date.substring(4, 6); var s_t = new Date(y, m-1, 1); if(s_t.getMonth()+1 != Number(m)) calendar_t = null; else calendar_t = new Date(y, m-1, 1); } // 오늘 날짜를 구함. (오늘날짜는 font-weight:bold; 스타일 지정) var t = new Date(); var today = t.getFullYear(); if(t.getMonth()+1 < 10) today += '0' + String(t.getMonth()+1); else today += String(t.getMonth()+1); if(t.getDate() < 10) today += '0' + String(t.getDate()); else today += String(t.getDate()); // 달력을 입력할 input box의 값으로 기본값이 설정되지 않았다면 오늘을 기준으로 달력을 표시함 if(!calendar_t) { var y = t.getFullYear(); var m = t.getMonth(); calendar_t = new Date(y, m, 1); } // 표시할 달력의 년월 var calendar_yymm = calendar_t.getFullYear(); calendar_yymm += (calendar_t.getMonth()+1 < 10)? String('0' + (calendar_t.getMonth() + 1)) : String(calendar_t.getMonth()+1); // 표시할 달력의 연도 var calendar_y = Number(calendar_yymm.substring(0, 4)); // 표시할 달력의 월 var calendar_m = Number(calendar_yymm.substring(4, 6)); // 이전달 링크 값 (◀클릭) var prev_m; if(Number(calendar_m) == 1) { prev_m = String(calendar_y - 1); prev_m += '12'; } else { prev_m = String(calendar_y); prev_m += (calendar_m - 1 < 10)? '0' + (calendar_m - 1) : calendar_m - 1; } // 다음달 링크값 (▶클릭) var next_m; if(Number(calendar_m) == 12) { next_m = String(calendar_y + 1); next_m += '01'; } else { next_m = String(calendar_y); next_m += (calendar_m + 1 < 10)? '0' + (calendar_m + 1) : calendar_m + 1; } // 이전 다음달 값을 계산하기 위해 달력표시 월의 값을 아래에서 스트링으로 변경 calendar_m = (calendar_m < 10)? String('0' + calendar_m) : String(calendar_m); // 전년도 var prev_y = String(calendar_y-1) + calendar_m; // 후년도 var next_y = String(calendar_y+1) + calendar_m; // 년, 월, 닫기 버튼 html html += ""; html += ""; html += ""; html += ""; // 요일표시 html html += ""; html += ""; html += ""; html += ""; html += ""; html += ""; html += ""; html += ""; html += ""; // 달력 시작일의 요일을 구함 var week = calendar_t.getDay(); // 시작일이 일요일이 아니면 빈 TD 출력 for(var i=0; i 6) { html += ""; week = 0; } if(week == 0) { html += ""; } // 달력의 년월 this_date = calendar_yymm; // 표시일 this_date += (i <= 9)? String('0' + i) : String(i); var style = "cursor:pointer; font-size:12px; text-align:center;"; // input box의 값과 동일한 날은 underline 표시 if(set_date == this_date) style += " text-decoration:underline;"; // 오늘날짜는 진하게 표시 if(today == this_date) style += " font-weight:bold;"; // 일요일은 빨간색 if(week == 0) style += " color:red;"; // 토요일은 파란색 if(week == 6) style += " color:blue;"; html += ""; week ++; } // 토요일로 끝나지 않았으면 빈 TD 출력 if(week < 6) { for(var i=week; i<=6; i++) { html += "\n"; } } html += ""; html += "
"; html += " "; html += "↕"+calendar_y+" "; html += " "; html += " "; html += "↕"+calendar_m+" "; html += " "; html += "X
SunMonTueWedThuFriSat
"+i+" 
"; html += "
"; html += calendar_years_html(calendar_y, calendar_m); html += "
"; // 월선택 레이어 설정 html += "
"; html += "
1
"; html += "
2
"; html += "
3
"; html += "
4
"; html += "
5
"; html += "
6
"; html += "
7
"; html += "
8
"; html += "
9
"; html += "
10
"; html += "
11
"; html += "
12
"; html += "
"; // 달력 표시 calendar_div.innerHTML = html; // IE/Maxton일때 숨겨진 select elements 표시 if(hidden_select) show_select(); // Crome, Firefox, Safari가 아니면 달력 영역과 겹치는 select elements 숨김 if(navigator.userAgent.indexOf('Chrome') == -1 && navigator.userAgent.indexOf('Firefox') == -1 && navigator.userAgent.indexOf('Safari') == -1) { hide_select(parseInt(calendar_div.style.left), parseInt(calendar_div.style.top), parseInt(calendar_div.style.left) + parseInt(calendar_div.offsetWidth), parseInt(calendar_div.style.top) + parseInt(calendar_div.offsetHeight)); } } // 선택한 날짜를 input box에 넣어줌. function calendar_set_date(set_date) { // FMT 변경 (YYYY-MM-DD) if(calendar_obj) { var year = set_date.substring(0,4); var month = set_date.substring(4,6); var day = set_date.substring(6,8); var calendar_value = calendar_fmt; calendar_value = calendar_value.replace(/yyyy/i, year); calendar_value = calendar_value.replace(/mm/i, month); calendar_value = calendar_value.replace(/dd/i, day); calendar_obj.value = calendar_value; } calendar_close(); } // 달력을 숨김 function calendar_hide() { if(calendar_tt) window.clearTimeout(calendar_tt); calendar_close(); } // 300ms 후에 달력을 숨김 // mouseout때 바로 닫히지 않고 약간의 텀을 두어 다시 마우스가 갔을때 달력 레이어를 유지하기 위함 function calendar_afterHide() { if(calendar_tt) window.clearTimeout(calendar_tt); this.t = window.setTimeout("calendar_hide()", 300); } // 달력 숨김 타이머를 삭제함. function calendar_clearT() { if(calendar_tt) window.clearTimeout(calendar_tt); } // 년도 클릭시 월선택 레이어를 표시/감춤 // on -> off // off -> on function calendar_years_toggle() { var calendar_div = document.getElementById('calendar_div'); var calendar_years = document.getElementById('calendar_years'); var calendar_months = document.getElementById('calendar_months'); calendar_months.style.visibility = "hidden"; // 달력의 년,월,닫기 부분은 표시되도록 상단에서 20px 내려서 표시함. var top = parseInt(calendar_div.style.top) + 20; var left = parseInt(calendar_div.style.left); // position:absolute; 이지만 달력 레이어의 상대성 절대좌표로 인식함. calendar_years.style.top = '20px'; calendar_years.style.left = '0px'; // 월선택 레이어 토글 if(calendar_years.style.visibility == "hidden") calendar_years.style.visibility = "visible"; else calendar_years.style.visibility = "hidden"; } // 월 클릭시 년도선택 레이어를 표시/감춤 // on -> off // off -> on function calendar_months_toggle() { var calendar_div = document.getElementById('calendar_div'); var calendar_months = document.getElementById('calendar_months'); var calendar_years = document.getElementById('calendar_years'); calendar_years.style.visibility = "hidden"; // 달력의 년,월,닫기 부분은 표시되도록 상단에서 20px 내려서 표시함. var top = parseInt(calendar_div.style.top) + 20; var left = parseInt(calendar_div.style.left); // position:absolute; 이지만 달력 레이어의 상대성 절대좌표로 인식함. calendar_months.style.top = '20px'; calendar_months.style.left = '0px'; // 월선택 레이어 토글 if(calendar_months.style.visibility == "hidden") calendar_months.style.visibility = "visible"; else calendar_months.style.visibility = "hidden"; } // 달력표시 호출함수 onClick="calendar(event, 'ID')"로 호출 function calendar(e, obj_name, fmt) { if(!e) var e = window.event; calendar_obj = document.getElementById(obj_name); // input object의 값에서 숫자만 추출함. var default_date = calendar_obj.value.replace(/[^0-9]/g, ''); // YYYYMM 혹은 YYYYMMDD 형식이 아니면 무시함. if(default_date.length != 6 && default_date.length != 8) default_date = ''; // onClick="calendar(event, 'ID')"의 ID Object가 없으면 달력을 표시해도 넣을곳이 없으므로 오류처리 if(!calendar_obj) { alert(calendar_obj + ' object is null'); return; } if(fmt) calendar_fmt = fmt; else calendar_fmt = "YYYY-MM-DD"; // 달력 레이어 초기화 calendar_set(e); // 달력 표시 calendar_draw(default_date); }