﻿// Support scripts

function ClientSupport_GetIdFromSubstring(ObjToSearch, StrMatch)
{
	var a = 0, BoolMatch = false;
	if (ObjToSearch != null)
	{
		while (a < ObjToSearch.childNodes.length && BoolMatch == false)
		{
			if (ObjToSearch.childNodes[a].id != null && ObjToSearch.childNodes[a].id.length > StrMatch.length && ObjToSearch.childNodes[a].id.substr(ObjToSearch.childNodes[a].id.length - StrMatch.length, StrMatch.length) == StrMatch)
				BoolMatch = true;
			a++;
		}
		if (BoolMatch == true)
			return document.getElementById(ObjToSearch.childNodes[a - 1].id);
	}
	return null;
}

function ClientSupport_SetupDateDay(DdlMonth, StrDdlDayId, StrParentContainerId)
{
	var a = 0; IntDayUpper = -1, ObjDdlDay = ClientSupport_GetIdFromSubstring(document.getElementById(StrParentContainerId), StrDdlDayId), ObjOption = null;
	if (DdlMonth != null && ObjDdlDay != null)
	{
		if (DdlMonth.value == "4" || DdlMonth.value == "6" || DdlMonth.value == "9" || DdlMonth.value == "11")
			IntDayUpper = 30;
		else if (DdlMonth.value == "2")
			IntDayUpper = 29;
		else
			IntDayUpper = 31;
			
		if (ObjDdlDay.options.length - 1 < IntDayUpper)
			for (a = ObjDdlDay.options.length - 1; a < IntDayUpper; a++)
			{
				ObjOption = document.createElement("option");
				ObjOption.text = (a + 1).toString();
				ObjOption.value = (a + 1).toString();
				try
				{
					// The following is standards compliant, but does not work in Internet Explorer.
					ObjDdlDay.add(ObjOption, null);
				}
				catch (Ex)
				{
					// The following works in Internet Explorer.
					ObjDdlDay.add(ObjOption);
				}
			}
		else if (ObjDdlDay.options.length - 1 > IntDayUpper)
			for (a = ObjDdlDay.options.length - 1; a > IntDayUpper; a--)
				ObjDdlDay.remove(a);
	}
}

function ClientSupport_ShowBrand(StrSenderId)
{
	var ObjSender = document.getElementById(StrSenderId);
	
	if (ObjSender.id.indexOf("DdlMedicationsListGeneric") >= 0)
	{
		var a = 0, BoolOk = false;
		var IntMedicationNumber = ObjSender.id.substr(ObjSender.id.indexOf("DdlMedicationsListGeneric") + 25);
		if (document.getElementById("SpanMedicationsList" + IntMedicationNumber) != null)
		{
			if (ObjSender.value == "Brand")
				document.getElementById("SpanMedicationsList" + IntMedicationNumber).className = "Page_SpanNormal";
			else
				document.getElementById("SpanMedicationsList" + IntMedicationNumber).className = "Page_SpanHidden";
		}
	}
}

function ClientSupport_ShowOther(StrSpanName, StrDropDownListName)
{
	var ObjDropDownList = document.getElementById(StrDropDownListName), ObjSpan = document.getElementById(StrSpanName);
	if (ObjDropDownList != null && ObjSpan != null)
		if (ObjDropDownList.value == "Other")
			ObjSpan.style.display = "inline";
		else
			ObjSpan.style.display = "none";
}

function ClientSupport_ShowOtherRadioButton(StrSpanName, StrSpanContainer, StrRadioButtonOther, StrRadioButtonOtherContainer)
{
	var a = 0, BoolFound = false;
	while (a < document.getElementById(StrRadioButtonOtherContainer).childNodes.length && BoolFound == false)
	{
		if (document.getElementById(StrRadioButtonOtherContainer).childNodes[a].tagName == "SPAN")
			BoolFound = true;
		a++;
	}
	var ObjRadioButtonOtherSpan = document.getElementById(StrRadioButtonOtherContainer).childNodes[a - 1];
	var ObjRadioButtonOther = ClientSupport_GetIdFromSubstring(ObjRadioButtonOtherSpan, StrRadioButtonOther), ObjSpan = ClientSupport_GetIdFromSubstring(document.getElementById(StrSpanContainer), StrSpanName), ObjSpan2 = ClientSupport_GetIdFromSubstring(document.getElementById(StrRadioButtonOtherContainer), StrSpanName + "2");
	if (ObjRadioButtonOther != null && ObjSpan != null)
		if (ObjRadioButtonOther.checked == true)
		{
			ObjSpan.style.display = "inline";
			ObjSpan2.style.display = "inline";
		}
		else
		{
			ObjSpan.style.display = "none";
			ObjSpan2.style.display = "none";
		}
}