////////////////////////////////////////////////////////////////
//
// Weblication® CMS Shop
// erstellt durch Scholl Communications AG, 77694 Kehl, www.scholl.de
// erstellt mit Weblication® Content Management Server, www.weblication.de
// Stand: Mai 2004
//
// Mehrwertsteuer pro Produkt hinzugefügt - Stand: 08/2006
// von Gerhard Flatscher, Hall in Tirol, www,netgraf.at
////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////
//
// Anzupassende Variablen:
//
// Vorlage fuer den Kopf der Bestellung
var orderHeaderTpl  = '<table width="100%" border="0" cellspacing="0" cellpadding="2"><tr><td class="tdBorderBottom" width="30" align="center" valign="top">Menge</td><td class="tdBorderBottom" width="255" align="left" valign="top">Bezeichnung</td><td class="tdBorderBottom" align="right" valign="top" width="70">Einzelpreis</td><td class="tdBorderBottom" width="70" bgcolor="#F0F0F0" align="right" valign="top">Gesamt</td><td class="tdBorderBottom" align="right" style="text-align:right;padding:8px;"><a href="javascript:clearBasket();location.reload();" title="Warenkorb löschen"><img src="/shop/img/loeschen.gif" title="Warenkorb löschen" alt="Warenkorb löschen" border="0" style="border-style:solid;border-width:1px;border-color:#000000;"></a></td><td class="tdBorderBottom" align="left" valign="top" width="70">Hinweis</td></tr>';
//
// Zeilenvorlage eines Produktes in der Bestellung
var orderProductTpl  = '<tr><td class="tdBorderBottom" width="30" align="center" valign="top"><input type="text" name="productAmount" value="$amount" onBlur="setProduct(\'$productId\', this.value,\'$productUrlName\',\'\');" style="width:32px;text-align:right;padding-right:2px"></td><td class="tdBorderBottom" width="255" align="left" valign="top">$productName $productMeta2</td><td class="tdBorderBottom" align="right" valign="top" width="70">$price €</td><td class="tdBorderBottom" width="70" bgcolor="#F0F0F0" align="right" valign="top">$sum €</td><td class="tdBorderBottom" align="right" valign="top" width="35"><input type="hidden" name="product_$productRechnungVon_$productName_$productId" value="(Art.Nr. $productId) $productMeta2 $amount Stueck à $price = $sum ($productRechnungVon)"><table cellpadding="4" cellspacing="0" border="0"><tr><td><a class="textDecorationNone" href="javascript:addProduct(\'$productId\',\'1\',\'$productUrlName\',\'\');" title="Menge erhöhen"><img src="/shop/img/plus.gif" title="Menge erhöhen" alt="Menge erhöhen" border="0" style="border-style:solid;border-width:1px;border-color:#000000;"></a></td><td><a class="textDecorationNone" href="javascript:removeProduct(\'$productId\',\'1\',\'$productUrlName\',\'\');" title="Menge verringern"><img src="/shop/img/minus.gif" title="Menge verringern" alt="Menge verringern" border="0" style="border-style:solid;border-width:1px;border-color:#000000;"></a></td><td><a class="textDecorationNone" href="javascript:clearProduct(\'$productId\');" title="Produkt löschen"><img src="/shop/img/loeschen.gif" title="Produkt löschen" alt="Produkt löschen" border="0" style="border-style:solid;border-width:1px;border-color:#000000;"></a></td></tr></table></td><td>$mwst</td></tr>';
//
// Vorlage fuer die Summe der Bestellung
var orderSumNettoTpl = '<tr><td class="tdBorderBottom" class="text">&nbsp;</td><td class="tdBorderBottom">Summe Netto</td><td class="tdBorderBottom">&nbsp;</td><td bgcolor="#F0F0F0" align="right" class="tdBorderBottom">$ordersum €<input type="hidden" name="orderSum" value="$ordersum"></td><td></td><td class="tdBorderBottom">&nbsp;</td></tr>';
//
// Vorlage fuer die Summe der Mehrwertsteuer
var orderSumTaxTpl = '<tr><td></td><td></td><td></td><td><input type="hidden" name="orderSumTax" value="$ordersumtax"></td><td></td><td></td></tr>';
//
// Vorlage fuer die Summe der Bestellung
var orderSumBruttoTpl = '<tr><td class="tdBorderBottom">&nbsp;</td><td align="left" class="tdBorderBottom" style="padding-top:16px;"><b>Gesamtbetrag</b> (ohne Versandkosten)</td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom" align="right" bgcolor="#CCCCCC" style="padding-top:16px;"><b>$ordersum</b> €<input type="hidden" name="orderSum" value="$ordersum"></td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom">&nbsp;</td></tr>';
var orderSum_Teil_Tpl = '<tr><td class="tdBorderBottom">&nbsp;</td><td align="left" class="tdBorderBottom" style="padding-top:16px;">Gesamtbetrag</td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom" align="right" bgcolor="#CCCCCC" style="padding-top:16px;">$ordersum €<input type="hidden" name="orderSum" value="$ordersum"></td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom">&nbsp;</td></tr>';
//
// Vorlage fuer den Fuss der Bestellung
var orderFooterTpl  = '<tr align="left" valign="top"><td colspan="6">&nbsp;</td></tr><tr align="left" valign="top"><td colspan="6"></td></tr></table>';
//

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Werte für Trennung der Organisationen
    var orgs = new Array("gmbh", "oeav");
    var oc = orgs.length;
    var orgstext = new Array("Ich bestelle bei der Alpenvereins-Betriebe Gmbh", "Ich bestelle beim Oesterreichischen Alpenverein");
    var orgsrtext = new Array("Summe Alpenvereins-Betriebe Gmbh", "Summe Oesterreichischen Alpenverein");
  
  var bestellinit = new Array("Ich bestelle bei der Alpenvereins-Betriebe Gmbh: ", "Ich bestelle beim Oesterreichischen Alpenverein: ");
  // var bestellinit = new Array("\nIch bestelle bei der Alpenvereins-Betriebe Gmbh:\n\n", "\nIch bestelle beim Oesterreichischen Alpenverein:\n\n");
  var bestelltext = new Array("", "");
  var vergleichtext = new Array("product_"+"gmbh"+"_", "product_"+"oeav"+"_");


