function checkEingabe()  {
//alert(document.cookie);
	var validemail = 0;
	var cmail = document.updateform.email.value;
	if (cmail.length > 6 && cmail.indexOf('@') > 0 && cmail.indexOf('@') < cmail.length - 4 && cmail.indexOf('.') > 0 && cmail.indexOf('.') < cmail.length - 2 && cmail.indexOf(' ') == -1) validemail = 1;
	if (document.updateform.name.value.length < 2)  {
		alert ("Bitte geben Sie Ihren Namen ein!");
		document.updateform.name.focus();
	}
	else if (document.updateform.vorname.value.length < 2)  {
		alert ("Bitte geben Sie Ihren Vornamen ein!");
		document.updateform.vorname.focus();
		}
		else if (document.updateform.strasse.value.length < 2)  {
			alert ("Bitte geben Sie die Strasse ein!");
			document.updateform.strasse.focus();
			}
			else if (document.updateform.plz.value.length < 4)  {
				alert ("Bitte geben Sie die PLZ ein!");
				document.updateform.plz.focus();
				}
				else if (document.updateform.ort.value.length < 2)  {
					alert ("Bitte geben Sie den Ort ein!");
					document.updateform.ort.focus();
					}
					else if (validemail == 0)  {
						alert ("Bitte geben Sie Ihre eMail-Adresse ein!");
						document.updateform.email.focus();
						}
						else if (document.updateform.konto && document.updateform.konto.value.length < 3) {
							alert ("Bitte geben Sie Ihre Kontonummer ein!");
							document.updateform.konto.focus();
							}
							else if (document.updateform.blz && document.updateform.blz.value.length < 8) {
								alert ("Bitte geben Sie Ihre Bankleitzahl ein!");
								document.updateform.blz.focus();
								}
								else if (document.updateform.blz && document.updateform.bank.value.length < 3) {
									alert ("Bitte geben Sie den Namen Ihrer Bank ein!");
									document.updateform.bank.focus();
									}
//									else alert("OK");
									else if (document.updateform.agb[0].checked != true) {
										alert ("Sie haben den AGB nicht zugestimmt!");
										document.updateform.agb[0].focus();
										}
										else {
											index = document.cookie.indexOf("TheBasket");
											document.cookie="TheBasket=.";
											document.updateform.submit();
										}
}

function buyItem(bestelltext) {
//	bestelltext = newItem;newPrice;newWeight;newQuantity;newKonfig
	newItem = bestelltext.substr(0,bestelltext.indexOf(";"));
	bestelltext = bestelltext.substr(newItem.length+1,bestelltext.length);
	newPrice = bestelltext.substr(0,bestelltext.indexOf(";"));
	bestelltext = bestelltext.substr(newPrice.length+1,bestelltext.length);
	newWeight = bestelltext.substr(0,bestelltext.indexOf(";"));
	bestelltext = bestelltext.substr(newWeight.length+1,bestelltext.length);
	newQuantity = bestelltext.substr(0,bestelltext.indexOf(";"));
	newKonfig = bestelltext.substr(newQuantity.length+1,bestelltext.length);
//	if (newQuantity <= 0) nix = alert('Die eingegebene Menge ist inkorrekt');
	if (newQuantity <= 0) newQuantity = 0;
	else {
//		if (confirm(newQuantity+' x '+newItem+' in den Einkaufskorb legen?')) {
			index = document.cookie.indexOf("TheBasket");
			countbegin = (document.cookie.indexOf("=", index) + 1);
			countend = document.cookie.indexOf(";", index);
			if (countend == -1) 	countend = document.cookie.length;
			document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newWeight+"|"+newKonfig+"|"+newQuantity+"]"
			if ((!document.cookie) || (document.cookie.indexOf("TheBasket")=="-1")) document.location.href = "nocookie.html";
//		}
	}
}

