function validateForm(){
	var elem = document.getElementById('submitForm').elements;
	var submitNow = true;
	for(var i = 0; i < elem.length; i++){
		if(elem[i].type == "text" && elem[i].value == ""){
			alert("Please fill in your " + removeDashes(elem[i].name));
			elem[i].focus();
			submitNow = false;
			break;
		}
		if(elem[i].name == "email-address" && !echeck(elem[i].value)){
			alert("Please fill in a valid email address");
			elem[i].focus();
			submitNow = false;
			break;
		}
		if(elem[i].type == "checkbox" && !elem[i].checked){
			alert("Please check the box allowing us to contact you.");
			elem[i].focus();
			submitNow = false;
		}
	} 
	if(submitNow){
		document.submitForm.submit();
	}
}
function validateNLForm(){
	if(document.getElementById('name').value == ""){
		alert("Please fill in your name");
		document.getElementById('name').focus();
	}
	else if(document.getElementById('Company').value == ""){
		alert("Please fill in your company name");
		document.getElementById('Company').focus();
	}
	else if(document.getElementById('ntikju-ntikju').value == "" || !echeck(document.getElementById('ntikju-ntikju').value)){
		alert("Please fill in a valid email address");
		document.getElementById('ntikju-ntikju').focus();
	}
	else if(!document.getElementById('opt-in').checked){
			alert("Please check the box allowing us to contact you.");
	}
	else{
		document.subForm.submit();
	}
}
function removeDashes(stringValue){
	var newString = "";
	for(i=0;i<=stringValue.length;i++){
		var currentLetter = stringValue.slice(i,i+1);
		if(currentLetter == "-"){
			newString += " ";
		}
		else{
			newString += currentLetter;
		}
	}
	return newString;
}
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	 if (str.indexOf(" ")!=-1){
		return false
	 }
	 return true					
}
function initDropdowns(){
	$("#nav-products").mouseenter(function(){
		clearTimeout($("#nav-products").data('timeoutId'));
		$("#products-drop-down").fadeIn("fast");
	}).mouseleave(function(){
		var timeoutId = setTimeout(function(){ $("#products-drop-down").fadeOut("fast");}, 100);
		$("#nav-products").data('timeoutId', timeoutId);
	});
	$("#products-drop-down").mouseenter(function(){
		clearTimeout($("#nav-products").data('timeoutId'));
		 $("#products-drop-down").fadeIn("fast");
	}).mouseleave(function(){
		clearTimeout($("#nav-products").data('timeoutId'));
		var timeoutId = setTimeout(function(){ $("#products-drop-down").fadeOut("fast");}, 100);
		$("#nav-products").data('timeoutId', timeoutId);
	});
	$("#nav-solutions").mouseenter(function(){
		clearTimeout($("#nav-solutions").data('timeoutId'));
		$("#solutions-drop-down").fadeIn("fast");
	}).mouseleave(function(){
		var timeoutId = setTimeout(function(){ $("#solutions-drop-down").fadeOut("fast");}, 100);
		$("#nav-solutions").data('timeoutId', timeoutId);
	});
	$("#solutions-drop-down").mouseenter(function(){
		clearTimeout($("#nav-solutions").data('timeoutId'));
		 $("#solutions-drop-down").fadeIn("fast");
	}).mouseleave(function(){
		clearTimeout($("#nav-solutions").data('timeoutId'));
		var timeoutId = setTimeout(function(){ $("#solutions-drop-down").fadeOut("fast");}, 100);
		$("#nav-solutions").data('timeoutId', timeoutId);
	});	
}
function initDropdownmenu(){
	$("#region-drop-down").mouseenter(function(){
		clearTimeout($("#region-drop-down").data('timeoutId'));
		$("#region-drop-down-body").fadeIn("fast");
	}).mouseleave(function(){
		var timeoutId = setTimeout(function(){ $("#region-drop-down-body").fadeOut("fast");}, 100);
		$("#region-drop-down").data('timeoutId', timeoutId);
	});
	$("#region-drop-down-body").mouseenter(function(){
		clearTimeout($("#region-drop-down").data('timeoutId'));
		 $("#region-drop-down-body").fadeIn("fast");
	}).mouseleave(function(){
		clearTimeout($("#region-drop-down").data('timeoutId'));
		var timeoutId = setTimeout(function(){ $("#region-drop-down-body").fadeOut("fast");}, 100);
		$("#region-drop-down").data('timeoutId', timeoutId);
	});
}
function initSlider(){
	//Show the paging and activate its first link
	$(".paging").show();
	$(".paging div:first").addClass("active");
	
	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel div").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging  and Slider Function
	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
	
		$(".paging div").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
	
		//Slider Animation
		$(".image_reel").animate({
			left: -image_reelPosition
		}, 500 );
	
	}; 
	
	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = $('.paging div.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging div:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (7 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});	
	
	//On Click
	$(".paging div").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
}
var selectedAccordion = null;
function initAccordion(){
	$('.main-accordion li').each(function(index) {
		$(this).children(".accordion-outer").animate({height:'toggle'},1);
		$(this).children(".accordion-link").click(function() {
			animateAccordion(index);
		});
    });
	expandMenu = false;
}
var expandMenu = true;
function menuHeight(){
	if(document.getElementById('main-menu') != null){
		if(document.getElementById('main-menu').offsetHeight > document.getElementById('main-base').offsetHeight && expandMenu == true){
			document.getElementById('main-base').style.height = (document.getElementById('main-menu').offsetHeight) + "px";
		}
		else{
			document.getElementById('main-menu').style.height = (document.getElementById('main-base').offsetHeight) + "px";
		}
	}
}
function animateAccordion(accordionId){
	$(".main-accordion li").each(function(index) {
		var arrow = $(this).children(".accordion-link").children("div").children(".accordion-arrow");
		if(index == accordionId || index == selectedAccordion){
			$(this).children(".accordion-outer").animate({height:'toggle'},500);
			if(accordionId == index && accordionId != selectedAccordion){
				arrow.attr("src", relativePath + "images/accordion-arrow-down.gif");
			}
			else if(selectedAccordion == index){
				arrow.attr("src",  relativePath + "images/accordion-arrow-up.gif");
			}
			else if(selectedAccordion == accordionId){
				arrow.attr("src",  relativePath + "images/accordion-arrow-up.gif");
				
			}
			else{
				arrow.attr("src", relativePath + "images/accordion-arrow-up.gif");
			}
		}
    });
	if(selectedAccordion == accordionId){
		selectedAccordion = null;
	}
	else{
		selectedAccordion = accordionId;
	}
}
var regionArray = new Array("Sales director", "Scotland", "Northern England", "Midland & East Anglia", "South and Ireland", "Sales supervisor", "Sales administrator");
function displayRegionalDetails(regionalId){
	for(i=0;i<=6;i++){
		currentDetails = document.getElementById('regional-' + i);
		if(i == regionalId){
			currentDetails.style.display = "block";
			selectRegion(regionalId);
		}
		else{
			currentDetails.style.display = "none";
		}
	}
	document.getElementById('region-drop-down-body').style.display = "none";
	document.getElementById('region-drop-down').innerHTML = regionArray[regionalId];
}
var selectedRegion = 0;
function changeMapColour(regionalId){
	if(regionalId > 0 && regionalId <= 4){
		document.getElementById('regionmap').src =  relativePath + "images/tubosider-regional-sales-uk-map-" + regionalId + ".gif";
	}
	else{
		document.getElementById('regionmap').src =  relativePath + "images/tubosider-regional-sales-uk-map.gif";
	}
}
function selectRegion(regionalId){
	selectedRegion = regionalId;
	changeMapColour(selectedRegion);
}
function CalculateTankWidth(volume, diameter, numberOfLegs) {

	// Ensure floating-point or integer
	volume = volume * 1.0;
	diameter = diameter * 1.0;
	numberOfLegs = numberOfLegs * 1;

	var tankWidth = (1 == numberOfLegs)
		? diameter
		: (diameter * numberOfLegs) + (0.6 * (numberOfLegs - 1));

	return tankWidth;
}

