function NM_onload()
{
	document.getElementById("fldLocation0").focus();
	calcdose();											// In case Back button pressed from Print Report page
}

function ZeroPad(n)
{
	sPad = n.toFixed(0);
	if(sPad.length == 1)
		sPad = "0" + sPad;
		
	return sPad;
}

function calcdose()
{
	flLineDose = 0;
	flTotalDose = 0;
	nTotalTime = 0;
	
	for(iLine=0; iLine<nLineCount; iLine++)									// Lepd for each line
	{
		nLevel = Number(document.getElementById("fldLevel" + iLine).value);
		nHour = Number(document.getElementById("fldTimeHr" + iLine).value);
		nMin = Number(document.getElementById("fldTimeMin" + iLine).value);
		nSec = nHour * 3600 + nMin * 60;
		
		if((nLevel >= 40) && (nLevel <= 140) && (nSec >= 60) && (nSec <= 86400))
		{
			flLineDose = 100 * nSec / (28800 / Math.pow(2,(nLevel-90)/5));
			
			flTotalDose += flLineDose;
			nTotalTime += nSec;
			
			divDose = document.getElementById("divDose" + iLine);
			
			if(flLineDose < 50.0)
				divDose.className = "";
			if((flLineDose >= 50.0) && (flLineDose < 100.0))
				divDose.className = "firstaction";
			if(flLineDose >= 100.0)
				divDose.className = "secondaction";
				
			divDose.innerHTML = flLineDose.toFixed(1) + "%";
			document.getElementById("fldDose" + iLine).value = flLineDose.toFixed(1);
		}
		else
		{
			document.getElementById("divDose" + iLine).innerHTML = "";
			document.getElementById("fldDose" + iLine).value = "";
		}
	}
	
	if((flTotalDose > 0) && (nTotalTime <= 86400))					// Show total Dose, TWA and Time
	{
		nHours = Math.floor(nTotalTime / 3600);
		nMins = ((nTotalTime / 3600) - Math.floor(nTotalTime / 3600)) * 60;
		sExpTime = ZeroPad(nHours) + ":" + ZeroPad(nMins);
		document.getElementById("divTotalTime").innerHTML = sExpTime;
		
		flTWA = 16.61 * (Math.log(flTotalDose/100) / Math.LN10) + 90;
		flTWA = Math.round(flTWA * 10) / 10;
		
		if(flTWA < 85.0)
			document.getElementById("divTotalDose").className = document.getElementById("divTWA").className = "";
		if((flTWA >= 85.0) && (flTWA < 90.0))
			document.getElementById("divTotalDose").className = document.getElementById("divTWA").className = "firstaction";
		if(flTWA >= 90.0)
			document.getElementById("divTotalDose").className = document.getElementById("divTWA").className = "secondaction";

		document.getElementById("divTotalDose").innerHTML = flTotalDose.toFixed(1) + "%";
		document.getElementById("divTWA").innerHTML = flTWA.toFixed(1) + " dB";
		
		document.getElementById("fldTotalDose").value = flTotalDose.toFixed(1);
		document.getElementById("fldTotalExpTime").value = sExpTime;		
		document.getElementById("fldTWA").value = flTWA.toFixed(1);
		
		ShowActions(flTWA);
	}
	else
	{
		document.getElementById("divTotalDose").innerHTML = "";
		document.getElementById("divTotalTime").innerHTML = "";
		document.getElementById("divTWA").innerHTML = "";
		ShowActions(0);
	}
}

