window.IsWebMD = true;

function OpenPopup(
	sPage,
	sPageName,
	sOptions,
	nWidth,
	nHeight,
    bShowScrollbar,
    bOverrideMaxSize,
    nMaxHeight,
    nMaxWidth) {
    
	var popupOffsetX = 25;
	var popupOffsetY = 25;
    
	var hasScrollbar = (bShowScrollbar == true) ? "yes" : "no";
    
   if (navigator.appName=="Netscape") {
      var posString = "screenX=" + popupOffsetX + ",screenY=" + popupOffsetY + ",";
      if (!sPageName)
         sPageName = "new";
   } else {
      var posString = "left=" + popupOffsetX + ",top=" + popupOffsetY + ",";
      if (!sPageName)
         sPageName = "_blank";
   }

   var sh = screen.height - 100;
   var sw = screen.width - 100;
   if (bOverrideMaxSize)
      var maxHeight = sh, maxWidth = sw;
   else {
      var maxHeight = (sh < nMaxHeight) ? sh : nMaxHeight;
      var maxWidth = (sw < nMaxWidth) ? sw : nMaxWidth;
   }

   if (nHeight > maxHeight) {
      nHeight = maxHeight;
      hasScrollbar = "yes";
   }

   if (nWidth > maxWidth) {
      nWidth = maxWidth;
      hasScrollbar = "yes";
   }

    propertiesString = posString + sOptions;
    propertiesString += ",scrollbars=" + hasScrollbar;
    propertiesString += ", width=" + nWidth;
    propertiesString += ",height=" + nHeight;
   
    return window.open(sPage, sPageName, propertiesString);
   
}

// var _NoteWindow;
function OpenContentPopup(contentName, windowHeight, windowWidth, bookmarkName)
{
	var NoteUrl = "ContentPopup.aspx?page=" + contentName ;
	
	if (bookmarkName)
	{
		if (bookmarkName.length > 0)
		{
			NoteUrl += "#" + bookmarkName;
		}
	}
	
	_NoteWindow=window.open(NoteUrl, "NoteWindow","height=" + 
	windowHeight +",width=" + windowWidth 
	+ ",fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no");
	
}

// Opens a popup window using a specified aspx page as a container
function OpenSpecifiedContentPopup(contentName, windowHeight, windowWidth, additionalData, appPath)
{
    // we can't do anything without the aspx page so just return false
    if ( appPath == null || appPath == '' )
        return false;
        
	var NoteUrl = appPath + "?page=" + contentName ;
	
	if (additionalData)
	{
		if (additionalData.length > 0)
		{
			NoteUrl += additionalData;
		}
	}
	
	_NoteWindow=window.open(NoteUrl, "NoteWindow","height=" + 
	windowHeight +",width=" + windowWidth 
	+ ",fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no");
}


// loads a url using the same container 
function OpenSpecifiedContentScreen(contentName,additionalData, appPath)
{
    // we can't do anything without the aspx page so just return false
    if ( appPath == null || appPath == '' )
        return false;
        
	var NoteUrl = appPath + "?page=" + contentName ;
	
	if (additionalData)
	{
		if (additionalData.length > 0)
		{
			NoteUrl += additionalData;
		}
	}
	window.location = NoteUrl;
	
}

	
//var _HelpWindow;
function RemoteStart(url, width, height, name) 
{
	var PageName = "Hospitals";
	if(name)
	{
		if (name.length > 0)
		{
			PageName = name;
		}
	}

	if (width == null) width = 600;
	if (height == null) height = 450;
	_HelpWindow = window.open(url, PageName, "scrollbars=yes,status,width=" + width + ",height=" + height + ",resizable=yes");
  
}

function TextWindow( txt )
{
	var w = window.open();
	w.document.open();
	w.document.write(txt);
	w.document.close();
}