function CalculateTankLength(volume, diameter, numberOfLegs) {

	// Ensure floating-point or integer
	volume = volume * 1.0;
	diameter = diameter * 1.0;
	numberOfLegs = numberOfLegs * 1;

	var tankWidth = (1 == numberOfLegs)
		? diameter
		: (diameter * numberOfLegs) + (0.6 * (numberOfLegs - 1));

	var area = diameter * diameter * Math.PI / 4.0;
	var length = volume / area;
	var tankLength = (length - (tankWidth * 2.0)) / numberOfLegs + (diameter * 2);
	return tankLength;
}
function CalculateTankWidth(volume, diameter, numberOfLegs) {

	// Ensure floating-point or integer
	volume = volume * 1.0;
	diameter = diameter * 1.0;
	numberOfLegs = numberOfLegs * 1;

	var tankWidth = (1 == numberOfLegs)
		? diameter
		: (diameter * numberOfLegs) + (0.6 * (numberOfLegs - 1));

	return tankWidth;
}

function CalculateTankLength(volume, diameter, numberOfLegs) {

	// Ensure floating-point or integer
	volume = volume * 1.0;
	diameter = diameter * 1.0;
	numberOfLegs = numberOfLegs * 1;

	var tankWidth = (1 == numberOfLegs)
		? diameter
		: (diameter * numberOfLegs) + (0.6 * (numberOfLegs - 1));

	var area = diameter * diameter * Math.PI / 4.0;
	var length = volume / area;
	var tankLength = (length - (tankWidth * 2.0)) / numberOfLegs + (diameter * 2);
	return tankLength;
}
function CalculateSafetyFactor(pipeDiameter, depthOfCover, waterDepthAboveInvert) {

	// Ensure floating-point
	pipeDiameter = pipeDiameter * 1.0;
	depthOfCover = depthOfCover * 1.0;
	waterDepthAboveInvert = waterDepthAboveInvert * 1.0;

	var DENSITY_OF_DRY_FILL = 1.900; // t/m^3
	var DENSITY_OF_WET_FILL = 1.100; // t/m^3

	var pipeRadius = pipeDiameter / 2.0;
	// alert("pipeRadius = " + pipeRadius);
	var pipeArea = (pipeRadius * pipeRadius) * Math.PI;
	// alert("pipeArea = " + pipeArea);
	var cordDepth = (waterDepthAboveInvert >= pipeDiameter)
		? 0.0
		: (waterDepthAboveInvert < pipeRadius)
			? waterDepthAboveInvert
			: pipeDiameter - waterDepthAboveInvert;
	// alert("cordDepth = " + cordDepth);
	var pipeRadiusLessCordDepth = pipeRadius - cordDepth;
	// alert("pipeRadiusLessCordDepth = " + pipeRadiusLessCordDepth);
	var semiCord = Math.sqrt((pipeRadius * pipeRadius) - (pipeRadiusLessCordDepth * pipeRadiusLessCordDepth));
	// alert("semiCord = " + semiCord);
	var fiveEighthsCordDepth = 0.625 * cordDepth;
	var cordArea = (waterDepthAboveInvert == pipeRadius)
		? pipeArea / 2.0
		: (4.0 * cordDepth / 3.0) * Math.sqrt((fiveEighthsCordDepth * fiveEighthsCordDepth) + (semiCord * semiCord));
	// alert("cordArea = " + cordArea);
	var volumeDisplacedPerMetreRun = (waterDepthAboveInvert >= pipeDiameter)
		? pipeArea
		: (waterDepthAboveInvert < pipeRadius)
			? cordArea
			: pipeArea - cordArea;
	var upthrust = volumeDisplacedPerMetreRun; // Density of water is 1.0
	// alert("upthrust = " + upthrust);

	// alert("pipeDiameter = " + pipeDiameter);
	// alert("depthOfCover = " + depthOfCover);
	// alert("waterDepthAboveInvert = " + waterDepthAboveInvert);
	var depthToInvert = pipeDiameter + depthOfCover;
	// alert("depthToInvert = " + depthToInvert);
	var depthOfCoverDry = (waterDepthAboveInvert > pipeDiameter)
		? depthToInvert - waterDepthAboveInvert
		: depthOfCover;
	// alert("depthOfCoverDry = " + depthOfCoverDry);
	var volumeOfDryFill = depthOfCoverDry * pipeDiameter;
	// alert("volumeOfDryFill = " + volumeOfDryFill);
	var weightOfDryFill = volumeOfDryFill * DENSITY_OF_DRY_FILL;
	// alert("weightOfDryFill = " + weightOfDryFill);
	var depthOfCoverWet = (waterDepthAboveInvert >= pipeDiameter)
		? waterDepthAboveInvert - pipeDiameter
		: 0.0;
	// alert("depthOfCoverWet = " + depthOfCoverWet);
	var volumeOfWetFill = depthOfCoverWet * pipeDiameter;
	// alert("volumeOfWetFill = " + volumeOfWetFill);
	var weightOfWetFill = volumeOfWetFill * DENSITY_OF_WET_FILL;
	// alert("weightOfWetFill = " + weightOfWetFill);
	var totalDownthrust = weightOfDryFill + weightOfWetFill;
	// alert("totalDownthrust = " + totalDownthrust);

	var factorOfSafety = (0.0 == upthrust)
		? "Not calculated"
		: (totalDownthrust / upthrust >= 0)
			? Math.round(totalDownthrust / upthrust * 100) / 100
			: "Impossible";

	return factorOfSafety;
}
function calculateFloatation() {
	var pipeDiameterElement = document.getElementById("pipeDiameter");
	var depthOfCoverElement = document.getElementById("depthOfCover");
	var waterDepthAboveInvertElement = document.getElementById("waterDepthAboveInvert");
	var factorOfSafety = CalculateSafetyFactor(pipeDiameterElement.value, depthOfCoverElement.value, waterDepthAboveInvertElement.value);
	var resultsHTML = "<strong>Factor of safety:<br />" + factorOfSafety + "</strong><br />";
	if(factorOfSafety >= 1.2){
		document.getElementById('error-message').style.color = "";
	}
	else{
		document.getElementById('error-message').style.color = "#ff0000";
	}
	
	document.getElementById('floatation-results').innerHTML = resultsHTML;
}

