/////////////////////////////////////////////////////////////////////////////////
// jslib.js - javascript utility library / implementation code
//
// Copyright (c) 2002, R&D Associates
//
// Warren Mann
//
//
// This file contains several utility functions for use in javascripting, dhtml
// and a bunch of other stuff I can't think of right now.
//
//
// REQUIRES
//
//	nothing
//
//
// HISTORY
//
// 2002.11.25
// created - wm.
/////////////////////////////////////////////////////////////////////////////////


//-------------------------------------------------------------------------------
// FUNCTION browser_type()
//
//	Creates an object chock full of browser information.  Use by declaring a
//	variable and then calling this with new.  var browser = new browser_type();
//	The browser variable will then contain the following members:
//
//		major - navigator major version.
//		minor - navigator minor version.
//		ns - true for netscape, false otherwise.
//		ns4 - true if version 4 netscape.
//		ns6 - true if version 6 netscape.
//		ie - true if internet explorer.
//		ie4 - true if version 4 of internet explorer.
//		ie5 - true if intenet explorer version 5.
//		iem5 - true if version 5 ie on mac.
//		ie55 - true if internet explorer version 5.5
//		ie6 - true if version 6 of internet explorer.
//		nsdom - true if netscape document object model.
//		ie5dom - true if internet explorer 5 document object model.
//		iedom - true if internet explorer document object model.
//		w3dom - true if w3 compliant document object model.
//		opera - true if opera.
//
// PARAMETERS:
//
//	none
//
// RETURNS
//
//	browser description object.
//-------------------------------------------------------------------------------
function browser_type() {

	// get major/minor version
	agent = navigator.userAgent.toLowerCase();
	this.major = parseInt( navigator.appVersion );
	this.minor = parseFloat( navigator.appVersion );

	// test for netscape
	this.ns = (
		(agent.indexOf( 'mozilla' ) != -1) &&
		(agent.indexOf( 'spoofer' ) == -1) &&
		(agent.indexOf( 'compatible' ) == -1) &&
		(agent.indexOf( 'opera' ) == -1) &&
		(agent.indexOf( 'webtv' ) == -1) &&
		(agent.indexOf( 'hotjava' ) == -1)
	);

	// test for opera
	this.opera = (
		(agent.indexOf( 'mozilla' ) == -1) &&
		(agent.indexOf( 'spoofer' ) == -1) &&
		(agent.indexOf( 'compatible' ) == -1) &&
		(agent.indexOf( 'opera' ) != -1) &&
		(agent.indexOf( 'webtv' ) == -1) &&
		(agent.indexOf( 'hotjava' ) == -1)
	);

	// if netscape, get version
	this.ns4 = (this.ns && (this.major == 4));
	this.ns6 = (this.ns && (this.major >= 5));

	// check for ie.
	this.ie = (
		(agent.indexOf( "msie" ) != -1) &&
		(agent.indexOf( "opera" ) == -1)
	);

	// ie 4?
	this.ie4 = (
		this.ie &&
		(this.major ==  4) &&
		(agent.indexOf( "msie 4" ) != -1)
	);

	// ie 5?
	this.ie5 = (
		this.ie &&
		(this.major == 4) &&
		(agent.indexOf( "msie 5." ) != -1) &&
		(agent.indexOf( "msie 5.5" ) == -1) &&
		(agent.indexOf( "mac" ) == -1)
	);

	// ie 5 on mac?
	this.iem5 = (
		this.ie &&
		(this.major == 4) &&
		(agent.indexOf( "msie 5." ) != -1) &&
		(agent.indexOf( "mac" ) != -1)
	);

	// ie 5.5
	this.ie55 = (
		this.ie &&
		(this.major == 4) &&
		(agent.indexOf( "msie 5.5" ) != -1)
	);

	// ie 6
	this.ie6 = (
		this.ie &&
		(this.major == 4) &&
		(agent.indexOf( "msie 6." ) != -1)
	);

	// get DOM type
	this.nsdom = (this.ns4 || this.ns6);
	this.ie5dom = (this.ie5 || this.iem5 || this.ie55);
	this.iedom = (this.ie4 || this.ie5dom || this.ie6);
	this.w3dom = (this.ns6 || this.iem5 || this.ie55 || this.ie6);
}


//-----------------------------------------------------------------------------
// FUNCTION client_width()
//
//	Returns the width of the browser display area.
//
// PARAMETERS:
//
//	none
//
// RETURNS:
//
//	width of browser display area, in pixels.
//-----------------------------------------------------------------------------
function client_width() {

	var	w;

	if ( typeof( window.innerWidth ) == 'number' )
		w = window.innerWidth;
	else {
		if ( document.documentElement && document.documentElement.clientWidth )
			w = document.documentElement.clientWidth;
		else
			if ( document.body && document.body.clientWidth )
				w = document.body.clientWidth;
	}

	return w;
}


//------------------------------------------------------------------------------
// FUNCTION client_height()
//
//	Returns the height of the browser display area.
//
// PARAMETERS:
//
//	none
//
// RETURNS:
//
//	height of browser display area, in pixels.
//------------------------------------------------------------------------------
function client_height() {

	var	h;

	if ( typeof( window.innerHeight ) == 'number' )
		h = window.innerHeight;
	else {
		if ( document.documentElement && document.documentElement.clientHeight )
			h = document.documentElement.clientHeight;
		else
			if ( document.body && document.body.clientHeight )
				h = document.body.clientHeight;
	}

	return h;
}


//-------------------------------------------------------------------------------
// FUNCTION element_position()
//
//	This function returns the position of an element with the specified id
//	attribute. Use by declaring a variable and then calling this function
//	with new.  var pos = new element_position( element_id );  The position
//	variable will then contain the following members:
//
//		x - horizontal position of the element.
//		y - vertical position of the element.
//
//	This function does not work in netscape 4.
//
// PARAMETERS:
//
//	id
//	the id assigned to the element you wish to retrieve the position of.
//
// RETURNS:
//
//	ns4
//	null
//
//	otherwise
//	element position object.
//-------------------------------------------------------------------------------
function element_position( id ) {

	// doesn't work with netscape 4
	if ( !(document.getElementById || document.all) )
		return null;

	// get the object with the supplied id
	var elem = document.getElementById( id );

	// start off at 0 horizontally and vertically
	this.x = 0;
	this.y = 0;

	// position of object is the sum of all parent objects' positions
	while ( elem.offsetParent ) {
		this.x += elem.offsetLeft;
		this.y += elem.offsetTop;
		elem = elem.offsetParent;
	}
}