//This function will toggle <TR>'s and Change the viewable text to the passed LINK object. 
function toggle(objTR,objLINK)
{
    if(objLINK.innerHTML == "[ more detail ]")
    {
       	document.getElementById("detail_" + objTR).style.display = "";
       	objLINK.innerHTML = "[ less detail ]";
    }
    else
    {
       	document.getElementById("detail_" + objTR).style.display = "none";
       	objLINK.innerHTML = "[ more detail ]";
    }

}

//This function will toggle off and on the Benchmark for a particular Measure for Generic Measure
function toggleBenchMark(objTABLE,objLINK)
{
    if(objLINK.innerHTML == "show bench mark")
    {
       	document.getElementById(objTABLE).style.display = "";
       	objLINK.innerHTML = "hide bench mark";
    }
    else
    {
       	document.getElementById(objTABLE).style.display = "none";
       	objLINK.innerHTML = "show bench mark";
    }
}

//This function will close the Benchmark Table
function hideBenchMark(objTABLE)
{
    document.getElementById(objTABLE).style.display = "none";
    var objLINK = document.getElementById(objTABLE + "_link");
    objLINK.innerHTML = "show bench mark";

}

//This function will expand/retract all rows depending on the text of the passed LINK object 
function expandAllRows(objLINK)
{
	if(objLINK.innerHTML == "[ expand all detail ]")
	{
		objLINK.innerHTML = "[ retract all detail ]";
		//show all <TR>'s
		obj = document.getElementsByTagName('TR');
		for (i=1; i<obj.length; i++)
		{
			//test for a detail TR
			if(obj[i].id.substr(0,7)=="detail_")
			{
				obj[i].style.display = "";
			}

		}
		//change all LINKs innerHTML
		objLinks = document.getElementsByTagName('A');
		for (j=0; j<objLinks.length; j++)
		{
			if((objLinks[j].innerHTML == "[ more detail ]") || (objLinks[j].innerHTML == "[ less detail ]"))
			{
				objLinks[j].innerHTML = "[ less detail ]";
			}
		}
	}
	else
	{
		objLINK.innerHTML = "[ expand all detail ]";
		//show all <TR>'s
		obj = document.getElementsByTagName('TR');
		for (i=1; i<obj.length; i++)
		{
			//test for a detail TR
			if(obj[i].id.substr(0,7)=="detail_")
			{
				obj[i].style.display = "none";
			}

		}
		//change all LINKs innerHTML
		objLinks = document.getElementsByTagName('A');
		for (l=0; l<objLinks.length; l++)
		{
			if((objLinks[l].innerHTML == "[ more detail ]") || (objLinks[l].innerHTML == "[ less detail ]"))
			{
				objLinks[l].innerHTML = "[ more detail ]";
			}
		}
	}
}

function openSpecDefinition( NUCC )
{
	RemoteStart('ProviderPopup.aspx?page=Specialty_Description_Popup&sCategoriesId='+NUCC, 550, 150);
}

function openArticle(url, client) {

	var popupUrl = "http://www.webmdhealth.com/common/content/contentViewSingle.aspx?csurl=" + url + "&c=" + client;
	var height;
	if ((screen.height - 200) < 570) {
		height = screen.height - 200;
	} else {
		height = 570;
	}
	var popup = OpenPopup(
		popupUrl,
		"ViewWebMDContent",
		"status=yes,titlebar=yes,menubar=yes,resizable=yes,alwaysRaised=yes",
		800,
		height,
		true,
		true,
		0,
		0);
	popup.focus();
	return false;
}

//Some cookie code. Not the best in the world. Stolen from somewhere. Should be replaced.
function SetBPXY(xyR) {
	var x = xyR[0] - xyR[2].left;
	var y = xyR[1] - xyR[2].top;
	document.cookie = "BPXY="+x+"|"+y+"; path=/";
}

function GetBPXY(R) {
	var nameEQ = "BPXY=";
	var ca = document.cookie.split(';');
	var ret = null;
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
		{
			ret = c.substring(nameEQ.length,c.length).split("|");
			ret[0] = ret[0] - 0 + R.left;
			ret[1] = ret[1] - 0 + R.top;
		}
	}
	return ret;
}

