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.methaneNumberL.value = 0;
			document.autoSumForm.methaneNumberCB.value = 0;
		} else {
			document.autoSumForm.methaneNumberL.value = testMN;
			document.autoSumForm.methaneNumberCB.value = testMN;
		}

		// test for value of methaneNumL, colour accordingly (ISL G)
		if (methaneNum <= 75) {
			document.autoSumForm.methaneNumberL.style.color = "#cc0000";
			$("#tlightL").css('background-position', 'left top');
		} else if ((methaneNum > 75) && (methaneNum <= 85)) {
			document.autoSumForm.methaneNumberL.style.color = "#f5bf01";
			$("#tlightL").css('background-position', 'center top');
		} else if (methaneNum > 85) {
			document.autoSumForm.methaneNumberL.style.color = "#00cc00";
			$("#tlightL").css('background-position', 'right top');
		} else {
			document.autoSumForm.methaneNumberL.style.color = "#FFFFFF";
			$("#tlightL").css('background-position', 'left top');
		}

		// test for value of methaneNumCB, colour accordingly (C Gas / B Gas Plus)
		if (methaneNum <= 65) {
			document.autoSumForm.methaneNumberCB.style.color = "#cc0000";
			$("#tlightCB").css('background-position', 'left top');
		} else if ((methaneNum > 65) && (methaneNum <= 75)) {
			document.autoSumForm.methaneNumberCB.style.color = "#f5bf01";
			$("#tlightCB").css('background-position', 'center top');
		} else if (methaneNum > 75) {
			document.autoSumForm.methaneNumberCB.style.color = "#00cc00";
			$("#tlightCB").css('background-position', 'right top');
		} else {
			document.autoSumForm.methaneNumberCB.style.color = "#FFFFFF";
			$("#tlightCB").css('background-position', 'left top');
		}

		// 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";
			$("#tlight").css('background-position', 'left top');
		}

		// 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";
			$("#tlight").css('background-position', 'left top');
		} 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.lhvText.value = "";
		document.autoSumForm.sulfurWeightText.value = "";
	}
}

function stopCalc(){
  clearInterval(interval);
}
