function needSessionIdInURL() {
	if (!document.cookie) {
		return true;
	}
	if (document.cookie.lastIndexOf("jsessionid")<0) {
		return true;
	}
	return false;
}

if (needSessionIdInURL()) {
	$(document).ready(addSessionID);
}

function addSessionID () {
	//alert("addSessionID");
	var atags = document.getElementsByTagName("A");
	for(var index=0;index<atags.length;index++) {
		var aTag = atags[index];
		var href = aTag.href;
		// alert("href = " + href);
	}
	$("a").not("[href*=jsessionid]").not("[href^=javascript]").not("[href*=pdf]").each(function(){
		if (
			((this.href.indexOf("http://")==-1) && (this.href.indexOf("https://")==-1))
			|| ((this.href.indexOf("http://"+window.location.hostname+"/")>-1) || (this.href.indexOf("https://"+window.location.hostname+"/")>-1))) {
			this.href=addSessionIDToUrl(this.href);
		}
	});
	$("*[onclick]").each(function(){
		try {
			clickAttr = $(this).attr("onclick");
			
			if (clickAttr.indexOf(".location.href")>-1){
				var newClickAttr;
				if (clickAttr.indexOf("http://")==-1 && clickAttr.indexOf("https://")==-1 && clickAttr.indexOf("jsessionid")==-1) {
					var pos = clickAttr.lastIndexOf("'");
					if (pos > -1) {
						var sub1 = clickAttr.substring(0,pos);
						var sub2 = clickAttr.substring(pos);
						newClickAttr = sub1 + sessionIdInfix + sub2;
					} else {
						newClickAttr = clickAttr + sessionIdInfix;
					}
					$(this).attr("onclick",newClickAttr);
				} 
			}
		} catch (e) {
			clickAttr = $(this).html();
			
			if (clickAttr.indexOf(".location.href")>-1){
				var newClickAttr;
				if (clickAttr.indexOf("http://")==-1 && clickAttr.indexOf("https://")==-1 && clickAttr.indexOf("jsessionid")==-1) {
					var pos = clickAttr.lastIndexOf("'");
					if (pos > -1) {
						var sub1 = clickAttr.substring(0,pos);
						var sub2 = clickAttr.substring(pos);
						newClickAttr = sub1 + sessionIdInfix + sub2;
					} else {
						newClickAttr = clickAttr + sessionIdInfix;
					}
					$(this).html(newClickAttr);
				} 
			}
		}
		
	});
	$("form[action]").not("[action*=jsessionid]").not("[action^=javascript]").each(function(){
		this.action=addSessionIDToUrl(this.action);
		//alert(this.action);
	});
}

function addSessionIDToUrl(url) {
	if (url.indexOf(sessionIdInfix)<0) {
		var pos = url.lastIndexOf("?");
		if (pos > -1) {
			var sub1 = url.substring(0,pos);
			var sub2 = url.substring(pos);
			url = sub1 + sessionIdInfix + sub2;
		} else {
			url = url + sessionIdInfix;
		}
	}
	return url;
}
