<!--#include file="REGlobalConstants.inc"-->
<!--#include file="Common.inc"-->

<%
	
	Const GUID_LENGTH = 38

	'-- String Constants
	Const STATUS_ICON_PATH = "Status"
	Const PREVIEW_IMG = "<img src=Toolbar/preview.gif BORDER=0>"
	Const NO_RIGHTS_STRING = "No Access Rights."
	Const NEW_PAGE_STRING = "No Access to Page."
	Const STR_INTERNAL_ERROR = "<INTERNAL ERROR>"
	Const TABLE_OPTIONS = "border=0 cellspacing=1 cellpadding=5 width=100%"
	Const DTABLE_OPTIONS = "BORDER=0 CELLSPACING=0 CELLPADDING=2"
	Const DTABLE_SPACING="&nbsp;&nbsp"
	Const HEADING_COLOUR = """#FFFFFF"""
	Const STATUS_COLOUR = """#FFFFFF"""
	Const LISTING_COLOUR = """#EEEEEE"""



	'-- Global Object Variables
	Dim g_pAESession
	Dim g_pAEServer
	Dim g_pRERightsChk
	
	'-------------------------------------------------
	' Helper subroutines
	'-------------------------------------------------
	
	'-- Returns the node's path within the container hierarchy
	Function GetPath(strNodeGuid)
		Dim pAECurNode
		Dim strPath
		
		strPath = ""
		
		Set pAECurNode = g_pAEServer.GetNodeByGuid(strNodeGuid, false)
		
		'-- Recursively go through the container hierarchy until we reach
		'-- the server node
		If not (pAECurNode.Type = AE_NODE_SERVER) Then
			While not(pAECurNode.Type = AE_NODE_SERVER)
				strPath= "/" + pAECurNode.Name + strPath
				Set pAECurNode= g_pAEServer.GetNodeByGuid(pAECurnode.Parent, false)
			Wend
		Else
			strPath = "/"
		End If
		
		GetPath=strPath	
	End Function
	
	
	'-- Returns the preview tag to be used based on the specifed node guid
	'-- version and layout (If it is a page)
	Function PreviewTagFor(strNodeGuid, bCheckedOutVersion, strLayoutName, eStatus)
		Dim pAENode
		Dim strPreviewTag
	
		set pAENode=GetNode(g_pAEServer, strNodeGuid, bCheckedOutVersion)
				
		'-- Only return a preview tag If user has rights to node
		If Not (pAENode is Nothing) Then
			Dim strUrl
			
			'-- If pAENode is Nothing,Then assume pAENode is a new node
			If (eStatus = RE_NEW) Then
				strPreviewTag=NEW_PAGE_STRING
			Else
				'-- Get the preview URL depending on the node type
				Select Case pAENode.Type
					Case AE_NODE_PAGE
						'-- Get the URL from the page layout with specified name
						Dim pAELayoutCol
						Dim pAELayout
												
						'-- get the default channel If exist						
						Dim pChannel
						Set pChannel = GetDefaultChannel(pAENode.Parent)
												
						'-- get the layout based on the template name.
						set pAELayoutCol=pAENode.layouts
						set pAELayout=pAELayoutCol.GetLayoutByName(strLayoutName)
												
						'-- get the URL by using the default channel If exist.
						strUrl=pAELayout.UrlPreviewInChannel(pChannel)
					Case AE_NODE_TEMPLATE
						'-- Get the URL directly from template
						strUrl=pAENode.AspTemplatePreviewUrl
						strLayoutName=""
					Case Else
						strUrl="Can't do it"
				End Select
					
				'-- Construct the preview tag
				strPreviewTag= "<a href=" + strUrl  + " TARGET='_blank'>" + PREVIEW_IMG + "&nbsp;&nbsp;Preview" + "</a> "
				
				If eStatus = RE_SAVED or eStatus = RE_DECLINED or eStatus = RE_WAITING Then
					If HasCheckedOutVersion(pAENode) And HasCheckedInVersion(pAENode) Then			
						strPreviewTag = strPreviewTag + " " + StatusFrom(bCheckedOutVersion)
					End If
				End If
			End If
		Else
			strPreviewTag = NO_RIGHTS_STRING
		End If
		
		PreviewTagFor = "<nobr>" + strPreviewTag + "</nobr>"
		
	End Function
	
	
	' -- This returns the preview tag for a page with the checked out version
	'		of a template
	Function RevisedPreviewTagFor(strNodeGuid, bCheckedOutVersion, strLayoutName, eStatus)
		Dim pAEPage
		Dim strPreviewTag
	
		set pAEPage=GetNode(g_pAEServer, strNodeGuid, bCheckedOutVersion)
		
		If Not (pAEPage is Nothing) Then
			
			Dim pAELayoutCol
			Dim pAELayout
			Dim pAELayoutTemplate
			
			If (eStatus = RE_NEW) Then
				strPreviewTag = NEW_PAGE_STRING
			Else		
				Set pAELayoutCol = pAEPage.layouts
				Set pAELayout = pAELayoutCol.GetLayoutByName(strLayoutName)
				Set pAELayoutTemplate = g_pAEServer.GetNodeByGuid(pAELayout.Template, True)
				strPreviewTag= "<a href='" + pAELayoutTemplate.AspRuntimeUrl(pAEPage, pAELayout) + "' TARGET='_blank'>" + PREVIEW_IMG + "Preview" + "</a> "
		
				If (eStatus = RE_SAVED) Or (eState = RE_DECLINED) Or (eState = RE_WAITING) Then
					If HasCheckedOutVersion(pAEPage) And HasCheckedInVersion(pAEPage) Then			
						strPreviewTag = strPreviewTag + " " + StatusFrom(bCheckedOutVersion)
					End If
				End If
			End If		
		Else
			strPreviewTag = NO_RIGHTS_STRING
		End If
		
		RevisedPreviewTagFor = "<nobr>" + strPreviewTag + "</nobr>"
	End Function
	
	
	'-- Returns whether the items represented by the specified guid strings
	'-- are the same node.
	Function AreSameNode(strNodeGuid1, strNodeGuid2)
		AreSameNode=(strNodeGuid1 = strNodeGuid2)
	End Function
	
	'-- Returns the status string based on the checked out status of a node
	Function StatusFrom(bIsCheckedOutVersion)
		If bIsCheckedOutVersion Then
			StatusFrom = "(Revised)"
		Else
			StatusFrom = "(Approved)"
		End If
	End Function
	
	
	'-- Function for creating table html
	
	Function TableHeaderFieldWith(strOptions, strContents)
		TableHeaderFieldWith = "<TD CLASS='WBC_styclsHeavyTextColor1' align='center' " + strOptions + "><nobr>" + strContents + "</nobr></TD>"
	End Function
		
	Function TableFieldWith( strContents, strAlign )
		Dim strLocalContents
		If len(strContents)>0 Then
			strLocalContents = strContents
		Else
			strLocalContents = "&nbsp;"
		End If
		
		TableFieldWith = "<TD class='WBC_styclsTextColor0' align='" + strAlign + "'>" + strLocalContents + "</TD>"
	End Function
	
	Function DTableHeadingFieldWith(strContents)
		DTableHeadingFieldWith = "<TD CLASS=""headcell"" BGCOLOR=" + HEADING_COLOUR + "ALIGN=""RIGHT"">" + strContents + DTABLE_SPACING + "</TD> "
	End Function

	Function DTableStatusFieldWith(strContents)
		DTableStatusFieldWith = "<TD CLASS=""headcell"" BGCOLOR=" + STATUS_COLOUR + ">" + strContents + DTABLE_SPACING + "</TD> "
	End Function

	Function DTableFieldWith( strContents, strAlign)
		DTableFieldWith = "<TD class='WBC_styclsAlternateRowBg1' align='" + strAlign + "'>" + strContents + DTABLE_SPACING + "</TD> "
	End Function
	
	Function DTableStatusFieldValueWith(pAENode, strContents)
		Dim strLocalContents
		
		strLocalContents = "<TD class='WBC_styclsAlternateRowBg1'>" + strContents
		
		If HasCheckedOutVersion(pAENode) And HasCheckedInVersion(pAENode) And HasBeenCommitted(pAENode) Then  
			strLocalContents = strLocalContents + ", Previous Version Exists" + "</TD> "
		Else
			strLocalContents = strLocalContents + DTABLE_SPACING + "</TD> "
		End If
		
		DTableStatusFieldValueWith = strLocalContents
	End Function

	'-- Get the start tag and options for the table
	Function GetTableBegin()
		Dim strTable

		strTable = "<TABLE " + TABLE_OPTIONS + " >" + vbcrlf

		GetTableBegin = strTable
	End Function

	'-- Get the End table tag
	Function GetTableEnd()
		Dim strTable

		strTable = strTable + "</TABLE>" + vbCrLf

		GetTableEnd = strTable
	End Function

	'-- Get the header row for the result table
	Function GetResultTableHeader(bWriteStatus, bWriteOwner, bWritePreview, bWriteRevisedPreview)
		Dim strTable

		'--- Setup table header
		Dim strHeaderCellOptions
		Dim strApprovedPreviewTitle
		Dim strRevisedPreviewTitle
		
		'--- When these options are both specified, their colunms will be
		'--- listed underneath a commom "Preview" colunm
		If bWritePreview and bWriteRevisedPreview Then
			strHeaderCellOptions="ROWSPAN=2"
			strApprovedPreviewTitle="Using Approved Template"
			strRevisedPreviewTitle="Using Revised Template"
		Else
			strHeaderCellOptions=""
			strApprovedPreviewTitle="Preview"
			strRevisedPreviewTitle="Revised Preview"
		End If
		
		strTable = strTable + "<TR class='WBC_styclsBackground2' valign='bottom'>" + vbcrlf
		
		If bWriteStatus Then
			strTable = strTable + TableHeaderFieldWith(strHeaderCellOptions, "Status") + vbcrlf
			strTable = strTable + TableHeaderFieldWith(strHeaderCellOptions, "Is Replacement") + vbcrlf
		End If
		
		'-- Put a common "Preview" Column If both types of preview were specified
		If bWritePreview and bWriteRevisedPreview Then
			strTable = strTable + TableHeaderFieldWith("COLSPAN=2", "Preview") + vbcrlf
		Else
			If bWritePreview Then
				strTable = strTable + TableHeaderFieldWith("ROWSPAN=1", strApprovedPreviewTitle) + vbcrlf
			End If

			If bWriteRevisedPreview Then
				strTable = strTable + TableHeaderFieldWith("ROWSPAN=1", strRevisedPreviewTitle) + vbcrlf
			End If
		End If
		
		'-- Write the Name and location columns (common to all)
		strTable = strTable + TableHeaderFieldWith(strHeaderCellOptions, "Name") + vbcrlf
		strTable = strTable + TableHeaderFieldWith(strHeaderCellOptions, "Location") + vbcrlf
	
		If bWriteOwner Then
			strTable = strTable + TableHeaderFieldWith(strHeaderCellOptions, "Owner") + vbcrlf
		End If
		
		'-- Put a second row If doing both previews
		If bWritePreview and bWriteRevisedPreview Then
			'-- Finish off row and begin next one
			strTable = strTable + "</TR>" + vbcrlf
			strTable = strTable + "<TR>" + vbcrlf

			strTable = strTable + TableHeaderFieldWith("ROWSPAN=1", strApprovedPreviewTitle) + vbcrlf
			strTable = strTable + TableHeaderFieldWith("ROWSPAN=1", strRevisedPreviewTitle) + vbcrlf
		End If
		
		strTable = strTable + "</TR>" + vbcrlf
		GetResultTableHeader = strTable
	End Function

	'-- Get the header row for the Resource Report result table
	Function GetResourcesResultTableHeader(bWriteStatus, bWriteLocation, bWriteOwner, bWriteMimeType, bWritePreview, bWritePlaceHolderType, bWriteSrcVersion)
		Dim strTable

		strTable = "<TR class='WBC_styclsBackground2' valign=""bottom"">" + vbcrlf
		
		If bWriteStatus Then
			strTable = strTable + TableHeaderFieldWith("", "Status") + vbcrlf
			strTable = strTable + TableHeaderFieldWith("", "Is Replacement") + vbcrlf
		End If
		
		strTable = strTable + TableHeaderFieldWith("", "Name") + vbcrlf
		
		If bWriteLocation Then
			strTable = strTable + TableHeaderFieldWith("", "Location") + vbcrlf
		End If
	
		If bWriteOwner Then
			strTable = strTable + TableHeaderFieldWith("", "Owner" ) + vbcrlf
		End If		
		
		If bWriteMimeType Then
			strTable = strTable +  TableHeaderFieldWith("", "Mime") + vbcrlf
		End If
		
		If bWritePreview Then
			strTable = strTable + TableHeaderFieldWith("", "Preview") + vbcrlf
		End If
		
		If bWritePlaceHolderType Then
			strTable = strTable + TableHeaderFieldWith("", "Type") + vbcrlf
		End If
		
		If bWriteSrcVersion Then
			strTable = strTable + + TableHeaderFieldWith("", "In Version") + vbcrlf
		End If
	
		strTable = strTable + "</TR>" + vbcrlf
	
		GetResourcesResultTableHeader = strTable
	End Function
	
	' -- Returns a results table based on a ServerSearchResult.  The boolean
	'	 flags specify which columns to write
	Function GetResultTable(pAEServerSearchResult, bWriteStatus, bWriteOwner, bWritePreview, bWriteRevisedPreview)

		'-- This function expects that the search result is sorted by Guid, LayoutName,
		'-- and version (Checked-out first, Then Checked in).  It also expects the
		'-- Item (GUID) field to contain either just a ndoeguid, 
		Dim strTable
		
		'--- Add table rows
		Dim i
		Dim lEnd 
		lEnd = pAEServerSearchResult.Count-1
		
		For i = 0 To lEnd
			
			If bWriteStatus or bWritePreview or bWriteRevisedPreview Then
				Dim eStatus
				eStatus = StatusFor(pAEServerSearchResult.Item(i), pAEServerSearchResult.CheckedOut(i))
			End If

			'-- Write the Status icon, name and path of node item
			strTable = strTable + "<TR valign=TOP class='WBC_styclsAlternateRowBg1'>" + vbcrlf  

			If bWriteStatus Then
				strTable = strTable + TableFieldWith( StatusImageTagFor(eStatus) + "&nbsp;&nbsp;" + StatusNameTagFor(eStatus), "left" ) + vbcrlf
				strTable = strTable + TableFieldWith( GetPreviousVersionString(pAEServerSearchResult.Item(i)), "center" ) + vbcrlf
			End If

			dim j
			Dim strPreview

			If bWritePreview Then

				strPreview = PreviewTagFor(pAEServerSearchResult.Item(i), pAEServerSearchResult.CheckedOut(i), pAEServerSearchResult.Hint(i), eStatus)				
				
				If eStatus<>RE_NO_RIGHTS Then
					' -- If the next items have same name, they must be added to this
					'	 column under the same row with either "Revised" or "Approved"
					j = i
					Do

						If j+1<=lEnd Then
							'-- The next index is in the valid range

							If AreSameNode(pAEServerSearchResult.Item(j), pAEServerSearchResult.Item(j+1)) Then
								'-- The current item has same guid as next item
								'-- Write preview tag for next item
								strPreview = strPreview + "<BR>"
								
								strPreview = strPreview + PreviewTagFor(pAEServerSearchResult.Item(j+1), pAEServerSearchResult.CheckedOut(j+1), pAEServerSearchResult.Hint(j+1), eStatus)	
								
								j=j+1
							Else
								'-- We are done
								exit do
							End If
						Else
							'-- We are done
							exit do
						End If
					loop

				End If

				strTable = strTable + TableFieldWith(strPreview, "left") + vbcrlf
			End If

			If bWriteRevisedPreview Then

				strPreview = RevisedPreviewTagFor(pAEServerSearchResult.Item(i), pAEServerSearchResult.CheckedOut(i), pAEServerSearchResult.Hint(i), eStatus)

				If eStatus<>RE_NO_RIGHTS Then
					' -- If the next items have same name, they must be added to this
					'	 column under the same row with either "Revised" or "Approved"
					j = i
					Do

						If j+1<=lEnd Then
							' -- The next index is in the valid range

							If AreSameNode(pAEServerSearchResult.Item(j), pAEServerSearchResult.Item(j+1)) Then
								' -- The current item has same guid as next item
								'		Write preview tag for next item
								strPreview = strPreview + "<BR>"
								strPreview = strPreview + RevisedPreviewTagFor(pAEServerSearchResult.Item(j+1), pAEServerSearchResult.CheckedOut(j+1), pAEServerSearchResult.Hint(j+1), eStatus)	
								j=j+1
							Else
								' -- We are done
								exit do
							End If
						Else
							'-- We are done
							exit do
						End If
					loop

				End If

				strTable = strTable + TableFieldWith(strPreview, "left") + vbcrlf		
			End If

			strTable = strTable + TableFieldWith( pAEServerSearchResult.Name(i), "left" ) + vbcrlf
			strTable = strTable + TableFieldWith( GetPath( pAEServerSearchResult.ParentGUID(i) ), "left" ) + vbcrlf

			If bWriteOwner Then
				strTable = strTable + TableFieldWith( ShortUsername(pAEServerSearchResult.Owner(i)), "left" ) + vbcrlf
			End If

			'-- Finish off table row
			strTable = strTable + "</TR>"


			Do ' Skip over duplicates
				If i+1<=lEnd Then
					If AreSameNode(pAEServerSearchResult.Item(i), pAEServerSearchResult.Item(i+1)) Then
						i=i+1
					Else
						exit do
					End If
				Else
					exit do
				End If
			loop
		Next
		
		GetResultTable=strTable
	End Function
	
	
	'-- Returns a results table based on a ResourcesReportCollection.  The boolean
	'-- flags specify which columns to write
	Function GetResourcesReportTable(pREResourcesReportCol, bWriteStatus, bWriteLocation, bWriteOwner, bWriteMimeType, bWritePreview, bWritePlaceholderType, bWriteSrcVersion)
		Dim strTable
		Dim strName
		DIm strPreviewUrl
		
		'--- Add table rows
		Dim i
		Dim pREResourcesReportItem
		For i=0 to pREResourcesReportCol.Count - 1

			set pREResourcesReportItem=pREResourcesReportCol.Item(i)
				
			strTable = strTable + "<TR VALIGN='TOP' class='WBC_styclsAlternateRowBg1'>" + vbcrlf 
			
			'-- Get the status icon
			If bWriteStatus Then
				Dim eStatus
				
				Select Case pREResourcesReportItem.ObjectStatus
					Case "Saved"
						eStatus = RE_SAVED
					Case "Approved"
						eStatus = RE_APPROVED
					Case Else
						eStatus = RE_UNKNOWN
				End Select

				Dim strTestURL

				strTestURL=pREResourcesReportItem.PreviewUrl

				If len(strTestURL)= 0 Then
					strTable = strTable + TableFieldWith( NO_RIGHTS_STRING, "left") + vbcrlf
				Else
					strTable = strTable + TableFieldWith( StatusImageTagFor(eStatus)  + " " + StatusNameTagFor(eStatus) , "left") + vbcrlf
				End If
				strTable = strTable + TableFieldWith( pREResourcesReportItem.PreviousVersion, "center") + vbcrlf
			End If
			
			 
			
			strName=pREResourcesReportItem.Name
			If strName = STR_INTERNAL_ERROR Then
				strName= "Failed to retrieve resource name."
			End If
			strTable = strTable + TableFieldWith(strName, "left") + vbcrlf
			
			If bWriteLocation Then
				strTable = strTable + TableFieldWith(pREResourcesReportItem.Location, "left") + vbcrlf
			End If
			
			If bWriteOwner Then
				strTable = strTable + TableFieldWith(ShortUserName(pREResourcesReportItem.Owner), "left") + vbcrlf
			End If
				
			If bWriteMimeType Then
				strTable = strTable + TableFieldWith(pREResourcesReportItem.MimeType, "center") + vbcrlf
			End If	
			
			If bWritePreview Then
					Dim strPreviewContents
	
					strPreviewUrl=pREResourcesReportItem.PreviewUrl
					
					If len(strPreviewUrl)= 0 Then
						'Url is empty meaning no rights
						strPreviewContents = NO_RIGHTS_STRING
					elseif strPreviewUrl = STR_INTERNAL_ERROR Then
						'There was an internal error getting url
						strPreviewContents = "Failed to retrieve resource URL."
					Else
						'We have a valid url.  Write it.
						If InStr(LCase(Left(strPreviewUrl, 4)), "http") > 0 Then
							strPreviewContents = "<a href=" + pREResourcesReportItem.PreviewUrl + " TARGET=""_blank""> "
						Else
							strPreviewContents = "<a href=" + g_pAEServer.ResolveUrl( strPreviewUrl ) + " TARGET=""_blank""> "
						End If
						strPreviewContents = strPreviewContents + PREVIEW_IMG + "Preview"
						strPreviewContents = strPreviewContents + "</a>"
					End If
					
					strTable = strTable + TableFieldWith(strPreviewContents, "left") + vbcrlf
			End If
			
			If bWritePlaceholderType Then
				strTable = strTable + TableFieldWith(pREResourcesReportItem.PlaceholderType, "center") + vbcrlf
			End If	
			
			If bWriteSrcVersion Then
				strTable = strTable + TableFieldWith(pREResourcesReportItem.VersionFoundIn, "center") + vbcrlf
			End If	
			
			strTable = strTable + "</TR>" + vbcrlf
			
		Next

		GetResourcesReportTable=strTable
	End Function
	
	
	'-- This function generates the description table for the passed in node
	'-- This function generates the description table for the passed in node
	Function GetDescriptionTable(pAENode)
		Dim strTable
		Dim bWriteStatus

		strTable=""

		strTable = strTable + "<TABLE " + DTABLE_OPTIONS + ">" + vbCrLf

		'-- Page status
		Dim eStatus
		If (pAENode.Type <> AE_NODE_RESOURCE_GALLERY_ITEM) Then
			strTable = strTable +  "<TR>" + vbCrLf
			eStatus=StatusOfNodeObject(pAENode)
			strTable = strTable + DTableHeadingFieldWith("Status:") + vbCrLf
			strTable = strTable + DTableStatusFieldWith( StatusImageTagFor(eStatus) ) + vbCrLf
			strTable = strTable + DTableStatusFieldValueWith( pAENode, StatusNameTagFor(eStatus) ) + vbCrLf
			strTable = strTable +  "</TR>" + vbCrLf
		End If

		'-- Location
		strTable = strTable +  "<TR>" + vbCrLf
		strTable = strTable + DTableHeadingFieldWith("Location:") + vbCrLf
		strTable = strTable + DTableStatusFieldWith( "&nbsp" ) + vbCrLf
		strTable = strTable + DTableFieldWith( GetPath(pAENode.Parent), "left" ) + vbCrLf
		strTable = strTable +  "</TR>" + vbCrLf

		'-- Owner
		strTable = strTable +  "<TR>" + vbCrLf
		strTable = strTable + DTableHeadingFieldWith("Owner:") + vbCrLf
		strTable = strTable + DTableStatusFieldWith( "&nbsp" ) + vbCrLf
		strTable = strTable + DTableFieldWith( ShortUsername(pAENode.Owner), "left" ) + vbCrLf
		strTable = strTable +  "</TR>" + vbCrLf

		strTable = strTable + "</TABLE>" + vbCrLf

		GetDescriptionTable = strTable

	End Function
	
	
	Function GetPreviousVersionString(strNodeGuid)
		
		Dim strPreviousVersionExist
		
		Dim pAENode
		
		Set pAENode = GetNode(g_pAEServer, strNodeGuid, True)
		If pAENode is nothing Then
			strPreviousVersionExist = ""
		Else
			If HasCheckedInVersion(pAENode) And HasCheckedOutVersion(pAENode) Then
				If HasBeenCommitted(pAENode) Then
					strPreviousVersionExist = "Yes"
				Else
					strPreviousVersionExist = "--"
				End If
			Else
				strPreviousVersionExist = "--"
			End If
		End If
			
		
		GetPreviousVersionString = strPreviousVersionExist
	End Function
	
	
%>
