var score=0;		//global variables
var age_val=0;
var BP_val=0;
var age_num=0;
var BP_num=0;
var chol_val=0;
var chol_num=0;
var HDL_val=0;
var HDL_num=0;
var smoke_val=0;
var smoke_num=0;
var diab_val=0;
var diab_num=0;
var BP_hold=0;
var CHD_num=0;
var percent=0;
var risklevel=0;
var target=0;
var FRS=0;
var risknum=0;
var initiate=0;
var riskcount=0;
var BPflag=0;
var cholflag=0;
var HDLflag=0;
var smokeflag=0;
var CHDflag=0;
var CHD_hold=0;
var finalresult=0;
var CRPrec=0;
var ASAflag=0;
var number=0;
xnnt=0;
nnt=0;


function resetcalc()		//resets the calculator
	{
score=0;
age_val=0;
BP_val=0;
age_num=0;
BP_num=0;
chol_val=0;
chol_num=0;
HDL_val=0;
HDL_num=0;
smoke_val=0;
smoke_num=0;
diab_val=0;
diab_num=0;
BP_hold=0;
CHD_num=0;
percent=0;
risklevel=0;
riskcount=0;
target=0;
initiate=0;
FRS=0;
risknum=0;
riskcount=0;
BPflag=0;
cholflag=0;
HDLflag=0;
smokeflag=0;
CHDflag=0;
CHD_hold=0;
finalresult=0;
CRPrec=0;
ASAflag=0;
number=0;
xnnt=0;
nnt=0;

	}

function totalvalue()		//converts string to integer and does math
	{
// Convert to numeric and Test output
age_num=parseFloat(age_val);
BP_num=parseFloat(BP_val);
chol_num=parseFloat(chol_val);
HDL_num=parseFloat(HDL_val);
smoke_num=parseFloat(smoke_val);
diab_num=parseFloat(diab_val);

score=age_num+BP_num+chol_num+HDL_num+smoke_num+diab_num;
//document.form1.total.value=score;		//take this out later
eval();					//evaluate risk scoring here, otherwise stays 1 step behind
//document.form1.riskfactors.value=riskcount;	//may take this out later
document.form1.percentage.value=percent;
xnnt=parseFloat(percent);
nnt=100/(xnnt*.27);
document.form1.number.value=nnt.toFixed(1);
document.form1.level.value=risklevel;
document.form1.goal.value=target;
document.form1.begin.value=initiate;
document.form1.CRPrecommended.value=CRPrec;
document.form1.ASArec.value=ASAflag;

	}


function calculator()
	{
//Initialize counter
age_val=0;
//Points for Age
age_val=document.getElementById("select2").value;
//Display
totalvalue();
	}


function BPcalc()
	{
//window.alert("PLEASE SELECT WHETHER OR NOT ON TREATMENT in the next checkbox");
//Initialize counter
BP_val=0;
//Points for BP
BP_val=document.getElementById("select1").value;
//document.getElementById("no4").checked=true;
//BP_hold="0";		//place holder for CHD calculation error check
if (BP_val>=2)	//risk factor counter for diabetes 
	{
	riskcount=riskcount+1;
	var counter=counter+1;
	if (counter>1)
		riskcount=riskcount-1;
	}
else 	{riskcount=0
	}
BPflag=riskcount;
if (riskcount!=0&&diab_val!=0)
	{
	diab_50_alert();
	}
else
//Display
totalvalue();
	}


function cholcalc()
	{
//Initialize the counter
chol_val=0;
//Points for cholesterol
chol_val=document.getElementById("select3").value;
if (chol_val>=3)	//risk factor counter for diabetes 
	{
	riskcount=riskcount+1;
	var counter=counter+1;
	if (counter>1)
		riskcount=riskcount-1;
	}
else 	{riskcount=0
	}
cholflag=riskcount;
if (riskcount!=0&&diab_val!=0)
	{
	diab_50_alert();
	}
else
//Display
totalvalue();
	}

function HDLcalc()
	{
//Initialize the counter
HDL_val=0;
//Points for HDL
HDL_val=document.getElementById("select4").value;
if (HDL_val==2)	//risk factor counter for diabetes 
	{
	riskcount=riskcount+1;
	var counter=counter+1;
	if (counter>1)
		riskcount=riskcount-1;
	}
else 	{riskcount=0
	}
HDLflag=riskcount;
if (riskcount!=0&&diab_val!=0)
	{
	diab_50_alert();
	}
else
//Display
totalvalue();
	}

function smokecalcNo()
	{
//Initialize the counter
smoke_val=0;
//Points for nonsmoking
document.getElementById("no2").checked=true;
smoke_val="0";	//string - needs brackets when testing for ""
//Display
totalvalue();
	}

function smokecalcYes()
	{
smoke_val=0;
//Points for smoking
document.getElementById("yes2").checked=true;
smoke_val=3;
if (smoke_val==3)	//risk factor counter for diabetes 
	{
	riskcount=riskcount+1;
	var counter=counter+1;
	if (counter>1)
		riskcount=riskcount-1;
	}
else 	{riskcount=0
	}
smokeflag=riskcount;
if (riskcount!=0&&diab_val!=0)
	{
	diab_50_alert();
	}
else	
//Display
totalvalue();
	}

