// Zaehlvariable fuer die Verlaengerung der Hoehe der rechtenSpalte
var k = 0;

function popup(url)
{
   newPopup = window.open(url, "", "width=700, height=500, top=50, left=50, location=yes, menubar=yes, toolbar=yes, status=yes, scrollbars=yes, resizable=yes");
}

/*
 * Gibt die Breite zurueck, nach der die Angebotsboxen
 * von alleine umbrechen (weil "#content" eben nicht
 * breit genug ist fuer alle Angebotsboxen)
 */
function getOptimaleAngebotsContainerBreite()
{
   // Breite von 'content' abzueglich (padding-left + padding-right)
   var contentWidth = xWidth("content") - 20;
   // Breite einer Angebotsbox ( inclusive 'border-left' & 'border-right', 'padding-left' & 'padding-right' UND 'margin-left' & 'margin-right' einer Angebotsbox!)
   var angebotWidth = 203;
   // Wie oft passt eine Angebotsbox in '#content'
   var angebotCount =  parseInt(contentWidth/angebotWidth);
   // + evtl. 'border-left' bzw. 'border-right' des Angebotscontainers
   var optimaleBreite = angebotCount*angebotWidth;

   return optimaleBreite;
}

/*
 * gibt "blockId" die Breite, die alle Angebotsboxen ".A"
 * in dem DIV "blockClass" benoetigen.
 * (Die eigentliche Zentrierung wird per "margin:auto;" gemacht)
 */
function centerAngebotsBlock(blockId ,blockClass)
{
   var divs = document.getElementsByTagName('div');
   var zaehler = 0;
   var angebotsBreite = 203;
   /*
    * Wir gehen nun alle DIVs durch.
    * Wenn wir auf das DIV mit dem Uebergebenen Klassennamen (blockClass)
    * stossen, zaehlen wir das Vorkommen der Angebotsboxen. Bis wir
    * auf das naechste "blockClass"-DIV stossen.
    */
   for ( var i=0 ; i<divs.length ; i++ )
   {
      if ( blockClass == divs[i].className )
      {
         // Angebotsboxen gezaehlt und schon beim 2. Block...
         if ( zaehler != 0 )
         {
            var gesamtBreite = zaehler * angebotsBreite;
            document.getElementById(blockId).style.width = gesamtBreite + "px";
            return;
         }
      }
      if ("A" == divs[i].className)
      {
         zaehler++;
      }
   }
}

/*
 * @param string  container   Die CSS-ID des zu pruefenden Containers
 * @param int     padTop      das "padding-top" des zu pruefenden Containers
 * @param int     padBot      das "padding-bottom" des zu pruefenden Containers
 *
 **/
function applyPositioningFix(container, padTop, padBot)
{
   var containerHeight = xHeight(container);
   if ( containerHeight%2 != 0 )
   {
      var newHeight = containerHeight - padTop - padBot + 1;
      document.getElementById(container).style.height = newHeight;
   }
}

// -------------- dieses Script passt die Hoehe von "rechteSpalte" automatisch an ----------------
function adjustLayout()
{
   // --------------------------------------------------------------------------------------------
   // Die Anpassung (zB Höhe & Breite) von Objekten im Quelltext
   // muss VOR der Spaltenhöhenanpassung erfolgen!
   // --------------------------------------------------------------------------------------------

   var optimaleBreite = getOptimaleAngebotsContainerBreite();

   if ( document.getElementById("aktionenC") != null )
   {
      centerAngebotsBlock("aktionenC","aktionsBlock");
   }
   if ( document.getElementById("startAC") != null )
   {
      xWidth("startAC",optimaleBreite);
   }
   if ( document.getElementById("katAC") != null )
   {
      xWidth("katAC",optimaleBreite);
   }
   if ( document.getElementById("recAG") != null )
   {
      xWidth("recAG",optimaleBreite);
   }
   if ( document.getElementById("recZG") != null )
   {
      xWidth("recZG",optimaleBreite);
   }
   if ( document.getElementById("microAG") != null )
   {
      xWidth("microAG",optimaleBreite);
   }
   if ( document.getElementById("empfehlungenC") != null )
   {
      xWidth("empfehlungenC",optimaleBreite);
   }

   // Die 2 Hoehen der 2 Haupt-Container der Produktdetails angleichen...
   if ( document.getElementById("prC") != null )
   {
      // da JavaScript ausgefuert werden kann, den Abstand wegen der (ggfs. 710px hohen) Info-Spalte der Pr-Details wegmachen
      document.getElementById("prJavaScriptAbstand").style.height = "auto";

      var prInfoHeight = xHeight("prInfo");
      var prDescHeight = xHeight("prBeschreibung") + xHeight("h1C");
      var maxHeight = Math.max(prInfoHeight, prDescHeight)
      xHeight("prInfo", maxHeight);
      xHeight("prBeschreibung", maxHeight - xHeight("h1C"));
   }

   // --------------------------------------------------------------------------------------------
   // Die Anpassung (zB Höhe & Breite) von Objekten im Quelltext
   // muss VOR der Spaltenhöhenanpassung erfolgen!
   // ------------------------------------------------------------------------------------- (Ende)

   //Ermitteln der Hoehe von "linkeSpalte" und  "content"
   var linkeSpalteHeight = xHeight("linkeSpalte");
   var rechteSpalteHeight = xHeight("rechteSpalte");
   var contentHeight = xHeight("content");

   //Ermitteln der groessten Hoehe
   var maxHeight = Math.max(linkeSpalteHeight,rechteSpalteHeight,contentHeight);

   //Zuweisen der groessten Hoehe
   xHeight("linkeSpalte",maxHeight);
   xHeight("rechteSpalte",maxHeight + 197);
   xHeight("content",maxHeight);

   applyPositioningFix("content", 15, 40);
   applyPositioningFix("linkeSpalte", 0, 0);
}

