var sExp = "" ;
var i = 0 ;
var articles = 0 ;
var ulrich = "ulrich.stein";
var peter = "peter.stein";
var admin = "admin";
var master = "webmaster";

function text2Data(sValue){
 sExp = / \n/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[br]");}
 sExp = /\n/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[]");}
 sExp = /ä/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[auml]");}
 sExp = /ö/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[ouml]");}
 sExp = /ü/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[uuml]");}
 sExp = /Ä/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[Auml]");}
 sExp = /Ö/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[Ouml]");}
 sExp = /Ü/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[Uuml]");}
 sExp = /ß/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[szlig]");}
 sExp = /é/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[eacute]");}
 sExp = /\&/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"[amp]");}
 return sValue;
}

function data2Html(sValue){
 sExp = /\[auml\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"ä");}
 sExp = /\[ouml\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"ö");}
 sExp = /\[uuml\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"ü");}
 sExp = /\[Auml\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"Ä");}
 sExp = /\[Ouml\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"Ö");}
 sExp = /\[Uuml\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"Ü");}
 sExp = /\[szlig\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"ß");}
 sExp = /\[eacute\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"é");}
 sExp = /\[amp\]/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"&");}
 return sValue;
}

function clearCode(sValue){
 sExp = /\>/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"&gt;");}
 sExp = /\</;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"&lt;");}
 sExp = /\[amp\]euro\;/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"€");}
 return sValue;
}
function lineFeed2BR(sValue){
 sExp = /\n/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"<br>");}
 sValue = sValue
}
function fullTrim( sValue ){
 sExp = /\s/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"");}
 return sValue ;
}
function trim( sValue ){
 sExp = /^\s/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"");}
 sExp = /\s$/;while(sExp.test(sValue)){sValue = sValue.replace(sExp,"");}
 return sValue ;
}
function validEMail( sValue ){
 var sMsg = "" ; 
 if( sValue == null || sValue.length == 0 || sValue.indexOf(".") == -1 || sValue.indexOf("@") == -1 || sValue.indexOf(" ") != -1 || sValue.indexOf( "$" ) != -1 || sValue.indexOf( "@" ) <= 1 || sValue.indexOf( "@" ) >= (sValue.length - 5) || sValue.indexOf( "." ) >= (sValue.length -2) ){
  sMsg = "Ihre E-Mail Adresse entspricht nicht den gültigen Regeln.\n\n";
  sMsg = sMsg + "Zulässig sind nur a-z, A-Z, 0-9, '.', '-', '_' und das '@'-Zeichen.\n" ;
  sMsg = sMsg + "Sie muß ein '@' Zeichen enthalten und mindestens einen Punkt.\n";
  sMsg = sMsg + "Die Adresse darf nicht mit einem '@' oder Punkt beginnen, bzw. enden.\n\nÜberprüfen Sie bitte ihre E-Mail-Adresse!" ;  
 }else{
  sMsg = "OK" ;
 }
 return sMsg ;
}   

function sendMailTo( sSendTo ){
 if(sSendTo == "admin"){
  document.location = "mailto:info@anton-tauscher.de";
 }else{
  document.location = "mailto:" + sSendTo + "@stein-weine.de";
 }
 return "";
}

function evaluate(sValue){
 if(sValue.indexOf("[eval ") > -1 ){
  sBefore = sValue.substring(0,sValue.indexOf("[eval "));
  sCode = sValue.substring(sValue.indexOf("[eval") + 6,sValue.indexOf("/eval]"));
  sAfter = sValue.substring(sValue.indexOf("/eval]") + 6);
  if(sAfter.indexOf("[eval ")){
   // rekursiver Aufruf
   sReturn = sBefore + eval(sCode) + evaluate(sAfter) ;
  }else{
   sReturn = sBefore + eval(sCode) + sAfter;
  }
 }else{
  sReturn = sValue;
 }
 return sReturn;
}
function formatCurrency(dPreis){
 var sPreis = dPreis + "" ;
 if(sPreis.indexOf(".") > -1){
  if(sPreis.indexOf(".") == sPreis.length - 2){
   sPreis = sPreis.replace(/\./,",") + "0";
  }else{
   sPreis = sPreis.replace(/\./,",") + "";
  }
 }else{
  sPreis = sPreis + ",00";
 }
 return sPreis;
}

