<%@ EnableSessionState=False %>
option explicit

Const USE_PROXY = True
Const AELOGIN_EDIT = &H1
Const AELOGIN_ATTACH_COOKIE_TO_URLS = &H8
const AELOGIN_ERROR = 30002			'	constant error number in proxy when the login has problem

Dim m_pAESession
Dim m_pAEServer
Dim m_bInitialized
Dim m_bIsAdmin
Dim m_bIsEditor
Dim m_bIsModerator

Const MAIN_TITLE = "Site Manager - Microsoft Content Management Server"
Const GLOBALPARAM_ADMINWINDOW = "AdminToolWindow:<%Response.Write(Request.ServerVariables.Item("HTTP_HOST"))%>"
Const STRMSG_LOGIN_ERROR1 = "To log into Microsoft Content Management Server, you must be logged onto the network with your password."
Const STRMSG_LOGIN_ERROR2 = "Please save current work, exit other applications, and restart your computer,"
Const STRMSG_LOGIN_ERROR3 = "entering your network password when prompted. Then start the MSCMS Site Manager again."
Const GLOBALPARAM_SHOW_APPROVALASST = "ShowApprovalAsst"

Const TITLBAR_IMAGES_PATH = "Titlebar/"
Const TOOLBAR_IMAGES_PATH = "Toolbar/"

'ACL consts
Const ACL_FULLCONTROL = &H80000000	'-2147483648

'Generic Properties for User Roles
Const GP_EDITOR_USERGALLERY = "_UserGalleryEditors"
Const GP_MODERATOR_USERGALLERY = "_UserGalleryModerators"

'Server Search type and option consts
Private Const SEARCH_ROLESBYUSER = "RolesByUser"
Private Const SEARCH_USERNAME = "UserName"

' User Role Constants
Private Const RE_ROLE_EDITOR = 4
Private Const RE_ROLE_MODERATOR = 5

dim m_pRightsChecker

sub window_OnLoad

	m_bInitialized = false

	'	prevent launching multiple-instance of AdminTool
	if GlobalParameters.Exists(GLOBALPARAM_ADMINWINDOW) then
		call GlobalParameters(GLOBALPARAM_ADMINWINDOW).BringToTop
		call FireClose
		exit sub
	end if
	set GlobalParameters(GLOBALPARAM_ADMINWINDOW) = window

	FrameOcxCLSID = "AdmnTool.AEChannel"

	WindowIcon = TITLBAR_IMAGES_PATH + "Resolution.ico"
	WindowTitle = MAIN_TITLE

	set m_pRightsChecker = CreateObject("RERightsChk.RERightsChecker")

	'	since we need to use the Server object to determine some of
	'	the logic in building the menus & toolbars, we need to initialize
	'	the program parameters now for Session and Server.
	call initParams

	' Find out some user role info for later use.
	m_bIsAdmin = m_pRightsChecker.IsAdministrator(m_pAESession, m_pAEServer)
    m_bIsEditor = m_pRightsChecker.IsInRole(m_pAESession, m_pAEServer, RE_ROLE_EDITOR)
    m_bIsModerator = m_pRightsChecker.IsInRole(m_pAESession, m_pAEServer, RE_ROLE_MODERATOR)

	'File Menu
	'==========
	Dim FileMenu, FileSubMenu
	Set FileMenu = MainMenu.AddSubmenuItem("File", "&File", "").Submenu
		Set FileSubMenu = FileMenu.AddSubmenuItem("FileNew", "&New", "").Submenu
			Call FileSubMenu.AddItem("FileNewChannel", "&Channel", "")
			Call FileSubMenu.AddItem("FileNewGallery", "&Gallery", "")
			Call FileSubMenu.AddSeperatorItem
			Call FileSubMenu.AddItem("FileNewRightsGroup", "&Rights Group", "")
		if m_pRightsChecker.IsAdministrator(m_pAESession, m_pAEServer) then		' the user is an administrator
			Set FileSubMenu = FileMenu.AddSubmenuItem("FilePackage", "&Package", "").Submenu
				Call FileSubMenu.AddItem("FilePackageExport", "E&xport...", "")
				Call FileSubMenu.AddItem("FilePackageImport", "&Import...", "")
		end if
		
		Call FileMenu.AddSeperatorItem
		Call FileMenu.AddItem("FileRevert", "Re&vert to Approved", "")
		Call FileMenu.AddItem("FileDelete", "&Delete", "")
		Call FileMenu.AddItem("FileRename", "Rena&me", "")
		Call FileMenu.AddItem("FileProperties", "P&roperties", "")

		Call FileMenu.AddSeperatorItem
		Call FileMenu.AddItem("FileExit", "E&xit", "")


	'Edit Menu
	'==========
	Dim EditMenu
	Set EditMenu = MainMenu.AddSubmenuItem( "Edit", "&Edit", "" ).Submenu
		Call EditMenu.AddItem("EditCut", "Cu&t", "")
		Call EditMenu.AddItem("EditCopy", "&Copy", "")
		Call EditMenu.AddItem("EditPaste", "&Paste", "")
		Call EditMenu.AddSeperatorItem
		Call EditMenu.AddItem("EditSelectAll", "Select &All", "")
		Call EditMenu.AddItem("EditInvertSel", "&Invert Selection", "")

	'View menu
	'==========
	Dim ViewMenu
	Set ViewMenu = MainMenu.AddSubmenuItem( "&View", "&View", "" ).Submenu
		Call ViewMenu.AddItem("ViewRefresh", "Global Re&fresh", "")

	'Tools menu
	'===========
	Dim ToolsMenu, ToolsSubMenu
	Set ToolsMenu = MainMenu.AddSubmenuItem("Tools", "&Tools", "").Submenu
		Call ToolsMenu.AddItem("ToolsDependantReport", "&Dependent Report", "")
		Call ToolsMenu.AddItem("ToolsPurgeRevisions", "&Clear Revision History...", "")
		
		Call ToolsMenu.AddSeperatorItem
		'Enable "Clear Deleted Items" for all users with access to the Site Manager.
		Call ToolsMenu.AddItem("ToolsPurgeAllDeletedItems", "&Clear Deleted Items", "")
		Call ToolsMenu.AddSeperatorItem
		Call ToolsMenu.AddItem("ToolsKillLock", "&Kill Lock", "")
		Call ToolsMenu.AddItem("ToolsOptions", "&Options...", "")
		

	'Help menu
	'==========
	Dim HelpMenu
	Set HelpMenu = MainMenu.AddSubmenuItem("Help", "&Help", "").Submenu
		Call HelpMenu.AddItem("Help", "&Contents", "")
		Call HelpMenu.AddSeperatorItem
		Call HelpMenu.AddItem("HelpWeb", "Information on the &Web", "")
		Call HelpMenu.AddSeperatorItem
		Call HelpMenu.AddItem("HelpAbout", "&About Microsoft Content Management Server", "")

	'Admin toolbar
	'==============
	Dim AdminToolbar
	Set AdminToolbar = Toolbars.CreateStdToolbar( "Admin" )
		Call AdminToolbar.AddButton("EditCut", "", TOOLBAR_IMAGES_PATH + "Cut.gif", "Cut")
		Call AdminToolbar.AddButton("EditCopy", "", TOOLBAR_IMAGES_PATH + "Copy.gif", "Copy")
		Call AdminToolbar.AddButton("EditPaste", "", TOOLBAR_IMAGES_PATH + "Paste.gif", "Paste")
		Call AdminToolbar.AddButton("FileDelete", "", TOOLBAR_IMAGES_PATH + "Delete.gif", "Delete" )
		
		Call AdminToolbar.AddSeperator
		Call AdminToolbar.AddButton("ViewRefresh", "", TOOLBAR_IMAGES_PATH + "Refresh.gif", "Global Refresh" )
		
		Call AdminToolbar.AddSeperator
		Call AdminToolbar.AddButton("FileProperties", "", TOOLBAR_IMAGES_PATH + "Properties.gif", "Properties")
	AdminToolbar.Visible = True
	
		
		'ER 86:  Remove Import button since all functionality is being moved to "New"
		'Call AdminToolbar.AddButton("FileImport", "", TOOLBAR_IMAGES_PATH + "Importfile.gif", "Import")
		
