function get_cookie(name) {
  var start = document.cookie.indexOf(name+"=")
  var len = start+name.length+1
  if ((!start) && (name != document.cookie.substring(0,name.length))) return null
  if (start == -1) return null
  var end = document.cookie.indexOf(";",len)
  if (end == -1) end = document.cookie.length
  return unescape(document.cookie.substring(len,end))
}

function split_path(stringToSplit)
{
  var siteroot = "http://www.leisurelocate.co.uk"
  var lastsep = stringToSplit.lastIndexOf(siteroot)
  return stringToSplit.substring(lastsep+siteroot.length)
}

function generate_id() {
  var date = new Date()
  // Convert to GMT
  date.setTime(date.getTime() + date.getTimezoneOffset()*60*1000)
  var d  = date.getDate()
  var day = (d < 10) ? '0' + d : d
  var m = date.getMonth() + 1
  var month = (m < 10) ? '0' + m : m
  var yy = date.getYear()
  var year = (yy < 1000) ? yy + 1900 : yy
  var time = date.getTime()
  var rnum = (Math.round(Math.random()*(99999-10000)))+10000
  var agt = navigator.userAgent.toString().toUpperCase()
  var temp = ''
  for (var i=0, valid="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; i<agt.length; i++) {
    if (valid.indexOf(agt.charAt(i)) != -1) temp += agt.charAt(i)
  }
  var mixed1 = ''
  var mixed2 = ''
  for (var i=0; i<8; i++) {
    var r = Math.round(Math.random()*(temp.length))
    mixed1 += temp.charAt(r)
    r = Math.round(Math.random()*(temp.length))
    mixed2 += temp.charAt(r)
  }

  return rnum.toString() + year.toString() + mixed1 + month.toString() + mixed2 + day.toString() + time.toString()
}

function do_session() {
  var cookiestatus = get_cookie("Session")
  var now = new Date()
  // 30 minutes from now
  var expires = new Date(now.getTime() + (30 * 60000))

  if (cookiestatus) {
    thisPage = split_path("" + location.href)
    if (thisPage != lastPage) {
  	  document.cookie = "Session=" + cookiestatus + ";expires=" + expires.toGMTString()
      lastPage = thisPage
    }
    setTimeout("do_session()", 1000)
  }
  else {
    thisPage = split_path("" + location.href)
    lastPage = thisPage
	
    var id = generate_id()
    document.cookie = "Session=" + id + ";expires=" + expires.toGMTString()
	
    // Check if cookie was accepted
    if (get_cookie("Session")) {
      var tempImg = new Image ()
      tempImg.src = "/cgi-bin/session.cgi?" + id + "&" + thisPage
      setTimeout("do_session()", 1000)
    }
  }
}

var thisPage = ""
var lastPage = ""
// Version 4 or higher browsers only
if (parseInt(navigator.appVersion) >= 4) {
  do_session()
}