﻿function changeFrame(url,z_frame)
{
  top.frames[z_frame].document.location=url;
}

function alterError(value) {
	if (value<=0.99) {
		newPounds = '0';
	} else {
		newPounds = parseInt(value);
	}
	newPence = parseInt((value+.0008 - newPounds)* 100);
	if (eval(newPence) <= 9) newPence='0'+newPence;
	newString = newPounds + '.' + newPence;
	return (newString);
}


function buyItem(newItem, newPrice, newQuantity, minpers) {
	if (newQuantity <= 0) {
		rc = alert('Die eingegebene Menge ist inkorrekt');
	} else {
		if (confirm(newQuantity+' x '+newItem+' in den Einkaufskorb legen?')) {
			if (parseInt(newQuantity)<parseInt(minpers)) {
				alert("Achtung, Sie haben weniger als die Mindestmenge eingegeben. Ist das richtig? Dann schicken Sie die Bestellung ruhig so ab, wir werden Sie telefonisch oder per email kontaktieren.");
			}
			index = document.cookie.indexOf("TheBasket");
			console.log(newItem+"|"+newPrice+"|"+newQuantity);
			countbegin = (document.cookie.indexOf("=", index) + 1);
			countend = document.cookie.indexOf(";", index);
			if (countend == -1) {
				countend = document.cookie.length;
			}
			d="["+encodeURIComponent(newItem)+"|"+encodeURIComponent(newPrice)+"|"+encodeURIComponent(newQuantity)+"]";
			document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+d;
			$(".Warenkorb_gross").hide().replaceWith('<div class="Warenkorb_gross">'+htmlItems(1)+"</div>").show();
			$("div.sichtbar").hide();
			$("div#Bestellen").show();

		}
	}
}



function resetShoppingBasket() {
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket=.";

	if (document.cookie) {
			} else {
			  alert('Sie haben die Verwendung von Cookies deaktiviert.\nDer Onlineshop benötigt aber unbedingt Cookies, um Ihre Bestellung zu speichern.\nBitte aktivieren Sie in Ihrem Browser die Verwendung von Cookies und laden Sie die Shopseiten neu!');
			}
	$(".Warenkorb_gross").hide().replaceWith('<div class="Warenkorb_gross">'+htmlItems(1)+"</div>").show();
}

