function NM_onload()
{
	if(document.getElementById("fldRep").value == "oct")
	{
		calcoct();											// In case Back button pressed from Print Report page
		document.getElementById("fldBand0").focus();
	}
	if(document.getElementById("fldRep").value == "hml")
	{
		calchml();											// In case Back button pressed from Print Report page
		document.getElementById("fldH").focus();
	}
	if(document.getElementById("fldRep").value == "nrr")
	{
		showprot();
		calcnrr();											// In case Back button pressed from Print Report page
		document.getElementById("fldNRR").focus();
	}
}

function calcoct()
{
	var anAWgt = new Array(8);
	var anAPV = new Array(8);
	var anMeas = new Array(8);
	
	anAWgt[0] = -26.2;
	anAWgt[1] = -16.1;
	anAWgt[2] = -8.6;
	anAWgt[3] = -3.2;
	anAWgt[4] = 0.0;
	anAWgt[5] = 1.2;
	anAWgt[6] = 1.0;
	anAWgt[7] = -1.1;

	bGotAll = true;
	bShowGraph = false;
	nSum = 0.0;
	for(iBand=0; iBand<8; iBand++)
	{
		sAPV = String(document.getElementById("fldBand" + iBand).value);
		nAPV = Number(sAPV);
		sMeas = String(document.getElementById("fldMeas" + iBand).value);
		nMeas = Number(sMeas);
		
		nRes = 0;
		if((sAPV != "") && (sMeas != "") && (nAPV >= 0) && (nAPV <= 60) && (nMeas >= 0) && (nMeas <= 140))
		{
			nRes = nMeas + anAWgt[iBand] - nAPV;
			nSum += Math.pow(10,nRes/10.0);
		}
		else
			bGotAll = false;

		nBar = (nMeas - 20.0) / 2;
		if((nBar >= 0) && (nBar <= 60))
		{
			document.getElementById("tdMeas" + iBand).style.backgroundPosition = "0px -" + nBar.toFixed(0) + "px";
			bShowGraph = true;
		}
		else
			document.getElementById("tdMeas" + iBand).style.backgroundPosition = "0px 0px";

		nBar = (nRes - 20.0) / 2;
		if((nBar >= 0) && (nBar <= 60))
		{
			document.getElementById("tdRes" + iBand).style.backgroundPosition = "0px -" + nBar.toFixed(0) + "px";
			bShowGraph = true;
		}
		else
			document.getElementById("tdRes" + iBand).style.backgroundPosition = "0px 0px";
		
		document.getElementById("divGraph").style.visibility = bShowGraph ? "visible" : "hidden";
	}
	
	if(bGotAll)
	{
		nEarpro = 10 * (Math.log(nSum) / Math.LN10)
		
		document.getElementById("divCalc").innerHTML = nEarpro.toFixed(1);
		document.getElementById("fldCalc").value = nEarpro.toFixed(1);
		
		ShowSummary(nEarpro);
	}
	else
	{
		document.getElementById("divCalc").innerHTML = "";
		document.getElementById("fldCalc").value = "";
		
		ShowSummary(0);
	}
}

function calchml()
{
	nH = Number(document.getElementById("fldH").value);
	nM = Number(document.getElementById("fldM").value);
	nL = Number(document.getElementById("fldL").value);
	
	nLA = Number(document.getElementById("fldLA").value);
	nLC = Number(document.getElementById("fldLC").value);
	
	if((nH >= 0) && (nH <= 60) && (nM >= 0) && (nM <= 60) && (nL >= 0) && (nL <= 60) &&
       (nLA >= 20) && (nLA <= 140) && (nLC >= 20) && (nLC <= 140))
	{
		if((nLC - nLA) > 2)
			nPNR = nM - (((nM - nL) / 8) * (nLC - nLA - 2));
		else
			nPNR = nM - (((nH - nM) / 4) * (nLC - nLA - 2));
		
		nEarpro = nLA - nPNR;

		document.getElementById("divCalc").innerHTML = nEarpro.toFixed(1);
		document.getElementById("fldCalc").value = nEarpro.toFixed(1);
		
		ShowSummary(nEarpro);
	}
	else
	{
		document.getElementById("divCalc").innerHTML = "";
		document.getElementById("fldCalc").value = "";
		
		ShowSummary(0);
	}
}

function GetRad(rad)
{
	for(i=0; i<rad.length; i++)
		if(rad[i].checked)
			return(rad[i].value);
			
	return("");
}

