function AmadeusBookingWindow(winId, timeRestId){
	this.windowElementId = winId;
	this.timeRestNoteId = timeRestId;
	
	this.cssDisplay = "none";
	this.cssPosition = "absolute";
	this.cssLeftPx = 0;
	this.cssTopPx = 0;
	
	this.timeRestriction = null;
	this.timeRestCssDisplay = "none";
	this.timeRestRelLeftPx = -197;
	this.timeRestRelTopPx = 55;

	
	
	
	this.displayWindow = function(winPosX, winPosY){
		this.cssDisplay = "block";
		this.cssLeftPx = winPosX;
		this.cssTopPx = winPosY;
		
		this.updateWindow();
	}
	
	
	this.hideWindow = function(){
		document.getElementById(this.windowElementId).style.display = "none";
		document.getElementById(this.timeRestNoteId).style.display = "none";
	}
	
	
	this.updateWindow = function(){		
		var windowElement = document.getElementById(this.windowElementId);
		var timeRestNote = document.getElementById(this.timeRestNoteId);
		
		windowElement.style.display = this.cssDisplay;
		windowElement.style.position = this.cssPosition;
		windowElement.style.left = this.cssLeftPx + "px";
		windowElement.style.top = this.cssTopPx + "px";
		
		if(this.timeRestriction != null){
			timeRestNote.style.left = this.cssLeftPx + this.timeRestRelLeftPx + "px";
			timeRestNote.style.top = this.cssTopPx + this.timeRestRelTopPx + "px";
			this.timeRestCssDisplay = "block";
		}
		else{
			this.timeRestCssDisplay = "none";
		}
		
		timeRestNote.style.display = this.timeRestCssDisplay;
	}
	
	this.setTimeRestriction = function(begDate, endDate){
		var timeRestNote = document.getElementById(this.timeRestNoteId);
		
		this.timeRestriction = new Array(begDate, endDate);
		var spanEmts = timeRestNote.getElementsByTagName("SPAN");
		var timeRestStr = begDate.substr(6,2) + "." + begDate.substr(4,2) + "." + begDate.substr(0,4);
		timeRestStr += " - ";
		timeRestStr += endDate.substr(6,2) + "." + endDate.substr(4,2) + "." + endDate.substr(0,4);
		
		spanEmts[0].innerHTML = timeRestStr;
	}
	
	this.removeTimeRestriction = function(){
		this.timeRestriction = null;
	}
	
	this.getTimeRestrictionStartAsDate = function(){
		if(this.timeRestriction != null){
			var startDate = new Date();
			var year = parseInt(this.timeRestriction[0].substr(0,4));
			var month = parseInt((this.timeRestriction[0].substr(4,2).charAt(0) == '0') ? this.timeRestriction[0].substr(5,1) : this.timeRestriction[0].substr(4,2));
			var date = parseInt((this.timeRestriction[0].substr(6,2).charAt(0) == '0') ? this.timeRestriction[0].substr(7,1) : this.timeRestriction[0].substr(6,2));
		
			startDate.setFullYear(year, month, date);
			startDate.setHours(0);
			startDate.setMinutes(0);
			
			return startDate;
		}
		else{
			return null;
		}
	}
	
	this.getTimeRestrictionEndAsDate = function(){
		if(this.timeRestriction != null){
			var endDate = new Date();
			var year = parseInt(this.timeRestriction[1].substr(0,4));
			var month = parseInt((this.timeRestriction[1].substr(4,2).charAt(0) == '0') ? this.timeRestriction[1].substr(5,1) : this.timeRestriction[1].substr(4,2));
			var date = parseInt((this.timeRestriction[1].substr(6,2).charAt(0) == '0') ? this.timeRestriction[1].substr(7,1) : this.timeRestriction[1].substr(6,2));
		
			endDate.setFullYear(year, month, date);
			endDate.setHours(23);
			endDate.setMinutes(59);
			
			return endDate;
		}
		else{
			return null;
		}
	}
	
	
	this.updateWindow();
}




var amaWin = null;
var packageFormId = "amadeusBooker";
var packageForm = null;

var createdElements = new Array();


function displayBookerWindow(emt, timeRestBeg, timeRestEnd, from, to){
	if(packageForm == null){
		packageForm = document.getElementById(packageFormId);
	}
	
	packageForm.B_LOCATION_1.value = from;
	packageForm.E_LOCATION_1.value = to;
	packageForm.B_LOCATION_2.value = to;
	packageForm.E_LOCATION_2.value = from;
	
	amaWin = new AmadeusBookingWindow("amaWin", "amaTimeRestriction");
	
	var posArr = findPos(emt);
	var posLeft = posArr[0];
	var posTop = posArr[1];
	
	if(timeRestBeg.length == 8 && timeRestEnd.length == 8){
		amaWin.setTimeRestriction(timeRestBeg, timeRestEnd);
		var begDate = amaWin.getTimeRestrictionStartAsDate();
		
		var deptDateOptions = document.getElementById('L_PV').getElementsByTagName('OPTION');
		var deptMonthOptions = document.getElementById('L_KK').getElementsByTagName('OPTION');
		var deptYearOptions = document.getElementById('L_VV').getElementsByTagName('OPTION');
		
		for(var i = 0; i < deptDateOptions.length; i++){
			if(deptDateOptions[i].value == begDate.getDate()){
				deptDateOptions[i].selected = true;
			}
			else{
				deptDateOptions[i].selected = false;
			}
		}
		
		for(var i = 0; i < deptMonthOptions.length; i++){
			if(deptMonthOptions[i].value == begDate.getMonth()){
				deptMonthOptions[i].selected = true;
			}
			else{
				deptMonthOptions[i].selected = false;
			}
		}
		
		for(var i = 0; i < deptYearOptions.length; i++){
			if(deptYearOptions[i].value == getFixedYear(begDate)){
				deptYearOptions[i].selected = true;
			}
			else{
				deptYearOptions[i].selected = false;
			}
		}
		
		setBDate1();
	}
	else{
		amaWin.removeTimeRestriction();
	}
	
	amaWin.displayWindow(posLeft - 10, posTop - 5);
}

function hideBookerWindow(){
	amaWin.hideWindow();
}





function setBDate1(){
	if(packageForm == null){
		packageForm = document.getElementById(packageFormId);
	}
	
	var selects = packageForm.getElementsByTagName("select");
	var lkk = null;
	var lpv = null;
	var lvv = null;
	
	var tpv = null;
	var tkk = null;
	var tvv = null;
	
	for(var i in selects){
		if(selects[i].name == "L_KK")
			lkk = (typeof(selects[i].options[selects[i].selectedIndex]) != 'undefined') ? selects[i].options[selects[i].selectedIndex].value : selects[i].options[0].value;
		if(selects[i].name == "L_PV")
			lpv = (typeof(selects[i].options[selects[i].selectedIndex]) != 'undefined') ? selects[i].options[selects[i].selectedIndex].value : selects[i].options[0].value;
		if(selects[i].name == "L_VV")
			lvv = (typeof(selects[i].options[selects[i].selectedIndex]) != 'undefined') ? selects[i].options[selects[i].selectedIndex].value : selects[i].options[0].value;
		if(selects[i].name == "T_PV")
			tpv = selects[i];
		if(selects[i].name == "T_KK")
			tkk = selects[i];
		if(selects[i].name == "T_VV")
			tvv = selects[i];
	}
	/*
	var lkk = packageForm.L_KK.options[packageForm.L_KK.selectedIndex].value;
	var lpv = packageForm.L_PV.options[packageForm.L_PV.selectedIndex].value;
	var lvv = packageForm.L_VV.options[packageForm.L_VV.selectedIndex].value;
	*/
	var strDate = (parseInt(lpv) < 10) ? '0' + lpv : '' + lpv;
	var strMonth = (parseInt(lkk) < 10) ? '0' + lkk : '' + lkk;
	
	packageForm.B_DATE_1.value = lvv + '' + strMonth + '' + strDate + '0000';
	
	var bDate2 = new Date();
	
	bDate2.setFullYear(parseInt(lvv), parseInt(lkk) - 1, parseInt(lpv) + 2);
	bDate2.setHours(0);
	bDate2.setMinutes(0);
	
	for(var i = 0; i < tpv.options.length; i++){
		if(tpv.options[i].value == bDate2.getDate()){
			tpv.options[i].selected = true;
		}
		else{
			tpv.options[i].selected = false;
		}
	}
	
	for(var i = 0; i < tkk.options.length; i++){
		if(tkk.options[i].value == bDate2.getMonth() + 1){
			tkk.options[i].selected = true;
		}
		else{
			tkk.options[i].selected = false;
		}
	}
	
	for(var i = 0; i < tvv.options.length; i++){
		if(tvv.options[i].value == getFixedYear(bDate2)){
			tvv.options[i].selected = true;
		}
		else{
			tvv.options[i].selected = false;
		}
	}
	
	strDate = (bDate2.getDate() < 10) ? '0' + bDate2.getDate() : '' + bDate2.getDate();
	strMonth = (bDate2.getMonth() + 1 < 10) ? '0' + (bDate2.getMonth() + 1) : '' + (bDate2.getMonth() + 1);
	
	packageForm.B_DATE_2.value = getFixedYear(bDate2) + '' + strMonth + '' + strDate + '0000';
	
	//alert(packageForm.B_DATE_1.value + '\n' + packageForm.B_DATE_2.value);
}






