Advertisement
Guest User

jscalendar helper script

a guest
Oct 24th, 2011
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- helper script that uses the calendar -->
  2. <script type="text/javascript">
  3.  
  4. var oldLink = null;
  5. // code to change the active stylesheet
  6. function setActiveStyleSheet(link, title) {
  7.   var i, a, main;
  8.   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  9.     if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
  10.       a.disabled = true;
  11.       if(a.getAttribute("title") == title) a.disabled = false;
  12.     }
  13.   }
  14.   if (oldLink) oldLink.style.fontWeight = 'normal';
  15.   oldLink = link;
  16.   link.style.fontWeight = 'bold';
  17.   return false;
  18. }
  19.  
  20. // This function gets called when the end-user clicks on some date.
  21. function selected(cal, date) {
  22.   cal.sel.value = date; // just update the date in the input field.
  23.   if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
  24.     // if we add this call we close the calendar on single-click.
  25.     // just to exemplify both cases, we are using this only for the 1st
  26.     // and the 3rd field, while 2nd and 4th will still require double-click.
  27.     cal.callCloseHandler();
  28. }
  29.  
  30. // And this gets called when the end-user clicks on the _selected_ date,
  31. // or clicks on the "Close" button.  It just hides the calendar without
  32. // destroying it.
  33. function closeHandler(cal) {
  34.   cal.hide();                        // hide the calendar
  35. //  cal.destroy();
  36.   _dynarch_popupCalendar = null;
  37. }
  38.  
  39. // This function shows the calendar under the element having the given id.
  40. // It takes care of catching "mousedown" signals on document and hiding the
  41. // calendar if the click was outside.
  42. function showCalendar(id, format, showsTime, showsOtherMonths) {
  43.   var el = document.getElementById(id);
  44.   if (_dynarch_popupCalendar != null) {
  45.     // we already have some calendar created
  46.     _dynarch_popupCalendar.hide();                 // so we hide it first.
  47.   } else {
  48.     // first-time call, create the calendar.
  49.     var cal = new Calendar(1, null, selected, closeHandler);
  50.     // uncomment the following line to hide the week numbers
  51.     // cal.weekNumbers = false;
  52.     if (typeof showsTime == "string") {
  53.       cal.showsTime = true;
  54.       cal.time24 = (showsTime == "24");
  55.     }
  56.     if (showsOtherMonths) {
  57.       cal.showsOtherMonths = true;
  58.     }
  59.     _dynarch_popupCalendar = cal;                  // remember it in the global var
  60.     cal.setRange(1900, 2070);        // min/max year allowed.
  61.     cal.create();
  62.   }
  63.   _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  64.   _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  65.   _dynarch_popupCalendar.sel = el;                 // inform it what input field we use
  66.  
  67.   // the reference element that we pass to showAtElement is the button that
  68.   // triggers the calendar.  In this example we align the calendar bottom-right
  69.   // to the button.
  70.   _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar
  71.  
  72.   return false;
  73. }
  74.  
  75. var MINUTE = 60 * 1000;
  76. var HOUR = 60 * MINUTE;
  77. var DAY = 24 * HOUR;
  78. var WEEK = 7 * DAY;
  79.  
  80. // If this handler returns true then the "date" given as
  81. // parameter will be disabled.  In this example we enable
  82. // only days within a range of 10 days from the current
  83. // date.
  84. // You can use the functions date.getFullYear() -- returns the year
  85. // as 4 digit number, date.getMonth() -- returns the month as 0..11,
  86. // and date.getDate() -- returns the date of the month as 1..31, to
  87. // make heavy calculations here.  However, beware that this function
  88. // should be very fast, as it is called for each day in a month when
  89. // the calendar is (re)constructed.
  90. function isDisabled(date) {
  91.   var today = new Date();
  92.   return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
  93. }
  94.  
  95. function flatSelected(cal, date) {
  96.   var el = document.getElementById("preview");
  97.   el.innerHTML = date;
  98. }
  99.  
  100. function showFlatCalendar() {
  101.   var parent = document.getElementById("display");
  102.  
  103.   // construct a calendar giving only the "selected" handler.
  104.   var cal = new Calendar(0, null, flatSelected);
  105.  
  106.   // hide week numbers
  107.   cal.weekNumbers = false;
  108.  
  109.   // We want some dates to be disabled; see function isDisabled above
  110.   cal.setDisabledHandler(isDisabled);
  111.   cal.setDateFormat("%A, %B %e");
  112.  
  113.   // this call must be the last as it might use data initialized above; if
  114.   // we specify a parent, as opposite to the "showCalendar" function above,
  115.   // then we create a flat calendar -- not popup.  Hidden, though, but...
  116.   cal.create(parent);
  117.  
  118.   // ... we can show it here.
  119.   cal.show();
  120. }
  121. </script>
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement