
	// Beräkning av runoff
	function doCalculate()
	{
		var frm = document.forms['frmCalc'];
		var divPredicted = document.getElementById('divPredicted');

		if (!validateForm())
		{
			divPredicted.innerHTML = 'Error';
			return;
		}

		var inclination = readFormValue(frm.txtINCLINATION.value);
		var SO2 = readFormValue(frm.txtSO2.value);
		var pH = readFormValue(frm.txtPH.value);
		var rain = readFormValue(frm.txtRAIN.value);

		var inclinationRad = Math.cos(toRadians(0)) / Math.cos(toRadians(inclination));
		var inclinationCosCos45 = Math.cos(toRadians(inclination)) / Math.cos(toRadians(45));
		var collectedVol = rain / (Math.cos(toRadians(0)) / Math.cos(toRadians(inclination)));
		var predicted = ((0.37 * Math.pow(SO2, 0.5)) + 0.96 * rain * Math.pow(10, (-0.62 * pH))) * inclinationCosCos45;

		if (predicted < 0.001 && predicted > -0.001)
			predicted = 0;

		predicted = roundTwoDecimals(predicted);

		divPredicted.innerHTML = predicted;

	}

	// Beräkning av flöde
	function doCalculateFlow()
	{
		
		var divFlowRunoff = document.getElementById('divFlowRunoff');
		var txtFlowArea = document.getElementById('txtFlowArea');
		var divFlowReleasePerDay = document.getElementById('divFlowReleasePerDay');
		var txtFlowDepth = document.getElementById('txtFlowDepth');
		var txtFlowWidth = document.getElementById('txtFlowWidth');
		var txtFlowFlow = document.getElementById('txtFlowFlow');
		var divFlowVolumeFlow = document.getElementById('divFlowVolumeFlow');
		var divFlowConc = document.getElementById('divFlowConc');
	
		if (!validateFormFlow())
		{
			divFlowConc.innerHTML = 'Error';
			return;
		}
	
		var divPredicted = document.getElementById('divPredicted');
		if (!isNumeric(divPredicted.innerHTML))
		{
			divFlowConc.innerHTML = "Error";
			return false;
		}
		var runoff = readFormValue(divPredicted.innerHTML);
		divFlowRunoff.innerHTML = runoff;
		
		var flowRelease = runoff * readFormValue(txtFlowArea.value);		
		var flowReleasePerDay = flowRelease / 365;
		divFlowReleasePerDay.innerHTML = roundTwoDecimals(flowReleasePerDay);
		
		var flowPerSecond = readFormValue(txtFlowDepth.value) * readFormValue(txtFlowWidth.value) * readFormValue(txtFlowFlow.value);
		var flowPerDay = flowPerSecond * 60 * 60 * 24;
		divFlowVolumeFlow.innerHTML = flowPerDay;
		
		var flowConc = flowReleasePerDay / flowPerDay;
		var flowConc = flowConc * 1000;		// g/m3 => µg/L
		
		divFlowConc.innerHTML = roundTwoDecimals(flowConc);

	}
	
	function roundTwoDecimals(numericValue)
	{
		return Math.round(numericValue * 100) / 100;
	}
	
	function readFormValue(orgVal)
	{
		orgVal = orgVal.replace(/,/, '.');
		return eval(orgVal);
	}


	function validateForm()
	{
		var frm = document.forms['frmCalc'];

		if(!validateNumericField(frm.txtRAIN))
			return false;
		if(!validateNumericField(frm.txtPH))
			return false;
		if(!validateNumericField(frm.txtSO2))
			return false;
		if(!validateNumericField(frm.txtINCLINATION))
			return false;

		return true;
	}
	
	function validateFormFlow()
	{
		var frm = document.forms['frmCalcFlow'];

		if(!validateNumericField(frm.txtFlowArea))
			return false;
		if(!validateNumericField(frm.txtFlowDepth))
			return false;
		if(!validateNumericField(frm.txtFlowWidth))
			return false;
		if(!validateNumericField(frm.txtFlowFlow))
			return false;

		return true;
	}	

	function validateNumericField(formField)
	{
	
		if (!isNumeric(formField.value))
		{
			setFormFieldStatus(formField, false);
			return false;
		}
		else
		{
			setFormFieldStatus(formField, true);	
			return true;
		}
	}

	function setFormFieldStatus(formField, newStatus)
	{
		if (newStatus)
			formField.style.backgroundColor = '';
		else
			formField.style.backgroundColor = '#EAC99C';
	}

	function isNumeric(value) 
	{
		if (value == null || value == '' || !value.toString().match(/^[-]?\d*\.?\,?\d*$/)) 
			return false;
  		return true;
	}

	function toRadians(degrees) 
	{
		var pi = Math.PI;
		var radians = (degrees * (pi/180));
		return radians;
	}