end sub

sub initParams
	Dim pErrorInfo

	Set m_pAESession = nothing
	Set m_pAEServer = nothing

	if not IsNull(GlobalParameters("AESession")) then
    	Set m_pAESession = GlobalParameters("AESession")
	end if

	if GlobalParameters.Exists("AEServer") then
		set m_pAEServer = GlobalParameters("AEServer")
	else

		if USE_PROXY then
			'   **** Proxy code ****
			if ( m_pAESession is nothing ) then
				Set m_pAESession = CreateObject("AESessionClient.AESessionClient.2")

				Call m_pAESession.Login("", "","", AELOGIN_EDIT + AELOGIN_ATTACH_COOKIE_TO_URLS)
			end if
			Call m_pAESession.SetCacheHint("DefaultStartup","")

			call Err.clear
			on error resume next
			Set m_pAEServer = m_pAESession.GetServerByUrl( ResolveRelativeURL("/NR/System/Marshalling/") )

			' 	check whether error is the login without password or not.
			' 	If yes, then display the login error message for the user;
			'	Otherwise, check the error message in the Frame control (AEChannel.ocx)
			if Err.Number <> 0 then
				Set pErrorInfo = m_pAESession.DecodeErrorInfo(Err.Description)
				if pErrorInfo.UniqueErrorId = AELOGIN_ERROR Then
					call Msgbox(STRMSG_LOGIN_ERROR1 & vbCrLf & vbCrLf & STRMSG_LOGIN_ERROR2 & vbCrLf & STRMSG_LOGIN_ERROR3, 48,MAIN_TITLE)
					exit sub
				else
					call Err.clear
				end if
			end if
			'   **** Proxy code ****
		else
			'   **** ServerObject code ****
			if ( m_pAESession is nothing ) then
				Set m_pAESession = CreateObject("AESession.AESession.1")
				Call m_pAESession.Login("WinNT://NCOMPASS-DEV/Administrator", "passwd", "machine", AELOGIN_EDIT + AELOGIN_ATTACH_COOKIE_TO_URLS)
			end if
			Set m_pAEServer = m_pAESession.GetDefaultServer
			'   **** ServerObject code ****
		end if

		set GlobalParameters("AEServer") = m_pAEServer
		Set GlobalParameters("AESession") = m_pAESession
	end if
end sub

sub window_OnActivate
	on error resume next

    call Frame.Init( (m_pAESession), (m_pAEServer), nothing, nothing, (window) )
	m_bInitialized = true
end sub

sub window_OnClose
	if (m_bInitialized = true) then
		Call Frame.UnInit
	end if

	Set m_pAESession = nothing
	Set m_pAEServer = nothing
end sub

sub window_onUnload
	Call GlobalParameters.Remove(GLOBALPARAM_ADMINWINDOW)
end sub

function hasApprovalRights
	hasApprovalRights = m_pRightsChecker.IsUIActionPossible(m_pAESession, m_pAEServer, nothing, nothing, m_pRightsChecker.StringToUIAction("RE_UI_ACTION_APPROVAL_ASSISTANT") )
end function