function alterError(value) {
	value = parseInt(value*100+0.5);
	if (isNaN(value) == true) value = eval('0');
	newPounds = String(value).substr(0,String(value).length-2);
	if (newPounds == '') newPounds = '0';
	if (isNaN(newPounds) == true) newPounds = eval('0');
	newPence = String(value).substr(String(value).length-2,2);
	if (isNaN(newPence) == true) newPence = eval('0');
	if (eval(newPence) <= 9) newPence='0'+eval(newPence);
	if (eval(newPence) == 0) newPence='00';
	return(newPounds+'.'+newPence);
}

function showItems() {
	var tablerowcolor = '#F0F0F0';
	index = document.cookie.indexOf("TheBasket");
	kauf = document.location.href.indexOf("kaufen.html");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) 	countend = document.cookie.length;
	fulllist = document.cookie.substring(countbegin, countend);
	totprice = 0;
	totmwst = 0;
	totweight = 0;
	discount = 0;
	if (kauf == -1) document.writeln('<form name="updateform"><div align="left">');
	else {
		document.writeln('<form name="updateform" action="cgi-bin/formularmail.pl" method="post"><div align="left">');
		document.write('<input type="hidden" name="recipient" value="bestellung"><input type="hidden" name="subject" value="Bestellung an sound-at-home">');
		document.write('<input type="hidden" name="redirect" value="../bestdanke.html">');
	}
	document.writeln('<table class="acht" width="600" border="0" cellpadding="2">');
	if (kauf == -1) document.writeln('<tr bgcolor="#F8F8F8"><td colspan="5"><span class="bold">Warenkorb:</span></td></tr>');
	else document.writeln('<tr bgcolor="#F8F8F8"><td colspan="5"><span class="bold">Bestellung bei sound@home:</span></td></tr>');
	document.writeln('<tr bgcolor="#dcdcdc"><td><span class="bold">Artikel</span></td><td align="right"><span class="bold">Menge</span></td><td align="right"><span class="bold">Einzelpreis</span></td><td align="right"><span class="bold">Gesamtpreis</span></td><td>&nbsp;</td></tr>');
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
			thisitem = 1;
		}
		else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			if (theprice == 0) {
				theweight = 0;
				thequantity = 0;
			}
			weighttotal = 0;
			weighttotal = (eval(theweight*thequantity));
			totweight = totweight + weighttotal;
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
			if (theprice != 0) {
				document.write('<tr valign="top" bgcolor="'+tablerowcolor+'"><td>'+theitem+'</td>');
				document.write('<td valign="top" align="right">'+thequantity+'</td><td valign="top" align="right">'+theprice+'</td><td valign="top" align="right">'+alterError(itemtotal)+'</td>');
				document.write('<td align="center"><a href="javascript:removeItem('+itemlist+')"><img src="bilder/del02.gif" width="16" height="16" border="0" alt="entfernen"></a></td></tr>');
				if (kauf != -1) {
					document.writeln('<input type="hidden" name="artikel'+itemlist+'" value="'+theitem1+'" size="40">');
					document.writeln('<input type="hidden" name="anzahl'+itemlist+'" value="'+thequantity+'" size="40">');
					document.writeln('<input type="hidden" name="einzelpreis'+itemlist+'" value="'+theprice+'" size="40">');
					document.writeln('<input type="hidden" name="gesamtpreis'+itemlist+'" value="'+alterError(itemtotal)+'" size="40">');
				}
			}
			if (tablerowcolor == '#F0F0F0') tablerowcolor = "#F8F8F8";
			else tablerowcolor = "#F0F0F0";
		}
		else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) {
				theitem = fulllist.substring(itemstart, i);
				//entfernt die links aus der email;
				theitem1 = theitem;
				while (theitem1.indexOf(">") > 0) {
					linkA = theitem1.indexOf("<");
					linkE = theitem1.indexOf(">");
					ersetzen = theitem1.substring(linkA,linkE+1);
					theitem1 = theitem1.replace(ersetzen,"");
				}
			}
			if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			if (thisitem==3) theweight = fulllist.substring(itemstart, i);
			thisitem++;
			itemstart=i+1;
		}
	}
	if (document.cookie.length < 28) document.write('<tr valign="top" bgcolor="'+tablerowcolor+'"><td colspan="5" valign="top" align="left">Ihr Warenkorb ist leer.</td>');
	if (document.cookie.length > 27 || kauf != -1) {
		versandArt();
		totmwst = totprice - totprice / 119e-2;
		document.writeln('<tr bgcolor="#F8F8F8"><td colspan=3 align=right>enthaltene MwSt. 19%</td><td align=right>'+alterError(totmwst)+'</td><td>&nbsp;</td></tr>');
		document.writeln('<tr bgcolor="#dcdcdc"><td><span class="bold">Total</span></td><td colspan=2 align=right>Versandgewicht: '+alterError(totweight)+'kg</td><td align=right><span class="bold">Euro '+alterError(totprice)+'</span></td><td>&nbsp;</td></tr>');
		document.writeln('<tr bgcolor="#FCFCFC"><td colspan=5>* f&uuml;r den Versand innerhalb Deutschlands. Die Versandkosten ausserhalb Deutschlands teilen wir Ihnen per email mit.</td></tr>');
		if (document.updateform.versand.options[2].selected) {
			document.writeln('<tr bgcolor="#F8F8F8"><td colspan=5><span class="red"><b>Bei Neukunden</b> werden Lastschriften <b><u>vor</u></b> dem Versand belastet (Vorkasse). Der Versand erfolgt nur,<br>'+
																			'sofern die Lastschrift nicht rückbelastet wird. Wir bitten um Verständnis für diese Vorgehensweise,<br>'+
																			'sind jedoch durch das Verhalten einiger Kunden dazu gezwungen worden.</span></td></tr>');
		}
	}
	if (kauf != -1) document.writeln('<input type="hidden" name="summe" value="'+alterError(totprice)+'" size="40">');
	if (kauf != -1) document.writeln('<input type="hidden" name="gewicht" value="'+alterError(totweight)+'" size="40">');

	document.writeln('</table><br>');
	if (kauf != -1) anschrift();
	document.writeln('</div>');
	document.writeln('</form>');
}

