Advertisement
Guest User

Untitled

a guest
May 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 34.61 KB | None | 0 0
  1. <script type="text/javascript">
  2. var bas_cal,dp_cal,ms_cal;      
  3. window.onload = function () {
  4. dp_cal  = new Epoch('epoch_popup','popup',document.getElementById('invc_date'));
  5. dp_cal  = new Epoch('epoch_popup','popup',document.getElementById('due_date'));
  6. }
  7.  
  8. </script>
  9.  
  10. <INPUT id="invc_date" NAME="invc_date" TYPE="text" size="15" readonly="yes" />
  11. <script type="text/javascript" src="../calendar.js"></script>
  12.  
  13.  
  14.  
  15. //////////////////////////////////////////////////calendar.js/////////////////////////
  16.  
  17. /*****************************************************************************
  18. Copyright (C) 2006  Nick Baicoianu
  19.  
  20. This program is free software; you can redistribute it and/or
  21. modify it under the terms of the GNU General Public License
  22. as published by the Free Software Foundation; either version 2
  23. of the License, or (at your option) any later version.
  24.  
  25. This program is distributed in the hope that it will be useful,
  26. but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. GNU General Public License for more details.
  29.  
  30. You should have received a copy of the GNU General Public License
  31. along with this program; if not, write to the Free Software
  32. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  33. *****************************************************************************/
  34. //constructor for the main Epoch class (ENGLISH VERSION)
  35. function Epoch(name,mode,targetelement,multiselect)
  36. {
  37.   this.state = 0;
  38.   this.name = name;
  39.   this.curDate = new Date();
  40.   this.mode = mode;
  41.   this.selectMultiple = (multiselect == true); //'false' is not true or not set at all
  42.  
  43.   //the various calendar variables
  44.   //this.selectedDate = this.curDate;
  45.   this.selectedDates = new Array();
  46.   this.calendar;
  47.   this.calHeading;
  48.   this.calCells;
  49.   this.rows;
  50.   this.cols;
  51.   this.cells = new Array();
  52.  
  53.   //The controls
  54.   this.monthSelect;
  55.   this.yearSelect;
  56.  
  57.   //standard initializations
  58.   this.mousein = false;
  59.   this.calConfig();
  60.   this.setDays();
  61.   this.displayYear = this.displayYearInitial;
  62.   this.displayMonth = this.displayMonthInitial;
  63.  
  64.   this.createCalendar(); //create the calendar DOM element and its children, and their related objects
  65.  
  66.   if(this.mode == 'popup' && targetelement && targetelement.type == 'text') //if the target element has been set to be an input text box
  67.   {
  68.     this.tgt = targetelement;
  69.     this.calendar.style.position = 'absolute';
  70.     this.topOffset = this.tgt.offsetHeight; // the vertical distance (in pixels) to display the calendar from the Top of its input element
  71.     this.leftOffset = 0;           // the horizontal distance (in pixels) to display the calendar from the Left of its input element
  72.     this.calendar.style.top = this.getTop(targetelement) + this.topOffset + 'px';
  73.     this.calendar.style.left = this.getLeft(targetelement) + this.leftOffset + 'px';
  74.     document.body.appendChild(this.calendar);
  75.     this.tgt.calendar = this;
  76.     this.tgt.onfocus = function () {this.calendar.show();}; //the calendar will popup when the input element is focused
  77.     this.tgt.onblur = function () {if(!this.calendar.mousein){this.calendar.hide();}}; //the calendar will popup when the input element is focused
  78.   }
  79.   else
  80.   {
  81.     this.container = targetelement;
  82.     this.container.appendChild(this.calendar);
  83.   }
  84.  
  85.   this.state = 2; //0: initializing, 1: redrawing, 2: finished!
  86.   this.visible ? this.show() : this.hide();
  87. }
  88. //-----------------------------------------------------------------------------
  89. Epoch.prototype.calConfig = function () //PRIVATE: initialize calendar variables
  90. {
  91.   //this.mode = 'flat'; //can be 'flat' or 'popup'
  92.   this.displayYearInitial = this.curDate.getFullYear(); //the initial year to display on load
  93.   this.displayMonthInitial = this.curDate.getMonth(); //the initial month to display on load (0-11)
  94.   this.rangeYearLower = 2005;
  95.   this.rangeYearUpper = 2037;
  96.   this.minDate = new Date(2005,0,1);
  97.   this.maxDate = new Date(2037,0,1);
  98.   this.startDay = 0; // the day the week will 'start' on: 0(Sun) to 6(Sat)
  99.   this.showWeeks = true; //whether the week numbers will be shown
  100.   this.selCurMonthOnly = false; //allow user to only select dates in the currently displayed month
  101.   this.clearSelectedOnChange = true; //whether to clear all selected dates when changing months
  102.  
  103.   //flat mode-only settings:
  104.   //this.selectMultiple = true; //whether the user can select multiple dates (flat mode only)
  105.  
  106.   switch(this.mode) //set the variables based on the calendar mode
  107.   {
  108.     case 'popup': //popup options
  109.       this.visible = false;
  110.       break;
  111.     case 'flat':
  112.       this.visible = true;
  113.      
  114.       break;
  115.   }
  116.   this.setLang();
  117. };
  118. //-----------------------------------------------------------------------------
  119. Epoch.prototype.setLang = function()  //all language settings for Epoch are made here.  Check Date.dateFormat() for the Date object's language settings
  120. {
  121.   this.daylist = new Array('Su','Mo','Tu','We','Th','Fr','Sa','Su','Mo','Tu','We','Th','Fr','Sa'); /*<lang:en>*/
  122.   this.months_sh = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  123.   this.monthup_title = 'Go to the next month';
  124.   this.monthdn_title = 'Go to the previous month';
  125.   this.clearbtn_caption = 'Clear';
  126.   this.clearbtn_title = 'Clears any dates selected on the calendar';
  127.   this.maxrange_caption = 'This is the maximum range';
  128. };
  129. //-----------------------------------------------------------------------------
  130. Epoch.prototype.getTop = function (element) //PRIVATE: returns the absolute Top value of element, in pixels
  131. {
  132.     var oNode = element;
  133.     var iTop = 0;
  134.    
  135.     while(oNode.tagName != 'BODY') {
  136.         iTop += oNode.offsetTop;
  137.         oNode = oNode.offsetParent;
  138.     }
  139.    
  140.     return iTop;
  141. };
  142. //-----------------------------------------------------------------------------
  143. Epoch.prototype.getLeft = function (element) //PRIVATE: returns the absolute Left value of element, in pixels
  144. {
  145.     var oNode = element;
  146.     var iLeft = 0;
  147.    
  148.     while(oNode.tagName != 'BODY') {
  149.         iLeft += oNode.offsetLeft;
  150.         oNode = oNode.offsetParent;        
  151.     }
  152.    
  153.     return iLeft;
  154. };
  155. //-----------------------------------------------------------------------------
  156. Epoch.prototype.show = function () //PUBLIC: displays the calendar
  157. {
  158.   this.calendar.style.display = 'block';
  159.   this.visible = true;
  160. };
  161. //-----------------------------------------------------------------------------
  162. Epoch.prototype.hide = function () //PUBLIC: Hides the calendar
  163. {
  164.   this.calendar.style.display = 'none';
  165.   this.visible = false;
  166. };
  167. //-----------------------------------------------------------------------------
  168. Epoch.prototype.toggle = function () //PUBLIC: Toggles (shows/hides) the calendar depending on its current state
  169. {
  170.   if(this.visible) {
  171.     this.hide();
  172.   }
  173.   else {
  174.     this.show();
  175.   }
  176. };
  177. //-----------------------------------------------------------------------------
  178. Epoch.prototype.setDays = function ()  //PRIVATE: initializes the standard Gregorian Calendar parameters
  179. {
  180.   this.daynames = new Array();
  181.   var j=0;
  182.   for(var i=this.startDay; i< this.startDay + 7;i++) {
  183.     this.daynames[j++] = this.daylist[i];
  184.   }
  185.    
  186.   this.monthDayCount = new Array(31,((this.curDate.getFullYear() - 2000) % 4 ? 28 : 29),31,30,31,30,31,31,30,31,30,31);
  187. };
  188. //-----------------------------------------------------------------------------
  189. Epoch.prototype.setClass = function (element,className) //PRIVATE: sets the CSS class of the element, W3C & IE
  190. {
  191.   element.setAttribute('class',className);
  192.   element.setAttribute('className',className); //<iehack>
  193. };
  194. //-----------------------------------------------------------------------------
  195. Epoch.prototype.createCalendar = function ()  //PRIVATE: creates the full DOM implementation of the calendar
  196. {
  197.   var tbody, tr, td;
  198.   this.calendar = document.createElement('table');
  199.   this.calendar.setAttribute('id',this.name+'_calendar');
  200.   this.setClass(this.calendar,'calendar');
  201.   //to prevent IE from selecting text when clicking on the calendar
  202.   this.calendar.onselectstart = function() {return false;};
  203.   this.calendar.ondrag = function() {return false;};
  204.   tbody = document.createElement('tbody');
  205.  
  206.   //create the Main Calendar Heading
  207.   tr = document.createElement('tr');
  208.   td = document.createElement('td');
  209.   td.appendChild(this.createMainHeading());
  210.   tr.appendChild(td);
  211.   tbody.appendChild(tr);
  212.  
  213.   //create the calendar Day Heading
  214.   tr = document.createElement('tr');
  215.   td = document.createElement('td');
  216.   td.appendChild(this.createDayHeading());
  217.   tr.appendChild(td);
  218.   tbody.appendChild(tr);
  219.  
  220.   //create the calendar Day Cells
  221.   tr = document.createElement('tr');
  222.   td = document.createElement('td');
  223.   td.setAttribute('id',this.name+'_cell_td');
  224.   this.calCellContainer = td;  //used as a handle for manipulating the calendar cells as a whole
  225.   td.appendChild(this.createCalCells());
  226.   tr.appendChild(td);
  227.   tbody.appendChild(tr);
  228.  
  229.   //create the calendar footer
  230.   tr = document.createElement('tr');
  231.   td = document.createElement('td');
  232.   td.appendChild(this.createFooter());
  233.   tr.appendChild(td);
  234.   tbody.appendChild(tr);
  235.  
  236.   //add the tbody element to the main calendar table
  237.   this.calendar.appendChild(tbody);
  238.  
  239.   //and add the onmouseover events to the calendar table
  240.   this.calendar.owner = this;
  241.   this.calendar.onmouseover = function() {this.owner.mousein = true;};
  242.   this.calendar.onmouseout = function() {this.owner.mousein = false;};
  243. };
  244. //-----------------------------------------------------------------------------
  245. Epoch.prototype.createMainHeading = function () //PRIVATE: Creates the primary calendar heading, with months & years
  246. {
  247.   //create the containing <div> element
  248.   var container = document.createElement('div');
  249.   container.setAttribute('id',this.name+'_mainheading');
  250.   this.setClass(container,'mainheading');
  251.   //create the child elements and other variables
  252.   this.monthSelect = document.createElement('select');
  253.   this.yearSelect = document.createElement('select');
  254.   var monthDn = document.createElement('input'), monthUp = document.createElement('input');
  255.   var opt, i;
  256.   //fill the month select box
  257.   for(i=0;i<12;i++)
  258.   {
  259.     opt = document.createElement('option');
  260.     opt.setAttribute('value',i);
  261.     if(this.state == 0 && this.displayMonth == i) {
  262.       opt.setAttribute('selected','selected');
  263.     }
  264.     opt.appendChild(document.createTextNode(this.months_sh[i]));
  265.     this.monthSelect.appendChild(opt);
  266.   }
  267.   //and fill the year select box
  268.   for(i=this.rangeYearLower;i<=this.rangeYearUpper;i++)
  269.   {
  270.     opt = document.createElement('option');
  271.     opt.setAttribute('value',i);
  272.     if(this.state == 0 && this.displayYear == i) {
  273.       opt.setAttribute('selected','selected');
  274.     }
  275.     opt.appendChild(document.createTextNode(i));
  276.     this.yearSelect.appendChild(opt);    
  277.   }
  278.   //add the appropriate children for the month buttons
  279.   monthUp.setAttribute('type','button');
  280.   monthUp.setAttribute('value','>');
  281.   monthUp.setAttribute('title',this.monthup_title);
  282.   monthDn.setAttribute('type','button');
  283.   monthDn.setAttribute('value','<');
  284.   monthDn.setAttribute('title',this.monthdn_title);
  285.   this.monthSelect.owner = this.yearSelect.owner = monthUp.owner = monthDn.owner = this;  //hack to allow us to access this calendar in the events (<fix>??)
  286.  
  287.   //assign the event handlers for the controls
  288.   monthUp.onmouseup = function () {this.owner.nextMonth();};
  289.   monthDn.onmouseup = function () {this.owner.prevMonth();};
  290.   this.monthSelect.onchange = function() {
  291.     this.owner.displayMonth = this.value;
  292.     this.owner.displayYear = this.owner.yearSelect.value;
  293.     this.owner.goToMonth(this.owner.displayYear,this.owner.displayMonth);
  294.   };
  295.   this.yearSelect.onchange = function() {
  296.     this.owner.displayMonth = this.owner.monthSelect.value;
  297.     this.owner.displayYear = this.value;
  298.     this.owner.goToMonth(this.owner.displayYear,this.owner.displayMonth);
  299.   };
  300.  
  301.   //and finally add the elements to the containing div
  302.   container.appendChild(monthDn);
  303.   container.appendChild(this.monthSelect);
  304.   container.appendChild(this.yearSelect);
  305.   container.appendChild(monthUp);
  306.   return container;
  307. };
  308. //-----------------------------------------------------------------------------
  309. Epoch.prototype.createFooter = function () //PRIVATE: creates the footer of the calendar - goes under the calendar cells
  310. {
  311.   var container = document.createElement('div');
  312.   var clearSelected = document.createElement('input');
  313.   clearSelected.setAttribute('type','button');
  314.   clearSelected.setAttribute('value',this.clearbtn_caption);
  315.   clearSelected.setAttribute('title',this.clearbtn_title);
  316.   clearSelected.owner = this;
  317.   clearSelected.onclick = function() { this.owner.resetSelections(false);};
  318.   container.appendChild(clearSelected);
  319.   return container;
  320. };
  321. //-----------------------------------------------------------------------------
  322. Epoch.prototype.resetSelections = function (returnToDefaultMonth)  //PRIVATE: reset the calendar's selection variables to defaults
  323. {
  324.   this.selectedDates = new Array();
  325.   this.rows = new Array(false,false,false,false,false,false,false);
  326.   this.cols = new Array(false,false,false,false,false,false,false);
  327.   if(this.tgt)  //if there is a target element, clear it too
  328.   {
  329.     this.tgt.value = '';
  330.     if(this.mode == 'popup') {//hide the calendar if in popup mode
  331.       this.hide();
  332.     }
  333.   }
  334.    
  335.   if(returnToDefaultMonth == true) {
  336.     this.goToMonth(this.displayYearInitial,this.displayMonthInitial);
  337.   }
  338.   else {
  339.     this.reDraw();
  340.   }
  341. };
  342. //-----------------------------------------------------------------------------
  343. Epoch.prototype.createDayHeading = function ()  //PRIVATE: creates the heading containing the day names
  344. {
  345.   //create the table element
  346.   this.calHeading = document.createElement('table');
  347.   this.calHeading.setAttribute('id',this.name+'_caldayheading');
  348.   this.setClass(this.calHeading,'caldayheading');
  349.   var tbody,tr,td;
  350.   tbody = document.createElement('tbody');
  351.   tr = document.createElement('tr');
  352.   this.cols = new Array(false,false,false,false,false,false,false);
  353.  
  354.   //if we're showing the week headings, create an empty <td> for filler
  355.   if(this.showWeeks)
  356.   {
  357.     td = document.createElement('td');
  358.     td.setAttribute('class','wkhead');
  359.     td.setAttribute('className','wkhead'); //<iehack>
  360.     tr.appendChild(td);
  361.   }
  362.   //populate the day titles
  363.   for(var dow=0;dow<7;dow++)
  364.   {
  365.     td = document.createElement('td');
  366.     td.appendChild(document.createTextNode(this.daynames[dow]));
  367.     if(this.selectMultiple) { //if selectMultiple is true, assign the cell a CalHeading Object to handle all events
  368.       td.headObj = new CalHeading(this,td,(dow + this.startDay < 7 ? dow + this.startDay : dow + this.startDay - 7));
  369.     }
  370.     tr.appendChild(td);
  371.   }
  372.   tbody.appendChild(tr);
  373.   this.calHeading.appendChild(tbody);
  374.   return this.calHeading;  
  375. };
  376. //-----------------------------------------------------------------------------
  377. Epoch.prototype.createCalCells = function ()  //PRIVATE: creates the table containing the calendar day cells
  378. {
  379.   this.rows = new Array(false,false,false,false,false,false);
  380.   this.cells = new Array();
  381.   var row = -1, totalCells = (this.showWeeks ? 48 : 42);
  382.   var beginDate = new Date(this.displayYear,this.displayMonth,1);
  383.   var endDate = new Date(this.displayYear,this.displayMonth,this.monthDayCount[this.displayMonth]);
  384.   var sdt = new Date(beginDate);
  385.   sdt.setDate(sdt.getDate() + (this.startDay - beginDate.getDay()) - (this.startDay - beginDate.getDay() > 0 ? 7 : 0) );
  386.   //create the table element
  387.   this.calCells = document.createElement('table');
  388.   this.calCells.setAttribute('id',this.name+'_calcells');
  389.   this.setClass(this.calCells,'calcells');
  390.   var tbody,tr,td;
  391.   tbody = document.createElement('tbody');
  392.   for(var i=0;i<totalCells;i++)
  393.   {
  394.     if(this.showWeeks) //if we are showing the week headings
  395.     {
  396.       if(i % 8 == 0)
  397.       {
  398.         row++;
  399.         tr = document.createElement('tr');
  400.         td = document.createElement('td');
  401.         if(this.selectMultiple) { //if selectMultiple is enabled, create the associated weekObj objects
  402.           td.weekObj = new WeekHeading(this,td,sdt.getWeek(),row)
  403.         }
  404.         else //otherwise just set the class of the td for consistent look
  405.         {
  406.           td.setAttribute('class','wkhead');
  407.           td.setAttribute('className','wkhead'); //<iehack>
  408.         }
  409.         td.appendChild(document.createTextNode(sdt.getWeek()));      
  410.         tr.appendChild(td);
  411.         i++;
  412.       }
  413.     }
  414.     else if(i % 7 == 0) //otherwise, new row every 7 cells
  415.     {
  416.       row++;
  417.       tr = document.createElement('tr');
  418.     }
  419.     //create the day cells
  420.     td = document.createElement('td');
  421.     td.appendChild(document.createTextNode(sdt.getDate()));// +' ' +sdt.getUeDay()));
  422.     var cell = new CalCell(this,td,sdt,row);
  423.     this.cells.push(cell);
  424.     td.cellObj = cell;
  425.     sdt.setDate(sdt.getDate() + 1); //increment the date
  426.     tr.appendChild(td);
  427.     tbody.appendChild(tr);
  428.   }
  429.   this.calCells.appendChild(tbody);
  430.   this.reDraw();
  431.   return this.calCells;
  432. };
  433. //-----------------------------------------------------------------------------
  434. Epoch.prototype.reDraw = function () //PRIVATE: reapplies all the CSS classes for the calendar cells, usually called after chaning their state
  435. {
  436.   this.state = 1;
  437.   var i,j;
  438.   for(i=0;i<this.cells.length;i++) {
  439.     this.cells[i].selected = false;
  440.   }
  441.   for(i=0;i<this.cells.length;i++)
  442.   {
  443.     for(j=0;j<this.selectedDates.length;j++) { //if the cell's date is in the selectedDates array, set its selected property to true
  444.       if(this.cells[i].date.getUeDay() == this.selectedDates[j].getUeDay() ) {
  445.         this.cells[i].selected = true;
  446.       }
  447.     }
  448.  
  449.     this.cells[i].setClass();
  450.   }
  451.   //alert(this.selectedDates);
  452.   this.state = 2;
  453. };
  454. //-----------------------------------------------------------------------------
  455. Epoch.prototype.deleteCells = function () //PRIVATE: removes the calendar cells from the DOM (does not delete the cell objects associated with them
  456. {
  457.   this.calCellContainer.removeChild(this.calCellContainer.firstChild); //get a handle on the cell table (optional - for less indirection)
  458.   this.cells = new Array(); //reset the cells array
  459. };
  460. //-----------------------------------------------------------------------------
  461. Epoch.prototype.goToMonth = function (year,month) //PUBLIC: sets the calendar to display the requested month/year
  462. {
  463.   this.monthSelect.value = this.displayMonth = month;
  464.   this.yearSelect.value = this.displayYear = year;
  465.   this.deleteCells();
  466.   this.calCellContainer.appendChild(this.createCalCells());
  467. };
  468. //-----------------------------------------------------------------------------
  469. Epoch.prototype.nextMonth = function () //PUBLIC: go to the next month.  if the month is december, go to january of the next year
  470. {
  471.  
  472.   //increment the month/year values, provided they're within the min/max ranges
  473.   if(this.monthSelect.value < 11) {
  474.     this.monthSelect.value++;
  475.   }
  476.   else
  477.   {
  478.     if(this.yearSelect.value < this.rangeYearUpper)
  479.     {
  480.       this.monthSelect.value = 0;
  481.       this.yearSelect.value++;
  482.     }
  483.     else {
  484.       alert(this.maxrange_caption);
  485.     }
  486.   }
  487.   //assign the currently displaying month/year values
  488.   this.displayMonth = this.monthSelect.value;
  489.   this.displayYear = this.yearSelect.value;
  490.  
  491.   //and refresh the calendar for the new month/year
  492.   this.deleteCells();
  493.   this.calCellContainer.appendChild(this.createCalCells());
  494. };
  495. //-----------------------------------------------------------------------------
  496. Epoch.prototype.prevMonth = function () //PUBLIC: go to the previous month.  if the month is january, go to december of the previous year
  497. {
  498.   //increment the month/year values, provided they're within the min/max ranges
  499.   if(this.monthSelect.value > 0)
  500.     this.monthSelect.value--;
  501.   else
  502.   {
  503.     if(this.yearSelect.value > this.rangeYearLower)
  504.     {
  505.       this.monthSelect.value = 11;
  506.       this.yearSelect.value--;
  507.     }
  508.     else {
  509.       alert(this.maxrange_caption);
  510.     }
  511.   }
  512.  
  513.   //assign the currently displaying month/year values
  514.   this.displayMonth = this.monthSelect.value;
  515.   this.displayYear = this.yearSelect.value;
  516.  
  517.   //and refresh the calendar for the new month/year
  518.   this.deleteCells();
  519.   this.calCellContainer.appendChild(this.createCalCells());
  520. };
  521. //-----------------------------------------------------------------------------
  522. Epoch.prototype.addZero = function (vNumber) //PRIVATE: pads a 2 digit number with a leading zero
  523. {
  524.   return ((vNumber < 10) ? '0' : '') + vNumber;
  525. };
  526. //-----------------------------------------------------------------------------
  527. Epoch.prototype.addDates = function (dates,redraw)  //PUBLIC: adds the array "dates" to the calendars selectedDates array (no duplicate dates) and redraws the calendar
  528. {
  529.   var j,in_sd;
  530.   for(var i=0;i<dates.length;i++)
  531.   {  
  532.     in_sd = false;
  533.     for(j=0;j<this.selectedDates.length;j++)
  534.     {
  535.       if(dates[i].getUeDay() == this.selectedDates[j].getUeDay())
  536.       {
  537.         in_sd = true;
  538.         break;
  539.       }
  540.     }
  541.     if(!in_sd) { //if the date isn't already in the array, add it!
  542.       this.selectedDates.push(dates[i]);
  543.     }
  544.   }
  545.   if(redraw != false) {//redraw  the calendar if "redraw" is false or undefined
  546.     this.reDraw();
  547.   }
  548. };
  549. //-----------------------------------------------------------------------------
  550. Epoch.prototype.removeDates = function (dates,redraw)  //PUBLIC: adds the dates to the calendars selectedDates array and redraws the calendar
  551. {
  552.   var j;
  553.   for(var i=0;i<dates.length;i++)
  554.   {
  555.     for(j=0;j<this.selectedDates.length;j++)
  556.     {
  557.       if(dates[i].getUeDay() == this.selectedDates[j].getUeDay()) { //search for the dates in the selectedDates array, removing them if the dates match
  558.         this.selectedDates.splice(j,1);
  559.       }
  560.     }
  561.   }
  562.   if(redraw != false) { //redraw  the calendar if "redraw" is false or undefined
  563.     this.reDraw();
  564.   }
  565. };
  566. //-----------------------------------------------------------------------------
  567. Epoch.prototype.outputDate = function (vDate, vFormat) //PUBLIC: outputs a date in the appropriate format (DEPRECATED)
  568. {
  569.   var vDay      = this.addZero(vDate.getDate());
  570.   var vMonth      = this.addZero(vDate.getMonth() + 1);
  571.   var vYearLong    = this.addZero(vDate.getFullYear());
  572.   var vYearShort    = this.addZero(vDate.getFullYear().toString().substring(3,4));
  573.   var vYear      = (vFormat.indexOf('yyyy') > -1 ? vYearLong : vYearShort);
  574.   var vHour      = this.addZero(vDate.getHours());
  575.   var vMinute      = this.addZero(vDate.getMinutes());
  576.   var vSecond      = this.addZero(vDate.getSeconds());
  577.   return vFormat.replace(/dd/g, vDay).replace(/mm/g, vMonth).replace(/y{1,4}/g, vYear).replace(/hh/g, vHour).replace(/nn/g, vMinute).replace(/ss/g, vSecond);
  578. };
  579. //----------------------------------------------------------------------------
  580. Epoch.prototype.updatePos = function (target) //PUBLIC: moves the calendar's position to target's location (popup mode only)
  581. {
  582.   this.calendar.style.top = this.getTop(target) + this.topOffset + 'px'
  583.   this.calendar.style.left = this.getLeft(target) + this.leftOffset + 'px'
  584. }
  585. //-----------------------------------------------------------------------------
  586.  
  587. /*****************************************************************************/
  588. function CalHeading(owner,tableCell,dow)
  589. {
  590.   this.owner = owner;
  591.   this.tableCell = tableCell;
  592.   this.dayOfWeek = dow;
  593.  
  594.   //the event handlers
  595.   this.tableCell.onclick = this.onclick;
  596. }
  597. //-----------------------------------------------------------------------------
  598. CalHeading.prototype.onclick = function ()
  599. {
  600.   //reduce indirection:
  601.   var owner = this.headObj.owner;
  602.   var sdates = owner.selectedDates;
  603.   var cells = owner.cells;
  604.  
  605.   owner.cols[this.headObj.dayOfWeek] = !owner.cols[this.headObj.dayOfWeek];
  606.   for(var i=0;i<cells.length;i++) //cycle through all the cells in the calendar, selecting all cells with the same dayOfWeek as this heading
  607.   {
  608.     if(cells[i].dayOfWeek == this.headObj.dayOfWeek && (!owner.selCurMonthOnly || cells[i].date.getMonth() == owner.displayMonth && cells[i].date.getFullYear() == owner.displayYear)) //if the cell's DoW matches, with other conditions
  609.     {
  610.       if(owner.cols[this.headObj.dayOfWeek])     //if selecting, add the cell's date to the selectedDates array
  611.       {
  612.         if(owner.selectedDates.arrayIndex(cells[i].date) == -1) { //if the date isn't already in the array
  613.           sdates.push(cells[i].date);
  614.         }
  615.       }
  616.       else                    //otherwise, remove it
  617.       {
  618.         for(var j=0;j<sdates.length;j++)
  619.         {
  620.           if(cells[i].dayOfWeek == sdates[j].getDay())
  621.           {
  622.             sdates.splice(j,1);  //remove dates that are within the displaying month/year that have the same day of week as the day cell
  623.             break;
  624.           }
  625.         }
  626.       }
  627.       cells[i].selected = owner.cols[this.headObj.dayOfWeek];
  628.     }
  629.   }
  630.   owner.reDraw();
  631. };
  632. /*****************************************************************************/
  633. function WeekHeading(owner,tableCell,week,row)
  634. {
  635.   this.owner = owner;
  636.   this.tableCell = tableCell;
  637.   this.week = week;
  638.   this.tableRow = row;
  639.   this.tableCell.setAttribute('class','wkhead');
  640.   this.tableCell.setAttribute('className','wkhead'); //<iehack>
  641.   //the event handlers
  642.   this.tableCell.onclick = this.onclick;
  643. }
  644. //-----------------------------------------------------------------------------
  645. WeekHeading.prototype.onclick = function ()
  646. {
  647.   //reduce indirection:
  648.   var owner = this.weekObj.owner;
  649.   var cells = owner.cells;
  650.   var sdates = owner.selectedDates;
  651.   var i,j;
  652.   owner.rows[this.weekObj.tableRow] = !owner.rows[this.weekObj.tableRow];
  653.   for(i=0;i<cells.length;i++)
  654.   {
  655.     if(cells[i].tableRow == this.weekObj.tableRow)
  656.     {
  657.       if(owner.rows[this.weekObj.tableRow] && (!owner.selCurMonthOnly || cells[i].date.getMonth() == owner.displayMonth && cells[i].date.getFullYear() == owner.displayYear)) //match all cells in the current row, with option to restrict to current month only
  658.       {
  659.         if(owner.selectedDates.arrayIndex(cells[i].date) == -1) {//if the date isn't already in the array
  660.           sdates.push(cells[i].date);
  661.         }
  662.       }
  663.       else                    //otherwise, remove it
  664.       {
  665.         for(j=0;j<sdates.length;j++)
  666.         {
  667.           if(sdates[j].getTime() == cells[i].date.getTime())  //this.weekObj.tableRow && sdates[j].getMonth() == owner.displayMonth && sdates[j].getFullYear() == owner.displayYear)
  668.           {
  669.             sdates.splice(j,1);  //remove dates that are within the displaying month/year that have the same day of week as the day cell
  670.             break;
  671.           }
  672.         }
  673.       }
  674.     }
  675.   }
  676.   owner.reDraw();
  677. };
  678. /*****************************************************************************/
  679. //-----------------------------------------------------------------------------
  680. function CalCell(owner,tableCell,dateObj,row)
  681. {
  682.   this.owner = owner;    //used primarily for event handling
  683.   this.tableCell = tableCell;       //the link to this cell object's table cell in the DOM
  684.   this.cellClass;      //the CSS class of the cell
  685.   this.selected = false;  //whether the cell is selected (and is therefore stored in the owner's selectedDates array)
  686.   this.date = new Date(dateObj);
  687.   this.dayOfWeek = this.date.getDay();
  688.   this.week = this.date.getWeek();
  689.   this.tableRow = row;
  690.  
  691.   //assign the event handlers for the table cell element
  692.   this.tableCell.onclick = this.onclick;
  693.   this.tableCell.onmouseover = this.onmouseover;
  694.   this.tableCell.onmouseout = this.onmouseout;
  695.  
  696.   //and set the CSS class of the table cell
  697.   this.setClass();
  698. }
  699. //-----------------------------------------------------------------------------
  700. CalCell.prototype.onmouseover = function () //replicate CSS :hover effect for non-supporting browsers <iehack>
  701. {
  702.   this.setAttribute('class',this.cellClass + ' hover');
  703.   this.setAttribute('className',this.cellClass + ' hover');
  704. };
  705. //-----------------------------------------------------------------------------
  706. CalCell.prototype.onmouseout = function () //replicate CSS :hover effect for non-supporting browsers <iehack>
  707. {
  708.   this.cellObj.setClass();
  709. };
  710. //-----------------------------------------------------------------------------
  711. CalCell.prototype.onclick = function ()
  712. {
  713.   //reduce indirection:
  714.   var cell = this.cellObj;
  715.   var owner = cell.owner;
  716.   if(!owner.selCurMonthOnly || cell.date.getMonth() == owner.displayMonth && cell.date.getFullYear() == owner.displayYear)
  717.   {
  718.     if(owner.selectMultiple == true)  //if we can select multiple cells simultaneously, add the currently selected cell's date to the selectedDates array
  719.     {
  720.       if(!cell.selected) //if this cell has been selected
  721.       {
  722.         if(owner.selectedDates.arrayIndex(cell.date) == -1) {
  723.           owner.selectedDates.push(cell.date);
  724.         }
  725.       }
  726.       else    
  727.       {
  728.         var tmp = owner.selectedDates; // to reduce indirection
  729.         //if the cell has been deselected, remove it from the owner calendar's selectedDates array
  730.         for(var i=0;i<tmp.length;i++)
  731.         {
  732.           if(tmp[i].getUeDay() == cell.date.getUeDay()) {
  733.             tmp.splice(i,1);
  734.           }
  735.         }
  736.       }
  737.     }
  738.     else //if we can only select one cell at a time
  739.     {
  740.       owner.selectedDates = new Array(cell.date);
  741.       if(owner.tgt) //if there is a target element to place the value in, do so
  742.       {
  743.         owner.tgt.value = owner.selectedDates[0].dateFormat();
  744.         if(owner.mode == 'popup') {
  745.           owner.hide();
  746.         }
  747.       }
  748.     }
  749.     owner.reDraw(); //redraw the calendar cell styles to reflect the changes
  750.   }
  751. };
  752. //-----------------------------------------------------------------------------
  753. CalCell.prototype.setClass = function ()  //private: sets the CSS class of the cell based on the specified criteria
  754. {
  755.   if(this.selected) {
  756.     this.cellClass = 'cell_selected';
  757.   }
  758.   else if(this.owner.displayMonth != this.date.getMonth() ) {
  759.     this.cellClass = 'notmnth';  
  760.   }
  761.   else if(this.date.getDay() > 0 && this.date.getDay() < 6) {
  762.     this.cellClass = 'wkday';
  763.   }
  764.   else {
  765.     this.cellClass = 'wkend';
  766.   }
  767.  
  768.   if(this.date.getFullYear() == this.owner.curDate.getFullYear() && this.date.getMonth() == this.owner.curDate.getMonth() && this.date.getDate() == this.owner.curDate.getDate()) {
  769.     this.cellClass = this.cellClass + ' curdate';
  770.   }
  771.  
  772.   this.tableCell.setAttribute('class',this.cellClass);
  773.   this.tableCell.setAttribute('className',this.cellClass); //<iehack>
  774. };
  775. /*****************************************************************************/
  776. Date.prototype.getDayOfYear = function () //returns the day of the year for this date
  777. {
  778.   return parseInt((this.getTime() - new Date(this.getFullYear(),0,1).getTime())/86400000 + 1);
  779. };
  780. //-----------------------------------------------------------------------------
  781. Date.prototype.getWeek = function () //returns the day of the year for this date
  782. {
  783.   return parseInt((this.getTime() - new Date(this.getFullYear(),0,1).getTime())/604800000 + 1);
  784. };
  785. /*function getISOWeek()
  786. {
  787.   var newYear = new Date(this.getFullYear(),0,1);
  788.   var modDay = newYear.getDay();
  789.   if (modDay == 0) modDay=6; else modDay--;
  790.  
  791.   var daynum = ((Date.UTC(this.getFullYear(),this.getMonth(),this.getDate(),0,0,0) - Date.UTC(this.getFullYear()),0,1,0,0,0)) /1000/60/60/24) + 1;
  792.  
  793.   if (modDay < 4 ) {
  794.       var weeknum = Math.floor((daynum+modDay-1)/7)+1;
  795.   }
  796.   else {
  797.       var weeknum = Math.floor((daynum+modDay-1)/7);
  798.       if (weeknum == 0) {
  799.           year--;
  800.           var prevNewYear = new Date(this.getFullYear(),0,1);
  801.           var prevmodDay = prevNewYear.getDay();
  802.           if (prevmodDay == 0) prevmodDay = 6; else prevmodDay--;
  803.           if (prevmodDay < 4) weeknum = 53; else weeknum = 52;
  804.       }
  805.   }
  806.  
  807.   return + weeknum;
  808. }*/
  809. //-----------------------------------------------------------------------------
  810. Date.prototype.getUeDay = function () //returns the number of DAYS since the UNIX Epoch - good for comparing the date portion
  811. {
  812.   return parseInt(Math.floor((this.getTime() - this.getTimezoneOffset() * 60000)/86400000)); //must take into account the local timezone
  813. };
  814. //-----------------------------------------------------------------------------
  815. Date.prototype.dateFormat = function(format)
  816. {
  817.   if(!format) { // the default date format to use - can be customized to the current locale
  818.     format = 'd/m/Y';
  819.   }
  820.   LZ = function(x) {return(x < 0 || x > 9 ? '' : '0') + x};
  821.   var MONTH_NAMES = new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  822.   var DAY_NAMES = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  823.   format = format + "";
  824.   var result="";
  825.   var i_format=0;
  826.   var c="";
  827.   var token="";
  828.   var y=this.getFullYear().toString();
  829.   var M=this.getMonth()+1;
  830.   var d=this.getDate();
  831.   var E=this.getDay();
  832.   var H=this.getHours();
  833.   var m=this.getMinutes();
  834.   var s=this.getSeconds();
  835.   var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
  836.   // Convert real this parts into formatted versions
  837.   var value = new Object();
  838.   //if (y.length < 4) {y=''+(y-0+1900);}
  839.   value['Y'] = y.toString();
  840.   value['y'] = y.substring(2);
  841.   value['n'] = M;
  842.   value['m'] = LZ(M);
  843.   value['F'] = MONTH_NAMES[M-1];
  844.   value['M'] = MONTH_NAMES[M+11];
  845.   value['j'] = d;
  846.   value['d'] = LZ(d);
  847.   value['D'] = DAY_NAMES[E+7];
  848.   value['l'] = DAY_NAMES[E];
  849.   value['G'] = H;
  850.   value['H'] = LZ(H);
  851.   if (H==0) {value['g']=12;}
  852.   else if (H>12){value['g']=H-12;}
  853.   else {value['g']=H;}
  854.   value['h']=LZ(value['g']);
  855.   if (H > 11) {value['a']='pm'; value['A'] = 'PM';}
  856.   else { value['a']='am'; value['A'] = 'AM';}
  857.   value['i']=LZ(m);
  858.   value['s']=LZ(s);
  859.   //construct the result string
  860.   while (i_format < format.length) {
  861.     c=format.charAt(i_format);
  862.     token="";
  863.     while ((format.charAt(i_format)==c) && (i_format < format.length)) {
  864.       token += format.charAt(i_format++);
  865.       }
  866.     if (value[token] != null) { result=result + value[token]; }
  867.     else { result=result + token; }
  868.     }
  869.   return result;
  870. };
  871. /*****************************************************************************/
  872. Array.prototype.arrayIndex = function(searchVal,startIndex) //similar to array.indexOf() - created to fix IE deficiencies
  873. {
  874.   startIndex = (startIndex != null ? startIndex : 0); //default startIndex to 0, if not set
  875.   for(var i=startIndex;i<this.length;i++)
  876.   {
  877.     if(searchVal == this[i]) {
  878.       return i;
  879.     }
  880.   }
  881.   return -1;
  882. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement