﻿/* Funktion für Info-Layer */
var infovisible = 0;
var clicked1 = 0;
var clicked2 = 0;
var clicked3 = 0;

function show(img) {
	document.getElementById(img).style.visibility = "visible";
}

function hide(img) {
	document.getElementById(img).style.visibility = "hidden";
}

function writeinfo(headline, content) {
	document.getElementById('infoheadline').innerHTML = headline;
	document.getElementById('infocontent').innerHTML = content;
}

function openinfo() {
	if (infovisible != true) {
		show('infobox');
	} 
	var infovisible = true;
}

function closeinfo(num) {
	hide('infobox'); 
	var infovisible = 0; 
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);

		position=new Object();
  	position.x=curleft;
		position.y=curtop;
		return position;
	}	
}

function openInfobox(clickElementId, textFragment, hDirection, vDirection, size) {
	var horizontalMargin = 6;
	var verticalMargin = 0;

	if (clickElementId == 'icon_mainhelp') {
		horizontalMargin += 9; 
	}
	if (clickElementId == 'execbonus') {
		verticalMargin -= 2;
	}
	if (clickElementId == 'bookingclass') {
		horizontalMargin += 9;
	}
	
	var iconWidth = eval("$('"+clickElementId+"').offsetWidth"); 
	
	if (size == 'small') {
		elementWidth = 408;
	}
	else if (size == 'large') {
		elementWidth = 608;
	}

	clickElement=document.getElementById(clickElementId);
	pos=findPos(clickElement);

	showElement=document.getElementById('infobox');
	showElement.className = size;

	eval("writeinfo(headline_"+textFragment+", content_"+textFragment+")"); 
	elementHeight = $('infobox').offsetHeight;
	
	if (hDirection == 'right') {
		showElement.style.left = pos.x + iconWidth + horizontalMargin + "px";	
	}
	else {
		showElement.style.left = (pos.x - elementWidth - horizontalMargin) + "px";	
	}
	
	if (vDirection == 'top') {
		showElement.style.top = pos.y - elementHeight + "px";	
	}
	else {
		showElement.style.top = pos.y + verticalMargin + "px";	
	}
	openinfo(textFragment);
}



