var ns4 = (document.layers)? true : false;
var ie4 = (document.all)? true : false;
// var ie4 = (document.all && !document.getElementById)? true : false;
// var ie5 = (document.getElementById && document.all)? true : false;
var ns6 = (document.getElementById && !document.all)? true: false;
var w3c = (document.getElementById)? true : false;

var hepB = "<b class=\"tdfont\">Hepatitis B</b>";
var dtaP = "<b class=\"tdfont\">Diphtheria, Tetanus, Pertussis</b>";
//  var hib = "<b class=\"tdfont\"><i style=\"font-family: 'Times New Roman';\">H. influenzae</i>, type b</b>";
var hib = "<b class=\"tdfont\"><i>H. influenzae</i>, type b</b>";
var polio = "<b class=\"tdfont\">Inactivated Polio</b>";
var pcv = "<b class=\"tdfont\">Pneumococcal Conjugate</b>";
var mmr = "<b class=\"tdfont\">Measles, Mumps, Rubella</b>";
var cpox = "<b class=\"tdfont\">Varicella</b>";
var hepA = "<b class=\"tdfont\">Hepatitis A</b>";
	
var dayName = new Array("Sunday","Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday");
var monName = new Array("January", "February", "March", "April","May", "June", "July", "August", "September", "October","November", "December");

	//  Create array of maximum days
	var maxDays = new Array(12);

	maxDays[0] = 31;	// January has 31 days
	maxDays[1] = 28;
	maxDays[2] = 31;
	maxDays[3] = 30;
	maxDays[4] = 31;
	maxDays[5] = 30;
	maxDays[6] = 31;
	maxDays[7] = 31;
	maxDays[8] = 30;
	maxDays[9] = 31;
	maxDays[10] = 30;
	maxDays[11] = 31;

function numDays(month,year) {
	//  Year 2000 test
	if (year < 300) {year += 1900}
	if (month == 1) {		//  February
		if ((year / 4) == Math.floor(year/4)) {
			if ((year / 100) == Math.floor(year/100)) {
				if ((year / 400) == Math.floor(year/400)) {days = 29;}
				else {days = 28;}
			} else {days = 29;}
		} else {days = 28;}
	} else {days = maxDays[month]}
	
	return days;
}

function whatDay(form,whichOne) {
	var shots = document.shots;
	var month = form.month.selectedIndex;
	var date = form.date.selectedIndex + 1;
	var year = form.year.value;
	if (month==-1 || date==0 || year=="") {return;}
	
	var today = new Date();
	thisYear = today.getYear();

	//  Year 2000 test
	if (thisYear < 300) {thisYear = thisYear + 1900}
	
	//  Change dates to reflect max days in selected month
	newDays = numDays(month,year);
	curDays = form.date.length;
	daysObject = form.date;
	
	if (curDays > newDays) {
    	for (i=0; i<(curDays - newDays); i++) {
			daysObject.options[daysObject.options.length - 1] = null
		}
		if (date > newDays) {daysObject.selectedIndex = (daysObject.options.length - 1);}
	}
	if (newDays > curDays) {
//		for (i=0; i<(newDays-curDays); i++) {
		for (i=curDays; i < newDays; i++) {
			newOption = new Option(daysObject.options.length + 1);
			//  daysObject.add(newOption);
			daysObject.options[i] = newOption;
		}
	}
//	if (daysObject.selectedIndex < 0) daysObject.selectedIndex == 0;
	if (daysObject.selectedIndex < 0) {daysObject.selectedIndex == daysObject.options.length - 1;}
	date = form.date.selectedIndex + 1;
	
	var whatDate = new Date(monName[month] + " " + date + ", " + year + " 12:00:00");
	form.day.selectedIndex = whatDate.getDay();

	// Calculate current age
		var today = new Date();
		var addTime = today.getTime() - whatDate.getTime();

		var addDays = addTime / (1000*60*60*24);
		var addWeeks = addDays / 7;
		var oldaddWeeks = addWeeks;
//		var addMonths = addDays / 30;
			var addMonthsYr = 12 * (today.getFullYear() - whatDate.getFullYear());
			var addMonthsMon = today.getMonth() - whatDate.getMonth();
			var addMonthsDay = (today.getDate() - whatDate.getDate()) / 30;
			
			addMonths = addMonthsYr + addMonthsMon + roundNum(addMonthsDay,1);
		var addYears = addMonths / 12;
		
		var addWeekDays = roundNum((addDays % 7),0);
		if (addWeekDays == 7) {
			addWeeks += 1;
			oldaddWeeks += 1;
			addWeekDays = 0
		}
		addWeeks = Math.floor(addWeeks);
		var oldaddWeeks = addWeeks;
		if (addWeekDays != 0) {addWeeks = addWeeks + " " + addWeekDays + "/7"}

		form.ageDays.value = roundNum(addDays,0);
		form.ageWeeks.value = addWeeks;
		form.ageMonths.value = roundNum(addMonths,1);
		form.ageYears.value = roundNum(addYears,1);

		if (addYears < 1) {var ageType = 2} else {ageType = 3};
		if (addMonths < 1) {var ageType = 1};
		if (oldaddWeeks < 1) {var ageType = 0};
		
		getAge(ageType);

	return true;
}

