// JavaScript Document


function BookingPageContent(aName, adultCount, childCount, kidCount)
{
  this.FIXEDNO=2;
  this.name=aName;
  this.id='_B_'+this.name;
  this.departureOffer=null;
  this.destinationOffer=null;
  this.height=390;
  this.width=585;

  this.leftContentOffset=10;
  this.rightContentOffset=10;
  this.departureElements=new Array();
  this.destinationElements=new Array();
  this.departureId='booking_content_departure_'+this.name;
  this.destinationId='booking_content_dest_'+this.name;
  this.bookingInfoId='booking_info_'+this.name;
  this.departLabel=new Array();
  this.destLabel=new Array();
  this.initStyle();
  this.cal = new GregorianCalendar();
  this.actionListener=null;
  this.buttonText='???';
  this.buttonHeight=25;
  this.buttonWidth=100;
  this.buttonTop=420;
  this.buttonLeft=380;
  this.buttonStyleClass = 'bookingButton';
  this.adultCount=adultCount ? adultCount : 1;
  this.childCount=childCount ? childCount : 0;
  this.kidCount=childCount ? childCount : 0;
  this.inputFields=new Array();
  this.inputElements=new Array();
  this.priceUpdater = new BookingPriceUpdater(this.bookingInfoId, 'Gesamtpreis');
  this.infoHeaderText=new Array('Hinflug', 'R&#252;ckflug');
  this.decimalPoint =',';
  //this.formatter=new DEHtmlStringFormatter();
  return this;
}

/**
 * Configuration of height, width, leftContentOffset, rightContentOffset, buttonStyleClass
 *   buttonTop; buttonLeft;
 */
BookingPageContent.prototype.init=function(anArray)
{
  for (var theElement in anArray)
  {
    this[theElement]=anArray[theElement];
  }
}


BookingPageContent.prototype.initInputFields=function(anArray)
{
 this.inputFields = anArray;
 for(var i=0; i<this.inputFields.length;i++)
 {
  this.inputElements[this.inputFields[i]]=null;
 }
}

BookingPageContent.prototype.getDepartureOffer=function()
{
  return this.departureOffer;
}

BookingPageContent.prototype.getDestinationOffer=function()
{
  return this.destinationOffer;
}

BookingPageContent.prototype.initStyle=function()
{
 var theWidth= new Array();
 theWidth['date']=60;
 theWidth['time']=40;
 theWidth['arrivalTime']=50;
 //theWidth['stop']=65;
 theWidth['from']=110;
 theWidth['to']=105;
 theWidth['price']=95;
 theWidth['airline']=90;

 this.departLabel[0]=new DispWrapper(this.id+'_adate', 'Datum',               theWidth['date'], 'date');
 this.departLabel[1]=new DispWrapper(this.id+'_adeparttime', 'Abflug',        theWidth['time'], 'time');
 this.departLabel[2]=new DispWrapper(this.id+'_adesttime', 'Ankunft',         theWidth['arrivalTime'], 'arrivalTime');
 //this.departLabel[3]=new DispWrapper(this.id+'_astop', 'Stop',                theWidth['stop'], 'from');
 this.departLabel[3]=new DispWrapper(this.id+'_adeparture', 'Start', theWidth['from'], 'from');
 this.departLabel[4]=new DispWrapper(this.id+'_adestination', 'Ziel',      theWidth['to'], 'to');
 this.departLabel[5]=new DispWrapper(this.id+'_aprice', 'Preis p.P. &#8364;',   theWidth['price'], 'netto');
 this.departLabel[6]=new DispWrapper(this.id+'_aline', 'Fluglinie',           theWidth['airline'], 'airline');


 this.destLabel[0]=new DispWrapper(this.id+'_ddate', 'Datum',                theWidth['date'], 'date');
 this.destLabel[1]=new DispWrapper(this.id+'_ddeparttime', 'Abflug',         theWidth['time'], 'time');
 this.destLabel[2]=new DispWrapper(this.id+'_ddesttime', 'Ankunft',          theWidth['arrivalTime'], 'arrivalTime');
 //this.destLabel[3]=new DispWrapper(this.id+'_dstop', 'Stop',               theWidth['stop'], 'from');
 this.destLabel[3]=new DispWrapper(this.id+'_ddeparture', 'Start',  theWidth['from'], 'from');
 this.destLabel[4]=new DispWrapper(this.id+'_ddestination', 'Ziel',       theWidth['to'], 'to');
 this.destLabel[5]=new DispWrapper(this.id+'_dprice', 'Preis p.P. &#8364;',    theWidth['price'], 'netto');
 this.destLabel[6]=new DispWrapper(this.id+'_dline', 'Fluglinie',            theWidth['airline'], 'airline');

}