function toggle() {
	this.obj = null; 
	
	var browser = 'Firefox';
	var thissteps = 5;
	var thistimeOut = 0;
		
	if (navigator.userAgent.indexOf('MSIE 6') != -1) {
		browser = 'IE6';
		var thissteps = 5;
		var thistimeOut = 0;
	}
	if (navigator.userAgent.indexOf('MSIE 7') != -1) {
		browser = 'IE7';
		var thissteps = 15;
		var thistimeOut = 0;
	}
	if (navigator.userAgent.indexOf('Firefox') != -1) {
		browser = 'Firefox';
		var thissteps = 5;
		var thistimeOut = 0;
	}
	if (navigator.userAgent.indexOf('Safari') != -1) {
		browser = 'Safari';
		var thissteps = 5;
		var thistimeOut = 0;
	}
	
	var elmtStatus = false; 
	var heightGiven = false; 
	var rowStatus = true;
	var rowHeightGiven = false; 
	var rowHeight = 0;
	
	this.status = "closed";
	
	this.toggleE = function() {
		if(elmtStatus) {
			document.getElementById(this.obj).style.height = parseInt(document.getElementById(this.obj).getElementsByTagName('div')[0].offsetHeight) + 'px';
		}
		toggleElmt(this.obj);
		
		if (this.status == "open") {
			this.status = "closed";

			if (this.obj == 'wrapper_economy') {
				$('hl_economy').style.background = "transparent url(img/arrow_blue.gif) 8px center no-repeat";
			}
			else if  (this.obj == 'wrapper_business') {
				$('hl_business').style.background = "transparent url(img/arrow_blue.gif) 8px center no-repeat";
			}
			else if  (this.obj == 'wrapper_first') {
				$('hl_first').style.background = "transparent url(img/arrow_blue.gif) 8px center no-repeat";
			}
			else if  (this.obj == 'wrapper_upgrade') {
				$('hl_upgrade').style.background = "transparent url(img/arrow_blue.gif) 8px center no-repeat";
			}
		}
		else {
			this.status = "open";
			
			if (this.obj == 'wrapper_economy') {
				$('hl_economy').style.background = "transparent url(img/arrow_down.gif) 4px center no-repeat";
			}
			else if  (this.obj == 'wrapper_business') {
				$('hl_business').style.background = "transparent url(img/arrow_down.gif) 4px center no-repeat";
			}
			else if  (this.obj == 'wrapper_first') {
				$('hl_first').style.background = "transparent url(img/arrow_down.gif) 4px center no-repeat";
			}
			else if  (this.obj == 'wrapper_upgrade') {
				$('hl_upgrade').style.background = "transparent url(img/arrow_down.gif) 4px center no-repeat";
			}
		}
	}
	
	function toggleElmt(thisobj) {
		if (!elmtStatus) {
			document.getElementById(thisobj).style.height = (parseInt(document.getElementById(thisobj).style.height) + thissteps) + 'px';
		}
		else {
			if (!heightGiven) {
				document.getElementById(thisobj).style.height = document.getElementById(thisobj).offsetHeight + 'px';
				heightGiven = true; 
			}
			
			if (parseInt(document.getElementById(thisobj).style.height) > 0) {
				document.getElementById(thisobj).style.height = (parseInt(document.getElementById(thisobj).style.height) - thissteps) + 'px';
			}
		}
		callWithTimeOut(thisobj);
	}
	
	function callWithTimeOut(thisobj) {
		if (!elmtStatus) {
			if((parseInt(document.getElementById(thisobj).style.height) + thissteps) <= parseInt(document.getElementById(thisobj).getElementsByTagName('div')[0].offsetHeight)) {
				var self=this; 
				thistimeOut=function(){ 
			 		window.setTimeout(function() { 
			       	toggleElmt.call(self, thisobj); 
			 		}, thistimeOut); 
				} 
				thistimeOut();
			}
			else {
				document.getElementById(thisobj).style.height = parseInt(document.getElementById(thisobj).getElementsByTagName('div')[0].offsetHeight) + 'px';
				//ie margin bug fix!
				if(browser == 'IE6' || browser == 'IE7') {
					document.getElementById(thisobj).style.marginBottom = '0';
				}
				elmtStatus = true; 
			}
		}
		else {
			if((parseInt(document.getElementById(thisobj).style.height) - thissteps) > 0) {
				var self=this; 
				thistimeOut=function(){ 
			 		window.setTimeout(function() { 
			     	toggleElmt.call(self, thisobj); 
			 		}, thistimeOut); 
				} 
				thistimeOut();
			}
			else {
				document.getElementById(thisobj).style.height = 0;
				elmtStatus = false; 
			}
		}
	}
	
	var rowHeight;
	
	this.toggleR = function(rowNr) {
		if(parseInt(document.getElementById(this.obj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].offsetHeight)!=0) {
			rowHeight = parseInt(document.getElementById(this.obj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].offsetHeight);
		}
		toggleRow(this.obj,rowNr);
	}
	
	function toggleRow(thisobj,rowNr) {
		if(document.getElementById(thisobj).offsetHeight != 0) {
			document.getElementById(thisobj).style.height = '';
		}
		else {
			//ie margin bug fix!
			if(browser == 'IE6' || browser == 'IE7') {
				document.getElementById(thisobj).style.marginBottom = '-3px';
			}
		}
		
		if (!rowStatus) {
			document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height = (parseInt(document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height) + thissteps) + 'px';
		}
		else {
			if (!rowHeightGiven) {
				document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height = document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].offsetHeight + 'px';
				rowHeightGiven = true; 
			}
			document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height = (parseInt(document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height) - thissteps) + 'px';
		}
		callRowWithTimeOut(thisobj,rowNr);
	}
	
	function callRowWithTimeOut(thisobj,rowNr) {
		if (!rowStatus) {
			if((parseInt(document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height) + thissteps) <= rowHeight) {
				var self=this; 
				thistimeOut=function(){ 
			 		window.setTimeout(function() { 
			       	toggleRow.call(self, thisobj, rowNr); 
			 		}, thistimeOut); 
				} 
				thistimeOut();
			}
			else {
				document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height = rowHeight + 'px';
				rowStatus = true; 
			}
		}
		else {
			if((parseInt(document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height) - thissteps) > 0) {
				var self=this; 
				thistimeOut=function(){ 
			 		window.setTimeout(function() { 
			       	toggleRow.call(self, thisobj, rowNr); 
			 		}, thistimeOut); 
				}
				thistimeOut();
			}
			else {
				document.getElementById(thisobj).getElementsByTagName('div')[0].getElementsByTagName('div')[rowNr].style.height = 0;
				rowStatus = false; 
			}
		}
	}
}


