	var walls = new Array(); // tillg?ngliga v?ggkombinationer
	var userWalls = new Array(); // anv?ndarena v?ggar
	var plasters = new Array(); // tillg?ngliga gipsskivor
	
	// Localization
	var language = "no";
	var LANG_INDEX = 0;
	
	function setLanguage( lang ){
		language = lang;
		if( language == "sv" ){
			LANG_INDEX = 1;
		}
	}
	
	var STRINGS = new Array(new Array(),new Array());
		STRINGS[0][0] = "Ny vegg";
		STRINGS[1][0] = "Ny vägg";
		STRINGS[0][1] = "Egen merkning/navn";
		STRINGS[1][1] = "Egen märkning/namn";
		STRINGS[0][2] = "Bredde på veggen (mm)";
		STRINGS[1][2] = "Längd på väggen (mm)";
		STRINGS[0][3] = "Høyde på veggen (mm)";
		STRINGS[1][3] = "Höjd på väggen (mm)";
		STRINGS[0][4] = "side";
		STRINGS[1][4] = "sida";
		STRINGS[0][5] = "Lage";
		STRINGS[1][5] = "Lag";
		STRINGS[0][6] = "Ta bort gruppen";
		STRINGS[1][6] = "Ta bort gruppen";
		STRINGS[0][7] = "Er du sikker på at du vil radere denne gruppe?";
		STRINGS[1][7] = "Är du säker på att du vill radera gruppen?";
		STRINGS[0][8] = "Spesifikasjon";
		STRINGS[1][8] = "Specifikation";
		STRINGS[0][9] = "Sammenstilling";
		STRINGS[1][9] = "Sammanställing";
		STRINGS[0][10] = "gipsplater";
		STRINGS[1][10] = "gipsskivor";
		STRINGS[0][11] = "tillbehør";
		STRINGS[1][11] = "tillbehör";
		STRINGS[0][12] = "Skruer";
		STRINGS[1][12] = "Skruv";
		STRINGS[0][13] = "Sparkel";
		STRINGS[1][13] = "Spackel";
		STRINGS[0][14] = "Total overflate";
		STRINGS[1][14] = "Total yta";
		STRINGS[0][15] = "Totalvekt";
		STRINGS[1][15] = "Totalvikt";
		STRINGS[0][16] = "Veggstørrelse";
		STRINGS[1][16] = "Väggstorlek";
		STRINGS[0][17] = "Veggoverflate";
		STRINGS[1][17] = "Väggyta";
		STRINGS[0][18] = "Vekt";
		STRINGS[1][18] = "Vikt";
		STRINGS[0][19] = "Legg til";
		STRINGS[1][19] = "Lägg till";
		STRINGS[0][20] = "-vegg";
		STRINGS[1][20] = " lag skivor per väggsida";
		STRINGS[0][21] = "Din nettlesere støtter ikke denne funksjon";
		STRINGS[1][21] = "Din browser stödjer inte denna funktion";
		STRINGS[0][22] = "Reglar / regelavstaånd c 450 mm"; // Översätt
		STRINGS[1][22] = "Reglar / regelavstaånd c 450 mm";
		STRINGS[0][23] = "Reglar / regelavstaånd c 600 mm"; // Översätt
		STRINGS[1][23] = "Reglar / regelavstaånd c 600 mm";
		STRINGS[0][24] = "Skenor, inkl anslutning";// Översätt
		STRINGS[1][24] = "Skenor, inkl anslutning";
	// --------------------------
	
	function Wall( wallId, numBoardsSide1, numBoardsSide2 ){
		this.id = wallId;
		this.title = "";
		this.width = 0;
		this.height = 0;
		this.sides = new Array(new Array(numBoardsSide1),new Array(numBoardsSide2));
		
		this.getType = function(){
			return this.sides[0].length + "+" + this.sides[1].length;
		};
		
		this.getSideCount = function(){
			return this.sides.length;
		};
		
		this.getBoardCount = function( side ){
			if( side-1 < 0 || side-1 >=this.sides.length ){
				return 0;
			} else {
				return this.sides[side-1].length;
			}
		};
		
		this.getArea = function(){
			return (this.width*this.height)/1000000.0;
		};
		
		this.getNumPlasters = function( plasterId ){
			var plasterCount = 0;
			for( var i = 0; i < this.sides.length; i++ ){
				for( var j = 0; j < this.sides[i].length; j++ ){
					if( this.sides[i][j].id == plasterId ){
						plasterCount++;
					}
				}
			}
			return plasterCount;
		};
	}
	

	function createNewWall( numBoardsSide1, numBoardsSide2 ){
		walls[walls.length] = new Wall( walls.length, numBoardsSide1, numBoardsSide2 );
	}
	
	function createNewUserWall( wallId ){
		var wall = walls[wallId];
		var newWall = new Wall( userWalls.length, wall.getBoardCount(1), wall.getBoardCount(2) );
		userWalls[userWalls.length] = newWall;
		createUserWallDiv(newWall);
		showHideCalcButton();
	}
	
	function createUserWallDiv( wall ){
		var wallContainer = document.getElementById("wallContainer");
		var newObject = document.createElement( "div" );
		newObject.className = "calcGroup_container";
		newObject.id = wall.id;
		
	/*	if( wallContainer.hasChildNodes() ){
			alert( wallContainer.item )
			var firstDiv = wallContainer.item(0);
			wallContainer.insertBefore( newObject, firstDiv );
		} else { */
			wallContainer.appendChild( newObject );
		//}
		var title = "";
		if( wall.title == null || wall.title == "" ){
			title = STRINGS[LANG_INDEX][0] + " (" + wall.getType() + ")";
		} else {
			title = wall.title;
		}
		var html = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"525\">\n" +
			"<tr>\n" +
			"<td colspan=\"2\"><label for=\"title_" + wall.id + "\">" + STRINGS[LANG_INDEX][1] + "</label><input type=\"text\" id=\"title_" + wall.id + "\" name=\"title_" + wall.id + "\" value=\"" + title + "\" style=\"width:525px;\"></td>\n" +
			"</tr>\n" +
			"<tr>\n" +
			"<td width=\"120\"><label for=\"width_" + wall.id + "\">" + STRINGS[LANG_INDEX][2] + "</label><input type=\"text\" id=\"width_" + wall.id + "\" name=\"width_" + wall.id + "\" value=\"" + wall.width + "\" style=\"width:120px;\"></td>\n" +
			"<td width=\"405\"><label for=\"height_" + wall.id + "\">" + STRINGS[LANG_INDEX][3] + "</label><input type=\"text\" id=\"height_" + wall.id + "\" name=\"height_" + wall.id + "\" value=\"" + wall.height + "\" style=\"width:120px;\"></td>\n" +
			"</tr>\n" +
			"</table>\n";
		if( wall.getBoardCount(1) > 0 ){
			html += "<br><fieldset><legend>" + STRINGS[LANG_INDEX][4] + " 1</legend>" +
			"<table cellspacing=\"1\" cellpadding=\"5\" border=\"0\">\n";
			for( var i = 0; i < wall.getBoardCount(1); i++ ){
				html += "<tr>\n<td style=\"vertical-align:middle;\">" + STRINGS[LANG_INDEX][5] + " " + (i+1) + ":</td><td>" + printPlasterSelect( wall, 1, i ) + "</td>\</tr>";
			}
			html += "</table>\n</fieldset>";
		}
		if( wall.getBoardCount(2) > 0 ){
			html += "<br><fieldset><legend>" + STRINGS[LANG_INDEX][4] + " 2</legend>" +
			"<table cellspacing=\"1\" cellpadding=\"5\" border=\"0\">\n";
			for( var i = 0; i < wall.getBoardCount(2); i++ ){
				html += "<tr>\n<td style=\"vertical-align:middle;\">" + STRINGS[LANG_INDEX][5] + " " + (i+1) + ":</td><td>" + printPlasterSelect( wall, 2, i ) + "</td>\n</tr>";
			}
			html += "</table>\n</fieldset>";
		}
		html += "<br><input type=\"button\" value=\"" + STRINGS[LANG_INDEX][6] + "\" onClick=\"removeWall(" + wall.id + ");\">";
		newObject.innerHTML = html;
	}
	
	function removeWall( id ){
		if( confirm(STRINGS[LANG_INDEX][7]) ) {
			collectWallData();
			// remove wall from object list
			var tmpWalls = new Array();
			var index = 0;
			for( var i = 0; i < userWalls.length; i++ ){
				if( userWalls[i].id != id ){
					tmpWalls[index] = userWalls[i];
					tmpWalls[index].id = index++;
				}
			}
			userWalls = tmpWalls;
			// repaint view
			resetUserWallDiv();
		}
	}
	
	function resetUserWallDiv(){
		var wallContainer = document.getElementById("wallContainer");
		wallContainer.innerHTML = "";
		for( var i = 0; i < userWalls.length; i++ ){
			createUserWallDiv( userWalls[i] );
		}
		showHideCalcButton();
		showHidePrintButton( true );
		var butt = document.getElementById("calcButt");
			butt.onclick = calculate;
			butt.src = "/static/site_mainGraphics/" + language + "/button_submitPlasterCalc.gif";
		
		var add_containerObj = document.getElementById("add_container");
			add_containerObj.style.display = "block";
			
		return false;
	}
	
	function calculate(){
		collectWallData();
		var wallContainer = document.getElementById("wallContainer");
		wallContainer.innerHTML = "";
		var newObject = document.createElement( "div" );
		wallContainer.appendChild( newObject );
		var html = "<h3>" + STRINGS[LANG_INDEX][8] + "</h3>";
		for( var i = 0; i < userWalls.length; i++ ){
			html += printWallSummary( userWalls[i] );
		}
		html += "<h3>" + STRINGS[LANG_INDEX][9] + "</h3>\n<div class=\"calcGroup_container\">";
		var totalPlasterWeight = 0;
		var totalArea = 0;
		html += "<fieldset><legend>" + STRINGS[LANG_INDEX][10] + "</legend>" +
		"<p style=\"padding: 0px 0px 10px 15px;\">";
		for( var i = 0; i < plasters.length; i++ ){
			var totalPlasterArea = 0;
			for( var j = 0; j < userWalls.length; j++ ){
				var numUsedInWall = userWalls[j].getNumPlasters(plasters[i].id);
				totalPlasterArea += userWalls[j].getArea()*numUsedInWall;
				totalArea += totalPlasterArea;
			}
			if( totalPlasterArea > 0 ){
				html += "<b>" + plasters[i].name + "</b> " + (totalPlasterArea) + " m<sup>2</sup><br>";
				totalPlasterWeight += plasters[i].weight*totalPlasterArea;
			}
		}
		html += "</p></fieldset>";
		var skruvPerM2 = 38.0;
		var spackelPerM2 = 0.5;
		var reglarC450PerM2 = 2.22;
		var reglarC600PerM2 = 1.67; 
		var skenorPerM2 = 0.8; 
		html += "<br>\n<fieldset><legend>" + STRINGS[LANG_INDEX][11] + "</legend>" +
		"<p style=\"padding: 0px 0px 10px 15px;\">" +
		"<b>" + STRINGS[LANG_INDEX][12] + ": </b> " + round(totalArea*skruvPerM2) + " st";
		html += "<br><b>" + STRINGS[LANG_INDEX][13] + ": </b> " + round(totalArea*spackelPerM2) + " kg";
		html += "<br><b>" + STRINGS[LANG_INDEX][22] + ": </b> " + round(totalArea*reglarC450PerM2) + " lm";
		html += "<br><b>" + STRINGS[LANG_INDEX][23] + ": </b> " + round(totalArea*reglarC600PerM2) + " lm";
		html += "<br><b>" + STRINGS[LANG_INDEX][24] + ": </b> " + round(totalArea*skenorPerM2) + " lm</p>\n</fieldset>";
		html += "<br>\n<fieldset><legend>Total</legend>" +
		"<p style=\"padding: 0px 0px 10px 15px;\">";
		html += "<b>" + STRINGS[LANG_INDEX][14] + ": </b> " + round(totalArea) + " m<sup>2</sup>";
		html += "<br><b>" + STRINGS[LANG_INDEX][15] + ": </b> " + round(totalPlasterWeight) + " kg</p></fieldset>\n</div><br>";
		newObject.innerHTML = html;
		showHideCalcButton();
		showHidePrintButton( true );
		var butt = document.getElementById("calcButt");
			butt.onclick = resetUserWallDiv;
			butt.src = "/static/site_mainGraphics/" + language + "/button_backPlasterCalc.gif";
			
		var add_containerObj = document.getElementById("add_container");
			add_containerObj.style.display = "none";
			
		return false;
	}
	
	function printWallSummary( wall ){
		var area = wall.getArea();
		var html = "<div class=\"calcGroup_container\">" +
			"<h3 style=\"margin-top:0px;\">" + wall.title + "</h3>" +
			"<p>" + STRINGS[LANG_INDEX][16] + ": <b>" + wall.width + "x" + wall.height + " mm</b><br>" +
			STRINGS[LANG_INDEX][17] + ": <b>" + round(area) + " m<sup>2</sup></b></p><br>";
		var totalWallWeight = 0;
		if( wall.getBoardCount(1) > 0 ){
			html += "<fieldset><legend>" + STRINGS[LANG_INDEX][4] + " 1</legend>" +
			"<table cellspacing=\"1\" cellpadding=\"5\" border=\"0\">\n";
			for( var i = 0; i < wall.getBoardCount(1); i++ ){
				var plaster = wall.sides[0][i];
				html += "<tr>\n<td style=\"vertical-align:middle;\">" + STRINGS[LANG_INDEX][5] + " " + (i+1) + "</td>\n" + printPlaster( plaster, area ) + "\n</tr>";
				totalWallWeight += plaster.weight*area;
			}
			html += "</table>\n</fieldset>";
		}
		if( wall.getBoardCount(2) > 0 ){
			html += "<br><fieldset><legend>" + STRINGS[LANG_INDEX][4] + " 2</legend>" +
			"<table cellspacing=\"1\" cellpadding=\"5\" border=\"0\">\n";
			for( var i = 0; i < wall.getBoardCount(2); i++ ){
				var plaster = wall.sides[1][i];
				html += "<tr>\n<td style=\"vertical-align:middle;\">" + STRINGS[LANG_INDEX][5] + " " + (i+1) + "</td>\n" + printPlaster( plaster, area ) + "\n</tr>";
				totalWallWeight += plaster.weight*area;
			}
			html += "</table>\n</fieldset>";
		}
		html += "<br><p><b>" + STRINGS[LANG_INDEX][15] + ": </b> " + round(totalWallWeight) + " kg</p></div>";
		return html;
	}
	
	function printPlaster( plaster, area ){
		var html = "<td>" + plaster.name + "</td>\n";
		html += "<td>" + STRINGS[LANG_INDEX][18] + ": " + round(plaster.weight*area) + " kg</td>";
		return html;
	}
	
	function collectWallData(){
		for( var i = 0; i < userWalls.length; i++ ){
			fillWallData( userWalls[i] );
		}
	}
	
	function fillWallData( wall ){
		var obj = document.getElementById( "title_" + wall.id );
		wall.title = obj.value;
		obj = document.getElementById( "width_" + wall.id );
		wall.width = parseInt(obj.value);
		obj = document.getElementById( "height_" + wall.id );
		wall.height = parseInt(obj.value);
		collectPlasters( wall, 1 );
		collectPlasters( wall, 2 );
	}
	
	function collectPlasters( wall, side ){
		if( wall.getBoardCount(side) > 0 ){
			for( var i = 0; i < wall.sides[side-1].length; i++ ){
				obj = document.getElementById( "plaster_" + side + "_" + i + "_" + wall.id );
				wall.sides[side-1][i] = plasters[parseInt( obj.options[obj.selectedIndex].value )];
			}
		}
	}
	
	function printPlasterSelect( wall, side, layerIndex ){
		var selectHtml = "<select id=\"plaster_" + side + "_" + layerIndex + "_" + wall.id + "\" name=\"plaster_" + side + "_" + layerIndex + "_" + wall.id + "\">";
		for( var i = 0; i < plasters.length; i++ ){
			if( wall.sides[side-1][layerIndex] != null && wall.sides[side-1][layerIndex].id == plasters[i].id ){
				selectHtml += "<option value=\"" + plasters[i].id + "\" SELECTED>" + plasters[i].name + "</option>";
			} else {
				selectHtml += "<option value=\"" + plasters[i].id + "\">" + plasters[i].name + "</option>";
			}
		}
		selectHtml += "</select>";
		return selectHtml;
	}
	
	function Plaster( id, name, thickness, width, weight ){
		this.id = id;
		this.name = name;
		this.thickness = thickness;
		this.width = width;
		this.weight = weight;
	}
	
	function createNewPlaster( name, thickness, width, weight ){
		plasters[plasters.length] = new Plaster( plasters.length, name, thickness, width, weight );
	}
	
	function round( value ){
		// 10 = 1 decimal
		// 100 = 2 decimals
		// 1000 = 3 decimals ...
		return Math.round(value*100)/100;
	}
	
	function initWallSelector(){
		var selectObject = document.getElementById("wallSelector");
		if( selectObject != null ) {
			for( var i = 0; i < walls.length; i++ ){
				//selectObject[i] = new Option( STRINGS[LANG_INDEX][19] + " " + walls[i].getType() + STRINGS[LANG_INDEX][20], walls[i].id, false);
				selectObject[i] = new Option( walls[i].getType() + STRINGS[LANG_INDEX][20], walls[i].id, false);
			}
		}
	}
	
	function showHideCalcButton() {
		
		var calcButtObj = document.getElementById("calcButt");
		if( userWalls.length > 0 ) {
			calcButtObj.style.display = "block";
		}
		else {
			calcButtObj.style.display = "none";
		}
	}
	
	function showHidePrintButton( showBool ) {
		
		var printButtObj = document.getElementById("printButt");
		if( showBool ) {
			printButtObj.style.display = "block";
		}
		else {
			printButtObj.style.display = "none";
		}
	}
	
	function printPlasterCalculator() {
	
		var contentObj = document.getElementById("calculator_container");
		var linksObj = document.getElementsByTagName("link");
		var linksStr = "";
		
		if( linksObj ) {
			for( var i = 0; i <= linksObj.length; i++) {
				if( linksObj[i] != null ) {
					linksStr = linksStr + '<link href="' + linksObj[i].getAttribute("href") + '" rel="stylesheet" type="text/css">\n';
				}
			}
		}
		
		if( contentObj != null ) {
			var winId = window.open('','win_printPlasterCalculator','width=640,height=600,scrollbars=yes'); 
				winId.document.write('<html>\n<head>\n<title>' + document.title + '</title>\n' + linksStr + '\n<style type="text/css">\nBODY{ background-color: #FFFFFF; background-image: none; margin: 40px; text-align: left; }\n</style>\n</head>\n<body><div id="' + contentObj.id + '"><img src="/static/site_mainGraphics/print_logo.gif"><br><br>' + contentObj.innerHTML + '</div>\n</body>\</html>'); 
				if( winId.document.getElementById("calcButt") != null )
					  winId.document.getElementById("calcButt").style.display = "none";
				if( winId.document.getElementById("printButt") != null )
					  winId.document.getElementById("printButt").style.display = "none";
				winId.document.close(); 
				winId.focus();
			
			if (window.print) winId.print();
		}
		else {
			alert( STRINGS[LANG_INDEX][21] );
	}
}

