// language to metrics mapping
var sageLanMapObj = {};
sageLanMapObj['en'] = new Array();
sageLanMapObj['en']['metrics_site_id'] = "aboutshell-en" ;
sageLanMapObj['en']['metrics_dcode'] = "d222" ;
sageLanMapObj['en_US'] = new Array();
sageLanMapObj['en_US']['metrics_site_id'] = "us-en" ;
sageLanMapObj['en_US']['metrics_dcode'] = "d072" ;
sageLanMapObj['en_GB'] = new Array();
sageLanMapObj['en_GB']['metrics_site_id'] = "uk-en" ;
sageLanMapObj['en_GB']['metrics_dcode'] = "d063" ;
sageLanMapObj['en_CA'] = new Array();
sageLanMapObj['en_CA']['metrics_site_id'] = "ca-en" ;
sageLanMapObj['en_CA']['metrics_dcode'] = "d074" ;
sageLanMapObj['fr_CA'] = new Array();
sageLanMapObj['fr_CA']['metrics_site_id'] = "ca-en" ;
sageLanMapObj['fr_CA']['metrics_dcode'] = "d075" ;
sageLanMapObj['de'] = new Array();
sageLanMapObj['de']['metrics_site_id'] = "de-de" ;
sageLanMapObj['de']['metrics_dcode'] = "d037" ;
sageLanMapObj['nl'] = new Array();
sageLanMapObj['nl']['metrics_site_id'] = "nl-nl" ;
sageLanMapObj['nl']['metrics_dcode'] = "d051" ;
sageLanMapObj['cn_CN'] = new Array();
sageLanMapObj['cn_CN']['metrics_site_id'] = "china-cn" ;
sageLanMapObj['cn_CN']['metrics_dcode'] = "d003" ;
sageLanMapObj['zh'] = new Array();
sageLanMapObj['zh']['metrics_site_id'] = "china-cn" ;
sageLanMapObj['zh']['metrics_dcode'] = "d003" ;
sageLanMapObj['en_CN'] = new Array();
sageLanMapObj['en_CN']['metrics_site_id'] = "china-en" ;
sageLanMapObj['en_CN']['metrics_dcode'] = "d004" ;
sageLanMapObj['en_MY'] = new Array();
sageLanMapObj['en_MY']['metrics_site_id'] = "my-en" ;
sageLanMapObj['en_MY']['metrics_dcode'] = "d013" ;

function sageTracking(pageId,referId,flashTracking)
{
	// set internal referer flag
	var internalReferer = (referId) ? 1 : 0;

	// get shell metrics values
	var metrics_site_id = sageLanMapObj[language_code]["metrics_site_id"];
	var metrics_dcode = sageLanMapObj[language_code]["metrics_dcode"];
	var metrics_surl =  "/" + metrics_site_id + "/local/realenergy_2/";
	
	// it's flash referer must have been internal make call
	if(flashTracking)
	{
		makeSageCall(metrics_surl + pageId, metrics_surl + referId, metrics_site_id, metrics_dcode);
	}
	else
	{
		// check to see if referer was set
		if(!referId)
		{
			referId = normalize_url(document.referrer);
		}
		// build internal string
		referId = (internalReferer) ? metrics_surl + referId : referId;
		// make call
		makeSageCall(metrics_surl + pageId, referId, metrics_site_id, metrics_dcode);		
	}
}
   
function makeSageCall(strURL, strRefURL, metrics_site_id, metrics_dcode, metrics_surl) {
 var st_iu=st_rs=st_cd=st_rf=st_us=st_hn=st_ce="";
 var l=location;
 var n=navigator; 
 st_ce=n.cookieEnabled;
 st_us = escape(strURL);
 st_rf = escape(strRefURL);
 
 st_hn=l.host;
 if(metrics_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+"//www.report.shell.com/" + (new Date()).getTime() + "/JS?ci=784&di="+metrics_dcode+"&pg=&us="+st_us+"&qs=&rf="+st_rf+"&rs="+st_rs+"&cd="+st_cd+"&hn="+st_hn+"&ce="+st_ce+"&jv="+metrics_v+"&tai=v:1.2.8&ai=st_siteId:"+metrics_site_id;
 var iXz=new Image(); iXz.src=st_iu;
}


function normalize_url(strURL) {
	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 '';
			}
		}
	}

	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));
}

function normalize_url1(strURL) {
    var 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 append_promo(strTag,strPromo) {
	return((strPromo=='')?strTag:strTag+'_promo_'+strPromo);
}

function unsimplify_url(strURL) {
	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;
}