			/****************************************************************
			 * Calendar script created by Kreshnik Gunga, Smilehouse Oy	*
			 * Modified for Lippupiste by Juha Valvanne, Smilehouse Oy	*
			 * This script is dedicated only for Smilehouse Oy's projects	*
			 ****************************************************************/
			var calendar = new Date();//fromCalendar
			var toCalendar = new Date();
			var stepDays = 7;
			var fromDay = calendar.getDate();
			var toDay = "";//fromDay+stepDays;
			var daysInM = daysInMonth(toCalendar.getMonth(),toCalendar.getFullYear());

			//select the last day in month if toDay is greater than last day of month
			/*if(toDay > daysInM){
				toDay = daysInM;
			}*/
			
			/*****************************************************************
			 * the following functions are used by fromCalendar
			 ****************************************************************/			
			//returns fromCalendar's of month
			function getCurrentDayOfMonth(){
				return calendar.getDate();
			}
			//returns fromCalendar's day of week
			function getCurrentDayOfWeek(){
				return calendar.getDay();
			}
			
			//returns fromCalendar's month
			function getCurrentMonth(){
				return calendar.getMonth()+1;
			}
			//returns fromCalendar's month name
			function getCurrentMonthName(){
				return getMonthName(calendar.getMonth());
			}
			//returns fromCalendar'snext month
			function getNextMonth(){
				var nextMonth = calendar.getMonth()+1;
				return (nextMonth%12)+1;
			}
			//returns fromCalendar's next month name
			function getNextMonthName(){
				var nextMonth = calendar.getMonth()+1;
				return getMonthName(nextMonth%12);
			}			
			
			//returns fromCalendar's year
			function getCurrentYear(){
				return calendar.getFullYear();
			}
			//returns fromCalendar's next year
			function getNextYear(){
				return calendar.getFullYear()+1;
			}
			/********************************************************
			 * the following functions are used by both calendars
			 *******************************************************/
			//return the day of week
			function getDayOfWeek(year, month, dayOfMonth){
				return new Date(year,month,dayOfMonth).getDay();
			}
			
			//return the day number of month
			function daysInMonth(iMonth, iYear){
				return (32 - new Date(iYear, iMonth, 32).getDate());
			}
		//month names in english
			function getMonthName(month){
				var months = new Array(12);
				months[0] = "January";
				months[1] = "February";
				months[2] = "March";
				months[3] = "April";
				months[4] = "May";
				months[5] = "June";
				months[6] = "July";
				months[7] = "August";
				months[8] = "September";
				months[9] = "October";
				months[10] = "November";
				months[11] = "December";
				return months[month];
			}	
			//return day name
			function getDayName(day){
				var days = new Array(7);
				days[1] = "Monday";
				days[2] = "Tuesday";
				days[3] = "Wednesday";
				days[4] = "Thursday";
				days[5] = "Friday";
				days[6] = "Saturday";
				days[0] = "Sunday";
				return days[day];
			}
			
			function printFromCalendar(){
				var tableHeader = "fromCalendarYearHeader";
				var dateInfo = "fromCalendarDateInfo";
				var days = daysInMonth(getCurrentMonth()-1,getCurrentYear());
				document.getElementById(tableHeader).innerHTML=getCurrentMonthName()+" "+getCurrentYear();
				document.getElementById(dateInfo).innerHTML=getDayName(getCurrentDayOfWeek())+" "+getCurrentDayOfMonth()+"."+getCurrentMonth()+"."+getCurrentYear();
				var currentDate = new Date();
				var daysToHide = -1;
				if(getCurrentMonth()-1==currentDate.getMonth() && getCurrentYear()==currentDate.getFullYear()){
					daysToHide = currentDate.getDate();
				}
                                if(navigator.appName != "Microsoft Internet Explorer"){
				        makeCalendar('fromCalendar', getCurrentYear(),getCurrentMonth()-1, days, fromDay,daysToHide,"fromDay");
                                }
                                else{
                                        makeCalendarForIE('fromCalendar', getCurrentYear(),getCurrentMonth()-1, days, fromDay,daysToHide,"fromDay");
                                }
			}

			function fromCalendarNextMonth(){
				deleteCalendarRows("fromCalendar");			
				deleteCalendarRows("toCalendar");
				var previousButtonOfToCalendar = document.getElementById('toCalendar').rows[0].cells[0].childNodes[0];
				if(getNextMonth()-1==toCalendar.getMonth()&&getNextYear()==toCalendar.getFullYear()){
				        previousButtonOfToCalendar.style.visibility = "hidden";
				}
				if(getCurrentMonth()-1==toCalendar.getMonth() && getCurrentYear()==toCalendar.getFullYear()){
					previousButtonOfToCalendar.style.visibility = "hidden";
					toDay = 1;
                                        var nMonth = (toCalendar.getMonth()+1)%12;
                                        var y = toCalendar.getFullYear();
				        if(nMonth == 0 ){
					         y++;
                                        }
                                        toCalendar.setDate(toDay);
                                        toCalendar.setMonth(nMonth);
                                        toCalendar.setFullYear(y);
				}
				var nextMonth = getNextMonth()-1;
				var year = getCurrentYear();
				if(nextMonth == 0){
					year = getNextYear();
				}
				fromDay = 1;
				calendar.setFullYear(year);
				calendar.setMonth(nextMonth);
				calendar.setDate(fromDay);
				var previousButton = document.getElementById('fromCalendar').rows[0].cells[0].childNodes[0];

				if(previousButton.style.visibility == 'hidden'){
					previousButton.style.visibility = "visible";
				}
                                printFromCalendar();
                                printToCalendar();
			}

			function fromCalendarPreviousMonth(){
				deleteCalendarRows("fromCalendar");
				deleteCalendarRows("toCalendar");
				var previousMonth = getCurrentMonth()-2;
				var year = getCurrentYear();
				fromDay = 1;
				if(previousMonth < 0){
					previousMonth = 11;
					year--;
				}
                                var previousButtonOfToCalendar = document.getElementById('toCalendar').rows[0].cells[0].childNodes[0];

				if(previousButtonOfToCalendar.style.visibility == 'hidden'){
                                        previousButtonOfToCalendar.style.visibility = "visible";
				}
                                var currentDate = new Date();
                                var previousButton = document.getElementById('fromCalendar').rows[0].cells[0].childNodes[0];
				if(currentDate.getFullYear() == year && previousMonth == currentDate.getMonth()){
					fromDay = currentDate.getDate();					
					previousButton.style.visibility = "hidden";
				}                                
				calendar.setFullYear(year);
				calendar.setMonth(previousMonth);
				calendar.setDate(fromDay);
				printFromCalendar();
                                printToCalendar();                                
			}

			function deleteCalendarRows(calendarId){
				var calendar = document.getElementById(calendarId);
				var rows = calendar.getElementsByTagName("tr")
				for(var i = rows.length-1; i > 2; i--){
					rows[i].parentNode.removeChild(rows[i]);
				}
			}			

			function printToCalendar(){
				var tableHeader = "toCalendarYearHeader";
				var dateInfo = "toCalendarDateInfo";
				var currentDay = 0;
				var selectedDay = toDay;

				var days = daysInMonth(toCalendar.getMonth(),toCalendar.getFullYear());
				var month = toCalendar.getMonth();
				var year = toCalendar.getFullYear();
				if(month == getCurrentMonth()-1 && year == getCurrentYear()){
					currentDay = fromDay;
					if(toDay < fromDay && toDay !=''){//when going back with toCalendar
						//select as a default the selected day in fromCalendar
						toDay = fromDay;
						selectedDay = fromDay;
					}
				}//pressing fromCalendar navigation buttons
				else if((month < getCurrentMonth()-1 && year == getCurrentYear()) || year < getCurrentYear()){
					currentDay = getCurrentDayOfMonth();
					month = getCurrentMonth()-1;
					year = getCurrentYear();
					toCalendar.setFullYear(year);
					toCalendar.setMonth(month);
					toCalendar.setDate(toDay);
				}
				document.getElementById(tableHeader).innerHTML=getMonthName(month)+" "+year;
				var dateInfoText = getDayName(getDayOfWeek(year,month,selectedDay))+" "+(selectedDay)+"."+(toCalendar.getMonth()+1)+"."+year;
				document.getElementById(dateInfo).innerHTML=dateInfoText;
                                if(navigator.appName != "Microsoft Internet Explorer"){
				        makeCalendar('toCalendar', year,month, days, selectedDay, currentDay, "toDay");
                                }
                                else{
                                        makeCalendarForIE('toCalendar', year,month, days, selectedDay, currentDay, "toDay");
                                }
                                document.forms["CalendarSearch"].elements["advancedsearch.mindate"].value = getFromDate();
                                document.forms["CalendarSearch"].elements["advancedsearch.maxdate"].value = getToDate();
			}

			function toCalendarNextMonth(){
				deleteCalendarRows("toCalendar");
				var nextMonth = (toCalendar.getMonth()+1)%12;
				var year = toCalendar.getFullYear();
				if(nextMonth == 0 ){
					year++;
				}
				var previousButton = document.getElementById('toCalendar').rows[0].cells[0].childNodes[0];

				if(previousButton.style.visibility == 'hidden'){
					previousButton.style.visibility = "visible";
				}
				toDay = 1;
				toCalendar.setFullYear(year);
				toCalendar.setMonth(nextMonth);
				toCalendar.setDate(toDay);
				printToCalendar();
			}

			function toCalendarPreviousMonth(){
				var previousMonth = toCalendar.getMonth()-1;
				var year = toCalendar.getFullYear();
				if((year == getCurrentYear() && previousMonth >= getCurrentMonth()-1) || (year > getCurrentYear())){
					deleteCalendarRows("toCalendar");
					if(previousMonth < 0){
						previousMonth = 11;
						year--;
					}
					toDay = 1;
                                        var previousButton = document.getElementById('toCalendar').rows[0].cells[0].childNodes[0];					
					var currentDate = new Date();
					if(currentDate.getFullYear() == year && previousMonth == currentDate.getMonth()){
						toDay = currentDate.getDate();
                                                previousButton.style.visibility="hidden";
					}
					if(getCurrentMonth()-1==previousMonth&&getCurrentYear()==year){
                                                previousButton.style.visibility="hidden";
                                        }
					toCalendar.setFullYear(year);
					toCalendar.setMonth(previousMonth);
					toCalendar.setDate(toDay);
					printToCalendar();
				}
			}

			function setSelectedDay(selected){
				//day in toCalendar clicked
				if(selected.parentNode.getAttribute('name') == 'toDay'){
					if (document.forms["CalendarSearch"].elements["advancedsearch.groupcode"].value = "tapahtumat")
					{
						document.forms["CalendarSearch"].elements["advancedsearch.groupcode"].value = "tilaisuudet";
					}
					toDay = parseInt(selected.innerHTML);
					toCalendar.setDate(toDay);
					deleteCalendarRows("toCalendar");
					printToCalendar();
				}
				else {//in in fromCalendar clicked
					fromDay = selected.innerHTML;
					if(getCurrentYear()==toCalendar.getFullYear() && getCurrentMonth()-1==toCalendar.getMonth()){
						if(fromDay > toDay){
							toDay = fromDay;
							toCalendar.setDate(toDay);
						}
						//update hidden days in toCalendar
						deleteCalendarRows("toCalendar");
						printToCalendar();
					}
                                        calendar.setDate(fromDay);
					deleteCalendarRows("fromCalendar");
					printFromCalendar();
				}
			}

			/*
			 * Function makeCalendar makes the calendar and prints it to table tag.
			 * All child tags of table tag are created and filled with day numbers.
			 * Every day number is a linki and can be selected to set dates. Some day numbers
			 * are hidden and can not be clicked.
			 */
			function makeCalendar(tableId, year, month, days, selectedDay, daysToHide, dayName){

				var firstDayOfMonthInWeek = new Date(year,month,1).getDay();
				var emptyDays = firstDayOfMonthInWeek-1;
				//also there is a need to calculate how many empty td-tags will be printed
				//to show the day numbers in correct places in calendar
				if(emptyDays < 0){//if sunday is first day of month
					emptyDays = 6;//print 6 empty td-tags
				}
				var calendar = document.getElementById(tableId);//get the table
				
				var dayOfMonth = 1;//day numbers
				var totalDays = days + emptyDays;//this is for a loop
				var tr = document.createElement("tr");//create first tr-tag of table
				calendar.appendChild(tr);
				for(var d = 0; d < totalDays; d++){//print days
					//first print empty td-tags
					if(d < emptyDays){//any td-tag to be printed?					
						var td = document.createElement("td");
						td.innerHTML = " ";
			                        td.className = "emptyDate";
						tr.appendChild(td);
						if(d == emptyDays-1){//if this td is the last one, append tr-tag to table
							calendar.appendChild(tr);
						}
					}
					//when selecting some day in from-calendar, there is a need to
					//hide in to-calendar all days that are less than selected day.
					//this condition is true only in same month.
					else if(daysToHide > 0 && dayOfMonth < daysToHide){
						var td = document.createElement("td");
						td.className = "hiddenDay";
						td.innerHTML = dayOfMonth;
						tr.appendChild(td);
						//which day of week is the day of month?
						var day = new Date(year,month,dayOfMonth).getDay();
						if(day == 0){//sunday?
							//yes, the sunday is the last day of week so
							//next iteration must fill new tr-tag
							tr = document.createElement("tr");
							//save new tr-tag to table
							calendar.appendChild(tr);
						}
						dayOfMonth++;
					}
					else {//no empty td-tags left
						//which day of week is the day of month?
						var day = new Date(year,month,dayOfMonth).getDay();
						if(day == 0){//sunday?
							//yes, the sunday is the last day of week so
							//fill it with day number
							var td = document.createElement("td");
							if(selectedDay == dayOfMonth){
						      td.className = "daySelected";
							} else {
					      	td.className = "sunday";
					      }
							var a = document.createElement("a");
							a.setAttribute("href","#");
							a.setAttribute("onclick","setSelectedDay(this);return false;");
							a.innerHTML = dayOfMonth;
							td.setAttribute("name",dayName);
							td.appendChild(a);
							tr.appendChild(td);
							//save the tr-tag to table
							//calendar.appendChild(tr);
							//next iteration must fill new tr-tag
							tr = document.createElement("tr");
							//save new tr-tag to table
							calendar.appendChild(tr);
							//get next day number
							dayOfMonth++;
						}
						//day is not sunday
						else if( dayOfMonth <= days){//print all day numbers of the month
							//create always new td-tag for a day number
							var td = document.createElement("td");
							if(selectedDay == dayOfMonth){
						                td.className = "daySelected";
							} else {
					                        td.className = "dayNotSelected";
					                }
							var a = document.createElement("a");
							a.setAttribute("href","#");
							a.setAttribute("onclick","setSelectedDay(this);return false;");
							a.innerHTML = dayOfMonth;
                                                        td.appendChild(a);
							td.setAttribute("name",dayName);
							//append the day to table row
							tr.appendChild(td);
							//get new day of month
							dayOfMonth++;
						}
					}
				}

			}
                        /*
			 * Function makeCalendar makes the calendar and prints it to table tag.
			 * All child tags of table tag are created and filled with day numbers.
			 * Every day number is a linki and can be selected to set dates. Some day numbers
			 * are hidden and can not be clicked.
			 */
			function makeCalendarForIE(tableId, year, month, days, selectedDay, daysToHide, dayName){

				var firstDayOfMonthInWeek = new Date(year,month,1).getDay();
				var emptyDays = firstDayOfMonthInWeek-1;
				//also there is a need to calculate how many empty td-tags will be printed
				//to show the day numbers in correct places in calendar
				if(emptyDays < 0){//if sunday is first day of month
					emptyDays = 6;//print 6 empty td-tags
				}
				var calendar = document.getElementById(tableId);//get the table
				
				var dayOfMonth = 1;//day numbers
				var totalDays = days + emptyDays;//this is for a loop
				var tr = calendar.insertRow();//create first tr-tag of table
				
				for(var d = 0; d < totalDays; d++){//print days
					//first print empty td-tags
					if(d < emptyDays){//any td-tag to be printed?					
						var td = tr.insertCell();
						td.innerHTML = " ";
			                        td.className = "emptyDate";
					}
					//when selecting some day in from-calendar, there is a need to
					//hide in to-calendar all days that are less than selected day.
					//this condition is true only in same month.
					else if(daysToHide > 0 && dayOfMonth < daysToHide){
						var td = tr.insertCell();
						td.innerHTML = dayOfMonth;
				                td.className = "hiddenDay";
						//which day of week is the day of month?
						var day = new Date(year,month,dayOfMonth).getDay();
						if(day == 0){//sunday?
							//yes, the sunday is the last day of week so
							//next iteration must fill new tr-tag
							tr = calendar.insertRow();
						}
						dayOfMonth++;
					}
					else {//no empty td-tags left
						//which day of week is the day of month?
						var day = new Date(year,month,dayOfMonth).getDay();
						if(day == 0){//sunday?
							//yes, the sunday is the last day of week so
							//fill it with day number
							var td = tr.insertCell();
							var a = document.createElement("a");
							a.setAttribute("href","#");
							a.onclick=function(){setSelectedDay(this); return false};
							a.innerHTML = dayOfMonth;
							td.setAttribute("name",dayName);
							if(selectedDay == dayOfMonth){
					                        td.className = "daySelected";
							} else {
					                        td.className = "sunday";
					                }
							td.appendChild(a);
							//next iteration must fill new tr-tag
							/********************************
							 * If the dayOfMonth is the last dat of month and it's sunday
							 * don't create any more any tr-element, otherwise the browser
							 * will create an empty td-element
							 ********************************/
							if(dayOfMonth < days){
								tr = calendar.insertRow();
								//get next day number
								dayOfMonth++;
							}
						}
						//day is not sunday
						else if( dayOfMonth <= days){//print all day number of the month
							//create always new td-tag for a day number
							var td = tr.insertCell();
							var a = document.createElement("a");
							a.setAttribute("href","#");
							a.onclick=function(){setSelectedDay(this);return false};
							a.innerHTML = dayOfMonth;
							td.setAttribute("name",dayName);
							if(selectedDay == dayOfMonth){
					                        td.className = "daySelected";
							} else {
					                        td.className = "dayNotSelected";
					                }
							td.appendChild(a);							
							//get new day of month
							dayOfMonth++;
						}
					}
				}

			}

			function getFromDate(){
                                var month = getCurrentMonth();
                                if(month < 10){
                                    month = "0"+month;
                                }
                                var day = fromDay;
                                if(day < 10){
                                    day = "0"+fromDay;
                                }
				return getCurrentYear()+""+month+""+day;
			}

			function getToDate(){
                                var month = (toCalendar.getMonth()+1);
                                if(month < 10){
                                    month = "0"+month;
                                }
                                var day = toDay;
                                if(day < 10){
                                    day = "0"+toDay;
                                }
				return toCalendar.getFullYear()+""+month+""+day;
			}
