// this is the library that 
// is able to execute flight
// search requests

// returns the stations that matches
// the search term the most
function GetMatchingStations(SearchTerm){
	/* look for a matching station */
	var BestMatchingStations = new Array();
	var DebugString = "";
	if(Stations!=null&&SortedStations!=null){
		// stations of microskies have been initialized
		for(var i=0;i<SortedStations.length;i++){
			var CurrentStationCode = SortedStations[i];
			var CurrentStationName = Stations[CurrentStationCode].name;
			
			// prepare the string for regexp comparison
			RegExpTerm = eval("/(.*)" 
			    + SearchTerm.toLowerCase()
			    .replace(" ","|") + "(.*)/");
			
			if(CurrentStationName.toLowerCase().search(RegExpTerm)!=-1){
			    BestMatchingStations.push(CurrentStationCode);
			    DebugString+=CurrentStationCode+"\n";
			}
		}
	}
	
	// alert(DebugString); // <-- use this for debugging!
	
	return BestMatchingStations;
}

// this function initiates a post to
// the skysales Search-step in order
// to get redirected to the Select-step
// directly without requiring the user
// to select his flight information once again.
function SearchFlights(Origin, Destination, OutwardDate, ReturnDate, 
					   DateFlexibility, AdultCount, ChildCount, InfantCount,
					   RequestedFareType, TravelType, Culture){
  var cult = Culture || "de-DE";
             
	// create the form and fiels and fill them
	var DynSkySalesForm = document.createElement("form");
	DynSkySalesForm.name = "SkySales"; DynSkySalesForm.id = "SkySales";
	DynSkySalesForm.method = "post";
	DynSkySalesForm.action = "/skysales/Search.aspx?culture=" + cult;
	
	// define the travel type
	var TravelTypeField = document.createElement("input");
	TravelTypeField.type = "hidden";
	TravelTypeField.name = "AvailabilitySearchInputSearchView$RadioButtonMarketStructure";
	TravelTypeField.id = TravelTypeField.name.replace("$","_");
	TravelTypeField.value = TravelType; // from the parameters
	DynSkySalesForm.appendChild(TravelTypeField);
	
	// form element for the origin definition
	var OriginField = document.createElement("input");
	OriginField.type = "hidden";
	OriginField.name = "AvailabilitySearchInputSearchView$DropDownListMarketOrigin1";
	OriginField.id = OriginField.name.replace("$","_");
	OriginField.value = Origin; // from the parameters
	DynSkySalesForm.appendChild(OriginField);
	
	// form element for the origin definition
	var DestinationField = document.createElement("input");
	DestinationField.type = "hidden";
	DestinationField.name = "AvailabilitySearchInputSearchView$DropDownListMarketDestination1";
	DestinationField.id = DestinationField.name.replace("$","_");
	DestinationField.value = Destination; // from the parameters
	DynSkySalesForm.appendChild(DestinationField);
	
		// form element for the origin definition
	var OriginField = document.createElement("input");
	OriginField.type = "hidden";
	OriginField.name = "AvailabilitySearchInputSearchView$DropDownListMarketOrigin2";
	OriginField.id = OriginField.name.replace("$","_");
	OriginField.value = Destination; // from the parameters
	DynSkySalesForm.appendChild(OriginField);
	
	// form element for the origin definition
	var DestinationField = document.createElement("input");
	DestinationField.type = "hidden";
	DestinationField.name = "AvailabilitySearchInputSearchView$DropDownListMarketDestination2";
	DestinationField.id = DestinationField.name.replace("$","_");
	DestinationField.value = Origin; // from the parameters
	DynSkySalesForm.appendChild(DestinationField);
	
	// define the requested fare type
	var FareTypeField = document.createElement("input");
	FareTypeField.type = "hidden";
	FareTypeField.name = "AvailabilitySearchInputSearchView$DropDownListFareTypes";
	FareTypeField.id = FareTypeField.name.replace("$","_");
	FareTypeField.value = RequestedFareType; // from the parameters
	DynSkySalesForm.appendChild(FareTypeField);
	
	  // outward flight date information
		// set the departure day value                           
		var OutwardDateObj = null;
		
		if ( OutwardDate == null || OutwardDate == '' ) {
			var currDate = new Date();
		  OutwardDateObj = new Date( currDate.getTime() + 1000*60*60*24*30 );
		} else {
		  OutwardDateObj = new Date(OutwardDate.substring(OutwardDate.lastIndexOf(".")+1),
		                           (OutwardDate.substring(OutwardDate.indexOf(".")+1,OutwardDate.lastIndexOf("."))-1),
		                            OutwardDate.substring(0,OutwardDate.indexOf(".")));		
		}
		
		var OutwardDayField = document.createElement("input");
		OutwardDayField.type = "hidden";
		OutwardDayField.name = "AvailabilitySearchInputSearchView$DropDownListMarketDay1";
		OutwardDayField.id = OutwardDayField.name.replace("$","_");
		OutwardDayField.value = OutwardDateObj.getDate();
		DynSkySalesForm.appendChild(OutwardDayField);
		
		// set the departure month and year value
		var OutwardMonthYear = document.createElement("input");
		OutwardMonthYear.type = "hidden";
		OutwardMonthYear.name = "AvailabilitySearchInputSearchView$DropDownListMarketMonth1";
		OutwardMonthYear.id = OutwardMonthYear.name.replace("$","_");
		OutwardMonthYear.value = OutwardDateObj.getFullYear() 
									+ "-" + (OutwardDateObj.getMonth()+1);
		DynSkySalesForm.appendChild(OutwardMonthYear);
									
		// set the flexibility value
		var OutwardFlexibility = document.createElement("input");
		OutwardFlexibility.type = "hidden";
		OutwardFlexibility.name = "AvailabilitySearchInputSearchView$DropDownListMarketDateRange1";
		OutwardFlexibility.id = OutwardFlexibility.name.replace("$","_");
		OutwardFlexibility.value = DateFlexibility;
		DynSkySalesForm.appendChild(OutwardFlexibility);
		
	  // return flight date information
		// set the departure day value
		var ReturnDateObj = null;
		
		if ( ReturnDate == null || ReturnDate == '' ) {
		  ReturnDateObj = new Date( OutwardDateObj.getTime() + 1000*60*60*24*7 );
		} else {
		  ReturnDateObj = new Date(ReturnDate.substring(ReturnDate.lastIndexOf(".")+1),
                              (ReturnDate.substring(ReturnDate.indexOf(".")+1,ReturnDate.lastIndexOf("."))-1),
                               ReturnDate.substring(0,ReturnDate.indexOf(".")));
		}		
		
		var ReturnDayField = document.createElement("input");
		ReturnDayField.type = "hidden";
		ReturnDayField.name = "AvailabilitySearchInputSearchView$DropDownListMarketDay2";
		ReturnDayField.id = ReturnDayField.name.replace("$","_");
		ReturnDayField.value = ReturnDateObj.getDate();
		DynSkySalesForm.appendChild(ReturnDayField);
		
		// set the departure month and year value
		var ReturnMonthYear = document.createElement("input");
		ReturnMonthYear.type = "hidden";
		ReturnMonthYear.name = "AvailabilitySearchInputSearchView$DropDownListMarketMonth2";
		ReturnMonthYear.id = ReturnMonthYear.name.replace("$","_");
		ReturnMonthYear.value = ReturnDateObj.getFullYear() 
									+ "-" + (ReturnDateObj.getMonth()+1);
		DynSkySalesForm.appendChild(ReturnMonthYear);
									
		// set the flexibility value
		var ReturnFlexibility = document.createElement("input");
		ReturnFlexibility.type = "hidden";
		ReturnFlexibility.name = "AvailabilitySearchInputSearchView$DropDownListMarketDateRange2";
		ReturnFlexibility.id = ReturnFlexibility.name.replace("$","_");
		ReturnFlexibility.value = DateFlexibility;
		DynSkySalesForm.appendChild(ReturnFlexibility);
		
	// define the amount pax for this trip
	var AdultCountField = document.createElement("input");
	AdultCountField.type = "hidden";
	AdultCountField.name = "AvailabilitySearchInputSearchView$DropDownListPassengerType_ADT";
	AdultCountField.id = AdultCountField.name.replace("$","_");
	AdultCountField.value = AdultCount;
	DynSkySalesForm.appendChild(AdultCountField);
	
	var ChildCountField = document.createElement("input");
	ChildCountField.type = "hidden";
	ChildCountField.name = "AvailabilitySearchInputSearchView$DropDownListPassengerType_CHD";
	ChildCountField.id = ChildCountField.name.replace("$","_");
	ChildCountField.value = ChildCount;
	DynSkySalesForm.appendChild(ChildCountField);
	
	var InfantCoundField = document.createElement("input");
	InfantCoundField.type = "hidden";
	InfantCoundField.name = "AvailabilitySearchInputSearchView$DropDownListPassengerType_INFANT";
	InfantCoundField.id = InfantCoundField.name.replace("$","_");
	InfantCoundField.value = InfantCount;
	DynSkySalesForm.appendChild(InfantCoundField);
	
	// some system values before the end
	var CurrentStepField = document.createElement("input");
	CurrentStepField.type = "hidden";
	CurrentStepField.name = "step";
	CurrentStepField.id = "step";
	CurrentStepField.value = "Select";
	DynSkySalesForm.appendChild(CurrentStepField);
	
	var EventTargetField = document.createElement("input");
	EventTargetField.type = "hidden";
	EventTargetField.name = "__EVENTTARGET";
	EventTargetField.id = "__EVENTTARGET";
	EventTargetField.value = "AvailabilitySearchInputSearchView$LinkButtonNewSearch";
	DynSkySalesForm.appendChild(EventTargetField);
	
	var EventArgumentField = document.createElement("input");
	EventArgumentField.type = "hidden";
	EventArgumentField.name = "__EVENTARGUMENT";
	EventArgumentField.id = "__EVENTARGUMENT";
	EventArgumentField.value = "";
	DynSkySalesForm.appendChild(EventArgumentField);
	
	var ViewStateField = document.createElement("input");
	ViewStateField.type = "hidden";
	ViewStateField.name = "__VIEWSTATE";
	ViewStateField.id = "__VIEWSTATE";
	ViewStateField.value = "";
	DynSkySalesForm.appendChild(ViewStateField);
	
	// finally submit the form
	var Placeholder = document.getElementById("DynamFormPlaceholder");
	Placeholder.appendChild(DynSkySalesForm);
	document.getElementById("SkySales").submit();
}