function setBDate2(formId){
	if(packageForm == null){
		packageForm = document.getElementById(packageFormId);
	}
	
	var selects = packageForm.getElementsByTagName("select");
	
	var tpv = null;
	var tkk = null;
	var tvv = null;
	
	for(var i in selects){
		if(selects[i].name == "T_PV")
			tpv = (selects[i].options[selects[i].selectedIndex].value.length < 2) ? '0' + selects[i].options[selects[i].selectedIndex].value : selects[i].options[selects[i].selectedIndex].value;
		if(selects[i].name == "T_KK")
			tkk = (selects[i].options[selects[i].selectedIndex].value.length < 2) ? '0' + selects[i].options[selects[i].selectedIndex].value : selects[i].options[selects[i].selectedIndex].value;
		if(selects[i].name == "T_VV")
			tvv = selects[i].options[selects[i].selectedIndex].value;
	}
	/*
	var tpv = (packageForm.T_PV.options[packageForm.T_PV.selectedIndex].value.length < 2) ? '0' + packageForm.T_PV.options[packageForm.T_PV.selectedIndex].value : packageForm.T_PV.options[packageForm.T_PV.selectedIndex].value;
	var tkk = (packageForm.T_KK.options[packageForm.T_KK.selectedIndex].value.length < 2) ? '0' + packageForm.T_KK.options[packageForm.T_KK.selectedIndex].value : packageForm.T_KK.options[packageForm.T_KK.selectedIndex].value;
	var tvv = packageForm.T_VV.options[packageForm.T_VV.selectedIndex].value;
	*/
	packageForm.B_DATE_2.value = tvv + '' + tkk + '' + tpv + '0000';
}




function updateRooms(selectEmt){
	var roomCount = parseInt(selectEmt.options[selectEmt.selectedIndex].value);
	var roomRows = document.getElementById("roomLayout").getElementsByTagName('TR');
	
	for(var i = 1; i < roomRows.length; i++){
		if(i <= roomCount){
			roomRows[i].className = "visibleRoom";
		}
		else{
			roomRows[i].className = "hiddenRoom";
		}
	}
}




function buildBooker(){
	if(packageForm == null){
		packageForm = document.getElementById(packageFormId);
	}
	
	var restrictedStart = amaWin.getTimeRestrictionStartAsDate();
	var restrictedEnd = amaWin.getTimeRestrictionEndAsDate();
	
	if(restrictedStart != null && restrictedEnd != null){
		var currentStart = new Date();
		var currentEnd = new Date();
		
		
		var selects = packageForm.getElementsByTagName("select");
	
		var lpv = null;
		var lkk = null;
		var lvv = null;
		var tpv = null;
		var tkk = null;
		var tvv = null;
		
		for(var i in selects){
			if(selects[i].name == "L_PV")
				lpv = selects[i];
			if(selects[i].name == "L_KK")
				lkk = selects[i];
			if(selects[i].name == "L_VV")
				lvv = selects[i];
			if(selects[i].name == "T_PV")
				tpv = selects[i];
			if(selects[i].name == "T_KK")
				tkk = selects[i];
			if(selects[i].name == "T_VV")
				tvv = selects[i];
		}
		
		
		currentStart.setFullYear(parseInt(lvv.options[lvv.selectedIndex].value), parseInt(lkk.options[lkk.selectedIndex].value), parseInt(lpv.options[lpv.selectedIndex].value));
		currentStart.setHours(23);
		currentStart.setMinutes(59);
		
		currentEnd.setFullYear(parseInt(tvv.options[tvv.selectedIndex].value), parseInt(tkk.options[tkk.selectedIndex].value), parseInt(tpv.options[tpv.selectedIndex].value));
		currentEnd.setHours(0);
		currentEnd.setMinutes(0);
		
		if(currentStart < restrictedStart || currentEnd > restrictedEnd){
			alert("Valitse lähtö- ja paluupäivämäärät ilmoitetulta aikaväliltä.");
			return false;
		}
	}

	
	updateRoomLayout();
	return true;
}