BookingPageContent.prototype.initButton=function(aText, aWidth, aHeight)
{
  return this.buttonText=aText; this.buttonHeight=aHeight;this.buttonWidth=aWidth;
}

BookingPageContent.prototype.setButtonText=function(aText)
{
  return this.buttonText=aText;
}


BookingPageContent.prototype.setActionListener=function(aListener)
{
  this.actionListener=aListener;
}

BookingPageContent.prototype.setHeight=function(aHeight)
{
  this.height=aHeight;
}

BookingPageContent.prototype.setWidth=function(aWidth)
{
  this.width=aWidth;
}

BookingPageContent.prototype.setFlightData=function(aDepartureOffer, aDestinationOffer)
{
  this.departureOffer=aDepartureOffer;
  this.destinationOffer=aDestinationOffer;
  this.setFlightDataByList(this.departLabel, this.departureOffer);
  this.setFlightDataByList(this.destLabel, this.destinationOffer);
  this.priceUpdater.updatePrice(this.adultCount, this.childCount, this.kidCount, this.departureOffer, this.destinationOffer);
}

BookingPageContent.prototype.setFlightDataByList=function(aList,aOffer)
{
  var theNext=null;
  var theElement;
  var theValue;

  for (var i=0; i<aList.length;i++)
  {
    theNext=aList[i];
    theElement = document.getElementById(theNext.id);
    if (!theElement)
    {
      alert('No book-element for id '+theNext.id);
    }
    else
    {
      theValue = this.getOfferValue(aOffer, theNext.attr);
      var theP = new PElement();
      theP.addContent(theValue);
      theElement.innerHTML=theP.write();
    }
  }
}

BookingPageContent.prototype.countChanged=function(anEvent)
{
  this.adultCount= anEvent.getAdults();
  this.childCount= anEvent.getChildren();
  this.kidCount= anEvent.getKids();
  //alert ("Count changed called! New: "+this.adultCount+" child "+this.childCount+" kids "+this.kidCount );
  this.priceUpdater.updatePrice(this.adultCount, this.childCount, this.kidCount, this.departureOffer, this.destinationOffer);
}

BookingPageContent.prototype.getOfferValue=function(aOffer, aName)
{
  var theValue ='null';
  if (aOffer)
  {
    theValue=aOffer[aName];

    if (aName=='date')
    {
      theValue = this.cal.print(theValue);
    }
    else if (aName == 'netto')
    {
      var theTaxes = aOffer['taxes'];
      theTaxes = Number(theTaxes).toFixed(this.FIXEDNO);
      theTaxes=theTaxes.replace(/\./,this.decimalPoint);

      theValue = Number(theValue).toFixed(this.FIXEDNO);
      theValue=theValue+'+'+theTaxes+' St.';
      theValue=theValue.replace(/\./,this.decimalPoint);
    }
    else if (aName=='from' || aName=='to')
    {
      theValue = theValue;
    }
  }
  //alert('Setting '+theValue);
  return theValue;
}

