<%
' ********************************************************************
' 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."
' ********************************************************************

' ---- [Private] Only loop through and set the placeholders that were actually emitted in the WBC form
Sub SetContentForAllEmittedPhs( pPosting )

	On Error Resume Next

	Dim pFormReader
	Set pFormReader = GetInitializedFormReaderSafe( False )

	' If no form elements were submitted, bail out
	If (pFormReader.Form.Count <= 0) Then
		Exit Sub
	End If

	Dim colPlaceholders
	Dim pPlaceholder

	Set colPlaceholders = pPosting.Placeholders
	' if error encountered, leave the Sub
	If (AutoSession.LastError.ID <> 0) Then
		Exit Sub
	End If

	' Use hidden form elements to find registered placeholder count and names for those plaeholders
	' that were actually emitted for editing in the WBC form.
	' This protects against overwriting data in the placeholders for which form content was not submitted.
	Dim nEmittedPlaceholderCount
	nEmittedPlaceholderCount = CInt( pFormReader.Form("NCPHCount") )

	Dim nPhIndex
	For nPhIndex = 1 To nEmittedPlaceholderCount
		Dim strPlaceholderName
		' Get the placeholder from the corresponding name in the form
		strPlaceholderName = pFormReader.Form("NCPHName_" & nPhIndex)

		Set pPlaceholder = Nothing
		Set pPlaceholder = GetPlaceholderSafe( colPlaceholders, strPlaceholderName )

		If Not pPlaceholder Is Nothing Then

			' Call hooks so that we check for each pPlaceholder we go through
			If ( OnInternalBeforeSetPhContent( pPosting, pPlaceholder ) And _
					OnBeforeSetPhContent( pPosting, pPlaceholder ) ) Then

				' Set the value for this placeholder
				SetPhContent( pPlaceholder )

				' if error encountered, leave the Sub
				If (AutoSession.LastError.ID <> 0) Then
					Exit Sub
				End If

				' If no error in setting pPlaceholder contents,
				' we call the after hooks

				Call OnInternalAfterSetPhContent( pPosting, pPlaceholder )

				Call OnAfterSetPhContent( pPosting, pPlaceholder )

			End If

			' if error encountered, leave the Sub
			If (AutoSession.LastError.ID <> 0) Then
				Exit Sub
			End If
		End If

	Next
End Sub

' ---- [Private] Get a placeholder from a placeholder collection by name or index, ignoring any errors.
'	If there is an index out of range, or any other error, this sub calls AutoSession.ClearLastError
Function GetPlaceholderSafe( colPlaceholders, strPlaceholderNameOrIndex )

	On Error Resume Next

	Dim pPlaceholder
	Set pPlaceholder = Nothing
	Set pPlaceholder = colPlaceholders.Item( strPlaceholderNameOrIndex )

	If pPlaceholder Is Nothing Then
		' Clear any "Index Out of Range" (7100) Error so that the entire operation
		'   does not fail and data in other placeholders can be saved.
		' NOTE: It would be possible to cause a JavaScript alert to notify the user
		'   that not all submitted data is being saved.  For now we are silently saving what we can.
		AutoSession.ClearLastError
	End If

	Set GetPlaceholderSafe = pPlaceholder
End Function

Dim g_bFiltered	' boolean to indicate whether placeholder content has been filtered
g_bFiltered = False

' ---- [Private] Set the content of a Placeholder
Sub SetPhContent( pPlaceholder )
	' If there is any error getting and initializing this form reader an error is printed
	'	and the response is ended
	Dim pFormReader
	Set pFormReader = GetInitializedFormReaderSafe( False )

	If pPlaceholder.IsEditable Then
		Dim strPlaceholderName
		strPlaceholderName = pPlaceholder.Name

		If (pPlaceholder.XIsSingleLineText Or _
			pPlaceholder.XIsMultiLineText Or pPlaceholder.XIsMultipurpose) Then
			Dim strHTML
			strHTML = pFormReader.Form("NCPH_" & strPlaceholderName)
			If SetPhHTML( pPlaceholder, strHTML) Then
				g_bFiltered = True
			End If
		ElseIf pPlaceholder.XIsSingleImage Then
			If (pPlaceholder.XSetImage(pFormReader.Form("NCPH_" & strPlaceholderName), _
						   pFormReader.Form("NCPHAltText_" & strPlaceholderName), _
						   pFormReader.Form("NCPHHRef_" & strPlaceholderName))) Then
				g_bFiltered = True
			End If
		ElseIf pPlaceholder.XIsSingleAttachment Then
			If (pPlaceholder.XSetAttachment(pFormReader.Form("NCPH_" & strPlaceholderName), _
							pFormReader.Form("NCPHDispText_" & strPlaceholderName))) Then
				g_bFiltered = True
			End If
		ElseIf pPlaceholder.XIsSingleVideo Then
			If (pPlaceholder.XSetAttachment(pFormReader.Form("NCPH_" & strPlaceholderName))) Then
				g_bFiltered = True
			End If
		End If
	End If
End Sub

' ---- [Private] Sets placeholder html
Function SetPhHTML( pPlaceholder, strHTML )
	If pPlaceholder.XIsMultiPurpose Or _
		(pPlaceholder.XIsMultiLineText And _
		InternalShouldRenderRichEditPlaceholder( AutoSession.This, pPlaceholder ) And _
		ShouldRenderRichEditPlaceholder( AutoSession.This, pPlaceholder )) Then

		SetPhHTML = pPlaceholder.SetHTML(strHTML)
	Else
		SetPhHTML = pPlaceholder.SetHTML(strHTML, True)
	End If
End Function

%>
