Recent Posts
JavaScript | 17 sec ago
Java | 53 sec ago
None | 57 sec ago
None | 1 min ago
PHP | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Python | 2 min ago
PHP | 3 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Dan on the 24th of Nov 2009 10:59:14 PM Download | Raw | Embed | Report
  1. /* MarcGrabanski.com */
  2.  
  3. /* Pop-Up Calendar Built from Scratch by Marc Grabanski */
  4.  
  5. var popUpCal = {
  6.     selectedMonth: new Date().getMonth(), // 0-11
  7.     selectedYear: new Date().getFullYear(), // 4-digit year
  8.     selectedDay: new Date().getDate(),
  9.     calendarId: 'calendarDiv',
  10.     inputClass: 'calendarSelectDate',
  11.    
  12.         init: function () {
  13.         var x = getElementsByClass(popUpCal.inputClass, document, 'input');
  14.         var y = document.getElementById(popUpCal.calendarId);
  15.         // set the calendar position based on the input position
  16.         for (var i=0; i<x.length; i++) {
  17.             x[i].onfocus = function () {
  18.                                 popUpCal.selectedMonth = new Date().getMonth();
  19.                 setPos(this, y); // setPos(targetObj,moveObj)
  20.                 y.style.display = 'block';
  21.                 popUpCal.drawCalendar(this);
  22.                 popUpCal.setupLinks(this);
  23.             }
  24.         }
  25.     },
  26.    
  27.     drawCalendar: function (inputObj) {
  28.                
  29.                 var html = '';
  30.                 html = '<a id="closeCalender">Close Calendar</a>';
  31.                 html += '<table cellpadding="0" cellspacing="0" id="linksTable"><tr>';
  32.         html += '       <td><a id="prevMonth">&laquo Prev</a></td>';
  33.                 html += '       <td><a id="nextMonth">Next &raquo</a></td>';
  34.                 html += '</tr></table>';
  35.                 html += '<table id="calendar" cellpadding="0" cellspacing="0"><tr>';
  36.                 html += '<th colspan="7" class="calendarHeader">'+getMonthName(popUpCal.selectedMonth)+' '+popUpCal.selectedYear+'</th>';
  37.                 html += '</tr><tr class="weekDaysTitleRow">';
  38.         var weekDays = new Array('S','M','T','W','T','F','S');
  39.         for (var j=0; j<weekDays.length; j++) {
  40.                         html += '<td>'+weekDays[j]+'</td>';
  41.         }
  42.                
  43.         var daysInMonth = getDaysInMonth(popUpCal.selectedYear, popUpCal.selectedMonth);
  44.         var startDay = getFirstDayofMonth(popUpCal.selectedYear, popUpCal.selectedMonth);
  45.         var numRows = 0;
  46.         var printDate = 1;
  47.         if (startDay != 7) {
  48.             numRows = Math.ceil(((startDay+1)+(daysInMonth))/7); // calculate the number of rows to generate
  49.         }
  50.                
  51.         // calculate number of days before calendar starts
  52.         if (startDay != 7) {
  53.             var noPrintDays = startDay + 1;
  54.         } else {
  55.             var noPrintDays = 0; // if sunday print right away 
  56.         }
  57.                 var today = new Date().getDate();
  58.                 var thisMonth = new Date().getMonth();
  59.                 var thisYear = new Date().getFullYear();
  60.         // create calendar rows
  61.         for (var e=0; e<numRows; e++) {
  62.                         html += '<tr class="weekDaysRow">';
  63.             // create calendar days
  64.             for (var f=0; f<7; f++) {
  65.                                 if ( (printDate == today)
  66.                                          && (popUpCal.selectedYear == thisYear)
  67.                                          && (popUpCal.selectedMonth == thisMonth)
  68.                                          && (noPrintDays == 0)) {
  69.                                         html += '<td class="weekDaysCell" id="today">'; // today
  70.                                 } else if ( ( (printDate < today)
  71.                                         && (popUpCal.selectedYear == thisYear)
  72.                                         && (popUpCal.selectedMonth == thisMonth)
  73.                                         && (noPrintDays == 0))
  74.                                         || ( (popUpCal.selectedMonth < thisMonth)
  75.                                         && (popUpCal.selectedYear == thisYear)
  76.                                         && (noPrintDays == 0))
  77.                                         || ( (popUpCal.selectedYear < thisYear)
  78.                                         && (noPrintDays == 0)))
  79.                                 {
  80.                         html += '<td class="weekDaysCellPast" >'; // past days
  81.                                 }
  82.                                 else {
  83.                                         html += '<td class="weekDaysCell" >'; // future days
  84.                                 }
  85.                 if (noPrintDays == 0) {
  86.                                         if (printDate <= daysInMonth) {
  87.                                                 if ( ( (printDate < today)
  88.                                                         && (popUpCal.selectedYear == thisYear)
  89.                                                         && (popUpCal.selectedMonth == thisMonth)
  90.                                                         && (noPrintDays == 0))
  91.                                                         || ( (popUpCal.selectedMonth < thisMonth)
  92.                                                         && (popUpCal.selectedYear == thisYear)
  93.                                                         && (noPrintDays == 0))
  94.                                                         || ( (popUpCal.selectedYear < thisYear)
  95.                                                         && (noPrintDays == 0))) {
  96.                                                                 html += printDate; // no link for days in the past
  97.                                                 }
  98.                                                 else {
  99.                                                         html += '<a>'+printDate+'</a>'; // link for current & future days
  100.                                                 }
  101.                                         }
  102.                     printDate++;
  103.                 }
  104.                 html += '</td>';
  105.                 if(noPrintDays > 0) noPrintDays--;
  106.             }
  107.             html += '</tr>';
  108.         }
  109.                 html += '</table>';
  110.        
  111.         // add calendar to element to calendar Div
  112.         var calendarDiv = document.getElementById(popUpCal.calendarId);
  113.         calendarDiv.innerHTML = html;
  114.        
  115.         // close button link
  116.         document.getElementById('closeCalender').onclick = function () {
  117.             calendarDiv.style.display = 'none';
  118.         }
  119.                 // setup next and previous links
  120.         document.getElementById('prevMonth').onclick = function () {
  121.             popUpCal.selectedMonth--;
  122.             if (popUpCal.selectedMonth < 0) {
  123.                 popUpCal.selectedMonth = 11;
  124.                 popUpCal.selectedYear--;
  125.             }
  126.             popUpCal.drawCalendar(inputObj);
  127.             popUpCal.setupLinks(inputObj);
  128.         }
  129.         document.getElementById('nextMonth').onclick = function () {
  130.             popUpCal.selectedMonth++;
  131.             if (popUpCal.selectedMonth > 11) {
  132.                 popUpCal.selectedMonth = 0;
  133.                 popUpCal.selectedYear++;
  134.             }
  135.             popUpCal.drawCalendar(inputObj);
  136.             popUpCal.setupLinks(inputObj);
  137.         }
  138.        
  139.     }, // end drawCalendar function
  140.    
  141.     setupLinks: function (inputObj) {
  142.         // set up link events on calendar table
  143.         var y = document.getElementById('calendar');
  144.         var x = y.getElementsByTagName('a');
  145.         for (var i=0; i<x.length; i++) {
  146.             x[i].onmouseover = function () {
  147.                 this.parentNode.className = 'weekDaysCellOver';
  148.             }
  149.             x[i].onmouseout = function () {
  150.                 this.parentNode.className = 'weekDaysCell';
  151.             }
  152.             x[i].onclick = function () {
  153.                 document.getElementById(popUpCal.calendarId).style.display = 'none';
  154.                 popUpCal.selectedDay = this.innerHTML;
  155.                 inputObj.value = formatDate(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear);              
  156.             }
  157.         }
  158.     }
  159.    
  160. }
  161. // Add calendar event that has wide browser support
  162. if ( typeof window.addEventListener != "undefined" )
  163.     window.addEventListener( "load", popUpCal.init, false );
  164. else if ( typeof window.attachEvent != "undefined" )
  165.     window.attachEvent( "onload", popUpCal.init );
  166. else {
  167.     if ( window.onload != null ) {
  168.         var oldOnload = window.onload;
  169.         window.onload = function ( e ) {
  170.             oldOnload( e );
  171.             popUpCal.init();
  172.         };
  173.     }
  174.     else
  175.         window.onload = popUpCal.init;
  176. }
  177.  
  178. /* Functions Dealing with Dates */
  179.  
  180. function formatDate(Day, Month, Year) {
  181.     Month++; // adjust javascript month
  182.     if (Month <10) Month = '0'+Month; // add a zero if less than 10
  183.     if (Day < 10) Day = '0'+Day; // add a zero if less than 10
  184.     var dateString = Month+'/'+Day+'/'+Year;
  185.     return dateString;
  186. }
  187.  
  188. function getMonthName(month) {
  189.     var monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  190.     return monthNames[month];
  191. }
  192.  
  193. function getDayName(day) {
  194.     var dayNames = new Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
  195.     return dayNames[day];
  196. }
  197.  
  198. function getDaysInMonth(year, month) {
  199.     return 32 - new Date(year, month, 32).getDate();
  200. }
  201.  
  202. function getFirstDayofMonth(year, month) {
  203.     var day;
  204.     day = new Date(year, month, 0).getDay();
  205.     return day;
  206. }
  207.  
  208. /* Common Scripts */
  209.  
  210. function getElementsByClass(searchClass,node,tag) {
  211.     var classElements = new Array();
  212.     if ( node == null ) node = document;
  213.     if ( tag == null ) tag = '*';
  214.     var els = node.getElementsByTagName(tag);
  215.     var elsLen = els.length;
  216.     var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  217.     for (i = 0, j = 0; i < elsLen; i++) {
  218.         if ( pattern.test(els[i].className) ) {
  219.             classElements[j] = els[i];
  220.             j++;
  221.         }
  222.     }
  223.     return classElements;
  224. }
  225.  
  226. /* Position Functions */
  227.  
  228. function setPos(targetObj,moveObj) {
  229.     var coors = findPos(targetObj);
  230.     moveObj.style.position = 'absolute';
  231.     moveObj.style.top = coors[1]+20 + 'px';
  232.     moveObj.style.left = coors[0] + 'px';
  233. }
  234.  
  235. function findPos(obj) {
  236.     var curleft = curtop = 0;
  237.     if (obj.offsetParent) {
  238.         curleft = obj.offsetLeft
  239.         curtop = obj.offsetTop
  240.         while (obj = obj.offsetParent) {
  241.             curleft += obj.offsetLeft
  242.             curtop += obj.offsetTop
  243.         }
  244.     }
  245.     return [curleft,curtop];
  246. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: