function append_promo(strTag,strPromo) {
	return((strPromo=='')?strTag:strTag+'_promo_'+strPromo);
}

function normalize_url1(strURL,strSourceSiteID) {
	var retval = '';
	var temp;
	var siteID;
	var ary;
	var promo='';
	var promoFirstParam=0;
	// Get the value of the promo tag if one was passed, then remove the parameter from strURL.
	if (strURL.search(/.*\?promo\=.*/i) != -1) {
		strURL = strURL.replace(/\?promo\=/i, '&promo=');
		promoFirstParam=1;
	}
	strURL = strURL.replace(/\&promo\=/i, '&promo='); // handle uppercase etc.
	if (strURL.indexOf("&promo=") != -1) {
		var promostart = strURL.indexOf("&promo=");
		var promoend = strURL.indexOf("&",promostart+1);
		if (promoend == -1) {
			if (strURL.length > promostart + 7) {
			   promo = strURL.substring(promostart+7, strURL.length);
			}
			strURL = strURL.substring(0,promostart);
		} else {
			if (promoend > promostart + 7) {
			   promo = strURL.substring(promostart+7, promoend);
			}
			if (promoFirstParam==0) {
				strURL = strURL.substring(0,promostart) + strURL.substring(promoend,strURL.length);
			} else {
				strURL = strURL.substring(0,promostart) + '?' + strURL.substring(promoend+1,strURL.length);
			}
		}
	}

	// Change XML format forms URLs into normal Framework URLs.
	if (strURL.search(/.*\/home\/WebformServlet\?.*/) != -1) {
		strURL = strURL.replace(/\/home\/WebformServlet\?/, '/home/Framework?');
		strURL = strURL.replace(/\.xml/, '.html');
	}

	// If the plain page servlet has been used, change the URL into that of a normal webserver URL.
	if(strURL.search(/.*\/home\/PlainPageServlet\?FC=/i) != -1) {
		strURL = strURL.replace(/\/home\/PlainPageServlet\?FC=/, '/home');
	}

	// Deal with portal page.
	if(strURL.search(/.*\/home\/Framework$/i) != -1) {
		return '/topsite/local/welcome.html';
	} else {

		// Deal with framework.
		if(strURL.search(/.*\/home\/Framework\?siteId=([^&]+)(.*)/i) != -1) {
			ary = strURL.match(/.*\/home\/Framework\?siteId=([^&]+)(.*)/i);
			if (ary != null) {
				siteID = ary[1];
				if (ary[2] != '') {
					//chop off the leading &
					temp = ary[2].slice(1);
					ary = temp.match(/FC3=([^&]+)/i);
					if (ary != null) {
						temp = ary[1];

						// remove any leading /home
						temp = temp.replace(/^\/home\//i, '');

						if (temp.search(/^\/.*$/i) != -1) {
							temp = ary[1].slice(1);
						}
						// At this point, temp contains the FC3 without the leading /
						if (temp.substring(0,7)=='global/') {
							temp = '/' + ((siteID == 'home')?'topsite':siteID) + '/' + temp;
							return append_promo(temp,promo);
						} else if (temp.substring(0,11)=='html/iwgen/') {
							return append_promo('/topsite/local/' + temp.substring(11),promo);
						} else if (temp.substring(0,9)=='tailored/') {
							return append_promo('/topsite/' + temp,promo);
						} else {
							// Remove the siteid from start of string.
							temp = temp.substring(temp.indexOf('/') + 1);
							if (temp.substring(0,11)=='html/iwgen/') {
								return append_promo('/' + siteID + '/local/' + temp.substring(11),promo);
							} else if (temp.substring(0,9)=='tailored/') {
								return append_promo('/' + siteID + '/' + temp,promo);
							} else {
								// This content is non-standard.
								return (siteID + '/' + temp);
							}
						}
					}
				} else {
					// No FC3 so this is a welcome page.
					retval = '/' + ((siteID == 'home')?'topsite':siteID) + '/local/welcome.html';
					return append_promo(retval,promo);
				}
			}
		}
	}

	if (retval == '') {
		if(strURL.search(/^\/home\/ExitPage\?URL=.*/i) != -1) {
			ary = strURL.match(/^\/home\/ExitPage\?URL=(.*)/i);
			if (ary != null) {
				retval = ary[1];
			}
		}
	}

	// Deal with framesets.
	if (retval == '') {
		if((strURL.search(/.*\/\/www.shell.com\/home\/.*/i) != -1)||(strURL.search(/.*\/\/www.shell.com\/static\/.*/i) != -1)) {
			ary = strURL.match(/.*\/\/www.shell.com\/(.*)/i);
			if (ary != null) {
				temp = ary[1];

				if (temp.substring(0,5)=='home/') {
					temp = temp.substring(5);
				} else {
			 		temp = temp.substring(7);
				}

				if (temp.substring(0,7)=='global/') {
					retval=((strSourceSiteID=='')?'/home/':('/' + strSourceSiteID + '/')) + temp;
					return append_promo(retval,promo);
				} else if (temp.substring(0,11)=='html/iwgen/') {
					return append_promo('/topsite/local/' + temp.substring(11),promo);
				} else if (temp.substring(0,5)=='html/') {
					return append_promo('/topsite/local/' + temp.substring(5),promo);
				} else if (temp.substring(0,9)=='tailored/') {
					return append_promo('/topsite/' + temp,promo);
				} else if (temp.substring(0,10)=='downloads/') {
					return append_promo('/topsite/local/' + temp,promo);
				} else {
					// Get the siteid from start of string then remove it.
					siteID = temp.substring(0,temp.indexOf('/'));
					temp = temp.substring(temp.indexOf('/') + 1);
					if (temp.substring(0,11)=='html/iwgen/') {
						return append_promo('/' + siteID + '/local/' + temp.substring(11),promo)
					} else if (temp.substring(0,5)=='html/') {
						return append_promo('/' + siteID + '/local/' + temp.substring(5),promo)
					} else if (temp.substring(0,9)=='tailored/') {
						return append_promo('/' + siteID + '/' + temp,promo)
					} else if (temp.substring(0,10)=='downloads/') {
						return append_promo('/' + siteID + '/local/' + temp,promo)
					} else {
						// This content is non-standard.
						return (temp);
					}
				}
			}
		}
	}

	// Deal with shortened URLs.
	if (retval == '') {
		if((strURL.search(/^\/home\/.*/i) != -1)||(strURL.search(/^\/static\/.*/i) != -1)) {
			temp = strURL.substring(1);
			if (temp.substring(0,5)=='home/') {
				temp = temp.substring(5);
			} else {
				temp = temp.substring(7);
			}
			if (temp.substring(0,7)=='global/') {
				return ((strSourceSiteID=='')?'/home/':('/' + strSourceSiteID + '/')) + temp;
			} else if (temp.substring(0,11)=='html/iwgen/') {
				return ('/topsite/local/' + temp.substring(11));
			} else if (temp.substring(0,5)=='html/') {
				return ('/topsite/local/' + temp.substring(5));
			} else if (temp.substring(0,9)=='tailored/') {
				return ('/topsite/' + temp);
			} else if (temp.substring(0,10)=='downloads/') {
				return ('/topsite/local/' + temp);
			} else {
				// Get the siteid from start of string then remove it.
				siteID = temp.substring(0,temp.indexOf('/'));
				temp = temp.substring(temp.indexOf('/') + 1);
				if (temp.substring(0,11)=='html/iwgen/') {
					return ('/' + siteID + '/local/' + temp.substring(11))
				} else if (temp.substring(0,5)=='html/') {
					return ('/' + siteID + '/local/' + temp.substring(5))
				} else if (temp.substring(0,9)=='tailored/') {
					return ('/' + siteID + '/' + temp)
				} else if (temp.substring(0,10)=='downloads/') {
					return ('/' + siteID + '/local/' + temp)
				} else {
					// This content is non-standard.
					return(temp);
				}
			}
		}
	}
	return((retval=='')?strURL:retval);
}

function unsimplify_url(strURL) {
	// Transform simplified URLs into normal ones. See 'Metrics URL simplification' document.
	var slash1;
	if (strURL.indexOf('//') == strURL.indexOf('/')) {
		slash1 = strURL.indexOf('/', strURL.indexOf('//')+4);
	} else {
		slash1 = strURL.indexOf('/');
	}
	var params = '';
	var promo = '';
	if (strURL.indexOf('?') != -1) {
		params = '&' + strURL.substr(strURL.indexOf('?')+1);
		strURL = strURL.substring(0,strURL.indexOf('?'));
		if (params.indexOf('&promo=') != -1) {
			promo=params.substr(params.indexOf('&promo='));
			if (promo.indexOf('&',1) != -1) { promo = promo.substring(0,promo.indexOf('&',1)); }
		}
	}
	var slash2 = strURL.indexOf('/', slash1+1);
	var slash3 = strURL.indexOf('/', slash2+1);
	var slash4 = strURL.indexOf('/', slash3+1);
	var slash5 = strURL.indexOf('/', slash4+1);
	var servlet = strURL.substring(slash2+1,slash3);
	var param1 = '';
	var param2 = '';
	var path = '';
	var contenttype = '';
	if (slash4 != -1) {
		param1 = strURL.substring(slash3+1,slash4);
		if (slash5 != -1) {
			param2 = strURL.substring(slash4+1,slash5);
		} else {
			param2 = strURL.substr(slash4+1);
		}
	} else {
		param1 = strURL.substr(slash3+1);
	}
	if (servlet == 'content3') {
		path = (slash5 != -1 ? strURL.substr(slash5) : '');
		if (path.indexOf('/tailored/') == 0) {
			contenttype = 'locally_shared_tailored';
			path = path.substr(9);
		} else {
			contenttype = 'locally_shared_local';
		}
	} else {
		path = (slash4 != -1 ? strURL.substr(slash4) : '');
		if (path.indexOf('/global/') == 0) {
			contenttype = (servlet == 'page' ? 'page_global' : 'global');
			path = path.substr(7);
		} else if (path.indexOf('/tailored/') == 0) {
			contenttype = (servlet == 'page' ? 'page_tailored' : 'tailored');
			path = path.substr(9);
		} else {
			contenttype = (servlet == 'page' ? 'page_local' : 'local');
		}
	}

	if (path == '' || path == '/') {
		path = '/welcome.html';
	} else if (path.charAt(path.length-1) == '/') {
		path = path + 'index.html';
	} else if (path.lastIndexOf('.') < path.lastIndexOf('/')) {
		path = path + '/index.html';
	}

	if (contenttype == 'local') {
		strURL = 'http://www.shell.com/home/Framework?siteId='+param1+'&FC3=/'+param1+'/html/iwgen'+path+promo;
	} else if (contenttype == 'global') {
		strURL = 'http://www.shell.com/home/Framework?siteId='+param1+'&FC3=/global'+path+promo;
	} else if (contenttype == 'tailored') {
		strURL = 'http://www.shell.com/home/Framework?siteId='+param1+'&FC3=/'+param1+'/tailored'+path+promo;
	} else if (contenttype == 'locally_shared_local') {
		strURL = 'http://www.shell.com/home/Framework?siteId='+param1+'&FC3=/'+param2+'/html/iwgen'+path+promo;
	} else if (contenttype == 'locally_shared_tailored') {
		strURL = 'http://www.shell.com/home/Framework?siteId='+param1+'&FC3=/'+param2+'/tailored'+path+promo;
	} else if (contenttype == 'page_local') {
		strURL = 'http://www.shell.com/home/PlainPageServlet?FC=/'+param1+'/html/iwgen'+path+promo;
	} else if (contenttype == 'page_global') {
		strURL = 'http://www.shell.com/home/PlainPageServlet?FC=/global'+path+promo;
	} else if (contenttype == 'page_tailored') {
		strURL = 'http://www.shell.com/home/PlainPageServlet?FC=/'+param1+'/tailored'+path+promo;
	}
	if (param1 == 'top') {
		strURL = strURL.replace('siteId=top','siteId=home');
		strURL = strURL.replace('&FC3=/top/','&FC3=/');
		strURL = strURL.replace('FC=/top/','FC=/');
	}
	if (servlet == 'form') {
		strURL = strURL.replace('/home/Framework','/home/WebformServlet');
	}
	return strURL;
}

function normalize_url(strURL, strSourceSiteID) {
	if (strURL == 'http://www.shell.com' || strURL == 'http://www.shell.com/' || strURL == 'www.shell.com' || strURL == 'www.shell.com/') {
		return '/topsite/local/welcome.html';
	}

	// If this isn't a relative URL...
	if (strURL.charAt(0) != '/') {
		// ...then replace any domain names used by oneshell sites with www.shell.com. This makes the pattern
		// matching in normalize_url1 work for these domains.
		if (strURL.search(/.*\/\/(www.nam.nl|sww-shellreport.shell.com|www.shell.ca|www-accept.shell.com)\/.*/i) != -1) {
			strURL = strURL.replace(/\/\/(www.nam.nl|sww-shellreport.shell.com|www.shell.ca|www-accept.shell.com)\//i, '//www.shell.com/');
		} else {
			if (strURL.search(/.*\/\/www.shell.com\/.*/i) == -1) {
				// Return blank as this isn't an internal URL, so we don't want to override it.
				return '';
			}
		}
	}

	if (strURL.search(/.*\/home\/PrintFramework\?siteId=([^&]+)(.*)/i) != -1) {
		return (strURL.replace(/\/home\/PrintFramework/i, '/home/Framework'), strSourceSiteID) + '_print';
	}
	var firstSlash;
	if (strURL.indexOf('://') == -1) {
		firstSlash=strURL.indexOf('/');
	} else {
		firstSlash=strURL.indexOf('/',strURL.indexOf('://')+4);
	}
	if (strURL.indexOf('/home/content/')==firstSlash||strURL.indexOf('/home/content2/')==firstSlash||strURL.indexOf('/home/content3/')==firstSlash||strURL.indexOf('/home/page/')==firstSlash||strURL.indexOf('/home/form/')==firstSlash) {
		strURL=unsimplify_url(strURL);
	}
	return (normalize_url1(strURL, strSourceSiteID));
}

function make_sage_call(intType,strURL,strSourceSiteID,strOptions) {
	var st_iu=st_rs=st_cd=st_rf=st_us=st_hn=st_ce="";
	var l=location;
	var n=navigator;
	var tempURL;
	st_ce=n.cookieEnabled;
	st_ai=st_ai.replace(/\+/g,'%2b');
	st_ai=st_ai.replace(/;/g,'%3b');
	tempURL = normalize_url(strURL, strSourceSiteID);
	if (tempURL != '') {
		strURL = tempURL;
	}
	if (strURL.indexOf('http')==0) {strURL = strURL.substring(strURL.indexOf('://')+3);}
	if(intType==1) {st_us = escape(strURL);}
	if(intType==2) {
		if (strSourceSiteID == 'home') {
			st_us = '/exit/topsite/'+escape(strURL);
		} else {
			st_us = '/exit/'+strSourceSiteID+'/'+escape(strURL);
		}
	}
	if(intType==3) {
		if (strSourceSiteID == 'home') {
			st_us = '/pageexit/topsite/'+strOptions+'/'+escape(strURL);
		} else {
			st_us = '/pageexit/'+strSourceSiteID+'/'+strOptions+'/'+escape(strURL);
		}
	}
	st_rf = normalize_url(document.URL, strSourceSiteID);
	if (st_rf.indexOf('http')==0) {st_rf = st_rf.substring(st_rf.indexOf('://')+3);}
	st_rf = escape(st_rf);
	st_hn=l.host;
	if(st_v==1.2) {
		var s=screen;
		st_rs=escape(s.width+"x"+s.height);
		st_cd=s.colorDepth;
	}
	proto = "http:";
	if (location.protocol == "https:") {proto = "https:";}
	st_iu=proto+"//"+st_dd+"/" + (new Date()).getTime() + "/JS?ci="+st_ci+"&di="+st_di+"&pg="+st_pg+"&us="+st_us+"&qs=&rf="+st_rf+"&rs="+st_rs+"&cd="+st_cd+"&hn="+st_hn+"&ce="+st_ce+"&jv="+st_v+"&tai="+st_tai+"&ai="+st_ai;
	var iXz=new Image();
	iXz.src=st_iu;
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("firefox") != -1) pausecomp(200);
}

function pausecomp(millis) {
Date = new Date(); var curDate = null;
do { var curDate = new Date(); }
while(curDate-date < millis);
}

function shell_sage_ev(intType,strURL,strSourceSiteID) {
	make_sage_call(intType,strURL,strSourceSiteID,'');
}

function page_exit_samesite(intType,strURL,strSourceSiteID,strClassification) {
	make_sage_call(3,strURL,strSourceSiteID,strClassification);
}

function page_exit_diffsite(intType,strURL,strSourceSiteID,strClassification) {
	make_sage_call(3,strURL,strSourceSiteID,strClassification);
	make_sage_call(intType,strURL,strSourceSiteID,'');
}

function shell_sage_openAnyWinTwo(intType,strSourceSiteID,strURL,wWindowName,wWidth,wHeight,wTop,wLeft,wDirectories,wLocation,wMenubar,wScrollbars,wStatus,wToolbar,wResizable){
	shell_sage_ev(intType,strURL,strSourceSiteID);
	openAnyWinTwo(strURL,wWindowName,wWidth,wHeight,wTop,wLeft,wDirectories,wLocation,wMenubar,wScrollbars,wStatus,wToolbar,wResizable);
}

function report_main(){
	st_ous=normalize_url(document.URL);
	st_orf=normalize_url(document.referrer);
}

report_main();