function removeItem(itemno) {
	newItemList = null;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			theitem = fulllist.substring(itemstart, itemend);
			itemlist=itemlist+1;
			if (itemlist != itemno) {
				newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
			}
		}
	}
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket="+newItemList;
	if (kauf == -1) self.location = "korb.html";
	else self.location.reload();
}

function clearBasket() {
	if (confirm('Möchten Sie wirklich alle Artikel aus dem Warenkorb entfernen?')) {
		index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket=.";
		self.location = "korb.html";
	}
}

function versandArt() {
	var versandkosten = 6.8;
	discount = totprice / 40;
	if (totweight < 1) versandkosten = 3.9;
	if (totweight > 4) versandkosten = ((totweight - 4) / 1.25) + versandkosten;
	if (discount < versandkosten) versandkosten = versandkosten - discount;
	else versandkosten = 0;
	va = new Array("Nachnahme","Vorkasse","Lastschrift");
	if (document.cookie.indexOf("Ship") == -1) document.cookie = "Ship=0,"+ (alterError(versandkosten + 6)) +";" + document.cookie;
	var ind = document.cookie.indexOf("Ship");
//	versandkosten = document.cookie.substring(ind+7,document.cookie.length);
	ind = document.cookie.substring(ind+5,ind+6);
	if (ind == 0) versandkosten = (alterError(versandkosten + 6));
	else versandkosten = (alterError(versandkosten));
	document.write('<tr bgcolor="#FCFCFC"><td colspan="2" align=left>Zahlung erfolgt per <select class="acht" name="versand" size="1" onChange="versandAend('+versandkosten+')"><option value="5" ' );
	if (ind == 0) document.write('selected');
	document.write('>Nachnahme</option><option value="0" ');
	if (ind == 1) document.write('selected');
	document.write('>Vorkasse</option><option value="0" ');
	if (ind == 2) document.write('selected');
	document.write('>Lastschrift</option></select></td>');
	document.write('<td align=right>Versandkosten *</td><td align=right>'+versandkosten+'</td><td>&nbsp;</td></tr>');
	document.writeln('<input type="hidden" name="bezahlung" value="'+va[ind]+'" size="15">');
	document.writeln('<input type="hidden" name="versandkosten" value="'+versandkosten+'" size="15">');
	document.cookie = "Ship=" + ind + "," + versandkosten + ";" + document.cookie;
	totprice = eval(totprice) + eval(versandkosten);
}

