/*
Multiple URL Query String and Cookie String Parsing
*/
var tempArray = new Array(7); // Larger of max_URLArray / max_CookieArray 
var tempParms = new Array(2); // Pairs
var i = 0;
var theString = "";

/*
Parse Multiple URL Query Parameters (name=value) "&" 
into theURLQueryNames and theURLQueryValues
*/
var max_URLArray = 6;	
var theURLQueryNames = new Array(max_URLArray);
var theURLQueryValues = new Array(max_URLArray);
for (i=0; i<tempArray.length; i++) {
	theURLQueryNames[i] = "";
	theURLQueryValues[i] = "";
}
theString = window.location.search.substring(1);
if (theString.length > 0) {
	tempArray = theString.split("&");
	for (i=0; i<theURLQueryNames.length; i++) {
		if (tempArray[i].indexOf("=") != -1) {
			tempParms = tempArray[i].split("=");
			theURLQueryNames[i] = tempParms[0];
			theURLQueryValues[i] = tempParms[1];
		}
	}
}

/*
Parse Multiple Cookie Parameters (name=value) "|" 
into theCookieNames and theCookieValues 
*/
var max_CookieArray = 7;
var theCookieNames = new Array(max_CookieArray);
var theCookieValues = new Array(max_CookieArray);
for (i=0; i<tempArray.length; i++) {
	theCookieNames[i] = "";
	theCookieValues[i] = "";
}
theString = unescape(document.cookie);
if (theString.length > 0) {
	tempArray = theString.split("|");
	for (i=0; i<theCookieNames.length; i++) {
		if (tempArray[i].indexOf("=") != -1) {
			tempParms = tempArray[i].split("=");
			theCookieNames[i] = tempParms[0];
			theCookieValues[i] = tempParms[1];
		}
	}
}

function getValue(ArrayNames, ArrayValues, name) {
	for (i=0; i<ArrayNames.length; i++) {
		if (ArrayNames[i] == name) {
			return ArrayValues[i]}
	}
	return "";
}

function Set_Cookie(cookieNames, cookieValues) {
	var currMMDDYYYY  = FmtMMDDYYYY() ;
	var today = new Date() ; 
	var nowHour = String(today.getHours()) ;
	var nowMins = String(today.getMinutes()) ;
	if (nowHour.length < 2) nowHour = "0" + nowHour ;
	if (nowMins.length < 2) nowMins = "0" + nowMins ;
	var nowTime  = nowHour  + ":" + nowMins ;
	
	var old_new       = getValue(cookieNames, cookieValues, "CookieName");
	var origVisitDate = getValue(cookieNames, cookieValues, "origVisitDate");
	var prevVisitDate = getValue(cookieNames, cookieValues, "prevVisitDate");
	var lastVisitDate = getValue(cookieNames, cookieValues, "lastVisitDate");
	var lastVisitTime = getValue(cookieNames, cookieValues, "lastVisitTime");
	var dayVisits     = getValue(cookieNames, cookieValues, "dayVisits");
	var totalVisits   = getValue(cookieNames, cookieValues, "totalVisits");
	if (isNaN(dayVisits))   {dayVisits=1}
	if (isNaN(totalVisits)) {totalVisits=1}

	var path = "/";
	var domain = null ;
	var secure = false ;
	if ((old_new != "Anderson") && (origVisitDate != "")) {
		// this deletes the old named cookie
		document.cookie = "origVisitDate=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
	if (origVisitDate == "") {origVisitDate = currMMDDYYYY }
	if (dayVisits 	  == "") {dayVisits = "0" }
	if (totalVisits	  == "") {totalVisits = dayVisits}
	if (lastVisitTime == "") {lastVisitTime = nowTime}
	var prevTime = new Array(2) ;
	prevTime = lastVisitTime.split(":") ;
	
	var prevHour = prevTime[0] ;
	var prevMins = prevTime[1] ;
	if (prevHour == "") {prevHour = "00"}
	if (prevMins == "") {prevMins = "00"}
	if (prevHour.length < 2) prevHour = "0" + prevHour;
	if (prevMins.length < 2) prevMins = "0" + prevMins;
	
	var nextHour = String((Number(prevHour) + 1)) ;	//not counting within 1 hr
	if ((nextHour > "23") && (nowHour == "00")) {
		nextHour = "00" ;
	}
	if (nextHour.length < 2) nextHour = "0" + nextHour;
	var nextTime = nextHour + ":" + prevMins ;
	
	if (lastVisitDate == currMMDDYYYY) {
		if (nowTime > nextTime) {  		//not counting within 1 hr
			totalVisits = Number(totalVisits) + 1 ;
			lastVisitTime = nowTime }
	} else {
		if (prevVisitDate == "") {
			prevVisitDate = currMMDDYYYY 
		} else {prevVisitDate = lastVisitDate }
		lastVisitTime = nowTime ;
		dayVisits   = Number(dayVisits) + 1 ;
		totalVisits = Number(totalVisits) + 1 ;
	}
	var expires = ((1000 * 60 * 60 * 24) * 120) ;	//120 Days
	var cookie_expire_date = new Date( today.getTime() + (expires) ) ;
	var cookieString = "CookieName=Anderson"      + 
		"|" + "origVisitDate=" +origVisitDate + 
		"|" + "prevVisitDate=" +prevVisitDate + 
		"|" + "lastVisitDate=" +currMMDDYYYY  + 
		"|" + "lastVisitTime=" +lastVisitTime + 
		"|" + "dayVisits="     +String(dayVisits) + 
		"|" + "totalVisits="   +String(totalVisits) + 
		((expires)? ";expires=" + cookie_expire_date.toGMTString() : "" ) + 
		((path)   ? ";path=" + escape (path) : "" ) + 
		((domain) ? ";domain=" + escape(domain) : "" ) + 
		((secure) ? ";secure" : "" )	; 
	document.cookie = cookieString;
} 

function SetLastVisit(cookieNames, cookieValues) { 
	Set_Cookie(cookieNames, cookieValues); 
	var currMMDDYYYY  = FmtMMDDYYYY();
	var origVisitDate = getValue(cookieNames, cookieValues, "origVisitDate");
	var prevVisitDate = getValue(cookieNames, cookieValues, "prevVisitDate");
	var totalVisits   = getValue(cookieNames, cookieValues, "totalVisits");
	if (origVisitDate == "") {origVisitDate = currMMDDYYYY }
	if (prevVisitDate == "") {prevVisitDate = currMMDDYYYY } 
	if (isNaN(totalVisits))  {totalVisits = "1"}
	if ((origVisitDate == currMMDDYYYY) && (totalVisits < 4)) {
		window.status=("Welcome new visitors. If you haven't done so, please sign our htmlGear GuestBook! " + totalVisits ) ;
	} else {
		FP_swapImg(1,0,/*id*/'FamSeal',/*url*/'miscimg/AndersonFamily-clock.gif');
		if (prevVisitDate == currMMDDYYYY) {
			window.status=("Welcome back. If you haven't done so, please sign our htmlGear GuestBook! " + totalVisits ) ;
		} else {
			window.status=("Welcome back. Your prior visit was on " + prevVisitDate +"! "+ totalVisits ) ;
		}
	}
}
