function Browser() {
	Browser.isIE = function() {
		return ((document.all != null) && (document.layers == null));
	}
} new Browser();

function Delegate(scope, func) {
	if ((scope != null) && (func != null)) {
		this.invokeScope = scope;
		this.invokeFunction = func;
		this.invokeKey = Math.floor(Math.random() * 10000000);

		Delegate.registerInvoke(this);
	} else 
		Delegate.invokes = new Array();
	
	Delegate.registerInvoke = function(delegate) {
		Delegate.invokes[delegate.invokeKey] = delegate;
	}

	Delegate.unregisterInvoke = function(invokeKey) {
		Delegate.invokes[invokeKey] = null;
	}

	Delegate.privateInvoke = function(invokeKey) {
		var delegate = Delegate.invokes[invokeKey];

		if (delegate != null)
			delegate.invokeFunction.call(delegate.invokeScope);

		Delegate.unregisterInvoke(invokeKey);
	}

	Delegate.prototype.invoke = function(time) {
		time = (time > 0) ? time : 1;
		setTimeout("Delegate.privateInvoke(" + this.invokeKey + ")", time);
	}
} new Delegate();

function EventHandler(scope, source, target) {
	EventHandler.prototype.handle = function(event) {
		this.eventTarget.call(this.eventScope, event);
	}

	if ((scope != null) && (source != null) && (target != null)) {
		this.eventScope = scope;
		this.eventTarget = target;

		var handler = this;
		var handledSource = function(event) {
			source.call(this, event);
			handler.handle(event);
		}

		return handledSource;
	}
} new EventHandler();

