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

' -- Boolean to keep track of whether EmitRichPh has been called
Dim g_bScriptGenerated
g_bScriptGenerated = False

' -- Boolean to keep track of whether page is emitting DHTML placeholder.
Dim g_bEmitForDHTMLPlaceholder
g_bEmitForDHTMLPlaceholder = False
' ---- [Public] Emitting DHTML placeholder for IE clients
Sub EmitRichPh( pThisPosting, PlaceholderEmitter )

	If Not g_bScriptGenerated Then
%>
		<script language="JavaScript">
		<!--
		function WBC_setThinEditIEImage(strPhName, strURL, strHRef, strAltText) {
			var strMultiPurposeTag;
			if ( strAltText != "" ) {
				strMultiPurposeTag = "<img src=\"" + strURL + "\" border=\"0\" alt=\"" + strAltText + "\">";
			} else {
				strMultiPurposeTag = "<img src=\"" + strURL + "\" border=\"0\">";
			}
			if ((strHRef != "") && (strHRef != IDS_HTTP_PROTOCOL)) {
				strMultiPurposeTag = "<a href=\"" + strHRef + "\">" + strMultiPurposeTag + "</a>";
			}
			document.all["NCPHRICH_" + strPhName].setFocusOnPlaceholder();
			document.all["NCPHRICH_" + strPhName].insertHtml(strMultiPurposeTag);
		}

		function WBC_setThinEditIEAttachment(strPhName, strURL, strDispText) {
			var strMyDispText = strDispText;
			if (strMyDispText == "") {
				strMyDispText = strURL;
			}
			document.all["NCPHRICH_" + strPhName].setFocusOnPlaceholder();
			document.all["NCPHRICH_" + strPhName].insertHtml("<a href=\"" + strURL + "\">" + strMyDispText + "</a>");
		}

		function WBC_setThinEditIEVideo(strPhName, strURL, bImgTag) {
			document.all["NCPHRICH_" + strPhName].setFocusOnPlaceholder();
			if (bImgTag) {
				document.all["NCPHRICH_" + strPhName].insertHtml("<img dynsrc=\"" + strURL + "\">");
			} else {
				document.all["NCPHRICH_" + strPhName].insertHtml("<embed src=\"" + strURL + "\" loop=\"false\" autoplay=\"false\" controller=\"true\">");
			}
		}

		function WBC_setThinEditIETable(strPhName, strTableTag) {
			document.all["NCPHRICH_" + strPhName].setFocusOnPlaceholder();
			document.all["NCPHRICH_" + strPhName].insertHtml(strTableTag);
		}
		//-->
		</script>
<%
		g_bScriptGenerated = True
		g_bEmitForDHTMLPlaceholder = True
	End If

%>
<input
	name="NCPH_<%=PlaceholderEmitter.pPlaceholder.Name%>"
	type="hidden"
	placeholderType="dhtml"
	value="<%= GetFilteredPhHTML(PlaceholderEmitter.pPlaceholder) %>">
</input>
<%
	Dim nPlaceholderWidth
	Dim nPlaceholderHeight

	' minimum placeholder width is 450
	nPlaceholderWidth = PlaceholderEmitter.Width
	if nPlaceholderWidth < 450 then
		nPlaceholderWidth = 450
	end if

	' this little piece of code figures out the font size and estimates the height
	' of the placeholder accordingly

	dim nPos, nNextPos
	dim strStyle, nScaler, strPoints

	strStyle = PlaceholderEmitter.Style
	nScaler = 12

	if len(strStyle) > 0 then
		nPos = instr(strStyle, "font-size: ")
		if nPos > 0 then
			nNextPos = instr(nPos, strStyle, "pt")
			if nNextPos > nPos then
				nPos = nPos + 11 ' skip the font-size
				strPoints = mid(strStyle, nPos, nNextPos-nPos)
				if IsNumeric(strPoints) then
					nScaler = CLng(strPoints)
				end if
			end if
		end if
	end if

	nPlaceholderHeight = CLng(PlaceholderEmitter.Rows * (nScaler*1.8) + 55 + 21) ' estimate 12 pixels per row and 55 pixel toolbar

	' limit the placeholder height to 500
	if nPlaceholderHeight > 500 then
		nPlaceholderHeight = 500
	end if
%>
<object
	data="/NR/System/WBC/Internals/PageTemplate/placeholdersSupport/DhtmlPlaceholder/deditor.asp?width=<%=nPlaceholderWidth%>&height=<%=nPlaceholderHeight%>"
	id="NCPHRICH_<%=PlaceholderEmitter.pPlaceholder.Name%>"
	type="text/x-scriptlet"
	width="<%=nPlaceholderWidth%>"
	height="<%=nPlaceholderHeight%>">
