function ReklanetForm(jsonEmtId){
	this.defaultLocale = "fi_FI"; // Relic, not used
	
	this.jsonStr = document.getElementById(jsonEmtId).innerHTML; // Get JSON string
	this.selectArray = null; // Array to hold all select data. Parsed from the JSON string
	this.select1 = document.getElementById("tmp1"); // First select menu
	this.select2 = document.getElementById("tmp2"); // Second select menu
	
	this.valueField = document.getElementById("tmpnro"); // Hidden field to hold the facility id
	
	// Validation error properties
	this.errorCount = 0;
	this.errorMessage = "";
	this.errorPrefix = "\n- ";
	
	// Removes all options from a select element
	this.removeOptions = function(emt){
		var children = emt.getElementsByTagName("option");
		for(var i = (children.length - 1); i >= 1; i--){
			emt.removeChild(children[i]);
		}
	}
	
	// Updates the facility id to the selected value
	this.updateField = function(selectEmt){
		var value = selectEmt.options[selectEmt.selectedIndex].value;
		this.valueField.value = value;
	}
	
	// Get facility id from URL
	this.getIdFromURL = function(){
		var queryValue = "";
		if(window.location.href.indexOf("?tmpnro=") > 0){
			var locationParts = window.location.href.split("?tmpnro=");
			queryValue = (locationParts.length > 1) ? locationParts[1] : "";
		}
		return queryValue;
	}
	
	// Pad a value to at least eight characters with preceding zeros
	this.padId = function(value){
		if(value.length > 2){
			while(value.length < 8){
				value = "0" + value;
			}
		}
		
		return value;
	}
	
	
	// Updates the second select menu or hides it if not needed. Also, updates the facility id if appropriate
	this.updateSelects = function(selectEmt){
		this.removeOptions(this.select2); // Clear the second select menu
		
		var queryValue = this.getIdFromURL(); // Variable to hold the id submitted in the URL
		var itemSelected = false; // Have we actually found the submitted id in the select menus		
		
		var idx = selectEmt.selectedIndex;
		
		// Repopulate the second select menu if the selected menu item has children in the select array
		if(this.selectArray[idx][1].length > 1){
			for(var i = 0; i < this.selectArray[idx][1].length; i++){
				var text = this.selectArray[idx][1][i][0];
				var value = this.padId(this.selectArray[idx][1][i][1]);
								
				// Create a new option element
				var optionEmt = document.createElement("option");
				var textNode = document.createTextNode(text);
				optionEmt.value = value;
				
				// Select the item if the queried value matches the current menu item
				if(value == queryValue){
					optionEmt.selected = true;
					itemSelected = true; // We have a match
				}
				
				optionEmt.appendChild(textNode);
				this.select2.appendChild(optionEmt);
			}
			document.getElementById("tmp2_row").className = "";
			
			// Set the facility id or reset it if no item matched the queried id
			if(itemSelected == true){
				this.valueField.value = queryValue;
			}
			else{
				this.valueField.value = ""; // Clear the facility id
			}
		}
		
		// No children were found from the select array for the currently selected item
		else{
			document.getElementById("tmp2_row").className = "hidden"; // Hide the whole table row containing the second select menu
			var value = this.padId(this.selectArray[idx][1][0]);
									
			this.valueField.value = value.replace("$", "@"); // Set the facility id
		}
	}
	
	// Build the first select menu
	this.buildSelect1 = function(){
		this.selectArray = eval("(" + this.jsonStr.replace(/&nbsp;/g, '').replace(/&amp;/g, '&') + ")"); // Evaluate the JSON string into a an array
		var optionEmts = new Array(); // It seems this is not used...
		
		var queryValue = this.getIdFromURL(); // Variable to hold the id submitted in the URL
		
		for(var i = 0; i < this.selectArray.length; i++){
			var text = this.selectArray[i][0];
			
			var value = this.padId(this.selectArray[i][1][0]);
			
			// Create a new option element
			var optionEmt = document.createElement("option");
			var textNode = document.createTextNode(text);
			optionEmt.appendChild(textNode);
			
			// Iterate through the children and select the current menu item if the queried id matches any of the children
			if(this.selectArray[i][1].length > 1){
				for(var j = 0; j < this.selectArray[i][1].length; j++){
					var value2 = this.padId(this.selectArray[i][1][j][1]);
									
					// Select the item if the queried value matches the current menu item or the current child
					if(value == queryValue || value2 == queryValue){
						optionEmt.selected = true;
					}
				}
			}
			else if(value == queryValue){
				optionEmt.selected = true;
			}
			
			this.select1.appendChild(optionEmt);
		}
		this.updateSelects(this.select1); // Run necessary updates
	}
	
	// Add a new validation error to the error message
	this.addError = function(str){
		this.errorCount++;
		this.errorMessage += this.errorPrefix + str;
	}
	
	// Check if SOME of the given elements has the checked property set to true
	this.someSelected = function(emtArray){
		var checkedCount = 0;
		for(var i = 0; i < emtArray.length; i++){
			if(emtArray[i].checked) checkedCount++;
		}

		return (checkedCount > 0) ? true : false;
	}
	
	// Check if ALL the given elements have values
	this.haveValues = function(emtArray){
		var valueCount = 0;
		for(var i = 0; i < emtArray.length; i++){
			if(emtArray[i].value != '') valueCount++;
		}

		return (valueCount == emtArray.length) ? true : false;
	}
	
	// Validate form fields
	this.check = function(){
		var rf1 = document.getElementById("tmpnro");
		var rf2 = new Array(document.getElementById("fbt_1"),document.getElementById("fbt_2"),document.getElementById("fbt_3"),document.getElementById("fbt_4"));
		var rf3 = new Array(document.getElementById("fba_5"),document.getElementById("fba_4"),document.getElementById("fba_2"),document.getElementById("fba_3"));
		var rf4 = document.getElementById("txt_palaute");
		var rf5 = new Array(document.getElementById("fbc_1"),document.getElementById("fbc_2"),document.getElementById("fbc_3"),document.getElementById("fbc_4"));
		
		var cf1 = new Array();
		cf1.push(document.getElementById("txt_etunimi"));
		cf1.push(document.getElementById("txt_sukunimi"));
		cf1.push(document.getElementById("txt_katuosoite"));
		cf1.push(document.getElementById("txt_postinumero"));
		cf1.push(document.getElementById("txt_postitoimipaikka"));
		
		var cf2 = new Array();
		cf2.push(document.getElementById("txt_etunimi"));
		cf2.push(document.getElementById("txt_sukunimi"));
		cf2.push(document.getElementById("txt_puhelin"));
		
		var cf3 = new Array();
		cf3.push(document.getElementById("txt_etunimi"));
		cf3.push(document.getElementById("txt_sukunimi"));
		cf3.push(document.getElementById("txt_sposti"));
		
		this.errorCount = 0;
		this.errorMessage = errorHeader;
		
		if(rf1.value == '') this.addError(localizedFields[0]);
		if(!this.someSelected(rf2)) this.addError(localizedFields[1]);
		if(!this.someSelected(rf3)) this.addError(localizedFields[2]);
		if(rf4.value == '') this.addError(localizedFields[3]);
		if(!this.someSelected(rf5)) this.addError(localizedFields[4]);
	
		if(rf5[1].checked && !this.haveValues(cf3)){this.addError(localizedFields[5]); this.addError(localizedFields[6]); this.addError(localizedFields[11]);}
		if(rf5[2].checked && !this.haveValues(cf2)){this.addError(localizedFields[5]); this.addError(localizedFields[6]); this.addError(localizedFields[10]);}
		if(rf5[3].checked && !this.haveValues(cf1)){this.addError(localizedFields[5]); this.addError(localizedFields[6]); this.addError(localizedFields[7]); this.addError(localizedFields[8]); this.addError(localizedFields[9]);}
		
		if(this.errorCount == 0){
			return true;
		}
		else{	
			alert(this.errorMessage);
			
			this.errorCount = 0;
			this.errorMessage = "";
			
			if(window.location.href.indexOf("debug") > -1){
				alert(rf1.value);
			}
			
			return false;
		}
	}
	
	
	// Show the appropriate asterisks
	this.showAsterisk = function(fieldArr){
		for(var i = 1; i < 8; i++){
			document.getElementById("c_label" + i).getElementsByTagName("span")[0].style.display = "none";
		}
		for(var i = 1; i < 8; i++){
			var currentLabel = document.getElementById("c_label" + i);
			for(var j = 0; j < fieldArr.length; j++){
				if(fieldArr[j] == i){
					currentLabel.getElementsByTagName("span")[0].style.display = "inline";
				}
			}
		}
	}
	
	
	// Start the building pipeline
	this.buildSelect1();
}