// JavaScript Document
//------------------- Diagram Content Renderer ----------------------------------------

function DiagramWrapper(aType, aMonthDiagram, aTabPane, aNo)
{
  this.type=aType;
  this.diagram=aMonthDiagram;
  this.tabPane=aTabPane;
  this.tabNo=aNo;
}

DiagramWrapper.prototype.setFlightContent=function(aModel)
{
  this.tabPane.selectTab(this.tabNo);
  if (this.type=='DEPARTURE')
  {
    this.diagram.setDepartureFlightContent(aModel);
    this.diagram.selectStartOffer();
  }
  else
  {
    this.diagram.setDestinationFlightContent(aModel);
    this.diagram.selectReturnOffer();
  }


}


function DiagramEvent(aDiagramSource, aEventType,aNewDate)
{
  this.RETURN_DATE_SHIFTED = 16;
  this.START_DATE_SHIFTED = 32;

  this.OFFER_SELECTION_DONE = 2;

  this.source=aDiagramSource;
  this.type=aEventType;
  this.newDate=aNewDate;
}

DiagramEvent.prototype.getSource=function()
{
  return this.source;
}

DiagramEvent.prototype.getType=function()
{
  return this.type;
}

DiagramEvent.prototype.getNewDate=function()
{
  return this.newDate;
}

function MonthDiagram(aName, aImageUrl, aSelectImageUrl)
{
  this.name = aName;
  this.imageUrl = aImageUrl;
  this.NO_RESULT_FOUND= {
    'destinationTable':'<p>Es konnten keine Daten f&#252;r den R&#252;ckflug ermittelt werden </p>',
    'departureTable':'<p>Es konnten keine Daten f&#252;r den Abflug ermittelt werden</p>'
  };

  this.selectImageUrl='img/yellow_dot.jpg';
  this.currencySign='img/euro_sign.jpg';
  //aSelectImageUrl;
  this.id = '__'+aName;
  this.departureId='departure_holder_'+this.id;
  this.destinationId='dest_holder_'+this.id;
  //this.width=596;
  this.height=450;
  this.leftPos=15;
  this.imgTopOffset=150;
  this.imgheight=20;
  this.barheight=10;
  this.scaleCount=5;//without first one.
  this.referenceLength=390;
  this.scaleWidth=35;
  this.backgroundImg='';

  this.tableWidth=580;
  this.tableHeight=170;
  this.scaleYHeight=120;


  this.toString = toFlightContent;

  this.selectedOffer=null;
  this.departureContentContainer= null;
  this.destinationContentContainer= null;
  this.destinationTable=null;
  this.departureTable=null;
  this.dateFilter = new DateSelectionFilter(null,null);
  this.departureDiagram='depart_diagram_'+this.name;;
  this.destinationDiagram='dest_diagram_'+this.name;
  this.bookingInfoId='b_info_'+this.name;

  this.depFlightRequestor = null;
  this.destFlightRequestor = null;


  this.destB=null;
  this.depB=null;

  this.url;
  this.horizontalBarImg='';

  this.scaleValue=100;

  this.selectedElement=null;

  this.departureContentModel=null
  this.destinationContentModel=null;
  //this.renderer=new TopCompleteListRenderer(this);
  
  this.windowName='diagramTableContainer'+'_'+this.name;
  window[this.windowName]=this;
  //scroll up function
  this.departureTable=null;
  this.table=null;
  this.cal=new GregorianCalendar();
  this.selectionListener=new Array();

  this.adultCount= 1;
  this.childCount= 0;
  this.kidCount= 0;

  this.depOffer=null;
  this.destOffer = null;
   // person count input element todo: externalize this reference to an independent PriceRenderer
  this.inputElements=new Array();

  this.dates=new Array();
  this.priceUpdater = new BookingPriceUpdater(this.bookingInfoId, 'Gesch&#228;tzter Gesamtpreis');
  this.monthList = new Array('Januar', 'Februar','M&#228;rz', 'April','Mai','Juni','Juli','August','September','Oktober','November','Dezember');
  this.actionListener = new Array();
  //this.stringFormatter = new DEHtmlStringFormatter();
  return this;
}


MonthDiagram.prototype.addActionListener=function(aListener)
{
  this.actionListener[this.actionListener.length]= aListener;
}

MonthDiagram.prototype.notifyActionListener=function(aDiagramEvent)
{
  for (var i =0; i<this.actionListener.length;i++)
  this.actionListener[i].diagramActionPerformed(aDiagramEvent);
}