///////////////////////////////////////////////////////////////////////////////////////////////////
//  mwstanzeige zum Anzeigen der Mehrwertsteuer Werte
var mwstanzeige = 0;
if (mwstanzeige == 1){
	//
	// Vorlage fuer den Kopf der Bestellung
	var orderHeaderTpl  = '<table width="100%" border="0" cellspacing="0" cellpadding="2"><tr><td class="tdBorderBottom" width="30" align="center" valign="top">Menge</td><td class="tdBorderBottom" width="255" align="left" valign="top">Bezeichnung</td><td class="tdBorderBottom" align="right" valign="top" width="70">Einzelpreis</td><td class="tdBorderBottom" align="right" valign="top" width="70">Mwst</td><td class="tdBorderBottom" width="70" bgcolor="#F0F0F0" align="right" valign="top">Gesamt</td><td class="tdBorderBottom" align="right" style="text-align:right;padding:8px;"><a href="javascript:clearBasket();location.reload();" title="Warenkorb löschen"><img src="/shop/img/loeschen.gif" title="Warenkorb löschen" alt="Warenkorb löschen" border="0" style="border-style:solid;border-width:1px;border-color:#000000;"></a></td></tr>';
	//
	// Zeilenvorlage eines Produktes in der Bestellung
	var orderProductTpl  = '<tr><td class="tdBorderBottom" width="30" align="center" valign="top"><input type="text" name="productAmount" value="$amount" onBlur="setProduct(\'$productId\', this.value,\'$productUrlName\',\'\');" style="width:32px;text-align:right;padding-right:2px"></td><td class="tdBorderBottom" width="255" align="left" valign="top">$productName</td><td class="tdBorderBottom" align="right" valign="top" width="70">$price €</td><td class="tdBorderBottom" align="right" valign="top" width="70">$mwst €</td><td class="tdBorderBottom" width="70" bgcolor="#F0F0F0" align="right" valign="top">$sum €</td><td class="tdBorderBottom" align="right" valign="top" width="35"><input type="hidden" name="product_$productName_$productId" value="(Art.Nr. $productId) Mwst:$mwst $productRechnungVon $productMeta2 $amount Stueck à $price = $sum"><table cellpadding="4" cellspacing="0" border="0"><tr><td><a class="textDecorationNone" href="javascript:addProduct(\'$productId\',\'1\',\'$productUrlName\',\'\');" title="Menge erhöhen"><img src="/shop/img/plus.gif" title="Menge erhöhen" alt="Menge erhöhen" border="0" style="border-style:solid;border-width:1px;border-color:#000000;"></a></td><td><a class="textDecorationNone" href="javascript:removeProduct(\'$productId\',\'1\',\'$productUrlName\',\'\');" title="Menge verringern"><img src="/shop/img/minus.gif" title="Menge verringern" alt="Menge verringern" border="0" style="border-style:solid;border-width:1px;border-color:#000000;"></a></td><td><a class="textDecorationNone" href="javascript:clearProduct(\'$productId\');" title="Produkt löschen"><img src="/shop/img/loeschen.gif" title="Produkt löschen" alt="Produkt löschen" border="0" style="border-style:solid;border-width:1px;border-color:#000000;"></a></td></tr></table></td></tr>';
	//
	// Vorlage fuer die Summe der Bestellung
	var orderSumNettoTpl = '<tr><td class="tdBorderBottom" class="text">&nbsp;</td><td class="tdBorderBottom">Summe Netto</td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom">&nbsp;</td><td bgcolor="#F0F0F0" align="right" class="tdBorderBottom">$ordersum €<input type="hidden" name="orderSum" value="$ordersum"></td><td class="tdBorderBottom">&nbsp;</td></tr>';
	//
	// Vorlage fuer die Summe der Mehrwertsteuer
	var orderSumTaxTpl = '<tr><td>&nbsp;</td><td align="left" >enthaltene MwSt.</td><td></td><td></td><td bgcolor="#F0F0F0" align="right" style="padding-right:12px;">$ordersumtax €<input type="hidden" name="orderSumTax" value="$ordersumtax"></td><td>&nbsp;</td></tr>';
	//
	// Vorlage fuer die Summe der Bestellung
	var orderSumBruttoTpl = '<tr><td class="tdBorderBottom">&nbsp;</td><td align="left" class="tdBorderBottom" style="padding-top:16px;">Gesamtbetrag (inkl. MwSt.)</td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom" align="right" bgcolor="#CCCCCC" style="padding-top:16px;">$ordersum €<input type="hidden" name="orderSum" value="$ordersum"></td><td class="tdBorderBottom">&nbsp;</td></tr>';
	//
	// Vorlage fuer den Fuss der Bestellung
	var orderFooterTpl  = '<tr align="left" valign="top"><td colspan="5">&nbsp;</td></tr><tr align="left" valign="top"><td colspan="6"></td></tr></table>';
	//
	}
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Sind Preise Brutto [0|1] (0=Nettopreise, 1=Bruttopreise)
var productPricesBrutto = 1;   // NICHT MEHR AKTIV - "0" zeigt nettopreise im Warenkorb an!
//  KONFIGURATION DER BRUTTO/NETTO-PREISE JETZT IN DER wConf.xml Parameter: preise=(brutto/netto)
//
// Mindestbestellwert
var minOrderValue = 0;
//
// Ausgabe Mindestbestellwert
var minOrderTpl   = '<span style="color:#FF0000;">Der Mindestbestellwert beträgt ' + minOrderValue + ',00 €!</span><br><br>';
//
// Ausgabe Mindestbestellwert in JavaScript Dialogfenster 
var minOrderAlert = 'Der Mindestbestellwert beträgt ' + minOrderValue + ',00 €!';
//
// Versandkostenmatrix           (Versandkosten, Wertuntergrenze, Wertobergrenze)
var freightCharges    = new Array()
    freightCharges[0] = new Array(0       		  ,  0              , 10);
    freightCharges[1] = new Array(0      	      , 10              , 20);
    freightCharges[2] = new Array(0      	      , 20              , 999999);