/* does the same as search flights, but posts the search
    to the bmibbaby website by using the parameters */
function SearchWWFlights(Origin, Destination, OutwardDate, ReturnDate, 
			                     DateFlexibility, AdultCount, ChildCount, InfantCount,
			                     RequestedFareType, TravelType, Culture){
    // language selection is done on the bmibaby site                            
    var OutwardDateObj = new Date(OutwardDate.substring(OutwardDate.lastIndexOf(".")+1),
            (OutwardDate.substring(OutwardDate.indexOf(".")+1,OutwardDate.lastIndexOf("."))-1),
            OutwardDate.substring(0,OutwardDate.indexOf(".")));
            
    var ReturnDateObj = new Date(ReturnDate.substring(ReturnDate.lastIndexOf(".")+1),
		                            (ReturnDate.substring(ReturnDate.indexOf(".")+1,ReturnDate.lastIndexOf("."))-1),
		                            ReturnDate.substring(0,ReturnDate.indexOf(".")));
             
	// create the form and fiels and fill them
	var DynSkySalesForm = document.createElement("form");
	DynSkySalesForm.name = "bmibabyForm"; DynSkySalesForm.id = "bmibabyForm";
	DynSkySalesForm.method = "post";
	DynSkySalesForm.action = "http://www.bmibaby.com/bmibaby/en/search.aspx?linkid=gw";
	DynSkySalesForm.target = "_blank";
			
	// Source value is always Germanwings
	var SourceField = document.createElement("input");
	SourceField.type = "hidden";
	SourceField.name = "Source";
	SourceField.id = SourceField.name;
	SourceField.value = "germanwings";
	DynSkySalesForm.appendChild(SourceField);

    // From value is the departure airport
	var FromField = document.createElement("input");
	FromField.type = "hidden";
	FromField.name = "From";
	FromField.id = FromField.name;
	FromField.value = Origin;
	DynSkySalesForm.appendChild(FromField);

    // To value is the arrival airport
	var ToField = document.createElement("input");
	ToField.type = "hidden";
	ToField.name = "To";
	ToField.id = ToField.name;
	ToField.value = Destination;
	DynSkySalesForm.appendChild(ToField);
	
	// OutboundDate value is the outbound flight date
	var OutboundDateField = document.createElement("input");
	OutboundDateField.type = "hidden";
	OutboundDateField.name = "OutboundDate";
	OutboundDateField.id = OutboundDateField.name;
	OutboundDateField.value = OutwardDateObj.getDate() + "/" 
	                        + (OutwardDateObj.getMonth()+1) + "/"
	                        + OutwardDateObj.getFullYear();
	DynSkySalesForm.appendChild(OutboundDateField);
	
	/* also specify the return date when its a round trip */
	if(TravelType=="RoundTrip"){
	    // ReturnDate value is the return flight date
	    var ReturnDateField = document.createElement("input");
	    ReturnDateField.type = "hidden";
	    ReturnDateField.name = "ReturnDate";
	    ReturnDateField.id = ReturnDateField.name;
	    ReturnDateField.value = ReturnDateObj.getDate() + "/" 
	                            + (ReturnDateObj.getMonth()+1) + "/"
	                            + ReturnDateObj.getFullYear();
	    DynSkySalesForm.appendChild(ReturnDateField);
	}
	
	// Adt value is the adult pax count
	var AdtField = document.createElement("input");
	AdtField.type = "hidden";
	AdtField.name = "Adt";
	AdtField.id = AdtField.name;
	AdtField.value = AdultCount;
	DynSkySalesForm.appendChild(AdtField);
	
	// Chd value is the child pax count
	var ChdField = document.createElement("input");
	ChdField.type = "hidden";
	ChdField.name = "Chd";
	ChdField.id = ChdField.name;
	ChdField.value = ChildCount;
	DynSkySalesForm.appendChild(ChdField);
	
	// Inf value is the infant pax count
	var InfField = document.createElement("input");
	InfField.type = "hidden";
	InfField.name = "Inf";
	InfField.id = InfField.name;
	InfField.value = InfantCount;
	DynSkySalesForm.appendChild(InfField);
	
	// finally submit the form
	var Placeholder = document.getElementById("DynamFormPlaceholder");
	Placeholder.appendChild(DynSkySalesForm);
	document.getElementById("bmibabyForm").submit();
	
}

