	var _CurrentProcedureArray;
	var _BodypartArray;
	var _ProcedureBodypartMap;
	
	var _CategoryArray			= new Array();
	var _AllProceduresArray		= new Array();

	var _ContentCache = {};
	
	var _CurrentCategoryIndex	= -1;
	var _CurrentProcedureIndex	= -1;
	
	var _NoteWindow;
	
	var _ProcedureCombo;
	var _CategoryCombo;
	
	var CATEGORY_NAME_OFFEST		= 0;
	var CATEGORY_ID_OFFSET			= 1;
	
	var CONTENT_DEFINITION_INDEX	= 0;
	var CONTENT_LINKS_INDEX			= 1;
	var POPUP_HEADLINE				= 2;
	var POPUP_TEXT					= 3;
	var CONTENT_ID_INDEX			= 4;
	
	var ie4 = (document.all) ? true : false;
	var nn4 = (document.layers) ? true : false;


	function GetXmlHttpRequest()
	{
		if ( typeof XMLHttpRequest != 'undefined' )
		{
			return new XMLHttpRequest();
		}
		try {
			var x = new ActiveXObject("Msxml2.XMLHTTP");
			return x;
		} catch(e){}
		try {
			var x = new ActiveXObject("Microsoft.XMLHTTP");
			return x;
		} catch(e){}
		return null;
	}

	function WriteLayer(ID, parentID, sText) 
	{ 
		if (document.layers) 
		{ 
			var oLayer; 
			if(parentID)
			{ 
				oLayer = eval('document.' + parentID + '.document.' + ID + '.document'); 
			}
			else
			{ 
				oLayer = document.layers[ID].document; 
			} 
			oLayer.open(); 
			oLayer.write(sText); 
			oLayer.close(); 
		} 
		else if (parseInt(navigator.appVersion) >= 5 && navigator.appName=="Netscape") 
		{ 
			document.getElementById(ID).innerHTML = sText; 
		} 
		else if (document.all) 
			document.all[ID].innerHTML = sText;
	} 

			
	function InitializeDynamicContent(categoryCombo, procedureCombo)
	{
	
		_CategoryCombo = categoryCombo;
		_ProcedureCombo = procedureCombo;
		DrawCategories(_CategoryCombo);
		
		DrawProcedures(_CurrentProcedureArray, _ProcedureCombo);
		
		//LoadContent();
	
	}
	
	/*
	//In the Brave New World of ReportContent, pre-loading doesn't make any sense. So we don't.
	function LoadContent() {
		var xmlhttp = GetXmlHttpRequest();
		var contentUrl = "Utility/WebServiceContent.aspx";
		xmlhttp.open("GET", contentUrl, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				var HtmlData = xmlhttp.responseText;
				if (HtmlData.length > 0) {
				
					var AllContent = HtmlData.split("!@*@!");
					
					for (contentIndex = 0; contentIndex < AllContent.length; contentIndex++)
					{
						var Content = AllContent[contentIndex].split("|#?&|");
						_definitions[Content[CONTENT_ID_INDEX]] = Content[CONTENT_DEFINITION_INDEX];
						_links[Content[CONTENT_ID_INDEX]] = Content[CONTENT_LINKS_INDEX];
					}
					
					_contentLoaded = true;
				
				}
			}
		}
		xmlhttp.send();
	}
	*/
	
	function DrawCategories(categoryCombo)
	{
		var CategoryIndex = 0;
	
		categoryCombo.options.length = 0;
		for(i=0; i< _CategoryArray.length; i+=2)
		{
			categoryCombo.options[CategoryIndex] = new Option(_CategoryArray[i], _CategoryArray[i + CATEGORY_ID_OFFSET]);
			CategoryIndex++;
		}

	}
	
	function GetCategoryCode( category )
	{
		for ( var i = 0 ; i < _CategoryArray.length ; i++ ) {
			if ( _CategoryArray[i] == category ) {
				return _CategoryArray[i+1];
			}
		}
		return -1;
	}
	
	function GetBodypartProcedures( part )
	{
		return _BodypartArray[part];
	}
	
	function GetProcedureBodypart( proc )
	{
		if ( !_ProcedureBodypartMap ) return '';
		return _ProcedureBodypartMap[ proc ];
	}
	
	function DrawProcedures(procedureArray, procedureCombo)
	{
		var index = 0;
		procedureCombo.options.length = 0;
		for(x=0; x < procedureArray.length; x+=2)
		{
			procedureCombo.options[index] = new Option(procedureArray[x], procedureArray[x+1]);
			index++;
		}
		_CurrentProcedureIndex = -1;
		
	}

	
	function ChangeCategory(categoryCombo)
	{
		var CategoryIndex = categoryCombo.selectedIndex;
		if (_CurrentCategoryIndex == CategoryIndex)
			return;

		_CurrentCategoryIndex  = CategoryIndex;
		_CurrentProcedureArray = _AllProceduresArray[_CurrentCategoryIndex];
		
		DrawProcedures(_CurrentProcedureArray, _ProcedureCombo);
		DrawContent(categoryCombo.options[CategoryIndex].value);
	
	}

	function ChangeCategoryByKey(categoryKey)
	{
		_CurrentCategoryIndex = SelectOptionByValue(_CategoryCombo, categoryKey)
		DrawContent(categoryKey);
	}
	
	function ChangeDiagnosis(procedureCombo)
	{
		var ProcedureIndex = procedureCombo.selectedIndex;
		if (_CurrentProcedureIndex == ProcedureIndex)
		{
			return;
		}
		_CurrentProcedureIndex = ProcedureIndex;
		var DiagnosisId = procedureCombo.options[_CurrentProcedureIndex].value;



		DrawContent(DiagnosisId);
	}
	
	function DrawContent( ReportContext )
	{
		// erase any pre-existing content
		WriteDescription(" ");

		if ( ReportContext == "-1" )
			return;

		var officeVisitRegEx = /typeofservice\|officevisit/i;
		if (ReportContext.search(officeVisitRegEx) != -1)
        {
            document.getElementById('divSearchDistance').style.display = 'none';
        }
        else
        {
            document.getElementById('divSearchDistance').style.display = 'block';
        }
        
        if (_ContentCache[ReportContext] != null)
		{
			RenderContentFromCache( ReportContext );
			return;
		}


		var url = "Utility/WebServiceContent.aspx?ContentId=" + ReportContext;
		// make sure that any query string changes get passed along, to keep in 
		// use style consistent
		var QueryString = location.search.substr(1);
		if ( QueryString.length > 0 )
		{
			url = url + "&" + QueryString;
		}
		var xmlhttp = GetXmlHttpRequest();
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				var HtmlData = xmlhttp.responseText;
				if (HtmlData.length > 0) {
					var newCache = {};
					var content = HtmlData.split("|#?&|");
					newCache.Description = content[CONTENT_DEFINITION_INDEX];
					newCache.HasPopup = content[POPUP_HEADLINE].length > 1;
					newCache.PopupHeadline = content[POPUP_HEADLINE];
					newCache.PopupText = content[POPUP_TEXT];
					newCache.RelatedLinks = content[CONTENT_LINKS_INDEX];
					_ContentCache[ReportContext] = newCache;
					RenderContentFromCache( ReportContext );
				}
			}
		}
		xmlhttp.send(null);
	}
	
	function RenderContentFromCache( ReportContext )
	{
		var content = _ContentCache[ReportContext];

		WriteDescription( content.Description );
		// if we have an alert, we need to pop that up
		if ( content.HasPopup )
		{
			RaisePopup( content.PopupHeadline, content.PopupText );
		}
	
		ShowRelatedLinks( content.RelatedLinks );
	}
	
	function ShowRelatedLinks(linksHTML)
	{
		var links = document.getElementById("ContentLinks");
		if (links != null) {
			links.innerHTML  = linksHTML;
		}
	}
	
	function SelectOptionByValue(selectorObject, valueToSelect)
	{
		for (j=0; j < selectorObject.options.length; j++) 
		{
			if (selectorObject.options[j].value == valueToSelect)
			{
				selectorObject.options[j].selected = true;
				return j;
			}
		}
	}
	
	function RaisePopup(headlineText, mainText)
	{
		_NoteWindow=window.open("", "MyWindow","menubar=no,height=300,width=400");
		_NoteWindow.document.write("<html>");
		_NoteWindow.document.write("<head>");
		_NoteWindow.document.write("<style> .PopupContent {	font-family: Verdana, Arial, Helvetica;	font-size: 8.5pt; color: #333; margin: 0 0pt 0pt 10pt; } </style>");
		_NoteWindow.document.write("<title>" + headlineText + "</title></head>");
		// bcg 02032004 -- modified in search of nasty cookie bug
		//_NoteWindow.document.write('<body onUnload="if(window.opener._NoteWindow)window.opener._NoteWindow = null;"><TABLE id="MessageTable" cellSpacing="1" cellPadding="1" width="100%" align="center" border="0">');
		_NoteWindow.document.write('<body><TABLE id="MessageTable" cellSpacing="1" cellPadding="1" width="100%" align="center" border="0">');
		
		_NoteWindow.document.write('<TR><TD class="PopupContent">' + mainText  + '</TD></TR>');
		_NoteWindow.document.write('<TR><TD vAlign="center" align="middle"><br><p><a href="javascript:window.close();">Close</a></p></td></tr></table></body></html>');
		_NoteWindow.document.close();
	}