function ClearFlipperCookies()
{
	var Result = true;
	var Regex = new RegExp( '(?:^|;)\\s*(FilterFlipper[^=]+)=', "g" );
	
	//c is for "cookie"
	var c = document.cookie;
	while (Result)
	{
		Result = Regex.exec( c );
		if ( Result )
		{
			var cookieName = Result[1];
			document.cookie = cookieName + "=0;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	}
}
//End bad stolen cookie code

//Some scroll/height/width code. Not the best in the world, but pretty good. Stolen from somewhere. Should be cleaned up.
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
//End mediocre stolen scoll/width/height code

/// <summary>
///     transforms a javascript array object into a csv
/// </summary>
/// <param name="array">array to be transformed</param>
function getCSV( array )
{
	var csv = "";
	for ( index = 0; index < array.length; index++ )
	{
		csv += array[ index ];
		if ( index < array.length - 1 )
		{
			csv += ",";
		}
	}
	
	return csv;
}


/// <summary>
///		Create an HTML text node
/// </summary>
/// <param name="txt">
///		Text to be put in the node
/// </param>
function TEXT( txt )
{
	return document.createTextNode( txt );
}

/// <summary>
///		Create an HTML object with certain properties
/// </summary>
/// <param name="type">
///		Type of HTML object to create (e.g. "div" or "span")
/// </param>
/// <param name="props">
///		A javascript object whose properties the new HTML object should match
/// </param>
function HTML( type, props )
{
	return PullProps( document.createElement(type), props );
}

/// <summary>
///		Sets all properties of one object to match those of another. Properties that exist only in
///		the object being modified are not changed.
/// </summary>
/// <remarks>
///		The copy is of the "deep" format -- the objects will not end up sharing references.
/// </remarks>
/// <param name="to">
///		The object whose properties should be set
/// </param>
/// <param name="from">
///		The object whose properties should be duplicated
/// </param>
/// <returns>
///		The object that has been modified, as a convenience
/// </returns>
function PullProps( to, from )
{
	for ( var p in from )
	{
		if ( typeof(from[p]) != "object" )
			to[p] = from[p];
		else
		{
			if ( typeof(to[p]) != "object" )
				to[p] = {};
			PullProps( to[p], from[p] );
		}
	}
	return to;
}

/// <summary>
///     registers the specified load event with the window; using this method ensures
///     that new events won't steamroll events added by other controls (assuming they have
///     unique names)
/// </summary>
/// <param name="myLoad">
///     name of load event (cannot take parameters -- if they are necessary, the load event
///     should reference objects initialized seperately by the control)
/// </param>

/*First, some initialization code, so we know when things are already loaded and we should just call passed functions immediately*/
var Loaded = false;
//because IE (surprise!) has its own, non-standard version…
if ( window.attachEvent )
{
	window.attachEvent( "onload", function(){Loaded=true;} );
}
// legacy (Firefox?)
else if ( window.addEventListener )
{
	window.addEventListener( 'load', function(){Loaded=true;}, false );
}
//This next way is for browsers which aren’t IE
else if ( document.addEventListener )
	document.addEventListener( 'load', function(){Loaded=true;}, false );

function AddLoad( myLoad ) {

	if ( Loaded )
	{
		return myLoad();
	}

    //because IE (surprise!) has its own, non-standard version…
    if ( window.attachEvent )
	{
		window.attachEvent( "onload", myLoad );
	}
	// legacy (Firefox?)
	else if ( window.addEventListener )
	{
		window.addEventListener( 'load', myLoad, false );
	}
    //This next way is for browsers which aren’t IE
    else if ( document.addEventListener )
		document.addEventListener( 'load', myLoad, false );

}

///	<summary>
///		Removes an element from the DOM tree and returns it
///	</summary>
function removeElement( htmlObj )
{
	return htmlObj.parentNode.removeChild( htmlObj );
}

/// <summary>
///		Registers an event
/// </summary>
/// <param name="obj">
///		The object to attach the event to. HTML object, not I.D. For example, the return value of document.getElementById()
/// </param>
/// <param name="evt">
///		Name of the event, minus "on". For example, "click"
/// </param>
/// <param name="func">
///		Function reference to register for the event. It will get an event object as an argument
/// </param>
/// <param name="eatEvent">
///		If true, all other results of this event will be suppressed.
/// </param>
/// <returns>
///		An event descriptor to pass to RemoveEvent to undo this registration
/// </returns>
function AddEvent( obj, evt, func, eatEvent )
{
	//Wrap the function up to wrap details of getting events. Could be wrapped separately for each branch,
	//below, but this is simpler.
	var wrappedFunc;
	if ( eatEvent )
		wrappedFunc = function(e) { e = e || window.event; var r = func( e ); EatEvent( e ); return r; };
	else
		wrappedFunc = function(e) { return func( e || window.event ); };
	
    //because IE (surprise!) has its own, non-standard version…
    if ( obj.attachEvent )
	{
		obj.attachEvent( "on" + evt, wrappedFunc );
	}
    //This next way is for browsers which aren’t IE
    else if ( obj.addEventListener )
		obj.addEventListener( evt, wrappedFunc, false );
		
	return [ obj, evt, wrappedFunc ];
}

/// <summary>
///		Suppress results of an event
/// </summary>
function EatEvent( e )
{
	//One solution for IE...
	e.cancelBubble = true;
	e.returnValue = false;

	//...and one for everyone else
	if ( e.preventDefault )
	{
		e.preventDefault();
	}
	if ( e.stopPropagation )
	{
		e.stopPropagation();
	}

}

/// <summary>
///		Unregisters an event registered by AddEvent
/// </summary>
/// <param name="evtDescriptor">
///		An object returned by AddEvent
/// </param>
function RemoveEvent( evtDescriptor )
{
	var obj = evtDescriptor[0];
	var evt = evtDescriptor[1];
	var func = evtDescriptor[2];
	
	//One branch for IE
	if ( obj.detachEvent )
	{
		obj.detachEvent( "on" + evt, func );
	}
	//And one for everyone else
	else if ( obj.removeEventListener )
	{
		obj.removeEventListener( evt, func, false );
	}
}

/// <summary>
///		Makes an html object draggable by the mouse.
///		Object MUST have "position: absolute" as well as valid left and top styles
/// </summary>
/// <returns>
///		A method to call when the object's visibility switches. This method returns a new method to call next time the visibility switches
/// </returns>
function MakeDraggable( htmlObj )
{
	//First define some closures we're going to use.
	var setGeometry = function()
	{
		if (htmlObj.backingIFrame != null)
		{
			htmlObj.backingIFrame.style.width = htmlObj.offsetWidth + "px";
			htmlObj.backingIFrame.style.height = htmlObj.offsetHeight + "px";
			htmlObj.backingIFrame.style.top = htmlObj.style.top;
			htmlObj.backingIFrame.style.left = htmlObj.style.left;
		}
	}
	
	var BeginDrag = function(e, htmlObj, backingIFrame)
	{
		setGeometry();
		var baseX = e.clientX - parseInt(htmlObj.style.left);
		var baseY = e.clientY - parseInt(htmlObj.style.top);
		
		var DragEvent = AddEvent( document, 'mousemove', 
			function(e)
			{				
				htmlObj.style.left = e.clientX - baseX + "px";
				htmlObj.style.top = e.clientY - baseY + "px";
				if ( backingIFrame != null )
				{
					backingIFrame.style.left = e.clientX - baseX + "px";
					backingIFrame.style.top = e.clientY - baseY + "px";
				}
			}, 
			true );
		var EndDragEvent = AddEvent( document, 'mouseup', 
			function(e)
			{
				RemoveEvent( DragEvent );
				RemoveEvent( EndDragEvent );
			} );
	}
	
	if ( htmlObj.attachEvent )
	{
		if ( htmlObj.backingIFrame != null && htmlObj.backingIFrame.parentNode != null )
		{
			htmlObj.backingIFrame.parentNode.removeChild( htmlObj.backingIFrame );
		}
		htmlObj.backingIFrame = document.createElement("iframe");
		htmlObj.backingIFrame.style.position = "absolute";
		htmlObj.backingIFrame.style.zIndex = 999;
		htmlObj.backingIFrame.src = "about:blank";
		setGeometry();
		htmlObj.style.zIndex = htmlObj.style.zIndex > 1000 ? htmlObj.style.zIndex : 2000;
		htmlObj.parentNode.appendChild( htmlObj.backingIFrame );		
	}

	AddEvent( htmlObj, 'mousedown', function(e) { BeginDrag(e, htmlObj, htmlObj.backingIFrame); }, true );
	
	var onShow;
	var onHide;
	if ( htmlObj.backingIFrame != null )
	{
		onHide = function() 
		{ 
			if (htmlObj.style.display == 'none')
			{
				removeElement( htmlObj.backingIFrame ); 
				return onShow;
			}
			setGeometry();
			return onHide;
		};
		
		onShow = function() 
		{
			setGeometry();
			document.body.appendChild( htmlObj.backingIFrame ); 			
			return onHide; 
		};
	} 
	else 
	{
		onHide = function() { return onShow; };
		onShow = function() { return onHide; };
	}
	return onHide;
}

/// <summary>
///		Makes an html object show over selects in IE6
/// </summary>
function IFrameBackForIE6(htmlObj) {
	var backingIFrame = document.getElementById('backingiframe');

	if (backingIFrame == null) {
		backingIFrame = document.createElement("iframe");
		document.body.appendChild(backingIFrame);
	}

	backingIFrame.style.position = "absolute";
	backingIFrame.style.zIndex = 1;
	backingIFrame.id = "backingiframe";
	backingIFrame.src = "about:blank";
	backingIFrame.style.width = htmlObj.offsetWidth + "px";
	backingIFrame.style.height = htmlObj.offsetHeight + "px";
	backingIFrame.style.top = htmlObj.offsetTop + "px";
	backingIFrame.style.left = htmlObj.offsetLeft + "px";
	backingIFrame.style.display = "block";
	htmlObj.style.zIndex = htmlObj.style.zIndex > 1 ? htmlObj.style.zIndex : 2;

	return backingIFrame;
}


/// <summary>
///		Is this window a popup?
/// </summary>
function IsPopup()
{
	try
	{
		return ( window.opener != null && window.opener.IsWebMD );
	} catch ( e ) {
		return false; //Why? Security can cause the above to throw.
	}
}

/// <summary>
///		Draws a box around an area.
/// </summary>
function HilightArea( rect )
{
	document.body.appendChild( HTML( "div", { "style": { "position":"absolute", "top":(rect.top + 'px'), "left":(rect.left + 'px'), "width":(rect.right - rect.left + 'px'), "height":(rect.bottom - rect.top + 'px'), "border":"1px solid black" } } ) );	
}

function openWindowUseMotionPointUrl(ispopup, id, append) {
    if (ispopup) {
        RemoteStart(document.getElementById(id).href + append);
    }
    else {
        window.open(document.getElementById(id).href + append);
    }
}   

/// <summary>
///		Updates or adds, as appropriate, a key/value to the query string.
///		Obviously, this causes a server trip
/// </summary>
function UpdateQueryStringValue( key, value )
{
	window.location.href = window.location.href.toString().replace(new RegExp("&"+key+"=.*?(&|$)"),'') + '&' + key + "=" + value;
}

/// <summary>
///		Use onkeydown="EnterClick(event)" to make the enter button work like clicking something
/// </summary>
function EnterClick( event )
{
	if ( event.keyCode == 13 )
		if ( document.createEventObject )
		{
			// IE
			return event.srcElement.fireEvent("onclick",document.createEventObject())
		}
		else
		{
			// Not IE
			var evt = document.createEvent("HTMLEvents");
			evt.initEvent("click", true, true );
			return !event.target.dispatchEvent(evt);
		}
}