// Warenkorbsystem basierend auf Javascript
// © 1998 Interest Verlag - Author: Interest Verlag (www.interest.de)
// © 2000 mediaDIALOG (www.mediadialog.de)
//	modifiziert: Jörg-Alexander Roth
//	modifiziert: Carsten Bank
// © 2002 Fusion IT (www.fusion-it.de)
//	modifiziert: Carsten Bank (Euro-Umstellung)

// Bitte die Einschränkungen beachten:
// a) Keine Unterverzeichnisse benutzen! Frames sind kein Problem!
// b) Bei etwas älteren Browsern sind max 20 Artikel auswählbar, weil ein Server maximal 20 Cookies speichern darf!
// c) Ein Cookie darf nicht länger als 4kB sein. Also beim 'eintragen' der Artikelbez. kurz fassen (wie bei Papier auch ;o)!

var TimeString = "";

function getDate() {
        x = new Date();
        Tagname = x.getDay();
        if (Tagname == 1) Tagname = 'Montag';
        else if (Tagname == 2) Tagname = 'Dienstag';
        else if (Tagname == 3) Tagname = 'Mittwoch';
        else if (Tagname == 4) Tagname = 'Donnerstag';
        else if (Tagname == 5) Tagname = 'Freitag';
        else if (Tagname == 6) Tagname = 'Samstag';
        else if (Tagname == 7) Tagname = 'Sonntag';
        else Tagname = 'Kein Tag';
        Tag = x.getDate();
        Monat = x.getMonth() + 1;
        Jahr = x.getYear() + 1900;
	
	hours = x.getHours();
	minutes = x.getMinutes();
	seconds = x.getSeconds();

        var timeValue = "" + ((hours < 10 ) ? "0" : "") + hours
        timeValue += ((minutes < 10) ? ":0" : ":") + minutes
        timeValue += ((seconds < 10) ? ":0" : ":") + seconds

	TimeString = Tagname+", dem "+Tag+"."+Monat+"."+Jahr+" - "+timeValue;
	return TimeString;
}

// Definierung der einzelnen Rabatte
function Rabatt(staffel,menge,rab) { this.staffel = staffel;
	this.menge   = menge;
	this.rabat   = rab;
}

var rabatte = new Array;
rabatte[0] = new Rabatt("A",  3, 3);
rabatte[0] = new Rabatt("A", 10, 5);
rabatte[0] = new Rabatt("A", 50,10);
rabatte[0] = new Rabatt("A",100,20);
rabatte[0] = new Rabatt("A", 10, 2);
rabatte[0] = new Rabatt("A", 50, 5);
rabatte[0] = new Rabatt("A",100, 0);
rabatte[0] = new Rabatt("",   0, 0);

//europrodm = 1/1.95583;

// Anzeige des Rabattes
function zeigeRabatt(staffel) {
	r="Rabattstaffel "+staffel+":\n\n";

	for (i=0; rabatte[i].staffel != ""; i++) {
		if (rabatte[i].staffel == staffel) {
			r += "Ab "+rabatte[i].menge+" Stück:   "+rabatte[i].rabatt+" % Rabatt\n";
		}
  	}

	alert(r);
}

// Rabattstaffelungen berechnen
function berechneRabatt(staffel,menge) {
  rab = 0;
  for (i=0; rabatte[i].staffel != ""; i++) {
	if (rabatte[i].staffel == staffel) {
		if (rabatte[i].menge <= menge) {
			rab = rabatte[i].rabatt;
		}
	}
  }
  return rab;
}

// Die Funktion zur Ablage der Daten im Cookie
function kaufe(bnr,bez,preis,menge,rab)
{ if (menge <=0)
  {alert('Menge sollte größer als 0 sein!');
   return false;
  }
  if ((pos=preis.indexOf(','))>-1)
  { preis = preis.substring(0,pos)+'.'+preis.substring(pos+1,preis.length); }
  rabatt = berechneRabatt(rab,menge);
  if (rabatt>0)
  { bem = " ("+rabatt+" % Mengenrabatt)";
    preis = Math.floor(preis * (1.0-rabatt/100)*100.0+0.5);
  } else
  { bem = "";
    preis = Math.floor(preis*100.0+0.5);
  }
  
//  preiseuro = Math.floor(preis*europrodm);
  
  if (confirm('Lege '+menge+' '+bez+'\nBest.Nr. '+bnr+bem+'\nzum Preis von je '+zeigePreis(preis)+' Euro in Warenkorb?'))
  { 

	cookie="["+menge+"|"+rab+"|"+bnr+"|"+bez+"|"+preis+"]";

	while (cookie.indexOf(';')>-1) {
		cookie = cookie.substring(o,pos)+','+cookie.substring(pos+1,cookie.length);
	}
	pos   = document.cookie.indexOf("Warenkorb");
	start = document.cookie.indexOf("=",pos)+1;
	ende  = document.cookie.indexOf(";",pos);

	if (ende<0) {
		ende = document.cookie.length;
	}

	document.cookie = "Warenkorb="+document.cookie.substring(start,ende)+cookie;
  }
	return true;
  }


