<%
' ********************************************************************
' Copyright (C) 2001 Microsoft Corporation. All rights reserved.
'
'	IMPORTANT.  Please read the legal.txt file, located in the
'	"<CMS Install Directory>\Server\httpexec\WBC\Internals"
'	directory, governing the use of this internal file."
' ********************************************************************

' ********************************************************************
' File:		CNCTabControl.inc
'
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	Responsible for Displaying a tab control
'
' Note:		To Implement this class, you need include a inc file
'			with a class consists of three public Sub procedures:
'			1)RenderTabActiveHead()
'			2)RenderTabInActiveHead()
'			3)RenderTabContent()
' 			RenderTabActiveHead Sub is responsible for displaying tab
'			head,which shows the tab content connected to this tab head.
'			RenderTabInActiveHead Sub is responsible for display tab head
'			, which hide the tab content and wait for the user to click.
'			When the user click the InActiveHead, the ActiveHead is activated
'			and the relative tab content will be activated as well.
'
' Warning:  1)It is not supported for Netscape 6 so far
'			2)If you want to support Netscapte, do not use: style="",
'				use class instead in your render class.
'
' Public Methods and Properties Exposed: (all integer parameter in px unit)
'	- Sub InitTabControl( nApsPosTop, 	(Integer, Absolute position for window top)
'						  nApsPosLeft,	(Integer, Absolute position for window left)
'   					  nHeadWidth, 	(Integer, Tab head width)
'                         nHeadHeight,	(Integer, Tab head Height)
'						  nContentWidth,(Integer, Tab content Width)
'                         nContentHeight)(Integer, Tab content Height)
'	- Sub AddTab(pRenderer) (Object, came from render class)
'	- Sub DisplayTabControl()
'
'	- Property CurrentTabIndex [read/write] (Integer, Set default Tab:
'											for instance, 0, means the first
'											tab will be activated when display.)
'
'
' How to implement this class(Example):
'			--Create an instance of CNCTabConTrol
'
'			Dim pTabControl
'			Set pTabControl = New CNCTabControl
'
'			--Create RenderTab instances
'
'			Dim pGeneralTabRender
'			Set pGeneralTabRender = New SCATabRenderer_General

'			Dim pCacheTabRender
'			Set pCacheTabRender = New SCATabRenderer_Cache
'
'			--Initialize tab control
'			The parameters in order are:
'			1)Absolute position for window top,default value = 100
'			2)Absolute position for window left, default value = 15
'			3)Tab head width, default value = 90
'			4)Tab head height, default value = 20
'			5)Tab Content Width, default value = 750
'			6)Tab Content Height, default value = 200
'			All parameters are integer, in px units
'			If you want to use defaut value, just set each parameter to 0
'
'			Call pTabControl.InitTabControl( 100, 15, 90, 20, 750, 200 )
'
'			If this control does not work in WBC, you have to change Java Script Path
'			'--Set Java Script Path
'			g_pASPPageManager.TabControl.JavaScriptPath = ""
'
'			--Add tab
'			The parameter is an instance of tab render class
'
'			Call pTabControl.AddTab(pGeneralTabRender)
'			Call pTabControl.AddTab(pCacheTabRender)
'
'			--Display tab control
'
'			Call pTabControl.DisplayTabControl
'
'			--clear Object
'			Set pTabControl = Nothing
'
' ********************************************************************

	'Define constants for default value
	Const IDS_DEFAULT_BACKCOLOR = "#ffffff"
	Const ID_DEFAULT_TOP = 100
	Const ID_DEFAULT_LEFT = 15
	Const ID_DEFAULT_HEAD_WIDTH = 80
	Const ID_DEFAULT_HEAD_HEIGHT = 20
	Const ID_DEFAULT_CONTENT_WIDTH = 750
	Const ID_DEFAULT_CONTENT_HEIGHT = 210

	'Define constants for parameters
	Const ID_TABCONTROL_INACTIVE_HEAD = 1
	Const ID_TABCONTROL_CONTENT = 2
	Const ID_TABCONTROL_ACTIVE_HEAD = 3
	Const ID_TABCONTROL_VALIDATE = 0

	'Define constants for division id prefix
	Const IDS_ID_PRE_HEAD_INACTIVE = "TABHEAD"
	Const IDS_ID_PRE_HEAD_ACTIVE = "TABACTIVEHEAD"
	Const IDS_ID_PRE_CONTENT = "TABCNT"

	'Define constants for style sheet
    Const IDS_STYLESHEET_PROPERTY_VISIBLE = "visible"
    Const IDS_STYLESHEET_PROPERTY_HIDDEN = "hidden"
    Const IDS_STYLESHEET_PROPERTY_CONTENT = "content"

    'Define constants for java script
    Const IDS_JAVASCRIPT_FILE_IE = "IE_DOM.js"
    Const IDS_JAVASCRIPT_FILE_NS = "NS_DOM.js"
    Const IDS_JAVASCRIPT_PATH = "/NR/System/WBC/Internals/ServerControls/TabControl/"
    Const IDS_JAVASCRIPT_FUNCTION_NAME = "NCTab_displayTab"
    Const IDS_JAVASCRIPT_HEAD_ARRAY = "aTabHead"
    Const IDS_JAVASCRIPT_CONTENT_ARRAY = "aTabContent"
    Const IDS_JAVASCRIPT_ACTIVE_HEAD_ARRAY = "aTabActiveHead"

    'Define constants for error message
    Const IDS_MESSAGE_ERROR_HEAD = "Error:"
    Const IDS_MESSAGE_N0OBJECT = "You must pass a rendering object when adding"
   	Const IDS_MESSAGE_ERRORPARAMETERS = "You must enter an integer as parameters for InitTopControl()"
    Const IDS_MESSAGE_WRONG_ID = "There is wrong div id"

