

function saveConfiguration()
{
	if(userid==0){
		user_show();
		return;
	}
	var saisie = prompt("Konfiguration Name eingeben :" , konfigname)
	
	// SH, 21-06-2011 //////////////////////////////////////////////////////////////
	konfigname = saisie;
	////////////////////////////////////////////////////////////////////////////////
	
	if (saisie!=null && saisie !="") 
	{
		saveConfigWithName(saisie);
		var globalString="&user_id="+userid;
		user_get_list();
	}
	else
	{
			if (saisie!=null) alert("Bitte geben Sie einen Namen!");
	}	
}

function delConfiguration(id)
{
	var wert =confirm("Hiermit wird diese Regalkonfiguration gelöscht.")
	if(wert){
		var globalString="&id="+id;
		answer=ajaxAccess2("php/user_class.php?task=delconfig"+ globalString,"");
	}
	var globalString="&user_id="+userid;
	answer=ajaxAccess2("php/user_class.php?task=getconfigs"+ globalString,"");
	document.getElementById("config_list").innerHTML= answer;
}

function sendConfiguration(){
	var saisie = prompt("Konfiguration Name eingeben :", konfigname)
	var to = prompt("An wen wollen Sie die E-Mail schicken?:", " @ ")
	var username = prompt("Bitte geben Sie Ihren Namen ein, damit der Mail-Empfänger weiß, von wem er die Mail bekommt");
	if(echeck(to)){
		if (saisie!=null && saisie !="") 
		{
			id= saveConfigWithName(saisie,"mail");
			var globalString="&to="+to+"&form="+user_data_email+"&id="+id+"&username="+username;
			answer=ajaxAccess2("php/mail_condfiguration.php?",globalString);
			alert(answer);
		}
		else
		{
			alert("Bitte geben Sie einen Namen ein!");
		}
	}
	else
	{
		alert("Bitte geben Sie eine gültige E-Mail ein!");
	}
}

	
function saveConfigWithName(configName,send)
{
	// Browse JS arrays to extract content
			var exportVerticalsArray=new Array(); // Will be used for the save operation
			var exportHorizontalsArray=new Array(); // Will be used for the save operation
			var exportAddonsArray=new Array(); // Will be used for the save operation
			
			// Verticals
			for(var i=0;i<verticals.length;i++)
			{
				for(var j=0;j<verticals[i].length;j++)
				{
					if(verticals[i][j]!=null && verticals[i][j].isRegal)
					{
						exportVerticalsArray.push(i+','+j);
						//alert('verticals:'+i+', '+j+' is regal');
					}
				}
			}
			
			// Horizontals
			for(var i=0;i<horizontals.length;i++)
			{
				for(var j=0;j<horizontals[i].length;j++)
				{
					if(horizontals[i][j]!=null && horizontals[i][j].isRegal)
					{
						exportHorizontalsArray.push(i+','+j);
						//alert('horizontals:'+i+', '+j+' is regal');
					}
				}
			}
			
			// Feet
			// Pas ok, pas bosser avec Âa
			/*alert('before feet:'+feet.length);
			for(var i=0;i<feet.length;i++)
			{
				for(var j=0;j<feet[i].length;j++)
				{
					if(feet[i][j]!=null && feet[i][j].isRegal)
					{
						alert('feet:'+i+', '+j+' is regal');
					}
				}
			}
			alert('after feet');*/
			
			// Boxes
			/*for(var i=1;i<boxes.length;i++)
			{
				for(var j=1;j<=boxes[i].length;j++)
				{
					if(boxes[i][j]!=null && boxes[i][j].hasAddon)
					{
						//alert('box:'+i+', '+j+' has addon, type:'+boxes[i][j].icon.id);
					}
				}
			}*/
			
			// Addons
			for(var i=0; i<addons.length;i++)
			{
				exportAddonsArray.push(addons[i].addonType+','+addons[i].ausfuehrung+','+addons[i].icon.style.left+','+addons[i].icon.style.top);
				//alert('Type:'+addons[i].addonType);
			}
			
			//Get ausfuerung
			
			for (key in regalArtikel.platteA){
				if( regalArtikel.platteA[key].anzahlPakete>0) var ausfuehrung= key;
			}
			
			// Send data to server using Ajax
			exportVerticalsString=exportVerticalsArray.join(";");
			exportHorizontalsString=exportHorizontalsArray.join(";");
			exportAddonsString=exportAddonsArray.join(";");
			
			
			var globalString = "&configName="+configName+"&verticals="+exportVerticalsString+"&horizontals="+exportHorizontalsString+"&addons="+exportAddonsString+"&ausfuerung="+ausfuehrung+"&send="+send;
			// SH, 17-05-2011 ////////////////////////////////////////////////////////////////////////////
			//if(send != "mail") globalString = globalString+"&id_user="+userid+"&config_id="+Config_id;
			globalString = globalString+"&id_user="+userid+"&config_id="+Config_id;
			//////////////////////////////////////////////////////////////////////////////////////////////
		
			answer=ajaxAccess("php/server.php", globalString);
			
			if(send=="mail") return answer;
			Config_id=0;
			alert(answer);
}

function ajaxAccess(file, arguments)
		{
			
			if(window.XMLHttpRequest) // FIREFOX
		          xhr_object = new XMLHttpRequest();
		     else if(window.ActiveXObject) // IE
		          xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		     else
		          return(false);
		     
		     request=file+"?regal="+arguments;     
		     
		     xhr_object.open("GET", request, false);
		     xhr_object.send(null);
		     if(xhr_object.readyState == 4) return(xhr_object.responseText);
		     else return(false);
		}
		
