function startCalc(){
	interval = setInterval("calc()",1);
}

function calc(){
	methane = document.autoSumForm.methane.value;
	ethane = document.autoSumForm.ethane.value;
	propane = document.autoSumForm.propane.value;
	butane = document.autoSumForm.butane.value;
	pentane = document.autoSumForm.pentane.value;
	hexane = document.autoSumForm.hexane.value;
	heptane = document.autoSumForm.heptane.value;
	octane = document.autoSumForm.octane.value;
	carbondioxide = document.autoSumForm.carbondioxide.value;
	nitrogen = document.autoSumForm.nitrogen.value;
	oxygen = document.autoSumForm.oxygen.value;
	sulfur = document.autoSumForm.sulfur.value;
	componentSum = (methane*1) + (ethane*1) + (propane*1) + (butane*1) + (pentane*1) + (hexane*1) + (heptane*1) + (octane*1) + (carbondioxide*1) + (nitrogen*1) + (oxygen*1) + (sulfur*1);
	
	// write out the sum of the composition, colour as needed.
	document.autoSumForm.thetotal.value = componentSum.toFixed(4);
	if (componentSum.toFixed(4) == 100) {
		document.autoSumForm.thetotal.style.color = "#00cc00";
		document.getElementById("alert").style.color="#00cc00";
		
		// calculate methane number
		reactiveHC = (((methane*4)+(ethane*6)+(propane*8)+(butane*10)+(pentane*12)+(hexane*14)+(heptane*16)+(octane*18))/((methane*1)+(ethane*2)+(propane*3)+(butane*4)+(pentane*5)+(hexane*6)+(heptane*7)+(octane*8)));
		motorOctane = -406.14+(508.04*reactiveHC)+(-173.55*Math.pow(reactiveHC,2))+(20.17*Math.pow(reactiveHC,3));
		methaneNum = (1.624*motorOctane)-119.1;
	
		// test for NaN
		testMN = methaneNum.toFixed(1)
		if ( isNaN(testMN) ) {
			document.autoSumForm.methaneNumber.value = 0;
		} else {
			document.autoSumForm.methaneNumber.value = testMN;
		}
	
		// test for value of methaneNum, colour accordingly
		if (methaneNum < 65) {
			document.autoSumForm.methaneNumber65Text.style.color = "#cc0000";
			document.autoSumForm.methaneNumber65Text.value = "FAIL";
			document.autoSumForm.methaneNumber75Text.style.color = "#cc0000";
			document.autoSumForm.methaneNumber75Text.value = "FAIL";
		} else if ((methaneNum >= 65) && (methaneNum < 75)) {
			document.autoSumForm.methaneNumber65Text.style.color = "#00cc00";
			document.autoSumForm.methaneNumber65Text.value = "PASS";
			document.autoSumForm.methaneNumber75Text.style.color = "#cc0000";
			document.autoSumForm.methaneNumber75Text.value = "FAIL";
		} else if (methaneNum >= 75) {
			document.autoSumForm.methaneNumber65Text.style.color = "#00cc00";
			document.autoSumForm.methaneNumber65Text.value = "PASS";
			document.autoSumForm.methaneNumber75Text.style.color = "#00cc00";
			document.autoSumForm.methaneNumber75Text.value = "PASS";
		} else {
			document.autoSumForm.methaneNumber65Text.style.color = "#cc0000";
			document.autoSumForm.methaneNumber65Text.value = "FAIL";
			document.autoSumForm.methaneNumber75Text.style.color = "#cc0000";
			document.autoSumForm.methaneNumber75Text.value = "FAIL";
		}
	
		// calculate lower heating value
		molecularWeight = ((methane*16.0428)+(ethane*30.0696)+(propane*44.0965)+(butane*58.1234)+(pentane*72.1503)+(hexane*86.1772)+(heptane*100.2040)+(octane*114.2309)+(carbondioxide*44.0098)+(nitrogen*28.0134)+(oxygen*31.9988)+(sulfur*64.1320))/100;
		methaneM = (16.0428*methane)/molecularWeight;
		methaneLHV = (methaneM/100)*50016;
		ethaneM = (30.0696*ethane)/molecularWeight;
		ethaneLHV = (ethaneM/100)*47489;
		propaneM = (44.0965*propane)/molecularWeight;
		propaneLHV = (propaneM/100)*46357;
		butaneM = (58.1234*butane)/molecularWeight;
		butaneLHV = (butaneM/100)*45742;
		pentaneM = (72.1503*pentane)/molecularWeight;
		pentaneLHV = (pentaneM/100)*45355;
		hexaneM = (86.1772*hexane)/molecularWeight;
		hexaneLHV = (hexaneM/100)*45105;
		heptaneM = (100.2040*heptane)/molecularWeight;
		heptaneLHV = (heptaneM/100)*44926;
		octaneM = (114.2309*octane)/molecularWeight;
		octaneLHV = (octaneM/100)*44791;
		carbondioxideM = (44.0098*carbondioxide)/molecularWeight;
		carbondioxideLHV = (carbondioxideM/100)*0;
		nitrogenM = (28.0134*nitrogen)/molecularWeight;
		nitrogenLHV = (nitrogenM/100)*0;
		oxygenM = (31.9988*oxygen)/molecularWeight;
		oxygenLHV = (oxygenM/100)*0;
		sulfurM = (64.1320*sulfur)/molecularWeight;
		sulfurLHV = (sulfurM/100)*0;
		metricLHV = methaneLHV + ethaneLHV + propaneLHV + butaneLHV + pentaneLHV + hexaneLHV + heptaneLHV + octaneLHV + carbondioxideLHV + nitrogenLHV + oxygenLHV + sulfurLHV;
		lowerHeatingValue = metricLHV/2.32444444444;
	
		// test for NaN
		testLHV = lowerHeatingValue.toFixed(0)
		if ( isNaN(testLHV) ) {
			document.autoSumForm.lhv.value = 0;
		} else {
			document.autoSumForm.lhv.value = testLHV;
		}
	
		// check LHV, colour accordingly
		if (lowerHeatingValue >= 16100) {
			document.autoSumForm.lhvText.style.color = "#00cc00";
			document.autoSumForm.lhv.style.color = "#00cc00";
			document.autoSumForm.lhvText.value = "PASS";
		} else {
			document.autoSumForm.lhvText.style.color = "#cc0000";
			document.autoSumForm.lhv.style.color = "#cc0000";
			document.autoSumForm.lhvText.value = "FAIL";
		}
	
		// sulfur content, test for NaN
		testSulfur = sulfurM.toFixed(3);
		if ( isNaN(testSulfur) ) {
			document.autoSumForm.sulfurWeight.value = 0;
		} else {
			document.autoSumForm.sulfurWeight.value = testSulfur;
		}
	
		// sulfur content, check and colour accordingly
		if (sulfurM > 0.001) {
			document.autoSumForm.sulfurWeightText.style.color = "#cc0000";
			document.autoSumForm.sulfurWeight.style.color = "#cc0000";
			document.autoSumForm.sulfurWeightText.value = "FAIL";
		} else {
			document.autoSumForm.sulfurWeightText.style.color = "#00cc00";
			document.autoSumForm.sulfurWeight.style.color = "#00cc00";
			document.autoSumForm.sulfurWeightText.value = "PASS";
		}		
	} else {
		document.autoSumForm.thetotal.style.color = "#cc0000";
		document.getElementById("alert").style.color="#cc0000";
		// clear other values
		document.autoSumForm.methaneNumber.value = "";
		document.autoSumForm.lhv.value = "";
		document.autoSumForm.sulfurWeight.value = "";
		document.autoSumForm.methaneNumber65Text.value = "";
		document.autoSumForm.methaneNumber75Text.value = "";
		document.autoSumForm.lhvText.value = "";
		document.autoSumForm.sulfurWeightText.value = "";
	}
}

function stopCalc(){
  clearInterval(interval);
}