// noiseJavaScript.js

	// pass in two div names, show the first one, hide the second
	function swapDivs(divToShow,divToHide) {
			showDiv(divToShow);
			hideDiv(divToHide);
	}

	// show a hidden div
	function showDiv (divName) {
		document.getElementById(divName).style.display = "block";
	}

	// hide a visible div
	// using display:none and display:block as opposed to visible as we don't want hidden divs to take up any real estate
	function hideDiv (divName) {
		document.getElementById(divName).style.display = "none";
	}

	// shows the loading... timer div
	// in addition to showing the div with the image in it, also need to set the timeout to avoid the problem in IE
	// where animate gifs freeze
	function showHolding(divToShow,divToHide,imageId) {
		swapDivs(divToShow,divToHide);
		var imageSrc = document.getElementById(imageId).src;
		setTimeout("document.getElementById('" + imageId + "').src = '" + imageSrc + "'", 100);
	}

	// populate a hidden field with a value that will get submitted to the server to prove javascript is enabled
	function setJavaScriptEnabled (hiddenJavaScript) {
		document.getElementById(hiddenJavaScript).value = 'true';
	}

	// populate a hidden field with a value so that we don't need to go back to the server to do it
	function setDisclaimerAccepted(hiddenDisclaimer) {
		document.getElementById(hiddenDisclaimer).value = 'Accepted';
	}


	// onchange check that the button can be enabled and at the sametime set focus on it
	function enableThenFocus(inputPostcode, buttonGoJS) {

		if(enableGoButton(inputPostcode, buttonGoJS)) {
			document.getElementById(buttonGoJS).focus()
		}
	}


	// onkeyup check that the user has entered more than two characters before enabling Go
	function enableGoButton(inputPostcode, buttonGoJS) {
		if(document.getElementById(inputPostcode).value.length > 1) {
			// Reg Expression check
			var postCode = document.getElementById(inputPostcode).value;
			var regEx = /^[a-zA-Z]{1,2}[0-9][0-9a-zA-Z\\s]*/;
			var check = postCode.match(regEx);
			if(check != null) {
				document.getElementById(buttonGoJS).disabled = false;
				document.getElementById(buttonGoJS).className = 'btn';
				return true;
			} else {
				document.getElementById(buttonGoJS).disabled = true;
				document.getElementById(buttonGoJS).className = 'btn btnDisabled';
				return false;
			}
		} else {
			document.getElementById(buttonGoJS).disabled = true;
			document.getElementById(buttonGoJS).className = 'btn btnDisabled';
			return false;
		}
		return false;
	}


	// Decide div to display based on disclaimer accepted or not
	function showNextDiv (hiddenDisclaimer, imageId, buttonAccept) {

		if(document.getElementById(hiddenDisclaimer) && document.getElementById(hiddenDisclaimer).value == 'Accepted') {
		// show Holding
			hideDiv('divSecondLevel');
			showHolding('divHolding','divSelectLayer',imageId);
			return true;
		} else {
		// show Disclaimer - set focus to decline button
			swapDivs('divDisclaimer','divSelectLayer');
			document.getElementById(buttonAccept).focus();
			return false;
		}
	}

	// function to decide on subsequent visits to the page which divs are available
	function initialiseMenus (menuSource, menuLayer, menuLocation, inputPostcode, hiddenDisclaimer, buttonGoJS) {

		changeSource(menuSource, menuLayer, menuLocation, inputPostcode, hiddenDisclaimer, buttonGoJS);

	}

	// function to determine what to place in the layerDropDown based on the source selection
	function changeSource(menuSource, menuLayer,menuLocation,inputPostcode,hiddenDisclaimer,buttonGoJS) {

		  var currentLayer = document.getElementById(menuLayer).value;

		  var options = document.getElementById(menuLayer).options;

		  for (var i = options.length - 1; i > 0; i--) {
		  	options[i] = null;
		  }
		  var selectedSource = layerArray[document.getElementById(menuSource).selectedIndex];

		  if(selectedSource.length > 1) {
			  for (var j = 0; j < selectedSource.length; j++) {
			    options[j] = new Option(selectedSource[j].text,selectedSource[j].value);
			    if(selectedSource[j].value == currentLayer) {
			    	options[j].selected = true;
			    }
			  }
 				showDiv('divLayer');
 				// if the postcode entry was previously hidden i.e. we were looking at a dropdown
				// then as well as enabling it, clear its contents so we don't see postcode for prev airport
				document.getElementById(inputPostcode).value = "";
		  } else {
				options[0] = new Option(selectedSource[0].text,selectedSource[0].value);
				hideDiv('divLayer');

		  }

		  changeLayer(menuSource, menuLayer,menuLocation,inputPostcode,hiddenDisclaimer,buttonGoJS);
	}

	// function to determine what to place in teh layer menu based on the other selections on the page
	function changeLayer(menuSource, menuLayer, menuLocation, inputPostcode, hiddenDisclaimer,buttonGoJS) {

  	  var currentLocation = document.getElementById(menuLocation).value;

	  var options = document.getElementById(menuLocation).options;
	  for (var i = options.length - 1; i > 0; i--) {
	  	options[i] = null;
	  }
	  var selectedLayer = locationArray[document.getElementById(menuSource).selectedIndex][document.getElementById(menuLayer).selectedIndex];
  	  if(selectedLayer.length >= 1) {



		  for (var j = 0; j < selectedLayer.length; j++) {
		    options[j] = new Option(selectedLayer[j].text,selectedLayer[j].value);

		    if(selectedLayer[j].value == currentLocation) {
				  options[j].selected = true;
		    }
		    // multiple locations then show location dropdown and hide input postcode
		    if(selectedLayer.length > 1) {
			    swapDivs('divLocation','divPostcode');
			} else {
				// if dropdown only has one element then hide it
				hideDiv('divPostcode');
				hideDiv('divLocation');
			}
			document.getElementById(menuLocation).disabled = false;
			document.getElementById(inputPostcode).disabled = true;
			document.getElementById(buttonGoJS).disabled = false;
			document.getElementById(buttonGoJS).className = 'btn';
		  }
	  } else {
  		    // show input postcode, hide location dropdown
			swapDivs('divPostcode','divLocation');
			document.getElementById(inputPostcode).disabled = false;
			document.getElementById(menuLocation).disabled = true;
			enableGoButton(inputPostcode,buttonGoJS);
	  }
	}

	// function to decide what to place in the timeslice menu based on other selections on the page
	function changeTimeslice(menuSource,menuTimeslice, menuLayer) {

		var currentTime = document.getElementById(menuTimeslice).value;

  	  	var timeOptions = document.getElementById(menuTimeslice).options;
		for (var i = timeOptions.length - 1; i > 0; i--) {
	  		timeOptions[i] = null;
		}

  		var selectedTime = timesliceArray[document.getElementById(menuSource).selectedIndex][document.getElementById(menuLayer).selectedIndex];
  		for (var t = 0; t < selectedTime.length; t++) {
	    	timeOptions[t] = new Option(selectedTime[t].text,selectedTime[t].value);
	    	if(selectedTime[t].value == currentTime) {
	    		timeOptions[t].selected = true;
	    	}
		}


	}

	// need to show and hide quite a few elements on the page for maps
	function showMaps() {
		document.getElementById('divMapTitle').style.display = "inline";
		hideDiv('divChartTitle');
		swapDivs('divMapPane','divChartPane');
		swapDivs('divMapLegend','divChartLegend');
	}

	// need to show and hide quite a few elements on the page for charts
	function showCharts() {
		document.getElementById('divChartTitle').style.display = "inline";
		hideDiv('divMapTitle');
		swapDivs('divChartPane','divMapPane');
		swapDivs('divChartLegend','divMapLegend');
	}

