/*
* $Id: flightSearch.js,v 1.4 2009/05/05 10:27:19 annette Exp $ 
*/
	function toggleOneWay() {
		var oneWayState = document.forms['flightJourneySearchForm'].ibeOnlyOutwardFlight.checked;
		if (oneWayState) {
		clearErrorFields('startDate', 'endDate');
	    document.forms['flightJourneySearchForm'].endDate.disabled = true;
	    document.forms['flightJourneySearchForm'].endDate.value = "";
	    }
		else document.forms['flightJourneySearchForm'].endDate.disabled = false;
	}
	function Destination(depAirportCode, depAirportName, depAirportCity, destAirportCode,  destAirportName, destAirportCity, countryCode, countryName){
			this.depAirportCode = depAirportCode;
			this.depAirportName = depAirportName;
			this.depAirportCity = depAirportCity;
			this.destAirportCode = destAirportCode;
			this.destAirportName = destAirportName;
			this.destAirportCity = destAirportCity;
			this.countryCode = countryCode;
			this.countryName = countryName;
		}
        function Airport(depAirportCode, depAirportName, destAirportCode,  destAirportName) {
			this.depAirportCode = depAirportCode;
			this.depAirportName = depAirportName;
			this.destAirportCode = destAirportCode;
			this.destAirportName = destAirportName;
		}
		function removeDestinations(formElement){
           	while(formElement.childNodes.length >0){
          		for(var i=0;i<formElement.childNodes.length;i++){
					childnode = formElement.childNodes[i];
					if (childnode!=null){
						childnode.parentNode.removeChild(childnode);
					} 
				}
			}                 
		}

  		function updateDepartureAirports(status){
   			var depAirportSelector = document.forms['flightJourneySearchForm'].departureAirport;
   			var destAirportSelector = document.forms['flightJourneySearchForm'].destination;
   			var selectedDepAirport;
   			var selectedDestAirport;
   			if(depAirportFromForm =='0' && !(depAirportSelector.selectedIndex>0)){
				selectedDepAirport = depAirportSelector.options[0].value;
			}else{
				selectedDepAirport = depAirportFromForm;
			}
			if(depAirportSelector.selectedIndex >0){
				selectedDepAirport = depAirportSelector.options[depAirportSelector.selectedIndex].value;
			}
			
			if(destAirportFromForm =='0' && !(destAirportSelector.selectedIndex>0)){
				selectedDestAirport = destAirportSelector.options[0].value;
			}else{
				selectedDestAirport = destAirportFromForm;
			}
			if(destAirportSelector.selectedIndex >0){
				selectedDestAirport = destAirportSelector.options[destAirportSelector.selectedIndex].value;
			}
   			
     		depAirportSelector.options.length=1;
     		var depCounter=1;
     		var depAirportCodes = [];
     		for (var i=0; i < destinations.length; i++){
      			thisDestination = destinations[i];
        		if(selectedDestAirport==0){
					existing =false;
					for (var j=0; j<depAirportCodes.length; j++){
						if(thisDestination.depAirportCode==depAirportCodes[j])
							existing=true;
					}
					if (existing==false){
						depAirportSelector.options.length=depAirportSelector.options.length+1;
						depAirportSelector.options[depCounter].value = thisDestination.depAirportCode;
							if(thisDestination.depAirportCity != '')
								depAirportSelector.options[depCounter].text = thisDestination.depAirportCity;
							else
                  				depAirportSelector.options[depCounter].text = thisDestination.depAirportName;
							if(status==true){
								if(thisDestination.depAirportCode==depAirportFromForm)
									depAirportSelector.options[depCounter].selected=true;
							}
							if(status==false){
								if(thisDestination.depAirportCode==selectedDepAirport)
									depAirportSelector.options[depCounter].selected=true;
							}
							depAirportCodes.length = depAirportCodes.length + 1;
							depAirportCodes[depAirportCodes.length-1]=thisDestination.depAirportCode;
							depCounter++;
					}	
				}
				else if(thisDestination.destAirportCode == selectedDestAirport){
					depAirportSelector.options.length=depAirportSelector.options.length+1;
					depAirportSelector.options[depCounter].value = thisDestination.depAirportCode;
					if(thisDestination.depAirportCity != '')
						depAirportSelector.options[depCounter].text = thisDestination.depAirportCity;
					else
                  		depAirportSelector.options[depCounter].text = thisDestination.depAirportName;
					if(status==true){
								if(thisDestination.depAirportCode==depAirportFromForm)
									depAirportSelector.options[depCounter].selected=true;
					}
					if(status==false){
						if(thisDestination.depAirportCode==selectedDepAirport)
							depAirportSelector.options[depCounter].selected=true;
					}
					depCounter++;
				}
			}
		}
