﻿function chooseCity(code) {			
      window.opener.document.getElementById(window.opener.document.getElementById('info_clicked').value).value = code;
			window.close();		
}

function showPopUp(action) {
//  var popup = Liferay.Popup({modal:true,width:400,height:100,title:"Kaupunkilista",center:true});
  if (action == 0)
    document.getElementById('info_clicked').value='reserveFlightDepartCityText';
  if (action == 1)
    document.getElementById('info_clicked').value='reserveFlightArriveCityText';
//window.alert(document.getElementById('info_clicked').value);
//  var organizationWindow = window.open('kaupunkilista?p_p_state=pop_up','Kaupunkilista','directories=no, height=640, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, width=400');
  var organizationWindow = window.open('/area/citylist.html','Kaupunkilista','directories=no, height=640, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, width=400');

//var organizationWindow = window.open('
//<portlet:renderURL windowState="<%= LiferayWindowState.POP_UP.toString() %>"><portlet:param name="struts_action" value="/enterprise_admin/select_organization" /><portlet:param name="tabs1" value="organizations" /></portlet:renderURL>',
//'Kaupunkilista','directories=no, height=640, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no, width=680');

  organizationWindow.focus();

/*var popup = new Expanse.Popup(
				{
					header: 'Kaupunkilista',
					position:[150,150],
					modal:true,
					width:500,
					height:300,
					xy: ['center', 100],
					url: 'url',
					urlData: {
						parameter1: 'value1',
            parameter2: 'value2'
					}
				}
			);
*/
//  jQuery(popup).load("");
}

function AreaReserveFlight() {
	/*
	Popupin otsikko
	*/
	this.header = "HINNAT & VARAA";
	
	/*
	Hae -napin teksti
	*/
	this.submit = "&nbsp;";
	
	this.closeHtml = "X";
	
	this.calendarHtml = "&nbsp;";
	
	/*
	Oletusarvoinen huoneiden lukum&auml;&auml;r&auml;
	*/
	this.rooms = 1;
	
	/*
	Perill&auml;olo&ouml;iden v&auml;himm&auml;ism&auml;&auml;r&auml;
	*/
	this.minimumStay = 1;
	
	this.url = "http://wftc2-b2c.amadeus.com/plnext/INareaB2C/HolidayPackageAvailabilityAction.action";
	this.site = "BAULBAUL";
	this.language = "FI";
	
	this.departCities = [["", "- valitse -"],
		["HEL", "Helsinki"],
		["JOE", "Joensuu"],
		["JYV", "Jyv&auml;skyl&auml;"],
		["KAJ", "Kajaani"],
		["KEM", "Kemi/Tornio"],
		["KOK", "Kokkola/Pietarsaari"],
		["KUO", "Kuopio"],
		["KAO", "Kuusamo"],
		["OUL", "Oulu"],
		["POR", "Pori"],
		["RVN", "Rovaniemi"],
		["SVL", "Savonlinna"],
		["SJY", "Sein&auml;joki"],
		["TMP", "Tampere"],
		["TKU", "Turku"],
		["VAA", "Vaasa"]];
	this.arriveCities = [["", "- valitse -"],
		["AALG", "Algarve"],
		["AMS", "Amsterdam"],
		["ATH", "Ateena"],
		["BKK", "Bangkok"],
		["BCN", "Barcelona"],
		["BIQ", "Biarritz"],
		["BER", "Berliini"],
		["BTS", "Bratislava"],
		["BRU", "Bryssel"],
		["BUD", "Budapest"],
		["BUH", "Bukarest"],
		["AGP", "Costa del Sol"],
		["CUR", "Curacao"],
		["DEL", "Delhi"],
		["DXB", "Dubai"],
		["DUB", "Dublin"],
		["DUS", "Dusseldorf"],
		["DBV", "Dubrovnik"],
		["FAO", "Faro"],
		["FLR", "Firenze"],
		["FRA", "Frankfurt"],
		["GDN", "Gdansk"],
		["GVA", "Geneve"],
		["GOI", "Goa"],
		["GOT", "G&ouml;teborg"],
		["HAM", "Hampuri"],
		["HEL", "Helsinki"],
		["HKG", "Hong Kong"],
		["IBZ", "Ibiza"],
		["IST", "Istanbul"],
		["CAI", "Kairo"],
		["KAJ", "Kajaani"],
		["CAN", "Kanton"],
		["CPT", "Kapkaupunki"],
		["KEM", "Kemi/Tornio"],
		["KRK", "Krakova"],
		["KAO", "Kuusamo"],
		["KUO", "Kuopio"],
		["CPH", "K&ouml;&ouml;penhamina"],
		["142", "Las Palmas, Gran Canaria"],
		["LEVI", "Levi"],
		["LIS", "Lissabon ja l&auml;hialue"],
		["LJU", "Ljubljana"],
		["LON", "Lontoo"],
		["LAX", "Los Angeles"],
		["LYS", "Lyon"],
		["FNC", "Madeira"],
		["MAD", "Madrid"],
		["PMI", "Mallorca"],
		["MLA", "Malta"],
		["MAN", "Manchester"],
		["MRU", "Mauritius"],
		["MIA", "Miami"],
		["MIL", "Milano"],
		["BOM", "Mumbai"],
		["MUC", "München"],
		["NGO", "Nagoya"],
		["NYC", "New York"],
		["NCE", "Nizza"],
		["ORL", "Orlando"],
		["OSL", "Oslo"],
		["OUL", "Oulu"],
		["PAR", "Pariisi"],
		["BJS", "Peking"],
		["PSA", "Pisa"],
		["HKT", "Phuket"],
		["OPO", "Porto"],
		["PRG", "Praha"],
		["REK", "Reykjavik"],
		["RIX", "Riika"],
		["ROM", "Rooma"],
		["RVN", "Rovaniemi"],
		["SFO", "San Francisco"],
		["SVQ", "Sevilla"],
		["SG", "Singapore"],
		["SHA", "Shanghai"],
		["SEL", "Soul"],
		["SPU", "Split"],
		["BOJ", "Sunny Beach"],
		["TLL", "Tallinna"],
		["STO", "Tukholma"],
		["TYO", "Tokio"],
		["YTO", "Toronto"],
		["WAW", "Varsova"],
		["VCE", "Venetsia"],
		["VAA", "Vaasa"],
		["VNO", "Vilna"],
		["VIE", "Wien"],
		["ZRH", "Zürich"]];
	this.defaultDepartCity = "HEL";
	this.defaultArriveCity = "";
	
	this.departCityError = "Valitse l&auml;ht&ouml;kaupunki";
	this.arriveCityError = "Valitse kohdekaupunki";
	
	/***********************************************
	 * Itse koodi alkaa t&auml;st&auml; -->
	*/
	this.dateFormat = "yyyy-MM-dd";
	this.startDate = new Date();
	this.startDate.setDate(this.startDate.getDate()+2);
	this.endDate = new Date();
	this.endDate.setFullYear(this.endDate.getFullYear()+1);
	this.parent = '';
	this.monthDays =[31,29,31,30,31,30,31,31,30,31,30,31];
	this.months =["Tammikuu","Helmikuu","Maaliskuu","Huhtikuu","Toukokuu","Kes&auml;kuu","Hein&auml;kuu","Elokuu","Syyskuu","Lokakuu","Marraskuu","Joulukuu"];
	this.dayHeaders =["S","M","T","K","T","P","L"];
	this.weekStartDay = 1;
	return this;
}