MonthDiagram.prototype.setSelectedOffers=function(aDepOffer, aDestOffer)
{
  this.depOffer=aDepOffer;
  this.destOffer=aDestOffer;
  this.updatePrice();
}

MonthDiagram.prototype.init=function(anInitArray)
{
  if (anInitArray != null && anInitArray.length>0)
  {
     for (var theKey in anInitArray)
     {
      this[theKey]=anInitArray[theKey];
     }

  }
}


MonthDiagram.prototype.selectStartOffer=function()
{
  if (this.departureTable != null && this.selectedOffer !=null)
  {
    var theValue = this.createValueObject(this.selectedOffer);
    this.departureTable.preSelect(theValue);
  }
  this.updatePrice();
}

MonthDiagram.prototype.findDepOffer=function(aStartOffer)
{
  if (aStartOffer)
  {
    var theOffer = this.destinationContentModel.getBestDayOffer(aStartOffer.getDate(), 'later');
    if (theOffer != null)
    {
      this.depOffer = theOffer;
    }
  }
}

MonthDiagram.prototype.selectReturnOffer=function()
{
  this.depOffer = this.findDepOffer(this.selectedOffer);
  if (this.destinationTable!= null && this.depOffer !=null)
  {
    var theValue = this.createValueObject(this.depOffer);
  }
  this.updatePrice();
}

MonthDiagram.prototype.setSelectedOffer=function(anOffer)
{
  this.selectedOffer=anOffer;
}

MonthDiagram.prototype.diagramSelected=function(aOfferId, aDiagramName, aElementId)
{
  if (aDiagramName==this.destinationDiagram)
  {

    var theOffer = this.destinationContentModel.getFlightOffer(aOfferId);
    var theSize = theOffer.getPrice();
    this.destinationTable.changeValueObject(aElementId, theOffer, theSize);
    this.destinationTable.selectElement(aElementId);
  }
  else if (aDiagramName==this.departureDiagram)
  {
    var theOffer = this.departureContentModel.getFlightOffer(aOfferId);
    var theSize = theOffer.getPrice();
    this.departureTable.changeValueObject(aElementId, theOffer, theSize);
    this.departureTable.selectElement(aElementId);

  }
  else
  {
    alert("Unknown Diagram name:"+aDiagramName);
  }

}

/**
 *Requests the flight Requestor for new offers a number of days before or after the date of the last request
 * Currently the number of days to be shifted is set to 31 from the 15th of the month of the curren choosen date.
 *@param anId specifies what diagram should be updated
 *@param aDir specifies the direction to be shifted (-1 or +1)
 **/
MonthDiagram.prototype.shiftDate=function(anId, aDir)
{
  if (anId==this.destB)
  {
    this.shiftDateDest(anId, aDir);
  }
  else {
    this.shiftDateDep(anId, aDir);
  }
}



/**
 *Requests the flight Requestor for new offers a number of days before or after the date of the last request
 * Currently the number of days to be shifted is set to 31 from the 15th of the month of the curren choosen date.
 *@param anId specifies what diagram should be updated
 *@param aDir specifies the direction to be shifted (-1 or +1)
 **/
MonthDiagram.prototype.shiftDateDep=function(anId, aDir)
{

  var theDate = this.dates[this.depB];
  var theReq = null;
  var theShift = 31*aDir;

  if (!theDate)
  {
    alert(anId+" Date to shift not set.");
    return;
  }

  var theReq=this.depFlightRequestor;

  var theDayTolerance= theReq.getDayTolerance();
  theReq.setDayTolerance(15);

  theDate=this.cal.shift(theShift, theDate);

  this.dates[this.depB]=theDate;


  this.depFlightRequestor.setStartDate(this.cal.print(theDate));
  this.depFlightRequestor.callFlights();

  this.notifyActionListener(new DiagramEvent(this, 32, theDate));

  var theDestDate = this.dates[this.destB];

  if (this.cal.before(theDestDate, theDate))
  {
    var theCount = 0;
    while (!this.cal.after(theDestDate, theDate) && theCount <100)
    {
        theDestDate = this.cal.shift(Math.abs(theShift), theDestDate);
        theCount++;
    }
    if (theCount>10)
    {
      alert("Unexpected number of loops! - "+theCount);
    }

    var theTol = this.destFlightRequestor.getDayTolerance();
    this.destFlightRequestor.setDayTolerance(15);
    this.destFlightRequestor.setStartDate(this.cal.print(theDestDate));
    this.destFlightRequestor.callFlights();
    this.dates[this.destB]=theDestDate;

    this.notifyActionListener(new DiagramEvent(this, 16, theDestDate));
  }

}


