// FG DHTML library base

// base functions

// get the object with the specified id
function getObject(id){
	if (document.getElementById){
		return document.getElementById(id);
	}
	else if (document.all){
		return  document.all[id];
	}
	else if (document.layers){
		return getObjectNN4(document, id);
	}
	else{
		return null;
	}
}

// helper function for getObject
// get object in Netscape 4
function getObjectNN4(obj,name){
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++){
		if (x[i].id == name)
			foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjectNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

// this replaces getElementsWithTagName
// works in IE4+, NS6+
function getTags(obj, tagName){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	if (obj.getElementsByTagName){
		return obj.getElementsByTagName(tagName);
	}
	else if (document.all){
		if (tagName == "*"){
			return obj.all;
		}
		else{
			// IE4 (and maybe some other browsers) support this method
			// but tagname must be in uppercase
			tagName = tagName.toUpperCase();
			return obj.all.tags(tagName);
		}
	}
	else{
	// we can't get the tags, so we'll return nothing
	return null;
	}
}

// return the object's parent
// this does not work in NS 4.x
function getParent(obj){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	if (obj.parentNode){
		return obj.parentNode;
	}
	else if (obj.parentElement){
		return obj.parentElement;
	}
	else if (obj.parentLayer){
		return obj.parentLayer;
	}
	else{
		// we can't get the parent, so return null
		return null;
	}
}

// this is a more compatible version of element.childNodes
// works in IE4+, NS6+
// moz/opera count whitespace nodes, but IE does not
function getChildren(obj){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	if (obj.childNodes){
		// for non IE browsers, we need to clean out the whitespace nodes
		// for consistency
		var nonWhitespace = new Array();
	
		for (var i = 0; i < obj.childNodes.length; i++){
			if (obj.childNodes[i].tagName != null && obj.childNodes[i].tagName != "!"){
				nonWhitespace[nonWhitespace.length] = obj.childNodes[i];
			}
		}
		return nonWhitespace;
	}
	else if (obj.children){	
		var nonWhitespace = new Array();
	
		// don't include comment tags (tags that have a tagName of !)
		for (var i = 0; i < obj.children.length; i++){
			if (obj.children[i].tagName != "!"){
				nonWhitespace[nonWhitespace.length] = obj.children[i];
			}
		}
		return nonWhitespace;
	}
	else if (obj.layers){
		return obj.layers;
	}
	else{
	// we can't get the tags, so we'll return nothing
	return null;
	}
}

// set the style for an object
function setStyle(obj, styleTag, value){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	// get the style object
	var style;
	if (document.getElementById){
		style = obj.style;
	}
	else if (document.all){
		style = obj.style;
	}
	else if (document.layers){
		style = obj;
	} 
	else{
		// we can't get the style, so return
		return false;
	}	
	// now, set the style
	style[styleTag] = value;
}

// show an element
// unless you specify a display type, the default of "block" is used
function show(obj, displayType){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	if (document.layers){
		obj.visibility  = "show";
		if (displayType != null){
			obj.display = displayType;
		}
		else{
		obj.display = "block";
		}
	}
	else if (obj.style){
		obj.style.visibility  = "visible";
		if (displayType != null){
			obj.style.display = displayType;
		}
		else{
			obj.style.display = "block";
		}
	}
	else{
		return false;
	}
}

// hide an object
// note that objects set display: none are inaccessible
// in the NN4.x DOM. If this matters, use hide instead
function hideDisplayNone(obj){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	if (document.layers){
		obj.visibility  = "hidden";
		obj.display = "none";
	}
	else if (obj.style){
		obj.style.visibility  = "hidden";
		obj.style.display = "none";
	}
	else{
		return false;
	}
}

// hide the object by setting visibility: hidden
// objects hidden with this function will remain in
// the document flow
function hide(obj){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	if (document.layers){
		obj.visibility  = "hidden";
	}
	else if (obj.style){
		obj.style.visibility  = "hidden";
	}
	else{
		return false;
	}
}

// hide all objects in a container
// then show the other one
function showOnly(obj){
	if (typeof(obj) == "string") obj = getObject(obj);
	show(obj);
	
	var container = getParent(obj);	
	var children = getChildren(container);
	
	for (var i = 0; i < children.length; i++){
		if (children[i] != obj){
			hideDisplayNone(children[i]);
			//hide(children[i]);
		}
	}
}

// CSS class functions

function hasAnyClass(obj){
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
}

// returns true if the object has the specified class
function hasClass(obj, className){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	if (obj.className){
		var classes = obj.className.split(" ");
		for (var i = 0; i < classes.length; i++){
			if (classes[i]	== className){
				return true;
			}
		}
		return false;
	}
	else{
		return false;
	}
}

// add the CSS class to the object
function addClass(obj, className){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	if (!hasClass(obj, className)){
		if (obj.className == null || obj.className == ""){
			obj.className = className;
		}
		else{
			// append this to the list of existing classes
			obj.className = obj.className + " " + className;
		}
	}
}

// remove the CSS class (if it exists)
function removeClass(obj, className){
	// we can take in an id string or an object pointer
	// if it's a string, just use getObject on the id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	var classes = obj.className.split(" ");
	var newClasses = new Array();
	
	for (var i = 0; i < classes.length; i++){
		if (classes[i]	!= className){
			newClasses[newClasses.length] = classes[i];
		}
	}
	
	var newClassString = newClasses.join(" ");
	obj.className = newClassString; 
}

// toggle the CSS class
// if it's not there, add it
// if it is, remove it
function toggleClass(obj, className){
	if (hasClass(obj, className)){
		removeClass(obj, className);
	}
	else{
		addClass(obj, className);
	}
}


// Screen functions

// get the document / body width
function getBodyWidth(){
	var x,y;
	
	if (document.getElementsByTagName){
		var bd = document.getElementsByTagName("body").item(0);
		x = getWidth(bd);
	}	
	return x;
}

function getBodyHeight(){
	var x,y;
	
	if (document.getElementsByTagName){
		var bd = document.getElementsByTagName("body").item(0);
		y = getHeight(bd);
	}	
	return y;
}

// get the browser width
function getScreenWidth(){
	var x,y;
	
	// all except Explorer
	if (self.innerHeight){
		x = self.innerWidth;
	}
	// Explorer 6 Strict Mode
	else if (document.documentElement && document.documentElement.clientHeight){
		x = document.documentElement.clientWidth;
	}
	// other Explorers
	else if (document.body){
		x = document.body.clientWidth;
	}
	return x;
}

// get the browser height
function getScreenHeight(){
	var x,y;
	
	// all except Explorer
	if (self.innerHeight){
		y = self.innerHeight;
	}
	// Explorer 6 Strict Mode
	else if (document.documentElement && document.documentElement.clientHeight){
		y = document.documentElement.clientHeight;
	}
	// other Explorers
	else if (document.body){
		y = document.body.clientHeight;
	}
	return y;
}

// get the vertical scroll
function getScrollTop(){
   var scrollTop = 0;
   if (self.pageYOffset){
      scrollTop = self.pageYOffset;
   }
   else if (document.documentElement && document.documentElement.scrollTop){
      scrollTop = document.body.scrollTop;
   }
   else if (document.body){
      scrollTop = document.body.scrollTop;
   }
   return scrollTop;
}

// get the horizontal scroll
function getScrollLeft(){
   var scrollLeft = 0;
   if (self.pageXOffset){
      scrollLeft = self.pageXOffset;
   }
   else if (document.documentElement && document.documentElement.scrollLeft){
      scrollLeft = document.body.scrollLeft;
   }
   else if (document.body){
      scrollLeft = document.body.scrollLeft;
   }
   return scrollLeft;
}

// get the height of an element
function getHeight(obj){
	if (typeof(obj) == 'string') obj = getObject(obj);		
	return obj.offsetHeight;
}

// get the width
function getWidth(obj){
	if (typeof(obj) == 'string') obj = getObject(obj);		
	return obj.offsetWidth;
}

// Returns the horizontal center coordinate. This is just the left scroll offset + browser width/2.
function getCenterX(){
	return ((getScrollLeft() + getScreenWidth())/2);
}

//  Returns the vertical center coordinate. This is just the top scroll offset + browser height/2.
function getCenterY(){
	return ((getScrollTop() + getScreenHeight())/2);
}

// will this object fit in the document horizontally?
function fitHorizontal(obj, xpos){
	return (xpos + getWidth(obj) <= getScreenWidth() + getScrollLeft() && xpos - getScrollLeft() >= 0);
}

function fitVertical(obj, ypos){
	return (ypos + getHeight(obj) <= getScreenHeight() + getScrollTop() && ypos - getScrollTop() >= 0);
}

// cribbed from quirksmode.org
// ms browsers (surprise!) have some quirks here
// find the horizontal offset
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// find the vertical offset
function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// resize object (stretch) functions

function stretchWidth(obj, offset){
	if (typeof(obj) == 'string') obj = getObject(obj);
	var w = getScreenWidth();
	obj.style.width = (w - offset) + "px";
}

function stretchHeight(obj, offset){
	if (typeof(obj) == 'string') obj = getObject(obj);
	var h = getScreenHeight();
	obj.style.height = (h - offset) + "px";
}
	
// generic popup function

// custom popup function - parameters passed in with link
function popup(alink, popup_width, popup_height, scrollbars, toolbars, resize){
	var url = alink.href;
	var name = alink.target;
	
	var popup_window = window.open(url,name,"toolbar="+toolbars+",directories="+toolbars+",status="+toolbars+",menubar="+toolbars+",location="+toolbars+",scrollbars="+scrollbars+",resizable="+resize+",width="+popup_width+",height="+popup_height);
	popup_window.focus();
}

// fixed popup function - parameters set in script which references popup()
function popupFixed(alink){
	var popup_window = popup(alink, 400, 345, 0, 0, 0); return false;
	popup_window.focus();			
}


// spindown functions
function spindown(e, caller) {
	if (!caller) caller = this;
	
	// get the bottom element
	var b = getObject(caller.id + "_body");
	
	// if we have both the top and bottom
	// set the appropriate styles
	if (b != null) {
		if (hasClass(caller, "spdn_open")) {
			// close it
			hideDisplayNone(caller.id + "_body")
			removeClass(caller, "spdn_open");
			addClass(caller, "spdn_closed");
		}
		else {
			// open it
			show(caller.id + "_body")
			removeClass(caller, "spdn_closed");
			addClass(caller, "spdn_open");
		}
	}
}

function setSpindown(obj) {
	if (typeof(obj) == "string") obj = getObject(obj);

	if (obj.captureEvents)
		obj.captureEvents(Event.CLICK);
	
	obj.onclick = spindown;
}

// Alternating table row colors
// modified by Shaun to change classes rather than colors
// modified by Maggie to use our core scripts
function stripe(){
	
	// the flag we'll use to keep track of whether the current row is odd or even
	var even = false;
	var evenStyle = 'even';
	var oddStyle = 'odd';
	
	var tb = getTags(document, 'tbody');
	
	for (var h = 0; h < tb.length; h++){
		// reset the even attribute
		even = false;
		
		if (hasClass(tb[h],'stripe')){
			// find all the tr elements... 
			var tr = getTags(tb[h], 'tr');
		
			// ... and iterate through them
			for (var i = 0; i < tr.length; i++){
				// avoid rows that have a class attribute
				// or backgroundColor style
				//if (!hasAnyClass(trs[i]))					
					tr[i].className = even ? evenStyle : oddStyle;
		
				// flip from odd to even, or vice-versa
				even =  ! even;
			}		
		}			
	}
}

// enable / disable checkboxes
function disableCheck(obj,chk){
	if (typeof(obj) == 'string') obj = getObject(obj);
	if (typeof(chk) == 'string') chk = getObject(chk);
	
	//get checkboxes
	var c = getTags(obj, 'input');	
	
	for(var i=0; i<c.length; i++){
		
		if (c[i] != chk){
			
			if (chk.checked){
				c[i].disabled = false;
			}
			else{
				c[i].disabled = true;
				c[i].checked = false;					
			}
		}	
	}							
}


// do a function onchange
function doFunction(obj){
	if (typeof(obj) == 'string') obj = getObject(obj);
	var c = obj.options[obj.selectedIndex].value;
	if (c) setTimeout(c,0);
}