function diabNo()
	{
diab_val=0;
//Points for no diabetes
document.getElementById("no3").checked=true;
diab_val="0";//string - needs brackets when testing for ""	
//Display
totalvalue();
	}

function diabYes()
	{
diab_val=0; 
//Points for diabetes
document.getElementById("yes3").checked=true;
diab_val=4;
if (age_val >=7)
	{
	window.alert("Diabetic over age 50 becomes high risk. DO NOT ENTER ANY FURTHER VALUES");
	percent=">20%";
	risklevel="HIGH";
	target="<2.0 or 50% Reduction";
	initiate="Consider treatment in all patients";
	ASAflag=">AGE 55"
	document.form1.percentage.value=percent;
	document.form1.level.value=risklevel;
	document.form1.goal.value=target;
	document.form1.begin.value=initiate;
	document.form1.ASArec.value=ASAflag;
	
	}
else if ((age_val<7)&&(BPflag==1||cholflag==1||HDLflag==1||smokeflag==1||CHDflag==1))
	{
diab_50_alert();	
	}
else	
//Display
totalvalue();
	}

function norxBP()
//Points for no BP meds
	{ 
if(BP_val=="")
	{	 
	window.alert("Please enter systolic BP first");	//stop here if no BP selected
 	document.getElementById("no4").checked=false;
	document.getElementById("yes4").checked=false;
	}
else if (BP_hold!="")	//decrement BP_val added by rxBP()
	{			
		BP_hold=BP_num-2;
		BP_val=BP_hold;
		}
else if (BP_hold=="") 
	BP_hold="0";				//Not on treatment. BP_val is unchanged. Needs quotes for test of ""
//Display
totalvalue();
	}

function rxBP()
//Points for BP meds
	{
if(BP_val=="")	 
	{	 
	window.alert("Please enter systolic BP first");	//stop here if no BP selected
 	document.getElementById("no4").checked=false;
	document.getElementById("yes4").checked=false;
	}
else 	{
	BP_hold=BP_num+2;
	BP_val=BP_hold;
	}
//Display
totalvalue();
	}

function CHD_no()
//Points for no family history
	{
if (age_val=="" || BP_val=="" || chol_val=="" || HDL_val=="" || smoke_val=="" || diab_val=="" || BP_hold=="")//Forces data entry
	{	
	window.alert("ONLY IF FIRST DEGREE RELATIVE WITH ONSET < AGE 60.  hsCRP is optional, but please be sure all other values are entered if the influence of family history is to be considered.");
	document.getElementById("no6").checked=false;
	document.getElementById("yes6").checked=false;
	}
else 	CHD_hold="0";		//seems necessary for CRP error check
	if (CHD_num!="")	
		CHD_num=0;
		
//Display
totalvalue();		
	}


function CHD_yes()
//Points for family history
	{
if (age_val=="" || BP_val=="" || chol_val=="" || HDL_val=="" || smoke_val=="" || diab_val=="" || BP_hold=="")//Forces data entry
	{	
	window.alert("ONLY IF FIRST DEGREE RELATIVE WITH ONSET < AGE 60.  hsCRP is optional, but please be sure all other values are entered if the influence of family history is to be considered.");
	document.getElementById("no6").checked=false;
	document.getElementById("yes6").checked=false;
	//reset();
		}
else
	{	
	CHD_hold=risknum*1.7;
	CHD_num=CHD_hold-risknum;
	CHD_num=round(CHD_num);
	if (CHD_num>0)	//risk factor counter for diabetes 
	{
		riskcount=riskcount+1;
		var counter=counter+1;
		if (counter>1)
			riskcount=riskcount-1;
	}
	else 	{riskcount=0
	}
CHDflag=riskcount;
if (riskcount!=0&&diab_val!=0)
	{
	diab_50_alert();
	}
	}

//Display
totalvalue();	
	}



function round(num)		//function to create 1 decimal place for CHD_num
	{
return Math.round(num*100)/100;
	}


