var EZcalendar = false; // Loaded or not? var bCalendarFade = true; // Fade in or not? Not suitable for slow machines var iCalendarFadeSpd = 25; // Lower the number, faster it is. var selectedDate; // whether the users cursor is over the calendar var target; // the target element for the date value var dateSeparator = "/"; // date separator unit var overCalendar = false; // whether the users cursor is over the calendar var shortMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); var fullMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); // simply return an object by id function getID(id) { return document.getElementById(id); } // return a number with 2 digits ("2" becomes "02") function formatNumber(n) { return (n<10) ? "0"+n : n; } function getScrollFromTop() { if (self.pageYOffset) { return self.pageYOffset; } else if (document.documentElement && document.documentElement.scrollTop) { return document.documentElement.scrollTop; } else { return document.body.scrollTop; } } // return a number > 10 as one single digit ("09" becomes "9") function removeFormatNumber(n) { return (n.substr(0,1)=="0") ? n.substr(1,1) : n; } // return the on-screen LEFT(x) position of an element function getPageOffsetLeft(el) { return (el.offsetParent != null) ? el.offsetLeft + getPageOffsetLeft(el.offsetParent) : el.offsetLeft; } // return the on-screen TOP(y) position of an element function getPageOffsetTop(el) { return (el.offsetParent != null) ? el.offsetTop + getPageOffsetTop(el.offsetParent) : el.offsetTop; } // Checks a string to see if it in a valid date format // of (D)D/(M)M/(YY)YY and returns true/false function isValidDate(s) { // format D(D)/M(M)/(YY)YY var dateFormat = /^\d{1,2}\/\d{1,2}\/\d{2,4}$/; if (dateFormat.test(s)) { // remove any leading zeros from date values s = s.replace(/0*(\d*)/gi,"$1"); var dateArray = s.split("/"); // correct month value dateArray[0] = dateArray[0]-1; // correct year value if (dateArray[2].length<4) { // correct year value dateArray[2] = (parseInt(dateArray[2]) < 50) ? 2000 + parseInt(dateArray[2]) : 1900 + parseInt(dateArray[2]); } var testDate = new Date(dateArray[2], dateArray[0], dateArray[1]); if (testDate.getDate()!=dateArray[1] || testDate.getMonth()!=dateArray[0] || testDate.getFullYear()!=dateArray[2]) { return false; } else { return true; } } else { return false; } } // get the calendar week of a date function getWeek(d) { /* thanks to http://www.quirksmode.org/js/week.html */ var today = new Date(d); Year = today.getFullYear(); Month = today.getMonth(); Day = today.getDate(); now = Date.UTC(Year,Month,Day+1,0,0,0); var Firstday = new Date(); Firstday.setYear(Year); Firstday.setMonth(0); Firstday.setDate(1); then = Date.UTC(Year,0,1,0,0,0); var Compensation = Firstday.getDay(); if (Compensation > 3) Compensation -= 4; else Compensation += 3; NumberOfWeek = Math.round((((now-then)/86400000)+Compensation)/7); return formatNumber(NumberOfWeek); } // change the calendar to the PREVIOUS month function prevMonth() { var months = getID("months"); var years = getID("years"); if (parseInt(months.value) - 1 >= 0) { months.value = parseInt(months.value) - 1; } else if (parseInt(years.value) > 1901) { months.value = 11; years.value = parseInt(years.value) - 1; } updateCalendar(); } // change the calendar to the NEXT month function nextMonth() { var months = getID("months"); var years = getID("years"); if (parseInt(months.value) + 1 < 12) { months.value = parseInt(months.value) + 1; } else if (parseInt(years.value) < 2099) { months.value = 0; years.value = parseInt(years.value) + 1; } updateCalendar(); } // change the calendar to the PREVIOUS year function prevYear() { var years = getID("years"); if (parseInt(years.value) > 1901) { years.value = parseInt(years.value) - 1; } updateCalendar(); } // change the calendar to the NEXT year function nextYear() { var years = getID("years"); if (parseInt(years.value) < 2099) { years.value = parseInt(years.value) + 1; } updateCalendar(); } // update the calendars values // this changes the tags innerHTML and href values function updateCalendar(d) { var today = new Date(); var y = getID("years"); var m = getID("months"); y = y.value; m = m.value; var selectedDate = null; if(d && (d != null || d != "" || d != 'undefined')){ selectedDate = new Date(y,m,d); } var calendarDate = new Date(y,m,1); getID("EZcalendar_text").innerHTML = shortMonths[calendarDate.getMonth()] + " " + calendarDate.getFullYear(); var defaultMonth = calendarDate.getMonth(); var difference = calendarDate.getDay()+6; calendarDate.setDate(calendarDate.getDate()-difference); for (r=0;r<6;r++) { //getID("week"+r).innerHTML = getWeek(calendarDate); for (c=0;c<7;c++) { if (calendarDate.getMonth()!=defaultMonth) { getID("cell"+r+c).className="outsideMonth"; } else { getID("cell"+r+c).className=""; } // is it today's date? if(selectedDate &&(selectedDate!= null || selectedDate!=""|| selectedDate=='undefined')){ if (calendarDate.getDate()+"/"+calendarDate.getMonth()+"/"+calendarDate.getFullYear()==selectedDate.getDate()+"/"+selectedDate.getMonth()+"/"+selectedDate.getFullYear()) { getID("cell"+r+c).className="today"; } }else { if (calendarDate.getDate()+"/"+calendarDate.getMonth()+"/"+calendarDate.getFullYear()==today.getDate()+"/"+today.getMonth()+"/"+today.getFullYear()) { getID("cell"+r+c).className="today"; } } getID("cell"+r+c).title = ""; getID("cell"+r+c).innerHTML = calendarDate.getDate(); getID("cell"+r+c).href = "javascript:setDateValue('" + formatNumber(calendarDate.getMonth()+1) + dateSeparator + formatNumber(calendarDate.getDate()) + dateSeparator + calendarDate.getFullYear() + "')"; getID("cell"+r+c).title = fullMonths[calendarDate.getMonth()] + " " + calendarDate.getDate() + ", " + calendarDate.getFullYear(); calendarDate.setDate(calendarDate.getDate()+1); } } } // when a user click the show calendar link, this function opens // the calendar and tries to show the correct calendar for the date in // the input field. function showCalendar(el) { if (EZcalendar) { if (typeof el == "string") { var el = getID(el); } target=el.id; var y = getID("years"); var m = getID("months"); var calendar = getID("EZcalendar"); var d; // test if string is valid date and if so, show calendar relative to the date they have chosen. if (isValidDate(el.value)) { var elDate = el.value.replace(/0*(\d*)/gi,"$1"); var dateArray = elDate.split(dateSeparator); // correct month value dateArray[0] = dateArray[0]-1; if (dateArray[2].length<4) { // correct year value dateArray[2] = (parseInt(dateArray[2]) < 50) ? 2000 + parseInt(dateArray[2]) : 1900 + parseInt(dateArray[2]); } m.value = dateArray[0]; y.value = dateArray[2]; d = dateArray[1]; } else { m.value = selectedDate.getMonth(); y.value = selectedDate.getFullYear(); } updateCalendar(d); var x = getPageOffsetLeft(el); var y = getPageOffsetTop(el) + el.clientHeight; calendar.style.top = (y+5)+"px"; calendar.style.left = x+"px"; if (bCalendarFade) { calendar.style.opacity = 0; calendar.style.filter = "alpha(opacity=0)"; calendar.MozOpacity = 0; calendar.KhtmlOpacity = 0; setTimeout("fadeIn(5)",iCalendarFadeSpd); } calendar.style.display = "block"; } else { alert("NOTICE:\n\nCalendar not finished loading, please wait..."); } } /* When the user clicks, this function tries to detect if they have clicked outside the calendar and if so it tries to hide it. */ function clickbg(e) { if (!overCalendar) { getID("EZcalendar").style.display="none"; } } /* When a user click the calendar date, this function updates the input field */ function setDateValue(d,el) { //getID(target).focus(); if (bCalendarFade) { setTimeout("fadeOut(95)",iCalendarFadeSpd); } else { getID("EZcalendar").style.display = "none"; } getID(target).value=d; } /* Fading in the calendar in a nice way */ function fadeIn(percentage) { var EZcalendar = getID("EZcalendar"); // apply opacity for all browsers EZcalendar.style.opacity = percentage/100; EZcalendar.style.filter = "alpha(opacity="+percentage+")"; EZcalendar.MozOpacity = (percentage / 100); EZcalendar.KhtmlOpacity = (percentage / 100); if (percentage<100) { setTimeout("fadeIn("+(percentage+5)+")",iCalendarFadeSpd); } } /* Fading in the calendar in a nice way */ function fadeOut(percentage) { var EZcalendar = getID("EZcalendar"); // apply opacity for all browsers EZcalendar.style.opacity = percentage/100; EZcalendar.style.filter = "alpha(opacity="+percentage+")"; EZcalendar.MozOpacity = (percentage / 100); EZcalendar.KhtmlOpacity = (percentage / 100); if (percentage>0) { setTimeout("fadeOut("+(percentage-5)+")",iCalendarFadeSpd); } else { EZcalendar.style.display = "none"; } } /* Loads the calendar for the first time by creating all the HTML */ function initCalendar() { // create our container DIV and add javascript to it var EZCalendarDIV = document.createElement('div'); EZCalendarDIV.id = "EZcalendar"; EZCalendarDIV.onmouseover = function() { overCalendar=true; } EZCalendarDIV.onmouseout = function() { overCalendar=false; } // hide calendar by default EZCalendarDIV.style.display = "none"; document.body.appendChild(EZCalendarDIV); var calendarHTML = ""; selectedDate = new Date(); calendarHTML += '
'; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += '
'; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; // build table using for loops... calendarHTML += ' '; // calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; calendarHTML += ' '; for (r=0;r<6;r++) { calendarHTML += ' '; //calendarHTML += ' '; for (c=0;c<7;c++) { calendarHTML += ' '; } } calendarHTML += ' '; calendarHTML += '
' + shortMonths[selectedDate.getMonth()] + " " + selectedDate.getFullYear() + '
 MTWTFSS
0000
'; // ... end calendarHTML += '
'; calendarHTML += '
'; EZCalendarDIV.innerHTML = calendarHTML; EZcalendar = true; document.onmousedown = clickbg; updateCalendar(); } /* Initialise the page by preparing the calendar when the page has loaded */ //document.onload = setTimeout("initCalendar()",500);