function hepChart(form) {
	var patient = form.patient.value
	var mrn = form.mrn.value
	var sex = form.sex.selectedIndex
	var weight = form.weight.value
	var height = form.height.value
	var baseKg, overKg, idealWt, doseWt, sexText
	
	if (patient == "") {
		alert("Please enter the patient's name.");
		form.patient.focus();
		return false;
	}
	
	if (mrn == "") {
		alert("Please enter the medical record number.");
		form.mrn.focus();
		return false;
	}
	
	if ((weight == "") || (weight <= 0) || (isNaN(weight))) {
		alert("Please enter the patient's weight.");
		form.weight.focus();
		form.weight.select();
		return false;
	}

	if ((height == "") || (height <= 0) || (isNaN(height))) {
		alert("Please enter the patient's height.");
		form.height.focus();
		form.height.select();
		return false;
	}

	for (var x=0; x < form.indic.length; x++) {
		if (form.indic[x].checked) {var indic = x}
	}
	
	if (isNaN(indic)) {
		alert("Please select an indication for heparin.");
		form.indic.focus();
		return false;
	}


//  Begin calculations

	var wtName = form.wtname.selectedIndex
	var htName = form.htname.selectedIndex

	//  Convert weight from "pounds" to "kilograms"
	if (wtName == 0) {weight = weight * 0.4535924}
	
	//  Convert height from "cm" to "inches"	
	if (htName == 1) {height = height * 0.3937008}
	
	if (sex == 0) {
		baseKg = 45.5;
		sexText = "Female"
	} else {
		baseKg = 50;
		sexText = "Male"
	}
	
	overKg = height - 60
	if (overKg < 0) {overKg = 0}
	overKg = overKg * 2.3
	
	idealWt = baseKg + overKg
	
	if (weight/idealWt < 1.2) {doseWt = weight}
		else {doseWt = idealWt + (0.4 * (weight - idealWt))}
	
	weight = roundNum(weight,1);
	height = roundNum(height,1);
	overKg = roundNum(overKg,1);
	idealWt = roundNum(idealWt,1);
	doseWt = roundNum(doseWt,1);
	

//  Indication for Heparin
	if (form.indic[0].checked) {		//  For DVT and PE
		var hepBolusConc = 80;
		var hepBolus = 80 * doseWt;
		var hepInfusConc = 18;
		var hepInfus = 18 * doseWt;
		var extraText = " ";
		var header = "<b>Treatment of Deep Venous Thrombosis (DVT) and Pulmonary Embolism (PE)</b>";
	} else {				//  For Cardiology, Stroke, and Other
		var hepBolusConc = 70;
		var hepBolus = 70 * doseWt;
		var hepInfusConc = 15;
		var hepInfus = 15 * doseWt;
		var extraText = "<input type=\"checkbox\"><b> No Loading Dose<sup>*</sup></b><br>";
		var header = "<b>Cardiology, Stroke, and All Other Indications</b><br>" +
			"<font size=\"-1\"><sup>*</sup>Bolus may be omitted in some Cardiology and Stroke patients, " +
			"as per clinical discretion.</font>";
	}
	
	hepBolus = roundNum(hepBolus,-2);
	hepInfus = roundNum(hepInfus,-2);
	var bolusDose = roundNum((hepBolusConc * doseWt),-2)
	var halfBolus = roundNum((hepBolusConc * doseWt / 2),-2)
	var dose5 = roundNum((5 * doseWt),-1);
	var dose4 = roundNum((4 * doseWt),-1);
	var dose3 = roundNum((3 * doseWt),-1);
	var dose2 = roundNum((2 * doseWt),-1);


//  Display the Heparin Weight-Based Protocol

	document.open();
	document.writeln("<center><h3>Weight-Based Heparin Order Sheet<br>");
	document.writeln("Hermann Hospital</h3></center><p>");

	document.writeln("<table align=right border=1 cellpadding=7>");
	document.writeln("<tr><td><b><font size=\"+1\" face=\"Arial\">" + mrn +"</font></b><br>" + 
		patient + "<br>" + sexText + "</td></tr>");
	document.writeln("</table>");

	document.writeln("<font size=\"-1\">");
	document.writeln("<ol><li>Obtain PTT, INR, CBC, and Platelet count prior to initiating heparin.<br>");
	document.writeln("<b>(Notify physician of abnormal values)</b><p>");
	document.writeln("<li>Dosing Weight:<br>");
	document.writeln("<dl><dd><b>Actual Body Weight (ABW):</b> " + weight + " kg. " +
		"&nbsp;&nbsp;&nbsp;<b>Height:</b> " + height + " inches.<br>");
	document.writeln("<dd><b>Ideal Body Weight (IBW):</b> " + idealWt + " kg.<br>");
	document.writeln("<dd><b>Dosing Weight:</b> " + doseWt + " kg.<p>");
	
	document.writeln("<center><table border=1 cellpadding=5>");
	document.writeln("<tr bgcolor=\"#ffff99\" align=center><td colspan=4>" + header + "<br>");
	document.writeln("Initial bolus = <b>" + hepBolusConc + "</b> units/kg dosing weight, followed by <b>" +
		hepInfusConc + "</b> units/kg dosing wt/hr.</td></tr>");
	document.writeln("<tr bgcolor=\"#ffff99\" align=center valign=center><th>PTT<br>(sec)</th>");
	document.writeln("<th>Bolus Dose IV</th>");
	document.writeln("<th>Hold Infusion<br>(min)</th>");
	document.writeln("<th>Rate Change</th></tr>");
	
	document.writeln("<tr align=center valign=center><td>&lt; 35</td>");
	document.writeln("<td>" + hepBolusConc + " units/kg =<br>" + bolusDose + " units</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>up by 4 U/kg/hr =<br>" + dose4 + " units/hr<br>" +
		"<font size=\"-2\">(Notify physician)</font></td></tr>");

	document.writeln("<tr align=center valign=center><td>35 - 54</td>");
	document.writeln("<td>" + hepBolusConc/2 + " units/kg =<br>" + halfBolus + 		" units</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>up by 3 U/kg/hr =<br>" + dose3 + 
		" units/hr</td></tr>");

	document.writeln("<tr align=center valign=center><td><b>55 - 75</b><br>(Therapeutic)</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>No Change</td></tr>");

	document.writeln("<tr align=center valign=center><td>76 - 95</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>down by 2 U/kg/hr =<br>" + dose2 + " units/hr</td></tr>");

	document.writeln("<tr align=center valign=center><td>96 - 115</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>30</td>");
	document.writeln("<td>down by 3 U/kg/hr =<br>" + dose3 + " units/hr</td></tr>");

	document.writeln("<tr align=center valign=center><td>116 - 150</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>60</td>");
	document.writeln("<td>down by 4 U/kg/hr =<br>" + dose4 + " units/hr</td></tr>");

	document.writeln("<tr align=center valign=center><td>&gt; 150</td>");
	document.writeln("<td>0</td>");
	document.writeln("<td>60</td>");
	document.writeln("<td>down by 5 U/kg/hr =<br>" + dose5 + " units/hr<br>" +
		"<font size=\"-2\">(Notify physician)</font></td></tr>");

	document.writeln("</table></center><p>");
	
	document.writeln("<form></dl><li>Initial Heparin Bolus (at " + hepBolusConc + " units/kg, rounded to nearest 100 units) : <b>" +
		hepBolus + "</b> units IV x 1<br>" + extraText);


	document.writeln("</form><p><li>Heparin Continuous IV Infusion (at " + hepInfusConc + " units/kg; 25,000 units in "
		+ "500 ml D<sub>5</sub>W = 50 units/ml) : <b>" + hepInfus + "</b> units/hr.<p>");
	document.writeln("<li>Draw PTT every 8 hours until within the therapeutic range (55 to 75 seconds).  Adjust " +
		"heparin dosing according to the tables above and document heparin doses administered and rate changes on " +
		"the MAR.  Once therapeutic range is reached, draw PTT daily while patient is on heparin.  Re-check PTT " +
		"in 8 hours following any dose change.<p>");
	document.writeln("<li>Contact physician if platelet count is < 100,000, or if PTT is < 35 or > 150 seconds.");
	document.writeln("</ol>");
	
	document.writeln("<center><table border=1 cellpadding=10 width=\"85%\">");
	document.writeln("<tr><td width=\"40%\">&nbsp;</td><td width=\"40%\">&nbsp;</td><td>&nbsp;</td></tr>");
	document.writeln("<tr><th><font size=\"-2\">Physician signature and pager number</font></th>");
	document.writeln("<th><font size=\"-2\">Physician printed name</font></th>");
	document.writeln("<th><font size=\"-2\">Date and Time</font></th></tr></table></center></font>");
		
	document.close();
	return true;
}

function roundNum(thisNum,dec) {
	thisNum = thisNum * Math.pow(10,dec)
	thisNum = Math.round(thisNum)
	thisNum = thisNum / Math.pow(10,dec)
	return thisNum
}