function updateDestinationAirports(status){
   			var depAirportSelector = document.forms['flightJourneySearchForm'].departureAirport;
   			var destAirportSelector = document.forms['flightJourneySearchForm'].destination;
   			var selectedDepAirport;
   			var selectedDestAirport;

   			if(depAirportFromForm =='0' && !(depAirportSelector.selectedIndex>0)){
				selectedDepAirport = depAirportSelector.options[0].value;
			}else{
				selectedDepAirport = depAirportFromForm;
			}
			if(depAirportSelector.selectedIndex >0){
				selectedDepAirport = depAirportSelector.options[depAirportSelector.selectedIndex].value;
			}
			
			if(destAirportFromForm =='0' && !(destAirportSelector.selectedIndex>0)){
				selectedDestAirport = destAirportSelector.options[0].value;
			}else{
				selectedDestAirport = destAirportFromForm;
			}
			if(destAirportSelector.selectedIndex >0){
				selectedDestAirport = destAirportSelector.options[destAirportSelector.selectedIndex].value;
			}
			var optgroups = [];
        	
       	//empty existing content of selectbox
	removeDestinations(destAirportSelector);
   	if(selectedDepAirport==0){
		for (var i=0; i < allDestinations.length; i++){
			var currDestination = allDestinations[i];
			var countryname = currDestination.countryName;
			var currOptGroup = null;
			var currOptGroupIndex = -1;
			
			var foundCountry = false;
			var counter=0;
			while (optgroups.length > counter && currOptGroup ==null){
				if(optgroups[counter].label==countryname){
					currOptGroupIndex = counter;
					currOptGroup=optgroups[counter];
					foundCountry=true;
				}
				counter++;
			}
			if( foundCountry == false){
      			var oGroup = document.createElement('OPTGROUP');
      			oGroup.label= countryname;
      			oGroup.style.fontWeight='bold';
   				oGroup.style.fontStyle='normal';
      			optgroups.length = optgroups.length+1;
				currOptGroupIndex = optgroups.length-1
      			optgroups[currOptGroupIndex] = oGroup;
				currOptGroup = oGroup;
			}
			value= currDestination.destAirportCode;
			if(currDestination.destAirportCity != '')
				text= currDestination.destAirportCity;
			else
				text= currDestination.destAirportName;
      		newoption = document.createElement('OPTION');
			newoption.value = value;
			newoption.innerHTML = thisTab+text;
			if(status==true){
				if(destAirportFromForm == value)
					newoption.selected=true;
			}
			if(status==false){
				if(selectedDestAirport == value)
					newoption.selected=true;
			}
			currOptGroup.appendChild(newoption);
      		optgroups[currOptGroupIndex]= currOptGroup;
      	}
      	newoption = document.createElement('OPTION');
		newoption.value = 0;
		newoption.innerHTML = 'Reiseziel suchen ...';
		destAirportSelector.appendChild(newoption);
      	for (var k=0; k<optgroups.length; k++){
      		destAirportSelector.appendChild(optgroups[k]);
      	}	
	}
	else{
		for (var l=0; l < destinations.length; l++){
			var currDestination = destinations[l];
			var countryname = currDestination.countryName;
			var currOptGroup = null;
			var currOptGroupIndex = -1;
			var foundCountry = false;
			if(currDestination.depAirportCode == selectedDepAirport){
				var counter=0;
				while (optgroups.length > counter && currOptGroup ==null){
					if(optgroups[counter].label==countryname){
						currOptGroupIndex = counter;
						currOptGroup=optgroups[counter];
						foundCountry=true;
					}
					counter++;
				}
				if( foundCountry == false){
      				var oGroup = document.createElement('OPTGROUP');
      				oGroup.label= countryname;
      				oGroup.style.fontWeight='bold';
      				oGroup.style.fontStyle='normal';
	      			optgroups.length = optgroups.length+1;
					currOptGroupIndex = optgroups.length-1
      				optgroups[currOptGroupIndex] = oGroup;
					currOptGroup = oGroup;
				}
				value= currDestination.destAirportCode;
				if(currDestination.destAirportCity != '')
					text= currDestination.destAirportCity;
				else
					text= currDestination.destAirportName;
      			newoption = document.createElement('OPTION');
				newoption.value = value;
				newoption.innerHTML = thisTab+text;
				if(status==true){
					if(destAirportFromForm == value)
						newoption.selected=true;
				}
				if(status==false){
					if(selectedDestAirport == value)
						newoption.selected=true;
				}
				
				currOptGroup.appendChild(newoption);
      			optgroups[currOptGroupIndex]= currOptGroup;
      		}
      	}
      	newoption = document.createElement('OPTION');
		newoption.value = 0;
		newoption.innerHTML = 'Reiseziel suchen ...';
		destAirportSelector.appendChild(newoption);
      	for (var k=0; k<optgroups.length; k++){
      		destAirportSelector.appendChild(optgroups[k]);
      	}	
	}
}

var flightStandalone = false;
  flightStandalone = $('#standalone').text();
		function departureAirportSelected(){
			airport = document.getElementById("departureAirport");
			airportSelectedValue = airport.options[airport.selectedIndex].value;
			if(airportSelectedValue == "0"){
			 if(!flightStandalone){
  				airportError = document.getElementById("departureAirportEmptyError");
  				airportError.style.display="block";
				}
				return false;
			}else{
				airportError = document.getElementById("departureAirportEmptyError");
				airportError.style.display="none";
				return true;
			}
		}
		function destinationAirportSelected(){
			airport = document.getElementById("destinationAirport");
			airportSelectedValue = airport.options[airport.selectedIndex].value;
			if(airportSelectedValue == "0"){
			 if(!flightStandalone){
				airportError = document.getElementById("destinationAirportEmptyError");
				airportError.style.display="block";
			}
      	return false;
			}else{
				airportError = document.getElementById("destinationAirportEmptyError");
				airportError.style.display="none";
				return true;
			}
		}

