var wk = {
 cartSite: 0,
 itemCount: 0,
 wkb: 0,
 freeze:0,
 total: 0,
 init: function(count) {
  this.itemCount=count;
  if(this.itemCount==0) {$("wk_btn").style.display="none";$("wk_price").style.display="none";}
  else this.wkb=1;
 },
 UpdateSHC: function() {
   if(this.itemCount==0 && this.cartSite) {window.location="index.php";this.freeze=1;}
   else if(this.cartSite) {window.location="index.php?modul=shop&mode=view";this.freeze=1;}
   if(this.itemCount==0 && this.wkb==1) {new Effect.Fade("wk_btn",{duration:0.5});this.wkb=0;new Effect.Fade("wk_price",{duration:0.5});}
   if(this.itemCount>0 && this.wkb==0) {new Effect.Appear("wk_btn",{duration:1.5});this.wkb=1;new Effect.Appear("wk_price",{duration:1.5});}
   if(this.total) $("wk_gp").innerHTML=numf(this.total);
  },
 add_b: function(frm,afl) {
  if(this.freeze) return;
  new Ajax.Updater('wk_items','index.php', {
   method:'post',
   evalScripts: true,
   postBody:'smp=102&frm='+dsk_b.frm+'&afl='+dsk_b.afl,
   onCreate:function(resp)   {dsk.incLoading();},
   onFailure:function(resp)  {alert('Fehler: '+resp.statusText);},
   onComplete:function(resp) {dsk.decLoading();},
   onException:function(resp,exeption) {alert('Ausnahme: '+exeption);}
   });
 },
 SubmitView: function(mode,submode,id) {
  document.shc_view.mode.value=mode;
  document.shc_view.submode.value=submode;
  document.shc_view.id.value=id;
  document.shc_view.submit();
 },
 RecalcView: function() {
  theprice = 0;
  prd.each(function(p,i) {
   if($('m'+p.id).value<1) $('m'+p.id).value=1;
   q = $('m'+p.id).value;
   bb = $('bb'+p.id).value;
   ep = p.p;
   var dcprs=dc[($('dc'+p.id).checked)?0:1];
   var bprs=0;
   if(bb>0 && bb<p.afl) {
    bprs=b[0];
    bprs+=Math.ceil(p.afl/bb)*b[1];
   }
   gp = q*ep;

   $('bb_e'+p.id).innerHTML=numf(bprs)+"&euro;";
   $('bb_g'+p.id).innerHTML=numf(q*bprs)+"&euro;";
   $('dc_e'+p.id).innerHTML=numf(dcprs)+"&euro;";
   $('dc_g'+p.id).innerHTML=numf(q*dcprs)+"&euro;";
   $('ep'+p.id).innerHTML=numf(ep)+"&euro;";
   $('gp'+p.id).innerHTML=numf(gp)+"&euro;";
   theprice+=gp+(q*bprs)+(q*dcprs);
  });
  mm = 1+(mwst/100);
  ms = (theprice/100)*mwst;
  bp = theprice*mm;
  $("g_np").innerHTML=numf(theprice)+"&euro;";
  $("g_ms").innerHTML=numf(ms)+"&euro;";
  $("g_bp").innerHTML=numf(bp)+"&euro;";
  $("wk_gp").innerHTML=numf(bp);
 },
 InitLieferkosten: function() {
  var cblf = 'cb_lf';
  var cbza = 'cb_za';

  var cbxlf = $(cblf);
  var cbxza = $(cbza);

  this.RemoveAllItems(cblf);
  this.RemoveAllItems(cbza);

  cbxlf.onchange=function() {
   var selid = this.options[this.selectedIndex].value;
   lf['dlvID']=this.options[this.selectedIndex].value;
   wk.InitLieferkosten();
  }

  cbxza.onchange=function() {
   lf['zaID']=this.options[this.selectedIndex].value;
   wk.RecalcCheckout();
  }
  for(lID in lf['dlv']) {
   var dlv = lf['dlv'][lID];
   var text = dlv['n'];
   if(text==undefined) continue;
   text+=" ("+numf(dlv['p'])+"€)";
   this.AddComboBox(cblf,lID,text,(lf['dlvID']==lID)?1:0);
   if(lf['dlvID']==lID) {
    var fID = 0;
    for(zID in dlv['zm']) {
     if(!fID) fID=zID;
     var text = dlv['zm'][zID]['n']
     if(text==undefined) continue;
     text+=" ("+numf(dlv['zm'][zID]['p'])+"€)";
     this.AddComboBox(cbza,zID,text,(lf['zaID']==zID)?1:0);
     if(lf['zaID']==zID) fID=-1;
    }
    if(fID>0) lf['zaID']=fID;
   }
  }
  this.RecalcCheckout();
 },
 RecalcCheckout: function() {
  var prs = 0;
  var k_vm = 0;
  var k_zm = 0;

  k_vm = lf['dlv'][lf['dlvID']]['p'];
  k_zm = lf['dlv'][lf['dlvID']]['zm'][lf['zaID']]['p'];

  $("k_nk").innerHTML=numf(g_nk)+"€";
  $("k_vm").innerHTML=numf(k_vm)+"€";
  $("k_zm").innerHTML=numf(k_zm)+"€";

  theprice = g_nk+k_vm+k_zm;
  mm = 1+(mwst/100);
  ms = (theprice/100)*mwst;
  bp = theprice*mm;
  $("g_np").innerHTML=numf(theprice)+"&euro;";
  $("g_ms").innerHTML=numf(ms)+"&euro;";
  $("g_bp").innerHTML=numf(bp)+"&euro;";
 },
 AddComboBox: function(id,val,text,selected) {
   var opt = document.createElement('option');
   opt.text = text;
   opt.value = val;
   opt.selected=selected;
   var chbox = document.getElementById(id);
   try {chbox.add(opt, null);} // standard version auser IE
   catch(ex) {chbox.add(opt);} // IE version
  },
 RemoveAllItems: function(id) {
   var chbox = document.getElementById(id);
   for (i = chbox.length - 1; i>=0; i--)
    chbox.remove(i);
  },
 add_p: function(afl) {
  if(this.freeze) return;
  new Ajax.Updater('wk_items','index.php', {
   method:'post',
   evalScripts: true,
   postBody:'smp=106'+'&'+$('prd_p').serialize(),
   onCreate:function(resp)   {dsk.incLoading();},
   onFailure:function(resp)  {alert('Fehler: '+resp.statusText);},
   onComplete:function(resp) {dsk.decLoading();},
   onException:function(resp,exeption) {alert('Ausnahme: '+exeption);}
   });
 },
 del: function(id) {
  if(this.freeze) return;
  new Ajax.Updater('wk_items','index.php', {
   method:'post',
   evalScripts: true,
   postBody:'smp=104&id='+id,
   onCreate:function(resp)   {dsk.incLoading();},
   onFailure:function(resp)  {alert('Fehler: '+resp.statusText);},
   onComplete:function(resp) {dsk.decLoading();},
   onException:function(resp,exeption) {alert('Ausnahme: '+exeption);}
  });
 }
}