function getAge(ageType) {
	var shots = document.shots;
	if (ageType == 0) {var whatAge = shots.ageDays.value};
	if (ageType == 1) {var whatAge = shots.ageWeeks.value};
	if (ageType == 2) {var whatAge = shots.ageMonths.value};
	if (ageType == 3) {var whatAge = shots.ageYears.value};

	shots.age.value = whatAge;
	shots.ageType.selectedIndex = ageType;
	return true;
}

function clearForm(form) {
	form.day.selectedIndex = -1;
	form.month.selectedIndex = -1;
	form.date.selectedIndex = -1;
	form.year.value = "";
	form.ageDays.value = "";
	form.ageWeeks.value = "";
	form.ageMonths.value = "";
	form.ageYears.value = "";
	return;
}

function shotsUTD(isUTD) {
	//  isUTD value = 0 if UTD, 1 if needs 1 set, 2 if needs more shots
	var shots = document.shots;
	var age = shots.age.value;
	var ageType = shots.ageType.selectedIndex;
	var ageMonths = convertAge(age, ageType, "month");
	var ageYears = convertAge(age, ageType, "year");
/*
	if (ns4) {
		alertText = "";
		for (i=0; i<document.layers.length; i++) {
			tempEl = document.layers[i];
	  		alertText += "div" + i + " = " + tempEl.id + "\n";
		}
		alert (alertText);
	}

	if (ageMonths < 2) {numShots(1,0,0,0,0,0,0,0,isUTD);}
	if (ageMonths >= 2 && ageMonths < 4) {numShots(2,1,1,1,1,0,0,0,isUTD);}
	if (ageMonths >= 4 && ageMonths < 6) {numShots(2,2,2,2,2,0,0,0,isUTD);}
	if (ageMonths >= 6 && ageMonths < 12) {numShots(3,3,3,3,3,0,0,0,isUTD);}
	if (ageMonths >= 12 && ageMonths < 15) {numShots(3,3,4,3,4,1,1,0,isUTD);}
	if (ageMonths >= 15 && ageMonths < 18) {numShots(3,4,4,3,4,1,1,0,isUTD);}
	if (ageMonths >= 18 && ageMonths < 24) {numShots(3,4,4,3,4,1,1,0,isUTD);}

	if (ageYears >= 2 && ageYears < 4) {numShots(3,4,4,3,4,1,1,2,isUTD);}
	if (ageYears >= 4 && ageYears < 11) {numShots(3,5,4,4,4,2,1,2,isUTD);}
	if (ageYears >= 11 && ageYears < 18) {numShots(3,6,4,4,4,2,1,2,isUTD);}
	if (ageYears >= 18) {numShots(3,3,0,3,2,2,2,2,isUTD);}
*/
	var shotSet = new Array();
	shotSet[0] = "numShots(0,0,0,0,0,0,0,0)";
	shotSet[1] = "numShots(1,0,0,0,0,0,0,0)";
	shotSet[2] = "numShots(2,1,1,1,1,0,0,0)";
	shotSet[3] = "numShots(2,2,2,2,2,0,0,0)";
	shotSet[4] = "numShots(3,3,3,3,3,0,0,0)";
	shotSet[5] = "numShots(3,3,4,3,4,1,1,0)";
	shotSet[6] = "numShots(3,4,4,3,4,1,1,0)";
	shotSet[7] = "numShots(3,4,4,3,4,1,1,0)";
	shotSet[8] = "numShots(3,4,4,3,4,1,1,2)";
	shotSet[9] = "numShots(3,5,4,4,4,2,1,2)";
	shotSet[10] = "numShots(3,6,4,4,4,2,1,2)";
	shotSet[11] = "numShots(3,3,0,3,2,2,2,2)";

	if (ageMonths < 2) {shotNum = 1;}
	if (ageMonths >= 2 && ageMonths < 4) {shotNum = 2;}
	if (ageMonths >= 4 && ageMonths < 6) {shotNum = 3;}
	if (ageMonths >= 6 && ageMonths < 12) {shotNum = 4;}
	if (ageMonths >= 12 && ageMonths < 15) {shotNum = 5;}
	if (ageMonths >= 15 && ageMonths < 18) {shotNum = 6;}
	if (ageMonths >= 18 && ageMonths < 24) {shotNum = 7;}

	if (ageYears >= 2 && ageYears < 4) {shotNum = 8;}
	if (ageYears >= 4 && ageYears < 11) {shotNum = 9;}
	if (ageYears >= 11 && ageYears < 18) {shotNum = 10;}
	if (ageYears >= 18) {shotNum = 11;}

	if (isUTD == 1) {shotNum--;}
	eval(shotSet[shotNum]);
	
	return;
}