BookingPageContent.prototype.show=function()
{
  var theParent = new DIVElement();
  theParent.addStyle('position:absolute');



  theParent.addAttribute('id',this.id);
  theParent.addAttribute('class', 'bookingPageContent_parent');

  var theDHeight=Math.round(this.height/3);

  var theTopPos = 10;
  var theAElement = this.initBookingElement(this.departureId,'bookingDeparture', theDHeight, theTopPos, 0);
  theParent.addElement(theAElement)

  theTopPos+=(theDHeight+20);
  var theBElement = this.initBookingElement(this.destinationId,'bookingDestination', theDHeight, theTopPos,1);
  theParent.addElement(theBElement)

  theTopPos+=(theDHeight+20);
  var theCElement = this.initInfoElement(this.bookingInfoId,'bookingInfoElement', 30, theTopPos,1);
  theParent.addElement(theCElement)

  /*var theButton = new JsButton('request_'+this.name);
  theButton.addActionListener(this.actionListener);
  theButton.setWidth(this.buttonWidth);
  theButton.setHeight(this.buttonHeight);
  theButton.setText(this.buttonText);
  theButton.getElement().addStyleElement('top', this.buttonTop+'px');
  theButton.getElement().addStyleElement('left', this.buttonLeft+'px'); */
  //theParent.addElement(theButton.getElement());



  var theBookButton = new IconButton('book_b_'+this.name, 'img/bookingButton.jpg', 'img/bookingButton.jpg','img/bookingButtonClick.jpg');
  theBookButton.setPosition(this.buttonTop,this.buttonLeft);
  theBookButton.setText(this.buttonText);
  theBookButton.setEnabled(true);
  theBookButton.addActionListener(this.actionListener);
  theBookButton.setStyleClass(this.buttonStyleClass);

  theParent.addElement(theBookButton.getElement());

  return theParent.write();

}




BookingPageContent.prototype.setVisible=function(aValue)
{
  if (!aValue)
  {
    return;
  }
  var theCount = 0;
  var theValue = null;
  for (var thePerson in this.inputElements)
  {
   if (!this.inputElements[thePerson])
   {
    this.inputElements[thePerson]=document.getElementById(thePerson);
   }
   if (theCount ==0 && this.inputElements[thePerson])
   {
    theValue = this.inputElements[thePerson].value;
    this.adultCount=theValue;
   }
   else if (theCount ==1 && this.inputElements[thePerson])
   {
    theValue = this.inputElements[thePerson].value;
    this.childCount= theValue;
   }
   else if (theCount ==2 && this.inputElements[thePerson])
   {
    theValue = this.inputElements[thePerson].value;
    this.kidCount=theValue;
   }
   //alert("Value "+theCount+" ("+thePerson+")="+theValue);
   theCount++;

  }
  this.priceUpdater.updatePrice(this.adultCount, this.childCount, this.kidCount, this.departureOffer, this.destinationOffer);
}

BookingPageContent.prototype.getContentLeft=function()
{
  return this.leftContentOffset;
}

BookingPageContent.prototype.getContentWidth=function()
{
  return (this.width - this.leftContentOffset -this.rightContentOffset);
}

BookingPageContent.prototype.initInfoElement=function(anId,aClass, aHeight, aTopPos, aType)
{
  var theElement= new DIVElement();
  theElement.addAttribute('id',anId);
  theElement.addAttribute('class',aClass);
  theElement.addStyle('position:absolute');
  theElement.addStyle('width:'+this.getContentWidth()+'px');
  //theElement.addStyle('top:'+aTopPos+'px');
  theElement.addStyleElement('left', this.getContentLeft()+'px');


  return theElement;

}