function initBDates(formEmtId){
	if(packageForm == null){
		packageForm = document.getElementById(packageFormId);
	}
	
	var selects = packageForm.getElementsByTagName("select");
	
	var slpv = null;
	var slkk = null;
	var slvv = null;
	var stpv = null;
	var stkk = null;
	var stvv = null;
	
	for(var i in selects){
		if(selects[i].name == "L_PV")
			slpv = selects[i];
		if(selects[i].name == "L_KK")
			slkk = selects[i];
		if(selects[i].name == "L_VV")
			slvv = selects[i];
		if(selects[i].name == "T_PV")
			stpv = selects[i];
		if(selects[i].name == "T_KK")
			stkk = selects[i];
		if(selects[i].name == "T_VV")
			stvv = selects[i];
	}
	
	var lpv = (slpv.options[slpv.selectedIndex].value.length < 2) ? '0' + slpv.options[slpv.selectedIndex].value : slpv.options[slpv.selectedIndex].value;
	var lkk = (slkk.options[slkk.selectedIndex].value.length < 2) ? '0' + slkk.options[slkk.selectedIndex].value : slkk.options[slkk.selectedIndex].value;
	var lvv = slvv.options[slvv.selectedIndex].value;
	
	packageForm.B_DATE_1.value = slvv + '' + slkk + '' + slpv + '0000';
	
	var tpv = (stpv.options[stpv.selectedIndex].value.length < 2) ? '0' + stpv.options[stpv.selectedIndex].value : stpv.options[stpv.selectedIndex].value;
	var tkk = (stkk.options[stkk.selectedIndex].value.length < 2) ? '0' + stkk.options[stkk.selectedIndex].value : stkk.options[stkk.selectedIndex].value;
	var tvv = stvv.options[stvv.selectedIndex].value;
	
	packageForm.B_DATE_2.value = tvv + '' + tkk + '' + tpv + '0000';
}




function updateRoomLayout(){
	if(packageForm == null){
		packageForm = document.getElementById(packageFormId);
	}
	
	var roomCount = parseInt(packageForm.roomCount.options[packageForm.roomCount.selectedIndex].value);
	var roomRows = document.getElementById("roomLayout").getElementsByTagName('TR');
	var totalTravellers = 0;
	
	if(createdElements.length > 0){
		for(var i = 0; i < createdElements.length; i++){
			packageForm.removeChild(createdElements[i]);
		}
	}
	
	createdElements = new Array();
	
	for(var i = 1; i <= roomCount; i++){
		var travellerType = "ADT";
		var selectEmts = document.getElementById("roomLayout").getElementsByTagName('SELECT');
		var selectedAdults = parseInt(selectEmts[(i - 1) * 2].options[selectEmts[(i - 1) * 2].selectedIndex].value);
		var selectedChildren = parseInt(selectEmts[(i - 1) * 2 + 1].options[selectEmts[(i - 1) * 2 + 1].selectedIndex].value);
		var roomTraveller = 0;
		
		for(var t = 1; t <= selectedAdults; t++){
			totalTravellers++;
			roomTraveller++;
			addTravellerToRoom(totalTravellers, travellerType, roomTraveller, i);
		}
		
		travellerType = "CHD";
		
		for(var t = 1; t <= selectedChildren; t++){
			totalTravellers++;
			roomTraveller++;
			addTravellerToRoom(totalTravellers, travellerType, roomTraveller, i);
		}
	}
}


