﻿// JScript File


	var cookieVisitID 	= 'my_LastVisit';	
	var cookieNumVisitID 	= 'my_NumVisits';	

	var gLastVisit;
	var gNumVisits;
	
	SetLastVisit();
	
	function GetLastVisit ()
	{

		if ( gLastVisit == "")
		{
			return "Welcome to Cookie Central's Javascript and Cookie Example!";
		}
		else 
		{
			var oldVisitDate = new Date(gLastVisit);
			return 	"Welcome back, your last visit to this page was on " 
					+ gLastVisit +".<BR>You have been here " 
					+ gNumVisits + " time" +(gNumVisits>1 ? "s" :"") 
					+" before."
		}
	}


	function SetLastVisit (name, value) 
	{
		var newVisitDate = new Date();
		var expDate = new Date (); 
		var numVisits = 0;

			// The expDate is the date when the cookie should
			// expire, we will keep it for a year
		expDate.setTime( expDate.getTime() + (365 * 24 * 60 * 60 * 1000) ); 

			// Info about last visit
		if (GetCookie (cookieVisitID) != null)
			gLastVisit = GetCookie (cookieVisitID);
		else
			gLastVisit = "";

		if (GetCookie (cookieNumVisitID) != null)
			gNumVisits = GetCookie (cookieNumVisitID);	
		else
			gNumVisits = 0;

			// Use eval() to convert a string to a number
		numVisits = eval(gNumVisits) +1;	

			// Store info about this visit
		SetCookie( cookieVisitID, 	newVisitDate, expDate); 
		SetCookie( cookieNumVisitID, numVisits, expDate); 
	}

    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" : "");
    }
    
	