var gridWidth = 20;
var gridHeight = 8;

var aufbaualternative = 0;

var regalTopEdge = 0;
var regalRightEdge = 0;
var regalBottomEdge=0;
var regalLeftEdge = 0;

var cols = new Array();
var rows = new Array();
var joints = new Array();
var horizontals = new Array();
var verticals = new Array();
var boxes = new Array();
var feet = new Array();
var buttonsColWidth = new Array();

for (var x = 0; x <= gridWidth; x++)
{
	joints[x] = new Array();
	verticals[x] = new Array();
	horizontals[x] = new Array();
	boxes[x] = new Array();
}

function createGrid()
{
	function Col()
	{
		var thisObject = this;
		this.x = x;
		this.isFreddyPlus = isFreddyPlus;
		this.isRightEdge = (isFreddyPlus && x == Math.floor(gridWidth / 2)) || (!isFreddyPlus && x == gridWidth);
		
		this.icon = document.createElement("col");
		this.icon.className = (isFreddyPlus ? "ColBoxFreddyPlus" : "ColBoxFreddy");
		this.icon.id = "colBox" + x; //@@
		regalColgroup.appendChild(this.icon);
		
		this.switchWidth = function ()
		{
			var hasAddon = false;
			for (var y = 1; y <= gridHeight; y++)
				if (boxes[thisObject.x][y].hasAddon)
				{
					hasAddon = true;
					y = gridHeight + 1;
				}

			if (!hasAddon)
			{
				if (thisObject.isFreddyPlus)
				{
					thisObject.isFreddyPlus = false;
					thisObject.icon.className = "ColBoxFreddy";
					document.getElementById("tdButtonColWidth" + thisObject.x).className = "ColBoxFreddy"; // for IE
					for (var i = 0; i < addons.length; i++)
						if (addons[i].col > thisObject.x)
							addons[i].moveX(-1);
				}
				else
				{
					thisObject.isFreddyPlus = true;
					thisObject.icon.className = "ColBoxFreddyPlus";
					document.getElementById("tdButtonColWidth" + thisObject.x).className = "ColBoxFreddyPlus"; // for IE
					for (var i = 0; i < addons.length; i++)
						if (addons[i].col > thisObject.x)
							addons[i].moveX(1);
				}
				hideCols();
				calculationRegal();
			}
		};

/*
		this.hide = function ()
		{
			for (var y = 0; y <= gridHeight; y++)
			{
				horizontals[thisObject.x][y].icon.style.display = "none";
				joints[thisObject.x][y].icon.style.display = "none";
			}
			for (var y = 1; y <= gridHeight; y++)
			{
				verticals[thisObject.x][y].icon.style.display = "none";
				boxes[thisObject.x][y].icon.style.display = "none";
			}
			feet[thisObject.x].icon.style.display = "none";
			buttonsColWidth[thisObject.x].icon.style.display = "none";

			thisObject.isRight = false;
			cols[thisObject.x - 1].isRight = true;
		};

		this.show = function ()
		{
			alert("show " + thisObject.x);
			for (var y = 0; y <= gridHeight; y++)
			{
				horizontals[thisObject.x][y].icon.style.display = "";
				joints[thisObject.x][y].icon.style.display = "";
			}
			for (var y = 1; y <= gridHeight; y++)
			{
				verticals[thisObject.x][y].icon.style.display = "";
				boxes[thisObject.x][y].icon.style.display = "";
			}
			feet[thisObject.x].icon.style.display = "";
			buttonsColWidth[thisObject.x].icon.style.display = "";
			
			thisObject.isRight = true;
			cols[thisObject.x - 1].isRight = false;
		};
*/
	}

	function Row()
	{
		var thisObject = this;
		this.isFreddyPlus = false;
		this.isTopEdge = (y == gridHeight);

		this.icon = document.createElement("tr");
		this.icon.className = "RowBoxFreddy"
		this.icon.id = "rowBox" + y; //@@
		regalBody.appendChild(this.icon);

		this.switchHeight = function ()
		{
			if (!thisObject.hasAddon)
			{
				thisObject.isFreddyPlus = !thisObject.isFreddyPlus;
				thisObject.icon.className = (thisObject.isFreddyPlus ? "RowBoxFreddyPlus" : "RowBoxFreddy");
				calculationRegal();
			}
		}
	}

	function Joint()
	{
		var thisObject = this;
		this.x = x;
		this.y = y;
		this.parity = (x + y + aufbaualternative) %2;
		
		this.toTop = false;
		this.toRight = false;
		this.toBottom = false;
		this.toLeft = false;
		
		this.icon = document.createElement("td");
		this.icon.id = "joint" + x + ":" + y; //@@
		row.appendChild(this.icon);
		
		this.setToTop = function (flag)
		{
			thisObject.toTop = flag;
			thisObject.refreshDisplay();
			
			if (thisObject.y == 0)
				feet[thisObject.x].set(thisObject.parity ? flag : thisObject.toRight || thisObject.toLeft);
		};
		
		this.setToRight = function (flag)
		{
			thisObject.toRight = flag;
			thisObject.refreshDisplay();

			if (thisObject.y == 0)
				feet[thisObject.x].set(thisObject.parity ? thisObject.toTop : flag || thisObject.toLeft);
		};
		
		this.setToBottom = function (flag)
		{
			thisObject.toBottom = flag;
			thisObject.refreshDisplay();
		};
		
		this.setToLeft = function (flag)
		{
			thisObject.toLeft = flag;
			thisObject.refreshDisplay();

			if (thisObject.y == 0)
				feet[thisObject.x].set(thisObject.parity ? thisObject.toTop : flag || thisObject.toRight);
		};
		
		this.refreshDisplay = function ()
		{
			if (thisObject.parity)
			{
				if (thisObject.toTop)
				{
					if (thisObject.toBottom)
					{
						thisObject.icon.className = "RegalJointVertical";
					}
					else
					{
						thisObject.icon.className = "RegalJointToTop";
					}
				}
				else 
				{
					if (thisObject.toBottom)
					{
						thisObject.icon.className = "RegalJointToBottom";
					}
					else
					{
						if (thisObject.toLeft && thisObject.toRight)
							thisObject.icon.className = "RegalJointError";
/*
						else if (thisObject.y == 0)
							thisObject.icon.className = "GridJointVerticalBottomEdge";
						else if (rows[thisObject.y].isTopEdge)
							thisObject.icon.className = "GridJointVerticalTopEdge";
*/
						else
							thisObject.icon.className = "GridJointVertical";
					}
				}
			}
			else
			{
				if (thisObject.toRight)
				{
					if (thisObject.toLeft)
					{
						thisObject.icon.className = "RegalJointHorizontal";
					}
					else
					{
						thisObject.icon.className = "RegalJointToRight";
					}
				}
				else 
				{
					if (thisObject.toLeft)
					{
						thisObject.icon.className = "RegalJointToLeft";
					}
					else
					{
						if (thisObject.toTop && thisObject.toBottom)
							thisObject.icon.className = "RegalJointError";
						else if (thisObject.x == 0)
							thisObject.icon.className = "GridJointHorizontalLeftEdge";
						else if (cols[thisObject.x].isRightEdge)
							thisObject.icon.className = "GridJointHorizontalRightEdge";
						else
							thisObject.icon.className = "GridJointHorizontal";
					}
				}
			}
		};

		this.refreshDisplay();
		
	}
	
	function Box()
	{
		var thisObject = this;
		this.x = x;
		this.y = y;
		this.hasAddon = false;
		this.hasAddonBox = false;
		this.hasAddonCdKreuz = false;
		this.hasAddonRueckwand = false;
		this.hasAddonGlastuer = false;
		this.icon = document.createElement("td");
		this.icon.className = "Box";
		rows[y].icon.appendChild(this.icon);
		this.icon.id = "box" + x + ":" + y; //@@
		
		this.icon.onclick = function ()
		{
			if (document.body.className == "SeitePlanung")
			{
				var x = thisObject.x;
				var y = thisObject.y;

				if (!boxes[x][y].hasAddon && horizontals[x][y].isRegal && verticals[x][y].isRegal && horizontals[x][y - 1].isRegal && verticals[x - 1][y].isRegal)
				{
					if (!(y < gridHeight && (boxes[x][y + 1].hasAddon || (verticals[x - 1][y + 1].isRegal && horizontals[x][y + 1].isRegal && verticals[x - 1][y + 1].isRegal))))
					{
						horizontals[x][y].set(false);
					}
					if (!(x < gridWidth && (boxes[x + 1][y].hasAddon || (horizontals[x + 1][y].isRegal && verticals[x + 1][y].isRegal && horizontals[x + 1][y - 1].isRegal))))
					{
						verticals[x][y].set(false);
					}
					if (!(y > 1 && (boxes[x][y - 1].hasAddon || (verticals[x][y - 1].isRegal && horizontals[x][y - 2].isRegal && verticals[x - 1][y - 1].isRegal))))
					{
						horizontals[x][y - 1].set(false);
					}
					if (!(x > 1 && (boxes[x - 1][y].hasAddon || (horizontals[x - 1][y - 1].isRegal && verticals[x - 2][y].isRegal && horizontals[x - 1][y].isRegal))))
					{
						verticals[x - 1][y].set(false);
					}
				}
				else
				{
					horizontals[x][y].set(true);
					verticals[x][y].set(true);
					horizontals[x][y - 1].set(true);
					verticals[x - 1][y].set(true);
				}
				calculationRegal();
			}
		};
	}

	function Vertical()
	{
		var thisObject = this;
		this.x = x;
		this.y = y;
		this.isRegal = false;
		this.parity = (x + y + aufbaualternative) %2;
		this.icon = document.createElement("td");
		this.icon.id = "vertical" + x + ":" + y; //@@
		rows[y].icon.appendChild(this.icon);
		
		this.icon.onclick = function()
		{
			if (document.body.className == "SeitePlanung" && (thisObject.x == 0 || !boxes[thisObject.x][thisObject.y].hasAddon) && (thisObject.x == gridWidth || !boxes[thisObject.x + 1][thisObject.y].hasAddon))
			{
				thisObject.set(!thisObject.isRegal);
				calculationRegal();
			}
		};
		
		this.set = function (flag)
		{
			thisObject.isRegal = flag;
			thisObject.refreshDisplay();
			
			joints[thisObject.x][thisObject.y - 1].setToTop(flag);
			joints[thisObject.x][thisObject.y].setToBottom(flag);
		};
		
		this.refreshDisplay = function ()
		{
			thisObject.icon.className = (thisObject.isRegal ? "RegalVertical" : "GridVertical" + thisObject.parity);
		};
		
		this.refreshDisplay();
	}

	function Horizontal()
	{
		var thisObject = this;
		this.x = x;
		this.y = y;
		this.isRegal = false;
		this.parity = (x + y + aufbaualternative) %2;
		this.icon = document.createElement("td");
		this.icon.className = "GridHorizontal" + this.parity;
		this.icon.id = "horizontal" + x + ":" + y; //@@
		row.appendChild(this.icon);
		
		this.icon.onclick = function()
		{
			if (document.body.className == "SeitePlanung" && (thisObject.y == 0 || !boxes[thisObject.x][thisObject.y].hasAddon) && (thisObject.y == gridHeight || !boxes[thisObject.x][thisObject.y + 1].hasAddon))
			{
				thisObject.set(!thisObject.isRegal);
				calculationRegal();
			}
		};
		
		this.set = function (flag)
		{
			thisObject.isRegal = flag;
			thisObject.refreshDisplay();

			joints[thisObject.x - 1][thisObject.y].setToRight(flag);
			joints[thisObject.x][thisObject.y].setToLeft(flag);
		};
		
		this.refreshDisplay = function ()
		{
			thisObject.icon.className = (thisObject.isRegal ? "RegalHorizontal" : "GridHorizontal" + thisObject.parity);
		};
		
		this.refreshDisplay();
	}

	function Foot()
	{
		var thisObject = this;
		this.x = x;
		this.isRegal = false;
		this.icon = document.createElement("td");
		this.icon.className = "GridFoot";
		this.icon.id = "foot" + x; //@@
		row.appendChild(this.icon);

		this.set = function (flag)
		{
			thisObject.isRegal = flag;
			thisObject.refreshDisplay();
		}

		this.refreshDisplay = function ()
		{
			thisObject.icon.className = (thisObject.isRegal ? "RegalFoot" : "GridFoot");
		};
	}
	
	function ButtonColWidth()
	{
		this.icon = document.createElement("td");
		this.icon.id = "tdButtonColWidth" + x;
		if (isFreddyPlus)
		{
			this.icon.className = "ColBoxFreddyPlus"; // for IE

			var button = document.createElement("button");
			button.id = "buttonColWidth" + x;
			button.onclick = cols[x].switchWidth;
			this.icon.appendChild(button);
		}
		else
		{
			this.icon.className = "ColBoxFreddy"; // for IE
		}
		row.appendChild(this.icon);
	}
		
	
	var regal = document.getElementById("regal");
	var regalBody = document.getElementById("regalBody");
	regal.removeChild(regalBody);
	
	var regalColgroup = document.createElement("colgroup");
	regal.appendChild(regalColgroup);

/*	
	while (regalBody.firstChild)
	{
		regalBody.removeChild(regalBody.firstChild);
	}
*/

	for (x = 0; x <= gridWidth; x++)
	{
		if (x)
		{
			cols[x] = new Col();
		}
		else
		{
			var col = document.createElement("col");
			col.id = "colButtons";
			regalColgroup.appendChild(col);
		}
		
		var col = document.createElement("col");
		col.id = "colVertical"  + x; //@@
		col.className = "ColVertical";
		regalColgroup.appendChild(col);
	}
	
	var regalBody = document.createElement("tbody");
	regalBody.id = "regalBody";
	regal.appendChild(regalBody);
	
	for (var y = gridHeight; y >= 0; y--)
	{
		var row = document.createElement("tr");
		row.id = "rowHorizontal" + y; //@@
		row.className = "RowHorizontal";
		if (y == gridHeight)
		{
			row.id = "rowTop";
		}
		else if (y == 0)
		{
			row.id = "rowBottom";
		}
		regalBody.appendChild(row);
		
		for (var x = 0; x <= gridWidth; x++)
		{
			if (x)
			{
				horizontals[x][y] = new Horizontal();
			}
			else
			{
				var td = document.createElement("td");
				td.className = "CellAboveButtonRowHeight";
				td.id = "cellAboveButtonRowHeight" + y;
				row.appendChild(td);
			}
			
			joints[x][y] = new Joint();
		}
		
		if (y)
		{
			rows[y] = new Row();

			for (var x = 0; x <= gridWidth; x++)
			{
				if (x)
				{
					boxes[x][y] = new Box();
				}
				else
				{
					var td = document.createElement("td");
					td.className = "CellButtonRowHeight";
					td.id = "cellButtonRowHeight" + y;
					rows[y].icon.appendChild(td);
/*					
					var button = document.createElement("button");
					button.onclick = rows[y].switchHeight; // currently not in use
					td.appendChild(button);
*/
				}
				
				verticals[x][y] = new Vertical();
			}
		}
		else
		{
			var row = document.createElement("tr");
			row.id = "rowButtons";
			regalBody.appendChild(row);

			for (var x = 0; x <= gridWidth; x++)
			{
				if (x)
				{
					buttonsColWidth[x] = new ButtonColWidth();
				}
				else
				{
					var td = document.createElement("td");
					row.appendChild(td);
				}
				
				feet[x] = new Foot();
			}
		}
	}

	if (isFreddyPlus)
	{
		hideCols();
	}
	
	function hideCols()
	{
		for (var getRightEdge = 1, width = 0; width <= gridWidth; width += (cols[getRightEdge++].isFreddyPlus ? 2 : 1));
		for (var x = --getRightEdge, className = ""; x <= gridWidth; className += "Col" + (x++) +"Hidden ");
		document.getElementById("canvas").className = className;
		
		if (getRightEdge <= gridWidth && cols[getRightEdge].isRightEdge)
		{
			cols[getRightEdge].isRightEdge = false;
			for (y = 0; y <= gridHeight; y++)
				joints[getRightEdge][y].refreshDisplay();
			cols[--getRightEdge].isRightEdge = true;
			for (y = 0; y <= gridHeight; y++)
				joints[getRightEdge][y].refreshDisplay();
		}
		else if (cols[getRightEdge - 2].isRightEdge)
		{
			cols[--getRightEdge].isRightEdge = true;
			for (y = 0; y <= gridHeight; y++)
				joints[getRightEdge][y].refreshDisplay();
			cols[--getRightEdge].isRightEdge = false;
			for (y = 0; y <= gridHeight; y++)
				joints[getRightEdge][y].refreshDisplay();
		}
	}

	document.getElementById("buttonAufbaualternative").onclick = function ()
	{
		aufbaualternative = (aufbaualternative ? 0 : 1);
		document.getElementById("regalContainer").className = (aufbaualternative ? "Aufbaualternative1" : "Aufbaualternative0");
		for (var x = 0; x <= gridWidth; x++)
			for (var y = 0; y <= gridHeight; y++)
			{
				joints[x][y].parity = !joints[x][y].parity;
				joints[x][y].refreshDisplay();
			}
		calculationRegal();
	};
	
	document.getElementById("buttonPlanungVerwerfen").onclick = function ()
	{
		if (confirm("Möchten Sie die Planung verwerfen?"))
		{
			removeAllAddons();	// in addons.js

			for (var x = 1; x <= regalRightEdge; x++)
				for (var y = 0; y <= regalTopEdge; y++)
					horizontals[x][y].set(false);
			for (var x = 0; x <= regalRightEdge; x++)
				for (var y = 1; y <= regalTopEdge; y++)
					verticals[x][y].set(false);
			calculationRegal();
		}
	};

	function calculationRegal()
	{
		var platteA = 0;
		var platteB = 0;
		var platteC = 0;
		var platteD = 0;
		var platteE = 0;
		var gehaeusePairs = 0;
		var stoepselPairs = 0;
		
		regalRightEdge = 0;
		for (x = gridWidth; x >= 0; x--)
		{
			for (y = gridHeight; y>= 0; y--)
			{
				var joint = joints[x][y];
				if (joint.toTop || joint.toRight || joint.toBottom || joint.toLeft)
				{
					regalRightEdge = x;
					x = -1;
					y = -1;
				}
			}
		}
		
		regalTopEdge = 0;
		for (y = gridHeight; y>= 0; y--)
		{
			for (x = regalRightEdge; x >= 0; x--)
			{
				var joint = joints[x][y];
				if (joint.toTop || joint.toRight || joint.toBottom || joint.toLeft)
				{
					regalTopEdge = y;
					x = -1;
					y = -1;
				}
			}
		}
		
		regalBottomEdge = 0;
		for (var y = 0; y <= regalTopEdge; y++)
		{
			for (var x = 0; x <= regalRightEdge; x++)
			{
				var joint = joints[x][y];
				if (joint.toTop || joint.toRight || joint.toBottom || joint.toLeft)
				{
					regalBottomEdge = y;
					x = gridWidth + 1;
					y = gridHeight + 1;
				}
			}
		}
		
		regalLeftEdge = 0;
		for (var x = 0; x <= regalRightEdge; x++)
		{
			for (var y = 0; y <= regalTopEdge; y++)
			{
				var joint = joints[x][y];
				if (joint.toTop || joint.toRight || joint.toBottom || joint.toLeft)
				{
					regalLeftEdge = x;
					x = gridWidth + 1;
					y = gridHeight + 1;
				}
			}
		}
		
//		alert("x: "+regalLeftEdge+" to "+regalRightEdge+"\ny: "+regalBottomEdge+" to "+regalTopEdge); //@@

		for (var x = 0; x <= regalRightEdge; x++)
		{
			for (var y = 0; y <= regalTopEdge; y++)
			{
				var joint = joints[x][y];
				
				if (isFreddyPlus)
				{
					if (joint.parity)
					{
						if (joint.toTop)
						{
							if (joint.toBottom)
							{
								if (rows[y].isFreddyPlus)
								{
									if (rows[y + 1].isFreddyPlus)
									{
										platteE++;
										verticals[x][y].icon.className = "RegalVertical RegalVerticalPlatteE";
									}
									else
									{
										platteD++;
										verticals[x][y].icon.className = "RegalVertical RegalVerticalPlatteD";
									}
								}
								else
								{
									if (rows[y + 1].isFreddyPlus)
									{
										platteD++;
										verticals[x][y].icon.className = "RegalVertical RegalVerticalPlatteD";
									}
									else
									{
										platteB++;
										verticals[x][y].icon.className = "RegalVertical RegalVerticalPlatteB";
									}
								}
								verticals[x][y + 1].icon.className = "RegalVertical";
								if (joint.toLeft && joint.toRight)
									gehaeusePairs += 2;
								else if (joint.toLeft || joint.toRight)
								{
									gehaeusePairs++;
									stoepselPairs++;
								}
							}
							else // !joint.toBottom
							{
								if (rows[y + 1].isFreddyPlus)
								{
									platteC++;
									verticals[x][y + 1].icon.className = "RegalVertical RegalVerticalPlatteC";
								}
								else
								{
									platteA++;
									verticals[x][y + 1].icon.className = "RegalVertical RegalVerticalPlatteA";
								}
								if (joint.toLeft && joint.toRight)
								{
									gehaeusePairs += 2;
									stoepselPairs++;
								}
								else if (joint.toLeft || joint.toRight)
								{
									gehaeusePairs++;
									stoepselPairs += 2;
								}
							}
						}
						else // !joint.toTop
						{
							if (joint.toBottom)
							{
								if (rows[y].isFreddyPlus)
								{
									platteC++;
									verticals[x][y].icon.className = "RegalVertical RegalVerticalPlatteC";
								}
								else
								{
									platteA++;
									verticals[x][y].icon.className = "RegalVertical RegalVerticalPlatteA";
								}
								if (joint.toLeft && joint.toRight)
								{
									gehaeusePairs += 2;
									stoepselPairs++;
								}
								else if (joint.toLeft || joint.toRight)
								{
									gehaeusePairs++;
									stoepselPairs += 2;
								}
							}
						}
					}
					else // !joint.parity
					{
						if (joint.toRight)
						{
							if (joint.toLeft)
							{
								if (cols[x].isFreddyPlus)
								{
									if (cols[x + 1].isFreddyPlus)
									{
										platteE++;
										horizontals[x][y].icon.className = "RegalHorizontal RegalHorizontalPlatteE";
									}
									else
									{
										platteD++;
										horizontals[x][y].icon.className = "RegalHorizontal RegalHorizontalPlatteD";
									}
								}
								else
								{
									if (cols[x + 1].isFreddyPlus)
									{
										platteD++;
										horizontals[x][y].icon.className = "RegalHorizontal RegalHorizontalPlatteD";
									}
									else
									{
										platteB++;
										horizontals[x][y].icon.className = "RegalHorizontal RegalHorizontalPlatteB";
									}
								}
								horizontals[x + 1][y].icon.className = "RegalHorizontal";
								if (joint.toTop && joint.toBottom)
								{
									gehaeusePairs += 2;
								}
								else if (joint.toTop || joint.toBottom)
								{
									gehaeusePairs++;
									stoepselPairs++;
								}
							}
							else // !joint.toLeft
							{
								if (cols[x + 1].isFreddyPlus)
								{
									platteC++;
									horizontals[x + 1][y].icon.className = "RegalHorizontal RegalHorizontalPlatteC";
								}
								else
								{
									platteA++;
									horizontals[x + 1][y].icon.className = "RegalHorizontal RegalHorizontalPlatteA";
								}
								if (joint.toTop && joint.toBottom)
								{
									gehaeusePairs += 2;
									stoepselPairs++;
								}
								else if (joint.toTop || joint.toBottom)
								{
									gehaeusePairs++;
									stoepselPairs += 2;
								}
							}
						}
						else // !joint.toRight
						{
							if (joint.toLeft)
							{
								if (cols[x].isFreddyPlus)
								{
									platteC++;
									horizontals[x][y].icon.className = "RegalHorizontal RegalHorizontalPlatteC";
								}
								else
								{
									platteA++;
									horizontals[x][y].icon.className = "RegalHorizontal RegalHorizontalPlatteA";
								}
							}
							if (joint.toTop && joint.toBottom)
							{
								gehaeusePairs += 2;
								stoepselPairs++;
							}
							else if (joint.toTop || joint.toBottom)
							{
								gehaeusePairs++;
								stoepselPairs += 2;
							}
						}
					}
				}
				else // !isFreddyPlus
				{
					if (joint.parity)
					{
						if (joint.toTop && joint.toBottom)
							platteB++;
						else if (joint.toTop || joint.toBottom)
							platteA++;
					}
					else // !joint.parity
					{
						if (joint.toRight && joint.toLeft)
							platteB++;
						else if (joint.toRight || joint.toLeft)
							platteA++;
					}
				}
			}
		}
		
		for (var x = 0, regalFeetPairs = 0; x <= regalRightEdge; x++)
		{
			if (feet[x].isRegal)
				regalFeetPairs++;
		}
		
		var regalMaszWidth = regalRightEdge - regalLeftEdge;
		var regalMaszHeight = regalTopEdge - regalBottomEdge;
		
		if (isFreddyPlus)
		{
			for (var x = regalLeftEdge + 1; x <= regalRightEdge; x++)
				if (cols[x].isFreddyPlus)
					regalMaszWidth++;
	
			for (var y = regalBottomEdge + 1; y <= regalTopEdge; y++)
				if (rows[y].isFreddyPlus)
					regalMaszHeight++;
		}
				
		if (regalMaszWidth + regalMaszHeight)
		{
			document.getElementById("feldRegalMaszWidth").firstChild.nodeValue = (regalMaszWidth * 38 + 2.5) + " cm"; // Modif DM 08.05.09 (3 -> 2.5)
			document.getElementById("feldRegalMaszHeight").firstChild.nodeValue = (regalMaszHeight * 38 + (regalBottomEdge ? 2.5 : 4.5)) + " cm"; // Modif DM 08.05.09 (3 -> 2.5, 5 -> 4.5)
			document.getElementById("feldRegalMaszDepth").firstChild.nodeValue = "35.5 cm"; // Modif DM 08.05.09 (35 -> 35.5)
		}
		else
		{
			document.getElementById("feldRegalMaszWidth").firstChild.nodeValue = "0 cm";
			document.getElementById("feldRegalMaszHeight").firstChild.nodeValue = "0 cm";
			document.getElementById("feldRegalMaszDepth").firstChild.nodeValue = "0 cm";
		}
	
		regalArtikel["platteA"][ausfuehrung].setAnzahl(platteA);
		regalArtikel["platteB"][ausfuehrung].setAnzahl(platteB);
		if (isFreddyPlus)
		{
			regalArtikel["platteC"][ausfuehrung].setAnzahl(platteC);
			regalArtikel["platteD"][ausfuehrung].setAnzahl(platteD);
			regalArtikel["platteE"][ausfuehrung].setAnzahl(platteE);

			var n = Math.ceil(regalFeetPairs / 3);
			regalArtikel["beschlagbeutel"]["1S"].setAnzahl(n);
			regalArtikel["beschlagbeutel"]["1L"].setAnzahl(regalMaszWidth + regalMaszHeight ? Math.max(Math.ceil(stoepselPairs / 8) - n, 1) : 0);
			
			var n = Math.ceil(gehaeusePairs / 4);
			if (n % 2)
			{
				regalArtikel["beschlagbeutel"]["2L"].setAnzahl(--n / 2);
				regalArtikel["beschlagbeutel"]["2S"].setAnzahl(1);
			}
			else
			{
				regalArtikel["beschlagbeutel"]["2L"].setAnzahl(n / 2);
				regalArtikel["beschlagbeutel"]["2S"].setAnzahl(0);
			}
			
			
//			alert((2 * gehaeusePairs) + " GehÃ¤use\n" + (2 * stoepselPairs) + " StÃ¶psel\n" + (2 * regalFeetPairs) + " Füï¿½e\n\n" + regalArtikel["beschlagbeutel"]["1S"].anzahl + " Beschlagbeutel 1 S\n" + regalArtikel["beschlagbeutel"]["1L"].anzahl + " Beschlagbeutel 1 L\n" + regalArtikel["beschlagbeutel"]["2S"].anzahl + " Beschlagbeutel 2 S\n" + regalArtikel["beschlagbeutel"]["2L"].anzahl + " Beschlagbeutel 2 L"); //@@

		}
		else
		{
			var uebrigPlatteA = 3 * platteA % 4;
			document.getElementById("tableUebrigPlatteA").firstChild.nodeValue = uebrigPlatteA;
			document.getElementById("tableUebrigPlatteA").parentNode.className = (uebrigPlatteA ? "" : "Anzahl0");
			
			var uebrigPlatteB = 3 * platteB % 4;
			document.getElementById("tableUebrigPlatteB").firstChild.nodeValue = uebrigPlatteB;
			document.getElementById("tableUebrigPlatteB").parentNode.className = (uebrigPlatteB ? "" : "Anzahl0");
				
			document.getElementById("tableUebrig").className = (uebrigPlatteA || uebrigPlatteB ? "" : "Anzahl0");
		}
		regalStueckliste.refresh();
	}
	
	var select = document.createElement("select");
	select.id = "selectAusfuehrungRegal";
	select.className = "SelectAusfuehrung RegalAusfuehrung";
	document.getElementById("menuPlanung").appendChild(select);
	
	for(var ausfuehrung in regalArtikel["platteA"])
	{
		var option = document.createElement("option");
		option.value = ausfuehrung;

		switch(ausfuehrung)
		{
			case "MelaminWeiss":
				option.appendChild(document.createTextNode("Melamin weiß / Kante ABS weiß"));
				break;
			// MODIF DM
			case "MelaminWeissNuss":
				option.appendChild(document.createTextNode("Melamin weiß / Kante Echtholz Nussbaum"));
				break;
			
			case "MelaminWeissBuche":
				option.appendChild(document.createTextNode("Melamin weiß / Kante Echtholz Buche"));
				break;
				
			case "MelaminBuche":
				option.appendChild(document.createTextNode("Melamin Buche / Kante Echtholz Buche"));
				break;
				
				
			case "MelaminNussbaum":
				option.appendChild(document.createTextNode("Melamin Nussbaum / Kante Echtholz Nussbaum"));
				break;
			// END MODIF DM
			case "LackWeiss":
				option.appendChild(document.createTextNode("Lack weiß / Kante Lack weiß"));
				break;
			case "LackGelb":
				option.appendChild(document.createTextNode("Lack gelb"));
				break;
			case "LackRot":
				option.appendChild(document.createTextNode("Lack rot"));
				break;
			case "LackGruen":
				option.appendChild(document.createTextNode("Lack grün"));
				break;
			case "LackAnthrazit":
				option.appendChild(document.createTextNode("Lack anthrazit / Kante Lack anthrazit"));
				break;
			case "LackOrange":
				option.appendChild(document.createTextNode("Lack orange"));
				break;
			case "LackAluminium":
				option.appendChild(document.createTextNode("Lack aluminium"));
				break;
			case "Nussbaum":
				option.appendChild(document.createTextNode("Kante Nussbaum"));
				break;
			default:
				option.appendChild(document.createTextNode(ausfuehrung));
		} // switch(ausfuehrung)

		select.appendChild(option);
	} // for(var ausfuehrung in regalArtikel["platteA"])

	select.onchange = function ()
	{
		document.getElementById("iconRegal").className = this.value;
		regal.className = this.value;
		
		regalArtikel["platteA"][this.value].setAnzahl(regalArtikel["platteA"][ausfuehrung].getAnzahl());
		regalArtikel["platteA"][ausfuehrung].setAnzahl(0);
		regalArtikel["platteB"][this.value].setAnzahl(regalArtikel["platteB"][ausfuehrung].getAnzahl());
		regalArtikel["platteB"][ausfuehrung].setAnzahl(0);
		if (isFreddyPlus)
		{
			regalArtikel["platteC"][this.value].setAnzahl(regalArtikel["platteC"][ausfuehrung].getAnzahl());
			regalArtikel["platteC"][ausfuehrung].setAnzahl(0);
			regalArtikel["platteD"][this.value].setAnzahl(regalArtikel["platteD"][ausfuehrung].getAnzahl());
			regalArtikel["platteD"][ausfuehrung].setAnzahl(0);
			regalArtikel["platteE"][this.value].setAnzahl(regalArtikel["platteE"][ausfuehrung].getAnzahl());
			regalArtikel["platteE"][ausfuehrung].setAnzahl(0);
		}
		ausfuehrung = this.value;
		regalStueckliste.refresh();
	};

	var ausfuehrung = (isFreddyPlus ? "LackWeiss" : "MelaminWeiss");
	regalStueckliste.refresh();
}