var currentState ="hotelandflight";
var areaReserveFlight=new AreaReserveFlight();
var ARFLib = {};
/**
Shows or hides flight and flight+hotel modes.
includes calendar refreshing to disable new set of dates.
*/
ARFLib.toggleMode = function (type) {
  
  this.currentState=type;

  depDateTmp = new Date();
  arrDateTmp = new Date();

  if(type == "flight") {
    jQuery('#reserveFlightArriveLoc').hide();
    jQuery('#reserveFlightDepartLoc').hide();
    jQuery('#reserveFlightDepartLocText').show();
    jQuery('#reserveFlightArriveLocText').show(); 
    jQuery("#flightDirections").show();
    jQuery('#bookRooms').hide();    
    jQuery('#passengerQTY').show();    
    jQuery('#reserveFlightHotel_submit').hide();    
    jQuery('#reserveFlight_submit').show();    
    jQuery('#linkToSearch').attr("href", "http://wftc2.e-travel.com/plnext/areaonline3/MasterPricerSearch.action?SITE=BAVHBAVH&LANGUAGE=FI&TRIP_FLOW=YES&EXTERNAL_ID=&DIRECT_LOGIN=NO");               
    jQuery('#reserveFlightForm').attr("action", "http://wftc2.e-travel.com/plnext/areaonline3/TravelShopperAvailability.action");
    jQuery('#amadeusSite').val("BAVHBAVH");

    areaReserveFlight.startDate.setDate(depDateTmp.getDate());
    depDateTmp.setDate(depDateTmp.getDate());

  } 
  
  if(type == "hotelandflight")  {
    jQuery('#reserveFlightArriveLoc').show();
    jQuery('#reserveFlightDepartLoc').show();
    jQuery('#reserveFlightDepartLocText').hide();
    jQuery('#reserveFlightArriveLocText').hide();  
    jQuery("#flightDirections").hide();
    jQuery('#bookRooms').show();
    jQuery('#passengerQTY').hide();    
    jQuery('#reserveFlightHotel_submit').show();    
    jQuery('#reserveFlight_submit').hide();
    jQuery('#linkToSearch').attr("href", "http://wftc2-b2c.amadeus.com/plnext/INareaB2C/CleanUpSessionPui.action?SITE=BAULBAUL&LANGUAGE=FI");        
    jQuery('#reserveFlightForm').attr("action", "http://wftc2-b2c.amadeus.com/plnext/INareaB2C/HolidayPackageAvailabilityAction.action");         
    jQuery('#amadeusSite').val("BAULBAUL");

    areaReserveFlight.startDate.setDate(depDateTmp.getDate() + 2);
    depDateTmp.setDate(depDateTmp.getDate() + 2);
    arrDateTmp.setDate(depDateTmp.getDate() + 1);
  }
  jQuery('#departDateField').attr("value", formatDate(depDateTmp, areaReserveFlight.dateFormat));
  jQuery('#arriveDateField').attr("value", formatDate(arrDateTmp, areaReserveFlight.dateFormat));
  
  // update all values in fields in form by calling these methods
  ARFLib.populateDepartDate();
  ARFLib.populateDepartMonthYear();
  ARFLib.populateArriveDate();
  ARFLib.populateArriveMonthYear();
}

/**
Shows or hides return date fields in type 'Flight' depending on which type user selects.
*/
ARFLib.toggleFlightType = function (type) {
  
  if(type == "oneway") {
    jQuery("#reserveFlightArriveDate").hide();
  } 
  
  if(type == "return")  {
    jQuery("#reserveFlightArriveDate").show();
  }
  
}

ARFLib.populateDepartCities = function () {
	var content = "";
	
	content += "<select name='reserveFlightDepartCity' onchange='' class='reserveFlight_selectBoxLocation'>";
	for (var i = 0; i < areaReserveFlight.departCities.length; i++) {
		if (areaReserveFlight.departCities[i]) {
			var iata = areaReserveFlight.departCities[i][0];
			var name = areaReserveFlight.departCities[i][1];
			content += "<option ";
			if (areaReserveFlight.defaultDepartCity == iata) {
				content += "selected='selected' ";
			}
			content += "value='";
			content += iata;
			content += "'>";
			content += name;
			content += "</option>";
		}
	}
	content += "</select>";
	
	jQuery("#reserveFlightDepartLoc").append(content);
}

ARFLib.prepareDepartCities = function () {
	var content = "";
	
	content += "<input style='width:145px' name='reserveFlightDepartCityText' id='reserveFlightDepartCityText' onchange='' /><a href='javascript:showPopUp(0)'><span id='fromCityDivImg' class='reserveFlight_info_img'></span></a>";
	
	jQuery("#reserveFlightDepartLocText").hide();
	jQuery("#reserveFlightDepartLocText").append(content);
}

