// Cookie stuff:

var CookieExpiresInDays = 365;
var CookieExpiryDate = new Date(); 
CookieExpiryDate.setTime(CookieExpiryDate.getTime() + (CookieExpiresInDays*24*60*60*1000));
var Loads = NumPageLoads();
//document.write(Loads);
if (Loads == 0)
{
   PageLoadMessage = "";
//   PageLoadMessage = "It looks like this is the first time that you have loaded this page.";
}
else
{
   PageLoadMessage = "You've loaded this page at least <B>" + Loads + "</B> time(s).<BR>";
}
var LastVisit = GetCookie("Fury_LastHereOn");
//document.write(LastVisit);
if (LastVisit == null)
{
   LastVisitMessage = "";
}
else 
{
   LastVisit = 1 * LastVisit;  // integerize that stringy cookie

//   var LastHereFormatting = new Date(LastVisit);  // Date-i-fy that number
//   var LastHereInDateFormat = "" + LastHereFormatting;  // Gotta use substring functions
//   var DayOfVisit = LastHereInDateFormat.substring(0,3);
//   var MonthOfVisit = LastHereInDateFormat.substring(4,7);
//   var DateOfVisit = LastHereInDateFormat.substring(8,10);
//   var TimeOfVisit = LastHereInDateFormat.substring(11,16);
//   var YearOfVisit = LastHereInDateFormat.substring(24,28);
//   LastVisitMessage = "Welcome back. Your last visit was at: " + TimeOfVisit +
//                      " on: " + DayOfVisit + ", " + DateOfVisit + " " + 
//		      MonthOfVisit + " " + YearOfVisit;

   var LastHereDate = new Date(LastVisit);

   number_minutes = LastHereDate.getMinutes();
   if (number_minutes < 10)
   {
      word_minutes = "0" + number_minutes;
   }
   else
   {
      word_minutes = number_minutes;
   }

   number_day = LastHereDate.getDate();
   if (number_day == 1 || number_day == 21 || number_day == 31)
   {
      ending = "st ";
   }   
   else if (number_day == 2 || number_day == 22)
   {
      ending = "nd ";
   }   
   else if (number_day == 3 || number_day == 23)
   {
      ending = "rd ";
   }   
   else
   {
      ending = "th ";
   }   

   number_month = LastHereDate.getMonth() + 1;
   if (number_month == 1)
   {
      word_month = "January";
   }   
   else if (number_month == 2)
   {
      word_month = "February";
   }   
   else if (number_month == 3)
   {
      word_month = "March";
   }   
   else if (number_month == 4)
   {
      word_month = "April";
   }   
   else if (number_month == 5)
   {
      word_month = "May";
   }   
   else if (number_month == 6)
   {
      word_month = "June";
   }   
   else if (number_month == 7)
   {
      word_month = "July";
   }   
   else if (number_month == 8)
   {
      word_month = "August";
   }   
   else if (number_month == 9)
   {
      word_month = "September";
   }   
   else if (number_month == 10)
   {
      word_month = "October";
   }   
   else if (number_month == 11)
   {
      word_month = "November";
   }   
   else if (number_month == 12)
   {
      word_month = "December";
   }   
   else
   {
      word_month = "unknown";
   }

   LastVisitMessage = "Welcome back. Your last visit was at: " +
                      LastHereDate.getHours() + ":" + word_minutes +
                      " on the: " + number_day + ending + word_month + " " +
		      LastHereDate.getYear();
}

// --- Start of functions:

// --- Start of cookie routines

function NumPageLoads()
{
   var Fury_visits = GetCookie('Fury_visits')
   if (Fury_visits == null)
   {
      SetCookie('Fury_visits','1', CookieExpiryDate);
      return 0;	// Needed to make subsequent code work
   }
   else 
   {
      var newcount = parseInt(Fury_visits) + 1;
      DeleteCookie('Fury_visits');
      SetCookie('Fury_visits', newcount, CookieExpiryDate);
      return newcount;
   }
}



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 getCookieVal(offset) 
{
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1) endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}



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 CookieExpiryDate = new Date();  
   CookieExpiryDate.setTime (CookieExpiryDate.getTime() - 1);   
   var cval = GetCookie (name);  
   document.cookie = name + "=" + cval + "; expires=" + CookieExpiryDate.toGMTString();
}

function SaveDateTime() 
{
   var rightNow = new Date();
   SetCookie ("Fury_LastHereOn", rightNow.getTime(), CookieExpiryDate)
}

// --- End of cookie routines