function calculateAttenuation() {
	var volumeElement = document.getElementById("volume");
	var diameterElement = document.getElementById("diameter");
	var numberOfLegsElement = document.getElementById("numberOfLegs");
	var tankWidth = CalculateTankWidth(volumeElement.value, diameterElement.value, numberOfLegsElement.value);
	var calculatedTankWidth = (tankWidth >= 0) ? Math.round(tankWidth * 1000) / 1000 + "m" : "Impossible";
	var tankLength = CalculateTankLength(volumeElement.value, diameterElement.value, numberOfLegsElement.value);
	var calculatedTankLength = (tankLength >= 0) ? Math.round(tankLength * 1000) / 1000 + "m" : "Impossible";
	var resultsHTML = "<strong>Tank dimensions:</strong><br />";
	resultsHTML += "Width: &nbsp;&nbsp;<strong>" + calculatedTankWidth + "</strong><br />";
	resultsHTML += "Height: &nbsp;<strong>" + calculatedTankLength + "</strong>";
	document.getElementById('attenuation-results').innerHTML = resultsHTML;
}

function focusFormInputs(formName){
	var elem = document.getElementById(formName).elements;
	for(var i = 0; i < elem.length; i++) {
		var currentInput =  elem[i];
		currentInput.initValue = currentInput.value;
		currentInput.onfocus = function(){
			if(this.value == this.initValue || this.value == "Enter a number"){
				this.value = "";
			}
		}
		currentInput.onblur = function(){
			if(this.value == ""){
				this.value = this.initValue;
			}
			if(isNaN(this.value) && this.value != this.initValue){
				this.value = "Enter a number";
			}
		}
	} 
}


