/******************************************************************************* 
   sensiblemonkey.js
   Copyright (c) 2007 by Steve Henson: www.sensiblemonkey.com
   Except for: Web Monkey's WM_preloadImages(), Suckerfish, Cookie functions,
               and the Form Validaton Functions
   All source code is released under the:
               GNU LESSER GENERAL PUBLIC LICENSE
		       Version 2.1, February 1999 
			   A copy of the LGPL license can be found at:
			                  http://www.openoffice.org/license.html
   Last Edited On: 06-17-07
*********************************************************************************/




// ...............................................................................
//                      Background State Global Variables 
// ...............................................................................

   var bg_button = false;   /* Are we on the background change button? */
   var bg_state = false;    /* Have we changed the background from 'flora.gif'? */


// ...............................................................................
//                      Miscelaneous Global Variables 
// ...............................................................................

   var idxflag=false;   /* Flag to tell us if we are on the index page. Keeps our
                           sidebar menu from being built */

 




// ###############################################################################
//
//                        Body Onload Functions 
//
// ###############################################################################

// ...............................................................................
// sm_init_CSS() function:
//
//   - Checks for 'sensiblemonkeystate' cookie when page loads to retrieve 
//     background image state.
//   - Sets the state according to whether or not we are at index.html ('idx')
//     so the sidebar menu won't be built on the index page.
// ...............................................................................
function sm_init_CSS(where) {
  var cur_state = read_Cookie("sensiblemonkeystate");
  
  if(where == 'idx')      /* we are on the index page */
  {

    idxflag=true;         /* set the index page flag for the menu system to not display */
	
	                      /* use the DOM to set the transparent backgound in our header bar on the index page */
    document.getElementById('bg_change_header_l').style.backgroundImage='url("art/bg_change_header_l_t.gif")';
	
	                      /* set the background image per the sensiblemonkey cookie saved state. Default is 'flora' */
	 if (cur_state == 'clouds') 
	 {
	    sm_bg('clouds');  /* call the background-switcher function and set it to 'clouds' */
	    bg_state=true;    /* set our flag so where the background-change button can behave correctly */
     } 
     else 
	 {
	    bg_state=false;
		sm_bg('flora');
     }
  } 
  else idxflag=false;      /* we are not on the index page; set a flag saying it's okay for the menu system to display */
  
  if(where != 'idx')       /* we are not on the index page, set background and button states */
  {
    if (cur_state == 'clouds') {
        sm_vis_it('bg_change_hover');     /* call our background-switcher function via the button function */ 
	    bg_state=true;
    } 
    else if (cur_state == 'flora') 
	{
	    bg_state=false;
	    sm_hide_it('bg_change_hover');
    }
    else sm_hide_it('bg_change_hover');
	
	                       /* make sure our button to change the background is visible to Javascript-enabled browsers. 
						      Defaults to 'hidden' so non-Javascript browsers don't display a useless button */
    document.getElementById('divbg').style.visibility='visible';
  }
}


// ...............................................................................
// Web Monkey image preloader: pulled from www.webmonkey.com
// ...............................................................................
function WM_preloadImages() {
  if (document.images) {
	if (typeof document.WM == 'undefined') {
	  document.WM = new Object();
	}
	document.WM.loadedImages = new Array();
	var argLength = WM_preloadImages.arguments.length;
	for(arg=0;arg<argLength;arg++) {
	  document.WM.loadedImages[arg] = new Image();
	  document.WM.loadedImages[arg].src = WM_preloadImages.arguments[arg];
	}
  }
}





// ###############################################################################
//
//                        Mouse Interaction Functions 
//
// ###############################################################################

// ...............................................................................
// sm_vis_it() function: sets hidden div style to visible, where 'what' = id of div
// ...............................................................................
function sm_vis_it(what) {
  if(what == 'bg_change_hover'){   /* check to see if we're switching the background */
    if ((!bg_button && !bg_state) || (bg_button && bg_state)) { /* logic to tell us if we are toggled to 'flora' and keep track of where the mouse is */
      sm_bg('clouds');             /* call our backround-switcher fnction and set it to 'clouds' */
	 }
	 if ((!bg_button && bg_state) || (bg_button && !bg_state)) {
      sm_bg('flora');
    }
  }                               /* this line below makes the div nambed by 'what' visible via the DOM */
  else document.getElementById(what).style.visibility = 'visible';
}



