readCookie();

function readCookie() {

     if (document.cookie == "") {
	writeCookie();
     	//alert ("Welcome\nThis is your first visit here.")
     } else {
	var the_cookie = document.cookie;
	the_cookie = unescape(the_cookie);
	the_cookie_split = the_cookie.split(";");
		
	for (loop=0; loop<the_cookie_split.length; loop++) {
		var part_of_split = the_cookie_split[loop];
		var find_today = part_of_split.indexOf("today");
		if (find_today!=-1) {
			break;
		}
	} // for
	if (find_today==-1) {
		writeCookie();
		//alert("inside 1000");
	} else {
		var date_split = part_of_split.split("=");
		var last = date_split[1];
		var last_split = last.split(",");
		
		today_year=last_split[0];
		today_month=last_split[1];
		today_day=last_split[2];

		//alert ("Welcome Back!\n the TODAY of your last visit was: year = "+today_year+"; month = "+today_month+"; day = "+today_day);

		//get today's date
 		var currentTime = new Date();
 		var year = currentTime.getFullYear();
     		var month = currentTime.getMonth();
     		var day = currentTime.getDate();

		//if the visit previous to this visit was not today then update the cookies
		if (!(year==today_year&&month==today_month&&day==today_day)){
			writeCookie2(today_year,today_month,today_day);
		}
	} // if else
      } // if else
} // readcookie()

function writeCookie() {
     var currentTime = new Date();
     var year = currentTime.getFullYear();
     var month = currentTime.getMonth();
     var day = currentTime.getDate();

     var the_date = new Date("December 31, 2023");
     var the_cookie_date = the_date.toGMTString();

    the_cookie =  "last_visit=" + year;
           the_cookie = the_cookie +              "," + month;
           the_cookie = the_cookie +              "," + day;
           the_cookie = the_cookie + ";expires=" + the_cookie_date;
     document.cookie=the_cookie;
    var the_cookie =  "today="        + year;
           the_cookie = the_cookie + "," + month;
           the_cookie = the_cookie + "," + day;
           the_cookie = the_cookie + ";expires=" + the_cookie_date;
     document.cookie=the_cookie;
}

function writeCookie2(today_year, today_month, today_day) {
     //alert("reset cookies");
	var currentTime = new Date();
     var year = currentTime.getFullYear();
     var month = currentTime.getMonth();
     var day = currentTime.getDate();

     var the_date = new Date("December 31, 2023");
     var the_cookie_date = the_date.toGMTString();
    	the_cookie =  "last_visit=" + today_year;
           	the_cookie = the_cookie +              "," + today_month;
          	 the_cookie = the_cookie +              "," + today_day;
          	 the_cookie = the_cookie + ";expires=" + the_cookie_date;
    	 document.cookie=the_cookie;
    	var the_cookie =  "today="        + year;
           	the_cookie = the_cookie + "," + month;
           	the_cookie = the_cookie + "," + day;
           	the_cookie = the_cookie + ";expires=" + the_cookie_date;
    	 document.cookie=the_cookie;
}

