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

' Add include files that should be added to any page including WBCSite.inc or
' 	WBCApp.inc (that should be all resolution enabled pages.


' ---- [Public] This function returns Content Connector query string params
Function CS_GetConnectorQueryStrings()
	Dim pQueryParm
	Dim strQueryString
	
	strQueryString = ""
	For Each pQueryParm in Request.QueryString
		If LCase(Left(pQueryParm, 3)) = "cs_" Then
			strQueryString = strQueryString & "&" & Server.URLEncode(pQueryParm) & "=" & Server.URLEncode(Request.QueryString(pQueryParm))
		End If
	Next
	
	CS_GetConnectorQueryStrings = strQueryString
	
End Function

'	[Public] This function adds a querystring to a url. The querystring is in the form of
'	key1=value1&key2=value2... It takes into account the url may already have the querystring.
Function CS_AppendQueryStringToURL(strURL, strQueryString)

	If strURL = "" Or strURL = IDS_NO_URL Then
		CS_AppendQueryStringToURL = IDS_NO_URL
		Exit Function
	End If
	
	If strQueryString = "" Then
		CS_AppendQueryStringToURL = strURL
		Exit Function
	End If
	
	Dim arrayQueryKeyValue
	arrayQueryKeyValue = Split(strQueryString, "&")

	Dim strTempURL
	Dim arrayQuery
	Dim ele
	
	strTempURL = CS_StripOffStagingTags(strURL)
	For Each ele in arrayQueryKeyValue
		If Instr(ele, "=") Then
			arrayQuery = Split(ele, "=")
			strTempURL = CS_AppendQueryKeyValueToURLNoStaging(strTempURL, arrayQuery(0), arrayQuery(1))
		End If
	Next
	
	CS_AppendQueryStringToURL = AutoSession.ResolveURL(strTempURL)
	
End Function

'	[Private] This function adds a query key-value pair to a url. It takes into account the url may
'	already have the query key-value pair. strQueryValue should already be URLEncoded. This
'	function differs from CS_AppendQueryKeyValueToURL in that there is no stripping of staging tags.
Function CS_AppendQueryKeyValueToURLNoStaging(strURL, strQueryKey, strQueryValue)

	Dim arrayURL
	Dim strLocation
	Dim strQueryString
	Dim nPosQuery
	Dim nPosQueryValueStart
	Dim nPosQueryValueEnd

	'	Extracts the entire querystring portion of the url
	arrayURL = Split(strURL, "?")
	strLocation = arrayURL(0)

	If UBound(arrayURL) = 0 Then
		CS_AppendQueryKeyValueToURLNoStaging = strLocation & "?" & strQueryKey & "=" & strQueryValue
		Exit Function
	End If

	strQueryString = arrayURL(1)
	nPosQuery = InStr(strQueryString, strQueryKey)

	'	If url does not the requested querystring, append it to the url
	If nPosQuery = 0 Then
		CS_AppendQueryKeyValueToURLNoStaging = strLocation & "?" & strQueryString & "&" & strQueryKey & "=" & strQueryValue
		Exit Function
	End If

	'	Otherwise, updates the querystring value
	nPosQueryValueStart = nPosQuery + Len(strQueryKey) + 1
	nPosQueryValueEnd = InStr(nPosQueryValueStart, strQueryString, "&") - 1
	If nPosQueryValueEnd = -1 Then
		nPosQueryValueEnd = Len(strQueryString)
	End If

	CS_AppendQueryKeyValueToURLNoStaging = strLocation & "?" & mid(strQueryString, 1, nPosQueryValueStart - 1) & strQueryValue & mid(strQueryString, nPosQueryValueEnd + 1)

End Function

'	[Public] This function adds a query key-value pair to a url. It takes into account the url may
'	already have the query key-value pair. strQueryValue should already be URLEncoded. This
'	function differs from CS_AppendQueryKeyValueToURLNoStaging in that staging tags are stripped.
Function CS_AppendQueryKeyValueToURL(strURL, strQueryKey, strQueryValue)

	Dim strNewURL
	strNewURL = CS_AppendQueryKeyValueToURLNoStaging(CS_StripOffStagingTags(strURL), strQueryKey, strQueryValue)
	CS_AppendQueryKeyValueToURL = AutoSession.ResolveURL(strNewURL)

End Function

'	[Public] This function removes <NCOMPASSSTAGINGSERVER> tags from urls. These tags appear
'	in staging mode. This function is most commonly used to remove
'	staging tags from redirection URLs in Staging mode.
Function CS_StripOffStagingTags(strUrl)
	CS_StripOffStagingTags = Replace(Replace(strUrl, "<NCOMPASSSTAGINGSERVER>", ""), "</NCOMPASSSTAGINGSERVER>", "")
End Function

' ---- [Public] Handle proper propagation of Content Connector query string params
Sub CS_PropagateQueryStrings()
	Dim pQueryParm
	For Each pQueryParm in Request.QueryString
		If LCase(Left(pQueryParm, 3)) = "cs_" Then
			Call AutoSession.PropagateParameter(Server.URLEncode(pQueryParm), Server.URLEncode(Request.QueryString(pQueryParm)))
		End If
	Next
End Sub

' ---- [Public] Reset propagation of Content Connector query string params
Sub CS_ResetQueryStrings()
	Dim pQueryParm
	For Each pQueryParm in Request.QueryString
		If LCase(Left(pQueryParm, 3)) = "cs_" Then
			Call AutoSession.PropagateParameter(Server.URLEncode(pQueryParm), "")
		End If
	Next
End Sub

%>