function SavePagePath()
{
	try
	{
		if (window.parent.frames.length > 0)
		{
			if (window.parent.frames("LeftTree") != null)
				window.parent.frames("LeftTree").document.all.MainPageHistoryPath.value = location.href;
		}
	}
	catch(e)
	{
	}
	
	return false;
}

function GetHistoryPath()
{
	if (location.href.search(/\/imageviewer\/(search|searchwf).aspx/i) > 0 )
	{
		return "Cancel" ;
	}
	else
	{
		return window.parent.frames("LeftTree").document.all.MainPageHistoryPath.value ;
	}
}

function ViewImages(strSUID, strModality, strSeriesUID)
{	
	if ( (strSUID == null) || (strSUID == '') )
	{
		alert('No images available for this study');
		return;
	}
	
	if (location.pathname.toLowerCase().search(/\/ViewerPlugin\/viewerPlugin.aspx/i) > -1)
		ShowImage(strSUID, strModality, strSeriesUID) ;
	else if (strSeriesUID == null)
	{
		location.href = "/fusionserver/ViewerPlugin/ViewerPlugin.aspx?Internal=true&Modality=" + strModality + "&SUID=" + strSUID ;
	}
	else
		location.href = "/fusionserver/ViewerPlugin/ViewerPlugin.aspx?Internal=true&Modality=" + strModality + "&SUID=" + strSUID + "&SeriesUID=" + strSeriesUID ;
	
	if (strSeriesUID == null)
		top.frames["LeftTree"].UpdateMenuItem("Study", strSUID) ;
	else
		top.frames["LeftTree"].UpdateMenuItem("Series", strSeriesUID) ;
}

function ViewKIImages(strSUID, strModality, strSeriesUID)
{	
	if ( (strSUID == null) || (strSUID == '') )
	{
		alert('No key images available for this study');
		return;
	}
	
	if (location.pathname.toLowerCase().search(/\/ViewerPlugin\/viewerPlugin.aspx/i) > -1)
		ShowImage(strSUID, strModality, strSeriesUID) ;
	else if (strSeriesUID == null)
	{
		location.href = "/fusionserver/ViewerPlugin/ViewerPlugin.aspx?Internal=true&Modality=" + strModality + "&SUID=" + strSUID ;
	}
	else
		location.href = "/fusionserver/ViewerPlugin/ViewerPlugin.aspx?Internal=true&Modality=" + strModality + "&SUID=" + strSUID + "&SeriesUID=" + strSeriesUID ;
	
	if (strSeriesUID == null)
		top.frames["LeftTree"].UpdateMenuItem("Study", strSUID) ;
	else
		top.frames["LeftTree"].UpdateMenuItem("KISeries", strSeriesUID) ;
}

//Displays the Report that matches the criteria, or displays a Not Found message.
function ViewReport(strPatientID, strAccessionNumber, strStudyUID, strModality)
{	
	if ( ( (strPatientID == null) || (strPatientID == '')) &&
		 ( (strAccessionNumber == null) || (strAccessionNumber == '')) &&
		 ( (strStudyUID == null) || (strStudyUID == '')) &&
		 ( (strModality == null) || (strModality == '')) )
	{
		alert("Insufficent data available to display distinct report.");
		return;
	}
	
	// location.href = "/fusionserver/applications/imageviewer/Report.aspx?anumb="+strAccessionNumber + "&pid=" + strPatientID + "&suid=" + strStudyUID + "&modality=" + strModality;
	var strNewLocation = "/fusionserver/applications/imageviewer/Report.aspx?";
	var strPrepend = "";
	
	if ((strAccessionNumber != null) && (strAccessionNumber != ''))
	{
		strNewLocation = strNewLocation + strPrepend + "anumb=" + strAccessionNumber;
		strPrepend = "&";
	}
	
	if ((strPatientID != null) && (strPatientID != ''))
	{
		strNewLocation = strNewLocation + strPrepend + "pid=" + strPatientID;
		strPrepend = "&";
	}
	
	if ((strStudyUID != null) && (strStudyUID != ''))
	{
		strNewLocation = strNewLocation + strPrepend + "suid=" + strStudyUID;
		strPrepend = "&";
	}
	
	if ((strModality != null) && (strModality != ''))
	{
		strNewLocation = strNewLocation + strPrepend + "modality=" + strModality;
		strPrepend = "&";
	}
	
	location.href = strNewLocation;
}

function SearchBySavedQuery(QueryID)
{

	//TODO: replace the hardcoded url with a variable
	
	// In the 'unnamed' case, just reload the page to reset it.
	if (QueryID == "")
	{
		window.parent.location.reload();
		return;
	}
	
	//Update the selected item
	var oTreeMenuFrame = window.parent.frames("LeftTree");	
	if(oTreeMenuFrame != null)		
	{
		var strRoot = "View";
		var oMenu = oTreeMenuFrame.CTreeMenu.Menus[strRoot].RootNode.GetMenuByID("SavedQuery_" + QueryID);
		if(oMenu != null)
		{
			for(i = 0 ; i < oMenu.Parent.ChildMenus.length ; i++)
			{
				oMenu.Parent.ChildMenus[i].Deselect();
			}
			oMenu.Select();
		}
	}
	
	location.href = GetCookie("searchPage") + "?SavedQueryID=" + QueryID ;
	return;
	/*
	if (location.pathname.toLowerCase().search(/\/imageviewer\/search.aspx/i) > -1)
	{
		ExecuteSearchBySavedQuery(QueryID)
	}
	else
	{
		location.href = "/fusionserver/applications/imageviewer/search.aspx?SavedQueryID=" + QueryID ;
	}
	*/
}

function BackToSearchPage()
{
	var strRef = GetHistoryPath() ; 
	if(strRef == "Cancel") 
	{
//		alert("cancel")
	} 
	else
	{
		if (strRef.search(/\/fusionserver\/applications\/ImageViewer\/(search|searchwf).aspx/i)>0) 
		{
			history.back() ;
		} 
		else 
		{ 
			location.href='/fusionserver/applications/ImageViewer/search.aspx' ;
		}
	}
}

function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}