/**
 *Requests the flight Requestor for new offers a number of days before or after the date of the last request
 * Currently the number of days to be shifted is set to 31 from the 15th of the month of the curren choosen date.
 *@param anId specifies what diagram should be updated
 *@param aDir specifies the direction to be shifted (-1 or +1)
 **/
MonthDiagram.prototype.shiftDateDest=function(anId, aDir)
{
  var theDate = this.dates[this.destB];
  var theReq = this.destFlightRequestor;
  var theShift = 31*aDir;
  if (!theDate)
  {
    alert(anId+" Date to shift not set.");
    return;
  }
  var theDayTolerance= theReq.getDayTolerance();
  theReq.setDayTolerance(15);

  theDate=this.cal.shift(theShift, theDate);

  var theDepDate = this.dates[this.depB];

  if (this.cal.after(theDate, theDepDate))
  {
    this.dates[this.destB]=theDate;
    theReq.setStartDate(this.cal.print(theDate));
    theReq.callFlights();
    this.notifyActionListener(new DiagramEvent(this, 16, theDate));
  }
}


MonthDiagram.prototype.shiftLeft=function(anId)
{
  this.shiftDate(anId, -1);
}

MonthDiagram.prototype.shiftRight=function(anId)
{
  this.shiftDate(anId, 1);
}


MonthDiagram.prototype.addSelectionListener=function(aListener)
{
  if(!aListener)
  {
    alert("addSelectionListener not possible: "+aListener);
    return;
  }
  this.selectionListener[this.selectionListener.length]=aListener;
}

MonthDiagram.prototype.setFlightRequestor=function(aDepRequestor, aDestRequestor)
{
  var theLoadingListener = new LoadingListener(this);
  this.depFlightRequestor=aDepRequestor;
  aDepRequestor.addListener(theLoadingListener);

  this.destFlightRequestor=aDestRequestor;
  aDestRequestor.addListener(theLoadingListener);
}


MonthDiagram.prototype.getFlightRequestor=function()
{
  return this.depFlightRequestor;
}

MonthDiagram.prototype.getPopupRequestor=function()
{
  return this.popupRequestor;
}


MonthDiagram.prototype.setHorizontalBarImg= function (aUrl)
{
	this.horizontalBarImg = aUrl;
}

MonthDiagram.prototype.initInputFields=function(anArray)
{
  this.inputElements=anArray;
}


MonthDiagram.prototype.setVisible=function(aBoolean)
{
  if (aBoolean)
  {
     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;
   }
   theCount++;

  }
  this.priceUpdater.updatePrice(this.adultCount, this.childCount, this.kidCount, this.depOffer, this.destOffer);
  }
}

MonthDiagram.prototype.show= function ()
{
  var theParent = new DIVElement();
  theParent.addStyle('position:relative');
  theParent.addStyle('top:0px');
  theParent.addStyle('left:0px');
  theParent.addAttribute('id',this.id);
  var theDHeight=Math.round((this.tableHeight+50));

  var theDepartureElement= new DIVElement();
  theDepartureElement.addAttribute('class','diagram_container');
  theDepartureElement.addAttribute('id',this.departureId);
  //theDepartureElement.addStyle('width:'+this.width+'px');
  theDepartureElement.addStyle('height:'+theDHeight+'px');
  theDepartureElement.addStyle('top:0px');
  theDepartureElement.addStyle('left:0px');
  theDepartureElement.addStyle('position:relative');

  theParent.addElement(theDepartureElement);

  var theDestElement= new DIVElement();
  theDestElement.addAttribute('class','diagram_container');
  theDestElement.addAttribute('id',this.destinationId);
  //theDestElement.addStyle('width:'+this.width+'px');
  theDestElement.addStyle('height:'+theDHeight+'px');
  theDestElement.addStyle('top:'+theDHeight+'px');
  theDestElement.addStyle('left:0px');
  theDestElement.addStyle('position:absolute');
  
  theParent.addElement(theDestElement);

  var theInfoElement = new DIVElement();
  theInfoElement.addAttribute('class','diagram_container_info');
  //theInfoElement.addStyle('width:'+this.width+'px');

  theInfoElement.addStyle('top:'+((2*theDHeight)+1)+'px');
  theInfoElement.addStyle('left:0px');
  theInfoElement.addStyle('position:absolute');

  var theInfo = new DIVElement();
  theInfo.addAttribute('class','diagram_info');
  theInfo.addAttribute('id',this.bookingInfoId);
  theInfo.addElement(
	  this.priceUpdater.createDiagramInfo(
		  this.adultCount, 
		  this.childCount, 
		  this.kidCount, 
		  this.departureOffer, 
		  this.destinationOffer));
  
  theInfoElement.addElement(theInfo);

  theParent.addElement(theInfoElement);

  return theParent.write();
}



