// (MS) 02.07.2009

/*
(DS) 13.11.2009
alle globalen Variablen, die in der Seite stehen, beginnen mit "page_"
*/
var ajax_debug = false;
var preiseErsetzt = false;
jQuery(document).ready(
   function()  {
      jQuery("#debug").hide();
      jQuery("select").change(
         function(){
            this.blur();
            dropDownHandling( this);
         });
   }
)

function dropDownHandling( dropDown)  {
   var aTest = /ausw/;
   if(! aTest.test("'"+dropDown.id+"'")) {
      return;  
   }
   page_aktAusfuehrung = 0;
   var aktAusfuehrungSich = 0;
   // Bestimmen der akt. Ausfuehrung
  
   var aktTest = /([0-9]+)/;
   var test = aktTest.exec( dropDown.id);
   page_aktAusfuehrung = test[0];
   aktAusfuehrungSich = test[0];
   tmpAusfuehrung = page_aktAusfuehrung-1;
   if( document.getElementById("hiddenausw"+tmpAusfuehrung))
      document.getElementById("hiddenausw"+tmpAusfuehrung).value = dropDown.value;
   // Bestimmen des Suchstring von 0 bis akt. Ausfuehrung
   page_lastKombiIndex = "";
   for( i = 1; i <= page_aktAusfuehrung; i++) {
      var index = jQuery("#ausw"+i).val();
      if( index)	{
      	index = index.replace( /^[0-9]+:/, "");
      	page_lastKombiIndex += index+".";
  	   }
   }
   // alle DropDowns leeren und deaktivieren, von akt. Position bis max. Ausfuehrungen
   for(; i <= page_maxAusfuehrungen; i++)  {
      if( jQuery("#ausw"+i)) {
         jQuery("#ausw"+i).removeOption(/^[0-9]*:/i);
         jQuery("#ausw"+i).removeOption(/^$/i);
         document.getElementById("ausw"+i).readOnly = true;
         document.getElementById("ausw"+i).disabled = true;
         document.getElementById("ausw"+i).style.background = 'lightgrey ';
      }
   }
//                  jQuery("#debug").show();
//                  jQuery("#debug").html( "#"+dropDown.id+" '"+dropDown.options[dropDown.selectedIndex].text+"': '"+dropDown.value+"' selected #MA:"+page_maxAusfuehrungen+" LKI:"+page_lastKombiIndex+" AKT:"+page_aktAusfuehrung );

   // so nun die akt.Auswahl +1 mit leben füllen und aktivieren aber nur, wenn nicht akt. und max. gleich ist
   var variant_id = dropDown.options[dropDown.selectedIndex].value;
   var variant_ok = (!variant_id.indexOf('xxnoinputxx') ==0);
   
   staffelrechner(0);    
   
   if( page_aktAusfuehrung < page_maxAusfuehrungen && variant_ok) {
      
      // Wenn schon mal die Preise aktualisiert wurden, dann erst mal wieder zurücksetzen
      resetPageCacheVars();//artikelDetail.js
      staffelrechner(0,'loading'); 
      callAjaxArtikelDetail(
               {action:'getKombis',lastKombiIndex:page_lastKombiIndex},
                {callback: getKombisReturn, debug:ajax_debug}
                );
      
      
   }
   if( aktAusfuehrungSich == page_maxAusfuehrungen && variant_ok)   {
      staffelrechner(0,'loading');
      getKombiKomplett( page_lastKombiIndex, "NORMAL");
   }
   
}

function getKombisReturn(json){
     page_aktAusfuehrung++;
     staffelrechner(0);
     dropDownAktualisieren( page_aktAusfuehrung, json, "xxnoinputxx");
}

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';
   }
   //alert(jsonDaten.length);
   var i;
   for(i=0;i<jsonDaten.length;i++){
      jQuery("#ausw"+aktAusf).addOption( jsonDaten[i]);
   }
   jQuery("#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
   
   callAjaxArtikelDetail(
               {action:'getKombisPreis',lastKombiIndex:lastKombiIndex},
                {callback: setKombiInfos, debug:ajax_debug}
                );
   
}
function setKombiInfos( jsonDaten)  {
   kombiOk = jsonDaten["kombiOk"];
    staffelrechner(0);
   if( !kombiOk)
      alert( "Kombi gibts wohl nicht!"+page_lastKombiIndex);
   if( kombiOk)   {
      
      var has_staffel = (typeof(page_has_staffel) != 'undefined' && page_has_staffel);
      
      var ampel = "#ampel_"+jsonDaten["ampelStr"];
      var statustext = "#statustext_"+jsonDaten["ampelStr"];
      showAmpel(ampel);
         showStatustext(statustext);

      initPageCacheVars();

      if( jsonDaten["darfBestellen"] > 0)
         showBestellEingabe();
      else
         hideBestellEingabe();
      for( p in jsonDaten)   {
         if( p != "kombiOk" && p != "darfBestellen"){
            //oberen preis nur, wenn keine staffeln:
            /*
            if(has_staffel && p =='einzelPreis')continue;
            if(has_staffel && p =='einzelPreisNw')continue;
            if(has_staffel && p =='strAbPreis')continue;
            */
            
            //oberen Preis nicht:
            if( p =='einzelPreis')continue;
            if( p =='einzelPreisNw')continue;
            if( p =='strAbPreis')continue;
            if( p == 'inhalt'){
                page_variante_inhalt = jsonDaten[p];    
            }
            if( p == 'divisor'){
                page_variante_divisor = jsonDaten[p];    
            }
            jQuery("#"+p).html(jsonDaten[p]);
         }
      }
      var menge = (el('wkmenge'))?el('wkmenge').value:1;
      staffelrechner(menge);
   }
}

/***********************************************************/
function getAlleDropDowns( gesamtIndex) {
   // Als erstes den AJAX-Aufruf, um dann auch festzustellen obs die Kombi gibt
   staffelrechner(0,'loading');
   var kombiGibts;
   callAjaxArtikelDetail(
               {action:'getAlleAusfuehrungsDropDowns',lastKombiIndex:gesamtIndex},
                {callback: function(json){
                        kombiGibts = getAlleDropDownsReturn(json);
                     }, 
                     debug:ajax_debug, wait:true}
                );
   return kombiGibts;
}

function getAlleDropDownsReturn(json){

    var i,tmpAusfuehrung;
    var kombiGibts = false;
    kombiGibts = json["kombiGipts"];
    if(!kombiGibts){
      staffelrechner(0);
      return false;
    }  
    //die DropDowns mit leben füllen
    var alleDropDowns = json["alleDropDowns"];
    for( i = 1; i <= page_maxAusfuehrungen; i++)  {
         // DropDown leeren
         if( jQuery("#ausw"+i)) {
            jQuery("#ausw"+i).removeOption(/^[0-9]*:/i);
            jQuery("#ausw"+i).removeOption(/^jQuery/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"];
    }
    //die eigentlichen Preis-Daten, etc. aktualisieren
    preisInfos = json["preisInfos"];
    setKombiInfos( preisInfos);
      
    return true;
}


