<%

'*****************************************************************************
' Microsoft Content Management Server
'
' File: SDReportUtility.INC
'
' Purpose:
'   ASP server-side include file that contains various utility functions 
'	for use by the reporting ASP files (and elsewhere).
'
' GLOBAL DEPENDENCIES:
'
'*****************************************************************************


	'*************************************************************************
	' public constANTS
	'*************************************************************************

	' SD Object Type Name Strings
	public const strRESOURCE_GALLERY 		= "ResourceGallery"
	public const strRESOURCE_GALLERY_ITEM 	= "Resource"
	public const strRESOURCE 				= "Resource"
	public const strPLACEHOLDER				= "Placeholder"
	public const strTEMPLATE_GALLERY		= "TemplateGallery"
	public const strTEMPLATE				= "Template"
	public const strPAGE					= "Page"
	public const strFOLDER					= "Folder"
	public const strPOSTING					= "Posting"
	public const strCHANNEL					= "Channel"
	public const strUSER_RIGHTS_GROUP 		= "RightsGroup"
	public const strUSER			 		= "User"
	public const strSERVER					= "Server (root-level)"
    public const strUNKNOWN 				= "Unknown Resolution Type"
	
	public const strDISPLAY_RESOURCE_GALLERY	= "Resource Gallery"
	public const strDISPLAY_RESOURCE_GALLERY_ITEM 	= "Resource"
	public const strDISPLAY_RESOURCE 				= "Resource"
	public const strDISPLAY_PLACEHOLDER				= "Placeholder"
	public const strDISPLAY_TEMPLATE_GALLERY		= "Template Gallery"
	public const strDISPLAY_TEMPLATE				= "Template"
	public const strDISPLAY_PAGE					= "Page"
	public const strDISPLAY_FOLDER					= "Folder"
	public const strDISPLAY_POSTING					= "Page"
	public const strDISPLAY_CHANNEL					= "Channel"
	public const strDISPLAY_USER_RIGHTS_GROUP 		= "Rights Group"
	public const strDISPLAY_USER			 		= "User"
	public const strDISPLAY_SERVER					= "Server (root-level)"
    public const strDISPLAY_UNKNOWN 				= "Unknown Resolution Type"
				
				
	'*************************************************************************
	' ENUMS
	'*************************************************************************

	' NR Site Deployment Object Types
	' 	NOTE: VBScript does not support Enum so we can't define an Enum type 
	'   eNR_OP_OBJECT_TYPE.	
	public const NR_OP_OBJECT_TYPE_UNKNOWN					= 0
	public const NR_OP_OBJECT_TYPE_RESOURCE					= 1
	public const NR_OP_OBJECT_TYPE_RESOURCE_GALLERY_ITEM	= 2
	public const NR_OP_OBJECT_TYPE_RESOURCE_GALLERY			= 3
	public const NR_OP_OBJECT_TYPE_TEMPLATE					= 4
	public const NR_OP_OBJECT_TYPE_TEMPLATE_GALLERY			= 5
	public const NR_OP_OBJECT_TYPE_PLACEHOLDER				= 6
	public const NR_OP_OBJECT_TYPE_PAGE						= 7
	public const NR_OP_OBJECT_TYPE_FOLDER					= 8
	public const NR_OP_OBJECT_TYPE_POSTING					= 9
	public const NR_OP_OBJECT_TYPE_CHANNEL					= 10
	public const NR_OP_OBJECT_TYPE_USER_RIGHTS_GROUP		= 11
	public const NR_OP_OBJECT_TYPE_USER						= 12
	public const NR_OP_OBJECT_TYPE_SERVER					= 255
	
				
	'*************************************************************************
	' GLOBAL VARIABLES
	'*************************************************************************

	' the client time zone bias for date adjustments	
	dim g_lClientTimeZoneBias
	
	' init global vars
	g_lClientTimeZoneBias = 0
	
	'*************************************************************************
	' FUNCTIONS
	'*************************************************************************
	

	'*************************************************************************
	' Description:
	'	Little utility function that converts a floating-point representation
	'	of a date to a string
	'
	' Parameters:
	'	fDate [in] - floating-point representation of a date
	'
	' Returns:
	'	string representation of floating-point date that was passed in
	'*************************************************************************
	function dateToStr( fDate )
		
		' HOTFIX #8110 - block below fixes around locale-specific Date format.
		
		
		Dim strDate	' retval
		Dim lCurrentLCID
		Dim dateIn
		
		lCurrentLCID = GetLocale
		
		If lCurrentLCID = lLCID_ENGLISH_US Then
			dateIn = CDate( CDbl( fDate ) )
			'strDate = CStr( CDate( CDbl( fDate ) ) )
		Else
			' switch locale to English (U.S.) to process date value
			Call SetLocale( lLCID_ENGLISH_US )

			Dim bErrOccurred
			Dim lErrNum
			Dim strErrSource
			Dim strErrDescription
			
			bErrOccurred = False
			
			' enable error handling here to ensure reset of locale 			
			On Error Resume Next

			dateIn = CDate( CDbl( fDate ) )

			' Cache error info for later before it's cleared
			If Err.Number <> 0 Then
				bErrOccurred = True
				lErrNum = Err.Number
				strErrSource = Err.Source
				strErrDescription = Err.Description				
			End If
			
			'disable error handling			
			On Error Goto 0

			' revert to original locale (even if date conversion failed)
			Call SetLocale( lCurrentLCID )

			If bErrOccurred Then
				' raise the exception now
				call Err.Raise( lErrNum, "dateToStr: " & strErrSource, strERROR_FAILED_DATE_CONVERSION & ": " & strErrDescription )
			End If
		end if
		
		' if we do in fact have a valid date, convert it to a string		
		if IsDate( dateIn ) then
			dim lClientLCID
			lClientLCID = pReportXMLTag.ReportInfo.ResClient.LocaleID
			
			bErrOccurred = false
			on error resume next
			
			call SetLocale(lClientLCID)
			
			strDate = CStr( dateIn )
		
			call SetLocale(lCurrentLCID)

		else
			strDate = "Unknown"	
		end if
		
		dateToStr = strDate		
	end function  'dateToStr

	
	'*************************************************************************
	' Description:
	'	Little utility function that converts a boolean (actually a 
	'	VARIANT_BOOL) from a numeric value to a yes/no string.
	'
	' Parameters:
	'	bFlag [in] - boolean value to be converted to a string
	'
	' Returns:
	'	if the value passed in is 0, then return "no"; otherwise return 
	'	"yes"
	'*************************************************************************
	function boolToStr( bFlag )
		if not bFlag then
			boolToStr = "No"
		else
			boolToStr = "Yes"
		end if  'if flag is true
		
	end function  'boolToStr


	'*************************************************************************
	' Description:
	'	Little utility function that returns the string representation of a 
	'   site deployment object type.
	'
	' Parameters:
	'	varObjectType [in] - the numeric site deployment object type to map to a string.
	' 		Note that a string containing the type number is also accepted.
	'
	' Returns:
	'	The string representing the object type
	'*************************************************************************
	public function MapSDTypeToString( varObjectType )

		' Code of this function is translated from the C++ code of
		' CNRSiteDeployTypeManager::MapSDTypeToString
		
		
		dim nObjectType

		' VBScript is loose-typed: 
		' just in case somebody passed in a number as a string thinking it's numeric...
		nObjectType = CInt( varObjectType )
		
		' TODO: raise error if caller passes us a string type name!!

		' retval
		dim strType		

		select case nObjectType
		
			case NR_OP_OBJECT_TYPE_RESOURCE_GALLERY			strType = strRESOURCE_GALLERY
			case NR_OP_OBJECT_TYPE_RESOURCE_GALLERY_ITEM	strType = strRESOURCE_GALLERY_ITEM
			case NR_OP_OBJECT_TYPE_RESOURCE					strType = strRESOURCE
			case NR_OP_OBJECT_TYPE_PLACEHOLDER				strType = strPLACEHOLDER
			case NR_OP_OBJECT_TYPE_TEMPLATE_GALLERY			strType = strTEMPLATE_GALLERY
			case NR_OP_OBJECT_TYPE_TEMPLATE					strType = strTEMPLATE
			case NR_OP_OBJECT_TYPE_PAGE						strType = strPAGE
			case NR_OP_OBJECT_TYPE_FOLDER					strType = strFOLDER
			case NR_OP_OBJECT_TYPE_POSTING					strType = strPOSTING
			case NR_OP_OBJECT_TYPE_CHANNEL					strType = strCHANNEL
			case NR_OP_OBJECT_TYPE_USER_RIGHTS_GROUP		strType = strUSER_RIGHTS_GROUP
			case NR_OP_OBJECT_TYPE_USER						strType = strUSER
			case NR_OP_OBJECT_TYPE_SERVER					strType = strSERVER
        	case Else										strType = strUNKNOWN
				
		end select  ' node type

		MapSDTypeToString = strType
		
	end function  'MapSDTypeToString

	public function MapSDTypeToDisplayString( varObjectType )

		' Code of this function is translated from the C++ code of
		' CNRSiteDeployTypeManager::MapSDTypeToString
		
		
		dim nObjectType

		' VBScript is loose-typed: 
		' just in case somebody passed in a number as a string thinking it's numeric...
		nObjectType = CInt( varObjectType )
		
		' TODO: raise error if caller passes us a string type name!!

		' retval
		dim strType		

		select case nObjectType
		
			case NR_OP_OBJECT_TYPE_RESOURCE_GALLERY			strType = strDISPLAY_RESOURCE_GALLERY
			case NR_OP_OBJECT_TYPE_RESOURCE_GALLERY_ITEM	strType = strDISPLAY_RESOURCE_GALLERY_ITEM
			case NR_OP_OBJECT_TYPE_RESOURCE					strType = strDISPLAY_RESOURCE
			case NR_OP_OBJECT_TYPE_PLACEHOLDER				strType = strDISPLAY_PLACEHOLDER
			case NR_OP_OBJECT_TYPE_TEMPLATE_GALLERY			strType = strDISPLAY_TEMPLATE_GALLERY
			case NR_OP_OBJECT_TYPE_TEMPLATE					strType = strDISPLAY_TEMPLATE
			case NR_OP_OBJECT_TYPE_PAGE						strType = strDISPLAY_PAGE
			case NR_OP_OBJECT_TYPE_FOLDER					strType = strDISPLAY_FOLDER
			case NR_OP_OBJECT_TYPE_POSTING					strType = strDISPLAY_POSTING
			case NR_OP_OBJECT_TYPE_CHANNEL					strType = strDISPLAY_CHANNEL
			case NR_OP_OBJECT_TYPE_USER_RIGHTS_GROUP		strType = strDISPLAY_USER_RIGHTS_GROUP
			case NR_OP_OBJECT_TYPE_USER						strType = strDISPLAY_USER
			case NR_OP_OBJECT_TYPE_SERVER					strType = strDISPLAY_SERVER
        	case Else										strType = strDISPLAY_UNKNOWN
				
		end select  ' node type

		MapSDTypeToDisplayString = strType
		
	end function  'MapSDTypeToDisplayString


	'*************************************************************************
	' Description:
	'	Little utility function that takes a string that represents the 
	'   type of a site deployment object, and returns the integer representation 
	'   of the type.
	'
	' Parameters:
	'	strItemType [in] - string representing the object type
	'
	' Returns:
	'	The integer object type that the string maps to
	'*************************************************************************
	public function MapStringToSDType( strItemType )

		' Code of this function is translated from the C++ code of
		' CNRSiteDeployTypeManager::MapStringToSDType

		dim nType

		select case strItemType
			case strRESOURCE_GALLERY
				nType = NR_OP_OBJECT_TYPE_RESOURCE_GALLERY				
			case strRESOURCE_GALLERY_ITEM
				nType = NR_OP_OBJECT_TYPE_RESOURCE_GALLERY_ITEM
			case strRESOURCE
				nType = NR_OP_OBJECT_TYPE_RESOURCE
			case strPLACEHOLDER
                nType = NR_OP_OBJECT_TYPE_PLACEHOLDER
			case strTEMPLATE_GALLERY
				nType = NR_OP_OBJECT_TYPE_TEMPLATE_GALLERY
			case strTEMPLATE
				nType = NR_OP_OBJECT_TYPE_TEMPLATE
			case strPAGE
				nType = NR_OP_OBJECT_TYPE_PAGE
			case strFOLDER
				nType = NR_OP_OBJECT_TYPE_FOLDER
			case strPOSTING
				nType = NR_OP_OBJECT_TYPE_POSTING
			case strCHANNEL
				nType = NR_OP_OBJECT_TYPE_CHANNEL
			case strUSER_RIGHTS_GROUP
				nType = NR_OP_OBJECT_TYPE_USER_RIGHTS_GROUP
			case strUSER
				nType = NR_OP_OBJECT_TYPE_USER
			case strSERVER
				nType = NR_OP_OBJECT_TYPE_SERVER
        	case Else		
				nType = NR_OP_OBJECT_TYPE_UNKNOWN
		end select  ' node type in string

		' this is meant to be type Integer
		mapStringToSDType = nType
		
	end function  'MapStringToSDType
	
	
	'*************************************************************************
	' Description:
	'	Little utility function that converts a given date to be in the client 
	'   time zone.  The client time zone is stored in a global variable, 
	'   g_lClientTimeZoneBias, and set as provided by the Report.
	'
	' Parameters:
	'	fDate [in] - floating-point representation of a date
	'
	' Returns:
	'	date converted to client time zone.
	'*************************************************************************
	function dateToClientTimeZone( fDate )

		' HOTFIX #8110 - block below fixes around locale-specific Date format.

		dim lCurrentLCID
		lCurrentLCID = GetLocale
		Dim dateIn
		
		If lCurrentLCID = lLCID_ENGLISH_US Then
			' "n" denotes interval in minutes
			dateToClientTimeZone = DateAdd("n", -(g_lClientTimeZoneBias), fDate)
		Else
			' switch locale to English (U.S.) to process date value
			Call SetLocale( lLCID_ENGLISH_US )
			
			Dim bErrOccurred
			Dim lErrNum
			Dim strErrSource
			Dim strErrDescription

			bErrOccurred = False
			
' enable error handling here to ensure reset of locale 			
On Error Resume Next
		
			' "n" denotes interval in minutes
			dateToClientTimeZone = DateAdd("n", -(g_lClientTimeZoneBias), fDate)
						
			' Cache error info for later before it's cleared
			If Err.Number <> 0 Then
				bErrOccurred = True
				lErrNum = Err.Number
				strErrSource = Err.Source
				strErrDescription = Err.Description				
			End If
			
'disable error handling			
On Error Goto 0

			' revert to original locale (even if date conversion failed)
			Call SetLocale( lCurrentLCID )

			If bErrOccurred Then
				' raise the exception now
				call Err.Raise( lErrNum, "dateToClientTimeZone: " & strErrSource, strERROR_FAILED_DATE_CONVERSION & ": " & strErrDescription )
			End If
			
		End If	' LCID <> EN-US
end function  'dateToClientTimeZone 

%>