function evalShots(form) {
	var age = form.age.value;
	var ageType = form.ageType.selectedIndex;
	var ageMonths = convertAge(age, ageType, "month");
	var ageYears = convertAge(age, ageType, "year");
	
	var hepB = form.HepB.value;
	
	if (ns4) {form = document.shots2}
	var dtaP = form.DTaP.value;
	var hib = form.Hib.value;
	var ipv = form.Polio.value;
	var pcv = form.PCV.value;
	var mmr = form.MMR.value;
	var cPox = form.cPox.value;
	var hepA = form.HepA.value;
	var shotsText = "";
	var footNotes = "";

	//  Hepatitis B
	if (ageMonths < 2 && hepB < 1) {shotsText = "<li>Hepatitis B #1";} else
	if (ageMonths >= 2 && hepB < 2) {
		shotsText = "<li>Hepatitis B # 2";
		footNotes = "<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Hep B :</b> Hepatitis B #2 should be given at least 1 month after first dose.</div>";
	} else if (ageMonths >= 6 && hepB < 3) {
		shotsText = "<li>Hepatitis B # 3";
		footNotes = "<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Hep B :</b> Hepatitis B #3 should be given at least 4 months after first dose AND at least 2 months after second dose.</div>";
	}
	
	//  DTaP
	if (ageMonths >= 2 && ageMonths < 4 && dtaP < 1) {shotsText += "\n<li>DTaP #1";} else
	if ((ageMonths >= 4 && ageMonths < 6 && dtaP < 2) || (ageMonths >= 6 && ageMonths < 15 && dtaP < 3) || (ageMonths >= 15 && ageYears < 4 && dtaP < 4) || (ageYears >=4 && ageYears < 11 && dtaP < 5))
		{shotsText += "\n<li>DTaP # " + (1 + parseInt(dtaP));}
	if (ageYears >= 11 && ageYears <= 16) {
		shotsText += "\n<li>Td, if none given in past 5 years";
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Td :</b> If no previous Td doses, will need 2 Td doses 4-8 weeks apart, then Td #3 at least 6 months after Td #2.</div>";
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Td :</b> Td booster every 10 years.</div>";
	}
	if (ageYears > 16) {
		shotsText += "\n<li>Td booster every 10 years.";
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Td :</b> If no previous Td doses, will need 2 Td doses 4-8 weeks apart, then Td #3 at least 6 months after Td #2.</div>";
	}

	//  Hib
	if ((ageMonths >= 2 && ageMonths < 4 && hib < 1) || (ageMonths >= 4 && ageMonths < 6 && hib < 2) || (ageMonths >= 6 && ageMonths < 12 && hib < 3) || (ageMonths >= 12 && ageMonths <= 18 && hib < 4))
		{shotsText += "\n<li>Hib # " + (1 + parseInt(hib));}
	
	//  IPV
	if ((ageMonths >= 2 && ageMonths < 4 && ipv < 1) || (ageMonths >= 4 && ageMonths < 6 && ipv < 2) || (ageMonths >= 6 && ageMonths < 48 && ipv < 3) || (ageYears >= 4 && ageYears < 7 && ipv < 4))
		{shotsText += "\n<li>IPV # " + (1 + parseInt(ipv));}
	if (ageYears >= 13 && ipv < 3) {
		shotsText += "\n<li>IPV # " + (1 + parseInt(ipv));
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>IPV :</b> IPV #2 should be given at least 4 weeks after IPV #1.  IPV #3 should be given at least 6 months after IPV #2.</div>";
	}
	
	//  Pneumococcal Vaccine
	//	http://www.cdc.gov/mmwr/PDF/rr/rr4909.pdf
	//  ****  Need to change the overlap between ageMonths 12-15  ****
	if ((ageMonths >= 2 && ageMonths < 4 && pcv < 1) || (ageMonths >= 4 && ageMonths < 6 && pcv < 2) || (ageMonths >= 6 && ageMonths < 12 && pcv < 3) || (ageMonths >= 12 && ageMonths <= 15 && pcv < 4)) {
		shotsText += "\n<li>Pneumococcal PCV # " + (1 + parseInt(pcv));
	}
	if (ageMonths >= 12 && ageMonths <= 23 && pcv < 2) {
		shotsText += "\n<li>Pneumococcal PCV # " + (1 + parseInt(pcv));
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Pneumococcal PCV :</b> Pneumococcal PCV #2 should be given at least 2 months after 1st dose.</div>";
	}
	if (ageMonths >= 24 && ageMonths <= 59 && pcv < 1) {
		shotsText += "\n<li>Pneumococcal PCV # " + (1 + parseInt(pcv));
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Pneumococcal PCV :</b> Pneumococcal PCV #2 should be given to children with certain chronic diseases at least 2 months after 1st dose.</div>";
	}
	if (ageYears >= 65) {
		shotsText += "\n<li>Pneumococcal Vaccine (PPV), if none given in past 5 years";
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Pneumococcal PPV :</b> Pneumococcal Vaccine (PPV) #2 should be given at least 5 years after PPV #1.</div>";
	}
	if (ageYears >=2 && ageYears < 65) {
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Pneumococcal PPV :</b> Consider Pneumococcal Vaccine (PPV) in persons > 2 years old with certain chronic illnesses, to be given at least 2 months after previous Pneumococcal PCV dose.  PPV #2 should then be given at least 5 years after PPV #1.</div>";
	}
		
	//  MMR
	if (ageYears >= 1 && ageYears < 4 && mmr < 1) {shotsText += "\n<li>MMR #1";} else
	if (ageYears >= 4 && mmr < 2) {
		shotsText += "\n<li>MMR # " + (1 + parseInt(mmr));
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>MMR :</b> MMR #2 should be given at least 4 weeks after MMR #1.</div>";
	}
	
	//  Varicella
	if (ageYears >= 1 && ageYears < 13 && cPox < 1) {shotsText += "\n<li>Varicella";} else
	if (ageYears >= 13 && cPox < 2) {
		shotsText += "\n<li>Varicella # " + (1 + parseInt(cPox));
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Varicella :</b> Persons &gt;= 13 yrs should receive 2 Varicella doses, given at least 4 weeks apart.</div>";
	}
	
	//  Hepatitis A
	if (ageYears >= 2 && hepA < 2) {
		shotsText += "\n<li>Hepatitis A # " + (1 + parseInt(hepA));
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Hep A :</b> Hep A #2 should be given at least 6 months after Hep A #1.</div>";
	}
	
	//  Influenza
	if (ageYears >= 50) {
		shotsText += "\n<li>Influenza Vaccine (yearly, Sept - Dec)";
	}
	if (ageMonths >= 6) {
		footNotes += "\n<div class=\"lilIndent\"><span class='bul'>&#149; </span><b>Influenza :</b> Consider Influenza Vaccine in certain patients > 6 months age with chronic diseases.</div>";
	}
	
	
	if (shotsText == "") {shotsText = "<span class='tdFont'>All shots are up-to-date.</span>";}
		else {shotsText = "<div class='tdFont'><ul>\n" + shotsText + "</ul><p>\nAll other shots are up-to-date.</div>";}
//	if (footNotes != "") {footNotes = "<b>Footnotes:</b><br><span style=\"font-size:10pt\">\n" + footNotes + "</span>";}
	if (footNotes != "") {footNotes = "<b class='tdFont'>Footnotes:</b><br>\n" + footNotes;}

//	alert (shotsText + "\n\n" + footNotes);
		
	if (ns4) {
		document.nscontainer.document.open();
//		document.nscontainer.document.layers["shotsText"].document.open();
//		document.nscontainer.document.layers["shotsText"].document.write(shotsText + "<p>" + footNotes);
		document.nscontainer.document.write(shotsText + "<p>" + footNotes);
//		document.nscontainer.document.layers["shotsText"].document.close();
		document.nscontainer.document.close();
	} else if (ie4) {form.all.shotsText.innerHTML = shotsText + "<p>" + footNotes;}
	else if (ns6) {document.getElementById("shotsText").innerHTML = shotsText + "<p>" + footNotes;}
	
	return;
}

