

var szMsg='';
var nErrCount=0;
var oForm;				
var nTally=0;
	
function scoreProtein() {			
//	if input doesn't validate, alert the user; otherwise, run the assessment scoring code																						   
	oForm=document.forms.frmProtein;
	valNumeric(oForm.txtWeight.value, "Weight");
	if (szMsg=='') {	//	no errors, do the compute	
		var nProtVal=0;
		var sOutput='';	
		switch(parseInt(getRadioValue(oForm.radActivityLevel))) {
			case 0:	//	Average Adult
			   	nProtVal=calcProtein(oForm.txtWeight.value,.8);
				sOutput=Math.round(nProtVal);
				break;
			case 1: //	Athlete in training
				nProtVal=calcProtein(oForm.txtWeight.value,1.2);
				sOutput=Math.round(nProtVal)+' - ';
				nProtVal=calcProtein(oForm.txtWeight.value,1.6);
				sOutput+=Math.round(nProtVal);
			   	break;
		}
		oForm.txtProtein.value=sOutput;
	}
	else {				//	display error alert with details
		var szErrNumber = '';
		if (nErrCount>1) {
			szErrNumber='s were ';
		}
		else {
		szErrNumber=' was ';
		}
		alert('________________________________________________________\n\n' +
      	'The following question' + szErrNumber + 'not completed properly:\n\n' + szMsg +
      	'\n________________________________________________________\n\n' +
      	'Please correct and re-submit the form.');
		nErrCount=0;
		szMsg='';
	}
	return;
}
				 
function calcProtein( nWeight, nMultiplier ) {
 	var nGrams=0;					
	nGrams=nWeight/2.2;						   
	nGrams*=nMultiplier;
	return nGrams;
}

function valNumeric( szValue, szFieldName ) {	
	/*	Numeric means *something* must be there (can't be blank), 												 
		and that only numbers are allowed.  */
	var szValueMatch = szValue.match( /^[0-9]+$/ );
	if (szValueMatch==null) {
		szMsg += '  ' + szFieldName + ' is required and must be numeric.\n';
		nErrCount+=1;
	}
	return;
}

function getRadioValue( oRadioGroup ) {
	var btnArray=oRadioGroup;
	var btnValue='';
  	for (var index=0; index<btnArray.length; index++) {
	    if ( btnArray[index].checked ) {
			btnValue=btnArray[index].value;
	      	break;
		}
  	} 
	return btnValue;
}