function versandAend(wert) {
	for (var i = 0; i < document.updateform.versand.options.length; ++i) {
		if (document.updateform.versand.options[i].selected == true) {
			versandkosten = alterError(wert + eval(document.updateform.versand.options[i].value));
			document.cookie = "Ship=" + i + "," + versandkosten + ";" + document.cookie;
			if (kauf == -1) self.location = "korb.html";
//			else self.location.reload();
			else self.location = "kaufen.html";
		}
	}
}

function anschrift() {
	document.write('<table class="acht" width="600" border=0" cellpadding="0">');
	document.write('<tr><td align="left" width="100"><span class="bold">Name:</span></td><td align="left"><input name="name" type="text" size="58" maxlength="100"></td></tr>');
	document.updateform.name.focus();
	document.write('<tr><td align="left"><span class="bold">Vorame:</span></td><td align="left"><input name="vorname" type="text" size="58" maxlength="100"></td></tr>');
	document.write('<tr><td align="left"><span class="bold">Stra&szlig;e:</span></td><td align="left"><input name="strasse" type="text" size="58" maxlength="300"></td></tr>');
	document.write('<tr><td align="left"><span class="bold">PLZ:</span></td><td align="left"><input name="plz" type="text" size="5" maxlength="10"><span class="bold">&nbsp;&nbsp;&nbsp;Ort (Land):&nbsp;</span><input name="ort" type="text" size="33" maxlength="300"></td></tr>');
	document.write('<tr><td align="left"><span class="bold">eMail:</span></td><td align="left"><input name="email" type="text" size="58" maxlength="58"></td></tr>');
	if (document.updateform.versand.options[2].selected) {
		document.write('<tr><td align="left"><span class="bold">Kontonummer:</span></td><td align="left"><input name="konto" type="text" size="20" maxlength="50"><span class="bold">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BLZ:&nbsp;</span><input name="blz" type="text" size="22" maxlength="8"></td></tr>');
		document.write('<tr><td align="left"><span class="bold">Bank:</span></td><td align="left"><input name="bank" type="text" size="58" maxlength="58"></td></tr>');
	}
	document.write('<tr><td align="left"><span class="bold">Bemerkung:</span></td><td align="left"><input name="bemerkung" type="text" size="58" value="keine"></td></tr>');
	document.write('<tr><td colspan="2" align="left">Die Lieferung sollte bevorzugt erfolgen durch <input class="radiobutton" name="lieferwunsch" type="radio" value="ups" checked>UPS  <input class="radiobutton" name="lieferwunsch" type="radio" value="post">Deutsche Post / DHL.</td></tr>');
//	document.write('<tr><td colspan="2" align="left" class="xsml"><span class="bold">Die <a href="agb.html"><span class="undl">AGB</span></a> habe ich gelesen und akzeptiere diese: <input class="radiobutton" name="agb" type="radio" value="ja">ja <input class="radiobutton" name="agb" type="radio" value="nein" checked>nein</span></td></tr>');
	document.write('<tr><td colspan="2" align="left" class="xsml"><span class="bold">Ich möchte die in meinem Einkaufswagen befindlichen Produkte zum Gesamtpreis von  '+alterError(totprice)+' Euro kaufen und akzeptiere die <a href="agb.html"><span class="undl">Allgemeinen Geschäftsbedingungen (AGB)</span></a>: <input class="radiobutton" name="agb" type="radio" value="ja">ja <input class="radiobutton" name="agb" type="radio" value="nein" checked>nein</span></td></tr>');
	document.write('</table><br>');
}