// Den Warenkorb in einer Tabelle anzeigen
function zeigeWarenkorb(flag) {
  pos   = document.cookie.indexOf("Warenkorb");
  start = document.cookie.indexOf("=",pos)+1;
  ende  = document.cookie.indexOf(";",pos);

  if (ende<0) {
	ende = document.cookie.length;
  }
  
  alles = document.cookie.substring(start,ende);
  
  if (flag==1) {
//	document.writeln('<form action="mailto:breidenbach-haiger@t-online.de?subject=Bestellung von '+getDate()+'" onSubmit="return checkFormular()" name="Bestellformular" method="post" enctype="text/plain">');
	document.writeln('<form action="warenkorb.php" name="Bestellformular" onSubmit="return checkFormular()" method="post">');
//	document.writeln('<input type="hidden" name="Pos" value="\t\tBest.-Nr.:\t\tMenge:\t\tBezeichnung:">');
	document.writeln('<input type="hidden" name="Pos" value="Best.-Nr.:\t\tMenge:\t\tBezeichnung:">');
  }

  document.writeln('<table>');
  document.writeln('<tr>');
  document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Nr.</b></font></td>');
  document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Menge</b></font></td>');
  document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Best.Nr.</b></font></td>');
  document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Bezeichnung</b></font></td>');
//  document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Preis DM</b></font></td>');
  document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Preis</b></font></td>');
  document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Befehl</b></font></td>');
  document.writeln('</tr>');
  
  pos = 0;
//  summedm = 0;
  summeeuro= 0;
  
  for (lfdnr=0;;lfdnr++) {
	pos = alles.indexOf("[",pos)+1;

	if (pos<1) {
		break;
	}
	
	sep = alles.indexOf ("|",pos);
	menge = alles.substring(pos,sep);
	pos = sep+1;
	
	sep = alles.indexOf ("|",pos);
	rab = alles.substring(pos,sep);
	pos = sep+1;
	
	sep = alles.indexOf ("|",pos);
	bnr = alles.substring(pos,sep);
	pos = sep+1;
	
	sep = alles.indexOf ("|",pos);
	bez = alles.substring(pos,sep);
	pos = sep+1;
	
//	sep = alles.indexOf ("|",pos);
//	dm  = alles.substring(pos,sep);
//	pos = sep+1;
	
	sep = alles.indexOf ("]",pos);
	euro= alles.substring(pos,sep);
	pos = sep+1;
	
//	betragdm   = eval(dm*menge);
	betrageuro = eval(euro*menge);
//	summedm   += betragdm;
	summeeuro += betrageuro;
	
//	document.writeln('<input type="hidden" name="Nr'+(lfdnr+1)+'" value="\t\t'+bnr+'\t\t\t'+menge+'\t\t'+bez+'">');
	document.writeln('<input type="hidden" name="Nr'+(lfdnr+1)+'" value="'+bnr+'\t\t\t'+menge+'\t\t'+bez+'">');

	document.writeln('<tr><td valign="top" align="right"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+(lfdnr+1)+'</font></td>');
	document.writeln('<td valign="top" align="right"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+menge+'</font></td>');
	document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+bnr+' <a href=javascript:zeigeRabatt(\''+rab+'\')">'+rab+'</a></font></td>');
	document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+bez+'</font></td>');
//	document.writeln('<td valign="top" align="right"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+zeigePreis(betragdm)+'</font></td>');
	document.writeln('<td valign="top" align="right"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+zeigePreis(betrageuro)+'</font></td>');
//	document.writeln('<td valign="top" align="right"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+zeigePreis(betragdm)+'</font></td>');
//	document.writeln('<td valign="top" align="right"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+zeigePreis(betrageuro)+'</font></td>');
	document.writeln('<td valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><a href="javascript:loescheEintrag('+lfdnr+')">Löschen</a></font></td></tr>');

  }
  
  document.writeln('<input type="hidden" name="============================================================================" value="\n">');
  document.writeln('<tr>');
  document.writeln('<td valign="top" colspan=4><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Summe</b></font></td>');
//  document.writeln('<td valign="top" align="right"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+zeigePreis(summedm)+'</font></td>');
  document.writeln('<td align="right"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">'+zeigePreis(summeeuro)+'</font></td>');
  document.writeln('<td><font size="2" face="Arial, Helvetica, sans-serif" color="#000000">&nbsp;</font></td>');
  document.writeln('</tr>');
  document.writeln('<tr>');
  document.writeln('<td colspan=6><br><br><br><br></td>');
  document.writeln('<td valign="bottom"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><input type="button" value="Warenkorb leeren!" onClick="loescheWarenkorb(0)"></font></td>');
  document.writeln('</tr></table>');

  document.writeln('<br><br>');

  document.writeln('<table>');
  document.writeln('<tr>');
  document.writeln('<td width="82" valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Vorname</b></font></td>');
  document.writeln('<td width="376" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><input type="text" name="Vorname" size="30"></font></td>');
  document.writeln('</tr><tr>');
  document.writeln('<td width="82" valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Name</b></font></td>');
  document.writeln('<td width="376" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><input type="text" name="Name" size="30"></font></td>');
  document.writeln('</tr><tr>');
  document.writeln('<td width="82" valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Stra&szlig;e und Hausnummer</b></font></td>');
  document.writeln('<td width="376" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><input type="text" name="Strasse" size="30"></font></td>');
  document.writeln('</tr><tr>');
  document.writeln('<td width="82" valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Postleitzahl</b></font></td>');
  document.writeln('<td width="376" valign="top"><font size="2" face="Arial, Helvetica, sans-serif" size="2"><input type="text" name="PLZ" maxlength="5" size="5"><b>&nbsp;&nbsp;Ort&nbsp;</b><input type="text" name="Wohnort" size="20"></font></td>');
  document.writeln('</tr>');
  document.writeln('<tr>');
  document.writeln('<td width="82" valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Kundennummer</b></font></td>');
  document.writeln('<td width="376" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><input type="text" name="Kundennummer" value="Falls vorhanden bzw. bekannt!" size="30"></font></td>');
  document.writeln('</tr>');
  document.writeln('<tr>');
  document.writeln('<td width="82" valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Telefon</b></font></td>');
  document.writeln('<td width="376" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><input type="text" name="Telefon" size="30"></font></td>');
  document.writeln('</tr>');
  document.writeln('<tr>');
  document.writeln('<td width="82" valign="top"><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><b>Zahlungsweise</b></font></td>');
  document.writeln('<td width="376" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><input type=radio name="Zahlverfahren" checked value="Nachnahme">Nachnahme (Bei Auslandslieferung und Neukunden!)<br><input type=radio name="Zahlverfahren" value="Bankeinzug">Bankeinzug (Bei vorliegender Einzugsberechtigung!)<br><input type=radio name="Zahlverfahren" value="Rechnung">Rechnung (Bei Altkunden und bekannter Bonit&auml;t!)</font></td>');
  document.writeln('</tr>');
  document.writeln('</table>');


  document.writeln('<table><tr><td><br><font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><input type="button" value="Ausdrucken!" onClick="javascript:print();">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Bestellen!"></font></td></tr></table>');

  document.writeln('</form>');
}

// Den Preis anzeigen
function zeigePreis(n) {
  n = eval(n *1);
  if (n<0) { vz="-"; n=-n; }
  else { vz=""; }
  dm = Math.floor(n/100);
  pf = n%100;
  if (pf==0) { pf="00"; }
  else if (pf<10) { pf="0"+pf; }
  return(vz+dm+","+pf);
}

// Inhalt des Cookies löschen
function loescheEintrag(nr) {
  cookieneu="";
  pos   = document.cookie.indexOf("Warenkorb");
  start = document.cookie.indexOf("=", pos)+1;
  ende  = document.cookie.indexOf(";", pos);
  if (ende<0) { ende=document.cookie.length; }
  
  alles = document.cookie.substring(start,ende);
  
  bis=0;
  for (lfdnr=0;;lfdnr++) {
    von = alles.indexOf("[",bis);
    bis = alles.indexOf("]",von);
	if (lfdnr==nr) break;
  }
  if (von>=0 && bis>=0) {
	document.cookie = document.cookie.substring(0,start+von) + document.cookie.substring(start+bis+1);
  }
  location = 'warenkorb.php';
}

// Abfrage zum löschen des Warenkorbes
function loescheWarenkorb(aHelp) {

	if (aHelp == 1) {
		document.cookie = "Warenkorb=";
	}
	else if (confirm('Wollen Sie wirklich den ganzen Warenkorb loeschen?')) {
		document.cookie = "Warenkorb=";
		location = 'warenkorb.php';
	}
}

function checkFormular() {

	alles = document.cookie.substring(start,ende);
	posa = alles.indexOf("[",pos)+1;

	if (posa<1) {
		alert("Bitte wählen Sie erst ein Produkt aus unserem Angebot! ;o)");
		return false;
	}

	if(document.Bestellformular.Vorname.value == "") {
		alert("Bitte Ihren Vornamen eingeben!");
		document.Bestellformular.Vorname.select();
		document.Bestellformular.Vorname.focus();
		return false;
	}

	if(document.Bestellformular.Name.value == "") {
		alert("Bitte Ihren Namen eingeben!");
		document.Bestellformular.Name.select();
		document.Bestellformular.Name.focus();
		return false;
	}

	if(document.Bestellformular.Strasse.value == "") {
		alert("Bitte einen Straßennamen eingeben!");
		document.Bestellformular.Strasse.select();
		document.Bestellformular.Strasse.focus();
		return false;
	}

	if(document.Bestellformular.PLZ.value == "") {
		alert("Bitte die Postleitzahl eingeben!");
		document.Bestellformular.PLZ.select();
		document.Bestellformular.PLZ.focus();
		return false;
	}
	var chkZ = 1;
	for(i=0;i<document.Bestellformular.PLZ.value.length;++i) {
		if (document.Bestellformular.PLZ.value.length < 5) chkZ = -1;
		if(document.Bestellformular.PLZ.value.charAt(i) < "0"
		   || document.Bestellformular.PLZ.value.charAt(i) > "9") chkZ = -1;
	}
	if(chkZ == -1) {
		alert("Ungültige Postleitzahl!");
		document.Bestellformular.PLZ.select();
		document.Bestellformular.PLZ.focus();
		return false;
	}

	if(document.Bestellformular.Wohnort.value == "") {
		alert("Bitte den Namen des Wohnortes eingeben!");
		document.Bestellformular.Wohnort.select();
		document.Bestellformular.Wohnort.focus();
		return false;
	}

	if(document.Bestellformular.Telefon.value != "") {
		for (var i = 0; i < document.Bestellformular.Telefon.value.length; i++) {
			var abc = document.Bestellformular.Telefon.value.substring(i, i + 1);

			if ( (abc < "0" || "9" < abc) && (abc != '.') && (abc != '/') && (abc != '\\') && (abc != '-') ) {
				alert("Bitte tragen Sie eine korrekte Telefonnummer ein.");
				document.Bestellformular.Telefon.select();
				document.Bestellformular.Telefon.focus();
				return false;
			}
		}
	}     

	if( (document.Bestellformular.Zahlverfahren[1].checked) && (document.Bestellformular.Kundennummer.value == "Falls vorhanden bzw. bekannt!") ) {
		alert("Bei Zahlung per Bankeinzug bitte Ihre Kundennummer eingeben!");
		document.Bestellformular.Kundennummer.select();
		document.Bestellformular.Kundennummer.focus();
		return false;
	}

	if( (document.Bestellformular.Zahlverfahren[2].checked) && (document.Bestellformular.Kundennummer.value == "Falls vorhanden bzw. bekannt!") ) {
		alert("Bei Zahlung auf Rechnung bitte Ihre Kundennummer eingeben!");
		document.Bestellformular.Kundennummer.select();
		document.Bestellformular.Kundennummer.focus();
		return false;
	}

//	loescheWarenkorb(1);
	return true;
}

function cPreis(dm, xldm, text) {
	if (text.indexOf('*') == -1) a = dm;
	else a = xldm;

	return a;
}

function cPreis2(dm, meddm, xldm, text) {
	if (text.indexOf('*') == -1) a = dm;
	else if (text.indexOf('**') == -1) a = meddm;
	else a = xldm;

	return a;
}
