// RJA May 2011 - This is PROD version (hence www.zurich.co.uk and sitestat URL).
function sitestat(u){
var d=document,
l=d.location;
ns_pixelUrl=u+"&ns__t="+(new Date().getTime());
u=ns_pixelUrl+
"&ns_c="+
((d.characterSet)?d.characterSet:d.defaultCharset)+
"&ns_ti="+escape(d.title)+
"&ns_jspageurl="+escape(l&&l.href?l.href:d.URL)+
"&ns_referrer="+escape(d.referrer);
(d.images)?new Image().src=u:d.write('<'+'p><img src="'+u+'"height="1"width="1"alt="*"/><'+'/p>');
};
// RJA May 2011 - take URL, convert '/' to '.', lowercase everything, convert everything, and change ending from 'htm' or 'html' to 'page'
// However, if we have a metadata item called 'comScoreName' (any case) set, use that as page name instead!
function getMetaContent(metaToGet) {
    var elems;
    if (document.all) {
        elems = document.all.tags("meta");
    }
    else if (document.documentElement) {
        elems = document.getElementsByTagName("meta");
    }
    for (var i = 1; i <= elems.length; i++) {
        var meta = elems.item(i - 1);
        if (meta.name) {
            if (meta.name.toLowerCase() == metaToGet.toLowerCase()) {
                return meta.content;
            }
        }
    }
    return '';
}
// Anonymous block to run code
var cSurl;
var cShost;
cShost = window.location.host.toLowerCase();
cSurl = window.location.pathname.toLowerCase();
// Only collect data for UAT site

if ((cShost.indexOf('www.navandgen.co.uk')==0)&&(cSurl.indexOf('/nr/')==-1)){
 var comVal = getMetaContent('comScoreName');
	if (comVal == '') {
		//cSurl = window.location.pathname.toLowerCase();
		cSurl=cSurl.replace(cShost,'');
		// Drop first level of Url as always same in MCMS
		cSurl = cSurl.substring((cSurl.substring(1)).indexOf('/')+1);
		//cSurl = cSurl.substring((cSurl.substring(1)).indexOf('/')+1);
		// escape no use, as comScore do not allow certain chars even if escaped (as % not allowed either)!
		// Only upper and lower case chars, numbers or '-', '.', '_' are allowed. Need to add '.p' - even though docs say add '.page'.
		// If no .htm or .html ending then add .p to end (but may be a ? so what out).
		//anyHTM = cSurl.lastIndexOf(".htm");
		cSurl=capContent(cSurl);
		if(cSurl.lastIndexOf(".htm")!=-1)
		{
		cSurl = (((cSurl.substring(1, cSurl.lastIndexOf("."))) + '.p').replace(/[\/]/g, '.')).replace(/[^a-zA-Z0-9\-\.\_]+/g, '-');
		}
		else
		{
		// Got no .htm(l) - Take off anything after '?' then add '.p'. If no '?' then assume it's a directory, so just add '.p' anyway. 
		cSurl = ((cSurl.split('?')[0]).substring(1) + '.p').replace(/[\/]/g, '.').replace(/[^a-zA-Z0-9\-\.\_]+/g, '-');
		}
	}
	else
	{
	  cSurl=cSurl.replace(cShost,'');
		// Drop first level of Url as always same in MCMS
		cSurl = cSurl.substring((cSurl.substring(1)).indexOf('/')+1);
		cSurl= cSurl.substring(0,cSurl.lastIndexOf('/')+1)+comVal+".htm";
		//cSurl = cSurl.substring((cSurl.substring(1)).indexOf('/')+1);
		// escape no use, as comScore do not allow certain chars even if escaped (as % not allowed either)!
		// Only upper and lower case chars, numbers or '-', '.', '_' are allowed. Need to add '.p' - even though docs say add '.page'.
		// If no .htm or .html ending then add .p to end (but may be a ? so what out).
		//anyHTM = cSurl.lastIndexOf(".htm");
		
		cSurl=capContent(cSurl);
		if(cSurl.lastIndexOf(".htm")!=-1)
		{
		  cSurl = (((cSurl.substring(1, cSurl.lastIndexOf("."))) + '.p').replace(/[\/]/g, '.')).replace(/[^a-zA-Z0-9\-\.\_]+/g, '-');
		}
		else
		{
		// Got no .htm(l) - Take off anything after '?' then add '.p'. If no '?' then assume it's a directory, so just add '.p' anyway. 
		cSurl = ((cSurl.split('?')[0]).substring(1) + '.p').replace(/[\/]/g, '.').replace(/[^a-zA-Z0-9\-\.\_]+/g, '-');
		}
	
	}
	sitestat("//uk.sitestat.com/zurich/navandgen/s?name="+cSurl);
}

function capContent(cSurl)
{
 if(cSurl.lastIndexOf(".htm")!=-1)
 {
	cSurl = cSurl.substring(0, cSurl.lastIndexOf("."));
 }
 
 var urlSplit = cSurl.split("/");
 var newUrl="";
 var temp="";
 var i=0;
 for(i=0;i<urlSplit.length;i++)
 {
  if(i != 0)
  {
   temp = urlSplit[i];
   if(temp.length > 25)
   {
    temp = temp.substring(0,25);
   }
   
   if (i < urlSplit.length-1)
   {newUrl+="/"+temp; }
   else
   {newUrl+="/"+temp+".htm"; }  
   
  }
 }

 return newUrl;
 
}