function ajaxAccess2(file, arguments)
{
	
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
	
	request=file+arguments;     
	
	xhr_object.open("GET", request, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);
}

var konfigname = 'konfiguration';

function loadConfiguration(configId , configname,mess)
		{
	if(mess !=1 ){
		var wert = confirm("Das angezeigte Regal wird hiermit überschrieben.")
	}
	else wert =true;
	if(wert == false ) return ; 
			//alert('loading config nummer:'+configId);
			konfigname = configname;
			// SH, 17-05-2011 //////////////////////////////////////////////////////////////////////
			//user_get_list();
			if(configname != 'Konfiguration') user_get_list(); // see planungsprogramm.php, line 75
			////////////////////////////////////////////////////////////////////////////////////////
			// Get config from DB using Ajax
			config=ajaxAccess("php/configLoader.php", "&configId="+configId);
			//alert("answer:"+config);
			
			// Parse config (verticals-horizontals-addons)
			var configSplitted=config.split('-');
			
			
			// Clean current regal
			removeAllAddons();
			ClearRegal();
			
			//alert(configSplitted.length);
			// Verticals
			var vertCoords=configSplitted[0].split(';');
			//alert(vertCoords.length);
			for(var i=0;i<vertCoords.length;i++)
			{
				//alert(vertCoords[i]);
				coordinatesVert=vertCoords[i].split(','); // Gives x and y
				if(!(isNaN (coordinatesVert[0])) && !(isNaN (coordinatesVert[1]) ) ) LoadVertical(parseInt(coordinatesVert[0]), parseInt(coordinatesVert[1])); 
			}
			//alert('nach vertical');
			// Horizontals
			var horCoords=configSplitted[1].split(';');
			for(var i=0;i<horCoords.length;i++)
			{
				coordinatesHor=horCoords[i].split(','); // Gives x and y
				if(!(isNaN (coordinatesHor[0])) && !(isNaN (coordinatesHor[1]) ) )LoadHorizontal(parseInt(coordinatesHor[0]), parseInt(coordinatesHor[1])); 
			}
			
			//var ausfuehrung = configSplitted[2];
			
			document.getElementById("iconRegal").className = configSplitted[2];
			var regal = document.getElementById("regal");
			regal.className = configSplitted[2];
			for (key in regalArtikel.platteA){
				if( regalArtikel.platteA[key].anzahlPakete>0) ausfuehrung= key;
			}
		

			regalArtikel["platteA"][configSplitted[2]].setAnzahl(regalArtikel["platteA"][ausfuehrung].getAnzahl());

			regalArtikel["platteA"][ausfuehrung].setAnzahl(0);

			regalArtikel["platteB"][configSplitted[2]].setAnzahl(regalArtikel["platteB"][ausfuehrung].getAnzahl());

			regalArtikel["platteB"][ausfuehrung].setAnzahl(0);

			if (isFreddyPlus)

			{

				regalArtikel["platteC"][configSplitted[2]].setAnzahl(regalArtikel["platteC"][ausfuehrung].getAnzahl());

				regalArtikel["platteC"][ausfuehrung].setAnzahl(0);

				regalArtikel["platteD"][configSplitted[2]].setAnzahl(regalArtikel["platteD"][ausfuehrung].getAnzahl());

				regalArtikel["platteD"][ausfuehrung].setAnzahl(0);

				regalArtikel["platteE"][configSplitted[2]].setAnzahl(regalArtikel["platteE"][ausfuehrung].getAnzahl());
	
				regalArtikel["platteE"][ausfuehrung].setAnzahl(0);

			}

			ausfuehrung = configSplitted[2];
			var confaddons=configSplitted[3].split(';');
			for(var i=0;i<confaddons.length;i++)
			{	
				confaddon=confaddons[i].split(','); // Gives x and y
				LoadAddons(confaddon[2], confaddon[3], confaddon[0], confaddon[1]);
			}
			
			
			
			regalStueckliste.refresh();
			
			calculationRegal(ausfuehrung);
		}	
		
		function LoadBox(x, y)
		{
			horizontals[x][y].set(true);
			verticals[x][y].set(true);
			horizontals[x][y - 1].set(true);
			verticals[x - 1][y].set(true);
		}
		
		function LoadVertical(x, y)
		{
			verticals[x][y].set(true);
		}
		
		function LoadHorizontal(x, y)
		{
			horizontals[x][y].set(true);
		}
		
		function LoadAddons(x, y, type, material)
		{
			var anzaddons = addons.length;
			if (anzaddons== null) anzaddons=0;
			addons[anzaddons] = new createaddons.Addon(type,material);
			addons[anzaddons].icon.setposition(x,y);
			addons[anzaddons].icon.drop();
			
		}
		
		function ClearRegal( )
		{
			// Clear verticals
			for(var i=0;i<verticals.length;i++)
			{
				for(var j=0;j<verticals[i].length;j++)
				{
					if(verticals[i][j]!=null && verticals[i][j].isRegal)
					{
						verticals[i][j].set(false);
						//alert('verticals:'+i+', '+j+' is regal');
					}
				}
			}
			// Clear horizontals
			for(var i=0;i<horizontals.length;i++)
			{
				for(var j=0;j<horizontals[i].length;j++)
				{
					if(horizontals[i][j]!=null && horizontals[i][j].isRegal)
					{
						horizontals[i][j].set(false);
						//alert('horizontals:'+i+', '+j+' is regal');
					}
				}
			}
		}
		
		function calculationRegal(ausfuehrung)
	{
		if( ! (typeof isausfuehrung == "string") )  ausfuehrung="MelaminWeiss";
		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();
	}

