// 15Feb2010	Brian Fries		Reference document.documentElement instead of document.body
// 22Feb2010	Brian Fries		Limit x-coordinate of popup text

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top=50,'+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

divRollOff = '';
function showDiv(theName) {
	x=document.getElementsByTagName('img');
	if (x) {
		for(i=0;i<x.length;i++){
			if (x[i].name == 'img_'+theName) {
				y=x[i];
				break;
			}
		}
	}
	d = document.getElementsByTagName('div');
	if (d) {
		for(i=0;i<d.length;i++){
			if (d[i].id == 'div_'+theName) {
				if (d[i].style.visibility == 'visible') {
					d[i].style.visibility = 'hidden';
					d[i].style.display = 'none';
					y.src = '/images/disclosure_hide.png';
				} else {
					d[i].style.visibility = 'visible';
					d[i].style.display = 'block';
					y.src = '/images/disclosure_show.png';
				}
			divRollOff = '';
			}
		}
	}
}

function divOver(theName) {
	x=document.getElementsByTagName('img');
	if (x) {
		for(i=0;i<x.length;i++){
			if (x[i].name == 'img_'+theName) {
				y=x[i];
				break;
			}
		}
	}
	divRollOff = y.src;
	y.src = '/images/disclosure_over.png';
}

function divOff(theName) {
	if (divRollOff != '') {
		x=document.getElementsByTagName('img');
		if (x) {
			for(i=0;i<x.length;i++){
				if (x[i].name == 'img_'+theName) {
					y=x[i];
					break;
				}
			}
		}
		y.src = divRollOff;
	}
}

function arrowOver(theName) {
	x=document.getElementsByTagName('img');
	if (x) {
		for(i=0;i<x.length;i++){
			if (x[i].name == 'arrow_'+theName) {
				y=x[i];
				break;
			}
		}
	}
	y.style.visibility = 'visible';
}

function arrowOff(theName) {
	x=document.getElementsByTagName('img');
	if (x) {
		for(i=0;i<x.length;i++){
			if (x[i].name == 'arrow_'+theName) {
				y=x[i];
				break;
			}
		}
	}
	y.style.visibility = 'hidden';
}

var needSubmit = true;
function submitIncidentForm(section) {
	if (needSubmit) {
		document.incident.pSection.value = section;
		document.incident.submit();
		needSubmit = false;
	}
	return true;
}

// Code for popup help layers, taken from Apple sample code
// ************************
// layer utility routines *
// ************************

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) {
    // 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;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject

// ********************************
// application-specific functions *
// ********************************

// 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 = 30;
var yOffset = -5;

function showPopup (targetObjectId, eventObj) {
    if(eventObj) {
	// hide any currently-visible popups
	hideCurrentPopup();
	// stop event from bubbling up any farther
	eventObj.cancelBubble = true;
	// move popup div to current cursor position 
	// (add scrollTop to account for scrolling for IE)
	var newXCoordinate = Math.min(520,(eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.documentElement.scrollLeft)?document.documentElement.scrollLeft:0));
	var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.documentElement.scrollTop)?document.documentElement.scrollTop:0);
	moveObject(targetObjectId, newXCoordinate, newYCoordinate);
	// 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;
	    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() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
	changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup

