
var bOK = false;               // Alle Pruefungen erfolgreich
var sURL = window.location;    // URL der aktuellen Seite
var sSubject = "Eine interessante Seite bei www.pralinenideen.de";



function checkEmailAddress(str){
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str))
    return true;
  else{
    return false;
  }
  return false;
}


function checkFormName() {
  var bRet = true;
  var sErrors = "";
  if ($F("hdnForward") != 1) return bRet;
  var aRequired = $$('.required');
  aRequired.each(function(oEle) {
    var xVal = $F(oEle);
    if (xVal == "") {
      $(oEle).addClassName("error");
      if (bRet) $(oEle).focus();
      bRet = false;
      if (sErrors != "") sErrors = sErrors + ", ";
      sErrors = sErrors + oEle.alt;
    } else {
      $(oEle).removeClassName("error");
    }
  });

  var bValidMail = checkEmailAddress($F("fEMAIL"));
  if (!bValidMail) {
    bRet = false;
    $("fEMAIL").addClassName("error");
    if (sErrors.search("E-Mail") == -1) {
      if (sErrors != "") sErrors = sErrors + ", ";
      sErrors = sErrors + " E-Mail";
    }
  }
  if (!bRet) {
    $("errmsg").innerHTML = 'Bitte füllen Sie die folgenden Felder korrekt aus: ' + sErrors;
  } else{
    $("errmsg").innerHTML = "";
  }

/*
  bRet = checkEmailAddress($F("fEMAIL"));
  if (!bRet) {
    alert("Bitte geben Sie eine korrekte E-Mail-Adresse an.");
    $("fEMAIL").focus();
  }
  */
  return bRet;
}


function checkGBinput() {
  if ($F("name") == "") {
    alert("Bitte geben Sie Ihren Namen ein.");
    $("name").focus();
    return false;
  }
  if ($F("email") != "") {
    if (!checkEmailAddress($F("email"))) {
      alert("Bitte geben Sie eine korrekte E-Mail-Adresse ein.");
      $("email").focus();
      return false;
    }
  }
  if ($F("message") == "") {
    alert("Bitte geben Sie Ihre Nachricht ein.");
    $("message").focus();
    return false;
  }
  return true;
}


function mailURL() {
  bOK = true;
  // Bei Formular mit Mailadresse die folgende Zeile einblenden
  //checkEmailAddress(document.eMailer.address);

  if (bOK) {
    window.location = "mailto:?subject=" + encodeURI(sSubject) + "&body=" + encodeURI(document.title) + "<br> " + encodeURI(sURL);
  }
}

  function hideAddQuantity() {
    Effect.BlindUp("additionalarticle", { duration: 0.2 });
  }
  
  function showAddQuantity(iZusatzLagen) {
    var sText = "";
    if (iZusatzLagen == 1) {
      sText = "1 Lage";
    } else if (iZusatzLagen > 1) {
      sText = iZusatzLagen + " Lagen";
    } else {
      sText = "keine Lagen";
    }
    $("additional_quantity").innerHTML = sText;
    if ($("additionalarticle").visible()) {
    } else {
      Effect.BlindDown("additionalarticle", { duration: 0.4 });
    }
  }

  function hideSpecialOffer() {
    Effect.BlindUp("specialoffer", { duration: 0.2 });
  }

  function showSpecialOffer(iZusatzLagen) {
    var sText = "";
    if (iZusatzLagen == 1) {
      sText = "1 Päckchen";
    } else if (iZusatzLagen > 1) {
      sText = iZusatzLagen + " Päckchen";
    } else {
      sText = "kein Päckchen";
    }
    $("specialoffer_quantity").innerHTML = sText;
    if ($("specialoffer").visible()) {
    } else {
      Effect.BlindDown("specialoffer", { duration: 0.4 });
    }
  }
  
  function hideAddQuantityHint() {
    Effect.BlindUp("additionalarticlehint", { duration: 0.2 });
  }

  function showAddQuantityHint(iZusatzLagen, fFehlSumme) {
    var sText = "";
    var sFehlSumme = "";
    if (iZusatzLagen == 1) {
      sText = "1 Lage";
    } else if (iZusatzLagen > 1) {
      sText = iZusatzLagen + " Lagen";
    } else {
      sText = "keine Lagen";
    }
    sFehlSumme = formatZahl(fFehlSumme, 2, 2);
    $("additional_quantity_hint").innerHTML = sText;
    $("additional_hint_sum").innerHTML = sFehlSumme;
    if ($("additionalarticlehint").visible()) {
    } else {
      Effect.BlindDown("additionalarticlehint", { duration: 0.4 });
    }
  }

  function hideArticleGroup(pGrp) {
    aEle = $$('tr.art.' + pGrp);
    aEle.each(function(oEle) {
      oEle.hide();
    });
  }
  
  
  function toggleArticleGroup(pGrp) {
    // zuerst alle Gruppen schließen
    aEle = $$('td.headline');
    aEle.each(function(oEle) {
      hideArticleGroup(oEle.id);
    });

    aEle = $$('tr.art.' + pGrp);
    aEle.each(function(oEle) {
      if (oEle.visible()) {
        oEle.hide();
      } else {
        oEle.show();
      }
    });
  }
  
  