/* creates the url for the Vueling search page by using the parameters */
function SearchVYFlights(Origin, Destination, OutwardDate, ReturnDate, 
			                     DateFlexibility, AdultCount, ChildCount, InfantCount,
			                     RequestedFareType, TravelType, Culture){

  var OutwardDateObj = new Date(OutwardDate.substring(OutwardDate.lastIndexOf(".")+1),
            (OutwardDate.substring(OutwardDate.indexOf(".")+1,OutwardDate.lastIndexOf("."))-1),
            OutwardDate.substring(0,OutwardDate.indexOf(".")));
  var ReturnDateObj = new Date(ReturnDate.substring(ReturnDate.lastIndexOf(".")+1),
                      (ReturnDate.substring(ReturnDate.indexOf(".")+1,ReturnDate.lastIndexOf("."))-1),
                      ReturnDate.substring(0,ReturnDate.indexOf(".")));

  var Language = "EN";
  switch (Culture){
    case "de-DE":
    case "en-GB":
      Language = "EN"; break;
    case "es-ES":
      Language = "ES"; break;
    case "fr-FR":
      Language = "FR"; break;
    case "it-IT":
      Language = "IT"; break;
    case "nl-NL":
      Language = "NL"; break;
    default:
      Language = "EN";
  }

  var NrMarkets = '2';
  if (TravelType === 'OneWay'){
    NrMarkets = '1';
  }

  var url = "http://www.vueling.com/skylights/cgi-bin/skylights.cgi?";
  
  // create parameter for Vueling deep linking
  var param = 'event=search&module=SB&page=SEARCH&language=' + Language;
  param += '&travel=' + NrMarkets + '&from1=' + Origin + '&to1=' + Destination;
  var OutwardDay = OutwardDateObj.getDate() < 10 ? '0' + OutwardDateObj.getDate().toString() : OutwardDateObj.getDate().toString();
  var OutwardMonth = (OutwardDateObj.getMonth()+1) < 10 ? '0' + (OutwardDateObj.getMonth()+1).toString() : (OutwardDateObj.getMonth()+1).toString();
  param += '&departDay1=' + OutwardDay;
  param += '&departMonth1=' + OutwardDateObj.getFullYear().toString() + OutwardMonth;
  param += '&departDate1=' + OutwardDateObj.getFullYear().toString() + OutwardMonth + OutwardDay;
  param += '&depart1FlexBy=0101';
  
  if (NrMarkets === '2') {
    param += '&from2=' + Destination + '&to2=' + Origin;
    var ReturnDay = ReturnDateObj.getDate() < 10 ? '0' + ReturnDateObj.getDate().toString() : ReturnDateObj.getDate().toString();
    var ReturnMonth = (ReturnDateObj.getMonth()+1) < 10 ? '0' + (ReturnDateObj.getMonth()+1).toString() : (ReturnDateObj.getMonth()+1).toString();
    param += '&departDay2=' + ReturnDay;
    param += '&departMonth2=' + ReturnDateObj.getFullYear().toString() + ReturnMonth;
    param += '&departDate2=' + ReturnDateObj.getFullYear().toString() + ReturnMonth + ReturnDay;
    param += '&depart2FlexBy=0101';
  }
  
  param += '&ADULT=' + AdultCount + '&CHILD=' + ChildCount + '&INFANT=' + InfantCount + '&numberMarkets=' + NrMarkets;

  window.open(url + param, "_blank");
}