function displayMap(origin,destination) {
	var req = new Request({
		url: 'php/get_map.php',
		method: 'get',
		data: { 
			'origin': $('airportcode_origin').value,
			'destination': $('airportcode_destination').value
		},  
		onComplete: function(response) { 
			$('map').src = "img/karten/"+response;
		}
	}).send();
}


// KOMMUNIKATION MIT DER FLASHKARTE

function thisMovie(movieName) { 
	if (navigator.appName.indexOf("Microsoft") != -1) { 
		return window.document[movieName]; 
	} 
	else { 
		return document[movieName];
	}
} 


// Uebergabe der gewaehlten Flughaefen aus HTML an Flash
function flashIsLoaded() {
}

// Uebergabe der Werte der Eingabefelder an Flash
function airportsToActionScript(origin,destination){
		thisMovie("flashMap").airportsToActionScript(origin,destination); 
}

// Zuruecksetzen der Karte
function resetToActionScript(){
		thisMovie("flashMap").resetToActionScript();
}


// Uebergabe der Werte von Flash an die Eingabefelder
function airportsToJavaScript(origin, destination){
	var req_name = new Request({
		method: 'get',  
		url: 'php/getCompleteName.php?language='+$('language').value,
		data: { 
			'origin': origin,
			'destination': destination
		},
		onComplete: function(response) { 
			// Response in Einzelteile zerlegen
			var parsed = response.split('|');  
			
			origin_complete = parsed[0];
			destination_complete = parsed[1];

			// Sonderzeichen ersetzen
			origin_complete = origin_complete.replace("&#268;", "Č");
			destination_complete = destination_complete.replace("&#268;", "Č");
			origin_complete = origin_complete.replace("&#269;", "č");
			destination_complete = destination_complete.replace("&#269;", "č");
			origin_complete = origin_complete.replace("&#352;", "Š");
			destination_complete = destination_complete.replace("&#352;", "Š");
			origin_complete = origin_complete.replace("&#353;", "š");
			destination_complete = destination_complete.replace("&#353;", "š");
			origin_complete = origin_complete.replace("&#381;", "Ž");
			destination_complete = destination_complete.replace("&#381;", "Ž");
			origin_complete = origin_complete.replace("&#382;", "ž");
			destination_complete = destination_complete.replace("&#382;", "ž");
			origin_complete = origin_complete.replace("&#379;", "Ż");
			destination_complete = destination_complete.replace("&#379;", "Ż");
			origin_complete = origin_complete.replace("&#380;", "ż");
			destination_complete = destination_complete.replace("&#380;", "ż");
			origin_complete = origin_complete.replace("&#346;", "Ś");
			destination_complete = destination_complete.replace("&#346;", "Ś");
			origin_complete = origin_complete.replace("&#347;", "ś");
			destination_complete = destination_complete.replace("&#347;", "ś");
			origin_complete = origin_complete.replace("&#321;", "Ł");
			destination_complete = destination_complete.replace("&#321;", "Ł");
			origin_complete = origin_complete.replace("&#322;", "ł");
			destination_complete = destination_complete.replace("&#322;", "ł");
			origin_complete = origin_complete.replace("&#260;", "Ą");
			destination_complete = destination_complete.replace("&#260;", "Ą");
			origin_complete = origin_complete.replace("&#261;", "ą");
			destination_complete = destination_complete.replace("&#261;", "ą");
			origin_complete = origin_complete.replace("&#280;", "Ę");
			destination_complete = destination_complete.replace("&#280;", "Ę");
			origin_complete = origin_complete.replace("&#281;", "ę");
			destination_complete = destination_complete.replace("&#281;", "ę");
			origin_complete = origin_complete.replace("&#243;", "ó");
			destination_complete = destination_complete.replace("&#243;", "ó");
			
			if (origin_complete != "") {
				$('origin').value = origin_complete;
				$('airportcode_origin').value = origin;
			}
			else {
				$('origin').value = "";
				$('airportcode_origin').value = "";
			
			}
			
			if (destination_complete != "") {
				$('destination').value = destination_complete;
				$('airportcode_destination').value = destination;
			}
			else {
				$('destination').value = "";
				$('airportcode_destination').value = "";
			}	
			
			if (origin_complete == "" || destination_complete == "") {
				if (location.href.indexOf('earn_miles.php') != -1) {
					disableAccordion();
				}
				else if (location.href.indexOf('spend_miles.php')!= -1) {
					//clearFields();
				}	
			}
			
			if (origin_complete != "" && destination_complete != "") {
				if (location.href.indexOf('earn_miles.php') != -1) {
					getVia(origin, destination);
				}
				else if (location.href.indexOf('spend_miles.php')!= -1) {
					activateAccordion();
					restoreAccordion();
					updateMiles();
				}
			}
		}
	}).send();
}

