var region;
var createaddons;
var userid = 0;
var user_data_email="";
var user_data_pw="";

window.onload = function ()
{
	createStuecklisten();
	createGrid();
	createaddons= new createAddonPatterns("MelaminWeiss");
	initDragAndDrop();
	

	setDatum();
	setHaendlerStatus();
	region = "Deutschland";
	setProgramStatus("SeitePlanung");

// Action associated to the "Anfrage an Floetotto senden" button
	document.getElementById("buttonAnfrage").onclick=function()
	{
		kundenInformationenPrompt();
	};	
	
	document.getElementById("buttonAnsicht").onclick = function ()
	{
		setProgramStatus("SeiteAnsicht");
		if(!isDesignValid()){	
	    showServiceForm();
	  }		
	};
	
	document.getElementById("buttonPlanung").onclick = function ()
	{
		setProgramStatus("SeitePlanung");		
	};
	
	document.getElementById("buttonHilfe").onclick = function ()
	{
		setProgramStatus("SeiteHilfe");		
	};

//	document.getElementById("buttonAufbaualternative").onclick = function ()
//	in regal.js

//	document.getElementById("buttonPlanungVerwerfen").onclick = function ()
//	in regal.js

	document.getElementById("buttonAngebot").onclick = function ()
	{
		setAnsichtStatus("AnsichtAngebot");		
	};
	document.getElementById("buttonBestellung").onclick = function ()
	{
		get_data_for_send();	
	};
	
	document.getElementById("buttonAuftragsbestaetigung").onclick = function ()
	{
		setAnsichtStatus("AnsichtAuftragsbestaetigung");		
	};
	
	document.getElementById("buttonStueckliste").onclick = function ()
	{
		setAnsichtStatus("AnsichtStueckliste");		
	};
	
	/*document.getElementById("buttonBestellung").onclick = function ()
	{
		setAnsichtStatus("AnsichtBestellung");		
	};*/
	
	document.getElementById("buttonAufbauanleitung").onclick = function ()
	{
		setAnsichtStatus("AnsichtAufbauanleitung");		
	};
	
	document.getElementById("buttonDrucken").onclick = function ()
	{
		window.print();
	};
	
	document.getElementById("buttonZurueckZuPlanung").onclick = function ()
	{
		setProgramStatus("SeitePlanung");		
	};
	
	document.getElementById("buttonHilfeDrucken").onclick = function ()
	{
		window.print();
	};
	
	// Action associated to the "Regal als PDF anzeigen" button
	document.getElementById("buttonPdfGenerierung").onclick=function()
	{
		generatePDFDatei(true, getClientData()); // See pdf.js
	};
	
	// ADD DM
	
	// Action associated to the "Zurück zum Shop" button
	/*document.getElementById("buttonGoToShop").onclick=function()
	{
		alert('Wenn Sie die Regal-Planung nicht mehr benötigen, können Sie die Planungsseite schließen.');
		window.open('/regalsystem/Kategorie/Regalboeden/Oberflaechenfarbe/Melamin_Weiss/Kantenfarbe/ABS_Weiss/regalboeden.html'); 
	};
	*/
	
	// END ADD DM

	var path = "../images/";
	preloadImages(path + "elmarFloetotto-print.gif",
		path + (isFreddyPlus ? "freddyPlusPlanungsprogramm-print.gif" : "freddyPlanungsprogramm-print.gif"),
		path + "regalHorizontal.gif",
		path + "regalHorizontal-print.gif",
		path + "regalJointError.gif",
		path + "regalJointToBottom-print.gif",
		path + "regalJointToLeft-print.gif",
		path + "regalJointToRight-print.gif",
		path + "regalJointToTop-print.gif",
		path + "regalVertical.gif",
		path + "regalVertical-print.gif");
		
	if (isFreddyPlus)
		preloadImages(path + "regalHorizontalPlatteA.gif",
			path + "regalHorizontalPlatteB.gif",
			path + "regalHorizontalPlatteC.gif",
			path + "regalHorizontalPlatteD.gif",
			path + "regalHorizontalPlatteE.gif",
			path + "regalVerticalPlatteA.gif",
			path + "regalVerticalPlatteB.gif",
			path + "regalVerticalPlatteC.gif",
			path + "regalVerticalPlatteD.gif",
			path + "regalVerticalPlatteE.gif");
		
	// MODIF DM	
	// ADD For Projekt Floetotto
	// Load first items of the regal
	LoadHorizontal(1, 0);
	LoadVertical(0, 1);
	
	// Preis
	regalArtikel["platteA"]["MelaminWeiss"].setAnzahl(2);
	window.regalStueckliste.refresh();
	
	calculationRegal();
	
	// END MODIF DM
};

// Tool function to load horizontal items of the regal structure
function LoadHorizontal(x, y)
{
	horizontals[x][y].set(true);
}

// Tool function to load vertical items of the regal structure
function LoadVertical(x, y)
{
	verticals[x][y].set(true);
}


function setDatum()
{
	var date = new Date();
	document.getElementById("feldDatum").firstChild.nodeValue = (date.getDate() < 10 ? "0" : "") + date.getDate() + "." + (date.getMonth() < 9 ? "0" : "") + (date.getMonth() + 1) + "." + date.getFullYear();
}

function setHaendlerStatus()
{
	document.getElementById("menuAnsicht").className = (window.isHaendler ? "MenuAnsichtHaendler" : "MenuAnsichtEndkunde");
}

function setProgramStatus(status)
{
	document.body.className = status;
	setAnsichtStatus("AnsichtAngebot")
}

function setAnsichtStatus(status)
{
	document.getElementById("ansicht").className = status;
	document.getElementById("regalBody").className = status;
	document.getElementById("configContainer").className = status;
}

function calculationRegal()
	{
		var platteA = 0;
		var platteB = 0;
		var platteC = 0;
		var platteD = 0;
		var platteE = 0;
		var gehaeusePairs = 0;
		var stoepselPairs = 0;
		var ausfuehrung = document.getElementById('selectAusfuehrungRegal').options[document.getElementById('selectAusfuehrungRegal').selectedIndex].value;
		
		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();
	}

window.onunload = function ()
{
	var regal = document.getElementById("regal");
	var regalBody = document.getElementById("regalBody");
	regal.removeChild(regalBody);

	removeAllAddons();

	document.getElementById("tableStueckliste").removeChild(document.getElementById("tableStueckliste").getElementsByTagName("tbody")[0]);

	while (cols.length)
		cols.pop();
		
	while (rows.length)
		rows.pop();
		
	while (joints.length)
		joints.pop();
		
	while (horizontals.length)
		horizontals.pop();

	while (verticals.length)
		verticals.pop();

	while (boxes.length)
		boxes.pop();

	while (feet.length)
		feet.pop();

	while (buttonsColWidth.length)
		buttonsColWidth.pop();
};