/* creates the url for the Condor search page by using the parameters */
function SearchDEFlights(Origin, Destination, OutwardDate, ReturnDate, 
			                     DateFlexibility, AdultCount, ChildCount, InfantCount,
			                     RequestedFareType, TravelType, Culture){
  /* create the date objects first */
  var OutwardDateObj = new Date(OutwardDate.substring(OutwardDate.lastIndexOf(".")+1),
            (OutwardDate.substring(OutwardDate.indexOf(".")+1,OutwardDate.lastIndexOf("."))-1),
            OutwardDate.substring(0,OutwardDate.indexOf(".")));
  var ReturnDateObj = new Date(ReturnDate.substring(ReturnDate.lastIndexOf(".")+1),
                      (ReturnDate.substring(ReturnDate.indexOf(".")+1,ReturnDate.lastIndexOf("."))-1),
                      ReturnDate.substring(0,ReturnDate.indexOf(".")));  
  /* define the language for the Condor IBE */
  var Language = "de";
  switch (Culture){
    case "de-DE":
      Language = "de"; break;
    case "en-GB":
      Language = "eu"; break;
    case "es-ES":
      Language = "es"; break;
    case "fr-FR":
      Language = "fr"; break;
    case "it-IT":
      Language = "it"; break;
    case "pl-PL":
      Language = "pl"; break;
    default:
      Language = "us";
  }
  
  // define the base url struckture
  var url = "http://www.condor.com/ibe/cfi/" + Language + "/flight/search.xhtml?"
          + "origin=" + Origin + "&destination=" + Destination;
  
  // add outward flight date value
  var OutwardDay = OutwardDateObj.getDate() < 10 ? '0' + OutwardDateObj.getDate().toString() : OutwardDateObj.getDate().toString();
  var OutwardMonth = (OutwardDateObj.getMonth()+1) < 10 ? '0' + (OutwardDateObj.getMonth()+1).toString() : (OutwardDateObj.getMonth()+1).toString();
  url += "&outwardYearMonthDay=" + OutwardDateObj.getFullYear().toString() + OutwardMonth + OutwardDay;
  
  // add the triptype to the urls
  if(TravelType=="OneWay"){
    url += "&flightVacancyType=1";
  }else{
    url += "&flightVacancyType=2";
    // add the return flight date
    var ReturnDay = ReturnDateObj.getDate() < 10 ? '0' + ReturnDateObj.getDate().toString() : ReturnDateObj.getDate().toString();
    var ReturnMonth = (ReturnDateObj.getMonth()+1) < 10 ? '0' + (ReturnDateObj.getMonth()+1).toString() : (ReturnDateObj.getMonth()+1).toString();
    url += "&returnYearMonthDay=" + ReturnDateObj.getFullYear().toString() + ReturnMonth + ReturnDay;
  }
  
  // define the number of passengers
  url += "&partyAdults=" + AdultCount 
        + "&partyChildren=" + ChildCount 
        + "&partyInfants=" + InfantCount;
    
  // execute the availability with this command
  url += "&operationType=search";
  
  // finally open the url
  window.open(url, "_blank");
}