var currencySymbol = " \u20AC";

function createStuecklisten()
{
	document.getElementById("tableStueckliste").removeChild(document.getElementById("tableStueckliste").getElementsByTagName("tbody")[0]);
	
	window.gesamtpreis = {
		refresh: function ()
		{
			document.getElementById("feldPreis").firstChild.nodeValue = (window.regalStueckliste.summe + window.addonStueckliste.summe) +  "" + currencySymbol;
		}
	};
	
	window.regalStueckliste = new Stueckliste("tableStueckliste", "feldPreisRegal");
	for (var artikel in regalArtikel)
		for (var ausfuehrung in regalArtikel[artikel])
			regalStueckliste.appendArtikel(regalArtikel[artikel][ausfuehrung]);
	
	window.addonStueckliste = new Stueckliste("tableStueckliste", "feldPreisAddons");
	for (var artikel in addonArtikel)
		for (var ausfuehrung in addonArtikel[artikel])
			addonStueckliste.appendArtikel(addonArtikel[artikel][ausfuehrung]);
}

function Stueckliste(parentId, ausgabeSummeId)
{
	this.parent = document.getElementById(parentId);
	this.tbody = document.createElement("tbody");
	
	this.parent.appendChild(this.tbody);
	this.summe = 0;
	
	var ausgabeSumme = document.getElementById(ausgabeSummeId);

	var liste = new Array();
	
	this.appendArtikel = function (artikel)
	{
		liste[liste.length] = artikel;
		this.tbody.appendChild(artikel.tr);
	};
	
	this.refresh = function ()
	{
		for (var i = 0, summe = 0; i < liste.length; summe += liste[i++].gesamtpreis);
		this.summe = summe;
		ausgabeSumme.firstChild.nodeValue = summe + "" + currencySymbol;
		
		gesamtpreis.refresh();
	}
}

function Artikel(bezeichnung, bestellnummer, verpackungseinheit, stueckpreisDeutschland)
{
	var thisObject = this;
	this.bezeichnung = bezeichnung;
	this.bestellnummer = bestellnummer;
	this.verpackungseinheit = verpackungseinheit;
	this.anzahl = 0;
	this.anzahlPakete = 0;
	this.stueckpreis = parseFloat(stueckpreisDeutschland);
	this.gesamtpreis = 0;
	
	this.tr = document.createElement("tr");
	this.tr.className = "Anzahl0";
		
	var tdBezeichnung = document.createElement("td");
	this.tr.appendChild(tdBezeichnung);
	tdBezeichnung.appendChild(document.createTextNode(bezeichnung));
	tdBezeichnung.className = "TableStuecklisteTdBezeichnung";
	
	var tdBestellnummer = document.createElement("td");
	this.tr.appendChild(tdBestellnummer);
	tdBestellnummer.appendChild(document.createTextNode(bestellnummer));
	
	var tdAnzahl = document.createElement("td");
	this.tr.appendChild(tdAnzahl);
	tdAnzahl.appendChild(document.createTextNode("0"));
	
	var tdStueckpreis = document.createElement("td");
	this.tr.appendChild(tdStueckpreis);
	tdStueckpreis.appendChild(document.createTextNode(stueckpreisDeutschland + "" + currencySymbol));
	
	var tdGesamtpreis = document.createElement("td");
	this.tr.appendChild(tdGesamtpreis);
	tdGesamtpreis.appendChild(document.createTextNode("0" + "" + currencySymbol));
	
	this.refresh = function ()
	{
		thisObject.anzahlPakete = Math.ceil(thisObject.anzahl / thisObject.verpackungseinheit);
		thisObject.uebrig = thisObject.anzahl % thisObject.verpackungseinheit;
		thisObject.tr.firstChild.firstChild.nodeValue = thisObject.bezeichnung + (thisObject.uebrig ? "*" : "");
		thisObject.gesamtpreis = thisObject.anzahlPakete * thisObject.stueckpreis;
		tdAnzahl.firstChild.nodeValue = thisObject.anzahlPakete;
		thisObject.tr.className = (thisObject.anzahlPakete == 0 ? "Anzahl0" : "");
		tdGesamtpreis.firstChild.nodeValue = thisObject.gesamtpreis + "" + currencySymbol;
	};
	
	this.setAnzahl = function (anzahl)
	{
		thisObject.anzahl = anzahl;
		thisObject.refresh();
	};
	
	this.getAnzahl = function ()
	{
		return thisObject.anzahl;
	};
}
