// (MS) 02.07.2009
var preiseErsetzt = false;
$(document).ready(
   function()  {
      $("#debug").hide();
      $("select").change(
         function(){
            dropDownHandling( this);
         });
   }
)
function dropDownHandling( dropDown)  {
   var aTest = /ausw/;
   if( aTest.test("'"+dropDown.id+"'")) {
      var aktAusfuehrung = 0;
      var aktAusfuehrungSich = 0;
      // Bestimmen der akt. Ausfuehrung
      var ausfOk  = /xxnoinputxx/i;
      var aktTest = /([0-9]+)/;
      var test = aktTest.exec( dropDown.id);
      aktAusfuehrung = test[0];
      aktAusfuehrungSich = test[0];
      tmpAusfuehrung = aktAusfuehrung-1;
      if( document.getElementById("hiddenausw"+tmpAusfuehrung))
         document.getElementById("hiddenausw"+tmpAusfuehrung).value = dropDown.value;
      // Bestimmen des Suchstring von 0 bis akt. Ausfuehrung
      lastKombiIndex = "";
      for( i = 1; i <= aktAusfuehrung; i++) {
         var index = $("#ausw"+i).val();
         if( index)	{
         	index = index.replace( /^[0-9]+:/, "");
         	lastKombiIndex += index+".";
     	   }
      }
      // alle DropDowns leeren und deaktivieren, von akt. Position bis max. Ausfuehrungen
      for( ; i <= maxAusfuehrungen; i++)  {
         if( $("#ausw"+i)) {
            $("#ausw"+i).removeOption(/^[0-9]*:/i);
            $("#ausw"+i).removeOption(/^$/i);
            document.getElementById("ausw"+i).readOnly = true;
            document.getElementById("ausw"+i).disabled = true;
            document.getElementById("ausw"+i).style.background = 'lightgrey ';
         }
      }
//                  $("#debug").show();
//                  $("#debug").html( "#"+dropDown.id+" '"+dropDown.options[dropDown.selectedIndex].text+"': '"+dropDown.value+"' selected #MA:"+maxAusfuehrungen+" LKI:"+lastKombiIndex+" AKT:"+aktAusfuehrung );

      // so nun die akt.Auswahl +1 mit leben füllen und aktivieren aber nur, wenn nicht akt. und max. gleich ist
      if( aktAusfuehrung < maxAusfuehrungen && !ausfOk.test(this.value)) {
         // Wenn schon mal die Preise aktualisiert wurden, dann erst mal wieder zurücksetzen
         if( eval("einzelPreis") && einzelPreis != "")  {
            $("#einzelPreis").html(einzelPreis);
         }
         if( einzelPreisNw != "")   {
            $("#einzelPreisNw").html(einzelPreisNw);
         }
         if( strAbPreis != "")   {
            $("#strAbPreis").html(strAbPreis);
         }
         if( staffelTabelle != "")  {
            $("#staffelTabelle").html(staffelTabelle);
         }
         if( lieferzeitStr != "")  {
            $("#lieferzeitStr").html(lieferzeitStr);
         }
         $.ajax({
            url:(ajaxUrl),
            data:'action=getKombis&sprache='+sprache+'&lastKombiIndex='+lastKombiIndex,
            type:'GET',
            timeout:3000,
            async: false,
            dataType:'json',
            error:function()  {
               alert('Fehler beim nachladen der Daten '+ajaxUrl);
            },
            success:function( json)  {
               aktAusfuehrung++;
               einzelJson = json;
               dropDownAktualisieren( aktAusfuehrung, einzelJson, "xxnoinputxx");
            }
         })
      }
//alert(aktAusfuehrung+" == "+maxAusfuehrungen+" && !"+ausfOk.test(dropDown.value)+" === "+dropDown.id+" -- "+aktAusfuehrungSich);
      if( aktAusfuehrungSich == maxAusfuehrungen && !ausfOk.test(this.value))   {
         getKombiKomplett( lastKombiIndex, "NORMAL");
      }
   }
}
function getAlleDropDowns( gesamtIndex) {
   // Als erstes den AJAX-Aufruf, um dann auch festzustellen obs die Kombi giKombi gibt
   $.ajax({
      url:(ajaxUrl),
      data:'action=getAlleAusfuehrungsDropDowns&sprache='+sprache+'&lastKombiIndex='+gesamtIndex+'&kundenId='+kundenId+'&kdGrpId='+kdGrpId+'&preisBasis='+preisBasis+'&basisArtikelNr='+basisArtikelNr+'&basisArtikelId='+basisArtikelId,
      type:'GET',
      async: false,
      timeout:5000,
      dataType:'json',
      error:function()  {
         //alert('Fehler beim nachladen der Daten '+ajaxUrl+" ### "+'action=getAlleAusfuehrungsDropDowns&sprache='+sprache+'&lastKombiIndex='+gesamtIndex+'&kundenId='+kundenId+'&kdGrpId='+kdGrpId+'&preisBasis='+preisBasis+'&basisArtikelNr='+basisArtikelNr+'&basisArtikelId='+basisArtikelId);
      },
      success:function(json)  {
         kombiGibts = false;
         kombiGibts = json["kombiGipts"];
         if( kombiGibts)   {
            // nü erstma die DropDowns mit leben füllen
            alleDropDowns = json["alleDropDowns"];
            for( i = 1; i <= maxAusfuehrungen; i++)  {
               // DropDown leeren
               if( $("#ausw"+i)) {
                  $("#ausw"+i).removeOption(/^[0-9]*:/i);
                  $("#ausw"+i).removeOption(/^$/i);
               }
               // DropDown neu füllen und die akt. selected Auswahlen treffen, dann noch die Hiddens korrekt füllen
               //alert( "DD: "+i+" SEL: "+alleDropDowns["ausw"+i+"select"]);
               dropDownAktualisieren( i, alleDropDowns["ausw"+i], alleDropDowns["ausw"+i+"select"])
               tmpAusfuehrung = i-1;
               if( document.getElementById("hiddenausw"+tmpAusfuehrung))
                  document.getElementById("hiddenausw"+tmpAusfuehrung).value = alleDropDowns["ausw"+i+"select"];
            }
            // nü die eigentlichen Preis-Daten, etc. aktualisieren
            preisInfos = json["preisInfos"];
            setKombiInfos( preisInfos);
         }
      }
   })
   return( kombiGibts);
}
function dropDownAktualisieren( aktAusf, jsonDaten, selectVal) {
   if( document.getElementById("ausw"+aktAusf))   {
      document.getElementById("ausw"+aktAusf).readOnly = false;
      document.getElementById("ausw"+aktAusf).disabled = false;
      document.getElementById("ausw"+aktAusf).style.background = 'white';
   }
   for( p in jsonDaten)   {
      $("#ausw"+aktAusf).addOption( jsonDaten[p]);
   }
   $("#ausw"+aktAusf).selectOptions( selectVal,true);
}
var kombiOk = false;
function getKombiKomplett( lastKombiIndex, aufruf)   {   // aufruf kann NORMAL oder BILD sein
   if( aufruf == "NORMAL") {
      changePic_dropdown( lastKombiIndex, "KOMBI");
   }
   // Hier jetzt Anhand der Auswahl die Preise und weitere Infos bestimmen und ersetzen
   $.ajax({
      url:(ajaxUrl),
      data:'action=getKombisPreis&sprache='+sprache+'&lastKombiIndex='+lastKombiIndex+'&kundenId='+kundenId+'&kdGrpId='+kdGrpId+'&preisBasis='+preisBasis+'&basisArtikelNr='+basisArtikelNr,
      type:'GET',
      timeout:5000,
      async: false,
      dataType:'json',
      error:function()  {
         //alert('Fehler beim nachladen der Daten '+ajaxUrl+" ### "+'action=getKombisPreis&sprache='+sprache+'&lastKombiIndex='+lastKombiIndex+'&kundenId='+kundenId+'&kdGrpId='+kdGrpId+'&preisBasis='+preisBasis+'&basisArtikelNr='+basisArtikelNr);
      },
      success:function(json)  {
         setKombiInfos( json);
      }
   })
}
function setKombiInfos( jsonDaten)  {
   kombiOk = jsonDaten["kombiOk"];
   //if( !kombiOk)
   //   alert( "Kombi gibts wohl nicht!"+lastKombiIndex);
   if( kombiOk)   {
      $("#ampel_gruen").hide();
      $("#ampel_gelb").hide();
      $("#ampel_rot").hide();
      var ampel = "#ampel_"+jsonDaten["ampelStr"];
      $(ampel).show();
      if( einzelPreis == "")
         einzelPreis = $("#einzelPreis").html();
      if( einzelPreisNw == "")
         einzelPreisNw = $("#einzelPreisNw").html();
      if( strAbPreis == "")
         strAbPreis = $("#strAbPreis").html();
      if( lieferzeitStr == "")
         lieferzeitStr = $("#lieferzeitStr").html();
      if( staffelTabelle == "")
         staffelTabelle = $("#staffelTabelle").html();
      if( jsonDaten["darfBestellen"] > 0)
         $("#darfBestellen").show()
      else
         $("#darfBestellen").hide()
      for( p in jsonDaten)   {
         if( p != "kombiOk" && p != "darfBestellen")
            $("#"+p).html(jsonDaten[p]);
      }
   }
}