Class CNCTabControl

	Public JavaScriptPath

	'Define Private variable

	'Define Division ID
    Private m_strTabHeadID
    Private m_strTabHeadActiveID
	Private m_strTabContentID

	'Define size for tab head and content
    Private m_nHeadWidth
    Private m_nHeadHeight
    Private m_nContentWidth
    Private m_nContentHeight

	'Define position for tab head and tab content
	Private m_nAbsPosTop
	Private m_nAbsPosLeft
    Private m_nTabContentTop
	Private m_nTabHeadLeft

    'local copy for public property

  	Private m_nCurrentTabIndex
   	Public Property Let CurrentTabIndex(ByVal vData )

   		m_nCurrentTabIndex = vData

   	End Property

   	Public Property Get CurrentTabIndex()

       If Not IsNumeric(m_nCurrentTabIndex) Then
          m_nCurrentTabIndex = 0
       ElseIf m_nCurrentTabIndex < ID_TABCONTROL_VALIDATE Then
          m_nCurrentTabIndex = 0
       End If

   	End Property


    ' --The Renderer Dictionary and the corresponding index
    ' Define a member of an instance of dictionary object
    Private m_pRendererDict

    ' Define dictionary key as index of tabs when user add a tab
    Private m_nDictIndex

    ' Define a variable to decide if display tab control or not
    ' if any error happen in initTabControl sub
    Private m_bErrorEraser

    ' ---- [Private] Standard Class_Initialize
    Private Sub Class_Initialize()
    	' -- init all member with default values

		'-- Create a dictionary for adding tab pages
        Set m_pRendererDict = CreateObject("Scripting.Dictionary")

    	'-- Default dictionary key for 0
    	m_nDictIndex  = 0

    	'-- Default error eraser is false
    	m_bErrorEraser =  False

    	m_nCurrentTabIndex = 0

    End Sub

    '*************************************************
	'Purpose: 	Validate position and size parameters and
	'			Set tob control position and site
	'
	'Input:		1)tab control absolute top position;
	'			2)tab control absolute left position;
	'			3)tab head width
	'			4)tab head Height
	'			5)tab content width
	'			6)tab content height
	'
	'Output:    none
    '*************************************************

    Public Sub InitTabControl(nAbsPosTop, _
    						  nAbsPosLeft, _
    						  nHeadWidth,_
							  nHeadHeight, _
							  nContentWidth,_
							  nContentHeight)

		'--Validate parameters against any none numeric
		If Not IsNumeric(nAbsPosTop) Then
			m_bErrorEraser = True
			Exit Sub
		ElseIf Not IsNumeric(nAbsPosLeft) Then
			m_bErrorEraser = True
			Exit Sub
		ElseIf Not IsNumeric(nHeadWidth) Then
			m_bErrorEraser = True
			Exit Sub
		ElseIf Not IsNumeric(nHeadHeight) Then
			m_bErrorEraser = True
			Exit Sub
		ElseIf Not IsNumeric(nContentWidth) Then
			m_bErrorEraser = True
			Exit Sub
		ElseIf Not IsNumeric(nContentHeight) Then
			m_bErrorEraser = True
			Exit Sub

		End If

		'--Set Java Script Path
		JavaScriptPath = IDS_JAVASCRIPT_PATH

		'--Convert to members
		m_nAbsPosTop = nAbsPosTop
		m_nAbsPosLeft = nAbsPosLeft
      	m_nHeadWidth = nHeadWidth
	  	m_nHeadHeight = nHeadHeight
		m_nContentWidth = nContentWidth
	  	m_nContentHeight = nContentHeight

    	'--Set default value
    	If CInt(m_nAbsPosTop) < ID_TABCONTROL_VALIDATE Then
    	   m_nAbsPosTop = ID_DEFAULT_TOP
    	End If
    	If CInt(m_nAbsPosLEFT) < ID_TABCONTROL_VALIDATE Then
		   m_nAbsPosLEFT = ID_DEFAULT_LEFT
    	End If
		If  CInt(m_nHeadWidth) <= ID_TABCONTROL_VALIDATE Then
			m_nHeadWidth =  ID_DEFAULT_HEAD_WIDTH
		End If
		If CInt(m_nHeadHeight) <= ID_TABCONTROL_VALIDATE Then
			m_nHeadHeight = ID_DEFAULT_HEAD_HEIGHT
		End If
		If CInt(m_nContentWidth) <= ID_TABCONTROL_VALIDATE Then
			m_nContentWidth = ID_DEFAULT_CONTENT_WIDTH
		End If
		If CInt(m_nContentHeight) <= ID_TABCONTROL_VALIDATE Then
			m_nContentHeight = ID_DEFAULT_CONTENT_HEIGHT
		End If



		'--Set value for the first tab head left position
		m_nTabHeadLeft = CInt(m_nAbsPosLeft)

		'--The tab Content top position
		m_nTabContentTop = m_nHeadHeight + m_nAbsPosTop

	End Sub


	'*************************************************
	'Purpose: 	Get a new key in dictionary as a new tab
	'
	'Input:		Renderer object
	'
	'Output:    none
    '*************************************************

	Public Sub AddTab( pRenderer)

		' -- validate pRenderer
		If Not IsObject(pRenderer) Then
		    Call Response.Write ("<div>" & IDS_MESSAGE_ERROR_HEAD & IDS_MESSAGE_N0OBJECT & " tab" & m_nDictIndex & "</div>")
			Exit Sub
		End If

        '-- Add keys into dictionary object
		Call m_pRendererDict.Add( m_nDictIndex, pRenderer )
		m_nDictIndex = m_nDictIndex + 1

	End Sub

	'*************************************************
	'Purpose: 	Display tab control
	'
	'Input:		none
	'
	'Output:    tab control (HTML,Style sheet, java script)
    '*************************************************

	Public Sub DisplayTabControl()

		'--Any error in initTabControl
		If m_bErrorEraser = True Then

			Call Response.Write ("<div>" & IDS_MESSAGE_ERROR_HEAD & IDS_MESSAGE_ERRORPARAMETERS & "</div>")
			Exit Sub

		End If

		Dim colRenderers
		'-- Get the items
		colRenderers = m_pRendererDict.Items

	    '-- Render java script for including java script file
		' and render there java script arrays
	    call RenderJavaScript()

		'-- Create style sheet
	    Call RenderStyleSheet()

	    '-- Create division for head and content

	    Dim nColIndex
	    Dim strTabHeadID
		Dim strTabHeadActiveID
		Dim strTabContentID

	    For nColIndex = 0 To m_pRendererDict.Count -1
	            '----Get tab head ID and content ID
				m_strTabHeadID = 	 IDS_ID_PRE_HEAD_INACTIVE &	CStr(nColIndex)
				m_strTabHeadActiveID = IDS_ID_PRE_HEAD_ACTIVE & CStr(nColIndex)
				m_strTabContentID =  IDS_ID_PRE_CONTENT & CStr(nColIndex)

			    '-- Create inactive tab Head
			    Call RenderTabInactiveHead(nColIndex,colRenderers)


				'-- Create tab active Head
				Call RenderTabActiveHead(nColIndex,colRenderers)


	 			'-- Create tab Content
	 			Call RenderTabContent(nColIndex,colRenderers)

		Next


		'-- Render a java script to activate tab head and tab content
		' as default setting

	    Call RenderJavaScriptForTabControlDefaultSetting()

	    '-- Clear objects

		Set m_pRendererDict = Nothing

	End Sub

	'----[Private]
	Private Sub RenderStyleSheet()

		Response.Write "<style type=text/css>" & vbCrLf & _
	    				 "<!--"  & vbCrLf

	    Dim nColIndex
	    Dim strTabHeadID
	    Dim strTabHeadActiveID
	    Dim strTabContentID


	    For nColIndex = 0 To m_pRendererDict.Count -1

	    	'-- Get class id
	    	strTabHeadID = 	 IDS_ID_PRE_HEAD_INACTIVE &	CStr(nColIndex)
	    	strTabHeadActiveID = IDS_ID_PRE_HEAD_ACTIVE & CStr(nColIndex)
			strTabContentID =  IDS_ID_PRE_CONTENT & CStr(nColIndex)

			'-- Render style sheet class
			Call RenderStyleSheetClass(strTabHeadID, _
									IDS_STYLESHEET_PROPERTY_VISIBLE)
			Call RenderStyleSheetClass(strTabHeadActiveID , _
			 						IDS_STYLESHEET_PROPERTY_HIDDEN)
			Call RenderStyleSheetClass(strTabContentID, _
			 						IDS_STYLESHEET_PROPERTY_CONTENT)
			m_nTabHeadLeft = m_nTabHeadLeft + m_nHeadWidth

		Next

		Response.Write "-->"  & vbCrLf & _
					 "</style>"  & vbCrLf

	End Sub


	'---- [Private] Render style sheet class for tab heads and tab content
	Private Sub RenderStyleSheetClass(strClassID,strStyleSheetProperty)

	    '-- Render tab content style sheet class
	    If strStyleSheetProperty = IDS_STYLESHEET_PROPERTY_CONTENT then

	    	%>
	    	#<% = strClassID %>{visibility: hidden; position: absolute;top: <% = m_nTabContentTop %>px; left: <% = m_nAbsPosLeft %>px;width: <% =m_nContentWidth %>px; height: <% =m_nContentHeight%>px}
	    	<%
	    '-- Render both active and inactive tab head style sheet classes
	    ElseIf  strStyleSheetProperty = IDS_STYLESHEET_PROPERTY_HIDDEN _
	    			Or strStyleSheetProperty = IDS_STYLESHEET_PROPERTY_VISIBLE Then
			%>
			#<% = strClassID %>{visibility: <% = strStyleSheetProperty %>;position: absolute; top: <% = m_nAbsPosTop %>px; left: <% =m_nTabHeadLeft%>px;width: <% =m_nHeadWidth %>px; height: <% =m_nHeadHeight%>}
			<%
		End If

	End Sub

    '----[Private] Render a java script for manipulate tab control
    Private Sub RenderJavaScript()

		'--Include Java script files based on different browser
 		Call IncludeJavaScriptFile()

 		'-- Render java script array
       	'-- Render java script head
       	Call Response.Write ("<script language=""javascript"" type=""text/javascript"" >" & vbCrLf)

		'--Render tab inactive head array
		Call RenderJavaScriptArray(IDS_ID_PRE_HEAD_ACTIVE,_
									IDS_JAVASCRIPT_HEAD_ARRAY)

		'--Render tab content array
		Call RenderJavaScriptArray(IDS_ID_PRE_CONTENT, _
									IDS_JAVASCRIPT_CONTENT_ARRAY)

		'--Render tab active head array
		Call RenderJavaScriptArray(IDS_ID_PRE_HEAD_INACTIVE, _
									IDS_JAVASCRIPT_ACTIVE_HEAD_ARRAY)

		'-- Close java script
		Call Response.Write ("</script>" & vbCrLf)

   End Sub

   '---- [Private] Include java script file based on different browser
   Private  Sub IncludeJavaScriptFile()

   		Dim strJavaScriptPath

    	'-- Give java script head and java script file path
    	Response.Write "<script language=""javascript"" type=""text/javascript"" SRC="""
    	strJavaScriptPath = JavaScriptPath

        '-- Check different browser to include different java script file
        If IsIEBrowser() Then
             	strJavaScriptPath = strJavaScriptPath & IDS_JAVASCRIPT_FILE_IE

   		Else
           		strJavaScriptPath = strJavaScriptPath & IDS_JAVASCRIPT_FILE_NS

   		End If

   		'-- Close java script
   		Response.Write strJavaScriptPath & """></script>"  & vbCrLf

   End Sub

   '---- [Private] Render Java Script array based on different parameter input
   Private Function RenderJavaScriptArray(idPrefix,ArrayName)

		'-- Start to render an array
		Call Response.Write (ArrayName & "= new Array(")

   		Dim strTabControlContent
   		Dim nColIndex
   		Dim strTabHeadID

   		For nColIndex = 0 To m_pRendererDict.Count-1
   		    '-- Get id from dictionary key
   		    strTabHeadID  = idPrefix & CStr(nColIndex)

   		    strTabControlContent = "'" & strTabHeadID & "'"

   		    '-- add ","
   		    If nColIndex <> (m_pRendererDict.Count - 1) Then
   		          strTabControlContent = strTabControlContent & ","
   		    End If

   		    '-- Render array content
   		    Response.Write  strTabControlContent
   		Next

		'-- Close this array
		Call Response.Write (");" & vbCrLf)


   End Function

	' ---- [Private] Render Div Head in HTML for tab content and tab heads
    Private Function RenderDivHead(strDivHeadType)

	    Dim strTabControlContent

		If strDivHeadType = ID_TABCONTROL_INACTIVE_HEAD Then

			' -- Create a string for the tab head
			strTabControlContent = "<div id=" & m_strTabHeadID & _
									"> <a href=""javascript:" & _
			                        IDS_JAVASCRIPT_FUNCTION_NAME & _
			                        "('" & m_strTabHeadActiveID & "')"">"

		ElseIf strDivHeadType = ID_TABCONTROL_ACTIVE_HEAD Then

			' -- Create a string for tab active head
			strTabControlContent = "<div id=" & m_strTabHeadActiveID & ">"
		ElseIf 	strDivHeadType = ID_TABCONTROL_CONTENT Then
			' -- Create a string for the tab content
			strTabControlContent = "<div id=" & m_strTabContentID & ">"

		Else
			Call Response.Write ("<div>" & IDS_MESSAGE_ERROR_HEAD & IDS_MESSAGE_WRONG_ID & "</div>")
		End If

		 '---- Return this string
	     RenderDivHead = strTabControlContent
	End Function

	'----[Private] Check browser is IE or NetsCape
	Private Function IsIEBrowser()

		Dim strUA
		'--Found out browser type
		strUA = Request.ServerVariables("HTTP_USER_AGENT")

		'--Return true or false
		If Instr(strUA, "MSIE") Then
	     	IsIEBrowser = True
		Else
     	  	IsIEBrowser = False
		End If

	End Function

	'----[Private]Render an inactive tab head for tab control
	Private Sub RenderTabInactiveHead(nColIndex,colRenderers)

		'-- Get div head
		Call Response.Write (RenderDivHead(ID_TABCONTROL_INACTIVE_HEAD) & vbCrLf)

		'-- Render inactive Tab head
		Call Response.Write (colRenderers(nColIndex).RenderTabInactiveHead & vbCrLf)

		'-- Close div
	 	Call Response.Write ("</a></div>" & vbCrLf)

	End Sub

	'----[Private]Render an inactive tab head for tab control
	Private Sub RenderTabactiveHead(nColIndex,colRenderers)

		'-- Get div head
		Call Response.Write (RenderDivHead(ID_TABCONTROL_ACTIVE_HEAD) & vbCrLf)

		'-- Render active Tab head
		Call Response.Write (colRenderers(nColIndex).RenderTabActiveHead & vbCrLf)

		'-- Close div
		 Call Response.Write ("</a></div>" & vbCrLf)

	End Sub

	'----[Private]Render tab content for tab control
	Private Sub RenderTabContent(nColIndex,colRenderers)

		'-- Get div head
		Call Response.Write (RenderDivHead(ID_TABCONTROL_CONTENT) & vbCrLf)

		'-- Render Tab Content
		Call Response.Write (colRenderers(nColIndex).RenderTabContent  & vbCrLf)

		'-- Close division
		Call Response.Write ("</a></div>" & vbCrLf)

	End Sub


 	'----[Private]Activate one tab head and tab content as default setting
	Private Sub RenderJavaScriptForTabControlDefaultSetting()

		Dim strTabHeadID
		'--Get tab head ID and content ID
		strTabHeadID =  IDS_ID_PRE_HEAD_ACTIVE &CStr(m_nCurrentTabIndex )

		'--Set activate tab head as default
		 %>
		 <script language="JavaScript">
		 <%= IDS_JAVASCRIPT_FUNCTION_NAME %>('<% = strTabHeadID %>')
		 </script>
		 <%

	End Sub

End Class
%>