function printItems() {
	document.writeln("-------------------------------------------------------------------");
	index = document.cookie.indexOf("TheBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) countend = document.cookie.length;
	fulllist = document.cookie.substring(countbegin, countend);
	document.writeln(fulllist);
	document.writeln("-------------------------------------------------------------------");		
	document.writeln(fulllist.indexOf("Leergut"));
}
	
function addItem(newItem, newPrice, newQuantity) {
	if (newQuantity <= 0) {
		rc = alert('Fehler in addItem. Die eingegebene Menge ist inkorrekt');
	} else {
		
			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+"|"+newQuantity+"]";
			$(".Warenkorb_gross").hide().replaceWith('<div class="Warenkorb_gross">'+htmlItems(1)+"</div>").show();


		}
	}

function htmlItems(gross) {
	var Mindermenge=false;
	var Mindermenge_aus=false;
	var tablerowcolor = '#F0F0F0';
	tx=""
	index = document.cookie.indexOf("TheBasket");
	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;
	tx+='<TABLE BORDER=0 CELLPADDING="3" cellspacing="0">';
	tx+='<TR><TD class="tableHead" width="300">Artikel</TD>';

	
	if (gross==1) {
		tx+='<TD class="tableHead" align="center">Menge</TD>';
		tx+='<TD class="tableHead" align="center" width="60">Preis</TD>';
		tx+='<TD class="tableHead"></TD>'; //weitere Spalte für Lösch/Änderungsicons
	}
	else
	{
		tx+='<TD class="tableHead" align="center">Menge x Preis</TD>';
	}
	tx+='</TR>';

	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
					
		if (fulllist.substring(i,i+1) == '[') {
			thisitem = 1;
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
			tx+='<tr height="40"><td colspan="1">'
			if (gross!=1) {	tx+='<a href="javascript:removeItem('+itemlist+')">X</a>'+"&nbsp;"; } //im kleinen Format vorne ein X zum Löschen, kein Icon.
			if (gross==1) {	tx+=theitem+"</td>"; } //Artikelzeile, bei "groß" ganz, sonst nur 14 Zeichen
			else
			{ tx+=theitem.substr(0,14)+"..."+"</td>"; }

			if (theitem=="Mindermengenzuschlag") {
				Mindermenge=true; //Wenn Mindermengenzuschlag dabei, dann Boolsche Variable Mindermenge auf True setzen
			}
			if (theitem=="Gänsetaxi - Gans für 4 Personen") {
				Mindermenge_aus=true; //Wenn Angebot "Gänsetaxi", dann Mindermengenzuschlag ausschalten
			}
			if (gross==1) {
				tx+='<td align="center" colspan="1"><INPUT TYPE=TEXT NAME="quant'+itemlist+'" ID="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=3 style="text-align:center"> '
				tx+='<td align="right">'+theprice+'&euro;</TD>'
				}
			 else {
				tx+='<td align=right colspan="2">'+thequantity
				tx+='x '+theprice+'&euro;</TD>'
				}
			
			if (gross==1) { // Lösch und Änderungsicons hinzufügen
				tx+='<TD><a href="javascript:removeItem('+itemlist+')"><img src="BilderPartyservice/entfernen.gif" alt="Aus dem Warenkorb entfernen" style="float:right;border:0;"></a>';
				if (theitem!="Leergut Abholen") {
					tx+='<a href="javascript:amendItem('+itemlist+',eval($('
					tx+="'#quant"+itemlist+"'"+').val()))"><img src="BilderPartyservice/aktualisieren.gif" alt="Geänderte Mengenaktualisieren" style="float:right;border:0;"> </a>';
				}
				tx+="</TD>"
			}
			tx+='</tr>';
			tx+='<INPUT TYPE="hidden" NAME="'+itemlist+'" VALUE="'+"("+thequantity+"x) "+theitem+" je "+theprice+" EUR = "+alterError(itemtotal)+" EUR"+'" SIZE="40">';

		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) {
				theitem = decodeURIComponent(fulllist.substring(itemstart, i));
			}
			if (thisitem==2) {
				theprice = decodeURIComponent(fulllist.substring(itemstart, i));
			}
			thisitem++;
			itemstart=i+1;
		}
			
	}

	//Wenn kein Leergut-Abholereintrag drin ist, diesen anbieten...
	if (fulllist.indexOf("Leergut")<0) {
		tx+='<tr height="40"><td valign="middle" align="left" colspan=1 >Sollen wir das Leergut von Ihnen abholen?</td>';
		tx+='<td valign="middle" align="right" colspan=2 >10.00&#8364;</td>';
		tx+='<td valign="middle" align="right" >';
		tx+='<b><input type="button" name="order" value="Bestellen" onclick="addItem('+"'Leergut Abholen','10.00', '1')"+'"></b';
		tx+='</td></tr>';
	}  
	if ((totprice<100)&&(Mindermenge_aus==false)) {
		if (Mindermenge==false) {
			
			itemlist=itemlist+1;
			itemtotal=totprice*0.15;
			tx+=('<INPUT TYPE="hidden" NAME="'+itemlist+'" VALUE="'+"(1x) Mindermengenzuschlag 15% = "+alterError(itemtotal)+" EUR"+'" SIZE="40">');
			tx+=('<tr height="40"><td valign="top" align="left" >Mindermengenzuschlag</td>');
			tx+=('<td valign="top" align="right" colspan="2">'+alterError(itemtotal)+'&euro;</td></tr>');
			totprice=totprice+itemtotal;
		}
	}

	tx+='<tr><td class="tableHead">Gesamtsumme</td><td class="tableHead" align=right colspan="2">'+alterError(totprice)+"&euro;"+'</td>';
	if (gross==1) {tx+="<td class='tableHead'></td>"}
	tx+='</tr>';
	tx+=('<INPUT TYPE="hidden" NAME="-- GESAMTSUMME" VALUE="'+alterError(totprice)+' EUR ---------------------------------------" SIZE="40">');
	tx+=('<INPUT TYPE="hidden" NAME="BESTELLDATEN" VALUE=" " SIZE=40>');
	tx+=('<INPUT TYPE="hidden" TEST>');
	tx+=('</TABLE>');
	return tx
}