//
//
//  Ausgabe der Versandkosten ///////////////////////////////////////////
var versandkostenanzeige = 0;
if (versandkostenanzeige == 1){
   var freightChargesTpl = '<tr><td class="tdBorderBottom">&nbsp;</td><td align="left" class="tdBorderBottom">Versandkosten</td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom">&nbsp;</td><td class="tdBorderBottom" align="right" bgcolor="#F0F0F0">$freightCharges €</td><td class="tdBorderBottom">&nbsp;</td></tr>';
   }
   else {
        var freightChargesTpl = '<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
        }
//////////////////////////////////////////////////////////////////////////
//
// Mehrwehrtsteuersatz in Prozent
var taxValuePercent = 0;
//
// Anzeige von Preisen ohne Nachkommaangabe
var stringNoCent = ".00";
//
// Kleinster Wert auf den bei der MwSt gerundet werden soll
var roundValue = 0.01;
//
//erscheint wenn ein Pflichtfeld nicht ausgefüllt wurde
var messageObligation = " ist ein Pflichtfeld!\n"; 
//
//Id des Shops
var shopId = 'AV-Shop';
////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////
//
// Globale Variablen (nicht ändern)
//
////////////////////////////////////////////////////////////////
//
var isMinOrderValue = "";
//
////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////
//
// Beschreibung: Zeigt den Inhalt des Warenkorbs an
//
////////////////////////////////////////////////////////////////

function showProducts(){
  showBasketInfo();
}

////////////////////////////////////////////////////////////////
//
// Beschreibung: Zeigt den Inhalt des Warenkorbs an
//
////////////////////////////////////////////////////////////////

function getBasketInfo(info){
  return showBasketInfo(info, 'return');
}