</object>

<SCRIPT language="JavaScript">
<!--
<%
	' Dynamically generate function name for client side script to call
	' during Window_onload()
	
	Dim strFunctionNamePostfix
	strFunctionNamePostfix = Replace(PlaceholderEmitter.pPlaceholder.Name, " ", "_")
%>
	function WBC_populatePlaceHolder<%= strFunctionNamePostfix %>()
	{
		var edt = document.all["NCPHRICH_<%=PlaceholderEmitter.pPlaceholder.Name%>"];
		edt.cssText = '<%= strStyle %>';
		edt.html = document.all["NCPH_<%=PlaceholderEmitter.pPlaceholder.Name%>"].value;

		var g_bAllowsHeadings = <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.AllowsHeadings) %>;
		var g_bAllowsTextMarkUp = <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.AllowsTextMarkUp) %>;
		var g_bAllowsFonts = <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.AllowsFonts) %>;
		var g_bAllowsFlow= <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.AllowsFlow) %>;
		var g_bAllowsFormattedLists = <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.AllowsFormattedLists) %>;
		var g_bAllowsImages = <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.AllowsImages) %>;
		var g_bAllowsVideos = <%= Bool2LCaseStr(AllowsVideos(PlaceholderEmitter.pPlaceholder)) %>;
		var g_bAllowsAttachments = <%= Bool2LCaseStr(AllowsAttachments(PlaceholderEmitter.pPlaceholder)) %>;
		var g_bIsSingleVideo = <%= Bool2LCaseStr(IsSingleVideo(PlaceholderEmitter.pPlaceholder)) %>;
		var g_bAllowsHyperlinks = <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.AllowsHyperlinks) %>;
        var g_bAllowsHtmlSrcEditing = <%= Bool2LCaseStr(PlaceholderEmitter.HtmlSrcCodeEditable) %>;

		//
		//	configure toolbar buttons on 1st row
		//

		if (!g_bAllowsHeadings) {
			edt.disableDropList('ParagraphStyle');
		}
		if (!g_bAllowsFonts) {
			edt.disableDropList('FontName');
			edt.disableDropList('FontSize');
			edt.disableButton('FgColor');
			edt.disableButton('BgColor');
		}
		if (!g_bAllowsImages) {
			edt.disableButton('Image');
		}
		if (!g_bAllowsVideos) {
	   		edt.disableButton('Video');
		}
		if (!g_bAllowsAttachments) {
			edt.disableButton('Attachment');
		}
		if (!g_bAllowsHyperlinks) {
			edt.disableButton('Hlink');
		}
        if (!g_bAllowsHtmlSrcEditing) {
            edt.disableHtmlEditing();
        }

		//
		//	configure toolbar buttons on 2nd row
		//

		//	edt.disableButton('Cut');
		//	edt.disableButton('Copy');
		//	edt.disableButton('Paste');

		if (WBC_getBrowserVer() < 5) {
			edt.disableButton('Undo');
			edt.disableButton('Redo');
		}
		if (!g_bAllowsTextMarkUp) {
			edt.disableButton('Bold');
			edt.disableButton('Italic');
			edt.disableButton('Underline');
		}
		if (!g_bAllowsFlow) {
			edt.disableButton('AlignLeft');
			edt.disableButton('AlignCenter');
			edt.disableButton('AlignRight');
			edt.disableButton('DecIndent');
			edt.disableButton('IncIndent');
		}
		if (!g_bAllowsFormattedLists) {
			edt.disableButton('Table');
			edt.disableButton('NumList');
			edt.disableButton('BulList');
		}

		// Hook up event handler
		edt.setEventHandler();
	}
//-->
</SCRIPT>