function Component(node) {
	if (node != null) {
		/* init */

		this.eventsAllowed = true;

		/* /init */

		/* init events */
		node.onfocus = function(event) { if ((this.component.eventsAllowed) && (this.component.onfocus != null)) this.component.onfocus(event); }
		node.onblur = function(event) { if ((this.component.eventsAllowed) && (this.component.onblur != null)) this.component.onblur(event); }
		node.onmouseover = function(event) { if ((this.component.eventsAllowed) && (this.component.onmouseover != null)) this.component.onmouseover(event); }
		node.onmouseout = function(event) { if ((this.component.eventsAllowed) && (this.component.onmouseout != null)) this.component.onmouseout(event); }
		node.onmousedown = function(event) { if ((this.component.eventsAllowed) && (this.component.onmousedown != null)) this.component.onmousedown(event); }
		node.onmouseup = function(event) { if ((this.component.eventsAllowed) && (this.component.onmouseup != null)) this.component.onmouseup(event); }
		node.onkeyup = function(event) { if ((this.component.eventsAllowed) && (this.component.onkeyup != null)) this.component.onkeyup(event); }
		node.onkeydown = function(event) { if ((this.component.eventsAllowed) && (this.component.onkeydown != null)) this.component.onkeydown(event); }
		node.onclick = function(event) { if ((this.component.eventsAllowed) && (this.component.onclick != null)) this.component.onclick(event); }
		node.ondblclick = function(event) { if ((this.component.eventsAllowed) && (this.component.ondblclick != null)) this.component.ondblclick(event); }

		/* custom events */
		node.onactivate = function() { if ((this.component.eventsAllowed) && (this.component.onactivate != null)) this.component.onactivate(); }
		node.ondeactivate = function() { if ((this.component.eventsAllowed) && (this.component.ondeactivate != null)) this.component.ondeactivate(); }
		node.ondisable = function() { if ((this.component.eventsAllowed) && (this.component.ondisable != null)) this.component.ondisable(); }
		node.onenable = function() { if ((this.component.eventsAllowed) && (this.component.onenable != null)) this.component.onenable(); }

		Component.prototype.onfocus = function(event) { this.cancelEvent(event); }
		Component.prototype.onblur = function(event) { this.cancelEvent(event);	}
		Component.prototype.onmouseover = function(event) {	this.cancelEvent(event); }
		Component.prototype.onmouseout = function(event) { this.cancelEvent(event); }
		Component.prototype.onmousedown = function(event) {	this.cancelEvent(event); }
		Component.prototype.onmouseup = function(event) { this.cancelEvent(event); }
		Component.prototype.onkeyup = function(event) { this.cancelEvent(event); }
		Component.prototype.onkeydown = function(event) { this.cancelEvent(event); }
		Component.prototype.onclick = function(event) { this.cancelEvent(event); }
		Component.prototype.ondblclick = function(event) { this.cancelEvent(event); }
		Component.prototype.onactivate = function() {}
		Component.prototype.ondeactivate = function() {}
		Component.prototype.ondisable = function() { this.node.style.backgroundColor = "#cccccc"; }
		Component.prototype.onenable = function() {	this.node.style.backgroundColor = "#ffffff"; }

		Component.prototype.getEvent = function(event) {
			if (event != null)
				return event.type;
			else
				return window.event.type;
		}

		Component.prototype.cancelEvent = function(event) {
			if (event != null)
				if (event.stopPropagation != null)
					event.stopPropagation();
				else
					event.cancelBubble = true;
			else
				window.event.cancelBubble = true;
		}

		Component.prototype.raiseEvent = function(event) {
			if (event != null)
				this["on" + this.getEvent(event)](event);
		}

		Component.prototype.disableEvents = function() {
			this.eventsAllowed = false;
		}

		Component.prototype.enableEvents = function() {
			this.eventsAllowed = true;
		}

		Component.prototype.getKey = function(event) {
			if (event != null)
				return event.keyCode;
			else
				return window.event.keyCode;
		}

		Component.prototype.getKeyChar = function(event) {
			if (event != null)
				return event.charCode;
			else
				return window.event.keyCode;
		}

		Component.prototype.cancelKey = function(event) {
			if (event != null)
				event.preventDefault();
			else
				window.event.returnValue = false;
		}

		Component.prototype.isSystemKey = function(key) {
			return !((key != 37) && (key != 38) && (key != 39) && (key != 40) && (key != 46) && (key != 8) && (key != 9) && (key != 27) && (key != 13))
		}

		Component.prototype.getLeft = function(node) {
			node = (node != null) ? node : this.node;
			var left = node.offsetLeft;

			if (node.offsetParent) {
				var parentLeft = this.getLeft(node.offsetParent);
				left += parentLeft;
			}

			return left;
		}

		Component.prototype.setLeft = function(left, node) {
			node = (node != null) ? node : this.node;
			node.style.left = left + "px";
		}

		Component.prototype.getTop = function(node) {
			node = (node != null) ? node : this.node;
			var top = node.offsetTop;

			if (node.offsetParent) {
				var parentTop = this.getTop(node.offsetParent);
				top += parentTop;
			}

			return top;
		}

		Component.prototype.setTop = function(top, node) {
			node = (node != null) ? node : this.node;
			node.style.top = top + "px";
		}

		Component.prototype.getWidth = function(node) {
			node = (node != null) ? node : this.node;

			return node.offsetWidth;
		}

		Component.prototype.setWidth = function(width, node) {
			node = (node != null) ? node : this.node;
			node.style.width = width + "px";//(width - (Browser.isIE() ? 0 : 2)) + "px"; //-2 border, except ie
		}

		Component.prototype.getHeight = function(node) {
			node = (node != null) ? node : this.node;

			return node.offsetHeight;
		}

		Component.prototype.setHeight = function(height, node) {
			node = (node != null) ? node : this.node;
			node.style.height = height + "px";//(height - (Browser.isIE() ? 0 : 2)) + "px"; //-2 border, except ie
		}

		Component.prototype.getRight = function(node) {
			node = (node != null) ? node : this.node;

			return this.getLeft(node) + this.getWidth(node);
		}

		Component.prototype.getBottom = function(node) {
			node = (node != null) ? node : this.node;

			return this.getTop(node) + this.getHeight(node);
		}

		Component.prototype.getLayout = function(node) {
			node = (node != null) ? node : this.node;

			return new Array(this.getLeft(node), this.getTop(node), this.getWidth(node), this.getHeight(node));
		}

		Component.prototype.setLayout = function(left, top, width, height, node) {
			node = (node != null) ? node : this.node;
			this.setLeft(left, node);
			this.setTop(top, node);
			this.setWidth(width, node);
			this.setHeight(height, node);
		}

		Component.prototype.activate = function() {
			Component.activeComponent = this;
			
			this.onactivate();
		}

		Component.prototype.deactivate = function() {
			if (Component.activeComponent == this)
				Component.activeComponent = null;

			this.ondeactivate();
		}

		Component.prototype.disable = function() {
			this.node.disabled = true;
			this.ondisable();
		}

		Component.prototype.enable = function() {
			this.node.disabled = false;
			this.onenable();
		}

		this.node = node;
		node.component = this;
	}
}
Component_activeComponent = null;