function ShowActions(flTWA)
{
	if(flTWA > 0)
	{
		if(flTWA < 85)
		{
			document.getElementById("divActionLevels").innerHTML = "TWA is less<br>than 85 dB";
			document.getElementById("divActionAction").innerHTML = "The worker's time weighted average is less than the first action level so <strong>no further action is required</strong> other than regular noise measurement, especially when there are any changes to the environment or machinery.";
			document.getElementById("fldAction").value = "The worker's time weighted average is less than the first action level so <strong>no further action is required</strong> other than regular noise measurement, especially when there are any changes to the environment or machinery.";
		}

		if((flTWA >= 85) && (flTWA < 90))
		{
			document.getElementById("divActionLevels").innerHTML = "TWA is between<br>85 and 90 dB";
			document.getElementById("divActionAction").innerHTML = "<p>The worker's time weighted average is above the first action level of 85 dB.</p><ul><li>Reduce the noise at source wherever possible.</li><li>Provide the worker with training about hearing damage and protection.</li><li>Provide suitable hearing protection to be worn if the worker chooses to.</li><li>Carry out regular monitoring of the noise levels to ensure they have not increased.  </li></ul>";
			document.getElementById("fldAction").value = "<p>The worker's time weighted average is above the first action level of 85 dB.</p><ul><li>Reduce the noise at source wherever possible.</li><li>Provide the worker with training about hearing damage and protection.</li><li>Provide suitable hearing protection to be worn if the worker chooses to.</li><li>Carry out regular monitoring of the noise levels to ensure they have not increased.  </li></ul>";
		}
		if((flTWA >= 90) && (flTWA < 100))
		{
			document.getElementById("divActionLevels").innerHTML = "TWA is<br>above 90 dB";
			document.getElementById("divActionAction").innerHTML = "<p>The worker's time weighted average is above the second action level of 90 dB.</p><ul><li>Reduce the noise at source wherever possible</li><li>Provide the worker with training about hearing damage and protection.</li><li><strong>Provide suitable hearing protection, which must be worn</strong></li><li>Carry out regular monitoring of the noise levels to ensure they have not increased.</li></ul>";
			document.getElementById("fldAction").value = "<p>The worker's time weighted average is above the second action level of 90 dB.</p><ul><li>Reduce the noise at source wherever possible</li><li>Provide the worker with training about hearing damage and protection.</li><li><strong>Provide suitable hearing protection, which must be worn</strong></li><li>Carry out regular monitoring of the noise levels to ensure they have not increased.</li></ul>";
		}
		if(flTWA >= 100)
		{
			document.getElementById("divActionLevels").innerHTML = "Exposure is<br>very high";
			document.getElementById("divActionAction").innerHTML = "<p>The worker's time weighted average is well above the second action. This noise level is so high that a more detailed analysis will be required to ensure that the hearing protection provided is adequate.</p><ul><li>Reduce the noise at source wherever possible</li><li>Provide the worker with training about hearing damage and protection.</li><li><strong>Provide suitable hearing protection, which must be worn</strong></li><li><strong>Carry out further analysis to ensure that the protection provided is adequate for the job</strong>. </li></ul>";
			document.getElementById("fldAction").value = "<p>The worker's time weighted average is well above the second action. This noise level is so high that a more detailed analysis will be required to ensure that the hearing protection provided is adequate.</p><ul><li>Reduce the noise at source wherever possible</li><li>Provide the worker with training about hearing damage and protection.</li><li><strong>Provide suitable hearing protection, which must be worn</strong></li><li><strong>Carry out further analysis to ensure that the protection provided is adequate for the job</strong>. </li></ul>";
		}

		document.getElementById("btnPrint").style.visibility = "visible";
	}
	else
	{
		document.getElementById("divActionLevels").innerHTML = "";
		document.getElementById("divActionAction").innerHTML = "Details will be shown when valid levels and times have been entered.";
		document.getElementById("fldAction").value = "";
		
		document.getElementById("btnPrint").style.visibility = "hidden";
	}
}

function help(sSubject)
{
	sHelp = "";
	
	if(sSubject == "location")
		sHelp = "Enter the location of the measurement.<br><br>Only needed if you want a printed report.";
	if(sSubject == "level")
		sHelp = "The average sound level in dB(A), measured for the worker at this location.";
	if(sSubject == "exptime")
		sHelp = "The length of time that the worker spends at this location each day.";
	if(sSubject == "partial")
		sHelp = "The noise dose % at each location based on the time spent at that paticular point. Useful in identifying the areas that would most benefit from improvement.";
	if(sSubject == "totalexptime")
		sHelp = "The total amount of time that the worker is exposed to noise during a working day.";
	if(sSubject == "dose")
		sHelp = "The worker's daily noise exposure <strong>Dose %</strong>. This is the parameter to check against the regulatory action levels of 50% and 100%.";
	if(sSubject == "twa")
		sHelp = "The worker's time weighted average <strong>TWA</strong> in dB. This is the parameter to check against the regulatory action levels of 85 and 90 dB.";
		
	if(sHelp != "")
		sHelp = "<strong>Help</strong><br><br>" + sHelp;

	document.getElementById("divHelp").innerHTML = sHelp;
	document.getElementById("divHelp").style.visibility = (sHelp == "" ? "hidden" : "visible");
}

function CheckSubmit()
{
	return (document.getElementById("btnPrint").style.visibility == "visible");
}