	function config() {
		this.tokName = "SNAuthToken";
		this.targetWin = "_blank";
		this.formName = "SSONForm";
		this.tokAccessURL = "/JHPortal/Services/tokenFromSession/";
	}

	function getToken() {
		if (!ssonConf) return;

		if (!window.XMLHttpRequest) {
			window.XMLHttpRequest = function () {
				return new ActiveXObject('Microsoft.XMLHTTP');
			}
		}
		var req = new XMLHttpRequest();
		try {
			req.open("POST", ssonConf.tokAccessURL, false);
			req.send("");
		} catch (e) {
			errMsg =
			"There was a problem displaying this page on your browser.\n"+
			"Please note that we only support Internet Explorer 6 and up.\n"
			alert(errMsg + "\n" + e.name + ": " + e.message);
			return false;
		}
		
		res = req.responseText;
		tagName = ssonConf.tokName;
		token = res.substring(res.indexOf(tagName)-1, res.lastIndexOf(tagName)+tagName.length+1);
		return token;
	}
	
	function doSson(url) {
		if (!ssonConf) return;

		var token = getToken();
		if (!token) return false;

		var ssonForm = document.getElementById(ssonConf.formName);
		if (!ssonForm) {
			ssonForm = document.createElement("form");
			ssonForm.setAttribute("name", ssonConf.formName);
			ssonForm.setAttribute("id", ssonConf.formName);
			ssonForm.setAttribute("method", "post");
			ssonForm.setAttribute("target", ssonConf.targetWin);

			authToken = document.createElement("input");
			authToken.setAttribute("name", ssonConf.tokName);
			authToken.setAttribute("type", "hidden");
			authToken.setAttribute("value", token);
			try {
				ssonForm.appendChild(authToken);
				body = document.getElementsByTagName("body").item(0);
				body.appendChild(ssonForm);
			} catch (e) {
				errMsg =
				"There was a problem displaying this page on your browser.\n"+
				"Please note that we only support Internet Explorer 6 and up.\n"
				alert(errMsg + "\n" + e.name + ": " + e.message);
				return false;
			}
		}
		if (ssonForm) {
			var actionURL = url;
			if (url.indexOf('JHPortal') > -1) {
				/*
				 * For SalesNet and MGroup there is a dedicated template
				 * to process the token authentication; see tokAuthURL
				 * This temlate uses 'gotoURL' parameter to transfer to
				 * the final destination
				 */
				var tokAuthURL = "/JHPortal/Common/Login/authTokenLogin/";
				actionURL = url.replace(/\?.*/, '');
				actionURL = actionURL.replace(/\/JHPortal.*\//, tokAuthURL);

				/*
				 * Redirect to the original url after the token authentication
				 * Strip off the protocol:host:port portion of the url
				 * Needed for SN redirect to work properly
				 */
				urlPath = url.substr(url.indexOf('/', url.indexOf('//')+2));
				var gotoName = "gotoURL";
				gotoURL = eval("ssonForm."+gotoName);
				if (! gotoURL) {
					gotoURL = document.createElement("input");
					gotoURL.setAttribute("name", gotoName);
					gotoURL.setAttribute("type", "hidden");
					ssonForm.appendChild(gotoURL);
				}
				gotoURL.value = urlPath;
			}
			ssonForm.setAttribute("action", actionURL);
			ssonForm.submit();
		}
	}

	var ssonConf = new config();
