/*
  
   Author:   Heman Lee
   Date:     December 2011
   Filename: counter.js

   Counter library: Set cookies for number of time a user visted a web site.

   Functions List:


   writeDateString(dateObj)
      Returns the date contained in the dateObj date object as
      a text string in the format mmm. dd, yyyy

   storeCookie(cName, cValue, expDate, cPath, cDomain, cSecure)
      Stores a cookie named cName with value cValue. Optional parameters
      expDate, cPath, cDomain, and cSecure set the expiry date,
      path, doman, and secure flag

   getCookie(cName)
      Returns the value of cookie, cName.

   setCounter()
      Retrieves and updates the date last visited and page counter
      cookies and displays them on the Web page along with the
      date last modified.

*/

addEvent(window, "load", setCounter, false);

function writeDateString(dateObj) {

   var monthName = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
    "Oct", "Nov", "Dec");
	
   var thisMonth = dateObj.getMonth();
   var thisYear = dateObj.getFullYear();

   return monthName[thisMonth] + " " + dateObj.getDate() + ", " + thisYear;
}

function setCookie(cName, cValue, nDays, cPath, cDomain, cSecure) {
   if (cName && cValue != "") {

     var today = new Date();
     var expDate = new Date();
     if (nDays==null || nDays==0) nDays=1;
     expDate.setTime(today.getTime() + 3600000 * 24 * nDays);

     // Set cookie string
     var cString = cName + "=" + escape(cValue);
     cString += (expDate ? ";expires=" + expDate.toGMTString(): "");
     cString += (cPath ? ";path=" + cPath : "");
     cString += (cDomain ? ";domain=" + cDomain : "");
     cString += (cSecure ? ";secure" : "");
     document.cookie = cString;
   }
}


function getCookie(cName) {
   if (document.cookie) {
     var cookies = document.cookie.split("; ");
     for (var i = 0; i < cookies.length; i++) {
        if (cookies[i].split("=")[0] == cName) {
           return unescape(cookies[i].split("=")[1]);
        }
      }
   }
}



function setCounter() {  
/*  Update the date that the page was last visited  and the number  of times visited,
    and date last updated  */

	var lastVisit = null;	
	if(lastVisit = getCookie("lastVisit"))
 	  lastVisit = "Last Visit: " + lastVisit;
	else
	  lastVisit = "&nbsp;&nbsp;";
	
	// Retrieve pageCount cookie	
	var pageCount = null;
	if (pageCount = getCookie("pageCount"))
          pageCount = parseInt(pageCount) + 1;
	else
          pageCount = 1;
        
      // Create cookie data strings
      lastUpdate = new Date(2011, 11, 23);
	pageUpdate = writeDateString(lastUpdate);

	var currentDate = new Date();
	var curtVisit   = writeDateString(currentDate);
		
	setCookie("lastVisit", curtVisit,3650);  // expire in 10yrs
	setCookie("pageCount", pageCount,3650);
	setCookie("pageUpdate", pageUpdate, 3630);
		
	var pageFooter = document.createElement("div");
	pageFooter.id = "pageFooter";
		
	var visitNum = document.createElement("span");
	visitNum.innerHTML = "Visit Number: " + pageCount +"<br />";
	pageFooter.appendChild(visitNum);

	var lstVisit = document.createElement("span");
	lstVisit.innerHTML = lastVisit;
	pageFooter.appendChild(lstVisit);	
			
	var lastUpdate = document.createElement("span");
	lastUpdate.innerHTML = "<br />Last Update: " + pageUpdate;
	pageFooter.appendChild(lastUpdate);
		
	var rightCol = document.getElementById("footer");
	rightCol.appendChild(pageFooter);
}
