//**************************************************************************
function cboService_onchange() {
	var objServices;
	var objPersonnels;
	var objPersonnel;
	var objElem;
	var objPersonnelRow;
	var objHoursRow;
	var objDatesRow;
	var objPersonnels;
	var objConfirmationDate;
	var intIndex = GetElemIndex(window.event.srcElement);

	// Deal with single control vs. a collection
	if (intIndex > -1)
		{	objServices = CSRequest.cboService(intIndex);
			objPersonnels = CSRequest.cboPersonnel(intIndex);
			objPersonnelRow = trPersonnels(intIndex);
			objHoursRow = trHours(intIndex);
			objDatesRow = trDates(intIndex);
			objConfirmationDate = trConfirmationDates(intIndex);
		};
	else
		{	objServices = CSRequest.cboService;
			objPersonnels = CSRequest.cboPersonnel;
			objPersonnelRow = trPersonnels;
			objHoursRow = trHours;
			objDatesRow = trDates;
			objConfirmationDate = trConfirmationDates;
		};
	if (objServices.selectedIndex != 0) 
	{	// Clear the Services cbo and add options based on Service selected
		for (objPersonnel in objPersonnels.options) {objPersonnels.remove(objPersonnel)};

		objPersonnelRow.style.display = "";
		objPersonnels.style.display = "";

		switch (objServices.value) 
		{
			case "PT" :
				objPersonnels.add(document.createElement("<OPTION></OPTION>"));
				objElem = document.createElement("OPTION");
				objElem.value = "PT";
				objElem.text = "Physical Therapist";
				objPersonnels.add(objElem);
				objElem = document.createElement("OPTION");
				objElem.value = "PTA";
				objElem.text = "Physical Therapist Assistant";
				objPersonnels.add(objElem);
				objElem = document.createElement("OPTION");
				objElem.value = "PTorA";
				objElem.text = "Physical Therapist or Assistant";
				objPersonnels.add(objElem);
				break
			case "OT" :
				objPersonnels.add(document.createElement("<OPTION></OPTION>"));
				objElem = document.createElement("OPTION");
				objElem.value = "OT";
				objElem.text = "Occupational Therapist";
				objPersonnels.add(objElem);
				objElem = document.createElement("OPTION");
				objElem.value = "COTA";
				objElem.text = "Certified Occupational Therapist Assistant";
				objPersonnels.add(objElem);
				objElem = document.createElement("OPTION");
				objElem.value = "OTorA";
				objElem.text = "Occupational Therapist or Assistant";
				objPersonnels.add(objElem);
				break
			case "ST" :
				objPersonnels.add(document.createElement("<OPTION></OPTION>"));
				objElem = document.createElement("OPTION");
				objElem.value = "SLP";
				objElem.text = "Speech Language Pathologist";
				objPersonnels.add(objElem);
				objElem = document.createElement("OPTION");
				objElem.value = "CFY";
				objElem.text = "CFY";
				objPersonnels.add(objElem);
				objElem = document.createElement("OPTION");
				objElem.value = "SPorAorSFY";
				objElem.text = "Speech Pathologist or Assistant or SFY";
				objPersonnels.add(objElem);
				break
		};
	}
	else
	{
		objPersonnels.style.display = "none";
		objPersonnelRow.style.display = "none";
		objHoursRow.style.display = "none";
		objDatesRow.style.display = "none";
		objConfirmationDate.style.display = "none";
		if (tblSubmit.style.display != "") {tblSubmit.style.display = "none";};
	};	

}
//**************************************************************************
function cboPersonnel_onchange() {
	var objHoursRow;
	var objDatesRow;
	var objPersonnels;
	var objConfirmationDate;
	var intIndex = GetElemIndex(window.event.srcElement);

	// Deal with single control vs. a collection
	if (intIndex > -1)
		{	objPersonnels = CSRequest.cboPersonnel(intIndex);
			objHoursRow = trHours(intIndex);
			objDatesRow = trDates(intIndex);
			objConfirmationDate = trConfirmationDates(intIndex);
		};
	else
		{	objPersonnels = CSRequest.cboPersonnel;
			objHoursRow = trHours;
			objDatesRow = trDates;
			objConfirmationDate = trConfirmationDates;
		};

	if (objPersonnels.selectedIndex != 0) 
		{	objHoursRow.style.display = "";
			objDatesRow.style.display = "";
			tblSubmit.style.display = "";
			objConfirmationDate.style.display = "";
			tblSubmit.scrollIntoView(1)
		};
	else
		{	objHoursRow.style.display = "none";
			objDatesRow.style.display = "none";
			if (tblSubmit.style.display != "") {tblSubmit.style.display = "none";};
			objConfirmationDate.style.display = "none";
		};
}
//**************************************************************************
function cmdNewRequest_onclick() {
// Duplicate the previous request and clear it
	var divRequests	= document.getElementsByName("divRequestDetail");
	var intNbrOfRequests = divRequests.length;
	var objPersonnel;
	CSRequest.insertAdjacentHTML("beforeEnd", divRequests(intNbrOfRequests - 1).outerHTML);
	// Hide the previous Add button
	CSRequest.cmdNewRequest(intNbrOfRequests -1).style.display = "none";
	
	// Hide new controls that should not be shown
	//CSRequest.cboPersonnel(intNbrOfRequests).style.display = "none"	;
	trDates(intNbrOfRequests).style.display = "none";
	trHours(intNbrOfRequests).style.display = "none";
	//trPersonnels(intNbrOfRequests).style.display = "none";
	trConfirmationDates(intNbrOfRequests).style.display = "none" ;

	// Blank the values on the new request
	//CSRequest.cboService(intNbrOfRequests).selectedIndex = -1;	
	CSRequest.cboPersonnel(intNbrOfRequests).selectedIndex = -1;	
	CSRequest.txtStartDate(intNbrOfRequests).value = "";
	CSRequest.txtEndDate(intNbrOfRequests).value = "";
	CSRequest.txtHours(intNbrOfRequests).value = "";
	CSRequest.txtSetting(intNbrOfRequests).value = ""  ;
	CSRequest.txtConfirmationDate(intNbrOfRequests).value = ""   ;
	
	// Clear the Personnel cbo
//	for (objPersonnel in CSRequest.cboPersonnel(intNbrOfRequests).options) {CSRequest.cboPersonnel(intNbrOfRequests).remove(objPersonnel)};
	
	// Reset the request number
	spnRequestNbr(intNbrOfRequests).innerHTML = parseInt(spnRequestNbr(intNbrOfRequests).innerText) + 1;
	
	// Scroll new request into view
	divRequestDetail(intNbrOfRequests).scrollIntoView(1);
}
//**************************************************************************
function RequestsValid() {
	// Performs validation on the Request portion of the form
	var divRequests	= document.getElementsByName("divRequestDetail");
	var intNbrOfRequests = divRequests.length;
	var intRequestNbr = 0;
	var strValue = "";
	var objElems;
	var objElem;
	var i;

	if (intNbrOfRequests == 1) 
	{
		// single request
//		if (CSRequest.cboService.value.trim() == "")
//		{
//			window.alert("Please select a 'Skill/Setting' for Request 1.");
//			CSRequest.cboService.style.backgroundColor = "<%=SSstrErrorColor%>";
//			CSRequest.cboService.focus();
//			return false;
//		};
//		else if (CSRequest.cboPersonnel.value.trim() == "")
		if (CSRequest.cboPersonnel.value.trim() == "")
		{
			window.alert("Please select 'Title' for Request 1.");
			//CSRequest.cboPersonnel.style.backgroundColor = "<%=SSstrErrorColor%>";
			CSRequest.cboPersonnel.focus();
			return false;
		};
		else if (CSRequest.txtStartDate.value.trim() == "" || CSRequest.txtEndDate.value.trim() == "")
		{
			window.alert("Please specify both a 'Requested From & To Date' for Request 1.");
			if (CSRequest.txtStartDate.value.trim() == "")
			{
				//CSRequest.txtStartDate.style.backgroundColor = "<%=SSstrErrorColor%>";
				CSRequest.txtStartDate.focus();
				CSRequest.txtStartDate.select();
			}
			else
			{
				//CSRequest.txtEndDate.style.backgroundColor = "<%=SSstrErrorColor%>";
				CSRequest.txtEndDate.focus();
				CSRequest.txtEndDate.select();
			}
			return false;
		};
		else if (CSRequest.txtHours.value.trim() == "")
		{
			window.alert("Please specify 'Hours' for Request 1.");
			//CSRequest.txtHours.style.backgroundColor = "<%=SSstrErrorColor%>";
			CSRequest.txtHours.focus();
			CSRequest.txtHours.select();
			return false;
		};
		else if (CSRequest.txtSetting.value.trim() == "")
		{
			window.alert("Please specify 'Clinical Setting' for Request 1.");
			//CSRequest.txtSetting.style.backgroundColor = "<%=SSstrErrorColor%>";
			CSRequest.txtSetting.focus();
			CSRequest.txtSetting.select();
			return false;
		};
		else if (CSRequest.txtConfirmationDate.value.trim() == "")
		{
			window.alert("Please specify 'Confirmation Date' for Request 1.");
			//CSRequest.txtConfirmationDate.style.backgroundColor = "<%=SSstrErrorColor%>";
			CSRequest.txtConfirmationDate.select();
			return false;
		};
	}
	else
	{
		// multiple requests
//		objElems = document.getElementsByName("cboService")
//		if (!fnIsBlank(objElems, "Skill/Setting")) {return false;}

		objElems = document.getElementsByName("cboPersonnel")
		if (!fnIsBlank(objElems, "Title")) {return false;}

		objElems = document.getElementsByName("txtStartDate")
		if (!fnIsBlank(objElems, "Requested From Date")) {return false;}

		objElems = document.getElementsByName("txtEndDate")
		if (!fnIsBlank(objElems, "Requested To Date")) {return false;}

		objElems = document.getElementsByName("txtHours")
		if (!fnIsBlank(objElems, "Hours")) {return false;}

		objElems = document.getElementsByName("txtSetting")
		if (!fnIsBlank(objElems, "Clinical Setting")) {return false;}

		objElems = document.getElementsByName("txtConfirmationDate")
		if (!fnIsBlank(objElems, "Confirmation needed by")) {return false;}
	}
	return true;
}
//**************************************************************************
function fnIsBlank(objElems, strFieldName)
{
	// Validate a collection of elements for the presense of contents
	var objElem;
	var i;
	for (i=0; i <= objElems.length-1; i++)
	{
		objElem = objElems.item(i);
		if (objElem.value.trim() == "")
		{
			window.alert("Please specify '" + strFieldName + "' for Request " + (i + 1) + ".");
			//objElem.style.backgroundColor = "<%=SSstrErrorColor%>";
			objElem.focus();
			return false;
		};
	}
	return true;
}
//**************************************************************************
function cmdSubmit_onclick() {
	// pre-validates data before sending to server

	var objElems = document.getElementsByTagName("input");
	var objSubElems;
	var i, x;

	// Clear warning highlight
	for (i=0; i <= objElems.length - 1; i++) 
	{
		if (objElems.item(i).length == undefined)
		{
			// Single Element
			objElems.item(i).style.backgroundColor = "";
		}
		else
		{
			// Multiple Elements
			objSubElems = objElems.item(i);
			for (x=0; i <= objSubElems.length - 1; x++) 
			{
				objSubElems.item(i).style.backgroundColor = "";
			};
		}
	};
	objElems = document.getElementsByTagName("select");
	for (i=0; i <= objElems.length - 1; i++) 
	{
		objElems.item(i).style.backgroundColor = "";
	};
	// Validate
	if (CSRequest.txtFacilityName.value.trim() == "")
	{
		window.alert("Please enter 'Facility Name'.");
		//CSRequest.txtFacilityName.style.backgroundColor = "<%=SSstrErrorColor%>";
		CSRequest.txtFacilityName.focus();
		CSRequest.txtFacilityName.select();
	};
	else if (CSRequest.txtContact.value.trim() == "")
	{
		window.alert("Please enter 'Contact Name'.");
		//CSRequest.txtContact.style.backgroundColor = "<%=SSstrErrorColor%>";
		CSRequest.txtContact.focus();
		CSRequest.txtContact.select();
	};
	else if (CSRequest.txtEMail.value.trim() == "" && CSRequest.txtPhone.value.trim() == "")
	{
		window.alert("Please enter a 'phone number' or 'e-mail address'.");
		if (CSRequest.txtPhone.value.trim() == "")
		{
			//CSRequest.txtPhone.style.backgroundColor = "<%=SSstrErrorColor%>";
			CSRequest.txtPhone.focus();
			CSRequest.txtPhone.select();
		}
		else
		{
			//CSRequest.txtEMail.style.backgroundColor = "<%=SSstrErrorColor%>";
			CSRequest.txtEMail.focus();
			CSRequest.txtEMail.select();
		}		
	};
	else if (!RequestsValid())
	{
		return false;
	};
	else
	{
		// Submit
		CSRequest.submit();
	};
}
//**************************************************************************
function chkBillingSame_onclick() {
	if (CSRequest.chkBillingSame.checked == true)
	{
		if (CSRequest.txtAddress.value.trim() + CSRequest.cboState.value.trim() + CSRequest.cboState.value.trim() +  CSRequest.txtCity.value.trim() + CSRequest.txtZip.value.trim() == "")
		{
			// Don't do anything if they haven't specified site of service yet
			window.alert("Please specify the Site of Service before selecting this option.");
			CSRequest.chkBillingSame.checked = false;
			return false;
		}
		CSRequest.txtBillingAddress.value = CSRequest.txtAddress.value;
		CSRequest.cboBillingState.value = CSRequest.cboState.value;
		CSRequest.txtBillingCity.value = CSRequest.txtCity.value;
		CSRequest.txtBillingZip.value = CSRequest.txtZip.value;
	};
	else
	{
		CSRequest.txtBillingAddress.value = "";
		CSRequest.cboBillingState.value = "";
		CSRequest.txtBillingCity.value = "";
		CSRequest.txtBillingZip.value = "";
	};
}
