/*This is the primary way of calling functions we want to run when the whole page has loaded. Uses jQuery. */
$(document).ready(function() {

	if(document.getElementById("text_fly_from")) {
		try {document.getElementById("text_fly_from").focus();}
		catch(err) {;}
	}
	
	keepFooterDown();

// takes too much time!
// 	$('#1 .calendartable td').hover(function() {
// 		$(this).addClass('cal_td_hover');
// 	}, function() {
// 		$(this).removeClass('cal_td_hover');
// 	});

//Do rounded corners
	if(document.getElementById("top_menu"))
		$("#top_menu").corner("bl 10px");
// 	if(document.getElementById("tab_2"))
// 		$("#tab_2").corner("top");
// 	if(document.getElementById("tab_3"))
// 		$("#tab_3").corner("top");
// 	if(document.getElementById("tab_4"))
// 		$("#tab_4").corner("top");
// 	if(document.getElementById("tab_5"))
// 		$("#tab_5").corner("top");

	//if results_wait page
	if($("#wait_animation")) {
		wait_animation(2);
	}
	
	//update selected days in calendars when page is loaded
	if ($("calendararea")) {
		mark_selected_days();
	}
	
});

/*Function that marks calendars days selected when the page is loaded, based on the values of selects. Used on index page and also result pages.*/
function mark_selected_days() {
	//loop through all calendars
	for (cal_id = 1; cal_id < 9; cal_id++) { //when you aff lmh, update this integer to 10
		//find out which calendar selects value should be used
		var b_call_change = false;
		switch (cal_id) {
			//flight there
			case 1: s_select_id = 'fly_there_day';
					s_tab_id = 1; break;
			case 2: s_select_id = 'fly_back_day';
					s_tab_id = 1; 
					b_call_change = true; break;
			case 3: s_select_id = 'car_there_day';
					s_tab_id = 2; break;
			case 4: s_select_id = 'car_back_day'; 
					s_tab_id = 2; 
					b_call_change = true; break;
			case 5: s_select_id = 'hotel_checkin_day'; 
					s_tab_id = 3; break;
			case 6: s_select_id = 'hotel_checkout_day';
					s_tab_id = 3; 
					b_call_change = true; break;
			case 7: s_select_id = 'fh_there_day'; 
					s_tab_id = 4; break;
			case 8: s_select_id = 'fh_back_day'; 
					s_tab_id = 4; 
					b_call_change = true; break;
			case 9: s_select_id = 'lmh_there_day'; 
					s_tab_id = 5; 
					b_call_change = true; break;
		}
		
		//check if calendar exists
		if ($("#x_"+cal_id).length) {
			//fetch the value
			var s_select_value = "";
// 			if($("#"+s_select_id)) {
// 				s_select_value = $("#"+s_select_id).val();
// 			}
		
			//call that takes care of updating calendar
			if (s_select_value = $("#"+s_select_id).val()) {
				//if result page, called from ready function -> last parameter true
				if($("#results_area").length) {
// 					alert(cal_id+" päivitetään kalenteri onload");
					select_day_changed(s_select_value, cal_id, true);
					//only call change function that scrolls both calendars at once, when cal_id refers 
					//to second calendar (because othwerwise select_day_changed hasn't been called on second 
					//calendar and calx_cursel is empty for it, will cause problem in change function)
					if(b_call_change) {
						change(s_tab_id, false);
					}
				} else if($("#main_info_column").length) { //index page, called from ready function -> last parameter true
// 					alert(cal_id+" päivitetään kalenteri index");
					select_day_changed(s_select_value, cal_id, true);
				} else { //index error page, called from ready function -> last parameter true
					select_day_changed(s_select_value, cal_id, true);
					//only call change function that scrolls both calendars at once, when cal_id refers 
					//to second calendar (because othwerwise select_day_changed hasn't been called on second 
					//calendar and calx_cursel is empty for it, will cause problem in change function)
					if(b_call_change) {
						change(s_tab_id, false);
					}
				}
			}
		}
	}
}



/*Shows calendars and dates that are on right side and tell the return date.
Used in flight and fh forms. Form identified by number 1 = flights, 4 = flight +hotel.
Div's that are hidden when user select oneway trip are inside div's with style
right_side_f (in flights) and right_side_fh (in fh). This function shows the hidden div's
when user selects rountrip. */

function show_right_side(id) {
	// For displaying selections that are in right side

	if(id == 1) {
		var array_divs = $('div.right_side_f');
		$(array_divs).show(); //shows right side Paluu and calendar 2
	}

	if(id == 4) {
		var array_divs = $('div.right_side_fh');
		$(array_divs).show();
	}
}

/*Hides calendars and dates that are on right side and tell the return date.
Used in flight and fh forms. Form identified by number 1 = flights, 4 = flight +hotel.
Div's that are hidden when user select oneway trip are inside div's with style
right_side_f (in flights) and right_side_fh (in fh). */

function hide_right_side(id) {
	// For hiding selections that are in right side
	var array_divs = document.getElementsByTagName("div");

	if(id == 1) {
		var array_divs = $('div.right_side_f');
		$(array_divs).hide();
	}

	if(id == 4) {
		var array_divs = $('div.right_side_fh');
		$(array_divs).hide();
	}
}