MonthDiagram.prototype.updatePrice=function()
{
  this.priceUpdater.updatePrice(this.adultCount, this.childCount, this.kidCount, this.depOffer, this.destOffer);
}

/**
 * Observer callback function for PersonCounter CounterEvents
 */
MonthDiagram.prototype.countChanged=function(aCountChangeEvent)
{
  this.adultCount= aCountChangeEvent.getAdults();
  this.childCount= aCountChangeEvent.getChildren();
  this.kidCount= aCountChangeEvent.getKids();
  //&alert("Diagram notified for person count ");
  this.updatePrice();
}

/**
 * set the content for the return flight.
 */
MonthDiagram.prototype.setDestinationFlightContent= function (aContentModel)
{
  if (this.departureContentContainer == null)
  {
    this.departureContentContainer = document.getElementById(this.departureId);
  }
  if (this.destinationContentContainer == null)
  {
    this.destinationContentContainer = document.getElementById(this.destinationId);
  }

  this.destinationContentModel=aContentModel;

  var theDiagram=this.createDestinationTable(this.destinationContentModel, this.destinationDiagram);
  this.destinationContentContainer.innerHTML=theDiagram;
}

MonthDiagram.prototype.setDepartureFlightContent= function (aContentModel)
{
  if (this.departureContentContainer == null)
  {
    this.departureContentContainer = document.getElementById(this.departureId);
  }
  if (this.destinationContentContainer == null)
  {
    this.destinationContentContainer = document.getElementById(this.destinationId);
  }

  if (this.departureContentContainer == null)
  {
    alert("DepartureContentContainer is null");
  }
    //for testing purposes setting destination equals departure
  this.departureContentModel=aContentModel;
  var theDiagram =  this.createDepartureTable(this.departureContentModel, this.departureDiagram);
  //alert("Setting Diagramm: "+theDiagram);
  this.departureContentContainer.innerHTML=theDiagram;
}



MonthDiagram.prototype.getHeaderStyle=function()
{
  var theStyle='font-family:Arial,';
  theStyle+=' Helvetica, sans-serif; font-size:15px; font-weight:bold;';
  theStyle+=' color:red';
  return theStyle;
}




MonthDiagram.prototype.getDestDiagram=function()
{
  return this.destinationDiagram;
}

MonthDiagram.prototype.getDepartureDiagram=function()
{
  return this.departureDiagram;
}

MonthDiagram.prototype.createDestinationTable=function(aContentModel, aName)
{

  this.destB='_destCt_';
  this.destinationTable=new DiagramMap();
  this.dateFilter.setDestinationTable(this.destinationTable);
  this.destinationTable.addSelectFilter(this.dateFilter);

  var theTable = this.createTable(this.destinationTable,aContentModel, aName, this.destB);
  if (!theTable)
  {
    if (this.NO_RESULT_FOUND['destinationTable'])
      return this.NO_RESULT_FOUND['destinationTable'];
    else
      alert("No destination content found.");
    return null;
  }

  var theElement = this.createContainer(this.destB);

  theElement.addElement(theTable.createElement());
  return theElement.write();
}

MonthDiagram.prototype.setSelectionListener=function(aDiagram)
{
  if (this.selectionListener.length>0)
  {
    for(var i=0;i<this.selectionListener.length;i++)
    {
      aDiagram.addSelectionListener(this.selectionListener[i]);
    }
  }
  var theListener = new PopUpListener(this, aDiagram.name);
  aDiagram.addSelectionListener(theListener);
}

MonthDiagram.prototype.createDepartureTable=function(aContentModel, aName)
{

  this.depB='_depCt_';
  this.departureTable=new DiagramMap();
  this.dateFilter.setDepartureTable(this.departureTable);
  this.departureTable.addSelectFilter(this.dateFilter);

  var theTable = this.createTable(this.departureTable, aContentModel, aName, this.depB);
  if (!theTable)
  {
    return this.NO_RESULT_FOUND['departureTable'];
  }
  var theElement = this.createContainer(this.depB);
  this.departureDiagram=theTable.getName();

  theElement.addElement(theTable.createElement());
  return theElement.write();
}


