/*Image hover*/

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
		
		/*

			This is a script which is mainly about form validation, and generating output into the "mailto:" html command.
			It works by receiving a form name, taking that name, it is able to loop through all the elements within that form,
			performing predefined actions on each element. Some of the elements will be put into the subject of the output mail,
			some fields will be checked, and thus required to have contents.
			
			The script has various debug options built into it. The debug options are mainly about showing various values through the script,
			every time there is a comment that says "DEBUG",  you can enable debugging, remove the two slashes (//) that is infront the line(s)  beneath (usualy an alert command).

		*/

		function customize_form(formID) {

			//Creates a boolean variable to check later whether or not the script has run into an error, such as a form field that has not been populated properly.
			//If this is variable ever becomes true, before the output is sent, it will stop the script. There is also embeded an option to display an error to the end-user, 
			//so that the user can see what went wrong. This is mainly used for validation, when the user has done something wrong. Such as writing only 3 characters in a
			//form field that requires atleast 5 characters.
			var error;
			var err_msg;
			
			//Creates a variable to use with checkboxes, it contain the names of the checkboxes used, so that duplicate checkboxes arent included into the output.
			//It works by putting the name of the checkbox into this variable. When adding a new checkbox, it checks the name first, and only proceed if the name is not
			//found among the already processed ones.
			var checkNames = '';
			
			//Creates the variable that will contain the RegExp function, which we can search through checkNames with, to see whether or not a checkbox has been added already.
			//this is basically a variable that contains a search function. When used, we can check if a string contains a certain word or sentence.
			var searchPattern;
			
			//Creates the variable that will contain the RegExp function, which will be used to check if a field is set to be exluded
			//If a field is set to be excluded, it will still be processed in the dynamic form, the difference is that it will not include the field
			//into the output.
			var checkForExclude = new RegExp('exclude');
				
			//This is used as an abbreviation to give us a shortcut into the form defined when the function is called. This is where we get all our fields from.
			var  myForm = document.getElementById(formID);

			//Here we start looping through each field in the form. Simply put, this is the key to how this entire script works. 
			//It works by counting how many fields there are total, and start looping through it, until every field has been checked.
			for (var i=0;i<myForm.elements.length;i++) {
				//The try command makes sure that the script will not stop on errors, such as a specific field that is not yet defined. This is the key to why the script will continue to work, 
				//even if something is misspelled in the html document, or something else goes wrong. It just ignores the error, include whatever it can, and goes on to the next field.
				//This is essential to make the script work in IE, because IE will stop the script if a field is missing some particular parameter. Typical error may be that the name parameter is not used
				//on a specific field. Thus, by using the try command, we can give the html writer more freedom when making the actual form.
				try {
					/* 
					
						FORM FIELD PROCESSING
						
					*/
					
					if (myForm.elements[i].disabled == undefined) {
						//alert(myForm.elements[i].nodeName);
						//alert(myForm.elements[i].nodeName + '  ' + myForm.elements[i].name + ':' + i + '  -    ' + myForm.elements[i].name + ' ' + myForm.elements[i].value + ' ' + myForm.elements[i].disabled);
						if (myForm.elements[i].nodeName == 'FIELDSET') {
							continue;
						}
						
						else {
							myForm.elements[i].disabled = false;
						}
					}
					
					if (myForm.elements[i].type == undefined) {
						//alert('3'+myForm.elements[i].nodeName);
					}
					
					//Here we check if a field is disabled. If it is, it skips it entirely. This is essential for the dynamic form to work, because the dynamic form
					//works by hiding the fields not used. Javascript, however, can not know that the field is invisible, instead it checks whether or not its disabled, and skips it by that way instead.
					//Since a field that is invisible is of no consequence to the end user anyway, its best to disable it entirely, and enable it if the container it is in, is shown.
					//It also tests by searching the class of the field, to see whether or not it is excluded
					var FieldValue = escape(myForm.elements[i].value);
					var FieldName = escape(myForm.elements[i].name);
					if ((myForm.elements[i].disabled == false) && !(checkForExclude.test(myForm.elements[i].className))) {
						/*
						
							FORM VALIDATION
						
						*/
						
						//Here starts the validation of the form. We start of by checking if the field in question is required. This is done by putting the string 'required' short for required, 
						//into the class section of the field. This is done in the html part.
						var checkForRequired = new RegExp("required");
						if (checkForRequired.test(myForm.elements[i].className)) {
							//If the field type is either hidden, radio, or checkbox, we add special logic since the minimum selected should be one, meaning it has to check multiple fields if one of them is populated correctly.
							//This is the most tricky part in modern validation.
							if ((myForm.elements[i].type == 'hidden') || (myForm.elements[i].type == 'radio') || (myForm.elements[i].type == 'checkbox')) {
								//Get all fields with the same name of the field in question.
								var x = document.getElementsByName(myForm.elements[i].name);
								//Here we start looping through it.
								for (var y = 0; y <= x.length; y++) {
									//Since we could put a hidden field to required, with the same name as a set of checkboxes, or radio, it is important that we don't actually require the hidden field to be filled,
									//since the user never actually has the chance to fill it with anything. Thus, a hidden field which is required can have no value, but the other fields with same name must have a value.
									if ((x[y].type == 'radio') || (x[y].type == 'checkbox')) {
										//If the field is not checked, we create an error.
										if (x[y].checked == false) {
											error = true;
											err_msg = x[y].name + ' : ' + 'Du må velge en!';
										}
										
										//If we find a checked field in the same group however, we reset the error to undefined, so that the script will ignore the unchecked fields.
										//It also breaks the loop, since we have found a checked field, it is not necessary to check whether or not the other fields are checked or not.
										//This is because we have fulfilled the requirement, thus, complete the objective of the required logic.
										else {
											error = void(0);
											break;
										}
									}
								}
							}
							
							//Here is the basic required check, whichs checks whether or not the field has a value or not. If nothing is written, it will write an error.
							//Since it comes last, it will get priority over the other errors, because the error variables get overwritten. This means that typing nothing into a field that is required, must contain only siffers, and lastly has a minimum limit
							//will still display the error for not typing anything. 
							if (myForm.elements[i].value === '') {
								error = true;
								err_msg = 'Du har ikke fylt ut alle nødvendige felt.';
							}
						}
						
						//Here we add additional logic to enable us to require fields to be filled with numbers only.
						//This is also written in the class section of the field, in html. This is added by putting 'numeric' into the class of a field.
						if (myForm.elements[i].value != '') {
							//This is the variable that contains the RegExp search function, which checks whether or not a field is supposed to contain only numeric characters.
							var checkForNumOnly = new RegExp("numeric");
							//Another variable with RegExp is added, this, unlike the previous RegExp variables we have used so far, contains a specific pattern,
							//When used on a string, it will check whether or not the string contains anything but the characters from the pattern, in this case 0-9.
							//This means that if anything but 0-9 is used, it will evaluate to true, giving the end user an error in the process.
							var checkNumeric = new RegExp("[^0-9]");
							if (checkForNumOnly.test(myForm.elements[i].className)) {
															
								//Here we take the pattern and test it on the field value.
								var check = checkNumeric.test(myForm.elements[i].value);
								//If it finds any characters that is banned by the pattern, it returns the error.
								if (check == true) {
									error = true;
									//We define the error that is given to the user here.
									err_msg = 'Dette feltet skal kun inneholde tall.';
								}
							}

							//This is the function part to require the user to write a minimum of characters into a field.
							//Note that it will not run if nothing is written at all. This is an intended function, to allow us more freedom when writing the form
							//This way, we can limit the user to write nothing, or everything that we require. An example of this is phone number, which should include
							//a given length, or not be included at all.
							//We start off the function to see whether or not its required to have a minimum value. Additional logic is added because the syntax requires it.
							var minLocation = myForm.elements[i].className.search('minimum ');
							//Now that we have found the 'minimum' string from the class of the field, and where in the entire class string its found, we can start processing it.
							//First IF field starts off by checking that its actually in the text at all.
							if (minLocation != -1) {
								//If it is, it jumps to the end of the word 'minimum' which is on 7 lines.
								minLocation += 7;
								//Then we add another space, since the syntax is 'numeric <value>',
								//All this extra logic is because we must define exactly where to find the intended length, we also do not know how long the length is, 
								//it could be '5' or '1000000'. Therefore we do a series of tests to make sure we find the entire numeric value. Here we start of by putting down where it starts.
								var start = ((minLocation)+1);
								//Then we put down where it ends, since it could be either last letter in the class, or be seperated with a space, we do a check of both.
								//We start of by saying that the end is found at the next space
								var end = myForm.elements[i].className.indexOf(' ', start);
								//If however no next space is found, we add the entire length instead, meaning it will go from the earlier defined start, to
								//the end of the line.
								if (myForm.elements[i].className.indexOf(' ', start) == -1) {
									end = myForm.elements[i].className.length;
								}
								
								//Now that we have found the length of the numeric value, we add it to a single variable.
								var min_length = myForm.elements[i].className.substring(start, end);
										//DEBUG
										//alert('Start : ' + start + "\n" + 'End : ' + end + "\n" + 'This becomes : ' + min_length);
								
								//Because of all the extra logic, a bit of caution is required; we double check that the minimum length contains a value, and that the value is numeric.
								//If the html form writer misspells and writes something like class="minimum required" No actual minimum length is found, since it would then try to use "required" in this case,
								//as that value, it would give an javascript error at runtime, risking to actually stop the script, and destroy the form.
								if ((min_length != '') && !(checkNumeric.test(min_length))) {
									//Minimum length is found, and is a healthy numeric value. We compare it to the length which the user has written.
									//If the text the user has written is equal or exceeds the minimum length that is required, no error is given.
									if (myForm.elements[i].value.length < min_length) {
										error = true;
										//Here we add the actual error text. We have added the min_length variable to tell the end-user how many characters is actually required.
										//For a norwegian phone number, it would say that it should have atleast 8 characters.
										err_msg = 'Dette feltet skal ha minimum ' + min_length + ' tegn';
									}
								}
								
								else {
									//An debug option is added to highlight the fields that have a misspelled 'minimum' function.
									//DEBUG
									//alert('Failed to get min_length');
								}
							}
						}

						//If the error variable is set to true, it means the script has found a field that does not satisfy the requirements. 
						//Thus, it will interrupt the script, focus on the field where the error was found, and give an alert message with the custom error
						if (error == true) {
							//It will output the predefined custom error, a carriage return, then the name of the field that triggerered the error.
							alert(err_msg + "\n" + myForm.elements[i].name);
							//It will then finally jump to the actual field, and focus on it.
							myForm.elements[i].focus();
							break;
						}
					}
				}
				
				//This is the end of the try command. Here we can enable the last line commented out, if we wish to debug the script, and see what errors occur.
				//Undoubtly, it will contain undefined errors, which is not critical for the script success, but would interrupt it, should it not have been ignored by the try statement.
				catch(err) {
					//Bug catcher, remove the two slashes below to display the insignificant errors that occur through the script.
					//Typical errors include javascript checking name of a field that has no name set.
					//alert(err.description);
				}
			}
			
			//If no errors were found earlier, it continues to accumulate the entire mail, by adding both subject and the body into the actual mail.
			//When the mail is finished, It changes the location bar (In the same way a normal link would do it) to that of the mailto: command. Because of the nature of the mailto command, it will not actually change the page,
			//but rather open the external mail client specified for the OS, and put all the values we have collected into it.
			if (error !== true) {
				myForm.submit();
			}
			
			else {
				return false;
			}
		}

		/*

			This is the script to make dynamic forms. In short; it works by specifying a specific div field, It will then go and hide all the div fields with the same class name, show the div field specified.
			It will also find all the form fields contained in the div field, enable them, and disable all the other form fields. This is done so that the customize_form function can loop through the fields properly,
			And avoid taking invisible fields into the output. 
			
			This script works by taking a parent <div> object from the form, named dyn_divm and finding *all* the div fields that exists inside it.
			
			The function takes one parameter only, the id value of the div field you want to SHOW, it will then hide all the other div fields with the same class name.

		*/
		function dyn_form (showThis) {
			//Here we find all div fields by searching the parent div.
			var divFields = document.getElementsByTagName('div');
				//We put alot of error detection when checking the classname of the div fields we want to show. This is because we want to avoid the script to fail due to badly written and/or incomplete html.
				//It also gives us a couple of options when specifying what div field to show
				try {
					var sublevel = document.getElementById(showThis).className;
				}
				
				catch(err) {
					try {
						sublevel = document.getElementById(showThis.value).className
					}
					
					catch(err2) {
						try {
							//If sublevel is not found, we try converting the specified div id to all lowercase, maybe the id is written in lowercase.
							sublevel = document.getElementById(showThis.toLowerCase()).className
						}
						
						catch(err3) {
							//If the div field is not found at all, it gives an error explaining that the content is incomplete.
							alert('Error code: 001' + "\n" + 'Content incomplete, missing, or invalid.');
							sublevel = false;
						}
					}
				}
				
			if (sublevel != false) {
				//We start looping through all the div fields that we have found. This is the main key to this script logic.
				//By doing this, we can in the html part put div fields freely wherever, and thus, we avoid restricting the html part into a very specific syntax.
				for (var y = 0; y < divFields.length; y++) {
					//As in the validation script, we add a try command, to avoid the script being interrupted by insignificant errors. 
					//This is useful if for an example it can't find the id refered to by a function run from a badly written html page. It will simply do nothing, and instead let you use
					//the function on div fields it can find. This way, it won't break, even if the html page is badly written.
					try {
						//IF when looping, we come across a div field with the same class name as the field we have selected, it will continue to run commands on that particular div field.
						//In short, it will test if the div field is the div field we want activated, if not, it will implement styles and commands to hide the field, and disable the form fields inside the div.
						//This way, it will hide and disable fields on the same sublevel as the field we are looking for is on. While the rest of the page remain untouched. This is one of the main-keys to how this script works.
						if (divFields[y].className == sublevel) {
							//Here we create a variable that will later be used to search through a div class tag, to see if it is supposed to be hidden.
							var checkForDisplay=new RegExp("sub");
							//Here we search and put all the form fields contained inside the looped div field. This way, we have a predefined list of form fields that needs to be interacted with.
							//This makes it easy to choose whether or not it should be hidden & disabled.
							var fields = divFields[y].getElementsByTagName('input');
							//We must do the same thing with the select fields, since they are written differently in html, meaning we have to use a seperate variable in javascript to access them.
							var selects = divFields[y].getElementsByTagName('select');
							//Finally, we have textareas, which must undergo same treatment as the previous two.
							var textareas = divFields[y].getElementsByTagName('textarea');
							//If the div field we have looped to, is equal to the one we want to show, it will run this part
							if (divFields[y].id == showThis) {
								//This shows the entire field, with all its contents. Next we have to enable the form fields.
								divFields[y].style.display = 'block';
								
								//We have to enable special logic to make the script treat hidden div fields inside other div fields properly. 
								//This is essentially to make sure that the end user is never required to fill out any form fields he or she actually can't see.
								for (var fnr = 0; fnr < fields.length; fnr++) {
									//We start first by enabling all form fields which is contained directly beneath the div field we have requested to show.
									if (fields[fnr].parentNode.id == showThis) {
										fields[fnr].disabled = false;
									}
									
									//If however, we find form fields not contained directly beneath the showThis div field
									else {
										//We set a variable to the parentNode, meaning it will look for what field it is contained in.
										var thisParent = fields[fnr].parentNode;
										//Since a hidden div field is not necessarily directly inside the top_div(dyn_div), or if we have a hidden div field with multiple sub divs that is not supposed to be hidden,
										//we have to make sure what form field should be disabled or not. So, this means, that the div field is actually hidden by now, and all the form fields within it, but they are not yet
										//Necessarily disabled. So, we make sure that they are, if they are supposed to be, so that the end-user never is required to fill out a form field he can't see.
										for (var pnr = 0; pnr < 20; pnr++) {
											//Looping through, we will check each parent for a number of possible values, and act according to what is found.
											
											//in the first possible angle, we have tried to find the parent of a form field already at the top level.
											//Here we have to make sure the loop breaks, so it does not disable anything that is not supposed to be disabled.
											if (thisParent.id == 'dyn_div') {
												fields[fnr].disabled = false;
												break;
											}
											
											//In the second possible angle, the form field is contained inside the direct child of the div field we want to show, naturally, all the form fields inside this
											//will be shown, therefore the form fields has to be activated as well.
											else if (thisParent.id == showThis) {
												fields[fnr].disabled = false;
												break;
											}
											
											//If the form fields, however, is not contained directly under the top_parent_div(dyn_div), or the div field that we want to show, we have to test the div field we are currently at,
											//To see whether or not it is supposed to be hidden or not. It does this by searching for the class value of SUB inisde the div element. 
											else if (checkForDisplay.test(thisParent.className)) {
												thisParent.style.display = 'none';
												fields[fnr].disabled = true;
												break;
											}
											
											//If none of the above angles happened, we put the next div element to search to the parent of the one we last looked at.
											//By doing this, it will continue to search parent of the parent until it finds what we are looking for. Looking above, you can see that it will search a maximum of 20 parents before it gives up. 
											//If this happens, and after 20 parents it still hasnt found anything, it will simply leave the form fields in its previous state, which is probably enabled. 
											//It will be extremely rare however, that more than 20 div fields will be contained in each other in a parent->child relation.
											else {
												thisParent = thisParent.parentNode;
											}
										}
									}
								}
								
								//Next, we have to do the exact same thing, only for the selections (we last did input form fields).
								//Being the exact same script, look at the previous loop for explaination of each part
								for (fnr = 0; fnr < selects.length; fnr++) {
									if (selects[fnr].parentNode.id == showThis) {
										selects[fnr].disabled = false;
									}
									
									else {
										var thisParent = selects[fnr].parentNode;
										for (var pnr = 0; pnr < 20; pnr++) {
											if (thisParent.id == 'dyn_div') {
												selects[fnr].disabled = false;
												break;
											}
											
											else if (thisParent.id == showThis) {
												selects[fnr].disabled = false;
												break;											
											}
											
											else if (checkForDisplay.test(thisParent.className)) {
												thisParent.style.display = 'none';
												selects[fnr].disabled = true;
												break;
											}
											
											else {
												thisParent = thisParent.parentNode;
											}
										}
									}
								}

								//Finally we have to do the same thing for textareas. Again, look above for explainations as these are functions identical to the ones used for textboxes.
								for (fnr = 0; fnr < textareas.length; fnr++) {
									if (textareas[fnr].parentNode.id == showThis) {
										textareas[fnr].disabled = false;
									}
									
									else {
										var thisParent = textareas[fnr].parentNode;
										for (var pnr = 0; pnr < 20; pnr++) {
											if (thisParent.id == 'dyn_div') {
												textareas[fnr].disabled = false;
												break;
											}
											
											else if (thisParent.id == showThis) {
												textareas[fnr].disabled = false;
												break;											
											}
											
											else if (checkForDisplay.test(thisParent.className)) {
												thisParent.style.display = 'none';
												textareas[fnr].disabled = true;
												break;
											}
											
											else {
												thisParent = thisParent.parentNode;
											}
										}
									}
								}
							}
							
							//Since we are looping through many div fields, and probably have multiple div fields on the same class sub level, we only want to show one of them.
							//Therefore, we must hide and disable the others on the same level. This is done essentially by taking the class of the div field we wanted to show in the first place,
							//find other div elements with the same class attributes, and perform a hide&disable action on everything found inside them.
							else {
								//First off we do another check to make sure that all fields user can see, are enabled, and those that he doesent see, is disabled.
								//Therefore we must once again make a variable to later search class of the div element if it is hidden by default.
								var checkForDisplay=new RegExp("sub");
								//Then, we find all divs which reside beneath (childs) of the current div element we're supposed to hide.
								var hideDivs = document.getElementById(divFields[y].id).getElementsByTagName('div');
								//Now we hide the entire field, this will automaticly hide everything inside of it, div fields being containers.
								divFields[y].style.display = 'none';
								//Next we disable all the input fields. No special logic is required here, because we must disable everything anyway, independent of sub of sub divs, etc.
								for (var fnr = 0; fnr < fields.length; fnr++) {
									fields[fnr].disabled = true;
								}
								
								//Next we disable all the select fields, which must be handled seperately from the input fields.
								for (fnr = 0; fnr < selects.length; fnr++) {
									selects[fnr].disabled = true;
								}
								
								//We also have to disable the textareas
								for (fnr = 0; fnr < textareas.length; fnr++) {
									textareas[fnr].disabled = true;
								}
								
								//Lastly we hide all the div elements which we earlier found to be residing beneath the current one.
								//This is essential because the user may click back and forth through different choices. This last bit is to prevent the user from ever seeing
								//a field that is disabled. Earlier on we made sure the user never is required to fill a field which he can't see. 
								for (var htd = 0; htd < hideDivs.length; htd++) {
									if (checkForDisplay(hideDivs[htd].className)) {
										hideDivs[htd].style.display = 'none';
									}
								}
							}
						}
					}
					
					//This is the end of the try command. Here we can enable the last line commented out, if we wish to debug the script, and see what errors occur.
					//Undoubtly, it will contain undefined errors, which is not critical for the script success, but would interrupt it, should it not have been ignored by the try statement.
					catch(err) {
						//Bug catcher, remove the two slashes below to display the insignificant errors that occur through the script.
						//alert(err.description);
					}
				}
			}
		}

    