function getWindowHeight() {
	// For finding out the current window height
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	} else {
		if (document.documentElement&& document.documentElement.clientHeight) {
			windowHeight=document.documentElement.clientHeight;
		} else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function keepFooterDown() {
	// Footer insist being tricky. Trying to keep it down where it belongs
	if (document.getElementById('footer')) {
		var windowHeight=getWindowHeight();
		if (windowHeight>0) {
			var contentHeight=document.getElementById('super_container').offsetHeight;
			var footerElement=document.getElementById('footer');
			var footerHeight=footerElement.offsetHeight;
			if (windowHeight-(contentHeight+footerHeight)>=0) {
				footerElement.style.position='relative';
				footerElement.style.top=(windowHeight-(contentHeight+footerHeight))+'px';
// 				alert(footerElement.style.top);
			} else {
			footerElement.style.position='static';
			}
		}
	}
}

window.onresize = function() {
	keepFooterDown();
}

//function that changes result_table's row color when user's mouse is over the row

// function change_bg_color(obj) {
// 	obj.className = "result_row_hover";
// }

//function that changes result_table's row color back to original when user's mouse leaves the row

// function reset_bg_color(obj, row) {
// 	if(row == "a") {
// // 		obj.style.backgroundColor = '#395530';
// 		obj.className = "result_row_a";
// 	}
// 	if(row == "b") {
// 		obj.className = "result_row_b";
// 	}
// }

/* Function that is called when user chooses a new month/year from select list.
It checks which day is currently chosen and then calls cal_change_day_options to make the change in fly_there_day select lists available options. */
function cal_change_day(s_year_month, i_cal_type) {

	//kaivetaan stringistä esiin kuukausi ja vuosi
	//parses all the numbers in the sring to an array
	var parse_year_month = s_year_month.match(/\d+/g);
	
	//alert(parse_year_month[0] + " " + parse_year_month[1])
	
	var i_sel_day;
	var i_sel_month = parse_year_month[1];
	var i_sel_year = parse_year_month[0];

	//tsekataan mikä kuukausi ja päivitetään valikon päivät
	//otetaan talteen valittuna oleva päivä
	//luodaan valikon päivät uusiksi
	//laitetaan valituksi aiemmin valittu päivä, paitsi jos
	//sitä ei löydy eli se on 29, 30 tai 31
	//valitaan sen sijaan joko 1 (ei?) tai viimeisin päivä 

	var s_select_name = "";
	
	switch (i_cal_type) {
		case 1: s_select_name = 'fly_there_day'; break;
		case 2: s_select_name = 'fly_back_day'; break;
		case 3: s_select_name = 'car_there_day'; break;
		case 4: s_select_name = 'car_back_day'; break;
		case 5: s_select_name = 'hotel_checkin_day'; break;
		case 6: s_select_name = 'hotel_checkout_day'; break;
		case 7: s_select_name = 'fh_there_day'; break;
		case 8: s_select_name = 'fh_back_day'; break;
		case 701: s_select_name = 'fh_checkin_day'; break; //for hotel days
		case 801: s_select_name = 'fh_checkout_day'; break; //for hotel days
		case 9: s_select_name = 'lmh_there_day'; break;
		default: s_select_name = 'error'; break;
	}
		
	if (day_select=document.getElementById(s_select_name)) {
	
		var options_length = day_select.options.length;
		
		// we store the current selected option	
		for (i=0; i<options_length; i++){
			if(day_select.options[i].selected == true) {
				i_sel_day = day_select.options[i].value;
				break;
			}
		}
	
		//check's how many days there should be in the list
		var i_last_day = last_day_in_this_month(i_sel_month,i_sel_year);
		cal_change_day_options(i_last_day,i_sel_day,i_cal_type);
	} else {
		alert("FIXME A");
	}
}
  
//Function that changes the month and year of the select fly_there_year_month and day based on user's selection in the calendar. Only changes the other selects and calendar's selected day if their current selected day is earlier than the new day user selected in 'there' calendar (would cause a conflict).

function cal_change_year_month(i_sel_day, i_sel_month, i_sel_year, i_cal_type) {
	//changes the selected month in the right calendar

	var s_select_name = "";
	var s_select_back = "";
	var s_select_back_day = "";
	var i_cal_type_2 = "";
	var b_fh = false; //this boolean true if fh, and we need to change two dates at once
	var b_check_back = false; //this boolean is true if user clicked the there calendar
	var b_change_back = false;
	var s_select_name_h = "";
	var s_select_back_h = "";
	var i_cal_type_h;
	
	switch (i_cal_type) {
		//change there selects (and change back selects value to this date, not currently)
		case 1: s_select_name = 'fly_there_year_month';
				s_select_back = 'fly_back_year_month';
				s_select_back_day = 'fly_back_day';
				i_cal_type_2 = 2;
				b_check_back = true;
				break;
		case 2: s_select_name = 'fly_back_year_month'; break;
		case 3: s_select_name = 'car_there_year_month';
				s_select_back = 'car_back_year_month';
				s_select_back_day = 'car_back_day';
				i_cal_type_2 = 4;
				b_check_back = true;
				break;
		case 4: s_select_name = 'car_back_year_month'; break;
		case 5: s_select_name = 'hotel_checkin_year_month';
				s_select_back = 'hotel_checkout_year_month';
				s_select_back_day = 'hotel_checkout_day';
				i_cal_type_2 = 6;
				b_check_back = true;
				break;
		case 6: s_select_name = 'hotel_checkout_year_month'; break;
		case 7: s_select_name = 'fh_there_year_month';
				s_select_name_h = 'fh_checkin_year_month'; 
				i_cal_type_h = 701; //needed when we change the days in checkin
				b_fh = true; //for hotel month/year
				s_select_back = 'fh_back_year_month';
				s_select_back_day = 'fh_back_day';
				s_select_back_h = 'fh_checkout_year_month';
				i_cal_type_h2 = 801; //needed when we change the days in checkout
				i_cal_type_2 = 8;
				b_check_back = true;
				//FIXME missing proper handling for hotel selects?
				break;
		case 8: s_select_name = 'fh_back_year_month';
				s_select_name_h = 'fh_checkout_year_month';
				i_cal_type_h = 801; //needed when we change the days in checkout
				b_fh = true;  //for hotel month/year
				break;
		case 9: s_select_name = 'lmh_there_year_month'; break;
				//only one calendar, no need to change anything
		default: s_select_name = 'error'; break;
	}
	
	//check now, if the other select's date is earlier than the new date user chose
	if (b_check_back && (day_select_2=document.getElementById(s_select_back_day)) &&
		(year_month_select_2=document.getElementById(s_select_back))) {
		//fetch the other date
// 		var day_value = $('#'+s_select_back_day).val();
		var day_value = day_select_2.value;
// 		var year_month_value = $('#'+s_select_back).val();
		var year_month_value = year_month_select_2.value;
		var array_split = year_month_value.split("_");
		var month_value = array_split[1];
		var year_value = array_split[0];
		//make it into js date
		var date_1 = new Date();
		var date_2 = new Date();
		date_2.setFullYear(year_value,month_value-1,day_value);
// 		var date_1 = new Date();
		date_1.setFullYear(i_sel_year,i_sel_month-1,i_sel_day);
		
		if(date_2 < date_1) { //change second date to the same as first
			b_change_back = true;
		} else { //only scroll second calendar
			//call function that scrolls calendar to right location
// 			alert("Eka scroll kutsu, 2. kalenteri.");
			calendar_scroll(i_sel_day, i_sel_month, i_sel_year, i_cal_type_2);
		}
	}
		
	//user clicked the first calendar, change first selects and possibly change second select and change also second calendar's selected date
	if (b_check_back && (year_month_select=document.getElementById(s_select_name)) &&
		(year_month_select_2=document.getElementById(s_select_back))) {
		// fly_there_year_month = year_month_select.selectedIndex;
		// var fly_there_year_month=document.form_flights.fly_there_year_month;
		
		//First, change there_year_month select
		var options_length = year_month_select.options.length;
		var selected_year_month = "" + i_sel_year + "_" + i_sel_month;
		
		for (i=0; i<options_length; i++){
			//we mark the current selected option as unselected
			if(year_month_select.options[i].selected == true) {
				year_month_select.options[i].selected = false;
			}
			// if it is the month & year user clicked, mark as selected
			if(year_month_select[i].value == selected_year_month) {
				year_month_select.options[i].selected = true; 
			}
		}
		
		// find out the last day in this month
		var i_last_day = last_day_in_this_month(i_sel_month,i_sel_year);

		//calls the function that changes the available days in the x_there_day select according to month
		//and marks selected the day (option) the user clicked in the calendar
		cal_change_day_options(i_last_day,i_sel_day,i_cal_type);
		
		if (b_change_back) {

			//Then, change back_year_month select (second selects)
			var options_length_2 = year_month_select_2.options.length;
			var selected_year_month_2 = "" + i_sel_year + "_" + i_sel_month;
			
			for (i=0; i<options_length; i++){
				//we mark the current selected option as unselected
				if(year_month_select_2.options[i].selected == true) {
					year_month_select_2.options[i].selected = false;
				}
				// if it is the month & year user clicked, mark as selected
				if(year_month_select_2[i].value == selected_year_month) {
					year_month_select_2.options[i].selected = true; 
				}
			}
			
			// find out the last day in this month
			var i_last_day_2 = last_day_in_this_month(i_sel_month,i_sel_year);

			//calls the function that changes the available days in the x_there_day select according to month
			//and marks selected the day (option) the user clicked in the calendar
			cal_change_day_options(i_last_day_2,i_sel_day,i_cal_type_2);
			
			//update second calendar's active day
			cal_highlight(i_sel_day, i_sel_month, i_sel_year, i_cal_type_2);
			
			//scroll the second calendar
// 			alert("Toka scroll kutsu, 2. kalenteri.");
			calendar_scroll(i_sel_day, i_sel_month, i_sel_year, i_cal_type_2);
		}
	
	//user clicked second calendar, change only that calendar 
	} else if(year_month_select=document.getElementById(s_select_name)) {
	
	// fly_there_year_month = year_month_select.selectedIndex;
	// var fly_there_year_month=document.form_flights.fly_there_year_month;
	var options_length = year_month_select.options.length;
	var selected_year_month = "" + i_sel_year + "_" + i_sel_month;
	
	for (i=0; i<options_length; i++){
		//we mark the current selected option as unselected
		if(year_month_select.options[i].selected == true) {
			year_month_select.options[i].selected = false;
		}
		// if it is the month & year user clicked, mark as selected
		if(year_month_select[i].value == selected_year_month) {
			year_month_select.options[i].selected = true; 
		}
	}
	
	// find out the last day in this month
	var i_last_day = last_day_in_this_month(i_sel_month,i_sel_year);

	//calls the function that changes the available days in the x_there_day select according to month
	//and marks selected the day (option) the user clicked in the calendar
	cal_change_day_options(i_last_day,i_sel_day,i_cal_type);

	} else {
		alert("FIXME B");
	}
	
	if (b_fh) { //if fh, we need to change the hotel checkin/checkout date aswell
	
		if (year_month_select=document.getElementById(s_select_name_h)) {
	
			// fly_there_year_month = year_month_select.selectedIndex;
			// var fly_there_year_month=document.form_flights.fly_there_year_month;
			var options_length = year_month_select.options.length;
			var selected_year_month = "" + i_sel_year + "_" + i_sel_month;
			
			for (var i=0; i<options_length; i++){
			
				//we mark the current selected option as unselected
				if(year_month_select.options[i].selected == true) {
					year_month_select.options[i].selected = false;
				}
			
				// if it is the month & year user clicked, mark as selected
				if(year_month_select[i].value == selected_year_month) {
					year_month_select.options[i].selected = true; 
				}
			}
		}
		
		cal_change_day_options(i_last_day, i_sel_day, i_cal_type_h);
		
		//do we change checkout date aswell?
		if (b_change_back && (year_month_select=document.getElementById(s_select_back_h))) {
			var options_length = year_month_select.options.length;
			var selected_year_month = "" + i_sel_year + "_" + i_sel_month;
			
			for (var i=0; i<options_length; i++){
			
				//we mark the current selected option as unselected
				if(year_month_select.options[i].selected == true) {
					year_month_select.options[i].selected = false;
				}
			
				// if it is the month & year user clicked, mark as selected
				if(year_month_select[i].value == selected_year_month) {
					year_month_select.options[i].selected = true; 
				}
			}
			cal_change_day_options(i_last_day, i_sel_day, i_cal_type_h2);
		}
	}
}

//function that changes fly_there_day select's options based on month
function cal_change_day_options(days, selected_day, i_cal_type) {

	// if cal_type == 1, we change 'there' calendar
	// if cal_type == 2, we change 'back' calendar

	var s_select_name = "";
	
	switch (i_cal_type) {
		case 1: s_select_name = 'fly_there_day'; break;
		case 2: s_select_name = 'fly_back_day'; break;
		case 3: s_select_name = 'car_there_day'; break;
		case 4: s_select_name = 'car_back_day'; break;
		case 5: s_select_name = 'hotel_checkin_day'; break;
		case 6: s_select_name = 'hotel_checkout_day'; break;
		case 7: s_select_name = 'fh_there_day'; break;
		case 8: s_select_name = 'fh_back_day'; break;
		case 701: s_select_name = 'fh_checkin_day'; break; //for hotel days
		case 801: s_select_name = 'fh_checkout_day'; break; //for hotel days
		case 9: s_select_name = 'lmh_there_day'; break;
		default: s_select_name = 'error'; break;
	}
	
	if (day_select=document.getElementById(s_select_name)) {
	
	day_select.options.length=0;
// 	day = day_select.selectedIndex;
	
	// oli:
	// var fly_there_day_select=document.getElementById('there_day');
	// fly_there_day_select.options.length=0;
	// fly_there_day = fly_there_day_select.selectedIndex;

	//FIXME puuttuu vielä: virhekäsittely jos 29, 30 tai 31 päivä ja kuukausi vaihtuu sellaiseksi, jossa valittu
	//päivä ei ole mahdollinen -> valituksi 1
	
	//adds new options and marks one of the selected
	for (i=1; i<=days; i++){
	// if it is the day user clicked, mark as selected
		if(i == selected_day) {
			day_select.options[day_select.options.length]=new Option(i, i, false, true) // name, value, defaultselected, selected
		} else {
			day_select.options[day_select.options.length]=new Option(i, i, false, false)
		}
	}
	} else {
		alert("FIXME");
	}
}

/*04.12.2009 Function that updates calendar's selected day and scrolls it to the right place when user changes day from day dropdown menu. This happens with both 'there' and 'back' dropdowns. Only changes the back selects and second calendar's selected day if their current selected day is earlier than the new day user selected in 'there' calendar (would cause a conflict). 
Boolean b_pageload is true if select_day_changed was called from $(document).ready(function(), otherwise false. */
function select_day_changed(i_sel_day, i_cal_type, b_pageload) {
	
	var s_select_name = "";
	var s_select_back = "";
	var s_select_back_day = "";
	
	var i_sel_month = "";
	var i_sel_year = "";
	var i_day_other = "";
	var i_month_other = "";
	var i_year_other = "";
	
	var b_fh = false; //this boolean true if fh, and we need to change two dates at once
	var b_check_back = false; //this boolean is true if user clicked the there calendar
	var b_change_back = false;
// 	var s_select_name_h = "";
// 	var i_cal_type_h;
	
	switch (i_cal_type) {
		//change 'there' calendar and change 'back' selects value to this date + 7 days
		case 1: s_select_name = 'fly_there_year_month';
				s_select_back = 'fly_back_year_month';
				s_select_back_day = 'fly_back_day';
				i_cal_type_other = 2;
				b_check_back = true;
				break;
		case 2: s_select_name = 'fly_back_year_month'; break;
		case 3: s_select_name = 'car_there_year_month';
				s_select_back = 'car_back_year_month';
				s_select_back_day = 'car_back_day';
				i_cal_type_other = 4;
				b_check_back = true;
				break;
		case 4: s_select_name = 'car_back_year_month'; break;
		case 5: s_select_name = 'hotel_checkin_year_month';
				s_select_back = 'hotel_checkout_year_month';
				s_select_back_day = 'hotel_checkout_day';
				i_cal_type_other = 6;
				b_check_back = true;
				break;
		case 6: s_select_name = 'hotel_checkout_year_month'; break;
		case 7: s_select_name = 'fh_there_year_month'; //FIXME
				s_select_name_h = 'fh_checkin_year_month'; 
				i_cal_type_h = 701; //needed when we change the days in checkin
				b_fh = true; //for hotel month/year
				s_select_back = 'fh_back_year_month';
				s_select_back_day = 'fh_back_day';
				//are checkin values needed?
				i_cal_type_other = 8;
				b_check_back = true;
				break;
		case 8: s_select_name = 'fh_back_year_month';
				s_select_name_h = 'fh_checkout_year_month';
				i_cal_type_h = 801; //needed when we change the days in checkout
				b_fh = true;  //for hotel month/year
				break;
		case 9: s_select_name = 'lmh_there_year_month';
				//only one calendar
				break;
		default: s_select_name = 'error'; break;
	}

	//update calendar's selected date (call to a function), and scroll it to right place (call to a function)
	if (year_month_select = document.getElementById(s_select_name)) {
		//find out which year_month is selected and pick month and year values from it
		var s_whole_string = year_month_select.options[year_month_select.selectedIndex].value;
		var array_split = s_whole_string.split("_");
		var i_sel_month = array_split[1];
		var i_sel_year = array_split[0];
		
		//update selected date in calendar
		cal_highlight(i_sel_day, i_sel_month, i_sel_year, i_cal_type);
		
		//check now, if the other select's date is earlier than the new date user chose
		if (b_check_back && (day_select_2=document.getElementById(s_select_back_day)) &&
			(year_month_select_2=document.getElementById(s_select_back))) {
			//fetch the other date
// 			var day_value = $('#'+s_select_back_day).val();
			var day_value = day_select_2.value;
// 			var year_month_value = $('#'+s_select_back).val();
			var year_month_value = year_month_select_2.value;
			var array_split = year_month_value.split("_");
			var month_value = array_split[1];
			var year_value = array_split[0];
			//make it into js date
			var date_1 = new Date();
			var date_2 = new Date();
			date_2.setFullYear(year_value,month_value-1,day_value);
			
			//fetch there year_month
// 			var date_1 = new Date();
			date_1.setFullYear(i_sel_year,i_sel_month-1,i_sel_day);
			
			if(date_2 < date_1) { //change second date to the same as first
				b_change_back = true;
			} else if(!b_pageload) { //only scroll second calendar if this is not a pageload call
				//call function that scrolls calendar to right location
// 				alert("Toinen päivä ok, scrollaa vain "+i_cal_type_other+" kalenteri (select_day_changed)");
				calendar_scroll(i_sel_day, i_sel_month, i_sel_year, i_cal_type_other);
			}
		}

		if (b_change_back) { //back date was earlier than there date!
			
			var i_day_other = i_sel_day;
			var i_month_other = i_sel_month;
			var i_year_other = i_sel_year;
	
			//call function that changes other year_month selects value
			cal_change_year_month(i_day_other, i_month_other, i_year_other, i_cal_type_other);
			//update selected date in other calendar
			cal_highlight(i_day_other, i_month_other, i_year_other, i_cal_type_other);
			//call function that scrolls calendar to right location
// 			calendar_scroll(i_day_other, i_month_other, i_year_other, i_cal_type_other);
		}
	}
	
	if (b_fh) { //if fh, we need to change the hotel checkin date aswell
	
		if (year_month_select=document.getElementById(s_select_name_h)) {
	
			// fly_there_year_month = year_month_select.selectedIndex;
			// var fly_there_year_month=document.form_flights.fly_there_year_month;
			var options_length = year_month_select.options.length;
			var selected_year_month = "" + i_sel_year + "_" + i_sel_month;
			
			for (var i=0; i<options_length; i++){
			
				//we mark the current selected option as unselected
				if(year_month_select.options[i].selected == true) {
					year_month_select.options[i].selected = false;
				}
			
				// if it is the month & year user clicked, mark as selected
				if(year_month_select[i].value == selected_year_month) {
					year_month_select.options[i].selected = true; 
				}
			}
		}
		// find out the last day in this month
		var i_last_day = last_day_in_this_month(i_sel_month,i_sel_year);		
		cal_change_day_options(i_last_day,i_sel_day, i_cal_type_h);
	}
}

/*04.12.2009 Function that updates calendar's selected day and scrolls it to the right place when user changes year_month from dropdown menu. This happens with both 'there' and 'back' dropdowns. Only changes the other selects and calendar's selected day if their current selected day is earlier than the new day user selected in 'there' calendar (would cause a conflict). */
function select_month_changed(i_sel_year_month, i_cal_type) {
	
	var s_select_name = ""; //contains day select's id
	var s_select_back = ""; //contains back day select's id
	var s_select_back_year_month = "";
	
	var i_sel_day ="";
	var i_sel_month = "";
	var i_sel_year = "";

	var i_day_other = "";
	var i_month_other = "";
	var i_year_other = "";
	var i_cal_type_other = "";
	
 	var b_fh = false; //this boolean true if fh, and we need to change two dates at once
	var b_check_back = false; //this boolean is true if user clicked the there calendar 
	var b_change_back = false;
// 	var s_select_name_h = "";
// 	var i_cal_type_h;
	
	switch (i_cal_type) {
		//change 'there' calendar
		case 1: s_select_name = 'fly_there_day';
				s_select_back = 'fly_back_day';
				s_select_back_year_month = 'fly_back_year_month';
				i_cal_type_other = 2;
				b_check_back = true;
				break;
		case 2: s_select_name = 'fly_back_day'; break;
		case 3: s_select_name = 'car_there_day';
				s_select_back = 'car_back_day';
				s_select_back_year_month = 'car_back_year_month';
				i_cal_type_other = 4;
				b_check_back = true;
				break;
		case 4: s_select_name = 'car_back_day'; break;
		case 5: s_select_name = 'hotel_checkin_day';
				s_select_back = 'hotel_checkout_day';
				s_select_back_year_month = 'hotel_checkout_year_month';
				i_cal_type_other = 6;
				b_check_back = true;
				break;
		case 6: s_select_name = 'hotel_checkout_day'; break;
		case 7: s_select_name = 'fh_there_day';
// 				s_select_name_h = 'fh_checkin_day'; 
				s_select_name_h = 'fh_checkin_year_month'; //we need to update checkin year_month
				i_cal_type_h = 701; //needed when we change the days in checkin
				b_fh = true; //for hotel month/year
				s_select_back = 'fh_back_day';
				s_select_back_year_month = 'fh_back_year_month';
				i_cal_type_other = 8;
				b_check_back = true;
				break;
		case 8: s_select_name = 'fh_back_day';
				// s_select_name_h = 'fh_checkout_day'; 
				s_select_name_h = 'fh_checkout_year_month'; //we need to update checkout year_month
				i_cal_type_h = 801; //needed when we change the days in checkout
				b_fh = true;  //for hotel month/year
				break;
		case 9: s_select_name = 'lmh_there_day'; break;
		default: s_select_name = 'error'; break;
	}
	
	//update calendar's selected date (call to a a function), and scroll it to right place (call to a function)
	if (day_select = document.getElementById(s_select_name)) {
		//find out which day is selected
		var i_sel_day = day_select.options[day_select.selectedIndex].value;
		var array_split = i_sel_year_month.split("_");
		var i_sel_month = array_split[1];
		var i_sel_year = array_split[0];
		
		//update selected date in calendar
		cal_highlight(i_sel_day, i_sel_month, i_sel_year, i_cal_type);
		
		//check now, if the other select's date is earlier than the new date user chose
		if (b_check_back && (day_select_2=document.getElementById(s_select_back)) &&
			(year_month_select_2=document.getElementById(s_select_back_year_month))) {
			//fetch the other date
// 			var day_value = $('#'+s_select_back).val();
			var day_value = day_select_2.value;
// 			var year_month_value = $('#'+s_select_back_year_month).val();
			var year_month_value = year_month_select_2.value;
			var array_split = year_month_value.split("_");
// 			var array_split = day_value.split("_");
			var month_value = array_split[1];
			var year_value = array_split[0];
			//make it into js date
			var date_1 = new Date();
			var date_2 = new Date();
			date_2.setFullYear(year_value,month_value-1,day_value);
			
			//fetch there year_month
// 			var date_1 = new Date();
			date_1.setFullYear(i_sel_year,i_sel_month-1,i_sel_day);
			
			if(date_2 < date_1) { //change second date to the same as first
				b_change_back = true;
			} else { //only scroll second calendar
				//call function that scrolls calendar to right location
// 				alert("Neljäs scroll kutsu, 2. kalenteri.");
				calendar_scroll(i_sel_day, i_sel_month, i_sel_year, i_cal_type_other);
			}
		}

		if (b_change_back) {
		
			var i_day_other = i_sel_day;
			var i_month_other = i_sel_month;
			var i_year_other = i_sel_year;
	
			//call function that changes other year_month selects value
			cal_change_year_month(i_day_other, i_month_other, i_year_other, i_cal_type_other);
			//update selected date in other calendar
			cal_highlight(i_day_other, i_month_other, i_year_other, i_cal_type_other);
			//call function that scrolls calendar to right location
// 			calendar_scroll(i_day_other, i_month_other, i_year_other, i_cal_type_other);
		}
	}
	
	if (b_fh) { //if fh, we need to change the hotel checkin/checkout date aswell
	
		if (year_month_select=document.getElementById(s_select_name_h)) {
	
			// fly_there_year_month = year_month_select.selectedIndex;
			// var fly_there_year_month=document.form_flights.fly_there_year_month;
			var options_length = year_month_select.options.length;
			var selected_year_month = "" + i_sel_year + "_" + i_sel_month;
			
			for (var i=0; i<options_length; i++){
			
				//we mark the current selected option as unselected
				if(year_month_select.options[i].selected == true) {
					year_month_select.options[i].selected = false;
				}
			
				// if it is the month & year user clicked, mark as selected
				if(year_month_select[i].value == selected_year_month) {
					year_month_select.options[i].selected = true; 
				}
			}
		}
		// find out the last day in this month
		var i_last_day = last_day_in_this_month(i_sel_month,i_sel_year);		
		cal_change_day_options(i_last_day,i_sel_day, i_cal_type_h);
	}
}

/*Function that highlights the calendar cell user clicked on. It also calls calendar scrolling function to make sure that calendar is in correct position. (created 24.09.2009, updated 3.12.2009) */
function cal_highlight(i_sel_day, i_sel_month, i_sel_year, i_cal_type) {
	var s_cell_name = "";
	var s_calendar_name = "";
	var cell = "";

// 	switch (i_cal_type) {
// 		case 1: s_cell_name = i_sel_day + '_' + i_sel_month + '_' + i_sel_year +'_'+ i_cal_type; 
// 				s_calendar_name = i_cal_type; break;
// 
// 		case 2: s_cell_name = i_sel_day + '_' + i_sel_month + '_' + i_sel_year +'_'+ i_cal_type; 
// 				s_calendar_name = 'search_cal_2'; break;
// 		default: s_cell_name = 'error'; break;
// 	}
	
	s_cell_name = '#' + "x_" + i_sel_day + '_' + i_sel_month + '_' + i_sel_year +'_'+ i_cal_type; 
	s_calendar_name = "x_" + i_cal_type;

	if ($(s_cell_name)) {
	
		// Store currently selected day with jQuery data()
// 		var this_cal = $('#' + i_cal_type); // select correct calendar
// 		var value = jQuery.data(this_cal, "date");
// // 		alert(value);
// 		if (value) { // there was already a date stored to this calendar
// // 			s_remove_cell_name = '#' + value[0] + '_' + value[1] + '_' + value[2] + '_' + i_cal_type; // remove highlight from with this ID cell
// // 			alert(s_remove_cell_name);
// 			$(value).removeClass('on');
// 		}
//
// 		jQuery.data(this_cal, "date", s_cell_name);
// 		alert(jQuery.data(this_cal, "date"));
		//remove highlight ('on' class) from all the cells in this calendar
// 		var tds = document.getElementById(s_calendar_name).getElementsByTagName('td');
// 		for (var m=0; m<tds.length; m++) {
// 			$(tds[m]).removeClass('on');
// 		}
		
		var cur_select = $('#cal' + i_cal_type + '_cursel').text();
		if (cur_select != "0") { // try to remove a highlighted cell from a calendar only if there is one
			$(cur_select).removeClass('on');
// 			$('#' + s_calendar_name + ' td.on:first').removeClass('on'); // old way
// 		alert("call_highlight scroll kutsu, "+i_cal_type+" kalenteri.");
			calendar_scroll(i_sel_day, i_sel_month, i_sel_year, i_cal_type); // also, scroll calendars to correct location
		}
		$('#cal' + i_cal_type + '_cursel').text(s_cell_name);
		
		$('#' + s_calendar_name + ' td.on:first').removeClass('on');
		
		//highlight the cell user clicked on
// 		s_cell_name = '#'+s_cell_name;
		$(s_cell_name).addClass('on');
		// newtds[i].style.display = '';
		// not all browsers like display = 'block' for cells
		//call a function that scrolls calendar to right location!
// 		calendar_scroll(i_sel_day, i_sel_month, i_sel_year, i_cal_type); // also, scroll calendars to correct location
		
	} else { //FIXME, turn this of or make a check whether date is in the past! 
		alert("Valitsit päivän, joka on menneisyydessä. Valitse uudelleen.");
	}
}

/*Function that scrolls the calendar to a selected date.*/
function calendar_scroll(i_sel_day, i_sel_month, i_sel_year, i_cal_type) {
	var s_cell_name = "";
	var s_calendar_name = "";
	var cell = "";
	
	s_cell_id = "x_" + i_sel_day + '_' + i_sel_month + '_' + i_sel_year +'_'+ i_cal_type;
	s_calendar_id = '#' + "x_" + i_cal_type;

	if (document.getElementById(s_cell_id)) {
		s_cell_id = '#'+s_cell_id;
		
		//scrolling happens here (using scrollTo plugin)
		$(s_calendar_id).scrollTo(s_cell_id, 300, {offset:-48});
		
	} else {
		alert("Valittu päivämäärä on menneisyydessä.");
		return;
	}
}

function wait_animation(counter) {

// 			alert (i);
			if(counter >= 7) {
				counter = 2;
			} 
			
			show_image(counter);
			counter++;
			setTimeout("wait_animation("+counter+")",1000);
}

function show_image(number) {
		var anim_text = "images/wait_animation_0"+number+".png";
		$('#wait_animation .img_animation').attr('src', anim_text);
}


// The following functions are used when creating a calendar component with JS

function last_day_in_this_month(i_month,i_year) {
	// returns an integer that tells the number of days in given month, also handles leap years
	if (i_month==2) {
		if (i_year%400==0)
			return 29;
		if (i_year%100==0)
			return 28;
		if (i_year%4==0)
			return 29;
		return 28;
	}
	if (i_month<8) {
		if (i_month%2==1)
			return 31;
		else
			return 30;
	} else {
		if (i_month%2==0)
			return 31;
		else
			return 30;
	}
}

function calendar_increase_month(i_month, i_year, s_month_td_background) {
	// when the calendar needs to create a new month, we call this function to find out the number of days and bg color
 	var i_day = 1;
 	if (i_month == 12) {
 		i_year++;
 		i_month = 1;
 	}
 	else
 		i_month++;
 	
 	if (s_month_td_background == "month_a")
 		s_month_td_background = "month_b";
 	else
 		s_month_td_background = "month_a";
 		
 	var i_last_day_in_this_month=last_day_in_this_month(i_month,i_year);
 	
 	var arr_ret = new Array();
 	arr_ret[0] = 1;
 	arr_ret[1] = i_month;
 	arr_ret[2] = i_year;
 	arr_ret[3] = s_month_td_background;
 	arr_ret[4] = i_last_day_in_this_month;

 	return arr_ret;
}

//Function that shows selected number of age fields in form based on the value of fly_select_passengers_children.
function show_children_ages(child_count, div_id_name, age_id_name) {
	
	var id_div=document.getElementById(div_id_name + '_ages');
	
	//if children selected
	if(child_count > 0) {
		id_div.style.display = "block";

		//hide all age fields, user selects less children than before, the ones shown before must be hidden
		for (i=1;i<=5;i++) {
			var id_age=document.getElementById(age_id_name + i);
			id_age.style.display = "none";
		}
		
// 		age_fields_left = 0;
		
		//change display of age fields
		for (j=1;j<=child_count;j++) {
			var id_age=document.getElementById(age_id_name + j);
			id_age.style.display = "inline";
// 			age_fields_left = i + 1;
		}

		//hide the rest
// 		for (j=age_fields_left;j<=child_count;j++) {
// 			id_age=document.getElementById('fly_age_' + j);
// 			id_age.style.display = "none";
// 		}
		
	} else {
		id_div.style.display = "none";
	}
}

//Function that shows selected number of room type fields in light+hotel form
function show_room_type_selections(room_count, div_id_name, room_div_id_name) {
	// One type will always be selected
	
	// var id_div=document.getElementById(div_id_name + '_ages');
	

	// hide all room type fields, user selects less children than before, the ones shown before must be hidden
	for (i=1;i<=4;i++) {
		var id_room_type=document.getElementById(room_div_id_name + i);
		id_room_type.style.display = "none";
	}

	//change display of age fields
	for (j=1;j<=room_count;j++) {
		var id_room_type=document.getElementById(room_div_id_name + j);
		id_room_type.style.display = "inline";
	}

		//hide the rest
// 		for (j=age_fields_left;j<=child_count;j++) {
// 			id_age=document.getElementById('fly_age_' + j);
// 			id_age.style.display = "none";
// 		}

}

function written_month(i_month) {
	// function for presenting written month names instead of numeric

	var s_written = "";

	switch (i_month) {
		case 1: s_written = "tammikuu"; break;
		case 2: s_written = "helmikuu"; break;
		case 3: s_written = "maaliskuu"; break;
		case 4: s_written = "huhtikuu"; break;
		case 5: s_written = "toukokuu"; break;
		case 6: s_written = "kesäkuu"; break;
		case 7: s_written = "heinäkuu"; break;
		case 8: s_written = "elokuu"; break;
		case 9: s_written = "syyskuu"; break;
		case 10: s_written = "lokakuu"; break;
		case 11: s_written = "marraskuu"; break;
		case 12: s_written = "joulukuu"; break;
	}
	return s_written;
} // end of written_month(i_month)

function calendar_create(i_cal) {
 	// value $i_cal: 1 = left side, 2 = right side

 	var s_starts = "\n<div class='calendararea_top'>\n" + "\t<table class='calendartable' cellspacing='0'>\n";
	s_starts += "\t\t<tr class='calendartable_headers'>\n" + "\t\t\t<th class='first'>kk/vuosi</th><th>Ma</th>\n";
	s_starts += "\t\t\t<th>Ti</th><th>Ke</th>\n" + "\t\t\t<th>To</th><th>Pe</th>\n";
	s_starts += "\t\t\t<th>La</th><th>Su</th>\n" + "\t\t</tr></table></div>\n";
	document.write(s_starts);

//  var s_starts = "\n<div class='calendararea' id='" + i_cal +"'>\n" + "\t<table class='calendartable'>\n";
	s_starts = "\n<div class='calendararea' id='" + "x_" + i_cal +"'>\n" + "\t<table class='calendartable' cellspacing='0'>\n";
 	s_starts += "\t\t<tr class='calendartable_th_row'>\n" + "\t\t\t<th class='first'></th><th></th>\n";
	s_starts += "\t\t\t<th></th><th></th>\n" + "\t\t\t<th></th><th></th>\n";
	s_starts += "\t\t\t<th></th><th></th>\n" + "\t\t</tr>\n";
	document.write(s_starts);

	var d = new Date();
	var i_day = d.getDate(); // returns current day number, 1-31
	var i_month = d.getMonth() + 1; // returns current month, range is 0-11, so we need to add 1
	var i_year = d.getFullYear(); // returns current year
	var i_weekday = d.getDay(); // returs the number of current weekday, range 0-6, begins from Sunday..
	// We don't want that Sunday is '0'. We want it to be 7
 	if (i_weekday == 0)
 		i_weekday = 7;
 	// Now the range of i_weekday is 1 (for Monday) through 7 (for Sunday)
 	var i_last_day_in_this_month = last_day_in_this_month(i_month,i_year);
 	
 	// alert("day: "+ i_day +"\nmonth: "+ i_month +"\nyear: "+ i_year +"\nweekday: "+ i_weekday +"\nlast day in this month: "+i_last_day_in_this_month);
 	
 	var i_how_many_months = 12; // How many months we should list in a calendar (number of fully displayed months is this number - 1)
 	var s_month_td_background = "month_a";

	// All the basic information is now set

	// Firts row of the calendar is a bit difficult one
	// What day is the first day of first shown week?
 	var i_first_day_of_first_week = (i_day - i_weekday + 1);
 	var i_graydays = i_first_day_of_first_week;

 	var s_print_first_row = ""; // we get the row's data to this variable and then document.write it
 	
	for (i=1; i<8; i++) { // this for-loop creates the first row of the calendar
		if (i_day > i_last_day_in_this_month) {
			var a_new_month_values = calendar_increase_month(i_month, i_year, s_month_td_background);
			i_day = a_new_month_values[0];
			i_month = a_new_month_values[1];
			i_year = a_new_month_values[2];
			s_month_td_background = a_new_month_values[3];
			i_last_day_in_this_month = a_new_month_values[4];
			i_how_many_months--;
		}
		if (i_graydays < 1) { // If i_graydays (which was i_first_day_of_first_week) is smaller than one, it is from last month.
			s_print_first_row += "\t\t\t\t<td class='empty'>&nbsp;</td>\n"; // In that case we don't want to display it at all.
			i_graydays++;
		}
		else if (i_graydays < i_day) { // i_graydays belongs to this month but it is in the past - show it in calendar but do not add links
			s_print_first_row += "\t\t\t\t<td class='gone_a'>"+ i_graydays +"</td>\n";
			i_graydays++;
		}
		else { // prints current and future days
			var cell_id = i_day +"_"+ i_month +"_"+ i_year +"_"+ i_cal;
			var parameters = i_day +","+ i_month +","+ i_year +","+ i_cal;
			
			if (i_day == i_graydays) { // Today, current day
				s_print_first_row += "\t\t\t\t<td class='"+ s_month_td_background +" today' id='"+ "x_" + cell_id +"'>";
 				s_print_first_row += "<a href='javascript:;' onclick='cal_change_year_month("+ parameters  +"); cal_highlight("+ parameters +")'>"+ i_day +"</a>";
			} else { // Tomorrow and following future days
				s_print_first_row += "\t\t\t\t<td class='"+ s_month_td_background +"' id='"+ "x_" + cell_id +"'>";
 				s_print_first_row += "<a href='javascript:;' onclick='cal_change_year_month("+ parameters  +"); cal_highlight("+ parameters +")'>"+ i_day +"</a>";
			}

			s_print_first_row += "</td>\n";
			i_day++;
			i_graydays = i_graydays + 2;
		}
	}
 	if (i_day < 8) // this is actually the first cell to be shown in this row
 		s_print_first_row = "\t\t\t\t<td class='empty'>&nbsp;</td>\n" + s_print_first_row;
 	else if (i_day > 7 && i_day < 15)
 		s_print_first_row = "\t\t\t\t<td class='month_a_first'>"+ written_month(i_month) +"</td>\n" + s_print_first_row;
 	else if (i_day > 14)
 		s_print_first_row = "\t\t\t\t<td class='month_a_first'>&nbsp;</td>\n" + s_print_first_row;

 	s_print_first_row = "\t\t\t<tr>\n" + s_print_first_row + "\t\t\t</tr>\n";
 	document.write(s_print_first_row);
 	//document.write("\t\t\t\t\t</tr>\n");

// following rows, until the i_how_many_months is zero
 	while (i_how_many_months > 0) {
		// document.write("\t\t\t<tr>\n");
 		var s_print_row = ""; // we get the row's data to this variable and then document.write it out
 		
		for (i=1; i<8; i++) { // this for-loop creates one row to the calendar
			if (i_day > i_last_day_in_this_month) {
				var a_new_month_values = calendar_increase_month(i_month, i_year, s_month_td_background);
				i_day = a_new_month_values[0];
				i_month = a_new_month_values[1];
				i_year = a_new_month_values[2];
				s_month_td_background = a_new_month_values[3];
				i_last_day_in_this_month = a_new_month_values[4];
				i_how_many_months--;
			}
			if (i_how_many_months == 0) {
				s_print_row += "\t\t\t\t<td class='empty'>&nbsp;</td>\n";
			} else {
				cell_id = i_day +"_"+ i_month +"_"+ i_year +"_"+ i_cal;
				parameters = i_day +","+ i_month +","+ i_year +","+ i_cal;
			
				s_print_row += "\t\t\t\t<td class='"+ s_month_td_background +"' id='"+ "x_" + cell_id +"'><a href='javascript:;' onclick='cal_change_year_month("+ parameters +"); cal_highlight("+ parameters +")'>"+ i_day +"</a></td>\n";
			}
				
			if (i == 1) {
				first_cell_bg = s_month_td_background + "_first";;
			}
			i_day++;
		}

		if (i_day > 7 && i_day < 15)
			s_print_row = "\t\t\t\t<td class='"+ first_cell_bg +"'>"+ written_month(i_month) +"</td>\n" + s_print_row;
		else if (i_day > 14 && i_day < 22)
			s_print_row = "\t\t\t\t<td class='"+ first_cell_bg +"'>"+ i_year +"</td>\n" + s_print_row;
		else 
			s_print_row = "\t\t\t\t<td class='"+ first_cell_bg +"'>&nbsp;</td>\n" + s_print_row;

		// add <tr> tags
		s_print_row = "\t\t\t<tr>\n" + s_print_row + "\t\t\t</tr>\n";
		// s_print_row += "\t\t\t</tr>\n";
		document.write(s_print_row);
 		// document.write("\t\t\t\t\t</tr>\n");
	}

 	document.write("\t\t</table>\n");
 	document.write("\t</div>\n\n");
}

function redirect_popup(s_popup_url) {
	var popup_window = window.open(s_popup_url,'Siirtosivu','width=1022,height=700,menubar=yes,status=yes,location=yes,scrollbars=yes,resizable=yes,left=0,top=30,screenX=0,screenY=30');
	if (window.focus) {
		popup_window.focus()
	}
}

/* Updates airport list pages country content. */
function updateAirport(s_destination, s_airportcode, s_fieldid, s_fieldidhidden) {

	//search parents correct airport input fields (visible and hidden) and change their value

	window.opener.document.getElementById(s_fieldid).value = s_destination;
	window.opener.document.getElementById(s_fieldidhidden).value = s_airportcode;
	
	//close airportlist page
	window.close();
	
}

/* Changes the currently visible tab on index page, when b_change_tabs has value true. 
   Always scrolls the visible calendars to correct position. 
   Also called from result pages to scroll their calendars, in this case b_change_tabs has value false. 
   Also called from error index page, when there was missing information in the search form. In this case 
   b_change_tabs has value false. */

function change(id, b_change_tabs) {

	if (b_change_tabs) {

		var activate_sc='sc_'+id;
		var activate_tab='tab_'+id;
		
		for (i=1; i<5; i++) {
			var sc_id_block=document.getElementById('sc_'+i);
			if (sc_id_block != null) {
				sc_id_block.className='blockme';
							
				sc_tab_block=document.getElementById('tab_'+i);
				sc_tab_block.className='norm';
			}
		}
		var sc_id=document.getElementById(activate_sc);
		sc_id.className=('showme');
		
		var sc_tab=document.getElementById(activate_tab);
		sc_tab.className=('current');
		// keepFooterDown();
	}
	
	//scroll calendar to selected date
	var s_sel_date_first = "";
	var s_sel_date_second = "";
	var b_second_cal = true;
	
	switch (id) {
		case 1: s_sel_date_first =  $("#cal1_cursel").text();
				s_sel_date_second =  $("#cal2_cursel").text();
				break;
		case 2: s_sel_date_first =  $("#cal3_cursel").text();
				s_sel_date_second =  $("#cal4_cursel").text();
				break;
		case 3: s_sel_date_first =  $("#cal5_cursel").text();
				s_sel_date_second =  $("#cal6_cursel").text();
				break;
		case 4: s_sel_date_first =  $("#cal7_cursel").text();
				s_sel_date_second =  $("#cal8_cursel").text();
				break;
		case 5: s_sel_date_first =  $("#cal9_cursel").text();
				//no second calendar
				b_second_cal = false;
				break;
		default: s_sel_date_first =  'error';
				 s_sel_date_second =  'error';
				 break;
	}
	//find out separate day and month and year value for first calendar
	var array_split_first = s_sel_date_first.split("_");
	
	var day_value = array_split_first[1]
	var month_value = array_split_first[2];
	var year_value = array_split_first[3];
	var cal_value = array_split_first[4];
	
	//call scroll function
// 	alert(id+"id, tabiklikki kutsu,"+ cal_value +". kalenteri.");
	calendar_scroll(day_value, month_value, year_value, cal_value);
	
	//same for the second calendar if it is needed
	if(b_second_cal) {
		var array_split_second = s_sel_date_second.split("_");
		
		var day_value = array_split_second[1]
		var month_value = array_split_second[2];
		var year_value = array_split_second[3];
		var cal_value = array_split_second[4];
		
		//call scroll function
// 		alert (id+" id, tabiklikki kutsu, "+ cal_value +". kalenteri.");
		calendar_scroll(day_value, month_value, year_value, cal_value);
	}
}

// not used
// function find_sel_value(sel_id) {
// 	var sel_value = "";
// 	if (sel_value = $("#"+sel_id).val()) {
// 		return sel_value;
// 	} else {
// 		return sel_value;
// 	}
// }

/*Not in use! FIXME if you want to use it*/
// function cal_rollover(i_sel_day, i_sel_month, i_sel_year, i_cal_type, cal_action) {
// 	var s_cell_name = "";
// 	var s_calendar_name = "";
// 	var cell = "";
// 
// 	switch (i_cal_type) {
// 		case 1: s_cell_name = i_sel_day + '_' + i_sel_month + '_' + i_sel_year +'_'+ i_cal_type; 
// 				s_calendar_name = 'search_cal_1'; break;
// 
// 		case 2: s_cell_name = i_sel_day + '_' + i_sel_month + '_' + i_sel_year +'_'+ i_cal_type; 
// 				s_calendar_name = 'search_cal_2'; break;
// 		default: s_cell_name = 'error'; break;
// 	}
// 
// 	if (cell=document.getElementById(s_cell_name)) {
// 
// 		s_cell_name = '#'+s_cell_name;
// 		
// 		//add new style
// 		$(s_cell_name).addClass('rollover');
// 
// 	} else {
// 		alert("FIXME B");
// 	}
// }