// ...............................................................................
// sm_hide_it() function: sets visble div style to hidden, where 'what' = id of div
// ...............................................................................
function sm_hide_it(what) {
  if(what == 'bg_change_hover'){   /* check to see if we're switching the background */
     if (!bg_button && bg_state) { /* glue logic to keep track of where our mouse is and has been and what the background is set to */
       sm_bg('clouds');	           /* call our function to change the background */
     }
	 if (!bg_button && !bg_state) {
       sm_bg('flora');
     }
  }                                /* we aren't on the backround button, so hide 'what' via the DOM */
  else  document.getElementById(what).style.visibility = 'hidden';
}




// ###############################################################################
//
//                        Background Change Functions 
//
// ###############################################################################

// ...............................................................................
// bg_change_it() function: a toggles the background state flags and the cookie in the oven
// ...............................................................................
function bg_change_it() {
  if (!bg_state) { 
    bg_state = true;
    var bg_name = "clouds";
    set_Cookie("sensiblemonkeystate", bg_name, 365)
  } 
  else { 
    bg_state = false;
    var bg_name = "flora";
    set_Cookie("sensiblemonkeystate", bg_name, 365)
  }
}


// ...............................................................................
// sm_bg() function: DOM functions to do the actual background switching
// ...............................................................................
function sm_bg(name) {
  if(name == 'clouds') {            /* this next line is necssary so that aliased portions of the foreground can be replaced. Once IE can do transparent .pngs, this can be removed */
    document.getElementById('bg_change_header_r').style.backgroundImage='url("art/bg_change_header_r_c.gif")';
	document.body.style.backgroundImage='url("art/bg_clouds.jpg")';  /* the line does the actual background changing in the DOM */
  }
  else if(name == 'flora') {
    document.getElementById('bg_change_header_r').style.backgroundImage='url("art/bg_change_header_r_g.gif")';
	document.body.style.backgroundImage='url("art/bg_flora.gif")';
  }
}





// ###############################################################################
//
//                        Cookie Functions 
//
// ###############################################################################

// ...............................................................................
// set_Cookie() and read_Cookie() functions:  
//      Pulled from the book "Accessible XHTML and CSS Web sites" by Jon Ducket
// ...............................................................................
function set_Cookie(name,value,days) {
  if(days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = ";expires="+date.toGMTString();
  } else {
    expires = "";
	}
  document.cookie = name+"="+value+expires+";";
}
function read_Cookie(name) {
  var needle = name + "=";
  var cookieArray = document.cookie.split(';');
  for(var i=0;i < cookieArray.length;i++) {
    var pair = cookieArray[i];
	while(pair.charAt(0)==' ') {
	  pair = pair.substring(1, pair.length);
	}
	if(pair.indexOf(needle) == 0) {
	  return pair.substring(needle.length, pair.length);
	}
  }
  return null;
}





// ###############################################################################
//
//                        Popup Window Functions 
//
// ###############################################################################

// ...............................................................................
// next_pop() function:  opens a popup window and sets dimensions and focus
// ...............................................................................
function next_pop(newUrl, windowName) {
  var width = screen.width;                   /* check the size of the display */
  var height = screen.height;
  
  var win_width;                              /* height and width of our new window */
  var win_height;
  
  if (width <= 640) win_width = width;        /* make sure we don't overflow the display @ 640x480 and smaller */
  else win_width = 780;
  if (height <= 480) win_height = height;
  else win_height = 580;
  
  var s_left = (width / 2) - (win_width / 2); /* put the window in the center of the display */
  var s_top = (height / 2) - (win_height / 2);
  
  var bleh = 768;                             /* on larger displays, give some air (whitespace) between the top of the window and the top of the display */
  if (screen.height >= bleh) s_top = 50;
                                              /* open up our new window */
  ExtWin = window.open(newUrl,windowName,'toolbar=yes,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + win_width + ',height=' + win_height + ',left=' + s_left +',top=' + s_top + ',screenX=' + s_left + ',screenY=' + s_top);
  ExtWin.focus();                             /* pop the window to the top if one is already open */
}





// ###############################################################################
//
//                        Sidebar Menuing Functions 
//
// ###############################################################################

// ...............................................................................
// suckerfish css menus: search for it and it's sons on google
// ...............................................................................
sfHover = function() {
  if(!idxflag){
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
  }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);





// ###############################################################################
//
//                        E-mail Form Validation Functions 
//
// ###############################################################################

// ...............................................................................
// Provided by the folks at Mailform. 
// Checks to make sure all required fields are present and well-formed
// ...............................................................................
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
