// Tripp Web JavaScript  
/* The following creates a getElementsByClassName function, which does not exist in JavaScript */

/* The following dynamically creates some additional styles when JavaScript is enabled.
   The class noscript is used to enclose code that in non-standards compliant html would have been included in noscript tags.  Add class="noscript" to any element that 
   you do not want to display when JavaScript is enabled
   The .moreMsg and .lessMsg styles only show those element is JavaScript is enabled
   The moreContent div and span are initally not shown when JavaScript is enabled
   the .js class is initally set in the main.css to display:none. Block items get set back to block when javascript isturned on.  Make sure you add these elements here as needed
*/
var myHTML = '\n<style type="text/css">\n';
myHTML += ".noscript { display:none;}\n";
myHTML += '.moreMsg, .lessMsg { display: inline;}\n';
myHTML += 'div.moreContent, span.moreContent { display: none;}';
myHTML += 'div.js { display: block;}';
myHTML += "</style>\n";
document.write(myHTML);

// Adjust the position of any tool tips to ensure they fit on the page 
var vpWidth = getViewportWidth();
var vpHeight = getViewportHeight();


// The following forces the tip into the viewport if it is cut off
// To use this you need to add a mouseover call to the function.  I am 
// only doing this for tips that I have determined have a potential problem
// I could go through all the elements with the class "tip", walking through the 
// DOM, but this would increase the page load time, so I am not doing it
function fixtipxy(parent, tipId)
{
	var el = document.getElementById(tipId);
	var elHeight = getElementHeight(el);
	var elWidth = getElementWidth(el);
	var coords = getPageCoords(el);
	var elRight = coords.x + elWidth;
	var elBottom = coords.y + elHeight;
	var parentTop = getPageCoords(parent).y;
	var parentLeft = getPageCoords(parent).x;
	// If the tool has already been moved, we need to set it back so that it will
	// work properly again after the user has scrolled up or down.
	// We don't worry about horizontal scrolling
	if (coords.y < parentTop) {
		el.style.top = "1.2em";
	}
	// If the right side of the element is cut off, push it to the left
	// but do not push it out of the viewport.  So we make sure the top of the 
	// tooltip is below the bottom of the parent and lines up on the left side
	
	if (elRight > vpWidth) {
		var adjX = Math.min((vpWidth - elRight), 0);  // calculate x adjustment
		el.style.left = adjX + "px";		// shift it over to the left
	}
	// If the bottom of the element is cut off, push it up
	var scrollY = getScrollY();   // amount user has scrolled down
	if (elBottom > (vpHeight + scrollY)) {
		var adjY = vpHeight + scrollY - elBottom;
		el.style.top = adjY + "px";
		// The following will position it above the parent
		adjY = (parentTop - coords.y) - elHeight + 15;
		el.style.top = adjY + "px";
	}
}

// The following is used to detect IE6
function isIE6() {
  $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
  if (ereg("msie 6.0", $userAgent)) {
    return true;
  } else {
    return false;
  }
}


/* The following changes the display property of the message and content when the user is given the option of showing more info.
The input parameter is a number that is used to create a unique id for the elements.
*/
function showMoreInline(trigger, target)
{
	document.getElementById(trigger).style.display  = 'none'; 
	document.getElementById(target).style.display  = 'inline'; 
}
function showLessInline(trigger, target)
{
	document.getElementById(trigger).style.display  = 'inline'; 
	document.getElementById(target).style.display  = 'none'; 
}
function showMoreBlock(trigger, target)
{
	document.getElementById(trigger).style.visibility  = 'hidden'; 
	document.getElementById(target).style.display  = 'block'; 
}
function showLess(trigger, target)
{
	document.getElementById(trigger).style.visibility  = 'visible'; 
	document.getElementById(target).style.display  = 'none';
}



// The following function gets the coordinates of the element relative to the 
// page, rather than relative to the window
function getPageCoords (element)
{
	var coords = { x: 0, y: 0 };
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}  
	return coords;  
}  
// The following function returns the width of an element
function getElementWidth(myElement)
{
	if (typeof myElement.clip !== "undefined") {
		return myElement.clip.width;
	} 
	else if (myElement.style.pixelWidth){
		return myElement.style.pixelWidth;
	} 
	else {
		return myElement.offsetWidth;
	}
}

// The following function returns the height of an element
function getElementHeight(myElement)
{
	alert("in the height function");
	if (typeof myElement.clip !== "undefined") {
		return myElement.clip.height;
	} 
	else if (myElement.style.pixelHeight){
		return myElement.style.pixelHeight;
	} 
	else {
		return myElement.offsetHeight;
	}
}


/* The following toggles the display of text */
/* I am not yet using this, but it is from */
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Ultimater, Mr J :: http://www.webdeveloper.com/forum/showthread.php?t=77389 */
function toggleMe(a){
  var e=document.getElementById(a);
  if(!e)return true;
  if(e.style.display=="none"){
    e.style.display="block"
  } else {
    e.style.display="none"
  }
  return true;
}

// The following function returns the width of an element
function getElementWidth(myElement)
{
	if (typeof myElement.clip !== "undefined") {
		return myElement.clip.width;
	} 
	else if (myElement.style.pixelWidth){
		return myElement.style.pixelWidth;
	} 
	else {
		return myElement.offsetWidth;
	}
}

// The following function returns the height of an element
function getElementHeight(myElement)
{
	if (typeof myElement.clip !== "undefined") {
		return myElement.clip.height;
	} 
	else if (myElement.style.pixelHeight){
		return myElement.style.pixelHeight;
	} 
	else {
		return myElement.offsetHeight;
	}
}

// The following functions returns the height and width of the viewport
// Using google, I found the logic for this at http://andylangton.co.uk and modified it for my needs
function getViewportHeight ()
{
	return getViewportDimension("h");
}
function getViewportWidth ()
{
	return  getViewportDimension("w");
}
function getViewportDimension (myType)
{
	var viewportheight;
	var viewportwidth;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use 
	// window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth = window.innerWidth;
		viewportheight = window.innerHeight;
	}

	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line 
	// in the document)

	else if (typeof document.documentElement != 'undefined'
		&& typeof document.documentElement.clientWidth !=
		'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportheight = document.documentElement.clientHeight
	}
	// older versions of IE

	else {
	   viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	if (myType == "h") {
		return viewportheight;
	} else if (myType == "w") {
		return viewportwidth;
	} else { return 0;}
	
}
// The following determines how far down the page the user has scrolled
 function getScrollY() {
            var scrOfY = 0;
            if( typeof( window.pageYOffset ) == 'number' ) {
                //Netscape compliant
                scrOfY = window.pageYOffset;
            } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
                //DOM compliant
                scrOfY = document.body.scrollTop;
            } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
                //IE6 standards compliant mode
                scrOfY = document.documentElement.scrollTop;
            }
            return scrOfY;
        }
		
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
		