function addTravellerToRoom(travellerId, travellerType, roomTraveller, room){
	if(packageForm == null){
		packageForm = document.getElementById(packageFormId);
	}
	
	var strTravellerType = 'TRAVELLER_TYPE_' + travellerId;
	var strRoomMapping = 'HOTEL_1_ROOM_' + room + '_TRAVELLER_' + roomTraveller + '_ID';
	
	var formInputs = packageForm.getElementsByTagName('INPUT');
	
	var travellerTypeEmt = document.createElement('input');
	travellerTypeEmt.type = "hidden";
	travellerTypeEmt.name = strTravellerType;
	travellerTypeEmt.value = travellerType;
	
	var roomMappingEmt = document.createElement('input');
	roomMappingEmt.type = "hidden";
	roomMappingEmt.name = strRoomMapping;
	roomMappingEmt.value = travellerId;
	
	createdElements.push(travellerTypeEmt);
	createdElements.push(roomMappingEmt);
	
	packageForm.appendChild(travellerTypeEmt);
	packageForm.appendChild(roomMappingEmt);
}










function getFixedYear(paramDate){
	if(!paramDate || paramDate == "undefined")
		paramDate = new Date();
	var retval = paramDate.getYear();
	if(paramDate.getYear() < 1900){
		retval = paramDate.getYear() + 1900;
	}
	
	return retval;
}





function selectGoto(selectEmt){
	var url = selectEmt.options[selectEmt.selectedIndex].value;
	if(url != ''){
		window.location.href = url;
	}
}


// Find element position
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}




// Dynamic drop down menu object. Used in extranet as a workaround to avoid cdata issues.
function DynamicDropDown(itemArray, optionObj){
	this.items = itemArray;
	this.optionObj = null;
	if(optionObj != null && optionObj != 'undefined')
		this.optionObj = optionObj;
	this.titleTxt = "Please select...";
	this.classTxt = null;
	this.onchangeEvent = null;
	this.optionEmts = new Array();
	this.selectEmt = null;
	
	
	this.build = function(){
		
		// Extract options from the options object
		if(this.optionObj != null){
			if(this.optionObj.titleTxt != 'undefined')
				this.titleTxt = this.optionObj.titleTxt;
			
			if(this.optionObj.classTxt != null && this.optionObj.classTxt != 'undefined')
				this.classTxt = this.optionObj.classTxt;
			
			if(this.optionObj.onchangeEvent != null && this.optionObj.onchangeEvent != 'undefined')
				this.onchangeEvent = this.optionObj.onchangeEvent;
		}
		
		// Create the select element
		this.selectEmt = document.createElement("select");
		
		// Attach event listener to the onchange event of the select element
		if(this.onchangeEvent != null){
			if(this.selectEmt.addEventListener){
				this.selectEmt.addEventListener('change', this.onchangeEvent, false);
			}
			else if(this.selectEmt.attachEvent){
				this.selectEmt.attachEvent('onchange', this.onchangeEvent);
			}
		}
		
		// Attach class name to the select element for CSS styling
		if(this.classTxt != null)
			this.selectEmt.className = this.classTxt;
		
		// Build title option (the first option element)
		if(this.titleTxt != null){
			var titleEmt = document.createElement("option");
			var titleTxt = document.createTextNode(this.titleTxt);
			titleEmt.appendChild(titleTxt);
			
			this.optionEmts.push(titleEmt);
		}
		
		// Build option elements
		for(var i = 0; i < this.items.length; i++){
			var tempOpt = document.createElement("option");
			tempOpt.value = this.items[i][0];
			var tempTxt = document.createTextNode(this.items[i][1]);
			tempOpt.appendChild(tempTxt);
			
			this.optionEmts.push(tempOpt);
		}
		
		// Attach option elements to the select element
		for(var i = 0; i < this.optionEmts.length; i++){
			this.selectEmt.appendChild(this.optionEmts[i]);
		}
	}
	
	
	
	// Replaces the given DOM element with the generated select element
	this.replaceElement = function(replacedEmt){
		replacedEmt.parentNode.replaceChild(this.selectEmt, replacedEmt);
	}
}