function Panel(node) {
	node = (node != null) ? node : Page.createElement("DIV");
	Component.prototype.constructor.call(this, node);

	if (node != null) {
		/* init */
		node.style.overflow = "auto";
		node.style.border = "1px solid threeddarkshadow";
		node.style.backgroundColor = "window";
		/* /init */

		Panel.prototype.activate = function() {
			this.base.activate.call(this);

			Page.resetComponents();

			this.node.style.position = "absolute";
			this.node.style.visibility = "visible";
			this.node.style.display = "block";

			if (this.node.firstChild) {
				this.node.firstChild.style.position = "absolute";
				this.node.firstChild.style.visibility = "visible";
				this.node.firstChild.style.display = "block";
			}

			Panel_activePanel = this;
		}

		Panel.prototype.deactivate = function() {
			this.base.deactivate.call(this);

			this.node.style.position = "absolute";
			this.node.style.visibility = "hidden";
			this.node.style.display = "none";

			if (this.node.firstChild) {
				this.node.firstChild.style.position = "absolute";
				this.node.firstChild.style.visibility = "hidden";
				this.node.firstChild.style.display = "none";
			}

			if (Panel_activePanel == this)
				Panel_activePanel = null;
		}

		Panel.prototype.setWidth = function(width, node) {
			this.base.setWidth.call(this, width, node);
			if (this.node.firstChild) this.node.firstChild.style.width = "auto";
		}

		Panel.prototype.getContentNode = function() {
			return this.node.firstChild;
		}

		Panel.prototype.setContentNode = function(contentNode) {
			if (contentNode.parentNode)
				contentNode.parentNode.removeChild(contentNode);

			if (this.node.firstChild)
				this.node.replaceChild(contentNode, this.node.firstChild)
			else
				this.node.appendChild(contentNode);
		}

		this.deactivate();
	}
}
Panel.prototype = new Component();
Panel.prototype.constructor = Panel;
Panel.prototype.base = Component.prototype;
Panel_activePanel = null;

function Control(event, node) {
	Component.prototype.constructor.call(this, node);

	if (node != null) {
		Control.prototype.setValue = function(value) {
			this.node.value = value;
		}

		Control.prototype.getValue = function() {
			return this.node.value;
		}
	}
}
Control.prototype = new Component();
Control.prototype.constructor = Control;
Control.prototype.base = Component.prototype;