function calcnrr()
{
	var nNRR = Number(document.getElementById("fldNRR").value);
	var nLAC = Number(document.getElementById("fldLAC").value);

	var sWeight = GetRad(document.formNRR.radWeight);
	var sMethod = GetRad(document.formNRR.radMethod);
	var sType = GetRad(document.formNRR.radProtType);
	
	if((nNRR >= 0) && (nNRR <= 60) && (nLAC >= 20) && (nLAC <= 140))
	{
		switch(sMethod)
		{
			case "OSHA" :
			{
				if(sWeight == "A")
					nEarpro = nLAC - ((nNRR - 7) / 2);
				else
					nEarpro = nLAC - (nNRR / 2);
				break;
			}
			
			case "NIOSH" :
			{
				if(sWeight == "A")
					nEarpro = nLAC - (nNRR - 7);
				else
				{
					nEarPro = nLAC;							// Just in case
					if(sType == "plug") nEarpro = nLAC - (nNRR * 0.5);
					if(sType == "other") nEarpro = nLAC - (nNRR * 0.3);
					if(sType == "muff") nEarpro = nLAC - (nNRR * 0.75);
				}
				break;
			}
			
			default : nEarpro = nLAC;						// Shouldn't happen
		}

		document.getElementById("divCalc").innerHTML = nEarpro.toFixed(1);
		document.getElementById("fldCalc").value = nEarpro.toFixed(1);
		
		document.getElementById("fldWeight").value = sWeight;					// For report generator
		document.getElementById("fldMethod").value = sMethod;
		document.getElementById("fldProtType").value = sType;
	
		ShowSummary(nEarpro);
	}
	else
	{
		document.getElementById("divCalc").innerHTML = "";
		document.getElementById("fldCalc").value = "";
		
		ShowSummary(0);
	}
}

function showprot()
{	
	var sMethod = GetRad(document.formNRR.radMethod);
	var sWeight = GetRad(document.formNRR.radWeight);
	
	document.getElementById("divProtType").style.display = (sMethod == "NIOSH") && (sWeight == "C") ? "" : "none";
}

function ShowSummary(nLev)
{
	if(nLev > 0)
	{
/*
		if(nLev < 70)
		{
			document.getElementById("divSummary").innerHTML = "Over-protection: It is possible that the worker is being over-protected, causing difficulties with communication and hearing warning signals.";
			document.getElementById("fldSummary").value = "Over-protection: It is possible that the worker is being over-protected, causing difficulties with communication and hearing warning signals.";
		}

		if((nLev >= 70) && (nLev < 80))
		{
			document.getElementById("divSummary").innerHTML = "Suitable: This hearing protector appears to give adequate protection.";
			document.getElementById("fldSummary").value = "Suitable: This hearing protector appears to give adequate protection.";
		}

		if(nLev >= 80)
		{
			document.getElementById("divSummary").innerHTML = "Inadequate: This hearing protector does not offer adequate protection.";
			document.getElementById("fldSummary").value = "Inadequate: This hearing protector does not offer adequate protection.";
		}
*/

		document.getElementById("btnPrint").style.visibility = "visible";
		document.getElementById("btnClear").style.visibility = "visible";
	}
	else
	{
/*
		document.getElementById("divSummary").innerHTML = "";
		document.getElementById("fldSummary").value = "";
*/
		
		document.getElementById("btnPrint").style.visibility = "hidden";
		document.getElementById("btnClear").style.visibility = "hidden";
	}
}

function help(sSubject)
{
	sHelp = "";
	
	if(sSubject == "apv")
		sHelp = "Enter the hearing protector's <strong>A</strong>ssumed <strong>P</strong>rotection <strong>V</strong>alue.<br><br>If not provided, this is:<br>(Mean Atten - Std Deviation)";
	if(sSubject == "octmeas")
		sHelp = "Enter the level measured in each of the octave bands in dB. No extra freq. weighting should be used on the meter.";
	if(sSubject == "octgraph")
		sHelp = "Graph of the <strong><font color='#CC0000'>measured noise levels</font></strong> and the <strong><font color='#33CC00'>calculated levels</font></strong> at the ear. Indicates any frequencies that may be causing problems.";

	if(sSubject == "hml")
		sHelp = "Enter the hearing protector's <strong>H</strong>, <strong>M</strong> and <strong>L</strong> values, which are provided by the protector manufacturer.";
	if(sSubject == "lalc")
		sHelp = "Enter the <strong>A</strong> and <strong>C</strong> weighted sound levels in dB, measured with a suitable sound level meter.";

	if(sSubject == "nrr")
		sHelp = "Enter the hearing protector's <strong>NRR</strong> (Noise Reduction Rating) value, which is provided by the protector manufacturer.";
	if(sSubject == "lac")
		sHelp = "Enter the <strong>A</strong> or <strong>C</strong> weighted sound level, measured with a suitable sound level meter.<br><br>Select <strong>dB(A)</strong> or <strong>dB(C)</strong> accordingly.";
	if(sSubject == "awgt")
		sHelp = "The sound level or Time Weighted Average is <strong>'A'</strong> weighted.";
	if(sSubject == "cwgt")
		sHelp = "The sound level or Time Weighted Average is <strong>'C'</strong> weighted.";
	if(sSubject == "method")
		sHelp = "Use the OSHA or NIOSH calculation method. More details of these can be found in the Help pages.";
	if(sSubject == "plug")
		sHelp = "NIOSH calculations for foam earplugs and custom molded earplugs.";
	if(sSubject == "other")
		sHelp = "NIOSH calculations for other (not foam or molded) earplugs and semi-inserts.";
	if(sSubject == "muff")
		sHelp = "NIOSH claculations for earmuffs.";

	if(sSubject == "earpro")
		sHelp = "The <strong>Calculated Level</strong> is the estimated level at the ear. This is the level calculated to the figures provided by the manufacturer.";
		
	if(sHelp != "")
		sHelp = "<strong>Help</strong><br><br>" + sHelp;

	document.getElementById("divHelp").innerHTML = sHelp;
	document.getElementById("divHelp").style.visibility = (sHelp == "" ? "hidden" : "visible");
}

