[MRBS-general] Putting the days of the week into date selectors with javascript
Brought to you by:
jberanek
From: michael w. <mic...@bt...> - 2009-01-21 16:09:52
|
diff moodle-live/blocks/mrbs/web/edit_entry.php moodle/blocks/mrbs/web/edit_entry.php 338a339 > <SCRIPT LANGUAGE="JavaScript">ChangeOptionDays(document.main, '');</SCRIPT> diff moodle-live/blocks/mrbs/web/functions.php moodle/blocks/mrbs/web/functions.php 77,156c77,106 < <!-- Begin < < /* Script inspired by "True Date Selector" < Created by: Lee Hinder, lee...@nt... < < Tested with Windows IE 6.0 < Tested with Linux Opera 7.21, Mozilla 1.3, Konqueror 3.1.0 < < */ < < function daysInFebruary (year){ < // February has 28 days unless the year is divisible by four, < // and if it is the turn of the century then the century year < // must also be divisible by 400 when it has 29 days < return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 ); < } < < //function for returning how many days there are in a month including leap years < function DaysInMonth(WhichMonth, WhichYear) < { < var DaysInMonth = 31; < if (WhichMonth == "4" || WhichMonth == "6" || WhichMonth == "9" || WhichMonth == "11") < DaysInMonth = 30; < if (WhichMonth == "2") < DaysInMonth = daysInFebruary( WhichYear ); < return DaysInMonth; < } < < //function to change the available days in a months < function ChangeOptionDays(formObj, prefix, updatefreerooms) < { < var DaysObject = eval("formObj." + prefix + "day"); < var MonthObject = eval("formObj." + prefix + "month"); < var YearObject = eval("formObj." + prefix + "year"); < < if (DaysObject.selectedIndex && DaysObject.options) < { // The DOM2 standard way < // alert("The DOM2 standard way"); < var DaySelIdx = DaysObject.selectedIndex; < var Month = parseInt(MonthObject.options[MonthObject.selectedIndex].value); < var Year = parseInt(YearObject.options[YearObject.selectedIndex].value); < } < else if (DaysObject.selectedIndex && DaysObject[DaysObject.selectedIndex]) < { // The legacy MRBS way < // alert("The legacy MRBS way"); < var DaySelIdx = DaysObject.selectedIndex; < var Month = parseInt(MonthObject[MonthObject.selectedIndex].value); < var Year = parseInt(YearObject[YearObject.selectedIndex].value); < } < else if (DaysObject.value) < { // Opera 6 stores the selectedIndex in property 'value'. < // alert("The Opera 6 way"); < var DaySelIdx = parseInt(DaysObject.value); < var Month = parseInt(MonthObject.options[MonthObject.value].value); < var Year = parseInt(YearObject.options[YearObject.value].value); < } < < // alert("Day="+(DaySelIdx+1)+" Month="+Month+" Year="+Year); < < var DaysForThisSelection = DaysInMonth(Month, Year); < var CurrentDaysInSelection = DaysObject.length; < if (CurrentDaysInSelection > DaysForThisSelection) < { < for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++) < { < DaysObject.options[DaysObject.options.length - 1] = null < } < } < if (DaysForThisSelection > CurrentDaysInSelection) < { < for (i=0; i<DaysForThisSelection; i++) < { < DaysObject.options[i] = new Option(eval(i + 1)); < } < } < if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex = 0; < if (DaySelIdx >= DaysForThisSelection) < DaysObject.selectedIndex = DaysForThisSelection-1; < else < DaysObject.selectedIndex = DaySelIdx; --- > function ChangeOptionDays(formObj, prefix, updatefreerooms){ > > var week=new Array(7); > week[1]="Mon"; > week[2]="Tue"; > week[3]="Wed"; > week[4]="Thu"; > week[5]="Fri"; > week[6]="Sat"; > week[0]="Sun"; > > > var DaysObject = eval("formObj." + prefix + "day"); > var currentDay = DaysObject.selectedIndex; > var MonthObject = eval("formObj." + prefix + "month"); > var YearObject = eval("formObj." + prefix + "year"); > > //wipe current list > for (j = DaysObject.options.length; j >= 0; j--) { > DaysObject.options[j] = null; > } > var day=DaysObject.selectedIndex+1; > var month=MonthObject.selectedIndex; > var year=YearObject.options[YearObject.selectedIndex].value; > > > var i=new Date(); > i.setDate(1); > i.setMonth(month); > i.setYear(year); 158,160c108,117 < if(updatefreerooms){ < updateFreeRooms(); < } --- > while (i.getMonth()==month){ > > DaysObject.options[i.getDate()-1] = new Option(week[i.getDay()]+" "+i.getDate(),i.getDate()); > i.setTime(i.getTime() + 86400000); > } > DaysObject.selectedIndex = currentDay; > > if(updatefreerooms){ > updateFreeRooms(); > } 163c120 < // End --> --- > |