ARFLib.prepareArriveCities = function () {
	var content = "";
	
	content += "<input style='width:145px' name='reserveFlightArriveCityText' id='reserveFlightArriveCityText' onchange='' /><a href='javascript:showPopUp(1)'><span id='toCityDivImg' class='reserveFlight_info_img'></span></a>";

	
	jQuery("#reserveFlightArriveLocText").hide();
	jQuery("#reserveFlightArriveLocText").append(content);
}


ARFLib.prepareflightDirections = function () {

	var content = "<div id='tripTypeRadios' class='reserveFlight_text'>";	
	content += "<input name='tripType' checked='checked' value='R' type='radio' class='reserveFlightRadio' onClick=\"ARFLib.toggleFlightType('return')\" /> Meno-Paluu <br /> ";
	content += "<input name='tripType' value='O' type='radio' class='reserveFlightRadio' onClick=\"ARFLib.toggleFlightType('oneway')\" /> Yhdensuuntainen";
	content += "</div>";

	jQuery("#flightDirections").hide();
	jQuery("#flightDirections").append(content);
}

ARFLib.populateArriveCities = function () {
	var content = "";
	
	content += "<select name='reserveFlightArriveCity' onchange='' class='reserveFlight_selectBoxLocation'>";
	for (var i = 0; i < areaReserveFlight.arriveCities.length; i++) {
		if (areaReserveFlight.arriveCities[i]) {
			var iata = areaReserveFlight.arriveCities[i][0];
			var name = areaReserveFlight.arriveCities[i][1];
			content += "<option ";
			if (areaReserveFlight.defaultArriveCity == iata) {
				content += "selected='selected' ";
			}
			content += "value='";
			content += iata;
			content += "'>";
			content += name;
			content += "</option>";
		}
	}
	content += "</select>";
	
	jQuery("#reserveFlightArriveLoc").append(content);
}
ARFLib.setDepartLoc = function(location) {
	jQuery("#reserveFlightForm > *:input[name='B_LOCATION_1']").attr("value", location);
	jQuery("#reserveFlightForm > *:input[name='E_LOCATION_2']").attr("value", location);
}
ARFLib.setArriveLoc = function(location) {
	jQuery("#reserveFlightForm > *:input[name='E_LOCATION_1']").attr("value", location);
	jQuery("#reserveFlightForm > *:input[name='B_LOCATION_2']").attr("value", location);
}
ARFLib.populateDepartDate = function() {
	var departStr = jQuery('#departDateField').attr("value");
	var departDate = new Date(getDateFromFormat(departStr, areaReserveFlight.dateFormat));
	
	var content = "";
	var start = 1;
	if (departDate.getFullYear() == areaReserveFlight.startDate.getFullYear() && departDate.getMonth() == areaReserveFlight.startDate.getMonth()) {
	  start = departDate.getDate();
	}
	content += "<select class='reserveFlight_selectBoxDate' onchange='ARFLib.updateDepartDate();'>";
	for (var i = start; i <= areaReserveFlight.monthDays[departDate.getMonth()]; i++) {
		content += "<option value='"+i+"'"+(departDate.getDate() == i ? "selected='selected'" : "")+">"+i+"</option>";
	}
	content += "</select>";
	
	jQuery("#reserveFlightDepartDateSelect").empty();
	jQuery("#reserveFlightDepartDateSelect").append(content);
	
}
ARFLib.populateDepartMonthYear = function () {
	var departStr = jQuery('#departDateField').attr("value");
	var departDate = new Date(getDateFromFormat(departStr, areaReserveFlight.dateFormat));
	var selected = ""+departDate.getFullYear()+ARBLib.zeroPad((departDate.getMonth()+1));
	
	var current = new Date(departDate);
  if(this.currentState == 'hotelandflight') {
    current.setDate(departDate.getDate());
  }
  else {
    current.setDate(departDate.getDate());
  }

	current.setDate(1);
	
	var content = "";
	
	content += "<select class='reserveFlight_selectBoxMonthYear' onchange='ARFLib.updateDepartMonthYear();'>";
	
	var maxLoop = 60;
	while (current.getYear() < areaReserveFlight.endDate.getYear() 
			|| current.getMonth() <= areaReserveFlight.endDate.getMonth()
			&& current.getYear() == areaReserveFlight.endDate.getYear()) {
		var value = ""+current.getFullYear()+ARBLib.zeroPad((current.getMonth()+1));
		var text = ""+areaReserveFlight.months[current.getMonth()]+" "+current.getFullYear();
		
		content += "<option value='"+value+"'"+(value == selected ? "selected='selected'" : "")+">"+text+"</option>";
		
		current.setMonth(current.getMonth()+1);
		if (maxLoop-- == 0) {
			break;
		}
	}
	
	content += "</select>";
	jQuery("#reserveFlightDepartMonthYearSelect").empty();
	jQuery("#reserveFlightDepartMonthYearSelect").append(content);
}
ARFLib.populateArriveDate = function () {
	var departStr = jQuery('#departDateField').attr("value");
	var departDate = new Date(getDateFromFormat(departStr, areaReserveFlight.dateFormat));
	var arriveStr = jQuery('#arriveDateField').attr("value");
	var arriveDate = new Date(getDateFromFormat(arriveStr, areaReserveFlight.dateFormat));
		
	var content = "";
	var start = 1;
	var selected = arriveDate.getDate();
	if (arriveDate.getFullYear() == departDate.getFullYear() && arriveDate.getMonth() == departDate.getMonth()) {
    // don't add minimumstay for flights
    if(this.currentState == 'hotelandflight') {
      start = departDate.getDate() + areaReserveFlight.minimumStay;
    }
    else {
      start = departDate.getDate();
    }
		if (selected < start) { 
			selected = start;
		}
	}
	
	content += "<select class='reserveFlight_selectBoxDate' onchange='ARFLib.updateArriveDate();'>";
	for (var i = start; i <= areaReserveFlight.monthDays[arriveDate.getMonth()]; i++) {
		content += "<option value='"+i+"'"+(selected == i ? "selected='selected'" : "")+">"+i+"</option>";
	}
	content += "</select>";
	
	jQuery("#reserveFlightArriveDateSelect").empty();
	jQuery("#reserveFlightArriveDateSelect").append(content);
}
ARFLib.populateArriveMonthYear = function () {
	var departStr = jQuery('#departDateField').attr("value");
	var departDate = new Date(getDateFromFormat(departStr, areaReserveFlight.dateFormat));
	var arriveStr = jQuery('#arriveDateField').attr("value");
	var arriveDate = new Date(getDateFromFormat(arriveStr, areaReserveFlight.dateFormat));
	var selected = ""+arriveDate.getFullYear()+ARBLib.zeroPad((arriveDate.getMonth()+1));
	
	var current = new Date(departDate);
  if(this.currentState == 'hotelandflight') {
    current.setDate(departDate.getDate()+1);
  }
  else {
    current.setDate(departDate.getDate());
  }
	current.setDate(1);
	
	var content = "";
	
	content += "<select class='reserveFlight_selectBoxMonthYear' onchange='ARFLib.updateArriveMonthYear();'>";
	
	var maxLoop = 60;
	while (current.getYear() < areaReserveFlight.endDate.getYear() 
			|| current.getMonth() <= areaReserveFlight.endDate.getMonth()
			&& current.getYear() == areaReserveFlight.endDate.getYear()) {
		var value = ""+current.getFullYear()+ARBLib.zeroPad((current.getMonth()+1));
		var text = ""+areaReserveFlight.months[current.getMonth()]+" "+current.getFullYear();
		
		content += "<option value='"+value+"'"+(value == selected ? "selected='selected'" : "")+">"+text+"</option>";
		
		current.setMonth(current.getMonth()+1);
		if (maxLoop-- == 0) {
			break;
		}
	}
	
	content += "</select>";

	jQuery("#reserveFlightArriveMonthYearSelect").empty();
	jQuery("#reserveFlightArriveMonthYearSelect").append(content);
}
ARFLib.populateRooms = function (initTrue) {
	var sel = jQuery("#reserveFlightRoomNum > option[selected]").attr("value");
	var i;
	
	var adults = [];
	var children = [];
	
	i = 0;
	jQuery("select.reserveFlight_selectBoxPersons[name^='reserveAdults']").each(function() {
		adults[i] = jQuery(this).find("option[selected]").attr("value"); 
		i++; 
	});
	i = 0;
	jQuery("select.reserveFlight_selectBoxPersons[name^='reserveChildren']").each(function() {
		children[i] = jQuery(this).find("option[selected]").attr("value"); 
		i++; 
	});
	
	var content = '';
	
	content += "<table cellpadding='0' border='0'><tbody>";
	content += "<tr><td></td><td class='reserveFlight_roomText'>Aikuisia</td><td class='reserveFlight_roomText'>Lapset (2-11 v.)</td></tr>";
	for (i = 1; i <= sel; i++) {
		content += "<tr>";
		content += "<td class='reserveFlight_roomText'>Huone "+i+"</td>";
		
		content += "<td><select class='reserveFlight_selectBoxPersons' name='reserveAdults"+i+"'>";
		if (i > 1) {
			content += "<option ";
			if (adults[i-1] == 0){content += "selected='selected' ";}
			content += "value='0'>0</option>";
		}
		content += "<option ";
		if (adults[i-1] == 1){content += "selected='selected' ";}
		content += "value='1'>1</option>";
		content += "<option ";
		if (adults[i-1] == 2 || initTrue){content += "selected='selected' ";}
		content += "value='2'>2</option>";
		content += "<option ";
		if (adults[i-1] == 3){content += "selected='selected' ";}
		content += "value='3'>3</option>";
		content += "<option ";
		if (adults[i-1] == 4){content += "selected='selected' ";}
		content += "value='4'>4</option>";
		content += '</select></td>';
		
		content += "<td><select class='reserveFlight_selectBoxPersons' name='reserveChildren"+i+"'>";
		content += "<option ";
		if (children[i-1] == 0){content += "selected='selected' ";}
		content += "value='0'>0</option>";
		content += "<option ";
		if (children[i-1] == 1){content += "selected='selected' ";}
		content += "value='1'>1</option>";
		content += "<option ";
		if (children[i-1] == 2){content += "selected='selected' ";}
		content += "value='2'>2</option>";
		content += "<option ";
		if (children[i-1] == 3){content += "selected='selected' ";}
		content += "value='3'>3</option>";
		content += '</select></td>';
		
		content += "</tr>";
	}
	
	jQuery("#reserveFlightRooms").empty();
	jQuery("#reserveFlightRooms").append(content);
}
ARFLib.updateDepartDate = function () {
	ARFLib.listbox2DepartDate();
	ARFLib.populateArriveMonthYear();
	ARFLib.listbox2ArriveDate();
	ARFLib.populateArriveDate();
}
ARFLib.updateDepartMonthYear = function () {
	ARFLib.listbox2DepartDate();
	ARFLib.populateDepartDate();
	ARFLib.populateArriveMonthYear();
	ARFLib.listbox2ArriveDate();
	ARFLib.populateArriveDate();
	ARFLib.listbox2DepartDate();
	ARFLib.listbox2ArriveDate();
}
ARFLib.updateArriveDate = function () {
	ARFLib.listbox2ArriveDate();
}
ARFLib.updateArriveMonthYear = function () {
	ARFLib.listbox2ArriveDate();
	ARFLib.populateArriveDate();
	ARFLib.listbox2ArriveDate();
}
ARFLib.populateDepartCalendar = function () {
	ARFLib.populateDepartDate();
	ARFLib.populateDepartMonthYear();
	ARFLib.populateArriveMonthYear();
	ARFLib.listbox2ArriveDate();
	ARFLib.populateArriveDate();
	ARFLib.listbox2DepartDate();
	ARFLib.listbox2ArriveDate();
}
ARFLib.populateArriveCalendar = function () {
	var departStr = jQuery('#departDateField').attr("value");
	var departDate = new Date(getDateFromFormat(departStr, areaReserveFlight.dateFormat));
	var arriveStr = jQuery('#arriveDateField').attr("value");
	var arriveDate = new Date(getDateFromFormat(arriveStr, areaReserveFlight.dateFormat));
	
	if (departDate.getTime() >= arriveDate.getTime()) {
		var newArriveDate = new Date(departDate.getFullYear(), departDate.getMonth(), departDate.getDate()+areaReserveFlight.minimumStay, 0, 0, 0);
		if (this.currentState == 'flight')
      newArriveDate = new Date(departDate.getFullYear(), departDate.getMonth(), departDate.getDate(), 0, 0, 0);
		jQuery('#arriveDateField').attr("value", formatDate(newArriveDate, areaReserveFlight.dateFormat));
	}
	
	ARFLib.populateArriveMonthYear();
	ARFLib.populateArriveDate();
}
ARFLib.listbox2DepartDate = function(formId) {
	var departBoxDate = jQuery("#reserveFlightDepartDateSelect > select > option[selected]").attr("value");
	var departBoxMonthYear = jQuery("#reserveFlightDepartMonthYearSelect > select > option[selected]").attr("value");

/*	if (departBoxDate > areaReserveFlight.monthDays[departBoxMonthYear.substring(4,6)-1]) {
    alert("departure date is greater than number of days in current month");
		departBoxDate = areaReserveFlight.monthDays[departBoxMonthYear.substring(4,6)-1];
	} 
*/	
	var departDate = new Date(0);
	departDate.setFullYear(departBoxMonthYear.substring(0,4), departBoxMonthYear.substring(4,6)-1, departBoxDate);

	jQuery('#departDateField').attr("value", formatDate(departDate, areaReserveFlight.dateFormat));
}
ARFLib.listbox2ArriveDate = function(formId) {
	var arriveBoxDate = jQuery("#reserveFlightArriveDateSelect > select > option[selected]").attr("value");
	var arriveBoxMonthYear = jQuery("#reserveFlightArriveMonthYearSelect > select > option[selected]").attr("value");
	
	if (arriveBoxDate > areaReserveFlight.monthDays[arriveBoxMonthYear.substring(4,6)-1]) {
		arriveBoxDate = areaReserveFlight.monthDays[arriveBoxMonthYear.substring(4,6)-1];
	}
	
	var arriveDate = new Date(0);
	arriveDate.setFullYear(arriveBoxMonthYear.substring(0,4), arriveBoxMonthYear.substring(4,6)-1, arriveBoxDate);
	
	jQuery('#arriveDateField').attr("value", formatDate(arriveDate, areaReserveFlight.dateFormat));
}
ARFLib.submit = function () {
	var form = jQuery("#reserveFlightForm");
	var genForm = jQuery("#reserveFlightGenerateForm");
	var error = false;
	
	var departCity="";
	var arriveCity="";
	
	if (this.currentState != "flight") {
    // departCity = genForm.find("select[name='reserveFlightDepartCity'] > option[selected]").attr("value");
    // arriveCity = genForm.find("select[name='reserveFlightArriveCity'] > option[selected]").attr("value");
    	departCity = jQuery("select[name='reserveFlightDepartCity'] > option[selected]").attr("value");
    	arriveCity = jQuery("select[name='reserveFlightArriveCity'] > option[selected]").attr("value");
	} else {
	  departCity = urlencode(jQuery("#reserveFlightDepartCityText").val());
	  arriveCity = urlencode(jQuery("#reserveFlightArriveCityText").val());
//	  departCity = jQuery("#reserveFlightDepartCityText").val();
//	  arriveCity = jQuery("#reserveFlightArriveCityText").val();
  } 
  
  if (!departCity) {
		jQuery("#reserveFlightDepartLocError").show();
		error = true;
	}
	else {
		jQuery("#reserveFlightDepartLocError").hide();
	}
	if (!arriveCity) {
		jQuery("#reserveFlightArriveLocError").show();
		error = true;
	}
	else {
		jQuery("#reserveFlightArriveLocError").hide();
	}
	
	if (error) {
		return;
	}
	
	var departBoxDate = jQuery("#reserveFlightDepartDateSelect > select > option[selected]").attr("value");
	var departBoxMonthYear = jQuery("#reserveFlightDepartMonthYearSelect > select > option[selected]").attr("value");
	var arriveBoxDate = jQuery("#reserveFlightArriveDateSelect > select > option[selected]").attr("value");
	var arriveBoxMonthYear = jQuery("#reserveFlightArriveMonthYearSelect > select > option[selected]").attr("value");
	
	var departDate = new Date(departBoxMonthYear.substring(0,4), departBoxMonthYear.substring(4,6)-1, departBoxDate, 0, 0, 0);
	var arriveDate = new Date(arriveBoxMonthYear.substring(0,4), arriveBoxMonthYear.substring(4,6)-1, arriveBoxDate, 0, 0, 0);
	
	form.find("*:input[name='B_DATE_1']").attr("value", ARBLib.getBDate(departDate));
	form.find("*:input[name='B_DATE_2']").attr("value", ARBLib.getBDate(arriveDate));
	
	
    var travellerId = 1;
    var rooms = jQuery("#reserveFlightRoomNum > option[selected]").attr("value");
    var i, j;
    var roomsContent = "";


    if (this.currentState != "flight") {
    
      ARFLib.setDepartLoc(departCity);
      ARFLib.setArriveLoc(arriveCity);
    
      for (i = 1; i <= rooms; i++) {
        var adults = genForm.find("*:input[name='reserveAdults"+i+"'] > option[selected]").attr("value");
        var children = genForm.find("*:input[name='reserveChildren"+i+"'] > option[selected]").attr("value");
        
        var count = 1;
        
        for (j = 0; j < adults; j++) {
          roomsContent += "<input type='hidden' name='TRAVELLER_TYPE_"+travellerId+"' value='ADT' />";
          roomsContent += "<input type='hidden' name='HOTEL_1_ROOM_"+i+"_TRAVELLER_"+count+"_ID' value='"+travellerId+"' />";
          travellerId++;
          count++;
        }
        
        for (j = 0; j < children; j++) {
          roomsContent += "<input type='hidden' name='TRAVELLER_TYPE_"+travellerId+"' value='CHD' />";
          roomsContent += "<input type='hidden' name='HOTEL_1_ROOM_"+i+"_TRAVELLER_"+count+"_ID' value='"+travellerId+"' />";
          travellerId++;
          count++;
        }
       }
    } else {
      roomsContent += "<input value='MP' name='SEARCH_PAGE' type='hidden'>";
      roomsContent += "<input value='STANDARD_AIR' name='PRODUCT_TYPE_1' type='hidden'>";
      roomsContent += "<input value='"+jQuery("#tripTypeRadios input:checked").attr("value")+"' name='TRIP_TYPE' type='hidden'>";
      roomsContent += "<input value='"+jQuery("#tripTypeRadios input:checked").attr("value")+"' name='tripType' type='hidden'>";
      roomsContent += "<input value='E' name='AIR_CABIN' type='hidden'>";
      roomsContent += "<input value='' name='SEVEN_DAY_SEARCH' type='hidden'>";
      roomsContent += "<input value='YES' name='TRIP_FLOW' type='hidden'>";
      roomsContent += "<input value='' name='EXTERNAL_ID' type='hidden'>";
      roomsContent += "<input value='1' name='DIRECT_LOGIN' type='hidden'>";
      roomsContent += "<input value='MPSEARCH' name='PLTG_FROMPAGE' type='hidden'>";
      roomsContent += "<input name='AIR_MAX_CONNECTIONS' value='1' type='hidden'>";
      roomsContent += "<input name='FIELD_YTH_NUMBER' value='0' type='hidden'>";      
      roomsContent += "<input name='FIELD_YCD_NUMBER' value='0' type='hidden'>";      
      roomsContent += "<input name='FIELD_INFANTS_NUMBER' value='0' type='hidden'>";      
      roomsContent += "<input name='TRAVELLER_TYPE_1' value='ADT' type='hidden'>";
      roomsContent += "<input name='TRAVELLER_TYPE_2' value='ADT' type='hidden'>";
      roomsContent += "<input name='HAS_INFANT_1' value='FALSE' type='hidden'>";
      roomsContent += "<input name='HAS_INFANT_2' value='FALSE' type='hidden'>";    
      roomsContent += "<input name='DIRECT_NON_STOP' value='false' type='hidden'>";
      roomsContent += "<input name='B_ANY_TIME_1' value='TRUE' type='hidden'>";
      roomsContent += "<input name='B_ANY_TIME_2' value='TRUE' type='hidden'>";
      roomsContent += "<input name='E_SEARCH_RANGE_1' value='1' type='hidden'>";
      roomsContent += "<input name='B_SEARCH_RANGE_1' value='1' type='hidden'>";
      roomsContent += "<input name='FIELD_ADT_NUMBER' value='"+jQuery("#ADT_QTY > option[selected]").attr("value")+"' type='hidden'>";
      roomsContent += "<input name='FIELD_CHD_NUMBER' value='"+jQuery("#CHD_QTY > option[selected]").attr("value")+"' type='hidden'>";      
      jQuery("#B_LOCATION_1").val(departCity);
      jQuery("#E_LOCATION_1").val(arriveCity);
    }

    form.append(roomsContent);
	
//	var text="URL="+form.attr("action")+"\n";form.find("*:input").each(function(){text+=this.name+"="+this.value+"\n";});
//	alert(text);
	
	var submit_url=form.attr("action")+"?";form.find("*:input").each(function(){submit_url+=this.name+"="+this.value+"&";});
//	alert(submit_url);
	
	window.location.href=submit_url;
//	jQuery("#reserveFlightForm").submit();
}

	function urlencode(str) {
    str = escape(str);
    str = str.replace('+', '%2B');
    str = str.replace('%20', '+');
    str = str.replace('*', '%2A');
    str = str.replace('/', '%2F');
    str = str.replace('@', '%40');
    str = str.replace('å', '%E5');
    str = str.replace('ä', '%E4');
    str = str.replace('ö', '%F6');
//    str = str.replace('Å', '%C3%85');
//    str = str.replace('Ä', '%C3%84');
//    str = str.replace('Ö', '%C3%96');
    return str;
  }