// # shop
function calc(sID,dValue){
 var d = document ;
 var df = d.forms['bestellung'] ;
 if( df.Comment != undefined ){df.Comment.value = clearCode( df.Comment.value );}
 var de = df.elements["anz_" + sID];
 de.value = parseInt(de.value) + parseInt(dValue);
 if(de.value == "" || de.value == "NaN" ){
  ARTIKEL[sID]['Anzahl'] = "0" ;
  de.value = "0" ;
 }else{
  ARTIKEL[sID]['Anzahl'] = de.value ;
 }
 var dE = d.getElementById("sum_" + sID);
 if(dE != undefined){
  var dPreis = parseFloat(de.value) * parseFloat(ARTIKEL[sID]['Preis'].replace( /\,/ , "." )); 
  if(dPreis > 0){
   dE.innerHTML = formatCurrency(dPreis) + " €" ;
  }else{
   dE.innerHTML = "" ;
  }
  var dE = d.getElementById("Link");
  var sumOrder = formatCurrency(recalcShop());
  var sText = "" + buildList() + "<div class='txt_Info'>----------------------------------------- <br>";
  sText = sText + "<table class='white'><tr><td width='70px'><b>Summe:</b> </td>";
  sText = sText + "<td class='whiteRight' width='140px'><b>" + sumOrder + " €</b></td></tr></table>" ;
  sText = sText + "zuz&uuml;glich anteilige Frachtkosten<br>siehe <a href='#warenkorb'>Warenkorb</a></div>" ;
  dE.innerHTML = sText;
 }
 dE = d.getElementById("warenkorb");
 if(dE != undefined){
  var warenkorb = buildWarenkorb();
  dE.innerHTML = warenkorb;
  var df = d.forms[0].Positionen;
  if( df != undefined ){ df.value = articles ; }
 } 
 return "";
}
function getFracht( flaschen ){
 var sFracht = "0,00" ;
 if( flaschen > 0 && flaschen <= 12 ){sFracht = formatCurrency( 6 );}
 if( flaschen > 12 && flaschen <= 24 ){sFracht = formatCurrency( 12 );}
 if( flaschen > 24 && flaschen <= 36 ){sFracht = formatCurrency( 16 );}
 if( flaschen > 36 && flaschen <= 48 ){sFracht = formatCurrency( 19 );}
 if( flaschen > 48 && flaschen <= 60 ){sFracht = formatCurrency( 22 );}
 if( flaschen > 60 && flaschen <= 72 ){sFracht = formatCurrency( 24 );}
 if( flaschen > 72 && flaschen <= 84 ){sFracht = formatCurrency( 26 );}
 if( flaschen > 84 && flaschen <= 99 ){sFracht = formatCurrency( 0.30 * flaschen );}
 if( flaschen > 99 && flaschen <= 159 ){sFracht = formatCurrency( 0.25 * flaschen );}
 if( flaschen > 159 && flaschen <= 240 ){sFracht = formatCurrency( 0.20 * flaschen );}
 if( flaschen > 240 ){sFracht = formatCurrency( 0.15 * flaschen );}
 return sFracht;
}
function buildList(){
 // # Waren als Liste für Navigation berechnen
 var sText = "<ul class='level_1'><li><a href='#top'>Bestellformular</a></li>" ;
 var sSum = ""
 for(i in ARTIKEL){
  if( ARTIKEL[i]['Anzahl'] == undefined ){}else{
   if( ARTIKEL[i]['Anzahl'] > 0){
    sSum = formatCurrency( parseFloat(ARTIKEL[i]['Anzahl']) * parseFloat(ARTIKEL[i]['Preis'].replace( /\,/ , "." )) );
    sText = sText + "<li><a href='#" + i + "' id='link_" + i + "' >";
    sText = sText + "<table class='white'><tr><td class='whiteRight' width='40px'>" +  ARTIKEL[i]['Anzahl'] + "</td>";
    sText = sText + "<td> x </td><td class='white' width='50px'>" + i + "</td><td> = </td>";
    sText = sText + "<td class='whiteRight' width='75px'>" + sSum + " €</td></tr></table></a></li>" ;
   }
  }
 }
 sText = sText + "</ul>" ;
 return sText ; 
}
function buildWarenkorb(){
 var anzFlaschen = 0 ;
 var c = 0 ;
 var sSum = "" ;
 var dSum = 0 ;
 var sTotal = "" ;
 var sText = "<table><tr><td colspan=5>Sie haben folgende Artikel für eine Bestellung ausgewählt:<br> <br></td></tr>" ;
 sText = sText + "<tr>" ;
 sText = sText + "<td style='text-align:center;'>Anz.</td>" ;
 sText = sText + "<td style='text-align:center;'>Nr.</td>" ;
 sText = sText + "<td style='text-align:left;'>Bezeichnung</td>" ;
 sText = sText + "<td style='text-align:center;'>je in &euro;</td>" ;
 sText = sText + "<td style='text-align:center;'>Summe</td>" ;
 sText = sText + "</tr>" ;
 var sName = "" ;
 for(i in ARTIKEL){
  if( ARTIKEL[i]['Anzahl'] == undefined ){}else{
   if( ARTIKEL[i]['Anzahl'] > 0){
    if( i == "CD" || i == "WP01" || i == "WP02"  ){
     anzFlaschen = anzFlaschen + parseFloat("0");
    }else{
     if( i != "09S2" ){
      anzFlaschen = anzFlaschen + parseFloat(ARTIKEL[i]['Anzahl']);
     }else{
      // 1 Karton mit 24 Picollo zählt wie ein Karton mit 12 Flaschen
      anzFlaschen = anzFlaschen + (parseFloat(ARTIKEL[i]['Anzahl']) / 2 );
     }
    }
    sSum = formatCurrency( parseFloat(ARTIKEL[i]['Anzahl']) * parseFloat(ARTIKEL[i]['Preis'].replace( /\,/ , "." )) );
    dSum = dSum + parseFloat(ARTIKEL[i]['Anzahl']) * parseFloat(ARTIKEL[i]['Preis'].replace( /\,/ , "." )) ;
    c++;
    articles = c ;
    sName = ARTIKEL[i]['Name'].replace( '<span style="font-weight: normal; font-size: 0.9em;">' , '' );
    sName = sName.replace( "</span>" , "" );
    sText = sText + "<tr>" ;
    sText = sText + "<td><input type='text' name='anz_" + c + "' style='text-align:center;width:2.5em' value='" + ARTIKEL[i]['Anzahl'] + "' readonly></td>" ;
    sText = sText + "<td><input type='text' name='id_" + c + "' style='text-align:center;width:4em' value='" + i + "' readonly></td>" ;
    sText = sText + "<td><input type='text' name='name_" + c + "' style='width:19em' value='" + sName + "' readonly></td>" ;
    sText = sText + "<td><input type='text' name='preis_" + c + "' style='text-align:right;width:4em' value='" + ARTIKEL[i]['Preis'] + " &euro;' readonly></td>" ;
    sText = sText + "<td><input type='text' name='sum_" + c + "' style='text-align:right;width:4.5em' value='" + sSum + " &euro;' readonly></td>" ;
    sText = sText + "</tr>" ;
   }
  }   
 }
 var sFracht = getFracht( anzFlaschen ) ;
 sText = sText + "<tr><td colspan=5><hr style='border-style:dotted;border-color:#999;'></td></tr>" ;
 sText = sText + "<tr><td colspan=4 ><b>Summe Warenkorb:</b></td><td>";
 sText = sText + "<input name='summe' type='text'style='text-align:right;width:4.5em' value='" + formatCurrency(dSum) + " &euro;' readonly></td></tr>" ;
 sText = sText + "<tr><td colspan=4 >Anteilige Frachtkosten: " + anzFlaschen + " Fl.<input type='hidden' name='flaschen' value='"+anzFlaschen+"'></td>" ;
 sText = sText + "<td><input type='text' name='fracht' style='text-align:right;width:4.5em' value='" + sFracht + " &euro;' readonly></td></tr>" ;
 sText = sText + "<tr><td colspan=5><hr style='border-style:dotted;border-color:#999;'></td></tr>" ;
 sText = sText + "<tr><td colspan=4 ><b>Gesamtpreis</b></td><td>" ;
 sText = sText + "<input name='total' type='text'style='text-align:right;width:4.5em' value='" + formatCurrency(dSum + parseFloat(sFracht.replace( /\,/ , "." ))) + " &euro;' readonly></td></tr>" ;
 sText = sText + "<tr><td colspan='5'><br><b>Bemerkungen zur Bestellung:</b><br>(Abweichende Lieferadresse und Hinweise bitte hier vermerken)</td></tr>" ;
 sText = sText + "<tr><td colspan='5'><textarea name='Comment' cols='55' rows='8'></textarea></td></tr>" ;
 sText = sText + "</table>" ;
 return sText ;
}
function recalcShop(){
 // # Warenkorb-Summe berechnen und zurückgeben
 var sSum = 0 ;
 for(i in ARTIKEL){
  if( ARTIKEL[i]['Anzahl'] == undefined ){}else{
   sSum = sSum + ( parseFloat(ARTIKEL[i]['Anzahl']) * parseFloat(ARTIKEL[i]['Preis'].replace( /\,/ , "." )) );
  }
 }
 return sSum ; 
}

// # no use???
function WB( id ){
 getData( "wb/wb_" + id );
}

function setDATA(id,arrLbl,arrVal){
 this.INFO = new Array();
 for(al = 0;al < arrLbl.length;al++){
  eval( "s"+ arrLbl[al] + " = '"+arrVal[al]+"'; " ) ;
  this.INFO[arrLbl[al]] = arrVal[al];
 }
 DATA[id] = this.INFO ;
 return "" ;
}