function DateControl(event, node, iconNodeName, backImg, nextImg, blurCB, xoffset, yoffset, icondisabled) {
	Control.prototype.constructor.call(this, event, node);

	if (node != null) {
		/* init */
		this.today = new Date();
		this.day = this.today.getDate();
		this.month = this.today.getMonth() + 1;
		this.year = this.today.getFullYear();

		this.backButton = "<img src='" + backImg + "' alt='' width='16' height='16'/>";
		this.nextButton = "<img src='" + nextImg + "' alt='' width='16' height='16'/>";

		this.blurCB = blurCB;

		this.xoffset = xoffset != null ? xoffset : 0;
		this.yoffset = yoffset != null ? yoffset : 0;

		this.icondisabled = icondisabled;
		this.panel = new Panel();

		if (iconNodeName != null) {
			this.icon = new Control(event, Page.getElement(iconNodeName));

			DateControl.prototype.icon_onmousedown = function(event) {
				this.activate();
			}

			if (icondisabled != true)
				this.icon.onmousedown = new EventHandler(this, this.icon.onmousedown, this.icon_onmousedown);
		}
		/* /init */

		DateControl.prototype.getDaysInMonth = function(month, year) {
			var monthdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
			if (month != 1) {
				return monthdays[month];
			} else {
				return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28);
			}
		}

		DateControl.prototype.getMonthName = function(month) {

			var months = document.getElementById("monthnames").value;
			var monthnames = months.split(",");

			return monthnames[month];
		}

		DateControl.prototype.activateCalendar = function(year, month, day) {
			year = (year != null) ? year : this.year;
			month = (month != null) ? month : this.month;
			day = (day != null) ? day : ((year == this.year) && (month == this.month)) ? this.day : 1;
			month--;
			
			this.calendarNode = Page.createElement("DIV", "mutti");

			var daysInMonth = this.getDaysInMonth(month, year);
			var thisMonth = new Date(year, month, 1);
			var firstOfMonth = thisMonth.getDay();

			var calendarDays = new Array(new Array());
			var rowIndex = 0;
			var dayIndex = 0;

			for(dayIndex = 0; dayIndex < firstOfMonth; dayIndex++)
				calendarDays[rowIndex][calendarDays[rowIndex].length] = '<td>&nbsp;</td>';

			var dayClass = null;
			dayIndex = 1;

			while(dayIndex <= daysInMonth) {
				//new row if 7 days done
				if (calendarDays[rowIndex].length == 7) {
					calendarDays[++rowIndex] = new Array();
				}

				dayClass = day == dayIndex ? "selcalday" : "calday";

				calendarDays[rowIndex][calendarDays[rowIndex].length] = '<td align="center" onmousedown="Page.getComponent(event, Page.getElement(\'' + this.node.id + '\')).selectDate(' + dayIndex + ', ' + (Number(month) + 1) + ', ' + year + ');" onmouseover="this.className=\'' + dayClass + 'over\'" onmouseout="this.className=\'' + dayClass + '\'" class="' + dayClass + '">' + (dayIndex++) + '</td>';
			}

			for(dayIndex=0; dayIndex<calendarDays.length; dayIndex++){
				calendarDays[dayIndex] = calendarDays[dayIndex].join('\n') + '\n';
			}

			var previousYear = thisMonth.getFullYear();
			var previousMonth = thisMonth.getMonth() - 1;
			if(previousMonth < 0) {
				previousMonth = 11;
				previousYear--;
			}
			
			var nextYear = thisMonth.getFullYear();
			var nextMonth = thisMonth.getMonth() + 1;
			if(nextMonth > 11) {
				nextMonth = 0;
				nextYear++;
			}

			var prevLinkHtml = '<div onmousedown="Page.getComponent(event, Page.getElement(\'' + this.node.id + '\')).activateCalendar(' + previousYear + ', ' + (previousMonth + 1) + ');">' + this.backButton + '</div>';
			var nextLinkHtml = '<div onmousedown="Page.getComponent(event, Page.getElement(\'' + this.node.id + '\')).activateCalendar(' + nextYear + ', ' + (nextMonth + 1) + ');">' + this.nextButton + '</div>';

			var days = document.getElementById("daynames").value;
			var daynames = days.split(",");

			var html = null;
			html = '<table border="0" cellspacing="0" cellpadding="0" class="cal" style="cursor:default;">';
			html += '<tr><td class="calheader">' + prevLinkHtml + '</td><td colspan="5" align="center" class="calheader">' + this.getMonthName(thisMonth.getMonth()) + ' ' + thisMonth.getFullYear() + '</td><td align="right" class="calheader">' + nextLinkHtml + '</td></tr>';
			html += '<tr>';
			html += '<td class="caldayname">' + daynames[6] + '</td>';
			html += '<td class="caldayname">' + daynames[0] + '</td>';
			html += '<td class="caldayname">' + daynames[1] + '</td>';
			html += '<td class="caldayname">' + daynames[2] + '</td>';
			html += '<td class="caldayname">' + daynames[3] + '</td>';
			html += '<td class="caldayname">' + daynames[4] + '</td>';
			html += '<td class="caldayname">' + daynames[5] + '</td></tr>';
			html += '<tr>' + calendarDays.join('</tr>\n<tr>') + '</tr>';
			html += '</table>';

			this.calendarNode.innerHTML = html;
			this.calendarNode.style.position = "absolute";
			this.calendarNode.style.visibility = "visible";
			this.calendarNode.style.display = "block";
			this.calendarNode.style.width = "auto";

			var deltaWidth = Browser.isIE() ? 2 : 0;
			var deltaWidthForPositionOffset = Browser.isIE() ? (icondisabled ? 2 : 3) : 0;
			var deltaHeight = Browser.isIE() ? 2 : 0;

			var calendarWidth = this.calendarNode.offsetWidth + deltaWidth;
			var calendarHeight = this.calendarNode.offsetHeight + deltaHeight;

			this.panel.setLayout(this.getLeft() + deltaWidthForPositionOffset + this.xoffset, this.getTop() + this.yoffset, calendarWidth, calendarHeight);

			this.calendarNode.style.position = "absolute";
			this.calendarNode.style.visibility = "hidden";
			this.calendarNode.style.display = "none";
			this.calendarNode.style.width = "auto";

			this.panel.setContentNode(this.calendarNode);

			this.panel.activate();
		}

		DateControl.prototype.deactivateCalendar = function() {
			this.panel.deactivate();
		}

		DateControl.prototype.activate = function() {
			this.base.activate.call(this);

			Page.resetComponents();

			this.parseDate();
			this.activateCalendar();

			DateControl_activeDateControl = this;
		}

		DateControl.prototype.deactivate = function() {
			this.base.deactivate.call(this);

			this.deactivateCalendar();

			if (DateControl_activeDateControl == this)
				DateControl_activeDateControl = null;
		}

		DateControl.prototype.getDate = function() {
			return new Date(this.day, this.month, this.year);
		}

		DateControl.prototype.setDate = function(day, month, year) {
			this.day = day;
			this.month = month;
			this.year = year;

			day = new String(day); day = (day.length < 2) ? "0" + day : day;
			month = new String(month); month = (month.length < 2) ? "0" + month : month;

			year = new String(year);

			this.setValue(day + "." + month + "." + year);
		}

		DateControl.prototype.parseDate = function() {
			var dateItems = this.getValue().split(/[\.,]/);

			var parseOk = false;

			if ((dateItems != null) && (dateItems.length == 3)) {
				var day = new Number(dateItems[0]);
				var month = new Number(dateItems[1]);
				var year = new Number(dateItems[2]);

				if ((this.isNum(day)) && (this.isNum(month)) && (this.isNum(year))) {
					this.day = day;
					this.month = month;
					if (year < 20)
						this.year = 2000 + year;
					else if (year < 99)
						this.year = 1900 + year;
					else if (year > 1900)
						this.year = year;

					parseOk = true;
				}
			} else if ((dateItems != null) && (dateItems.length == 2)) {
				var day = new Number(dateItems[0]);
				var month = new Number(dateItems[1]);
				var year = this.today.getFullYear();

				if ((this.isNum(day)) && (this.isNum(month))) {
					this.day = day;
					this.month = month;
					this.year = year;
					if (this.month - 1 < this.today.getMonth())
					{
						this.year = this.today.getFullYear() + 1;
					}
					else if ((this.month - 1 == this.today.getMonth()) && (this.day < this.today.getDate()))
					{
						this.year = this.today.getFullYear() + 1;
					}
					else 
					{
						this.year = this.today.getFullYear();
					}

					parseOk = true;
				}
			} else {
				var plainDate = this.getValue();
				if (plainDate.length == 8) {
					var day = new Number(plainDate.substring(0, 2));
					var month = new Number(plainDate.substring(2, 4));
					var year = new Number(plainDate.substring(4, 8));

					if ((this.isNum(day)) && (this.isNum(month)) && (this.isNum(year))) {
						this.day = day;
						this.month = month;
						this.year = year;

						parseOk = true;
					}
				} else if (plainDate.length == 6) {
					var day = new Number(plainDate.substring(0, 2));
					var month = new Number(plainDate.substring(2, 4));
					var year = new Number(plainDate.substring(4, 6));

					if ((this.isNum(day)) && (this.isNum(month)) && (this.isNum(year))) {
						this.day = day;
						this.month = month;

						if (year < 20)
							this.year = 2000 + year;
						else
							this.year = 1900 + year;

						parseOk = true;
					}
				} else if (plainDate.length == 4) {
					var day = new Number(plainDate.substring(0, 2));
					var month = new Number(plainDate.substring(2, 4));

					if ((this.isNum(day)) && (this.isNum(month))) {
						this.day = day;
						this.month = month;
						if (this.month - 1 < this.today.getMonth())
						{
							this.year = this.today.getFullYear() + 1;
						}
						else if ((this.month - 1 == this.today.getMonth()) && (this.day < this.today.getDate()))
						{
							this.year = this.today.getFullYear() + 1;
						}
						else 
						{
							this.year = this.today.getFullYear();
						}

						parseOk = true;
					}
				} else if (plainDate.length == 2) {
					var day = new Number(plainDate.substring(0, 2));

					if (this.isNum(day)) {
						this.day = day;
						if (this.today.getDate() > this.day)
							this.month = this.today.getMonth() + 2;
						else
							this.month = this.today.getMonth() + 1;

						if (this.month > 12)
							this.year = this.today.getFullYear() + 1;
						else
						{
							if (this.month - 1 < this.today.getMonth())
							{
								this.year = this.today.getFullYear() + 1;
							}
							else if ((this.month - 1 == this.today.getMonth()) && (this.day < this.today.getDate()))
							{
								this.year = this.today.getFullYear() + 1;
							}
							else 
							{
								this.year = this.today.getFullYear();
							}
						}
						parseOk = true;
					}
				}
			}

			return parseOk;
		}

		DateControl.prototype.isNum = function(val) {
			return (!isNaN(val) && (Math.floor(val) == new Number(val)));
		}

		DateControl.prototype.selectDate = function(day, month, year) {
			this.setDate(day, month, year);
			this.deactivate();
		}

		DateControl.prototype.onfocus = function(event) {
			this.base.onfocus.call(this, event);

			this.parseDate();
		}

		DateControl.prototype.onblur = function(event) {
			this.base.onblur.call(this, event);

			if (this.getValue().length > 0) {
				if (this.parseDate())
					this.setDate(this.day, this.month, this.year);
			}

			this.deactivate();

			if ((this.blurCB != null) && (this.getValue().length > 0))
				this.blurCB(event, this.day, this.month, this.year);
		}

		this.raiseEvent(event);
	}
}
DateControl.prototype = new Control();
DateControl.prototype.constructor = DateControl;
DateControl.prototype.base = Control.prototype;
DateControl_activeDateControl = null;
 