window.onload = function()
{
   xAddEventListener(window, "resize", adjustLayout, false );
   adjustLayout();
}

// dateCheck Funktion
// Diese Funktion generiert bei einer Datumseingabe per Select Box, die Select Box fuer die Tage, nach der Auswahl des Monates bzw Jahres automatisch
function dateCheck(tag_id, monat_id, jahr_id)
{
   var tag = tag_id.value;
   var monat = monat_id.value;
   var jahr = jahr_id.value;

   // Ausgewähltes Jahr ein Schaltjahr?
   var schaltjahr = false;
   var schaltjahr_string = new String(jahr / 4);
   if (-1 == schaltjahr_string.indexOf("."))
   {
      schaltjahr = true;
   }

   if(4 == monat || 6 == monat || 9 == monat || 11 == monat) // Monat mit 30 Tagen
   {
      // Loescht Tag 31 und fügt 29 und 30 neu ein
      tag_id.options[31] = null;
      tag_id.options[30] = new Option("30", "30", false, false);
      tag_id.options[29] = new Option("29", "29", false, false);
      if(tag <= 30 && tag >= 1) { tag_id.options[tag] = new Option(tag, tag, false, true); } // Selected einen vorher ausgewählten Tag innerhalb der neuen Tages"Range"
   }
   else if(02 == monat) // Februar mit 28 bzw 29 Tagen
   {
      // Je nach Schaltjahr 29 oder 28 Tage
      if( schaltjahr )
      {
         // Fuegt Tag 29 ein und loescht 30 und 31
         tag_id.options[31] = null;
         tag_id.options[30] = null;
         tag_id.options[29] = new Option("29", "29", false, false);
         if(tag <= 29 && tag >= 1) { tag_id.options[tag] = new Option(tag, tag, false, true); } // Selected einen vorher ausgewählten Tag innerhalb der neuen Tages"Range"
      }
      else
      {
         // Loescht Tag 29,30 und 31
         tag_id.options[31] = null;
         tag_id.options[30] = null;
         tag_id.options[29] = null;
         if(tag <= 28 && tag >= 1) { tag_id.options[tag] = new Option(tag, tag, false, true); } // Selected einen vorher ausgewählten Tag innerhalb der neuen Tages"Range"
      }
   }
   else  // Monat mit 31 Tagen
   {
      tag_id.options[31] = new Option("31", "31", false, false);
      tag_id.options[30] = new Option("30", "30", false, false);
      tag_id.options[29] = new Option("29", "29", false, false);
      if(tag <= 31 && tag >= 1) { tag_id.options[tag] = new Option(tag, tag, false, true); } // Selected einen vorher ausgewählten Tag innerhalb der neuen Tages"Range"
   }
}

function toggleQuestion(questionsId)
{
   $('#question_' + questionsId).toggle('blind');
   if( $('#questionImage_' + questionsId).attr('src').indexOf('GPfeil1') > -1)
   {
      $('#questionImage_' + questionsId).attr('src', $('#questionImage_' + questionsId).attr('src').replace('GPfeil1', 'GPfeil2'));
   }
   else
   {
      $('#questionImage_' + questionsId).attr('src', $('#questionImage_' + questionsId).attr('src').replace('GPfeil2', 'GPfeil1'));
   }

   return false;
}