MonthDiagram.prototype.createNoElementsTable=function(aTable, aContentModel, aName, aSyncName, aMessage)
{
  var theTable = aTable;//new DiagramTable();
  theTable.contentMarginTop=25; //Distance to diagram from border top
  theTable.contentMarginLeft=40; //Distance to diagram from border left
  theTable.headerMarginTop = 5; //Distance header to border
  theTable.headerMarginLeft = 0;

  theTable.setName(aName);
  theTable.setDimension(this.tableWidth,this.tableHeight, this.scaleYHeight);

  var theDate = aContentModel.getFirstDate();
  var theCurrentDate = new Date();
  if (!theDate)
  {
    theDate = this.dates[aSyncName];
    if (!theDate)
    {
      theDate = new Date();
    }
  }
  if (this.cal.compare(theCurrentDate, theDate)<0)
  {
    theDate = theCurrentDate;

  }
  this.dates[aSyncName]=theDate;
  theTable.setXStartValue(theDate);

  var theHighestPrice = aContentModel.getHighestOfferPrice();
  if (!theHighestPrice)
    theHighestPrice=100;
  theTable.setMaxValue(theHighestPrice);
  var  theTableHeader=this.createTableHeaderN(theDate,aSyncName, aMessage, '');

  theTable.setTableHeader(theTableHeader);

  return theTable;
}

MonthDiagram.prototype.createTable=function(aTable, aContentModel, aName, aSyncName)
{
  	var theLength = aContentModel.getAllOfferNodes() ? aContentModel.getAllOfferNodes().length : 0;
  if (theLength<=0)
  {   
    return this.createNoElementsTable(aTable,aContentModel, aName, aSyncName, 'Kein Ergebnis erhalten');
  }
  var theTable = aTable;//new DiagramTable();
  theTable.contentMarginTop=25; //Distance to diagram from border top
  theTable.contentMarginLeft=40; //Distance to diagram from border left
  theTable.headerMarginTop = 5; //Distance header to border
  theTable.headerMarginLeft = 0;

  theTable.setName(aName);
  theTable.setDimension(this.tableWidth,this.tableHeight, this.scaleYHeight);
  this.setSelectionListener(theTable);

  var theDate = aContentModel.getFirstDate();
  var theCurrentDate = new Date();
  if (!theDate)
  {
    alert ("No date set in application!");
  }
  if (this.cal.compare(theCurrentDate, theDate)<0)
  {
    theDate = theCurrentDate;

  }
  this.dates[aSyncName]=theDate;
  theTable.setXStartValue(theDate);

  var theHighestPrice = aContentModel.getHighestOfferPrice();
  theTable.setMaxValue(theHighestPrice);

  var theContent='';
  var theDayOffers = aContentModel.getDayOffers();
  var theCount=0;
  var theTableHeader=null;
  var theOffer=null;
  var theNextString='';
  var theDebugString='';
  for (var theNext in theDayOffers)
  {
    if (theDayOffers[theNext])
    {
      theNextString+=theNext+':';
      theOffer = theDayOffers[theNext].getBestOffer();
      if (theTableHeader==null)
      {
        theTableHeader=this.createTableHeader(theDate,aSyncName, theOffer);
      }
      theCount++;
      var thePos=this.findPosition(i,theDate, theOffer);
      if (thePos>=0 && thePos<=31)
      {
        theTable.setValue(thePos,this.createValueObject(theOffer));
      }
      else
      {
        theDebugString+=theOffer.id+' - ';
      }
    }
  }
  theTable.setTableHeader(theTableHeader);
  return theTable;
}

MonthDiagram.prototype.createTableHeader=function(aDate,aSyncName, aOffer, aDirection)
{
  if (aOffer !=null)
  {
    var theDeparture = aOffer.getFrom();
    var theDestination = aOffer.getTo();
    return this.createTableHeaderN(aDate,aSyncName, theDeparture, theDestination);
  }
  var theElement=new DIVElement();
  theElement.addAttribute('class','barDiagramHeader');
  return theElement;
}

MonthDiagram.prototype.createTableHeaderN=function(aDate,aDirection, aDeparture, aDestination)
{

  var theElement=new DIVElement();
  theElement.addAttribute('class','barDiagramHeader');

  var theSElement = new DIVElement();
  theSElement.addAttribute('class','barDiagramHeader_sign');
  var theImage = new ElementTag('img');
  theImage.addAttribute('src', this.currencySign);
  theImage.addAttribute('alt', 'currency');
  theSElement.addElement(theImage);

  var theDirectionElement = new DIVElement();
  theDirectionElement.addAttribute('class','barDiagramHeader_direction');
  this.addDirectionInfo(aDirection, theDirectionElement);

  var theDElement = new DIVElement();
  theDElement.addAttribute('class','barDiagramHeader_data');

  var thePElement = new PElement();
  thePElement.addContent(aDeparture+' - '+aDestination);
  theDElement.addElement(thePElement);

  var theMElement = new DIVElement();
  theMElement.addAttribute('class','barDiagramHeader_month');
  var thePMElement = new PElement();

  var theDate = aDate;
  var theInt = theDate.getMonth();
  var theDay = theDate.getDate();
  if (theDay < 10)
  {
    thePMElement.addContent(this.monthList[theInt]);
  }
  else
  {
    thePMElement.addContent(this.monthList[theInt]+'/'+this.monthList[((theInt+1)%12)]);
  }

  theMElement.addElement(thePMElement);

  theElement.addElement(theSElement);
  theElement.addElement(theDirectionElement);
  theElement.addElement(theDElement);
  theElement.addElement(theMElement);

  return theElement.write();
}

MonthDiagram.prototype.addDirectionInfo=function(aDirection, aElement)
{
  if (aDirection==this.depB)
  {
    this.createInfoHeader(aElement,'img/flieger_sw_hin.gif', 'Hinflug');

  }
  else
  {
    this.createInfoHeader(aElement, 'img/flieger_sw_rueck.gif', 'R&#252;ckflug');
  }
}

MonthDiagram.prototype.createInfoHeader=function(anElement, anImg, aText)
{
  var theInfoElement = anElement;
  //theInfoElement.addStyle('font-size:3em; background-color:#FFFFF0');
  var theDiv = new DIVElement();
  theDiv.addAttribute('class', 'barDiagramHeader_icon');

  var theElement = new ElementTag('img');
  theElement.addAttribute('src',anImg);
  theElement.addAttribute('alt',aText);
  theDiv.addElement(theElement);

  var thePDiv = new DIVElement();
  thePDiv.addAttribute('class', 'barDiagramHeader_text');
  var thePElement = new PElement();
  thePElement.addContent(aText);
  thePDiv.addElement(thePElement);

  theInfoElement.addElement(theDiv);
  theInfoElement.addElement(thePDiv);
  return theInfoElement;
}

MonthDiagram.prototype.createContainer=function(anId)
{
  var theElement=new DIVElement();
  var theLeftPadding = 5;
  var theIconOffset=10;
  var theIconWidth=20;
  theElement.addStyle('position:absolute');
  theElement.addStyle('top:10px');
  theElement.addStyle('left:'+theLeftPadding+'px');
  theElement.addStyle('height:'+this.tableHeight+'px');
  theElement.addStyle('width:'+this.tableWidth+'px');

  var theLeftShiftElement = new DIVElement();
  var theTop=(this.tableHeight)+10;

  theLeftShiftElement.addAttribute('id','left_'+anId);
  theLeftShiftElement.addAttribute('onclick','javascript: window.'+this.windowName+'.shiftLeft(\''+anId+'\');nd(20);');
  theLeftShiftElement.addAttribute('onmouseover','document.body.style.cursor=\'pointer\';return overlib(\'- letzter Monat\');');
  theLeftShiftElement.addAttribute('onmouseout','document.body.style.cursor=\'default\';return nd(20);');
  theLeftShiftElement.addStyle('position:absolute');
  theLeftShiftElement.addStyleElement('top',theTop+'px');
  theLeftShiftElement.addStyle('left:'+theIconOffset+'px');
  theLeftShiftElement.addStyle('height:'+20+'px');
  theLeftShiftElement.addStyle('width:'+theIconWidth+'px');
  theLeftShiftElement.addStyleElement('z-index','55');
  var theLeftIcon=new ElementTag('img');
  theLeftIcon.addAttribute('src', 'img/l_shift.jpg');
  theLeftShiftElement.addElement(theLeftIcon);
  var theRightShiftElement = new DIVElement();

  var theLeft=(this.tableWidth-theIconOffset - 15);
  theRightShiftElement.addAttribute('id','right_'+anId);
  theRightShiftElement.addAttribute('onclick','javascript: window.'+this.windowName+'.shiftRight(\''+anId+'\');nd(20);');
  theRightShiftElement.addAttribute('onmouseover','document.body.style.cursor=\'pointer\';return overlib(\'+ n&#228;chster Monat\');');
  theRightShiftElement.addAttribute('onmouseout','document.body.style.cursor=\'default\';return nd(20);');
  theRightShiftElement.addStyle('position:absolute');
  theRightShiftElement.addStyleElement('top',theTop+'px');
  theRightShiftElement.addStyleElement('left',theLeft+'px');
  theRightShiftElement.addStyleElement('height',20+'px');
  theRightShiftElement.addStyleElement('width',theIconWidth+'px');
  theRightShiftElement.addStyleElement('z-index','55');
  var theRIcon=new ElementTag('img');
  theRIcon.addAttribute('src', 'img/r_shift.jpg');
  theRightShiftElement.addElement(theRIcon);

  var theTextElement = new DIVElement();
  theTextElement.addStyle('position:absolute');
  theTextElement.addStyleElement('width',(this.tableWidth-(2*(theIconOffset+theIconWidth)))+'px');
  theTextElement.addStyleElement('left',(theIconOffset+theIconWidth)+'px');
  theTextElement.addAttribute('class', 'diagram_foot_text');
  theTextElement.addStyleElement('z-index','55');
  theTextElement.addStyleElement('top',theTop+'px');

  var thePElement = new PElement();
  thePElement.addContent('Wochentag');
  theTextElement.addElement(thePElement);

  theElement.addElement(theLeftShiftElement);
  theElement.addElement(theTextElement);
  theElement.addElement(theRightShiftElement);
  return theElement;
}

