/////////////////////////////////////////////////////////////////////////
// Netscape 4 resize bugfix from Macromedia Dreamweaver
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
   if (init==true) with (navigator) {if 
((appName=="Netscape")&&(parseInt(appVersion)==4)) {
     document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; 
onresize=MM_reloadPage; }}
   else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) 
location.reload();
}
MM_reloadPage(true);
// End resize bugfix
/////////////////////////////////////////////////////////////////////////


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// Global Javascript available for use by any page
function switchImage(imgSrc, imgName)
{

  // mouse on/off any image not contained in a layer
  if(document.images)
    {
	document[imgName].src = imgSrc;
    }
}

function preLoadImages()
{
  // preload images: pass in as many as you wish
  var images = new Array();
  for(i = 0; i<preLoadImages.arguments.length;i++)
  {
    images[i] = new Image();
    images[i].src = preLoadImages.arguments[i];
  }
}

// useful functions to control/limit special stuff showing up
// such as number of times a flash piece or popup shows
function showSpecial(cookieName, expDays) 
 {
   var exp = new Date(); 
   //exp.setTime(exp.getTime() + (expDays*24*60*60*1000)); // in days
   exp.setTime(exp.getTime() + (expDays*60*1000)); // in minutes

   var count = getCookie(cookieName);
   if (count == null) 
    {
      count = 1;
      setCookie(cookieName, count, exp);
	//alert ('showing ' + cookieName);
      return true;
    }
   else 
    {
        // having the next line in place changes the behavior: if you do it,
	// then the expiration date will be extended after every visit, instead
	// of being based off the first visit (or the last time the Flash piece
	// was shown)
	//count++;
        //setCookie('showFlash', count, exp); 
	//alert ('no flash');
      return false;
    }
 }


// new routine July/11/2005
function showSpecialSession(cookieName, expDays)
{
   // should we do something special? if we haven't done it for expDays days, 
   //  then return true, otherwise return false.
   // uses cookies good for this session only. stores the timestamp of the last view.
			
   last_viewed = getCookie(cookieName)*1.; // timestamp
   now_ts = new Date().getTime();
			
   if( last_viewed && last_viewed>1120866554791 )
   { 
			
      // cookie exists...they've seen it before
      if( now_ts < (last_viewed + expDays*24*60*60*1000) ) {
          return false; // waiting period not yet over
      }
      // waiting period IS over, reset cookie with current timestamp
          setCookie(cookieName,now_ts); 
          return true;
				
   }			
   else 
   {
			
      // not previously viewed
      setCookie(cookieName,now_ts);
      return true;
   }

}




function getCookie (cookieName)
 {   
   cookieName = cookieName + "=";  
   var cookieNameLength = cookieName.length;  
   var cookieLength = document.cookie.length;  

   var i = 0;  
   while (i < cookieLength) 
    {    
      var j = i + cookieNameLength;    
      if (document.cookie.substring(i, j) == cookieName)      
	return getCookieVal (j);    
      i = document.cookie.indexOf(" ", i) + 1;    
      if (i == 0)
	break;   
    }  
   return null;
 }

 function setCookie (cookieName, cookieValue) 
  {  
    var argv = setCookie.arguments;  
    var argc = setCookie.arguments.length;  
    var expires = (argc > 2) ? argv[2] : null;  
    var path = (argc > 3) ? argv[3] : null;  
    var domain = (argc > 4) ? argv[4] : null;  
    var secure = (argc > 5) ? argv[5] : false;  
    document.cookie = cookieName + "=" + escape (cookieValue) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
  }

function deleteCookie (cookieName) 
 {  
   var exp = new Date();  
   exp.setTime (exp.getTime() - 1);  
   var cval = getCookie (cookieName);  
   document.cookie = cookieName + "=" + cval + "; expires=" + exp.toGMTString();
 }


function getCookieVal(offset) 
 {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
	endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
 }

// Slightly less complex than the onload one below,
// because it doesn't need to deal so much with <body onload=>
// situations
function addOnResizeEvent(generic)
{
       //if there's an existing onresize function
        if(typeof window.onresize == 'function')
        {
                //store it
                var existing = onresize;

                //add new onload handler
                window.onresize = function()
                {
                        //call existing onload function
                        existing();

                        //call generic onload function
                        generic();
                };
        }
        else
        {
                //setup onresize function
                window.onresize = generic;
        }

}

// Generic event "adder"--pass in either "resize" or "load" for 
// "onResize" or "onLoad" -- based on code from 
// http://www.brothercake.com/site/resources/scripts/onload/
//setup onload function
function addOnLoadEvent(generic)
{
if(typeof window.addEventListener != 'undefined')
{
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', generic, false);
}
else if(typeof document.addEventListener != 'undefined')
{
	//.. opera 7
	document.addEventListener('load', generic, false);
}
else if(typeof window.attachEvent != 'undefined')
{
	//.. win/ie
	window.attachEvent('onload', generic);
}

//** remove this condition to degrade older browsers
else
{

	//.. mac/ie5 and anything else that gets this far
	
	//if there's an existing onload function
	if(typeof window.onload == 'function')
	{
		//store it
		var existing = onload;
		
		//add new onload handler
		window.onload = function()
		{
			//call existing onload function
			existing();
			
			//call generic onload function
			generic();
		};
	}
	else
	{
		//setup onload function
		window.onload = generic;
	}
}

}





// general-use function to add handlers.
// use like this:
//    if(document.getElementById('ehs.form')){
//      addEvent(document.getElementById('ehs.form'), 'click', handleRadioClick);
//    }
// JS Bible 6th ed.
function addEvent(elem,evtType,func){
  if(elem.addEventListener){ elem.addEventListener(evtType,func,false); }
  else if(elem.attachEvent){ elem.attachEvent("on"+evtType, func); }
  else { elem["on"+evtType] = func; }
}


// this omits the slider menu while in the CMS
function omitSlideoutMenu(){
  if(document.getElementById('thessm')){
    document.getElementById('thessm').style.display='none';
  }
}
addEvent(window,"load",omitSlideoutMenu);
