function Calculate(theForm) {
theForm.totalq1.value = 0;
theForm.totalq2.value = 0;
theForm.totalq3.value = 0;
window.document.getElementById('totalv1').innerHTML = 0;
window.document.getElementById('totalv2').innerHTML = 0;
window.document.getElementById('totalv3').innerHTML = 0;


theForm.totalvimp1.value = 0;
theForm.totalvimp2.value = 0;
theForm.totalvimp3.value = 0;
<!--Set the field filter -->
var filter=/^[0-9]*$/

function notify(str,i) {
	alert('Please correct the form fields\n____________________________\n\nHINT: enter only numeric characters.');
	str.focus();
	window.document.getElementById('ina'+i).style.color = "#E93232";
	window.document.getElementById('out'+i).style.color = "#E93232";
}
for (var i= 1; i< 21; i++) {
window.document.getElementById('ina'+i).style.color = "#2c4e71";
window.document.getElementById('out'+i).style.color = "#2c4e71";
var str=theForm['inb'+i];
if (!filter.test(str.value) || str.value == "") {notify(str,i);return (false);}
window.document.getElementById('out'+i).innerHTML = Number(window.document.getElementById('ina'+i).innerHTML)*Number(theForm['inb'+i].value);
theForm.totalq1.value = Number(theForm.totalq1.value) + Number(theForm['inb'+i].value);
window.document.getElementById('totalv1').innerHTML = Number(window.document.getElementById('totalv1').innerHTML) + Number(window.document.getElementById('out'+i).innerHTML);
theForm['outimp'+i].value = Number(window.document.getElementById('ina'+i).innerHTML)*Number(theForm['inb'+i].value);
theForm.totalvimp1.value = Number(theForm.totalvimp1.value) + Number(theForm['outimp'+i].value);
}

for (var i= 21; i< 40; i++) {
window.document.getElementById('ina'+i).style.color = "#2c4e71";
window.document.getElementById('out'+i).style.color = "#2c4e71";
var str=theForm['inb'+i];
if (!filter.test(str.value) || str.value == "") {notify(str,i);return (false);}
window.document.getElementById('out'+i).innerHTML = Number(window.document.getElementById('ina'+i).innerHTML)*Number(theForm['inb'+i].value);
theForm.totalq2.value = Number(theForm.totalq2.value) + Number(theForm['inb'+i].value);
window.document.getElementById('totalv2').innerHTML = Number(window.document.getElementById('totalv2').innerHTML) + Number(window.document.getElementById('out'+i).innerHTML);
theForm['outimp'+i].value = Number(window.document.getElementById('ina'+i).innerHTML)*Number(theForm['inb'+i].value);
theForm.totalvimp2.value = Number(theForm.totalvimp2.value) + Number(theForm['outimp'+i].value);
}

for (var i= 40; i< 51; i++) {
window.document.getElementById('ina'+i).style.color = "#2c4e71";
window.document.getElementById('out'+i).style.color = "#2c4e71";
var str=theForm['inb'+i];
if (!filter.test(str.value) || str.value == "") {notify(str,i);return (false);}
window.document.getElementById('out'+i).innerHTML = Number(window.document.getElementById('ina'+i).innerHTML)*Number(theForm['inb'+i].value);
theForm.totalq3.value = Number(theForm.totalq3.value) + Number(theForm['inb'+i].value);
window.document.getElementById('totalv3').innerHTML = Number(window.document.getElementById('totalv3').innerHTML) + Number(window.document.getElementById('out'+i).innerHTML);
theForm['outimp'+i].value = Number(window.document.getElementById('ina'+i).innerHTML)*Number(theForm['inb'+i].value);
theForm.totalvimp3.value = Number(theForm.totalvimp3.value) + Number(theForm['outimp'+i].value);
}
<!--Get the totals and format them-->
theForm.totalimpq.value = Number(theForm.totalq1.value) + Number(theForm.totalq2.value) + Number(theForm.totalq3.value);
theForm.totalimpv.value = Number(theForm.totalvimp1.value) + Number(theForm.totalvimp2.value) + Number(theForm.totalvimp3.value);
theForm.grandimpv.value = Number(theForm.totalimpv.value) + Number(theForm.hvol.value);
theForm.grandimpw.value = Number(theForm.grandimpv.value) * 12;

theForm.totalimpv.value = roundToFact(theForm.totalimpv.value, roundfact );
theForm.totalimpv.value = roundToDp( theForm.totalimpv.value, decfact );
theForm.totalimpv.value = comma(theForm.totalimpv.value);

theForm.grandimpv.value = roundToFact(theForm.grandimpv.value, roundfact );
theForm.grandimpv.value = roundToDp( theForm.grandimpv.value, decfact );
theForm.grandimpv.value = comma(theForm.grandimpv.value);

theForm.grandimpw.value = roundToFact(theForm.grandimpw.value, roundfact );
theForm.grandimpw.value = roundToDp(theForm.grandimpw.value, decfact );
theForm.grandimpw.value = comma(theForm.grandimpw.value);

window.document.getElementById('totalv').innerHTML = theForm.totalimpv.value;
window.document.getElementById('grandv').innerHTML = theForm.grandimpv.value;
window.document.getElementById('grandw').innerHTML = theForm.grandimpw.value;

<!--Get the list of all items -->
theForm.totaltxt.value = "";
for (var i= 1; i< 51; i++) {
if ( theForm['outimp'+i].value > 0 ){
theForm.totaltxt.value =  theForm.totaltxt.value + '<br>' + window.document.getElementById('txt'+i).innerHTML + "&nbsp;" + window.document.getElementById('ina'+i).innerHTML + "&nbsp;" + theForm['inb'+i].value + "&nbsp;" + theForm['outimp'+i].value;
}
}
}