BookingPageContent.prototype.initBookingElement=function(anId,aClass, aHeight, aTopPos, aType)
{
  var theElement= new DIVElement();
  theElement.addAttribute('id',anId);
  theElement.addAttribute('class',aClass);

  var theBColor=null;
  if (aType==0)
  {
    var theBColor='#d0e9cd';
  }
  else
  {
    var theBColor='#ead2ca';
  }
  theElement.addStyle('border:2px; border-style:solid; border-color:'+theBColor+';');
  theElement.addStyleElement('width',this.getContentWidth()+'px');
  theElement.addStyleElement('height',aHeight+'px');
  theElement.addStyleElement('top',aTopPos+'px');
  theElement.addStyleElement('left', this.getContentLeft()+'px');
  theElement.addStyleElement('position', 'absolute');
  //theElement.addContent("CONTAINER");
  if (aType==0)
  {
    theElement.addElement(this.createInfoHeader('img/flieger_sw_hin.gif', this.infoHeaderText[0], 0));
    theElement.addElement(this.renderDeparture());
  }
  else
  {
    theElement.addElement(this.createInfoHeader('img/flieger_sw_rueck.gif', this.infoHeaderText[1],1));
    theElement.addElement(this.renderDestination());
  }

  return theElement;
}

/**
 * Creates the Header for the booking information - types will be used to extend the class names of the Attributes
 */
BookingPageContent.prototype.createInfoHeader=function(anImg, aText, aType)
{
  var theInfoElement = new DIVElement();
  theInfoElement.addAttribute('class', 'bookingInfoHeader_'+aType);
  theInfoElement.addStyle('width:'+(this.width-this.getContentWidth())+'px');
  //theInfoElement.addStyle('font-size:3em; background-color:#FFFFF0');

  var theIContainer = new DIVElement();
  theIContainer.addStyleElement('position','absolute');
  theIContainer.addStyleElement('width',30+'px');
  theIContainer.addStyleElement('height',30+'px');
  theIContainer.addStyleElement('top',5+'px');
  theIContainer.addStyleElement('left', 5+'px');

  var theIElement = new ElementTag('img');
  theIElement.addAttribute('src',anImg);
  theIElement.addAttribute('alt','');

  theIContainer.addElement(theIElement);

  var thePContainer = new DIVElement();
  thePContainer.addAttribute('class','bookingInfoHP_'+aType);
  //thePContainer.addAttribute('class','lfTextEntry');
  thePContainer.addStyleElement('position','absolute');
  thePContainer.addStyleElement('width',130+'px');
  thePContainer.addStyleElement('height',30+'px');
  thePContainer.addStyleElement('top', 5+'px');
  thePContainer.addStyleElement('left', 50+'px');

  var thePElement = new PElement();
  thePElement.addContent(aText);

  thePContainer.addElement(thePElement);
  theInfoElement.addElement(theIContainer);
  theInfoElement.addElement(thePContainer);

  return theInfoElement;
}



BookingPageContent.prototype.renderDeparture=function()
{
    var theElement= new DIVElement();
    theElement.addAttribute('id',this.id+'_departure_booklabel');
    theElement.addAttribute('class','departure_booklabel');
    //theElement.addContent("DEPARTURE");
    this.initRenderElement(theElement);
    var theTopPos=0;
    this.addCells(theElement, this.departLabel, theTopPos,'label_', 'label_');
    theTopPos=50;
    this.addCells(theElement, this.departLabel, theTopPos, '', 'val_0_');
    var theNext;
    var theLeftPos=0;
    return theElement;
}


BookingPageContent.prototype.renderDestination=function()
{
    var theElement= new DIVElement();
    theElement.addAttribute('id',this.id+'_dest_booklabel');
    theElement.addAttribute('class','dest_booklabel');
    //theElement.addContent("DESTINATION");
    this.initRenderElement(theElement);
    var theTopPos=0;
    this.addCells(theElement, this.destLabel, theTopPos, 'label_','label_');
    theTopPos=50;
    this.addCells(theElement, this.destLabel, theTopPos, '','val_1_');
    return theElement;
}


