// start of constants
var days=100;			// set life of cookie

var url ="http://www.kniveton.net/logs/stats2.php"

// functions
function GetFilename(url)
{
   if (url)
   {
      var m = url.toString().match(/.*\/(.+?)\./);
      if (m && m.length > 1)
      {
         return m[1];
      }
   }
   return "";
}


function setCookie(c_name,value,exdays)			//this sets a cookie
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

// end of functions 

// start of code itself

// get the page 
var page=GetFilename(location.href);	// page
if (!page) {var page='index'};		// if above fais call it index

var visits=getCookie(page);						// read the cookie
if (visits) {								// if cookie is set
if (visits != parseInt(visits)) 	setCookie(page,1, days);	// is not numeric so make it 1
if (visits == parseFloat(visits)) 					// its numeric 
		{
		visits=parseFloat(visits)+1;				// so add 1
		setCookie(page,visits, days);				// and overwrite
		}	
} else {
setCookie(page,1, days)							// no cookie set so make one
visits=1;
}

// get the referer
if (!document.referrer) {var from="Not known.";} else {var from=document.referrer;}


// make the url 	inc page, referer, time, visits

 url=""+url+"?page="+page+"&from="+from+"&visits="+visits;

 




var startTime = new Date();        //Start the clock!
    window.onbeforeunload = function()        //When the user leaves the page(closes the window/tab, clicks a link)...
    {
        var endTime = new Date();        //Get the current time.
        var timeSpent = (endTime - startTime);        //Find out how long it's been.
        var xmlhttp;        //Make a variable for a new ajax request.
        if (window.XMLHttpRequest)        //If it's a decent browser...
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();        //Open a new ajax request.
        }
        else        //If it's a bad browser...
        {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");        //Open a different type of ajax call.
        }
// send the url
        url = url+"&time="+timeSpent;        //Send the url.
        xmlhttp.open("GET",url,false);        //The false at the end tells ajax to use a synchronous call which wont be severed by the user leaving.
        xmlhttp.send(null);        //Send the request and don't wait for a response.
		
    }

