/*
 * Author: Brian S. Wilson
 * Date: April 12, 2005
 * Purpose: This is a collection of all the javascript functions used on my personal web site.
 *              Some of these functions are from various sources and books, some are my original work.
 */

function Clock() {
	/* Set up a clock on the Status Line. */

	var now=new Date();
	window.status="Current Date and Time: "+now;
	setTimeout("Clock()",1000);
	return true;
}
 
function Describe(name, text) {
	/* Change the text in an element named "desc" */

	if (!document.getElementById) return;
	var box=document.getElementById(name);
	if (text == '') text="You may navigate this site using the links below.";
	box.innerHTML=text;
	return true;
}

function show_hide(name) {
	/* Routine for turning the visibility of an item on or off.  This will not require the page to be re-drawn. */

	if (!document.getElementById) return;
	var object=document.getElementById(name);
	if (object.style.visibility == "hidden") {
		object.style.visibility="visible";
	} else {
		object.style.visibility="hidden";
	}
	return true;
}


function process_mail(form) {
	/* Notice when the guest book entry has been mailed. */
	
	alert("Your message has been sent.");
	return true;
}


function setCookie(cookieName, valueArray, expdate) {
	/* Set a cookie based on the values passed into this array. */

	var err=0;
	var errMsg="";
	var value="";

	if (cookieName == "") { errMsg+="Cookie Name, "; }
	if (valueArray == null) { errMsg+="Cookie Value, "; }
	if (expdate == "") { errMsg+="Cookie Expiration Date, "; }

	for (i=0; i<valueArray.length-1; i++) {
		value+=valueArray[i]+"|"
	}
	value+=valueArray[valueArray.length-1]
		
	if (errMsg == "") {
		document.cookie=cookieName+"="+value+";expires="+expdate.toGMTString();
	} else {
		alert("ERROR[setCookie]: "+errMsg+" not specified.");
		err=1;
	}
	return err;
}


function getCookie(cookieName) {
	/* Read a cookie and return the values as an array. */

	if (document.cookie != "" ) {
		var cookie=document.cookie.split("; ");
		for (i=0; i<cookie.length; i++) {
			if (cookieName == cookie[i].split("=")[0]) {
				var array=cookie[i].split("=")[1].split("|");
				for (i=0; i<array.length; i++) {
					if (array[i] == "") { array[i]="undefined" }
				}
				return array;
			}
		}
	}
	return Array();
}

function setName(newName) {
	/* Set the name value in a cookie. */

	/* set user name from form. */
	if (newName == "") { return }

	if (newName.match('/|;,/g') == null) {
	/* Set (or reset) a cookie with a name. */
		var oldArray=getCookie("BSW");
		var array=new Array();
		var expiration=new Date();

		array[0]=newName
		array[1]=oldArray[1];
		array[2]=oldArray[2];
		expiration=expiration.setMonth(expiration.getMonth()+6);

		setCookie("BSW", array, expiration);
	} else {
		alert("Notice:The newName you specified contains an invalid character.");
	}
}

function processGreeting() {
	/* Check for the existance of a cookie, set (or reset) a cookie. */
	
	/*\
	 * nameDate[0] := User Name from form (possibly blank).
	 * nameDate[1] := Date of last visit. (never blank).
	 * nameDate[2] := Count of past visits. (never blank).
	\*/

	var greeting="";
	var now=new Date();
	var hour=now.getHours();
	var expiration=new Date();
	exipration=expiration.setMonth(expiration.getMonth()+6);

	var nameDate=getCookie("BSW");

	if (hour < 12 ) { greeting="morning ";
	} else if (hour < 18) { greeting="afternoon ";
	} else { greeting="evening "; }
	
	if (nameDate == "" || nameDate == null ) { /* Never visited before. */
		greeting+="and welcome to my home page.  I see this is your first visit.";
		nameDate[0]="undefined";
		nameDate[2]=1;
	} else { /* Visited before */
		if (nameDate[0] == "undefined" || nameDate[0] == null ) { /* Never put their name in the Guest Book. */
			greeting+="and welcome to my home page.  ";
		} else { /* Put their name in the Guest Book. */
			greeting+=nameDate[0]+".  Welcome to my home page.  ";
		}
		nameDate[2]++;
		greeting+="I see this is visit number "+nameDate[2]+" and you last visited on "+nameDate[1]+".  ";
	}
	document.write(greeting);
	nameDate[1]=now.toGMTString();
	setCookie("BSW", nameDate, expiration);
}

function addBookmark(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}
