/* MarcGrabanski.com v2.4 */
/* Pop-Up Calendar Built from Scratch by Marc Grabanski */
/* Enhanced by Keith Wood (kbwood@iprimus.com.au). */
/* Under the Creative Commons Licence http://creativecommons.org/licenses/by/3.0/Share or Remix it but please Attribute the authors. */
var popUpCal = {selectedDay:0,selectedMonth:0,selectedYear:0,clearText:'L&ouml;schen',closeText:'Schlie&szlig;en',prevText:'&lt;Zur&uuml;ck',nextText:'Vor&gt;',currentText:'Heute',appendText:'',buttonText:'',buttonImage:'images/calendar.gif',buttonImageOnly:true,dayNames:['So','Mo','Di','Mi','Do','Fr','Sa'],monthNames:['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','October','November','Dezember'],dateFormat:'DMY.',yearRange:'-0:+2',changeMonth:false,changeYear:false,firstDay:1,changeFirstDay:false,showOtherMonths:false,minDate:null,maxDate:null,speed:'medium',autoPopUp:'both',closeAtTop:false,hideIfNoPrevNext:true,customDate:null,fieldSettings:null,inline:false,customEvent:false,init:function(){this.popUpShowing = false;this.selectingMonthYear = false;this.standalone = false;this.lastInput = null;this.disabledInputs = [];this.calendarDiv = $('<div id="calendar_div"></div>');$('body').append(this.calendarDiv);$(document).mousedown(popUpCal.checkExternalClick);},showStandalone: function(dateText, prompt, callback, pos){if(!this.standaloneInput){this.standaloneInput = $('<input type="text" style="position:absolute;top:-100px;"/>');this.standaloneInput.keydown(this.doKeyDown);$('body').append(this.standaloneInput);}this.standaloneInput.val(dateText);var windowWidth, windowHeight;if(self.innerHeight){windowWidth = self.innerWidth;windowHeight = self.innerHeight;} else if(document.documentElement && document.documentElement.clientHeight){windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;}else if(document.body) {windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;}this.pos = pos || ($.blockUI ? [0, 0] : [(windowWidth - 200) / 2,(windowHeight - 200) / 2]);this.prompt = prompt;this.customEvent = callback;this.standalone = true;this.calendarDiv.addClass('calendar_dialog');this.showFor(this.standaloneInput[0]);if($.blockUI) $.blockUI(this.calendarDiv,{ width: '200px'});},showFor: function(target){var input = (target.nodeName && target.nodeName.toLowerCase() == 'input' ? target : this);if(input.nodeName.toLowerCase() != 'input'){input = $('../input', input)[0];}if(popUpCal.lastInput == input){return;}for(var i = 0; i < popUpCal.disabledInputs.length; i++){if (popUpCal.disabledInputs[i] == input){return;}}popUpCal.input = $(input);popUpCal.hideCalendar('');popUpCal.lastInput = input;popUpCal.setDateFromField();if(!popUpCal.pos){popUpCal.pos = popUpCal.findPos(input);popUpCal.pos[1] += input.offsetHeight;}popUpCal.calendarDiv.css('position',(popUpCal.standalone && $.blockUI ? 'static' : 'absolute')).css('left', popUpCal.pos[0] + 'px').css('top', popUpCal.pos[1] + 'px');popUpCal.pos = null;$.extend(popUpCal,(popUpCal.fieldSettings ? popUpCal.fieldSettings(input) : {}));popUpCal.showCalendar();},doKeyDown: function(e) {if (popUpCal.popUpShowing) {switch (e.keyCode){case 9:popUpCal.hideCalendar('');break;case 13:popUpCal.selectDate();break;case 27:popUpCal.hideCalendar(popUpCal.speed); break;case 33:popUpCal.adjustDate(-1, (e.ctrlKey ? 'Y' : 'M'));break;case 34:popUpCal.adjustDate(+1, (e.ctrlKey ? 'Y' : 'M'));break;case 35:if (e.ctrlKey) popUpCal.clearDate();break;case 36:if (e.ctrlKey) popUpCal.gotoToday();break;case 37:if (e.ctrlKey) popUpCal.adjustDate(-1, 'D');break;case 38:if (e.ctrlKey) popUpCal.adjustDate(-7, 'D');break;case 39:if(e.ctrlKey) popUpCal.adjustDate(+1, 'D');break;case 40:if (e.ctrlKey) popUpCal.adjustDate(+7, 'D');break;}}else if(e.keyCode == 36 && e.ctrlKey){popUpCal.showFor(this);}},doKeyPress: function(e){var chr = String.fromCharCode(e.charCode == undefined ? e.keyCode : e.charCode);return (chr < ' ' || chr == popUpCal.dateFormat.charAt(3) || (chr >= '0' && chr <= '9'));},connectCalendar:function(target){var $input = $(target);$input.after('<span class="calendar_append">' + this.appendText + '</span>');if(this.autoPopUp == 'focus' || this.autoPopUp == 'both'){$input.focus(this.showFor);}if(this.autoPopUp == 'button' || this.autoPopUp == 'both'){$input.wrap('<span class="calendar_wrap"></span>').after(this.buttonImageOnly ? '<img class="calendar_trigger" src="' +this.buttonImage + '" alt="' + this.buttonText + '" title="' + this.buttonText + '"/>' :'<button type="button" class="calendar_trigger">' + (this.buttonImage != '' ?'<img src="' + this.buttonImage + '" alt="' + this.buttonText + '" title="' + this.buttonText + '"/>' :this.buttonText) + '</button>');$((this.buttonImageOnly ? 'img' : 'button') + '.calendar_trigger',$input.parent('span')).click(this.showFor);}$input.keydown(this.doKeyDown).keypress(this.doKeyPress);},displayInline: function(target){$(target).append('<div id="calendar_inline_div"></div><div style="clear:both"></div>');popUpCal.selectedDay = new Date().getDate();popUpCal.selectedMonth = new Date().getMonth();popUpCal.selectedYear = new Date().getFullYear();popUpCal.adjustDate();popUpCal.showCalendar();},enableFor: function(inputs){inputs = (inputs.jquery ? inputs : $(inputs));inputs.each(function(){this.disabled = false;$('../button.calendar_trigger', this).each(function(){ this.disabled = false; });$('../img.calendar_trigger',this).css('opacity','1.0');var $this = this;popUpCal.disabledInputs = $.map(popUpCal.disabledInputs,function(value){ return (value == $this ? null : value); });});return false;},disableFor: function(inputs){inputs = (inputs.jquery ? inputs : $(inputs));inputs.each(function(){this.disabled = true;$('../button.calendar_trigger', this).each(function(){ this.disabled = true; });$('../img.calendar_trigger', this).css('opacity', '0.5');var $this = this;popUpCal.disabledInputs = $.map(popUpCal.disabledInputs,function(value){ return (value == $this ? null : value); });popUpCal.disabledInputs[popUpCal.disabledInputs.length] = this;});return false;},showCalendar: function(){this.popUpShowing = true;var today = new Date();today = new Date(today.getFullYear(), today.getMonth(), today.getDate());var controls = '<div id="calendar_control">' +'<a id="calendar_clear" onclick="popUpCal.clearDate();">' + this.clearText + '</a>' +'<a id="calendar_close" onclick="popUpCal.hideCalendar(popUpCal.speed);">' +this.closeText + '</a></div>';var html = (this.prompt ? '<div id="calendar_prompt">' + this.prompt + '</div>' : '') +(this.closeAtTop && !this.inline ? controls : '') + '<div id="calendar_links">' +(this.canAdjustMonth(-1) ? '<a id="calendar_prev" ' +'onclick="popUpCal.adjustDate(-1, \'M\');">' + this.prevText + '</a>' :(this.hideIfNoPrevNext ? '' : '<label id="calendar_prev">' + this.prevText + '</label>')) +(this.isInRange(today) ? '<a id="calendar_current" ' +'onclick="popUpCal.gotoToday();">' + this.currentText + '</a>' : '') +(this.canAdjustMonth(+1) ? '<a id="calendar_next" ' +'onclick="popUpCal.adjustDate(+1, \'M\');">' + this.nextText + '</a>' :(this.hideIfNoPrevNext ? '' : '<label id="calendar_next">' + this.nextText + '</label>')) +'</div><div id="calendar_header">';if(!this.changeMonth) {html += this.monthNames[this.selectedMonth] + '&nbsp;';}else {var inMinYear = (this.minDate && this.minDate.getFullYear() == this.selectedYear);var inMaxYear = (this.maxDate && this.maxDate.getFullYear() == this.selectedYear);html += '<select id="calendar_newMonth" onchange="popUpCal.selectMonthYear(this, \'M\');" ' +'onclick="popUpCal.clickMonthYear();">';for (var month = 0; month < 12; month++){if ((!inMinYear || month >= this.minDate.getMonth())&&(!inMaxYear || month <= this.maxDate.getMonth())){html += '<option value="' + month + '"' +(month == this.selectedMonth ? ' selected="selected"' : '') +'>' + this.monthNames[month] + '</option>';}}html += '</select>';}if(!this.changeYear){html += this.selectedYear;}else{var years = this.yearRange.split(':');var year = 0;var endYear = 0;if (years.length != 2){year = this.selectedYear - 10;endYear = this.selectedYear + 10;}else if(years[0].charAt(0) == '+' || years[0].charAt(0) == '-'){year = this.selectedYear + parseInt(years[0]);endYear = this.selectedYear + parseInt(years[1]);}else{year = parseInt(years[0]);endYear = parseInt(years[1]);}year = (this.minDate ? Math.max(year, this.minDate.getFullYear()) : year);endYear = (this.maxDate ? Math.min(endYear, this.maxDate.getFullYear()) : endYear);html += '<select id="calendar_newYear" onchange="popUpCal.selectMonthYear(this, \'Y\');" ' +'onclick="popUpCal.clickMonthYear();">';for(; year <= endYear; year++) {html += '<option value="' + year + '"' +(year == this.selectedYear ? ' selected="selected"' : '') +'>' + year + '</option>';}html += '</select>';}html += '</div><table id="calendar" cellpadding="0" cellspacing="0"><thead>' +'<tr class="calendar_titleRow">';for(var dow = 0; dow < 7; dow++) {html += '<td>' + (this.changeFirstDay ? '<a onclick="popUpCal.changeWeekDay(this);">' : '') +this.dayNames[(dow + this.firstDay) % 7] + (this.changeFirstDay ? '</a>' : '') + '</td>';}html += '</tr></thead><tbody>';var daysInMonth = this.getDaysInMonth(this.selectedYear, this.selectedMonth);this.selectedDay = Math.min(this.selectedDay, daysInMonth);var leadDays = (this.getFirstDayOfMonth(this.selectedYear, this.selectedMonth) - this.firstDay + 7) % 7;var currentDate = new Date(this.currentYear, this.currentMonth, this.currentDay);var selectedDate = new Date(this.selectedYear, this.selectedMonth, this.selectedDay);var printDate = new Date(this.selectedYear, this.selectedMonth, 1 - leadDays);var numRows = Math.ceil((leadDays + daysInMonth) / 7);for(var row = 0; row < numRows; row++) {html += '<tr class="calendar_daysRow">';for(var dow = 0; dow < 7; dow++) {var customSettings = (this.customDate ? this.customDate(printDate) : [true, '']);var otherMonth = (printDate.getMonth() != this.selectedMonth);var unselectable = otherMonth || !customSettings[0] ||(this.minDate && printDate < this.minDate) ||(this.maxDate && printDate > this.maxDate);html += '<td class="calendar_daysCell' +((dow + this.firstDay + 6) % 7 >= 5 ? ' calendar_weekEndCell' : '') + (otherMonth ? ' calendar_otherMonth' : '') + (printDate.getTime() == selectedDate.getTime() ? ' calendar_daysCellOver' : '') + (unselectable ? ' calendar_unselectable' : '') +  (!otherMonth || this.showOtherMonths ? ' ' + customSettings[1] : '') + '"' + (printDate.getTime() == currentDate.getTime() ? ' id="calendar_currentDay"' : (printDate.getTime() == today.getTime() ? ' id="calendar_today"' : '')) + (unselectable ? '' : ' onmouseover="$(this).addClass(\'calendar_daysCellOver\');"' +' onmouseout="$(this).removeClass(\'calendar_daysCellOver\');"' +' onclick="popUpCal.selectDay(this);")') + '>' + (otherMonth ? (this.showOtherMonths ? printDate.getDate() : '&nbsp;') : (unselectable ? printDate.getDate() : '<a>' + printDate.getDate() + '</a>')) + '</td>'; printDate.setDate(printDate.getDate() + 1);}html += '</tr>';}html += '</tbody></table>' + (this.closeAtTop || this.inline ? '' : controls) +'<!--[if lte IE 6.5]><iframe src="javascript:false;" id="calendar_cover"></iframe><![endif]-->';if(!this.inline){this.calendarDiv.empty().append(html).show(this.speed, this.coverSelects);if(this.speed == '')this.coverSelects();this.input[0].focus();}else {$('#calendar_inline_div').empty().append(html);}},coverSelects: function(){if ($.browser.msie){$('#calendar_cover').css({width: popUpCal.calendarDiv[0].offsetWidth + 4,height: popUpCal.calendarDiv[0].offsetHeight + 4});}},hideCalendar: function(speed) {if (this.popUpShowing){this.calendarDiv.hide(speed, this.tidyStandalone);if(speed == '')this.tidyStandalone();this.popUpShowing = false;this.lastInput = null;this.prompt = null;if (this.standalone && $.blockUI) $.unblockUI();this.standalone = false;}},tidyStandalone: function(){popUpCal.calendarDiv.removeClass('calendar_dialog');$('calendar_prompt').remove();},gotoToday: function(){var date = new Date();popUpCal.selectedDay = date.getDate();popUpCal.selectedMonth = date.getMonth();popUpCal.selectedYear = date.getFullYear();popUpCal.adjustDate();},selectMonthYear:function(select, period){popUpCal.selectingMonthYear = false;popUpCal[period == 'M' ? 'selectedMonth' : 'selectedYear'] =select.options[select.selectedIndex].value - 0;popUpCal.adjustDate();},clickMonthYear: function(){if (popUpCal.selectingMonthYear && !$.browser.msie) {popUpCal.input[0].focus();}popUpCal.selectingMonthYear = !popUpCal.selectingMonthYear;},changeWeekDay:function(a){for (var i = 0; i < 7; i++){if (popUpCal.dayNames[i] == a.firstChild.nodeValue){popUpCal.firstDay = i;break;}}popUpCal.showCalendar();},selectDay: function(td){popUpCal.selectedDay = $("a", td).html();popUpCal.selectDate();},selectDate:function(value){this.currentDay = this.selectedDay;this.currentMonth = this.selectedMonth;this.currentYear = this.selectedYear;this.date = (value != null ? value : this.formatDate(this.selectedDay,this.selectedMonth,this.selectedYear));if(this.customEvent){this.customEvent();} else {this.input.val(this.date);this.input.trigger('change');}if(!this.inline){this.hideCalendar(this.speed);}},clearDate: function(){this.selectDate('');},checkExternalClick: function(event){if(popUpCal.popUpShowing && !(popUpCal.standalone && $.blockUI)) {var node = event.target;var cal = popUpCal.calendarDiv[0];while(node && node != cal && node.className != 'calendar_trigger'){node = node.parentNode;}if(!node){popUpCal.hideCalendar('');}}},noWeekends: function(date){var day = date.getDay();return [(day > 0 && day < 6), ''];},formatDate:function(day, month, year){month++;var dateString = '';for(var i = 0; i < 3; i++){dateString += this.dateFormat.charAt(3)+(this.dateFormat.charAt(i) == 'D' ? (day < 10 ? '0' : '') + day :(this.dateFormat.charAt(i) == 'M' ? (month < 10 ? '0' : '') + month :(this.dateFormat.charAt(i) == 'Y' ? year : '?')));}return dateString.substring(this.dateFormat.charAt(3) ? 1 : 0);},setDateFromField: function(){var currentDate = this.input.val().split(this.dateFormat.charAt(3));if(currentDate.length == 3){this.currentDay = parseInt(this.trimNumber(currentDate[this.dateFormat.indexOf('D')]));this.currentMonth = parseInt(this.trimNumber(currentDate[this.dateFormat.indexOf('M')])) - 1;this.currentYear = parseInt(this.trimNumber(currentDate[this.dateFormat.indexOf('Y')]));}else{this.currentDay = new Date().getDate();this.currentMonth = new Date().getMonth();this.currentYear = new Date().getFullYear();}this.selectedDay = this.currentDay;this.selectedMonth = this.currentMonth;this.selectedYear = this.currentYear;this.adjustDate(0, 'D', true);},trimNumber:function(value){if (value == '')return '';while (value.charAt(0) == '0'){value = value.substring(1);}return value;},adjustDate:function(offset, period, dontShow) {var date = new Date(this.selectedYear + (period == 'Y' ? offset : 0),this.selectedMonth + (period == 'M' ? offset : 0),this.selectedDay + (period == 'D' ? offset : 0));date = (this.minDate && date < this.minDate ? this.minDate : date);date = (this.maxDate && date > this.maxDate ? this.maxDate : date);this.selectedDay = date.getDate();this.selectedMonth = date.getMonth();this.selectedYear = date.getFullYear();if(!dontShow){this.showCalendar();}},getDaysInMonth: function(year, month) {return 32 - new Date(year, month, 32).getDate();},getFirstDayOfMonth: function(year, month){return new Date(year, month, 1).getDay();},canAdjustMonth:function(offset){var date = new Date(this.selectedYear, this.selectedMonth + offset, 1);if(offset < 0) {date.setDate(this.getDaysInMonth(date.getFullYear(),date.getMonth()));}return this.isInRange(date);},isInRange:function(date){return ((!this.minDate || date >= this.minDate)&&(!this.maxDate || date <= this.maxDate));},findPos: function(obj){var curleft = curtop = 0;if (obj.offsetParent){curleft = obj.offsetLeft;curtop = obj.offsetTop;while(obj = obj.offsetParent){var origcurleft = curleft;curleft += obj.offsetLeft;if(curleft < 0){curleft = origcurleft;}curtop += obj.offsetTop;}}return [curleft,curtop];}};$.fn.calendar = function(settings){$.extend(popUpCal,settings || {});return this.each(function(){if(this.nodeName.toLowerCase() == 'input'){popUpCal.connectCalendar(this);}else if(this.nodeName.toLowerCase() == 'div'){popUpCal.displayInline(this);}});};$(document).ready(function(){popUpCal.init();});