function doCalc() { 
	
	var theOpt = document.getElementById('selAge').value;
	var CD4P = parseInt(document.getElementById('CD4P').value);
	var CD4A = parseInt(document.getElementById('CD4A').value);
	var TLC = parseInt(document.getElementById('TLC').value);
	var VL = parseInt(document.getElementById('VL').value);
	
	// Checks for patient Stats
	if ( (isNaN(CD4P)) && (isNaN(CD4A)) && (isNaN(TLC)) && (isNaN(VL)) ) {
		alert("Please enter a measurement!")
		return
		}
	else if ((CD4P >= 0) && ((CD4A >= 0) || (TLC >= 0) || (VL >= 0))) {
		alert("Please enter only one measurement!")
		return
		}
	else if ((CD4A >= 0) && ((TLC >= 0) || (VL >= 0))) {
		alert("Please enter only one measurement!")
		return
		}
	else if ((TLC >= 0) && (VL >= 0)) {
		alert("Please enter only one measurement!")
		return
		}
	
	//Calculates age
	var age, mm, yyyy

	if (theOpt == "age"){
		
		mm = parseInt(document.getElementById('ageMonths').value);
		yyyy = parseInt(document.getElementById('ageYears').value) * 12;

		age = yyyy + mm;
	}
	else if (theOpt == "dob"){

		mm = parseInt(document.getElementById('curMonth').value);
		yyyy = parseInt(document.getElementById('curYear').value) * 12;
		
		temp = yyyy + mm;
		
		mm = parseInt(document.getElementById('dateMonth').value);
		yyyy = parseInt(document.getElementById('dateYear').value) * 12;
				
		//check for DOB data entered
		if ((isNaN(mm)) || (isNaN(yyyy))) {		
			alert("Please check patients DOB!")
			return
		}
		
		age = temp - (yyyy + mm);
		
		//Check that date of test is AFTER DOB !
		if (age < 0){
			alert("Date of test is before patients DOB!")
			return			
		}
	}
	
	if (age > 144){
		alert("Please note that predictions are unreliable after age 12 years.")
	}
	
	
	// Sets data as universal variable for calcs later
	var data
	
	if (CD4P >= 0){
		data = CD4P
		age = (age/12) + 0.3		//converts age to a decimal & adds constant
		var ageT = Math.log(age);
		
		// CD4P CALCS
		//---------------------------------------------------
	
				
		// CD4P & AIDS 
		a1 = -2.2422
		a2 = -0.7753
		b1 = 0.3000
		b2 = -0.2557
		c1 = -2.3422
		c2 = 0.4130
	
		a = Math.exp(a1+a2*ageT);
		b = Math.exp(b1+b2*ageT);
		c = Math.exp(c1+c2*ageT);
	
		calc = a + b * Math.exp(-c*data);
		result = 100*(1-Math.exp(-calc))
		rounded = NumRound(result)
		document.getElementById('Aids12').value = rounded;

		
		//---------------------------------------------------
	
		// CD4P & Death 
		a1 = -3.4948
		a2 = -1.1472
		b1 = 0.0005
		b2 = -0.4523
		c1 = -2.0135
		c2 = 0.3354
	
		a = Math.exp(a1+a2*ageT);
		b = Math.exp(b1+b2*ageT);
		c = Math.exp(c1+c2*ageT);

		calc = a + b * Math.exp(-c*data);
		result = 100*(1-Math.exp(-calc))
		rounded = NumRound(result)
		document.getElementById('Death12').value = rounded;
	
	}


	if (CD4A >= 0){
		data = (CD4A+1)/10
		age = (age/12)	//converts age to a decimal & adds constant
		if (age<=4) {
			ageT = age
		}
		else {
			ageT=4+0.1*(age-4)
		}
		
		// CD4A CALCS
		//---------------------------------------------------
	
				
		// CD4A & AIDS 
		a1=-1.9137;
		a2=-0.3557;
		b1=0.5124;
		b2=-0.1908;
		c1=-4.4111;
		c2=0.5660;
				
		a = Math.exp(a1+a2*ageT);
		b = Math.exp(b1+b2*ageT);
		c = Math.exp(c1+c2*ageT);
	
		calc = a + b * Math.exp(-c*data);
		result = 100*(1-Math.exp(-calc))
		rounded = NumRound(result)
		document.getElementById('Aids12').value = rounded;

		
		//---------------------------------------------------
	
		// CD4A & Death 
		a1=-3.0564;
		a2=-0.6700;
		b1=0.0514;
		b2=-0.2455;
		c1=-4.0416;
		c2=0.5121;

		a = Math.exp(a1+a2*ageT);
		b = Math.exp(b1+b2*ageT);
		c = Math.exp(c1+c2*ageT);
	
		calc = a + b * Math.exp(-c*data);
		result = 100*(1-Math.exp(-calc))
	
		rounded = NumRound(result)
	
		document.getElementById('Death12').value = rounded;
	
	}



	if (TLC >= 0){
		data = TLC/1000 
		age = (age/12) + 0.3		//converts age to a decimal & adds constant
		var ageT = Math.log(age);
		
		// TLC CALCS
		//---------------------------------------------------
	
				
		// TLC & AIDS 
		a1=-2.1740;
		a2=-0.5188;
		b1=0.3285;
		b2=0.1215;
		c1=-0.5048;
		c2=0.6914;

		a = Math.exp(a1+a2*ageT);
		b = Math.exp(b1+b2*ageT);
		c = Math.exp(c1+c2*ageT);
	
		calc = a + b * Math.exp(-c*data);
		result = 100*(1-Math.exp(-calc))
		rounded = NumRound(result)
		document.getElementById('Aids12').value = rounded;

		
		//---------------------------------------------------
	
		// TLC & Death 
		
		a1=-3.6393;
		a2=-0.6402;
		b1=0.1210;
		b2=0.0520;
		c1=-0.2254;
		c2=0.6069;

		a = Math.exp(a1+a2*ageT);
		b = Math.exp(b1+b2*ageT);
		c = Math.exp(c1+c2*ageT);
	
		calc = a + b * Math.exp(-c*data);
		result = 100*(1-Math.exp(-calc))
		rounded = NumRound(result)
		document.getElementById('Death12').value = rounded;
	
	}


	if (VL >= 0){

		data = 7 - Math.log(VL)/Math.LN10
		age = (age/12) + 0.3		//converts age to a decimal & adds constant
		var ageT = Math.log(age);
		

		// VL CALCS
		//---------------------------------------------------
				
		// VL & AIDS 
		a1 = -2.4231
		a2 = -0.8246
		b1 = -0.39374
		b2 = 0
		c1 = 0.3487
		c2 = 0
		
		a = Math.exp(a1+a2*ageT);
		b = Math.exp(b1+b2*ageT);
		c = Math.exp(c1+c2*ageT);
		
		calc = a + b * Math.exp(-c*data);
		result = 100*(1-Math.exp(-calc))
		rounded = NumRound(result)
		document.getElementById('Aids12').value = rounded;
		

	
		//---------------------------------------------------
				
		// VL & Death
		a1 = -4.0474
		a2 = -1.1982
		b1 = -1.0972
		b2 = 0
		c1 = 0.3637
		c2 = 0
		
		a = Math.exp(a1+a2*ageT);
		b = Math.exp(b1+b2*ageT);
		c = Math.exp(c1+c2*ageT);
		
		calc = a + b * Math.exp(-c*data);
		result = 100*(1-Math.exp(-calc))
		rounded = NumRound(result)
		document.getElementById('Death12').value = rounded;
	}
		

	// Displays results
	document.getElementById('results').style.display = "";
	

}


// modify following to give 2 sig figures

function NumRound(no) {

	if (no > 10) {	
		no = Math.round(no)
	
		if (no <= 1){
			return("<1")}
		else {return(no)}
 	}
	else {
	var rnum = no
	var rlength = 1; // The number of decimal places to round to
		if (rnum > 8191 && rnum < 10485) {
			rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);}
		//newnumber = newnumber+5000;
	 else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		}
	no = newnumber;
	return(no);
	}		

}

