/* oahscripts.js */

/* General JavaScript library for Inside OAH site
 * jjh rev. 5/18/2004
 */

/* Define new boolean variables to test for browser
 * jjh rev. 3/9/2004
 */
var isMSIE = (navigator.appName.indexOf("Microsoft") >= 0);
var isDOM = !document.all && document.getElementById;
var isNS4 = ((navigator.appName.indexOf("Netscape")  >= 0) && (parseInt(navigator.appVersion) == 4))

/* Utility function to sniff Netscape or Microsoft browser
 * (Maintain for backward compatibility only -- 
 * DON'T USE THIS CODE FOR FUTURE DEVELOPMENT! // jjh 3/9/2004)
 * 
 * jjh rev. 3/13/2002
 */
var ns = 0;
var ie = -1;
function checkBrowser() {
	if (navigator.appName.indexOf("Netscape") != -1) {
		return ns;
	} else {
		return ie;
	}
}

/* Set and clear messages from status bar
 * jjh rev. 3/13/2002
 */
function setStatusBar(theMessage) {
	window.status = theMessage;
	return true;
}
function clearStatusBar() {
	window.status="";
	return true;
}

/* Utility form validation function.
 *
 * The function checks a string value
 * to see if it is null, empty, or 
 * whitespace only.
 *
 * Returns true if the string is empty,
 * else returns false.
 *
 * jjh rev. 5/18/2004
 */
function isEmpty(s) {
	// Is string null or zero length?
	if ((s == null) || (s.length == 0)) {
		return true;
	}
	// Look for characters that are not whitespace
	var whitespace = " \t\n\r";
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) { // some non-whitespace character was found
			return false;
		}
	}
	// If the code gets to this point, only whitespace was found in the string
	return true;	
}

/* Return today's date in "DOW, MMMM dd, yyyy" format
 * (code borrowed from IBM WebSphere) 
 * jjh rev. 3/13/2002
 */
function insertDate() {
	var now = new Date();
	var yr  = now.getYear();
	var mn  = now.getMonth() + 1;//  months in JavaScript run from 0 to 11
	var dt  = now.getDate();
	var dy  = now.getDay();

	var fyr = (yr < 1900) ? 1900 + yr : yr;//  Account for year 2000 issues in some browsers
	var dys = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	var dy = dys[dy];
	var mns = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var mn = mns[mn-1];

	var vDate  = dy + ", " + mn + " " + dt + ", " + fyr;
	return vDate;
}

/* Countdown to a date
 * Code borrowed from example in O'Reilly JavaScript book (p. 449)
 *
 * jjh 4/1/2002
 */
function countdownDate(theYear, theMonth, theDay) {

//	 Set variable values from parameters
	var today = new Date();
	var target = new Date();
	target.setYear(theYear);
	target.setMonth(theMonth - 1);//  JavaScript months run 0-11, not 1-12
	target.setDate(theDay);
	
//	 Must manipulate date represented in milliseconds for accurate return value
	var difference = target.getTime() - today.getTime();
	difference = Math.floor(difference / (1000 * 60 * 60 * 24));//  Convert back from milliseconds to days
	
//	 Return string based on difference calculation above	
	if (difference < 1) {
		return "0 days";
	}
	if (difference == 1) {
		return "1 day";
	}
	if (difference > 1) {
		return difference + " days";
	}
}

/* Open web pages in a new browser window.
 * The new window will not have the standard browser bars (menu, tools, location).
 * The window can be resized and has scrollbars if appropriate.
 *
 * jjh rev. 3/9/2004
 */
function openInNewWindow(theUrl,theWidth,theHeight,theLeft,theTop,theWindowName) {

//	 Default left and top window parameters to Netscape syntax
	var theLeftParm = "screenx";
	var theTopParm = "screeny";
	
//	 If browser is Microsoft, change the parameter names
	//if (checkBrowser() == ie) {
	if (isMSIE) { // jjh 3/9/2004
		theLeftParm = "left";
		theTopParm = "top";
	}
	
//	 Define the parameter string
	var theParameters = "";
	if (theWidth != "") {
		theParameters += "width=" + theWidth + ",";
	}
	if (theHeight != "") {
		theParameters += "height=" + theHeight + ",";
	}
	if (theLeft != "") {
		theParameters += theLeftParm + "=" + theLeft + ",";
	}
	if (theTop != "") {
		theParameters += theTopParm + "=" + theTop + ",";
	}
	theParameters += "directories=no,";
	theParameters += "location=no,";
	theParameters += "menubar=no,";
	theParameters += "resizable=yes,";
	theParameters += "scrollbars=yes,";
	theParameters += "status=yes,";
	theParameters += "toolbars=no";
	
//	 Open a new window using the given URL and parameters
	var theWindow = window.open(theUrl,theWindowName,theParameters);
	theWindow.focus(); // jjh 3/9/2004
}

/* Utility functions for browser windows without standard browser bars
 * (tool bar, menu bar, location bar)
 *
 * jjh 3/13/2002
 */

function previousPage() {
	history.back();
}

function nextPage() {
	history.forward();
}

function closeWindow() {
	window.close();
}

function goHome() { // jjh rev. 3/9/2004
	var parentWin = window.opener;
	if (parentWin == null) {
		location.href = "/";
	} else {
		parentWin.location.href = "http://inside-oah.oah.wa.gov";
		parentWin.focus();
		window.close();
	}
}