ARFcalendarDepartFlightHotel = new CalendarPopup("areaReserveFlightCalendar1");
ARFcalendarArriveFlightHotel = new CalendarPopup("areaReserveFlightCalendar2");

ARFLib.departCalendarFlightHotel=function() {ARFcalendarDepartFlightHotel.select(jQuery('#departDateField').get(0),"reserveFlightHotelDepartCalendar","yyyy-MM-dd");return false;}
ARFLib.arriveCalendarFlightHotel=function() {ARFcalendarArriveFlightHotel.select(jQuery('#arriveDateField').get(0),"reserveFlightHotelArriveCalendar","yyyy-MM-dd");return false;}

jQuery(document).ready(function() {
	if (jQuery("#areaReserveFlight").length > 0) {
		var depDate = formatDate(areaReserveFlight.startDate, areaReserveFlight.dateFormat);
		var arrDateTmp = new Date(areaReserveFlight.startDate);
		arrDateTmp.setDate(arrDateTmp.getDate() + areaReserveFlight.minimumStay);
    
		var arrDate = formatDate(arrDateTmp, areaReserveFlight.dateFormat);
		
		var depCalStart = new Date(areaReserveFlight.startDate);
		depCalStart.setDate(depCalStart.getDate() - 1);
		var depCalEnd = new Date(areaReserveFlight.endDate);
		depCalEnd.setDate(depCalEnd.getDate() + 1 - areaReserveFlight.minimumStay);
		var arrCalStart = new Date(areaReserveFlight.startDate);
		arrCalStart.setDate(arrCalStart.getDate() - 1 + areaReserveFlight.minimumStay);
		var arrCalEnd = new Date(areaReserveFlight.endDate);
		arrCalEnd.setDate(arrCalEnd.getDate() + 1);
		
		ARFcalendarDepartFlightHotel.addDisabledDates(null, formatDate(depCalStart,areaReserveFlight.dateFormat));
		ARFcalendarDepartFlightHotel.addDisabledDates(formatDate(depCalEnd,areaReserveFlight.dateFormat), null);
		ARFcalendarArriveFlightHotel.addDisabledDates(null, formatDate(arrCalStart,areaReserveFlight.dateFormat));
		ARFcalendarArriveFlightHotel.addDisabledDates(formatDate(arrCalEnd,areaReserveFlight.dateFormat), null);
		
		ARFcalendarDepartFlightHotel.setTodayText("");
		ARFcalendarDepartFlightHotel.setWeekStartDay(areaReserveFlight.weekStartDay);
		ARFcalendarDepartFlightHotel.setMonthNames(areaReserveFlight.months[0],areaReserveFlight.months[1],areaReserveFlight.months[2],areaReserveFlight.months[3],areaReserveFlight.months[4],areaReserveFlight.months[5],areaReserveFlight.months[6],areaReserveFlight.months[7],areaReserveFlight.months[8],areaReserveFlight.months[9],areaReserveFlight.months[10],areaReserveFlight.months[11]);
		ARFcalendarDepartFlightHotel.setDayHeaders(areaReserveFlight.dayHeaders[0],areaReserveFlight.dayHeaders[1],areaReserveFlight.dayHeaders[2],areaReserveFlight.dayHeaders[3],areaReserveFlight.dayHeaders[4],areaReserveFlight.dayHeaders[5],areaReserveFlight.dayHeaders[6]);
		ARFcalendarDepartFlightHotel.setCssPrefix("ARF");
		
		ARFcalendarArriveFlightHotel.setTodayText("");
		ARFcalendarArriveFlightHotel.setWeekStartDay(areaReserveFlight.weekStartDay);
		ARFcalendarArriveFlightHotel.setMonthNames(areaReserveFlight.months[0],areaReserveFlight.months[1],areaReserveFlight.months[2],areaReserveFlight.months[3],areaReserveFlight.months[4],areaReserveFlight.months[5],areaReserveFlight.months[6],areaReserveFlight.months[7],areaReserveFlight.months[8],areaReserveFlight.months[9],areaReserveFlight.months[10],areaReserveFlight.months[11]);
		ARFcalendarArriveFlightHotel.setDayHeaders(areaReserveFlight.dayHeaders[0],areaReserveFlight.dayHeaders[1],areaReserveFlight.dayHeaders[2],areaReserveFlight.dayHeaders[3],areaReserveFlight.dayHeaders[4],areaReserveFlight.dayHeaders[5],areaReserveFlight.dayHeaders[6]);
		ARFcalendarArriveFlightHotel.setCssPrefix("ARF");
		
		var content = "";
    
		content +="<div class='reserveFlight_text'>";
		content +="<h3><strong>Vertaile hintoja & varaa</strong></h3>";
		content +="<form action='#' method='get'>";
		content +="<input class='reserveFlightRadio' type='radio' checked='checked' id='hotelPackageButton'  name='search' value='hotelPackageButton' onClick=\"ARFLib.toggleMode('hotelandflight'); \" />";
		content +="<label for='hotelPackageButton'>Lento + Hotelli</label> <br/>";
		content +="<input class='reserveFlightRadio' type='radio' id='flightButton'  name='search' value='flightButton' onClick=\"ARFLib.toggleMode('flight')\" />";    
		content +="<label for='flightButton'>Lennot</label> <br/>";
		content +="<input class='reserveFlightRadio' type='radio' id='insuranceButton'  name='search' value='insuranceButton' onClick=\"document.location.href='/vakuutukset/'\" />";
		content +="<label for='insuranceButton'>Vakuutukset</label>";
		content +="<br/>";
		content +="<div class='reserveRadioWrapper'>";
		content +="<div class='reserveRadioLeftEntity'><input class='reserveFlightRadio' type='radio' id='hotelButton'  name='search' value='hotelButton' onClick=\"document.location.href='/hotellit/'\" />";
		content +="<label for='hotelButton'>Hotellit</label><br/>";
		content +="<input class='reserveFlightRadio' type='radio' id='carButton'  name='search' value='carButton' onClick=\"document.location.href='/autot/'\" />";
		content +="<label for='carButton'>Autot</label></div>";
		content +="<div class='reserveRadioRightEntity'><input class='reserveFlightRadio' type='radio' id='cottageButton'  name='search' value='cottageButton' onClick=\"document.location.href='/loma-asunnot/'\" />";
		content +="<label for='cottageButton'>Loma-asunnot</label> ";
		content +="<br/>";
		content +="<input class='reserveFlightRadio' type='radio' id='shipButton'  name='search' value='shipButton' onClick=\"document.location.href='/laivamatkat/'\" />";
		content +="<label for='shipButton'>Laivat</label></div>";
		content +="</div>";
		content +="</form>";
		content +="</div>";		
		
		// IE 6 needs to append the content here more often.
		jQuery("#areaReserveFlight").append(content);	
		content="";
		
		content +="<form enctype='text/plain' id='reserveFlightForm' name='reserveFlightForm' method='GET' action='"+areaReserveFlight.url+"'>";
		content +="<input type='hidden' name='info_clicked' id='info_clicked' value='' />";
		content +="<input type='hidden' name='B_DATE_1' value='' />";
		content +="<input type='hidden' name='B_DATE_2' value='' />";
		content +="<input type='hidden' id='B_LOCATION_1' name='B_LOCATION_1' value='' />";
		content +="<input type='hidden' id='E_LOCATION_1' name='E_LOCATION_1' value='' />";
		content +="<input type='hidden' name='B_LOCATION_2' value='' />";
		content +="<input type='hidden' name='E_LOCATION_2' value='' />";
		content +="<input type='hidden' name='LANGUAGE' value='"+areaReserveFlight.language+"' />";
		content +="<input type='hidden' id='amadeusSite' name='SITE' value='"+areaReserveFlight.site+"' />";
		content +="</form>";
		
		content +="<form id='reserveFlightGenerateForm'>";
		content +="<input type='hidden' name='departDate' id='departDateField' value='"+depDate+"' />";
		content +="<input type='hidden' name='arriveDate' id='arriveDateField' value='"+arrDate+"' />";

		content +="<div class='reserveFlight_mainContent'>";
		content +="<div class='reserveFlight_text'>Mist&auml;:</div>";
		content +="<div id='reserveFlightDepartLocError' class='reserveFlight_error'>"+areaReserveFlight.departCityError+"</div>";
		
		content +="<div id='reserveFlightDepartLoc'></div>";
		content +="<div id='reserveFlightDepartLocText'></div>";
		
		content +="<div class='reserveFlight_text'>Mihin:</div>";
		content +="<div id='reserveFlightArriveLocError' class='reserveFlight_error'>"+areaReserveFlight.arriveCityError+"</div>";
		
		content +="<div id='reserveFlightArriveLoc' style='width:100%;overflow:hidden'></div>";
		content +="<div id='reserveFlightArriveLocText'></div>";
		
		content +="<div id='flightDirections'></div>";
	
		content +="<div class='reserveFlight_text'>L&auml;ht&ouml;:</div>";
		content +="<div id='reserveFlightDepartDate'>";
		content +="<a href='#' id='reserveFlightHotelDepartCalendar' class='reserveFlight_calendar' onclick='return ARFLib.departCalendarFlightHotel();'>"+areaReserveFlight.calendarHtml+"</a>";
		content +="<span id='reserveFlightDepartDateSelect'></span>";
		content +="<span id='reserveFlightDepartMonthYearSelect'></span>";
		content +="</div>";
		content +="<div class='reserveFlight_text'>Paluu:</div>";
		content +="<div id='reserveFlightArriveDate'>";
		content +="<a href='#' id='reserveFlightHotelArriveCalendar' class='reserveFlight_calendar' onclick='return ARFLib.arriveCalendarFlightHotel();'>"+areaReserveFlight.calendarHtml+"</a>";
		content +="<span id='reserveFlightArriveDateSelect'></span>";
		content +="<span id='reserveFlightArriveMonthYearSelect'></span>";
		content +="</div>";
		content +="<span id='bookRooms'>";
		content +="<div class='reserveFlight_text'>Huoneiden lukum&auml;&auml;r&auml;:</div>";
		content +="<select name='reserveFlightRoomNum' id='reserveFlightRoomNum' class='reserveFlight_selectBoxRooms' onChange='ARFLib.populateRooms(false);'>";
		content +="<option value='1'"+(areaReserveFlight.rooms == 1 ? " selected='selected'" : "" )+">1</option>";
		content +="<option value='2'"+(areaReserveFlight.rooms == 2 ? " selected='selected'" : "" )+">2</option>";
		content +="<option value='3'"+(areaReserveFlight.rooms == 3 ? " selected='selected'" : "" )+">3</option>";
		content +="</select>";
		content +="<div id='reserveFlightRooms' class='reserveFlight_roomList'></div>";
		content +="</span>";
		content +="<span id='passengerQTY'>";
		content +="<div class='reserveFlight_roomText'></div>";
		content +="<div id='reserveFlightPassengers' class='reserveFlight_passengerList'>";
		content +="		<table cellpadding='0' border='0'><tbody>";
		content +="		<tr><td class='reserveFlight_roomText'>Aikuisia</td><td class='reserveFlight_roomTextChildren'>Lapset (2-11v.) </td></tr>";
		content +="		<tr>";
		content +="      ";
		content +="      <td>";
		content +="        <select class='reserveFlight_selectBoxPersons' name='ADT_QTY' id='ADT_QTY'>";
		content +="        <option value='0'>0</option>";
		content +="        <option selected='selected' value='1'>1</option>";
		content +="        <option value='2'>2</option>";
		content +="        <option value='3'>3</option>";
		content +="        <option value='4'>4</option>";
		content +="        <option value='5' style='color: rgb(102, 102, 102);'>5</option>";
		content +="        <option value='6' style='color: rgb(102, 102, 102);'>6</option>";
		content +="        <option value='7' style='color: rgb(102, 102, 102);'>7</option>";
		content +="        <option value='8' style='color: rgb(102, 102, 102);'>8</option>";
		content +="        <option value='9' style='color: rgb(102, 102, 102);'>9</option>";
		content +="      </select>";
		content +="      </td>";
		content +="      <td>";
		content +="        <select class='reserveFlight_selectBoxPersons' name='CHD_QTY' id='CHD_QTY'>";
		content +="        <option selected='selected' value='0'>0</option>";
		content +="        <option value='1'>1</option>";
		content +="        <option value='2'>2</option>";
		content +="        <option value='3' style='color: rgb(102, 102, 102);'>3</option>";
		content +="        <option value='4' style='color: rgb(102, 102, 102);'>4</option>";
		content +="        <option value='5' style='color: rgb(102, 102, 102);'>5</option>";
		content +="        <option value='6' style='color: rgb(102, 102, 102);'>6</option>";
		content +="        <option value='7' style='color: rgb(102, 102, 102);'>7</option>";
		content +="        <option value='8' style='color: rgb(102, 102, 102);'>8</option>";
		content +="        <option value='9' style='color: rgb(102, 102, 102);'>9</option>";
		content +="      </select>";
		content +="      </td>";
		content +="		</tr></tbody></table>";
		content +="		</div>";
		content +="</span>";
		content +="<div id='reserveFlightHotel_submit' class='reserveFlightHotel_submit'><a href='#' onclick='ARFLib.submit(); return false;'>"+areaReserveFlight.submit+"</a></div>";
		content +="<div id='reserveFlight_submit' class='reserveFlight_submit'><a href='#' onclick='ARFLib.submit(); return false;'>"+areaReserveFlight.submit+"</a></div>";
		content +="<div class='reserveFlight_more' id='reserveFlight_morexx'>";
		content +="<a title='Laajennettu haku' id='linkToSearch' href='http://wftc2-b2c.amadeus.com/plnext/INareaB2C/CleanUpSessionPui.action?SITE=BAULBAUL&LANGUAGE=FI'>* Lis&auml;&auml; hakuvaihtoehtoja &raquo;</a>";
		content +="</div>";		
		
		content +="<div>";
		content +="<a href='http://fusion.google.com/add?moduleurl=http%3A//www.area.fi/area/google/ggadget.xml'>";
		content +="</a>";
		content +="</div>";		
		
		content +="</div>";
		content +="</form>";
		
		jQuery("#areaReserveFlight").append(content);		
		
		ARFLib.populateRooms(true);
		ARFLib.populateDepartDate();
		ARFLib.populateDepartMonthYear();
		ARFLib.populateArriveDate(true);
		ARFLib.populateArriveMonthYear();
		
		ARFLib.populateDepartCities();
		ARFLib.populateArriveCities();
		
		ARFLib.prepareDepartCities();
		ARFLib.prepareArriveCities();
		ARFLib.prepareflightDirections();
		
		// to avoid both 'hotel' and 'hotel and flight' fields to show initially
		ARFLib.toggleMode("hotelandflight");

	}jQuery
});