// lastvisit.js


// Track last visit to webpage by date and number of visits
// Oct 30/02 - Written
// Jan 3/04  - Code clean-up
//           - Added time of day and fixed year display problem with various browsers.
// Jan 4/04  - Leading zero missing when minutes < 10
// Aug 4/04 - Added build todays date in yyymmdd format - used by index page to determine changed pages.
// Aug 6/04 - Changed Lastyymmdd to be a date from 6 months ago. Show recently updated pages from index.htm
// Aug 7/04 - Moved Aug 4 & 6 code into index.htm (the page that uses it)
// Dec 21/05 - Added RCPvisits - used by index.htm

// These lines are placed in the body at the point of calling
// <script LANGUAGE="JavaScript">
//   var ThisPage = 'unique-page-name';
//   var ThisPageCnt = ThisPage + 'Cnt';
//   var ThisPageWhen = ThisPage + 'Whn';
//   if (Count() > 0) {
//     document.write("Your last visit was:<br>" + When());
//     }
//   else {
//     When();
//     }
//  </script>

var expDays = 300;
var exp = new Date(); 
var RCPvisits = 0;
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function Who(info){
  var VisitorName = GetCookie(ThisPage)
  if (VisitorName == null) {
    SetCookie (ThisPage, ThisPage, exp);
    }
  return VisitorName;
  }

function When(info){
  var rightNow = new Date()
  var WWHTime = 0;
  WWHTime = GetCookie(ThisPageWhen)
  WWHTime = WWHTime * 1
  var lastHereRaw = new Date(WWHTime);
  var lastHereInDateFormat = "" + lastHereRaw;
  var dayOfWeek = lastHereInDateFormat.substring(0,3)
  var dateMonth = lastHereInDateFormat.substring(4,10)
  var lastyear = lastHereRaw.getFullYear();
  var LastHours = lastHereRaw.getHours();
  var ToD = "am"
  if (LastHours > 12) {
    LastHours = LastHours-12;
    ToD = "pm";
    }
  var LastMins = lastHereRaw.getMinutes();
  if (LastMins < 10) {
    LastMins = "0" + LastMins;
    }
  var WWHText = dayOfWeek + ". " + dateMonth + ", " + lastyear
  WWHText = WWHText + "<br>at " + LastHours + ":" + LastMins + ToD
  // line below displays full date and time of last visit
  //   var WWHText = lastHereInDateFormat
  SetCookie (ThisPageWhen, rightNow.getTime(), exp)
  return WWHText
  }

function Count(info){
  var WWHCount = GetCookie(ThisPageCnt)
  if (WWHCount == null) {
    WWHCount = 0;
    }
  else{
    WWHCount++;
    RCPvisits = WWHCount;
    }
  SetCookie (ThisPageCnt, WWHCount, exp);
  return WWHCount;
  }

function set(){
  SetCookie (ThisPage, ThisPage, exp);
  SetCookie (ThisPageCnt, 0, exp);
  SetCookie (ThisPageWhen, 0, exp);
  }

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
  }

function GetCookie (name) {  
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
  return null;
  }

function SetCookie (name, value) {  
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
  }

function DeleteCookie (name) {  
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
  }