function activateAccordion() {
	// geöffnete Accordionelemente schliessen
	if (ecoToggle.status == 'open') {
		ecoToggle.toggleE();
	}
	if (businessToggle.status == 'open') {
		businessToggle.toggleE();
	}
	if (firstToggle.status == 'open') {
		firstToggle.toggleE();
	}
	if (upgradeToggle.status == 'open') {
		upgradeToggle.toggleE();
	}
				
	// Accordion klickbar machen	
	$('hl_economy').onclick = function(e) {
		ecoToggle.toggleE();
	};
	$('hl_business').onclick = function(e) {
		businessToggle.toggleE();
	};
	$('hl_first').onclick = function(e) {
		firstToggle.toggleE();
	};
	$('hl_upgrade').onclick = function(e) {
		var elmt = (document.all) ? event.srcElement : e.target;
		if(elmt.className != 'help')
			upgradeToggle.toggleE();
		};
	
	// Hinweis entfernen
	$('notification').style.display = 'none';
	
	$('wrapper_economy').style.display = 'block';
	$('wrapper_business').style.display = 'block';
	$('wrapper_first').style.display = 'block';
	$('wrapper_upgrade').style.display = 'block';
	
	ecoToggle.toggleE();
}

function disableAccordion() {
	if (location.href.indexOf('earn_miles.php') != -1) {
		closeAccordion('economy');
		closeAccordion('business');
		closeAccordion('first');
	
		$('wrapper_economy').style.display = 'none';
		$('wrapper_business').style.display = 'none';
		$('wrapper_first').style.display = 'none';
	
		$('hl_economy').className = '';
		$('hl_business').className = '';
		$('hl_first').className = '';
	
		$('hl_economy').style.background = "transparent url(img/arrow.gif) 8px center no-repeat";
		$('hl_business').style.background = "transparent url(img/arrow.gif) 8px center no-repeat";
		$('hl_first').style.background = "transparent url(img/arrow.gif) 8px center no-repeat";
	}
	else if (location.href.indexOf('spend_miles.php')!= -1) {
		// Residence-Checkbox ausblenden
		$('residence').style.display = "none";	
		$('resetButton').style.top = "9px";
	
		// Hinweis anzeigen
		$('notification').style.display = 'block';
	
		// Sichtbarkeit der Accordion-Tabellen-Wrapper aufheben
		$('wrapper_economy').style.display = 'none';
		$('wrapper_business').style.display = 'none';
		$('wrapper_first').style.display = 'none';
		$('wrapper_upgrade').style.display = 'none';
	
		// kein Hand-Cursor fuer Accordion-Balken
		$('hl_economy').className = '';
		$('hl_business').className = '';
		$('hl_first').className = '';
		$('hl_upgrade').className = '';

		$('hl_economy').style.background = "transparent url(img/arrow.gif) 8px center no-repeat";
		//$('hl_business').style.background = "transparent url(img/arrow.gif) 8px center no-repeat";
		//$('hl_first').style.background = "transparent url(img/arrow.gif) 8px center no-repeat";

	
		// ausgeblendete Tabellenreihen einblenden
		$('miles_fs_business').parentNode.style.display = 'block';
		$('miles_first').parentNode.style.display = 'block';
		$('miles_oneway_first').parentNode.style.display = 'block';
		$('miles_up_eco1').parentNode.style.display = 'block';
		$('miles_up_eco2').parentNode.style.display = 'block';
		$('miles_up_business').parentNode.style.display = 'block';
							
		// Texte in Accordion-Balken entfernen
		$('no_first_class').innerHTML = "";
		$('no_upgrades').innerHTML = "";
	}
}