function numShots(hepB,dtaP,hib,ipv,pcv,mmr,cPox,hepA) {
	var shots = document.shots;
	shots.HepB.value = hepB;
	if (ns4) {shots = document.shots2;}
	shots.DTaP.value = dtaP;
	shots.Hib.value = hib;
	shots.Polio.value = ipv;
	shots.PCV.value = pcv;
	shots.MMR.value = mmr;
	shots.cPox.value = cPox;
	shots.HepA.value = hepA;
	
	return;
}

function changeStyle(tag, whatColor) {
	//  alert ("tag = " + tag + "\nwhatColor = " + whatColor);
	if (ns4) {tag.color = whatColor;}
		else {tag.style.color = whatColor;}
	return;
}


function convertAge(age, ageType, convertTo) {
	parseFloat(age);
	if (convertTo == "month") {
		if (ageType == "2") {return age;}
		if (document.shots.ageMonths.value != "") {
			return document.shots.ageMonths.value;
		}
		if (ageType == "0") {age = age/30}
		if (ageType == "1") {age = age/4}
		if (ageType == "3") {age *= 12}

		age = roundNum(age,1);
		return age;
	}
	
	if (convertTo == "year") {
		if (ageType == "3") {return age;}
		if (document.shots.ageYears.value != "") {
			return document.shots.ageYears.value;
		}
		if (ageType == "0") {age = age/365}
		if (ageType == "1") {age = age/52}
		if (ageType == "2") {age = age/12}

		age = roundNum(age,1);
		return age;
	}
}