function formatZahl(zahl, k, fix)
{
    if(!k) k = 0;
    var neu = '';
    // Runden
    var f = Math.pow(10, k);
    zahl = '' + parseInt( zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;
    // Komma ermittlen
    var idx = zahl.indexOf('.');
    // fehlende Nullen einfügen
    if(fix)
    {
         zahl += (idx == -1 ? '.' : '' )
         + f.toString().substring(1);
    }
    // Nachkommastellen ermittlen
    idx = zahl.indexOf('.');
    if( idx == -1) idx = zahl.length;
    else neu = ',' + zahl.substr(idx + 1, k);

    // Tausendertrennzeichen
    while(idx > 0)
    {
        if(idx - 3 > 0)
        neu = '.' + zahl.substring( idx - 3, idx) + neu;
        else
        neu = zahl.substring(0, idx) + neu;
        idx -= 3;
    }

    return neu;
}

    function calcNewPrice(pElement) {
      var aInput = document.forms["frmOrder"].elements;
      var fPrice = fVersand;
      var iTotalQuantity = 0;
      var iSpecialOfferArticle = 0;
      for (i in aInput) {
        if (!Opera || !isNaN(i)) {
          var ele = document.forms["frmOrder"].elements[i];
          if (ele != null) {
            var sName = new String(ele.name);
            if (sName.substring(0, 4) == 'art_') {
              fAmount = ele.value;
              if (isNaN(ele.value) || parseInt(ele.value) <= 0 || parseInt(ele.value) != ele.value) {
                ele.value = "";
                document.getElementById(sName + '_sum').innerHTML = "";
              } else {
                if (document.getElementById("max_" + sName) != undefined) {
                  iMax = document.getElementById("max_" + sName).value;
                  if (parseInt(ele.value) > parseInt(iMax)) ele.value = parseInt(iMax);
                }
                fItemPrice = document.forms["frmOrder"].elements["price_" + sName].value;
                fTotalPrice = fItemPrice * parseInt(ele.value);
                document.getElementById(sName + '_sum').innerHTML = formatZahl(fTotalPrice, 2, true);
                fPrice += fTotalPrice;
                if (sName.substring(0, 5) != 'art_9') {
                  iTotalQuantity += parseInt(ele.value);
                }
                if (sSpecialOffer.indexOf(sName.substring(4, 10)) >= 0) {
                  iSpecialOfferArticle += parseInt(ele.value);
                }
              }
            }
          }
        }
      }
      document.getElementById('gesamtsumme').innerHTML = formatZahl(fPrice, 2, true);
      var fFehlSumme = getFehlSummeAdditionalQuantity(iTotalQuantity, fPrice);
      var iZusatzLagen = getAdditionalQuantity(iTotalQuantity, fPrice + fFehlSumme);
      if (fFehlSumme > 0) {
        showAddQuantityHint(iZusatzLagen - getAdditionalQuantity(iTotalQuantity, fPrice), fFehlSumme);
      } else {
        hideAddQuantityHint();
      }
      var iZusatzLagen = getAdditionalQuantity(iTotalQuantity, fPrice);
      if (iZusatzLagen > 0) {
        showAddQuantity(iZusatzLagen);
      } else {
        hideAddQuantity();
      }
      var iZusatzKuvertuere = getSpecialOfferQuantity(iSpecialOfferArticle);
      if (iZusatzKuvertuere > 0) {
        showSpecialOffer(iZusatzKuvertuere);
      } else {
        hideSpecialOffer();
      }

    }