function restoreAccordion() {
	if (location.href.indexOf('earn_miles.php') != -1) {
		// Accordion-Tabellen-Wrapper sichtbar machen
		$('wrapper_economy').style.display = 'block';
		$('wrapper_business').style.display = 'block';
		$('wrapper_first').style.display = 'block';

		// Hand-Cursor fuer Accordion-Balken
		$('hl_economy').className = 'clickable';
		$('hl_business').className = 'clickable';
		$('hl_first').className = 'clickable';

		// versteckte Reihen wieder sichtbar machen
		$('tr_economy1').style.display = 'block';
		$('tr_economy2').style.display = 'block';
		$('tr_economy3').style.display = 'block';
		$('tr_business').style.display = 'block';
		$('tr_first').style.display = 'block';
	
		// Texte in Ueberschriften entfernen
		$('no_first_class').innerHTML = '';
	}
	else if (location.href.indexOf('spend_miles.php')!= -1) {
		// Accordion-Tabellen-Wrapper sichtbar machen
		$('wrapper_economy').style.display = 'block';
		$('wrapper_business').style.display = 'block';
		$('wrapper_first').style.display = 'block';
		$('wrapper_upgrade').style.display = 'block';

		// Hand-Cursor fuer Accordion-Balken
		$('hl_economy').className = 'clickable';
		$('hl_business').className = 'clickable';
		$('hl_first').className = 'clickable';
		$('hl_upgrade').className = 'clickable';

		// versteckte Reihen wieder sichtbar machen
		$('tr_economy_fs').style.display = 'block';
		$('tr_business_fs').style.display = 'block';
		$('tr_first').style.display = 'block';
		$('tr_first_oneway').style.display = 'block';
		$('tr_eco1_up').style.display = 'block';
		$('tr_eco2_up').style.display = 'block';
		$('tr_business_up').style.display = 'block';
	
		// Texte in Ueberschriften entfernen
		$('no_first_class').innerHTML = '';
		$('no_upgrades').innerHTML = '';
	
		// Links zur Online-Buchung einblenden
		$('link_first').style.display = 'block'
		$('link_upgrade').style.display = 'block'
	
		// Styles auf Darstellung von Zahlen anpassen
		$('miles_economy_online').className = "miles last";
		$('miles_fs_economy_online').className = "miles last";
		$('miles_business_online').className = "miles last";
		$('miles_fs_business_online').className = "miles last";
		$('miles_first_online').className = "miles last";
		$('miles_up_eco1_online').className = "miles last";
		$('miles_up_eco2_online').className = "miles last";
		$('miles_up_business_online').className = "miles last";		
	}	
}


function resetInput(autocomplete_origin, autocomplete_destination) {
/*
$('reset').value = 'true';
*/
	autocomplete_origin.setOptions({postData: {'mode': 'origin', 'origin': ''}});
	autocomplete_destination.setOptions({postData: {'mode': 'origin', 'origin': ''}});

	$('origin').value = "Land/Stadt/3-Letter-Code";
	$('destination').value = "Land/Stadt/3-Letter-Code";
	$('airportcode_origin').value = "";
	$('airportcode_destination').value = "";

	
	if (location.href.indexOf('earn_miles.php') != -1) {
		$('connectiondetails').style.display = 'none';
		resetFields('details');
	}
	disableAccordion();
	resetToActionScript();
}



function isDefined(variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}


function openFAQ(url) {
	satellit = window.open(url,"FAQ","height=650,width=800,left=20,top=20,screenX=20,screenY=20,resizable=no,menubar=no,scrollbars=no,status=yes,toolbar=no,adressbar=no");
}

