/*
******************************************************************************************
*                                                                                        *
* File Name:         inc_formulary_prov_search1.js                                       *
* File PathName:     D:\www\healthplan_golive\GHPJavaScript\formulary\                   *
* File Description:  Javascript for provider formulary search validations                *
******************************************************************************************
* SR / TT    Date       Author Modification Description                                  *
* ---------- ---------- ------ ----------------------------------------------------------*
*    SR 3306 09-19-2005  JRS   Medicare Part D Formulary enhancement                     *
*  SR 4070	9-14-2006	SRS		Added ZD and SA formularies
******************************************************************************************
*/
//
// Get request object by Type of DOM
//
			function getObject(id)
			{
			if (IEDOM)
						{
						var ref = id;
						}
			else if (NS4DOM)
						{
						var ref = "document." + id;
						}
			else if (W3CDOM)
						{
						var ref = "document.getElementById('" + id +"')";
						}
			var object = eval(ref);
			return object;
			}
			//
			// Removes leading and trailing spaces
			//
			function trimAll(strValue)
			{
			var objRegExp = /^(\s*)$/;
			// check for all spaces
			if (objRegExp.test(strValue))
						{
						strValue = strValue.replace(objRegExp, '');
						if (strValue.length == 0) return strValue;
						}
			// check for leading & trailing spaces
			// remove leading and trailing whitespace characters
			objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
			if (objRegExp.test(strValue))
						{
						strValue = strValue.replace(objRegExp, '$2');
						}
			return strValue;
			}
			//
			// Load correct provider Categories
			//
			function obtain_correctCategories(ProvFormulary,whichSearch)
			{
			var drugobj  = getObject("drugsafe");
			var commlobj = getObject("traditional");
			var partdobj = getObject("partd");
			if ( parseInt(ProvFormulary.selectedIndex) == 0 )
						{
						if (NS4DOM)
									{
									drugobj.visibility  = "show";
									commlobj.visibility = "hide";
									partdobj.visibility = "hide";
									}
						else
									{
									drugobj.style.visibility  = "visible";
									commlobj.style.visibility = "hidden";
									partdobj.style.visibility = "hidden";
									}
						return;
						}
			if (NS4DOM)
						{
						drugobj.visibility = "hide";
						}
			else
						{
						drugobj.style.visibility = "hidden";
						}
			var formulary = ProvFormulary.options[ProvFormulary.selectedIndex].value;
			if (formulary == "C")
						{
						if (NS4DOM)
									{
									commlobj.visibility = "show";
									partdobj.visibility = "hide";
									}
						else
									{
									commlobj.style.visibility = "visible";
									partdobj.style.visibility = "hidden";
									}
						}
			else if ( (formulary == "PD") || (formulary == "BF") ||(formulary == "TT") ||(formulary == "ZD") ||(formulary == "SA") )
						{
						if (NS4DOM)
									{
									commlobj.visibility = "hide";
									partdobj.visibility = "show";
									}
						else
									{
									commlobj.style.visibility = "hidden";
									partdobj.style.visibility = "visible";
									}
						}
			else
						{
						if (NS4DOM)
									{
									drugobj.visibility  = "show";
									commlobj.visibility = "hide";
									partdobj.visibility = "hide";
									}
						else
									{
									drugobj.style.visibility  = "visible";
									commlobj.style.visibility = "hidden";
									partdobj.style.visibility = "hidden";
									}
						}
			return;
			}
			//
			// Load search form, if applicable
			//
			function ld_drugForm()
			{
			var drugobj  = getObject("drugsafe");
			var commlobj = getObject("traditional");
			var partdobj = getObject("partd");
			if (NS4DOM)
						{
						drugobj.visibility   = "show";
						commlobjl.visibility = "hide";
						partdobj.visibility  = "hide";
						}
			else
						{
						drugobj.style.visibility  = "visible";
						commlobj.style.visibility = "hidden";
						partdobj.style.visibility = "hidden";
						}
			stg_fmlyCode = trimAll(stg_fmlyCode);
			if (stg_fmlyCode.toUpperCase() == "ALL" || stg_fmlyCode.length < 1 || int_errCode == 0)
						{
						document.provider_formulary.frm_selectedFormulary.focus();
//						document.provider_formulary.frm_MegaCat.disabled     = true;
//						document.provider_formulary.frm_MegaSubCat.disabled  = true;
//						document.provider_formulary.frm_DrugName.disabled    = true;
//						document.provider_formulary.submitBtn.disabled       = true;
//						document.provider_formulary.resetBtn.disabled        = true;
						}
			else
						{
						var totFrmuly      = document.provider_formulary.frm_selectedFormulary.length;
						var totMegCatgy    = document.provider_formulary.frm_MegaCat.length;
						var totMegSubCatgy = document.provider_formulary.frm_MegaSubCat.length;
						for (var idx = 0; idx <= totFrmuly; idx++)
									{
									if (document.provider_formulary.frm_selectedFormulary.options[idx].value == stg_fmlyCode)
												{
												document.provider_formulary.frm_selectedFormulary.options[idx].selected = true;
												idx = idx + totFrmuly;
												}
									}
						if (int_errCode != 0)
									{
									idx = 0;
									for (idx = 0; idx <= totMegCatgy; idx++)
												{
												if (document.provider_formulary.frm_MegaCat.options[idx].value == stg_MegaCatgy)
															{
															document.provider_formulary.frm_MegaCat.options[idx].selected = true;
															idx = idx + totMegCatgy;
															}
												}
									idx = 0;
									for (idx = 0; idx <= totMegSubCatgy; idx++)
												{
												if (document.provider_formulary.frm_MegaSubCat.options[idx].value == stg_ProvCatgy)
															{
															document.provider_formulary.frm_MegaSubCat.options[idx].selected = true;
															idx = idx + totMegSubCatgy;
															}
												}
									document.provider_formulary.frm_DrugName.value = stg_DrugName;
									}
//						document.provider_formulary.frm_MegaCat.disabled     = false;
//						document.provider_formulary.frm_MegaSubCat.disabled  = false;
//						document.provider_formulary.frm_DrugName.disabled    = false;
//						document.provider_formulary.submitBtn.disabled       = false;
//						document.provider_formulary.resetBtn.disabled        = false;
						document.provider_formulary.frm_DrugName.focus();
						}
			}
			//
			// Validate form fields for data entered
			//
			function frmlyVerify(currForm)
			{
			var int_error_cntr       = 0;
			var flg_errorHasOccurred = false;
			var flg_errorMsgSet      = false;
			var flg_focusSet         = false;
			var err_msg = "_______________________________________________________\r\n";
							err_msg += "The following errors have occurred:\r\n\r\n";
			//
			if (parseInt(currForm.frm_selectedFormulary.selectedIndex) == 0)
						{
						err_msg += "Please select a Prescription Drug Plan.\r\n";
						err_msg += "_______________________________________________________\r\n";
						window.alert(err_msg);
						currForm.frm_selectedFormulary.focus();
						for (var ii=1; ii < currForm.elements.length; ii++)
										{
										if (currForm.elements[ii].title == 'Drug_Name1' || currForm.elements[ii].title == 'Drug_Name2')
													{
													currForm.elements[ii].value = "";
													}
										else if (currForm.elements[ii].title == 'Mega_Cat1' || currForm.elements[ii].title == 'Mega_Cat2')
													{
													currForm.elements[ii].selectedIndex = 0;
													}
										else if (currForm.elements[ii].title == 'Mega_Sub_Cat1')
													{
													currForm.elements[ii].selectedIndex = 0;
													}
										}
						return false;
						}
			else
						{
						var formulary = currForm.frm_selectedFormulary.options[currForm.frm_selectedFormulary.selectedIndex].value;
						for (var i=1; i < currForm.elements.length; i++)
										{
										if (formulary == 'C')
													{
													if (currForm.elements[i].title == 'Drug_Name1')
																{
																var idx_drug = i;
																}
													else if (currForm.elements[i].title == 'Mega_Cat1')
																{
																var idx_mega = i;
																}
													else if (currForm.elements[i].title == 'Mega_Sub_Cat1')
																{
																var idx_megasub = i;
																}
													}
										else if (formulary != 'C')
													{
													if (currForm.elements[i].title == 'Drug_Name2')
																{
																var idx_drug = i;
																}
													else if (currForm.elements[i].title == 'Mega_Cat2')
																{
																var idx_mega = i;
																}
													}
										}
//
						currForm.elements[idx_drug].value = trimAll(currForm.elements[idx_drug].value)
						if ((currForm.elements[idx_drug].value).length < 1)
									{
									err_msg += "Please specify a Brand or Generic Drug Name or a Provider Drug Category.\r\n";
									flg_errorMsgSet      = true;
									flg_errorHasOccurred = true;
									int_error_cntr      += 1;
									if (!flg_focusSet)
												{
												currForm.elements[idx_drug].focus();
												flg_focusSet = true;
												}
									}
						}
			//
			if (parseInt(currForm.elements[idx_mega].selectedIndex) == 0)
						{
						int_error_cntr += 1;
						if (!flg_errorHasOccurred)
									{
									if (!flg_errorMsgSet)
												{
												flg_errorMsgSet = true;
												err_msg += "Please specify a Brand or Generic Drug Name or a Provider Drug Category.\r\n";
												}
									flg_errorHasOccurred = true;
									}
						if (!flg_focusSet)
									{
									currForm.elements[idx_drug].focus();
									flg_focusSet = true;
									}
						}
		//
			if (formulary == 'C')
						{
						if (parseInt(currForm.elements[idx_megasub].selectedIndex) == 0)
									{
									int_error_cntr += 1;
									if (!flg_errorHasOccurred)
												{
												if (!flg_errorMsgSet)
															{
															flg_errorMsgSet = true;
															err_msg += "Please specify a Brand or Generic Drug Name or a Provider Drug Category.\r\n";
															}
												flg_errorHasOccurred = true;
												}
									if (!flg_focusSet)
												{
												currForm.elements[idx_drug].focus();
												flg_focusSet = true;
												}
									}
						}
			//
			if (flg_errorHasOccurred && 
							((formulary == "C" && int_error_cntr == 3) ||
								(formulary != "C" && int_error_cntr == 2)))
						{
						err_msg += "_______________________________________________________\r\n";
						window.alert(err_msg);
					return false;
						}
			return true;
			}