function dateSelect(Month,dd,Year,whichOne) {
	var today = new Date();

	// JavaScript:  Choose Day
	document.writeln("<select name=\"day\" " + "onChange=\"whatDay(this.form," + whichOne + ")\">");

	//	Select the names for the days
	for (var i=0; i < 7; i++) {
		if (i == today.getDay()) {document.writeln("<option selected>" + dayName[i]);}
			else {document.writeln("<option>" + dayName[i]);}
	}
	document.writeln("</select>, ");

	// JavaScript:  Choose Month
	document.writeln("<select name=\"month\" " + "onChange=\"whatDay(this.form," + whichOne + ");shotsUTD(document.shots.nowShots.selectedIndex);evalShots(this.form);\">");

	//	Select the names for the months
	for (var i=0; i < 12; i++) {
		if (i == Month) {document.writeln("<option selected>" +	monName[i]);}
			else {document.writeln("<option>" + monName[i]);}
	}
	document.writeln("</select>");

	// JavaScript:  Choose Date
	document.writeln("<select name=\"date\" " +	"onChange=\"whatDay(this.form," + whichOne + ");shotsUTD(document.shots.nowShots.selectedIndex);evalShots(this.form);\">");
	var dMax = numDays(Month,Year);
	for (var d = 1; d <= dMax; d++) {
		if (d == dd) {document.writeln("<option selected>" + dd);}
			else {document.writeln("<option>" + d);}
	}
	document.writeln("</select>, ");

	// JavaScript:  Choose Year
	/*
		For dates before the year 2000 (ex. 1999), the year
		is stored as "xx" (ex. 99).  However, for dates on or 
		after the year 2000 (ex. 2003), the year is stored as 
		"xxxx" (ex. 2003).
	*/
	if (Year < 300) {Year = Year + 1900}
	document.writeln("<input type=text size=4 name=\"year\" value=\"" + Year + "\" " + 
		"onChange=\"whatDay(this.form," + whichOne + ");shotsUTD(document.shots.nowShots.selectedIndex);evalShots(this.form);\">");

	return true;
}

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