/* counters.js */

var sUrl = window.location.pathname;
var sHostname = window.location.host;
var sWindowLoc = window.location.href;
var queryString = window.location.search;
var sTopLevelDomain = sHostname;
var sServerName = 'www';
var sTLD = 'online.nl';
var sPathname = '';

var bEnableSiteStatFooter = true;
var sSubDomain = sHostname.substring(0,sHostname.indexOf(sTLD));

//alert(sSubDomain);

if(sSubDomain.length != 0) {
	switch(sSubDomain) {
			case 'www.':
				if(sUrl == '/aanbiedingen/' || sUrl == '/aanbiedingen/index.html') {
					var clientValue = 'visitor';
					if(sCookieExists('subscriptionInfo')) {
						clientValue = sGetCookieValue('subscriptionInfo');
					} else if(isCustomer) {
						clientValue = 'customer';
					} else {
						clientValue = 'visitor';
					}
					
					if(clientValue == 'customer') {
						if(sUrl == '/aanbiedingen/') {
							sUrl = sUrl.replace(/aanbiedingen\//, "aanbiedingen/index/keep.html");
						} else {
							sUrl = sUrl.replace(/index.html/, "index/keep.html");
						}
					} else {
						if(sUrl == '/aanbiedingen/') {
							sUrl = sUrl.replace(/aanbiedingen\//, "aanbiedingen/index/get.html");
						} else {
							sUrl = sUrl.replace(/index.html/, "index/get.html");
						}
					}
				}
				sPathname = sUrl;
				break;
			case 'zoeken.':
				sPathname = 'zoeken/';
				break;
			case 'goudengids.':
				sPathname = 'zoeken/goudengids/';
				break;
			case 'telefoongids.':
				sPathname = 'zoeken/telefoongids/';
				break;
			case 'kms.':
				sPathname = 'klantenservice/';
				if(queryString.indexOf('strTitel=') != -1) {
					var strTitle = queryString.substring(queryString.indexOf('strTitel=') + 9);
					if(strTitle.indexOf('&') != -1) {
						strTitle = strTitle.substring(0,strTitle.indexOf('&'));
					}
					strTitle = strTitle.replace(/%20/g, '_');
					strTitle = encodeURIComponent(strTitle);
					strTitle = strTitle.toLowerCase();
					sPathname += strTitle;
				}
				break;
			case 'dsl.':
			case 'breedband.':
			case 'int-dsl.':
			case 'int-breedband.':			
			case 'acc-dsl.':
			case 'acc-breedband.':
			case 'wcr.':
			case 'int-wcr.':
			case 'acc-wcr.':
				bEnableSiteStatFooter = false;
				//sPathname = 'pccheck/';
				break;
			case 'wasp.':
			case 'wsp.':
				var str = queryString;
				var patt = new RegExp('command=[a-zA-Z]*');
				var result = patt.exec(str);
				result = new String(result);
				var command = result.substr(8).toLowerCase();
				sPathname = 'mijn-online/' + command;
				break;
	}
}

if(sPathname.indexOf("/") == 0) {
	sPathname = sPathname.substring(1);
}

if(sPathname.lastIndexOf(".html") != (sPathname.length - 5) && sPathname.lastIndexOf("/") != (sPathname.length - 1)) {
	sPathname += "/" ;
}

if(sPathname.lastIndexOf("/") == (sPathname.length - 1)) {
	sPathname += "index" ;
}

if(sPathname.lastIndexOf(".html") != -1) {
	sPathname = sPathname.substr(0, sPathname.length - 5);
}

sHostname = sHostname.replace(/\./g, "-");
sPathname = sPathname.replace(/\./g, "-");
sPathname = sPathname.replace(/-/g, "_");
sPathname = sPathname.replace(/\//g, ".");

if(sPathname.lastIndexOf(".") == (sPathname.length - 1)) {
	sPathname = sPathname.substring(0, sPathname.length - 1);
}


//if we are on the 404 page, the counter needs to be 'error404'
if(sPathname == '404.index') {
	sPathname = 'error404';
}

//if we are on the homepage, the function is called by the dynamicPillSelector
if(sPathname == 'index') {
	//sPathname = 'homepage.index';
	bEnableSiteStatFooter = false;
}

var strSitestatURL = "http://nl.sitestat.com/online/online-nl/s?" + sPathname;

if(bEnableSiteStatFooter) {
	/*if (window.sitestat == null) {
		include_dom("http://www.online.nl/js/sitestat.js");
	}*/
	sitestat(strSitestatURL);
}

function sitestat(ns_l){ns_l+='&amp;ns__t='+(new Date()).getTime();ns_pixelUrl=ns_l;
ns_0=document.referrer;
ns_0=(ns_0.lastIndexOf('/')==ns_0.length-1)?ns_0.substring(ns_0.lastIndexOf('/'),0):ns_0;
if(ns_0.length>0)ns_l+='&amp;ns_referrer='+escape(ns_0);
if(document.images){ns_1=new Image();ns_1.src=ns_l;}else
document.write('<img src="'+ns_l+'" width="1" height="1" alt="">');}

/*function include_dom(script_filename) {
	var html_doc = document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', script_filename);
	html_doc.appendChild(js);
	return false;
}*/

function sCookieExists(cookieName) {
  if (getCookieValue (cookieName))
    return true;
  else
  	return false;
}

function sGetCookieValue(cookieName) {
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(cookieName + "=");
	  if (c_start!=-1)
	    { 
	    c_start=c_start + cookieName.length+1; 
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    } 
	  }
	return "";
}