////////////////////////////////////////////////////////////////
//
// Beschreibung: Zeigt Informationen zum Warenkorb an
//
////////////////////////////////////////////////////////////////

  function showBasketInfo(info, option){

    var productStr = getCookie(shopId);
    var productRowTmp;
    var numberProducts = 0;
    var numberProductsTimeout = 64;

    if(! info){
      info = "";
    }  
    
    var entry = productStr;
  
    var productId      = "";
    var productName    = "";
    var productUrlName = "";
    var productRechnungVon   = "";
    var productMeta2   = "";
    var productAmount  = 1;
    var productPrice   = 0;    
    var productMwst    = 0;  
    var productSum     = 0;
    var productSumMwst = 0;
    var orderSum       = 0;
    var orderSumMwst   = 0;
    var orderSumNetto  = 0;
    var orderSumBrutto = 0;    
    var orderSumTax    = 0;
    var productPricePrint = 0;
    var productMwstPrint = 0;
    var productSumPrint = 0;
    var productMwstSum = 0;

    var orgDabei = new Array(oc);
    var orgSum = new Array(oc);


    if(info == ""){
       for(var i = 0; i < oc; i++) {
          orgDabei[i] = productStr.search("-__"+orgs[i]+"~");
          orgSum[i] = 0;
       }
    }

    if(info == ""){                      
      document.write(orderHeaderTpl);    
}


for(var o = 0; o < oc; o++) {

    if (orgDabei[o] == -1) continue;

    if(info == ""){                      
       document.write("<tr><td colspan='6'><br><h3>"+orgstext[o]+"<h3></td></tr>");
    }  
  
var products = productStr.split('~~~');
    
    
    for(var i = products.length - 1; i >= 0; i--){

      if(products[i] != ''){
        entry      = products[i].replace(/p:/, '');    
        //alert(entry);  
      
        productId     = entry.replace(/^([^\~]+)\~___.*/, '$1');
        if(productId == entry){
          productId = "";
        }
        productAmount = entry.replace(/.*\~___([^\~]+)\~__-.*/, '$1');    
        if(productAmount == entry){
          productAmount = "";
        }   
           
        productName   = entry.replace(/.*\~__-([^\~]+)\~_-_.*/, '$1');
        if(productName == entry){
          productName = "";
        }
        else
        {
         productName = productName.replace('%26','&');
// Einfuegung LTh 7.6.2007, damit auch " im productName funktionieren
         productName = productName.replace(/"/g,'\'');
        }

        productUrlName   = entry.replace(/.*\~__-([^\~]+)\~_-_.*/, '$1');
        if(productUrlName == entry){
          productUrlName = "";
        } 
        else
        {
         productUrlName = escape(productUrlName);
         productUrlName = productUrlName.replace('%27','\\\'');
        }
        
        productPrice  = entry.replace(/.*\~_-_([^\~]+)\~_--.*/, '$1');    
        if(productPrice == entry){
          productPrice = "";
        }    

        productMwst  = entry.replace(/.*\~_--([^\~]+)\~-__.*/, '$1');    
        if(productMwst == entry){
          productMwst = "";
        }    


        productRechnungVon  = entry.replace(/.*\~-__([^\~]+)\~-_-.*/, '$1');    
        if(productRechnungVon == entry){
          productRechnungVon = "";
        } 
	if (productRechnungVon != orgs[o] ) continue;


        productMeta2  = entry.replace(/.*\~-_-([^\~]+)\~--_.*/, '$1');    
        if(productMeta2 == entry){
          productMeta2 = "";
        } 

        if(productMwst != ""){
          productMwst  = productMwst.replace(/\,/, ".");
          productMwst  = Math.round(parseFloat(productMwst)* 100)/100;
          productMwstSum    = Math.round(productMwst * parseFloat(productAmount) * 100)/100;;
        }

        if(productPrice != ""){
          productPrice  = productPrice.replace(/\,/, ".");
          productPrice  = Math.round(parseFloat(productPrice)* 100)/100;
          productSum    = Math.round(productPrice * parseFloat(productAmount) * 100)/100;;

//////////////////////////////////////////////////////////////////////////////////////////////
// Summenbildung enthaltene Mwst
          productSumMwst    = Math.round(productMwst * parseFloat(productAmount) * 100)/100;;
//////////////////////////////////////////////////////////////////////////////////////////////
        }

        if(entry != "" && productPrice.toString() != ""){
          productPricePrint  = convertPrice(productPrice);    
          productMwstPrint  = convertPrice(productMwst);    
          productSumPrint    = convertPrice(productSum);        
          productRowTmp = orderProductTpl.replace(/\$productId/g, productId);
          productRowTmp = productRowTmp.replace(/\$productName/g, productName);
          productRowTmp = productRowTmp.replace(/\$productUrlName/g, productUrlName);
          productRowTmp = productRowTmp.replace(/\$amount/g, productAmount);
          productRowTmp = productRowTmp.replace(/\$price/g, productPricePrint);
	  if(productMwst > 0) {
             productRowTmp = productRowTmp.replace(/\$mwst/g, "Betrag enth&auml;lt "+productMwst+"% Umsatzsteuer.");
	  }
	  else {
             productRowTmp = productRowTmp.replace(/\$mwst/g, "Betrag enth&auml;lt keine Umsatzsteuer.");
	  }
          productRowTmp = productRowTmp.replace(/\$sum/g, productSumPrint);    
          productRowTmp = productRowTmp.replace(/\$productRechnungVon/g, productRechnungVon); 
          productRowTmp = productRowTmp.replace(/\$productMeta2/g, productMeta2);     
          if(info == ""){                    
            document.write(productRowTmp);
          }  
          orderSum += productSum;
	  orgSum[o] += productSum;
          orderSumMwst += productSumMwst;
          if(entry != ""){
            numberProducts += parseInt(productAmount);
          }              
        }      
      }
    } 


// Ausdruck Summe pro Organisation
      if(info == ""){      
        if(orgSum[o] != 0){
	  orgSum[o] = convertPrice(orgSum[o]);
          var argSumText = orderSum_Teil_Tpl.replace(/\$ordersum/g, orgSum[o]);
          argSumText = argSumText.replace(/orderSum/g, "orderSum"+o.toString());
          argSumText = argSumText.replace(/Gesamtbetrag/g, orgsrtext[o]);

          document.write(argSumText);  

        }
      }  



}


    if(productPricesBrutto == 0){
      checkMinOrderValue(orderSum * (100 + taxValuePercent) / 100);
      if(info == "" && orderSum != 0){  
        if(freightCharges.length != -1){
          orderSum = orderSum + getFreightCharge(orderSum);
        }  
      }
      orderSumNetto = orderSum;
      orderSumNetto = convertPrice(orderSumNetto);    
      orderSumNettoTpl = orderSumNettoTpl.replace(/\$ordersum/g, orderSumNetto); 

      orderSumTax = orderSum * taxValuePercent / 100;
      orderSumTax = convertPrice(orderSumTax);
      orderSumTaxTpl = orderSumTaxTpl.replace(/\$ordersumtax/g, orderSumTax);        
    
      orderSumBrutto = orderSum * (100 + taxValuePercent) / 100;
      orderSumBrutto = convertPrice(orderSumBrutto);    
      orderSumBruttoTpl = orderSumBruttoTpl.replace(/\$ordersum/g, orderSumBrutto);    

      if(info == ""){      
        if(orderSum != 0){
          document.write(getFreightChargesTpl(orderSum));  
        }
        document.write(orderSumNettoTpl);    
        document.write(orderSumBruttoTpl);
        document.write(orderSumTaxTpl);   
      }  
    }
    else{
      checkMinOrderValue(orderSum);
      var freightChargesTemplate = getFreightChargesTpl(orderSum);
      if(info == "" && orderSum != 0){ 
        if(freightCharges.length != -1){
          orderSum = orderSum + getFreightCharge(orderSum);
        }
      }
      orderSumTax = orderSum / (100 + taxValuePercent) * taxValuePercent;
////////////////////////////////////////////////////////////
// Summe der enthaltenen Mwst
// auskommentiert und direkt gesetzt in der nächsten Zeile: 
// funktioniert derzeit nur so lange nur 1 Produkt !!!!!
// Original:   orderSumTax = convertPrice(orderSumTax);
// und darauffolgende Zeile neu hinzugefügt (schreiben ind TPL)
////////////////////////////////////////////////////////////
      orderSumTax = convertPrice(productMwstSum);
      orderSumTaxTpl = orderSumTaxTpl.replace(/\$ordersumtax/g, orderSumMwst);        

      orderSumTaxTpl = orderSumTaxTpl.replace(/\$ordersumtax/g, orderSumTax);        
  
      orderSumBrutto = orderSum;
      orderSumBrutto = convertPrice(orderSumBrutto);    
      orderSumBruttoTpl = orderSumBruttoTpl.replace(/\$ordersum/g, orderSumBrutto);    

      if(info == ""){ 
        if(orderSum != 0){   
          document.write(freightChargesTemplate); 
        }   
        document.write(orderSumBruttoTpl);
        document.write(orderSumTaxTpl);    
      }  
    }    
    
    if(info == ""){                      
      document.write(orderFooterTpl);
     
      if(isMinOrderValue == false){
        document.write(minOrderTpl);
      }  
    }  
    else if(info == "numberProducts"){    
      if(option == "return"){                  
        return numberProducts;
      }
      else{
        document.write(numberProducts);      
      }  
    }      
    else if(info == "sumBrutto"){                      
      if(option == "return"){                  
        return orderSumBrutto;
      }
      else{
        document.write(orderSumBrutto);      
      }        
    }       
  }      
    
////////////////////////////////////////////////////////////////
//
// Beschreibung: Konvertiert die Preise
//
////////////////////////////////////////////////////////////////  
  
  function convertPrice(price){

    price = Math.round(price * 100)/100;  
    
    if(parseInt(roundValue) != 0){
      var convertedRoundValue = 1 / roundValue;
      price = parseInt(price * convertedRoundValue + 0.5) / convertedRoundValue;
    }

    var priceInt = parseInt(price);

    
    if(price == priceInt){
      price += stringNoCent;    
      return price;
    }
   
    var testPrice = /\.(\d+)$/;
    testPrice.test(price);
    var result = RegExp.$1;
    if(result == ""){
      price += stringNoCent;
    }
    else if(result.length == 1){
      price += "0";
    }
    return price;
  }
  



////////////////////////////////////////////////////////////////
//
// Beschreibung: Fuegt ein neues Produkt dem Warenkorb hinzu
//
////////////////////////////////////////////////////////////////

  function addProduct (productId, productAmount, productName, productPrice, productMwst, productRechnungVon, productMeta2){
 
     // Prüfen ob Feld vorhanden und eine Farbe gewählt wurde - Gerhard Flatscher 06.03.2007
     	if (document.getElementById('Farben') && document.getElementById('Farben').value == 'bitte wählen') {
			alert ("Bitte Farbe wählen!");
        	return;
			}
        
     // Prüfen ob Feld vorhanden und eine Groesse gewählt wurde  - Gerhard Flatscher 06.03.2007
     	if (document.getElementById('Groessen') && document.getElementById('Groessen').value == 'bitte wählen') {
			alert ("Bitte Größe wählen!");
        	return;
			}
        

    var productStr = getCookie(shopId);
    productName = productName.replace('%27','\'');
	// productName = productName.replace(/\+/,' ');
	productName = productName.replace(/\+/g,' ');
    
    if(!productRechnungVon){
      productRechnungVon = '';
    }
    if(!productMeta2){
      productMeta2 = '';
    }
    

    if(isNaN(productAmount) == true || productAmount < 1){
      return;
    }
    
    var regExpStr;
    var existsProduct = new RegExp(":" + productId + "~___");
    if(existsProduct.test(productStr) == true){
      var numberProductsBasket    = productStr.replace(":" + productId + "~___", ":~~~~~~~~");
      numberProductsBasket    = numberProductsBasket.replace(/.*\~\~\~\~\~\~\~\~/, "");    
      numberProductsBasket    = numberProductsBasket.replace(/^(\d+).*/, '$1');        

      var numberProductsBasketNew = parseInt(numberProductsBasket) + parseInt(productAmount);
      productStr       = productStr.replace(":" + productId + "~___", ":~~~~~~~~");
      var dollar1      = productStr.replace(/\~\~\~\~\~\~\~\~(\d+).*$/, '');
      productStr       = productStr.replace(/(.*)\~\~\~\~\~\~\~\~(\d+)/, dollar1 + productId + "~___" +  numberProductsBasketNew);
      setProducts(productStr);
    }
    else{
      var productsFirst = new RegExp("p:");

      if(productsFirst.test(productStr) == true){
        productStr = "p:" + productId + "~___" + productAmount + "~__-" + productName + "~_-_" + productPrice + "~_--" + productMwst + "~-__" + productRechnungVon + "~-_-" + productMeta2 + "~--_" + "~~~" + productStr;
      }
      else{
        productStr = "p:" + productId + "~___" + productAmount + "~__-" + productName + "~_-_" + productPrice + "~_--" + productMwst + "~-__" + productRechnungVon + "~-_-" + productMeta2 + "~--_" + "~~~";      
      }     
      setProducts(productStr);
    }
    document.location.reload();

  }  
////////////////////////////////////////////////////////////////
//
// Beschreibung: Setzt eine bestimmte Anzahl eines Produktes
//
////////////////////////////////////////////////////////////////

  function setProduct (productId, productAmount, productName, productPrice, productMwst, productRechnungVon, productMeta2){
  
    var productStr = getCookie(shopId);
    productName = productName.replace('%27','\'');
    
    var regExpStr;
    var existsProduct = new RegExp(":" + productId + "~___");
    if(existsProduct.test(productStr) == true){
      var numberProductsBasket    = productStr.replace(":" + productId + "~___", ":~~~~~~~~");
      numberProductsBasket    = numberProductsBasket.replace(/.*\~\~\~\~\~\~\~\~/, "");
      numberProductsBasket    = numberProductsBasket.replace(/^(\d+).*/, '$1');          

      var numberProductsBasketNew = parseInt(productAmount);
      productStr       = productStr.replace(":" + productId + "~___", ":~~~~~~~~");
      var dollar1      = productStr.replace(/\~\~\~\~\~\~\~\~(\d+).*$/, '');
      productStr       = productStr.replace(/(.*)\~\~\~\~\~\~\~\~(\d+)/, dollar1 + productId + "~___" +  numberProductsBasketNew);
      setProducts(productStr);
    }
    else{
      var productsFirst = new RegExp("p:");

      if(productsFirst.test(productStr) == true){
        productStr = "p:" + productId + "~___" + productAmount + "~__-" + productName + "~_-_" + productPrice + "~_--" + productMwst + "~-__" + productRechnungVon + "~-_-" + productMeta2 + "~--_" + "~~~" + productStr;
      }
      else{
        productStr = "p:" + productId + "~___" + productAmount + "~__-" + productName + "~_-_" + productPrice + "~_--" + productMwst + "~-__" + productRechnungVon + "~-_-" + productMeta2 + "~--_" + "~~~";      
      }     
      setProducts(productStr);
    }
    document.location.reload();
  }  
  
////////////////////////////////////////////////////////////////
//
// Beschreibung: Entfernt eine bestimmte Anzahl eines Produktes aus dem Warenkorb
//
////////////////////////////////////////////////////////////////

  function removeProduct (productId, productAmount){
  
    var productStr = getCookie(shopId);
    var regExpStr;
    var existsProduct = new RegExp(":" + productId + "~___");
    
    if(existsProduct.test(productStr) == true){
      productAmount    = productStr.replace(":" + productId + "~___", ":~~~~~~~~");
      productAmount    = productAmount.replace(/.*\~\~\~\~\~\~\~\~/, "");
      productAmount    = productAmount.replace(/^(\d+).*/, '$1');          
      var productAmountNew = productAmount * 2;
      productAmountNew = productAmountNew/2-1;
      if(productAmountNew > 0){
        productStr       = productStr.replace(":" + productId + "~___", ":~~~~~~~~");
        var dollar1      = productStr.replace(/\~\~\~\~\~\~\~\~(\d+).*$/, '');
        productStr       = productStr.replace(/(.*)\~\~\~\~\~\~\~\~(\d+)/, dollar1 + productId + "~___" +  productAmountNew);
      }
      else{                      
        productStr       = productStr.replace(":" + productId + "~___", "~~~~~~~~");      
        productStr       = productStr.replace(/p\~\~\~\~\~\~\~\~[^:]*\~\~\~/ , "");
      }      
      setProducts(productStr);
      document.location.reload();
    }
  }

////////////////////////////////////////////////////////////////
//
// Beschreibung: Entfernt ein Produkt aus dem Warenkorb
//
////////////////////////////////////////////////////////////////

  function clearProduct (productId){
  
    var productStr = getCookie(shopId);
    var regExpStr;
    var existsProduct = new RegExp(":" + productId + "~___");
    
    if(existsProduct.test(productStr) == true){
      productStr       = productStr.replace(":" + productId + "~___", "~~~~~~~~");      
      productStr       = productStr.replace(/p\~\~\~\~\~\~\~\~[^:]*\~\~\~/ , "");
    }      
    setProducts(productStr);
	document.location.reload();
  }  
  
////////////////////////////////////////////////////////////////
//
// Beschreibung: Schreibt den Warenkorb
//
////////////////////////////////////////////////////////////////  

function setProducts (productStr){
  document.cookie = shopId + "=" + encodeURI(productStr) + "; path=/;";
}

////////////////////////////////////////////////////////////////
//
// Beschreibung: Loescht den Warenkorb
//
////////////////////////////////////////////////////////////////  

function clearBasket (){
  
  document.cookie = shopId + "=; path=/;";
}
  
////////////////////////////////////////////////////////////////
//
// Beschreibung: Sendet die Bestellung
// 
// Parameter: form = Formular, das gesendet werden soll.
//
////////////////////////////////////////////////////////////////

function submitOrder(form, cacheMode){
  
  if(isMinOrderValue == false){
    alert(minOrderAlert);
    return;
  } 
  
	if(!form.productAmount){
	 alert ("Der Warenkorb ist leer!");
	 return false;
	}
  
  var messageEmail      = " enthält keine gültige Email Adresse!\n"; //erscheint wenn ein Pflichtfeld nicht ausgefüllt wurde
  var messageEmailBlank      = " enthält Leerzeichen in der Email Adresse!\n"; //erscheint wenn ein Pflichtfeld nicht ausgefüllt wurde

  var isObligation = /\|\w*o\w*$/i;  //Pflichtfeld, wenn Feldname am Schluss |o  enthaelt   (z.B.: "Strasse|o")
  var isEmail      = /\|\w*e\w*$/i;  //Emailfeld wenn Feldname am Schluss |e enthaelt       (z.B.: "eMail|e")
  var isCache      = /\|\w*c\w*$/i;  //Wird fuer weitere Formulare gespeichert am Schluss |c enthaelt       (z.B.: "eMail|c")
                    //Pflicht- und Emailfeld, wenn am Schluss |oe oder |eo (z.B.: "eMail|eo") 

  var checkEmail   = /.*\@.*\.\w+/i;
  var formElement;
  var formElementNameOrig;
  var alertStr = "";

  var cookieStr = "";

  for(var v=0; v<oc; v++) {
     bestelltext[v] = "";
  }
  
  for(var i = 0; i <= form.elements.length - 1; i++){
    var formElement = form.elements[i];
    if(formElement){
      if(formElement.type == "text" || formElement.type == "textarea"){
        formElementNameOrig = formElement.name.replace(/\|\w+/, ''); 
        if(isObligation.test(formElement.name) == true){  
          if(formElement.value == ""){
            alertStr += formElementNameOrig + messageObligation;                     
          }
        } 
        if(isEmail.test(formElement.name) == true){  
          if (checkEmail.test(formElement.value) == false){
            alertStr += formElementNameOrig + messageEmail;                     
          }
          else if (formElement.value.indexOf(" ") >= 0){
            alertStr += formElementNameOrig + messageEmailBlank;                     
          }
        }
        if(cacheMode == "1"){        
          if(isCache.test(formElement.name) == true){ 
            if(formElement.value != ""){
              cookieStr += "wFc_" + formElementNameOrig + "=" + formElement.value + ":";
            }
          }        
        }          
      }
      else if(formElement.type == "hidden"){
         for(var v=0; v<oc; v++) {
            if (formElement.name.search(vergleichtext[v])>-1){
	       if (bestelltext[v].length == 0) {
	          bestelltext[v] = bestellinit[v] + " \n";
	       }
               // bestelltext[v] = bestelltext[v] + formElement.name.replace(vergleichtext[v], "") + ":\n" + formElement.value + "\n\n";
               bestelltext[v] = bestelltext[v] + formElement.name.replace(vergleichtext[v], "") + ": \n" + formElement.value + " \n    \n";
	    }
	 }
      }
      else if(formElement.type == "checkbox"){
        formElementNameOrig = formElement.name.replace(/\|\w+/, '');   
        if(isObligation.test(formElement.name) == true){  
          if(formElement.checked == false){
            alertStr += formElementNameOrig + messageObligation;                     
          }
        } 
      }
    }
  }
  
  document.frmContact.rechnungen.value = "\n";

  for(var v=0; v<oc; v++) {
     document.frmContact.rechnungen.value = document.frmContact.rechnungen.value + bestelltext[v];
  }

  if(alertStr != ""){
    alert(alertStr);  
  }
  else{
    if(cacheMode == "1"){      
      cookieStr += "; path=/";    
      document.cookie = cookieStr;  
    }
    clearBasket();
// test hidden-produkt-felder auf name=dummy setzen
    var jetzt = new Date();
    var Jahr = jetzt.getFullYear();
    var Monat = jetzt.getMonth();
    var Tag = jetzt.getDate();
    var Std = jetzt.getHours();
    var Min = jetzt.getMinutes();
    var Sek = jetzt.getSeconds();
    var StdAusgabe = ((Std < 10) ? "0" + Std : Std);
    var MinAusgabe = ((Min < 10) ? "0" + Min : Min);
    var SekAusgabe = ((Sek < 10) ? "0" + Sek : Sek);
    

    for(var i = 0; i <= form.elements.length - 1; i++){
      var formElement = form.elements[i];
      if(formElement){
        if(formElement.type == "hidden"){
           for(var v=0; v<oc; v++) {
              if (formElement.name.search(vergleichtext[v])>-1){
                 formElement.name = "bestellzeit";
                 formElement.value = Jahr+"-"+Monat+"-"+Tag+" "+StdAusgabe+":"+MinAusgabe+":"+SekAusgabe;
              }
           }
        }
      }
    }

// test ende
    form.submit();
  }
}

////////////////////////////////////////////////////////////////
//
// Beschreibung: Ermittelt ein Cookie
//
////////////////////////////////////////////////////////////////  

function getCookie(name){
  var cookieStr = document.cookie;
  if(name && name != ''){
    var nameReg = new RegExp('.*' + name + '=');
    cookieStr = cookieStr.replace(nameReg, '');
    cookieStr = cookieStr.replace(/;.*/, '');    
  }
  return cookieStr;
}

function getCookie(name){
  var cookieStr = document.cookie;
  if(name && name != ''){
        
    var nameReg = new RegExp(name + '=');
    
    cookiePos = cookieStr.search(nameReg);
    
    if (cookiePos != -1){
     cookieStr = cookieStr.substring(cookiePos);   
     cookieStr = cookieStr.replace(nameReg, '');
     cookieStr = cookieStr.replace(/;.*/, '');
     cookieStr = decodeURI(cookieStr);
    }
    else cookieStr="";
  }
  return cookieStr;
}

////////////////////////////////////////////////////////////////
//
// Beschreibung: Prüft den Mindestbestellwert
//
////////////////////////////////////////////////////////////////  

function checkMinOrderValue(orderSum){
  if(orderSum < minOrderValue){
    isMinOrderValue = false;
  }
  else{
    isMinOrderValue = true;
  }
}


////////////////////////////////////////////////////////////////
//
// Beschreibung: Gibt das Template der Versandkosten zurück
//
////////////////////////////////////////////////////////////////  

function getFreightChargesTpl(orderSum){
  freightChargesTpl = freightChargesTpl.replace("$freightCharges", convertPrice(getFreightCharge(orderSum)));
  return freightChargesTpl;
}

////////////////////////////////////////////////////////////////
//
// Beschreibung: Gibt die Versandkosten zurück
//
////////////////////////////////////////////////////////////////  

function getFreightCharge(orderSum){
  for(var i = 0; i < freightCharges.length; i++){
    if(orderSum >= freightCharges[i][1] && orderSum < freightCharges[i][2]){
      return freightCharges[i][0];
    }
  }
}