MonthDiagram.prototype.calculateStartDate=function(aModel, aTopOfferId)
{
  var theTop=aModel.getTopOfferById(aTopOfferId);
  var theDate=null;
  var theNext;
  if (theTop==null)
  {
    alert("aTopOfferId not found in Model");
    return;
  }

  for (var i=0;i<3;i++)
  {
    var theOffer = theTop.getOffer(i);
    if(theOffer !=null)
    {
      theNext=theOffer.getDate();
      if(theDate==null)
      {
        theDate=theNext;
      }
      else if (this.cal.before(theNext, theDate))
      {
        theDate=theNext;
      }
    }
  }
  return theDate;
}

MonthDiagram.prototype.findPosition=function(aNumber, aDate, aOffer)
{
  var theOffer = aOffer; //aTopOffer.getOffer(aNumber);
  if (theOffer)
  {
    var theDate = theOffer.getDate();
    if (!theDate)
    {
      alert ("Offer date not set: "+theOffer.getOfferId());
      return -1;
    }
    return this.cal.compare(aDate, theDate);
  }
  else
  {
    alert("Offer "+aNumber+" is not defined ");
  }
  return 0;
}


MonthDiagram.prototype.selectDiagramBar=function(anId, aTopOfferId)
{
  if(this.departureContentModel != null && this.departureContentContainer != null)
  {
    this.diagram
    var theValue =  this.createDepartureTable(this.departureContentModel, this.departureDiagram);
    this.departureContentContainer.innerHTML= theValue;
  }
  if (this.destinationContentModel &&this.destinationContentContainer != null)
  {
    var theValue =  this.createDestinationTable(this.destinationContentModel, this.destinationDigram);
    this.destinationContentContainer.innerHTML= theValue;
  }
  else
  alert("MontDiagram: content or container is null");
}

MonthDiagram.prototype.createValueObject=function(aOffer)
{
  var theOffer = aOffer;//aTopOffer.getOffer(aNo);

  var theValueObject = new ValueObject();
  theValueObject.setName(theOffer.getOfferId());
  theValueObject.setUserObject(theOffer);
  if (theOffer != null)
  {
    var thePrice = theOffer.getPrice();
    theValueObject.setSize(thePrice);
  }
  else
  {
    alert("Offer=null ")
    theValueObject.setSize(0);
  }
  theValueObject.setValue(" ");
  return theValueObject;
}





MonthDiagram.prototype.getCityNameStyle=function (aNumber)
{
   var theStyle = 'class="tabheader" style="font-family:Arial, Helvetica, sans-serif; font-size:11px; font-weight:bold; color:red;';
   theStyle+='position:absolute;';
   theStyle+=' top:'+((this.imgheight*aNumber)+this.imgTopOffset)+'px;';
   theStyle+=' left:'+(this.leftPos)+'px;"';
   return theStyle;
}


MonthDiagram.prototype.getDigramBarStyle= function (aNumber,aSub, aPrice, aScale)
{    
   theStyle+=' top:'+((this.imgheight*aNumber)+this.imgTopOffset)+'px;';
   theStyle+=' left:'+123+'px;width:'+this.scale(aPrice, aScale)+'px;height:'+this.barheight+'px;';
   theStyle+=' background-image:url('+this.getSumImage(aSub)+')';
   theStyle+=';"';
   return theStyle;
}

