// A library of standard javaScripts useful for the majority of webpages
//###############################

// PopUp window
function newWindow(url,winName,attributes) {
window.open(url,winName,attributes);
} // end newWindow()

//#######################################

// Rollover script courtesy of ImageReady
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
} // end newImage()

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
} // end changeImages()

//########################################

/* Cookie Functions
WM_setCookie(), WM_readCookie(), WM_killCookie()
A set of functions that eases the pain of using cookies.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Nadav Savio
Author Email: nadav@wired.com
*/


// This next little bit of code tests whether the user accepts cookies.
var WM_acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
	WM_acceptsCookies = true; 
    }// If it succeeds, set variable
} else { // there was already a cookie
  WM_acceptsCookies = true;
}


function WM_setCookie (name, value, hours, path, domain, secure) {
    if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
	var not_NN2 = (navigator && navigator.appName 
		       && (navigator.appName == 'Netscape') 
		       && navigator.appVersion 
		       && (parseInt(navigator.appVersion) == 2))?false:true;

	if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { // calculate Date from number of hours
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} // WM_setCookie


function WM_readCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
	return false; 
    } else { // there is a cookie
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }	
} // WM_readCookie

function WM_killCookie(name, path, domain) {
  var theValue = WM_readCookie(name); // We need the value to kill the cookie
  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }
} // WM_killCookie

// end Cookie Functions

//###########################################

// Browser Detection Functions
/*
BrowserDetector()
Parses User-Agent string into useful info.

Original Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Richard Blaylock
Author Email: blaylock@wired.com
Modified by: Glenn Hummel
Modifier email: glenn@gchrt.com

Usage: var bd = new BrowserDetector(navigator.userAgent);
*/


// Utility function to trim spaces from both ends of a string
function Trim(inString) {
  var retVal = "";
  var startWord = 0;
  while ((startWord < inString.length) && (inString.charAt(startWord) == ' ')) {
    ++startWord;
  }
  var endWord = inString.length;
  while ((endWord > 0) && (inString.charAt(endWord - 1) == ' ')) {
    --endWord;
  }
  retVal = inString.substring(startWord, endWord);
  return retVal;
}

function BrowserDetector(ua) {

// Defaults
  this.browser = "Unknown";
  this.platform = "Unknown";
  this.version = "";
  this.majorver = "";
  this.minorver = "";

  uaLen = ua.length;

// ##### Split into stuff before parens, stuff in parens, and stuff after parens
  var preparens = "";
  var parenthesized = "";
  var postparens = "";
  var secondParen = ua.indexOf(")");
  var firstParen = ua.indexOf("(");
	  postparens = (ua.substring(secondParen + 1, uaLen));

  if (firstParen >= 0) {
    preparens = Trim(ua.substring(0,firstParen));
        parenthesized = ua.substring(firstParen + 1, uaLen);
        endParen = parenthesized.indexOf(")");
        if (endParen >= 0) {
          parenthesized = parenthesized.substring(0, endParen);
        }
  }
  else {
    preparens = ua;
  }

// ##### First assume browser and version are in preparens
// ##### override later if we find them in the parenthesized stuff
  var browVer = preparens;

  var tokens = parenthesized.split(";");
  var token = "";
// # Now go through parenthesized tokens
  for (var i=0; i < tokens.length; i++) {
    token = Trim(tokens[i]);
        //## compatible - might want to reset from Netscape
        if (token == "compatible") {
          //## One might want to reset browVer to a null string
          //## here, but instead, we'll assume that if we don't
          //## find out otherwise, then it really is Mozilla
          //## (or whatever showed up before the parens).
        //## browser - try for Opera or IE
    }
        else if (token.indexOf("MSIE") >= 0) {
      browVer = token;
    }
    else if (token.indexOf("Opera") >= 0) {
      browVer = token;
    }
        //'## platform - try for X11, SunOS, Win, Mac, PPC
    else if ((token.indexOf("X11") >= 0) || (token.indexOf("SunOS") >= 0) ||
(token.indexOf("Linux") >= 0)) {
      this.platform = "Unix";
        }
    else if (token.indexOf("Win") >= 0) {
      this.platform = token;
        }
    else if ((token.indexOf("Mac") >= 0) || (token.indexOf("PPC") >= 0)) {
      this.platform = token;
        }
  }
  
  // now checking postparens for browser and browser version
  	var tokensRedux = postparens.split(" ");
  	var tokenRedux = "";
	var netscapesplit = "";
	if (postparens != ""){
		for (var loop = 0; loop < tokensRedux.length; loop++){
		tokenRedux = Trim(tokensRedux[loop]);
		//alert(tokensRedux.length);
			if (tokenRedux.indexOf("Opera") >= 0){
			browVer = tokenRedux + tokensRedux[loop + 1];
			}// end if
			else if (tokenRedux.indexOf("Netscape") >= 0){
			netscapesplit = tokenRedux.split("/");
			browVer = netscapesplit[0];
			//this.version = parseInt(netscapesplit[1]);			
			} // end else if
 		} // end for
    } // end if postparens
	
  var msieIndex = browVer.indexOf("MSIE");
  if (msieIndex >= 0) {
    browVer = browVer.substring(msieIndex, browVer.length);
  }

  var leftover = "";
   if (browVer.substring(0, "Netscape".length) == "Netscape") {
    this.browser = "Netscape";
        leftover = browVer.substring("Netscape".length, browVer.length);
  }
  if (browVer.substring(0, "Mozilla".length) == "Mozilla") {
    this.browser = "Netscape";
        leftover = browVer.substring("Mozilla".length+1, browVer.length);
  }
  else if (browVer.substring(0, "Lynx".length) == "Lynx") {
    this.browser = "Lynx";
        leftover = browVer.substring("Lynx".length+1, browVer.length);
  }
  else if (browVer.substring(0, "MSIE".length) == "MSIE") {
    this.browser = "IE";
    leftover = browVer.substring("MSIE".length+1, browVer.length);
  }
  else if (browVer.substring(0, "Microsoft Internet Explorer".length) ==
"Microsoft Internet Explorer") {
    this.browser = "IE"
        leftover = browVer.substring("Microsoft Internet Explorer".length+1,
browVer.length);
  }
  else if (browVer.substring(0, "Opera".length) == "Opera") {
    this.browser = "Opera"
    leftover = browVer.substring("Opera".length, browVer.length);
  }

  leftover = Trim(leftover);

  // # Try to get version info out of leftover stuff
  i = leftover.indexOf(" ");
  if (i >= 0) {
    this.version = leftover.substring(0, i);
  }
  else
  {
    this.version = leftover;
  }
  j = this.version.indexOf(".");
  if (j >= 0) {
    this.majorver = this.version.substring(0,j);
    this.minorver = this.version.substring(j+1, this.version.length);
  }
  else {
    this.majorver = this.version;
  }

} // end function BrowserDetector

// end Browser Detection Functions

//###########################################

// Standard DHTML style determiner
// borrowed from Dave Thau

function getDivStyle(the_div) {
	var the_style;
	if (document.documentElement) { // for w3c browsers
		the_style = document.getElementById(the_div).style;
	}else if(document.all) {   // for ie4 browsers
		the_style = eval("document.all." + the_div + ".style");
	}else if (document.layers) { // for netscape4 browsers
		the_style = eval("document." + the_div);
	}else {
		alert("You need at least a 4.0 Browser");
		return false;
	}
	return the_style;
} // end getDivStyle()

// end DHTML style determiner

//###########################################