/* 
----------------------------------------------------------
 ASP Session Management Methods
 Classic ASP facade to session Web Service
----------------------------------------------------------

*/

// Return new collection object for session
function NewSession()
{
	return(new ActiveXObject("Scripting.Dictionary"));
}

// Access web service using plain HTTP POST interface
function GetWebService(Function, Parameters)
{
	var xmlPayload = "";
	var sURL = "http://www.fullerdata.com/ASPBridge/bridge.asmx" + "/" + Function;

	//' server-side HTTP request with ASP.Net cookie
	var xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP");
	xmlHTTP.open("POST", sURL, false);	
	
	var clientCookie = Request.Cookies("ASP.Net_SessionId");
	var cookieSet = "" + clientCookie;

	xmlHTTP.setRequestHeader("cookie", "ASP.NET_SessionId=" + cookieSet + "; path=/;");
	xmlHTTP.setRequestHeader("cookie", "ASP.NET_SessionId=" + cookieSet + "; path=/;");
	xmlHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHTTP.send(Parameters);	
	var httpHeader = "" + xmlHTTP.getAllResponseHeaders();
	var httpCookie = getCookie("ASP.NET_SessionId", httpHeader);
	var httpBody = "" + xmlHTTP.responseText;
	xmlHTTP = null;
	
	/* uncomment to debug headers
	Response.Write("<hr>");
	Response.Write(httpCookie);
	Response.Write("<hr>");
	Response.Write(httpHeader);
	Response.Write("<hr>");
	*/
	
	if (httpCookie >"")
	{
		Response.Cookies("ASP.NET_SessionId") = httpCookie;		
	}
	
	xmlPayload = transformEscapedCharacters(httpBody);
	
	return(xmlPayload);
}

// Get a value from the ASP.Net session or BOSS/ICAF session
function GetSessionValue(SessionVariable)//, dctSession)
{
	var paramList = "sessionVariable=" + SessionVariable;
	var xmlPayload = GetWebService("getSessionValue", paramList);	
	return(xmlPayload);
}

// Set a value in the ASP.Net session or BOSS/ICAF session
function SetSessionValue(SessionVariable, SessionValue)//, dctSession)
{
	var paramList = "sessionVariable=" + SessionVariable + "&sessionValue=" + SessionValue;
	var xmlPayload = GetWebService("setSessionValue", paramList);	
	return(xmlPayload);
}

// Get a collection of values from the ASP.Net session
function XMLToDict(xmlPayload)
{
	// read named pairs into dictionary object
	var dctSession = new ActiveXObject("Scripting.Dictionary");
	var matchArray = null;
	var re = new RegExp("<SessionItem ", "g");
	var nEquals = 0;
	var nSlash = 0;
	var sessionName = "";
	var sessionValue = "";

	// (interpreted ASP / RegExp is slow) 
	while ((matchArray = re.exec(xmlPayload)) != null)
	{
		nEquals = matchArray.lastIndex;
		while (xmlPayload.charAt(nEquals) != "=") { nEquals++; }
		nSlash = nEquals;
		while (xmlPayload.charAt(nSlash) != "/") { nSlash++; }

		sessionName = xmlPayload.substring(matchArray.lastIndex, nEquals);
		sessionValue = xmlPayload.substring(nEquals + 2, nSlash - 2);	
		dctSession.Item(sessionName) = sessionValue;
	}

	return(dctSession);
}

function GetSession()
{
	var paramList = "";
	var xmlPayload = GetWebService("getSessionValues", paramList);
	return (XMLToDict(xmlPayload));
}

// Set a collection of values in the ASP.Net session
function DictToXML(SessionValues)
{
	var xmlSession = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
	xmlSession += "<Session GUID=\"";
	xmlSession += "";
	xmlSession += "\">";
	if (SessionValues != null)
	{
		var arraySession = (new VBArray(SessionValues.Keys())).toArray();
		var i;
		for (i in arraySession)
		{
			xmlSession += "<SessionItem " + arraySession[i] + "=\"" + SessionValues(arraySession[i]) + "\" />";
		}
	}
	xmlSession += "</Session>";
	return(xmlSession);
}

function SetSession(SessionValues)
{
	var xmlSession = DictToXML(SessionValues);		
	var paramList = "xmlSessionValues=" + xmlSession;
	var xmlPayload = GetWebService("setSessionValues", paramList);	
	return(xmlPayload);
}

// replace safe &; display characters with unsafe characters
function transformEscapedCharacters(src)
{
var dest;
var re;

	dest=src;
	re = new RegExp("&lt;", "g");
	dest = dest.replace(re, "<");
	re = new RegExp("&gt;", "g");
	dest = dest.replace(re, ">");
	re = new RegExp("&amp;", "g");
	dest = dest.replace(re, "&");
	re = new RegExp("&copy;", "g");
	dest = dest.replace(re, "©");
	re = new RegExp("&nbsp;", "g");
	dest = dest.replace(re, " ");

	return(dest);
}

// find cookie in header
function getCookie(CookieName, httpHeader)
{
	var result			= ""
	var httpSearch		= "";
	var reCookieName	= new RegExp(CookieName, "g");
	var reEquals		= new RegExp("=", "g");
	var reSemiColon		= new RegExp(";", "g");
	var i				= 0;

	httpSearch = httpHeader;
	i = httpSearch.search(reCookieName);
	if (i > 0)
	{
		httpSearch = httpSearch.substr(i);
		i = httpSearch.search(reEquals);
		if (i > 0)
		{
			httpSearch = httpSearch.substr(i+1);
			i = httpSearch.search(reSemiColon);
			if (i > 0)
			{
				result = httpSearch.substring(0, i);
			}
		}
	}
	
	return(result);
}