function showItems() {
	var Mindermenge=false;
	var tablerowcolor = '#F0F0F0';
	index = document.cookie.indexOf("TheBasket");
	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;
	document.writeln('<FORM action="bfinish.htm" target="_top">');
	document.writeln('<TABLE WIDTH=100% BORDER=0 CELLPADDING="2">');
	document.writeln('<TR BGCOLOR="#C0C0C0"><TD><b>Artikel</b></TD><TD align="right"><b>Menge</b></TD><TD align="right"><b>Einzelpreis</b></TD><td align="right"><b>Gesamtpreis</b></td></TR>');
	document.writeln('<TR BGCOLOR="F8F8F8"><TD colspan="4"><HR></TD></TR>');

	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
					
		if (fulllist.substring(i,i+1) == '[') {
			thisitem = 1;
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;

			document.write('<tr BGCOLOR="'+tablerowcolor+'"> <td>'+theitem+'</td>');
			//Mindermenge? Dann Variable auf True
			if (theitem=="Mindermengenzuschlag") {
				Mindermenge=true; //Wenn Mindermengenzuschlag dabei, dann Boolsche Variable Mindermenge auf True setzen
			}
			
			document.writeln('<td align=right>'+thequantity+'</td><td align=right>'+theprice+'</td><td align=right>'+alterError(itemtotal)+" EUR"+'</td></tr>');
			document.writeln('<INPUT TYPE="hidden" NAME="'+itemlist+'" VALUE="'+"("+thequantity+"x) "+theitem+" je "+theprice+" EUR = "+alterError(itemtotal)+" EUR"+'" SIZE="40">');

			if (tablerowcolor == '#F0F0F0') {
			  tablerowcolor = "#F8F8F8";
			} else {
			  tablerowcolor = "#F0F0F0";
			}

		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) {
				theitem = decodeURIComponent(fulllist.substring(itemstart, i));
			}
			if (thisitem==2) {
				theprice = decodeURIComponent(fulllist.substring(itemstart, i));
			}
			thisitem++;
			itemstart=i+1;
		}
	}

	if (totprice<100) {
		if (Mindermenge==false) {
			//addItem('Mindermengenzuschlag',alterError(totprice*0.15), '1');
			itemlist=itemlist+1;
			itemtotal=totprice*0.15;
			document.writeln('<INPUT TYPE="hidden" NAME="'+itemlist+'" VALUE="'+"(1x) Mindermengenzuschlag 15% = "+alterError(itemtotal)+" EUR"+'" SIZE="40">');
			document.write('<td valign="top" align="left" BGCOLOR="'+tablerowcolor+'">Mindermengenzuschlag</td>');
			document.write('<td valign="top" align="right" BGCOLOR="'+tablerowcolor+'">   </td>');
			document.write('<td valign="top" align="right" BGCOLOR="'+tablerowcolor+'">   </td>');
			document.write('<td valign="top" align="right" BGCOLOR="'+tablerowcolor+'">'+alterError(itemtotal)+' EUR</td>');
			totprice=totprice+itemtotal;
		}
	}

	document.writeln('<TR BGCOLOR="F8F8F8"><TD colspan="4"><HR></TD></TR>');
	document.writeln('<tr BGCOLOR="#C0C0C0"><td colspan=3><b>Gesamtsumme</b></td><td align=right><b>EUR '+alterError(totprice)+" EUR"+'</b></td></tr>');
	document.writeln('<INPUT TYPE="hidden" NAME="-- GESAMTSUMME" VALUE="'+alterError(totprice)+' EUR ---------------------------------------" SIZE="40">');
	document.writeln('<INPUT TYPE="hidden" NAME="BESTELLDATEN" VALUE=" " SIZE=40>');
	document.writeln('<INPUT TYPE="hidden" TEST>');
	document.writeln('</TABLE>');

}