MonthDiagram.prototype.getSumImage=function(aSubNum)
{
	if(aSubNum==0)
  		return this.imageUrl;
 	else if (aSubNum==1)
  		return 'img/red_dot.jpg';
 	else if (aSubNum==2)
  		return 'img/green_dot.jpg';
}

MonthDiagram.prototype.scale=function(aPrice, aScale)
{
	var theLength = Math.round(aScale * aPrice);
	return theLength;
}



MonthDiagram.prototype.getScaleLabels=function(aCount, aLength, aMaxPrice, aScaleWidth)
{
	var theFontSize = 11;
	var theStyle = ' style="font-family:Arial, Helvetica, sans-serif; font-size:';
	   theStyle+=theFontSize+'px; font-weight:bold; color:gray;';
	   theStyle+='position:absolute;';
	   theStyle+=' top:'+(aScaleWidth - 10)+'px;';
	   theStyle+=' left:';
   var i=0;
   var theStyleValue;
   var theHtml='';
   var theStep = Math.round(aMaxPrice/aCount);
   var theInterval = Math.round(aLength/aCount);
   //alert(theInterval+" = interval");
   var theDistance;
   var theValue;
   for(i=0; i<=aCount; i++)
   {
   	 theValue=i*theStep;
	 theDistance=i*theInterval;
   	 theStyleValue = theStyle+(theDistance-this.calculateLableOffset(theFontSize, theValue))+'px;"';
   	 theHtml+='<p '+theStyleValue+'>'+theValue+'</p>';

   }
   return theHtml;
}

MonthDiagram.prototype.calculateLableOffset=function(aFontSize, aValue)
{
	if(aValue<10)
	{
		return 1;
	}
	if (aValue <100)
	{
		return 5;
	}
	if (aValue <1000)
	{
		return 9;
	}
	return 13;

}



function DateSelectionFilter(aDepTable, aDestTable)
{
  this.departureTable=aDepTable;
  this.destTable =aDestTable;
  this.destDate=null;
  this.depDate=null;
  this.cal = new GregorianCalendar();
}

DateSelectionFilter.prototype.setDepartureTable=function(aTable)
{
  this.departureTable=aTable;
  this.destDate=null;
  this.depDate=null;
}

DateSelectionFilter.prototype.setDestinationTable=function(aTable)
{
  this.destinationTable=aTable;
  this.destDate=null;
  this.depDate=null;
}

DateSelectionFilter.prototype.passFilter=function(aSource, aValue1)
{
  if (aSource==this.departureTable)
  {
   var theDate = aValue1.getDate();
   this.depDate=theDate;
   return true;
  }
  else
  {
    var theDDate = aValue1.getDate();
    if (this.depDate)
    {
      var theFlag = this.cal.after(theDDate, this.depDate);
      return theFlag;
    }
    return true;
  }
}


function LoadingListener(aDiagram)
{
  this.diagram=aDiagram;
  this.message = '<div style="'+this.getStyle()+'"><H1></H1></div>';
  this.message += '<div style="position:absolute; top:50px; left: 230px; width: 160px"><img src="img/wait_ajax.gif" alt=""/></div> ';
}

LoadingListener.prototype.getStyle=function()
{
  var theStyle='';
      theStyle+='position:absolute; top:0px; left: 5px; width: 60px; color:gray; vertical-align: middle;';
      theStyle+=' text-align: left; font-family:Arial, Helvetica, sans-serif; font-size:11px; height:13px;';
  return theStyle;
}

LoadingListener.prototype.requestPerformed=function(anEvent)
{
  if (anEvent.action=='STARTED')
  {
    this.depFlightRequestor = null;


    if (this.diagram.departureContentContainer == null)
    {
      this.diagram.departureContentContainer = document.getElementById(this.diagram.departureId);
    }
    if (this.diagram.destinationContentContainer == null)
    {
      this.diagram.destinationContentContainer = document.getElementById(this.diagram.destinationId);
    }
    if (anEvent.source== this.diagram.destFlightRequestor)
    {     
      this.diagram.destinationContentContainer.innerHTML=this.message;
    }
    else if (anEvent.source==this.diagram.depFlightRequestor)
    {      
      this.diagram.departureContentContainer.innerHTML=this.message;
    }
    else alert ("Waiting to Server for response....");
  }
} 
