  /**
   *  Sammlung von JavaScript-Funktionen für Bestellformular
   *  
   *  @author   Joerg Halitschke <j.halitschke@i-d.de>
   *  
   */               
  
  
  /**
   *  berechnet den Gesamtpreis für die übergebene Anzahl & Preis
   *  Stückzahl für Rabatt und Rabatt in Prozent optional   
   *  und setzt das Ergebnis in das Element "target"
   *     
   *  @author   Joerg Halitschke <j.halitschke@i-d.de>
   *  
   *  @param    field         int     Formularfeld mit der Anzahl
   *  @param    price         float   Preis
   *  @param    discountRate  float   Rabatt in Prozent
   *  @param    discountItems int     Rabatt ab wieviel Stück   
   *  @param    target        string  ID eines Elements für das Ergebnis
   *  @param    hidden        string  ID eines Form-Elements für das Ergebnis
   */             
  function calculateItems (field, price, discountRate, discountItems, target, hidden) {
    
    // Stückzahl prüfen
    var v = field.value;
    checkValue = /[0-9]+/;
    if (checkValue.test(v) == false) {
      alert('Bitte nur numerische Werte für die Stückzahl angeben.');
      field.value = 0;
      return false;
    }
    
    var t = target;
    var h = hidden;
    var p = parseFloat(price);
    
    // Sonderrabatt nur für Schulen
    if (document.forms["Bestellformular"].Bildungseinrichtung.checked == true
        &&
        discountRate > 0
        &&
        v >= discountItems)
    {
      p = p - (p * (discountRate / 100));
    }
    
    var r = v * p;
    
    document.getElementById(t).innerHTML = '&nbsp;ingesamt&nbsp;' + r.toFixed(2) + '&nbsp;&euro;';
    document.getElementById(h).value = r.toFixed(2) + ' EUR';
  }
  
  
  /**
   *  berechnet die Stückzahl und daraus die Versandkosten für die Anzahl
   *  aller Artikel und setzt das Ergebnis in das Element "target"
   *     
   *  @author   Joerg Halitschke <j.halitschke@i-d.de>
   *  
   *  @param    target    string  ID eines Elements für die Versandkosten (innerHTML)
   *  @param    hidden    string  ID eines Form-Elements für das Ergebnis
   */
  function calculateShipping (target, hidden) {
    
    var q = 0;
    
    // Felder mit Anzahl ermitteln (_Anzahl)
    for (i = 0; i < document.forms["Bestellformular"].elements.length; i++) 
    {
      if (document.forms["Bestellformular"].elements[i].type == "text"
          &&
          document.forms["Bestellformular"].elements[i].name.indexOf("_Anzahl") != -1
          &&
          document.forms["Bestellformular"].elements[i].value != NaN )
      {
        q = q + parseInt(document.forms["Bestellformular"].elements[i].value);        
      }
    }
    
    document.getElementById(hidden).value = q;
    
    var v;
    if (q <= 5) {
      v = 2;
    }
    else if (q <= 10) {
      v = 4;
    }
    else if (q >= 11) {
      v = 0;
    }
    
    // Anzeige im target-Element
    document.getElementById(target).innerHTML = v.toFixed(2) + '&nbsp;&euro;';
  }