function showItemsNEW() {
	var Mindermenge=false;
	var tablerowcolor = '#F8F8F8';
	index = document.cookie.indexOf("TheBasket");
	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;
	document.writeln('<div id="TEST"></div>');
	document.writeln('<FORM action="bfinish.htm" target="_top">');
//	document.writeln('<FORM NAME="updateform">');
	document.writeln('<div align="left">');
	document.writeln('<TABLE WIDTH=100% BORDER=0 CELLPADDING="2">');
// Überschrift Tabellentitel
	document.writeln('<TR BGCOLOR="#F0F0F0" style="font-size:8px;"><TD><b>Artikel</b></TD><TD align="right"><b>Menge</b></TD><TD align="right"><b>Einzelpreis</b></TD><td align="right"><b>Gesamtpreis</b></TD><TD>&nbsp;</TD></TR>');
// Nun die horizontale Linie:
	document.writeln('<TR BGCOLOR="F0F0F0"><TD colspan="5"><HR></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);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;

			document.write('<tr valign="top"BGCOLOR="'+tablerowcolor+'"><td>'+theitem+'</td>');
			
			if (theitem=="Mindermengenzuschlag") {
				Mindermenge=true; //Wenn Mindermengenzuschlag dabei, dann Boolsche Variable Mindermenge auf True setzen
			}
			
			document.write('<td valign="top" align="right">');
			if (theitem!="Leergut Abholen") document.write('<INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=3>');
			document.write('</td><td valign="top" align="right">'+theprice+' &#8364;</td>');
			document.write('<td valign="top" align="right">'+alterError(itemtotal)+' &#8364;</td>');
			document.write('<td  align="center" valign="top"><a href="javascript:removeItem('+itemlist+')"><img src="BilderPartyservice/entfernen.jpeg" alt="Aus dem Warenkorb entfernen" style="float:left;border:0;"></a>');
			if (theitem!="Leergut Abholen") document.write('<a href="javascript:amendItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"><img src="BilderPartyservice/aktualisieren.jpeg" alt="Geänderte Mengenaktualisieren" style="float:left;border:0;"> </a>');
			document.write('</td></tr>');
				
			if (tablerowcolor == '#F0F0F0') {
			  tablerowcolor = "#F8F8F8";
			} else {
			  tablerowcolor = "#F0F0F0";
			}
			
		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) {
				theitem = decodeURIComponent(fulllist.substring(itemstart, i));
			}
			if (thisitem==2) {
				theprice = decodeURIComponent(fulllist.substring(itemstart, i));
			}
			thisitem++;
			itemstart=i+1;
		}
	}
	
	//Wenn kein Leergut-Abholereintrag drin ist, diesen anbieten...
	if (fulllist.indexOf("Leergut")<0) {
		document.write('<tr><td valign="top" align="left" colspan=2 BGCOLOR="'+tablerowcolor+'">Sollen wir das Leergut von Ihnen abholen?</td>');
		document.write('<td valign="middle" align="left" colspan=2 BGCOLOR="'+tablerowcolor+'"nowrap>10.00 &#8364; pro Abholung&nbsp;&nbsp;&nbsp;');
		document.write('<td valign="middle" align="right" BGCOLOR="'+tablerowcolor+'"><p>');
		document.write('<strong>');
		document.write('<input type="button" name="order" value="Bestellen" onclick="addItem('+"'Leergut Abholen','10.00', '1')"+'"></strong>');
		document.write('</td>');
		document.write('</tr>');
		if (tablerowcolor == '#F0F0F0') {
		   tablerowcolor = "#F8F8F8";
		} else {
		   tablerowcolor = "#F0F0F0";
		}

	}      
	
	if (totprice<100) {
		if (Mindermenge==false) {
			//addItem('Mindermengenzuschlag',alterError(totprice*0.15), '1');

			document.write('<td valign="top" align="left" BGCOLOR="'+tablerowcolor+'">Mindermengenzuschlag</td>');
			document.write('<td valign="top" align="right" BGCOLOR="'+tablerowcolor+'">   </td>');
			document.write('<td valign="top" align="right" BGCOLOR="'+tablerowcolor+'">   </td>');
			document.write('<td valign="top" align="right" BGCOLOR="'+tablerowcolor+'">'+alterError(totprice*0.15)+' &#8364;</td>');
			document.write('<td valign="top" align="right" BGCOLOR="'+tablerowcolor+'">   </td>');
			totprice=totprice+totprice*0.15
		}
	}
	document.writeln('<TR BGCOLOR="F8F8F8"><TD colspan="5"><HR></TD></TR>');
	document.writeln('<tr BGCOLOR="#C0C0C0"><td colspan=3><b>Gesamt</b></td><td align=right colspan=2><b>EUR '+alterError(totprice)+" EUR"+'</b></td></tr>');
	document.writeln('<INPUT TYPE="hidden" NAME="-- GESAMTSUMME" VALUE="'+alterError(totprice)+' EUR -------------------------------------" SIZE="40">');
	document.writeln('<INPUT TYPE="hidden" NAME="BESTELLDATEN" VALUE=" " SIZE=40>');
	document.writeln('<INPUT TYPE="hidden" TEST>');
	document.writeln('</TABLE>');
	document.writeln('</div>');
	document.writeln('</FORM>');
	
}

function amendItem(itemno, newquant) {
	newItemList = null;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			thisitem = 1;
			itemstart = i+1;
			fullstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			itemlist=itemlist+1;
			if (itemlist != itemno) {
				newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
			} else {
				newItemList = newItemList + '['+theitem+'|'+theprice+'|'+newquant+']';
			}
		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			thisitem++;
			itemstart=i+1;
		}
	}
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket="+newItemList;
	$(".Warenkorb_gross").hide().replaceWith('<div class="Warenkorb_gross">'+htmlItems(1)+"</div>").show();

}

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;

	$(".Warenkorb_gross").hide().replaceWith('<div class="Warenkorb_gross">'+htmlItems(1)+"</div>").show();
}

// clearBasket() - removes all items from the basket
function clearBasket() {
	if (confirm('Möchten Sie wirklich alle Artikel aus dem Einkaufswagen entfernen?')) {
		index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket=.";
		$(".Warenkorb_gross").hide().replaceWith('<div class="Warenkorb_gross">'+htmlItems(1)+"</div>").show();
	}
}