BookingPageContent.prototype.addCells=function(aElement, aCellList, aTop, aIdPrefix, aClassP)
{

    var theLeftPos=0;
    //var theClasses='';
    for(var i = 0; i<aCellList.length; i++)
    {
      theNext=new DIVElement();
      theNext.addAttribute('id', aIdPrefix+aCellList[i].id);
      theNext.addAttribute('class', aClassP+aCellList[i].attr);
      //theClasses+='\n'+aClassP+aCellList[i].attr;
      theNext.addStyleElement('position','absolute');
      theNext.addStyleElement('height', 20+'px');
      theNext.addStyleElement('top',aTop+'px');
      theNext.addStyleElement('left',theLeftPos+'px');
      theNext.addStyleElement('z-index',100);
      var thePElement=new PElement();
      thePElement.addContent(aCellList[i].title);
      theNext.addElement(thePElement);
      aElement.addElement(theNext);
      theLeftPos=Math.abs(aCellList[i].width)+theLeftPos;
    }
    //alert("Booking Cell Classes="+theClasses);
}


BookingPageContent.prototype.initRenderElement=function(aElement)
{
    aElement.addStyle('width:'+((this.getContentWidth()-20)+'px'));
    //aElement.addStyle('font-size:3em; background-color:#FFFFF0');
    aElement.addStyleElement('height',24+'px');
    aElement.addStyle('top:'+40+'px');
    aElement.addStyle('left:'+10+'px');
    aElement.addStyle('position:absolute');
}




function BookingActionListener (aBookingContent)
{
  this.content=aBookingContent;
  this.cal = new GregorianCalendar();
}

BookingActionListener.prototype.actionPerformed=function(anEvent)
{
  var theDep = this.content.getDepartureOffer();
  var theDest= this.content.getDestinationOffer();

  var theStartDate = formatDate2String(theDep.getDate());
  var theStartFrom = theDep.getFrom();
  var theStartTo   = theDep.getTo();
  var theStartFromIata=theDep.getFromIataCode();
  var theStartToIata=theDep.getToIataCode();
  var theStartAirline = theDep.getAirlineShort();

  var theDestDate = formatDate2String(theDest.getDate());
  var theDestFrom = theDest.getFrom();
  var theDestTo   = theDest.getTo();
  var theDestFromIata=theDest.getFromIataCode();
  var theDestToIata=theDest.getToIataCode();
  var theDestAirline = theDest.getAirlineShort();

 var theAdultCount = this.content.adultCount;
  var theChildCount = this.content.childCount;
  var theKidCount = this.content.kidCount;
  /**
   * Anleitung:
   * document.location.href=<URI>    je nach Andforderung: z.B.
   * document.location.href="/a/airport_xml.php"+"?StartOrt="+theStartFrom+"?StartDatum="+theStartDate+"?ZielOrt="+theDestFrom+...;
  */

   	//var bookingLink = "booking.php"+"?fromiatacode="+theStartFromIata+"&startdate="+theStartDate+"&airline="+theStartAirline+"&toiatacode="+theDestFromIata+"&returnairline="+theDestAirline+"&enddate="+theDestDate+"&adult="+theAdultCount+"&child="+theChildCount+"&infant="+theKidCount;
   //window.open(bookingLink, "sucheFlugverbindung", "width=830,height=600,left=50,top=30,resizable=yes,scrollbars=yes");
	
	var bookingLink = ''; 
	if (window.clientDomain != null 
		&& window.clientDomain.length > 0 
		&& window.clientContext != null 
		&& window.clientDomain.length > 0) 
	{
		bookingLink += window.clientDomain + window.clientContext + '/do/parameterTransferToFares?';		
	} else {
		bookingLink += 'http://websrv.inhouse.partners.de:8080/lf_client_fred_lfo/do/parameterTransferToFares?';
	}
		
	bookingLink	+= 'airline=' + '&deltaOut=0' + '&deltaIn=0';
	bookingLink	+= '&refId=LFshop';		
	bookingLink += '&oneway=return';
	bookingLink += '&agencyNumber=' + (window.agencyNumber ? window.agencyNumber : 'LFAJAX');
	bookingLink += '&userId=' + (window.userID ? window.userID : 'IPARTNERS');
	bookingLink += '&password=' + ( window.password ? window.password : 'blues');
	bookingLink += '&style=' + ( window.styleName ? window.styleName : '');	
	bookingLink += '&designProperty=' + ( window.designProperty ? window.designProperty : '');
	bookingLink += '&departure=' + ( theStartFromIata ? theStartFromIata : '');
	bookingLink += '&destination=' + ( theStartToIata ? theStartToIata : '');
	bookingLink += '&departureDate=' + ( theStartDate ? theStartDate : '');
	bookingLink += '&returnDate=' + ( theDestDate ? theDestDate : '');
	bookingLink += '&fareGroup=' + ( window.fareGroup ? window.fareGroup : '');
	bookingLink += '&passengerTypeCode=' + ( 
		window.passengerTypeCode && window.passengerTypeCode != '' ? window.passengerTypeCode : 'ADT');
	bookingLink += '&adult='  + ( !isNaN(theAdultCount) ? theAdultCount : '1');
	bookingLink += '&child='  + ( !isNaN(theChildCount) ? theChildCount : '0');
	bookingLink += '&infant='  + ( !isNaN(theKidCount) ? theKidCount : '0');
	bookingLink += '&sortKey='  + ( window.sortKey ? window.sortKey : 'PRICE');
	bookingLink += '&sortKeyIntern='  + ( window.sortKeyIntern ? window.sortKeyIntern : 'PRICE');
	bookingLink += '&showWaitPage='  + ( window.showWaitPage ? window.showWaitPage : 'true');
	bookingLink += '&refURL='  + ( window.refURL ? window.refURL : '');
	bookingLink += '&fareSelector='  + ( window.fareSelector ? window.fareSelector : 'A');
	
	window.location.href=bookingLink;
}