function CRP()
//Decision for hsCRP if done
	{
if (age_val=="" || BP_val=="" || chol_val=="" || HDL_val=="" || smoke_val=="" || diab_val=="" || BP_hold=="" || CHD_hold=="") //Forces data entry
	{
	window.alert ("All other data must be entered into the calculator before using this value.  ");
	document.getElementById("no5").checked=false;
	document.getElementById("yes5").checked=false;
	return;
	}
if (age_val<9 || finalresult<10)
 	{
	window.alert("The patient must be over 60 years old and at moderate CVD risk.  Be sure you have entered all the data. IF YOU HAVE, CRP IS OF NO HELP IN DECISION ANALYSIS");	document.getElementById("no5").checked=false;
	document.getElementById("yes5").checked=false;
	}
else if (finalresult>=20) 
	{	
	window.alert("The patient is already at high CVD risk.  CRP IS OF NO HELP IN DECISION ANALYSIS");
	document.getElementById("no5").checked=false;
	document.getElementById("yes5").checked=false;
	}
else if (age_val>=9 && finalresult>=10&&finalresult<20) 
	{
	window.alert("A PERSON WITH LDL<3.5 SHOULD CONSIDER STATIN THERAPY REGARDLESS OF LIPID LEVELS because age>60 and moderate CVD risk with hsCRP>2 imparts increased risk.  DO NOT ENTER FURTHER DATA"); 	
	document.getElementById("no5").checked=false;
	document.getElementById("yes5").checked=true;
	//percent=">20%";
	//risklevel="HIGH";
	target="<2.0 or 50% Reduction";
	initiate="LDL>3.5mmol/L      Chol/HDL>5.0       hsCRP>2_and_LDL<3.5 ";
	document.form1.percentage.value=percent;
	document.form1.level.value=risklevel;
	document.form1.goal.value=target;
	document.form1.begin.value=initiate;
	}
	}

function convert()
	{
	{
if (score<=-2)
	FRS="1% or less";
else if (score==-1)
	FRS="1.0%";
else if (score==0)
	FRS="1.2%";
else if (score==1)
	FRS="1.5%";
else if (score==2)
	FRS="1.7%";
else if (score==3)
	FRS="2.0%";
else if (score==4)
	FRS="2.4%";
else if (score==5)
	FRS="2.8%";
else if (score==6)
	FRS="3.3%";
else if (score==7)
	FRS="3.9%";
else if (score==8)
	FRS="4.5%";
else if (score==9)
	FRS="5.3%";
else if (score==10)
	FRS="6.3%";
else if (score==11)
	FRS="7.3%";
else if (score==12)
	FRS="8.6%";
else if (score==13)
	FRS="10.0%";
else if (score==14)
	FRS="11.7%";
else if (score==15)
	FRS="13.7%";
else if (score==16)
	FRS="15.9%";
else if (score==17)
	FRS="18.5%";
else if (score==18)
	FRS="21.5%";
else if (score==19)
	FRS="24.8%";
else if (score==20)
	FRS="27.5%";
else if (score>=21)
	FRS="30% or more";
}
}


function eval()
	{
convert();
risklevel=0;
target=0;
risknum=parseFloat(FRS);	//otherwise FRS returns a string
finalresult=risknum+CHD_num;		//adds family history as percentage
ASAcalc();
if (age_val=="" || BP_val=="" || chol_val=="" || HDL_val=="" || smoke_val=="" ||diab_val=="" || BP_hold=="" || CHD_hold=="" )
		CRPrec="Need More Information";
else if (age_val!="" && BP_val!="" && chol_val!="" && HDL_val!="" && smoke_val!="" && diab_val!="" && BP_hold!="" && 		CHD_hold!=""&&age_val>=9&&(finalresult>=10&&finalresult<20))
		{
		CRPrec="YES if LDL<3.5";
		}
else
		{
		CRPrec="NO";
		}
if (finalresult<10)	{
	percent=finalresult + "%";
	risklevel="LOW";
	target="50% Reduction";
	initiate="LDL Cholesterol >5.0 mmol/L";

		}
else if (finalresult>=10&&finalresult<20)	{
	percent=finalresult + "%";
	risklevel="MODERATE";
	target="<2.0 or 50% Reduction";
	initiate="LDL Cholesterol >3.5mmol/L or Chol/HDL >5.0    *hsCRP >2 may change risk *Family history may change risk";
	
		}
else if (finalresult>=20)	{
	percent=finalresult + "%";
	risklevel="HIGH";
	target="<2.0 or 50% Reduction";
	initiate="Consider treatment in all patients";
			}
	}

function diab_50_alert()
	{
window.alert("Diabetic under age 50 with one or more additional CVD risk factor becomes high risk.  DO NOT ENTER ANY FURTHER VALUES");
	percent=">20%";
	risklevel="HIGH";
	target="<2.0 or 50% Reduction";
	initiate="Consider treatment in all patients";
	document.form1.percentage.value=percent;
	document.form1.level.value=risklevel;
	document.form1.goal.value=target;
	document.form1.begin.value=initiate;
	
	}
	
function ASAcalc()
	{
if (age_val!="" && BP_val!="" && chol_val!="" && HDL_val!="" && smoke_val!="" && diab_val!="" && BP_hold!="" &&	CHD_hold!="")
	{
	if (age_val==8&&finalresult>=3)
		ASAflag="Consider";
	else if (age_val==9||age_val==10&&finalresult>=8)
		ASAflag="Consider : YES >65yr."; 
	else if (age_val>=11&&finalresult>=11)
		{
		window.alert ("No reliable data for ASA if the patient is over 80 years old");
		ASAflag="YES: ? if >80 years";
		}
	else ASAflag="NO";		
	}

else ASAflag="Need More Information"
	}

function toMale()
	{
window.location="2009 CCS Framingham_male.html";
	}