<SCRIPT language="JavaScript" for="NCPHRICH_<%=PlaceholderEmitter.pPlaceholder.Name%>" event="onscriptletevent(name, data)">
<!--
	if (name == "onShowImgGalleryClick") {
		WBC_launchImageGallery("<%=AutoSession.ThisPosting.QueryString%>", "<%= PlaceholderEmitter.pPlaceholder.Name %>", "<%=IDS_PHTYPE_THINEDITIE%>", <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.XAllowsImageUpload) %>);
		return false;
	}
	else if (name == "onShowAttachmentGalleryClick") {
		WBC_launchAttachmentGallery("<%=AutoSession.ThisPosting.QueryString%>", "<%=PlaceholderEmitter.pPlaceholder.Name%>", "<%=IDS_PHTYPE_THINEDITIE%>", <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.XAllowsAttachmentUpload) %>, <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.XAttachmentsAsIcons) %>, false);
		return false;
	}
	else if (name == "onShowVideoGalleryClick") {
		WBC_launchVideoGallery("<%=AutoSession.ThisPosting.QueryString%>", "<%=PlaceholderEmitter.pPlaceholder.Name%>", "<%=IDS_PHTYPE_THINEDITIE%>", <%= Bool2LCaseStr(PlaceholderEmitter.pPlaceholder.XAllowsAttachmentUpload) %>, <%= Bool2LCaseStr( LCase(PlaceholderEmitter.pPlaceholder.XEmbeddedVideoElement) = "img") %>);
		return false;
	}
	else if (name == "onCreateTableClick") {

		// show table dialog
		var strTableURL = IDS_FRAMEWORK_VIRTUAL_PATH + "/Internals/WBCFuncPages/Cross-Browser/Table/Table.asp?" + "<%=AutoSession.ThisPosting.QueryString%>" + "&" + IDS_QPARAM_PHNAME + "=" + "<%= Server.URLEncode(PlaceholderEmitter.pPlaceholder.Name) %>" + "&" + IDS_QPARAM_PHTYPE + "=" +  IDS_PHTYPE_THINEDITIE;
		var strDlgRet = window.showModalDialog(strTableURL, "", "dialogWidth:295px;dialogHeight:190px;help:no;status:no");
		if (typeof(strDlgRet) == "undefined") return false;
		if (strDlgRet == "Cancel") return false;

		var aAttributes = strDlgRet.split(";");
		WBC_setTable("<%=PlaceholderEmitter.pPlaceholder.Name%>", "<%=IDS_PHTYPE_THINEDITIE%>", aAttributes[0], aAttributes[1], aAttributes[2]);
		return false;
	}

	// didn't handle it.
	return true;
//-->
</SCRIPT>
<%
End Sub
' ---- [Public] Emit client-side Window_OnLoad() handler code when using DHTML placeholder
Sub EmitWindowOnLoadForDHTMLPlaceholder()
%>
		<SCRIPT language="VBScript">
		' ---- [Private] To inject content to DHTML placeholder upon loading of page
		Sub Window_onLoad()

			' make sure we have a form before proceeding			
			Dim frmEditForm
			Set frmEditForm = document.NC_EditForm	
			If (frmEditForm Is Nothing) Then
				Exit Sub
			End If
			
			Dim nPhIndex
			Dim nPlaceHolderCount
			Dim strPlaceHolderName
			Dim strInputFieldForPhName
			Dim strFunctionName
			Dim strPlaceHolderType
			Dim strPlaceHolderNameWithoutSpace

			' Loop through the collection of placeholders and move content from
			' hidden input field to DHTML scriptlet for DHTML placeholders
			nPlaceHolderCount = frmEditForm.NCPHCount.value
			For nPhIndex = 1 To nPlaceHolderCount
		
				' 1st we look for the value of the placeholder name
				' from hidden fields named as "NCPHNAME_n" where n is an index
				strInputFieldForPhName = IDS_NCPHNAME + "_" + CStr(nPhIndex) 
		
				' Next we get the actual placeholder name
				' This should be something like NCPH_MyName
				strPlaceHolderName = IDS_NCPH_ + frmEditForm.elements(strInputFieldForPhName).value
		
				' We then check to make sure this is a DHTML placeholder
				strPlaceHolderType = frmEditForm.elements(strPlaceHolderName).getAttribute(IDS_PLACEHOLDER_TYPE)
				
				If strPlaceHolderType = IDS_PLACEHOLDER_DHTML Then
								
					' For each DHTML placeholder in the form,  replace the " " in the name with a "_" and attach a 
					' prefix "populatePlaceHolder" to it. So for a placeholdername "NCPH_Home Address",
					' we will create a function call "populatePlaceHolderNCPH_Home_Address". 
					' We then call this function which should have been generated on the server 
					' side after placeholder creation to fill up the placeholder data
										
					strPlaceHolderNameWithoutSpace = Mid(Replace(strPlaceHolderName, " ", "_"),6)
					strFunctionName = "WBC_populatePlaceHolder" + strPlaceHolderNameWithoutSpace + "()"
					Execute strFunctionName
					
				End If
					
			Next	
		End Sub
		</SCRIPT>
<%	
End Sub
%>	
