// This file contains JavaScript routines to support popup help boxes
// Author: Damian Burke, standing on the shoulders of many giants.
// 		In other word, I merely modified freely-available scripts.
// Intended Browsers: Netscape 4 and up, IE 5 and up

//////////////////////////////////////////////////////////////////////////////////////
// These next 2 functions are from Webmonkey and they fix the Netscape 4 resize bug.
// http://hotwired.lycos.com/webmonkey/reference/javascript_code_library/wm_ns_css_rs_fx/
//		?tw=reference&category=dhtml

function WM_netscapeCssFix() {
  /*
    Source: Webmonkey Code Library
    (http://www.hotwired.com/webmonkey/javascript/code_library/)

    Author: Taylor
    Author Email: taylor@wired.com
    Author URL: http://www.taylor.org/
    */

  // This part was inspired by Matthew_Baird@wayfarer.com
  // It gets around another unfortunate bug whereby Netscape 
  // fires a resize event when the scrollbars pop up. This 
  // checks to make sure that the window's available size 
  // has actually changed.
  if (document.WM.WM_netscapeCssFix.initWindowWidth != window.innerWidth || 
  			document.WM.WM_netscapeCssFix.initWindowHeight != window.innerHeight) {
    document.location = document.location;
  }
}

function WM_netscapeCssFixCheckIn() {
  // This function checks to make sure the version of Netscape 
  // in use contains the bug; if so, it records the window's 
  // width and height and sets all resize events to be handled 
  // by the WM_netscapeCssFix() function.
  if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) {
    if (typeof document.WM == 'undefined'){
      document.WM = new Object;
    }
    if (typeof document.WM.WM_scaleFont == 'undefined') {
      document.WM.WM_netscapeCssFix = new Object;
      document.WM.WM_netscapeCssFix.initWindowWidth = window.innerWidth;
      document.WM.WM_netscapeCssFix.initWindowHeight = window.innerHeight;
    }
    window.onresize = WM_netscapeCssFix;
  }
}

//////////////////////////////////////////////////////////////////////////////////////

// These next 9 functions are based on:
//		 http://developer.apple.com/internet/_javascript/hideshow_layer.html

// Store variables to control where the popup will appear relative to the cursor position.
// Positive numbers are below and to the right of the cursor, negative numbers are 
// above and to the left.
var xOffset = 10;
var yOffset = -5;

window.onload = initializeHacks; // initialize hacks whenever the page loads

document.onclick = hideCurrentPopup; // setup an event handler to hide popups for 
									 //generic clicks on the document
WM_netscapeCssFixCheckIn();

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
    } else {
		return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.visibility = newVisibility;
		return true;
    } else {
		// we couldn't find the object, so we can't change its visibility
		return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate, myWidth) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.left = newXCoordinate;
		styleObject.top = newYCoordinate;
		if (myWidth) {
			styleObject.width = myWidth + "px";
		}
		return true;
    } else {
		// we couldn't find the object, so we can't very well move it
		return false;
    }
} // moveObject

function showPopup (targetObjectId, eventObj, myXoffset, myYoffset, myWidth) {
    if(eventObj) {
    	xOffset = myXoffset;
    	yOffset = myYoffset;
    	// stop event from bubbling up any farther
		hideCurrentPopup();
		eventObj.cancelBubble = true;
		
		// hide any currently-visible popups 
		// if it returns true, user clicked one already selected, so we're done
		
		// move popup div to current cursor position 
		// (add scrollTop to account for scrolling for IE)
		var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + 
				xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
		var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + 
				yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
		// alert("new x, y = "  + newXCoordinate + ", " + newYCoordinate);
		moveObject(targetObjectId, newXCoordinate, newYCoordinate, myWidth);
		

		// and make it visible
		if( changeObjectVisibility(targetObjectId, 'visible') ) {
			// if we successfully showed the popup
			// store its Id on a globally-accessible object
			window.currentlyVisiblePopup = targetObjectId;
			setWidgets("hidden");
			return true;
		} else {
			// we couldn't show the popup, boo hoo!
			return false;
		}
	} else {
		// there was no event object, so we won't be able to position anything, so give up
		return false;
	}
} // showPopup

function hideCurrentPopup(targetObjectId) {
	// note: we've stored the currently-visible popup on the global object 
	// window.currentlyVisiblePopup
	if(window.currentlyVisiblePopup) {
		changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
		window.currentlyVisiblePopup = false;
		setWidgets("visible");
	}
} // hideCurrentPopup


function initializeHacks() {
	// this ugly little hack resizes a blank div to make sure you can click
	// anywhere in the window for Mac MSIE 5
	if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
			&& (navigator.platform.indexOf('Mac') != -1)
			&& getStyleObject('blankDiv')) {
		window.onresize = explorerMacResizeFix;
	}
	resizeBlankDiv();
	// this next function creates a placeholder object for older browsers
	createFakeEventObj();
}

function createFakeEventObj() {
	// create a fake event object for older browsers to avoid errors in function call
	// when we need to pass the event object to functions
	if (!window.event) {
		window.event = false;
	}
} // createFakeEventObj


function resizeBlankDiv() {
	// resize blank placeholder div so IE 5 on mac will get all clicks in window
	if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
			&& (navigator.platform.indexOf('Mac') != -1)
			&& getStyleObject('blankDiv')) {
		getStyleObject('blankDiv').width = document.body.clientWidth - 20;
		getStyleObject('blankDiv').height = document.body.clientHeight - 20;
	}
}

function explorerMacResizeFix () {
	location.reload(false);
}

///////////////////////////
// Damian's added routines
///////////////////////////

// Used to turn off visibility of form elements, lest they shine
// through a popup help window in IE 5/windows
function setWidgets(theValue) {
	// browser detection from 
	// http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
	
 	// convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();

    // *** BROWSER VERSION ***
    // Note: On IE5, these return 4, so use is_ie5up to detect IE5.
    var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);

    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    var is_nav2 = (is_nav && (is_major == 2));
    var is_nav3 = (is_nav && (is_major == 3));
    var is_nav4 = (is_nav && (is_major == 4));
    var is_nav4up = (is_nav && (is_major >= 4));
    var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
                          (agt.indexOf("; nav") != -1)) );
    var is_nav6 = (is_nav && (is_major == 5));
    var is_nav6up = (is_nav && (is_major >= 5));

    var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var is_ie3    = (is_ie && (is_major < 4));
    var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
    var is_ie4up  = (is_ie && (is_major >= 4));
    var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
    var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
    var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
    var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
    var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
    
    var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
    
	// if IE 5  on windows
	if (is_ie && is_win) {
		// alert("IE Windows");
		// Loop through all form elements in all forms and make invisible/visible 
		//	the oollowing elements:
		// 		- selections
		for (var i = 0; i < document.forms.length; i++) {	
			var f = document.forms[i];
			for (var j = 0; j < f.length; j++) {
				var e = f.elements[j];
				if (e.type == "select-one") {
					e.style.visibility = theValue;
				}
			}
		}
		return;
	}
}
