Guest User

Untitled

a guest
May 31st, 2011
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // <!-- <![CDATA[
  2.  
  3. // Project: Dynamic Date Selector (DtTvB) - 2006-03-16
  4. // Script featured on JavaScript Kit- http://www.javascriptkit.com
  5. // Code begin...
  6. // Set the initial date.
  7. var ds_i_date = new Date();
  8. ds_c_month = ds_i_date.getMonth() + 1;
  9. ds_c_year = ds_i_date.getFullYear();
  10.  
  11. // Get Element By Id
  12. function ds_getel(id) {
  13.     return document.getElementById(id);
  14. }
  15.  
  16. // Get the left and the top of the element.
  17. function ds_getleft(el) {
  18.     var tmp = el.offsetLeft;
  19.     el = el.offsetParent
  20.     while(el) {
  21.         tmp += el.offsetLeft;
  22.         el = el.offsetParent;
  23.     }
  24.     return tmp;
  25. }
  26. function ds_gettop(el) {
  27.     var tmp = el.offsetTop;
  28.     el = el.offsetParent
  29.     while(el) {
  30.         tmp += el.offsetTop;
  31.         el = el.offsetParent;
  32.     }
  33.     return tmp;
  34. }
  35.  
  36. // Output Element
  37. var ds_oe = ds_getel('ds_calclass');
  38. // Container
  39. var ds_ce = ds_getel('ds_conclass');
  40.  
  41. // Output Buffering
  42. var ds_ob = '';
  43. function ds_ob_clean() {
  44.     ds_ob = '';
  45. }
  46. function ds_ob_flush() {
  47.     ds_oe.innerHTML = ds_ob;
  48.     ds_ob_clean();
  49. }
  50. function ds_echo(t) {
  51.     ds_ob += t;
  52. }
  53.  
  54. var ds_element; // Text Element...
  55.  
  56. var ds_monthnames = [
  57. 'January', 'February', 'March', 'April', 'May', 'June',
  58. 'July', 'August', 'September', 'October', 'November', 'December'
  59. ]; // You can translate it for your language.
  60.  
  61. var ds_daynames = [
  62. 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  63. ]; // You can translate it for your language.
  64.  
  65. // Calendar template
  66. function ds_template_main_above(t) {
  67.     return '<table cellpadding="3" cellspacing="1" class="ds_tbl">'
  68.          + '<tr>'
  69.          + '<td class="ds_head" style="cursor: pointer" onclick="ds_py();"><<</td>'
  70.          + '<td class="ds_head" style="cursor: pointer" onclick="ds_pm();"><</td>'
  71.          + '<td class="ds_head" style="cursor: pointer" onclick="ds_hi();" colspan="3">[Close]</td>'
  72.          + '<td class="ds_head" style="cursor: pointer" onclick="ds_nm();">></td>'
  73.          + '<td class="ds_head" style="cursor: pointer" onclick="ds_ny();">>></td>'
  74.          + '</tr>'
  75.          + '<tr>'
  76.          + '<td colspan="7" class="ds_head">' + t + '</td>'
  77.          + '</tr>'
  78.          + '<tr>';
  79. }
  80.  
  81. function ds_template_day_row(t) {
  82.     return '<td class="ds_subhead">' + t + '</td>';
  83.     // Define width in CSS, XHTML 1.0 Strict doesn't have width property for it.
  84. }
  85.  
  86. function ds_template_new_week() {
  87.     return '</tr><tr>';
  88. }
  89.  
  90. function ds_template_blank_cell(colspan) {
  91.     return '<td colspan="' + colspan + '"></td>'
  92. }
  93.  
  94. function ds_template_day(d, m, y) {
  95.     return '<td class="ds_cell" onclick="ds_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
  96.     // Define width the day row.
  97. }
  98.  
  99. function ds_template_main_below() {
  100.     return '</tr>'
  101.          + '</table>';
  102. }
  103.  
  104. // This one draws calendar...
  105. function ds_draw_calendar(m, y) {
  106.     // First clean the output buffer.
  107.     ds_ob_clean();
  108.     // Here we go, do the header
  109.     ds_echo (ds_template_main_above(ds_monthnames[m - 1] + ' ' + y));
  110.     for (i = 0; i < 7; i ++) {
  111.         ds_echo (ds_template_day_row(ds_daynames[i]));
  112.     }
  113.     // Make a date object.
  114.     var ds_dc_date = new Date();
  115.     ds_dc_date.setMonth(m - 1);
  116.     ds_dc_date.setFullYear(y);
  117.     ds_dc_date.setDate(1);
  118.     if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
  119.         days = 31;
  120.     } else if (m == 4 || m == 6 || m == 9 || m == 11) {
  121.         days = 30;
  122.     } else {
  123.         days = (y % 4 == 0) ? 29 : 28;
  124.     }
  125.     var first_day = ds_dc_date.getDay();
  126.     var first_loop = 1;
  127.     // Start the first week
  128.     ds_echo (ds_template_new_week());
  129.     // If sunday is not the first day of the month, make a blank cell...
  130.     if (first_day != 0) {
  131.         ds_echo (ds_template_blank_cell(first_day));
  132.     }
  133.     var j = first_day;
  134.     for (i = 0; i < days; i ++) {
  135.         // Today is sunday, make a new week.
  136.         // If this sunday is the first day of the month,
  137.         // we've made a new row for you already.
  138.         if (j == 0 && !first_loop) {
  139.             // New week!!
  140.             ds_echo (ds_template_new_week());
  141.         }
  142.         // Make a row of that day!
  143.         ds_echo (ds_template_day(i + 1, m, y));
  144.         // This is not first loop anymore...
  145.         first_loop = 0;
  146.         // What is the next day?
  147.         j ++;
  148.         j %= 7;
  149.     }
  150.     // Do the footer
  151.     ds_echo (ds_template_main_below());
  152.     // And let's display..
  153.     ds_ob_flush();
  154.     // Scroll it into view.
  155.     ds_ce.scrollIntoView();
  156. }
  157.  
  158. // A function to show the calendar.
  159. // When user click on the date, it will set the content of t.
  160. function ds_sh(t) {
  161.     // Set the element to set...
  162.     ds_element = t;
  163.     // Make a new date, and set the current month and year.
  164.     var ds_sh_date = new Date();
  165.     ds_c_month = ds_sh_date.getMonth() + 1;
  166.     ds_c_year = ds_sh_date.getFullYear();
  167.     // Draw the calendar
  168.     ds_draw_calendar(ds_c_month, ds_c_year);
  169.     // To change the position properly, we must show it first.
  170.     ds_ce.style.display = '';
  171.     // Move the calendar container!
  172.     the_left = ds_getleft(t);
  173.     the_top = ds_gettop(t) + t.offsetHeight;
  174.     ds_ce.style.left = the_left + 'px';
  175.     ds_ce.style.top = the_top + 'px';
  176.     // Scroll it into view.
  177.     ds_ce.scrollIntoView();
  178. }
  179.  
  180. // Hide the calendar.
  181. function ds_hi() {
  182.     ds_ce.style.display = 'none';
  183. }
  184.  
  185. // Moves to the next month...
  186. function ds_nm() {
  187.     // Increase the current month.
  188.     ds_c_month ++;
  189.     // We have passed December, let's go to the next year.
  190.     // Increase the current year, and set the current month to January.
  191.     if (ds_c_month > 12) {
  192.         ds_c_month = 1;
  193.         ds_c_year++;
  194.     }
  195.     // Redraw the calendar.
  196.     ds_draw_calendar(ds_c_month, ds_c_year);
  197. }
  198.  
  199. // Moves to the previous month...
  200. function ds_pm() {
  201.     ds_c_month = ds_c_month - 1; // Can't use dash-dash here, it will make the page invalid.
  202.     // We have passed January, let's go back to the previous year.
  203.     // Decrease the current year, and set the current month to December.
  204.     if (ds_c_month < 1) {
  205.         ds_c_month = 12;
  206.         ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
  207.     }
  208.     // Redraw the calendar.
  209.     ds_draw_calendar(ds_c_month, ds_c_year);
  210. }
  211.  
  212. // Moves to the next year...
  213. function ds_ny() {
  214.     // Increase the current year.
  215.     ds_c_year++;
  216.     // Redraw the calendar.
  217.     ds_draw_calendar(ds_c_month, ds_c_year);
  218. }
  219.  
  220. // Moves to the previous year...
  221. function ds_py() {
  222.     // Decrease the current year.
  223.     ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
  224.     // Redraw the calendar.
  225.     ds_draw_calendar(ds_c_month, ds_c_year);
  226. }
  227.  
  228. // Format the date to output.
  229. function ds_format_date(d, m, y) {
  230.     // 2 digits month.
  231.     m2 = '00' + m;
  232.     m2 = m2.substr(m2.length - 2);
  233.     // 2 digits day.
  234.     d2 = '00' + d;
  235.     d2 = d2.substr(d2.length - 2);
  236.     // YYYY-MM-DD
  237.     return y + '-' + m2 + '-' + d2;
  238. }
  239.  
  240. // When the user clicks the day.
  241. function ds_onclick(d, m, y) {
  242.     // Hide the calendar.
  243.     ds_hi();
  244.     // Set the value of it, if we can.
  245.     if (typeof(ds_element.value) != 'undefined') {
  246.         ds_element.value = ds_format_date(d, m, y);
  247.     // Maybe we want to set the HTML in it.
  248.     } else if (typeof(ds_element.innerHTML) != 'undefined') {
  249.         ds_element.innerHTML = ds_format_date(d, m, y);
  250.     // I don't know how should we display it, just alert it to user.
  251.     } else {
  252.         alert (ds_format_date(d, m, y));
  253.     }
  254. }
  255.  
  256. // And here is the end.
  257.  
  258. // ]]> -->
Advertisement
Add Comment
Please, Sign In to add comment