function DiagramBookingSelectionListener(aTab, aTabNo, aDiagram, aDepartureDiagramName, aDestDiagramName, aBookingPage)
{
   this.departureOffer=null;
   this.destinationOffer=null;
   this.departureDiagram=aDepartureDiagramName;
   this.destinationDiagram=aDestDiagramName;
   this.tab=aTab;
   this.tabNo=aTabNo;
   this.bookingPage=aBookingPage;
   this.diagram=aDiagram;
   this.cal = new GregorianCalendar();
}

DiagramBookingSelectionListener.prototype.selectionPerformed=function(aSelectionEvent)
{
   var theDiagram = aSelectionEvent.source;
   var theType = aSelectionEvent.type;
   //alert("Selection event received:"+aSelectionEvent.source.getName());
   if (theType!=aSelectionEvent.SELECTION_DONE)
   {
    return;
   }
   //alert("The Diagram name "+theDiagram.name+" > "+this.departureDiagram+"="+(theDiagram.name==this.departureDiagram));
   //alert("The Diagram name "+theDiagram.name+" < "+this.destinationDiagram+"="+(theDiagram.name==this.destinationDiagram));

   if (theDiagram.name==this.departureDiagram)
   {
    this.departureOffer=aSelectionEvent.valueObject;
    this.diagram.setSelectedOffers(this.departureOffer, null);

    this.destinationOffer=null;
   }
   else
   {
    this.destinationOffer=aSelectionEvent.valueObject;
    this.diagram.setSelectedOffers(this.departureOffer, this.destinationOffer);
   }
   if (this.departureOffer)
   {
    if (this.destinationOffer)
    {
      //alert("Setting Offers ");
      var theDepDate=this.departureOffer.getDate();
      var theDestDate=this.destinationOffer.getDate();
      if (this.cal.after(theDestDate,theDepDate))
      {
        this.bookingPage.setFlightData(this.departureOffer, this.destinationOffer);
        this.tab.selectTab(this.tabNo);
      }
    }
   }
}


function DispWrapper(anId,aTitle, aWidth, aAttr)
{
  this.title=aTitle;
  this.width=aWidth;
  this.attr=aAttr;
  this.id=anId;
}
