<!--- -------------------------------------------------------------------- --->
<!--- -------------------------------------------------------------------- --->
<!--- Begin Glossary JavaScript --->

<!--- This code is to be placed within a pair of HTML script tags in the HTML head> section. --->

<!--- Define global variables being used by multiple glossary related functions as well as instream JavaScript. --->
var i; //Array index.

var VarIEBrowser = false; //Boolean variable for other IE browser.
var VarNetscapeBrowser = false; //Boolean variable for Netscape browser.
var VarOtherBrowser = false; //Boolean variable for other browsers.

var VarTextX; //Horizontal coordinate location (where cursor highlighted text).
var VarTextY; //Vertical coordinate location (where cursor highlighted text).

<!--- Determine which browser is being used. --->
switch (navigator.appName)
{
	case "Microsoft Internet Explorer":
		VarIEBrowser = true;
		break;
	case "Netscape":
		VarNetscapeBrowser = true;
		break;
	default:
		VarOtherBrowser = true;
		
}

<!--- -------------------------------------------------------------------- --->
<!--- This function is triggered by pressing a character key.  It ensures that Ctrl + y was used to perform a Dynamic Glossary Lookup and calls the Selection function, else it displays an error message.  The event object (e) is passed to it. --->
function DetermineKey(e)
{
	<!--- Declare function variables. --->
	var VarKey = 0;  //Value of the character key that was pressed to cause this function to execute.
	var VarCtrlKey = false;  //Boolean value of CTRL key.
	
	var VarVersion = 0; //Version number of client browser.
	var ConIEPgmVersion = "6"; //Version of IE browser that we have programmed for.
	var ConNetscapePgmVersion = "4"; //Version of Netscape browser that we have programmed for. 

<!--- ----------------Removed the warning alert box, that let the user know that their browswer may or may not work with the Dynamic Glossary Lookup. ------------------ --->
	
	<!--- Determine where the cursor was on the screen, at the time that CTRL + y was pressed, in other words, where the highlighted text is.  This will be used in the DisplayGlossaryWindow function to open the glossary window away from the text.  The global variables, VarTextX and VarTextY have been declared above in the Global area.  --->	
	if (VarIEBrowser)
	{
		VarTextX = window.event.screenX;
		VarTextY = window.event.screenY;
	}
	if (VarNetscapeBrowser)
	{
		VarTextX = e.screenX;
		VarTextY = e.screenY;
	}
	
	
	
	<!--- Determine if the CTRL key was pressed. --->
	<!--- With IE, we use the Window object's event property's subproperty of the CTRL modifier key.  The Boolean value of true is returned if the CTRL key was pressed.  --->
	if (VarIEBrowser)
	{
		VarCtrlKey = window.event.ctrlKey;
	}

	<!--- With Netscape, we use the event (e) object's property of the CTRL modifier key.  The Boolean value of true is returned if the CTRL key was pressed.  --->

	if (VarNetscapeBrowser)
	{
		VarCtrlKey = e.ctrlKey;
	}
	
	
	
	<!--- Determine if the "y" key was pressed. --->
	<!--- With IE we use the Window object's event property's subproperty of "keyCode" to determine which keyboard character was pressed.  --->
	if (VarIEBrowser)
	{
		VarKey = window.event.keyCode;
	}

	<!--- With Netscape we use the event (e) object's object property of "which" to determine which keyboard character was pressed.  --->
	if (VarNetscapeBrowser)
	{
		VarKey = e.which;
	}
		
		
		
	<!--- Call Selection function or display error message. --->
	if (VarIEBrowser || VarNetscapeBrowser)
	{
		<!--- In IE, when CTRL + y are pressed, the character value is 25 (which is a non printable character).  In Netscape, when CTRL + y are pressed, the character value is 121 (which is the y numeric value). --->
		if (VarCtrlKey == true)
		{
			if (VarKey == 25 || VarKey == 121)
			{
				Selection();
			}
		}
		

	}
} //End of DetermineKey function.


<!--- -------------------------------------------------------------------- --->
<!--- This function is called by the DetermineKey function if CTRL = y was pressed.  It then handles the selection of the text accordingly, followed by edits and a call to the DisplayGlossaryWindow function. --->
function Selection()
{
	<!--- Declare function variables. --->
	var VarTextRange; //Interim Netscape selected text.
	var VarSelection; //Selected text from both browsers placed here.
	var TheURL; //The name of the asp program to execute the lookup.
	
	<!--- IE 4.0+ (Win32) uses the createRange method of the selection object to create the TextRange object.  This object is placed in an object variable.  This object's text property is then obtained to populate the VarSelection variable. --->
	if (VarIEBrowser)
	{
		VarTextRange = document.selection.createRange();
		VarSelection = VarTextRange.text;
	}
	
	<!--- Netscape 4.0+ uses the getSelection method to obtain the text that the user has selected.  --->
	if (VarNetscapeBrowser)
	{
		VarSelection = document.getSelection();
	}
			
	<!--- Ensure that some text has been selected. --->
	if (VarSelection.length == 0)
	{	
		alert('Ctrl + y has been pressed, but no text has been selected (highlighted).  This page is expecting that text would have been selected before pressing Ctrl + y, in order to perform a Dynamic Glossary Lookup.\n\nIf you wish to perform a Dynamic Glossary Lookup, please select some text with your mouse, then press Ctrl + y.\n\nMultiple words can be selected by clicking and dragging your mouse, or a single word can be selected by double clicking on it.');
		return false;
	}

	<!--- Ensure selected text is not greater than the following limit. --->
	if (VarSelection.length > 255)
	{
		alert('Ctrl + y has been pressed, but the selected (highlighted) text contains more than the maximum length of a term in the database.  This page is expecting only one or a few words of text to be selected, before pressing Ctrl + y, in order to perform a Dynamic Glossary Lookup.\n\nIf you wish to perform a Dynamic Glossary Lookup, please select less text with your mouse, then press Ctrl + y.\n\nMultiple words can be selected by clicking and dragging your mouse, or a single word can be selected by double clicking on it.');
		return false;
	}
	
	<!--- Ensure selected text does not contain a single quote.  The lookup routine will fail if there is at least one single quote.  This is because SQL Server uses single quotes for its delimiters. --->
	if (VarSelection.indexOf("'") >= 0)
	{
		alert('Ctrl + y has been pressed, but the selected (highlighted) text contains a least one single quote, which is not permitted.  This page is expecting only one or a few words of text to be selected, before pressing Ctrl + y, in order to perform a Dynamic Glossary Lookup.\n\nIf you wish to perform a Dynamic Glossary Lookup, please select non quoted text with your mouse, then press Ctrl + y.\n\nMultiple words can be selected by clicking and dragging your mouse, or a single word can be selected by double clicking on it.');
		return false;
	}
		
	<!--- The escape function encodes the variables should there be any spaces in the term so that it can safely be sent as a query string to the asp program. --->
	VarSelection = escape(VarSelection);
	
	<!--- Once the selected text has been obtained, edited and escaped, it is used as a parameter for the asp program which will look up the term in the glossary definition. The document.domain property is used so that this code can run in Devl or Prod --->
	TheURL = "http://"+document.domain+"/scripts/glossary/GlossaryResultSet.asp?Term=" + VarSelection + "&URL=" + self.document.URL;

	<!--- Call the function to display the child window. --->
	DisplayGlossaryWindow(TheURL);
	
} //End of Selection function.


<!--- -------------------------------------------------------------------- --->
<!--- This function is called by the Selection function.  It determines the user's screen size and divides the screen into 4 quadrants to open the child window in a quadrant away from where the user highlighted the text. It opens the window at 75% of the quandrant size and at 10% from the quandrant's left and top borders. --->
function DisplayGlossaryWindow(TheURL)
{
	<!--- Declare function variables. --->
	var VarScreenWidth;		//The screen's Width.
	var VarScreenHeight;	//The screen's Height.
	var VarTextQuad;		//The quad number of where the highlighted text is.
	var VarGlossaryQuad;	//The quad number of where the Glossary window will be opened.
	var VarGlossaryWidth;	//The calculated glossary window's width.
	var VarGlossaryHeight;	//The calculated glossary window's height.
	var VarGlossaryX;		//The calculated glossary window's horizontal point.
	var VarGlossaryY;		//The calculated glossary window's vertical point.
	
	var VarFeatures;		//The glossary window attributes.
	
	<!--- Declare 2D array. --->
	<!--- The Parent Array represents the 4 quadrants, with 1 and 2 on the top going from left to right and 3 and 4 at the bottom, also from left to right. --->
	var ArQuad = new Array(4);
	
	<!--- Loop through each of the four parent elements (a quadrant), and declare a Sub Array for the 4 sides of each quadrant in the following order: --->
		 <!---  Left, Right Top, Bottom. --->
	for (i=0; i<4; i++)
	{
		ArQuad[i] = new Array (4);
	}
	
	<!--- Determine the user's screen size (resolution). --->
	VarScreenWidth = window.screen.availWidth;
	VarScreenHeight = window.screen.availHeight;
	
	<!--- Fill the array by calculating the dimensions of each of the 4 quadrants.  Element one always starts as element zero in Javascript. --->
	<!--- Quadrant 1 --->
	ArQuad[0][0] = 0;						//Left
	ArQuad[0][1] = VarScreenWidth / 2;		//Right
	ArQuad[0][2] = 0;						//Top
	ArQuad[0][3] = VarScreenHeight / 2;		//Bottom
	
	<!--- Quadrant 2 --->
	ArQuad[1][0] = (VarScreenWidth / 2) + 1;	//Left
	ArQuad[1][1] = VarScreenWidth;			//Right
	ArQuad[1][2] = 0;						//Top
	ArQuad[1][3] = VarScreenHeight / 2;		//Bottom
	
	<!--- Quadrant 3 --->
	ArQuad[2][0] = 0;						//Left
	ArQuad[2][1] = VarScreenWidth / 2;		//Right
	ArQuad[2][2] = (VarScreenHeight / 2) + 1;//Top
	ArQuad[2][3] = VarScreenHeight;			//Bottom
	
	<!--- Quadrant 4 --->
	ArQuad[3][0] = (VarScreenWidth / 2) + 1;	//Left
	ArQuad[3][1] = VarScreenWidth;			//Right
	ArQuad[3][2] = (VarScreenHeight / 2) + 1;//Top
	ArQuad[3][3] = VarScreenHeight;			//Bottom
	
	<!--- Determine which quadrant the highlighted text is in.  The X coordinate is horizontal and the Y is vertical.  --->
	for (i=0; i<4; i++)
	{
		if(	VarTextX > ArQuad[i][0] &&
			VarTextX <= ArQuad[i][1] &&
			VarTextY > ArQuad[i][2] &&
			VarTextY <= ArQuad[i][3] )
		{
			break;
		}
	}
	
	<!--- Calculate which quadrant to open the Glossary window so that it does not cover the highlighted text.  This is subjective.  If text is found in any quadrant other than the top right one, then open in top right.  Else if text is found in the top right, then open in bottom right.  Remember that the numbers below refer to the quadrant from the array point of view.   As explained above when declaring the array, Quadrant 1 and 2 are on the top going from left to right and 3 and 4 at the bottom, also from left to right. However the values are one less in the array due to the array indexes starting at zero instead of 1.  --->
	if(	i == 0 ||
		i == 2 ||
		i == 3)
	{
		VarGlossaryQuad = 1;
	}
	else
	{
		VarGlossaryQuad = 3;
	}
	
	<!--- Calculate the width and the height of the Glossary window to be 75% of the quadrant size. --->	
	VarGlossaryWidth = ((ArQuad[VarGlossaryQuad][1] - ArQuad[VarGlossaryQuad][0]) * 75) / 100;
	VarGlossaryHeight = ((ArQuad[VarGlossaryQuad][3] - ArQuad[VarGlossaryQuad][2]) * 75) / 100;
	
	<!--- Calculate the x (horizontal) and y (vertical) coordinates within the quadrant to place the window on:  Calculate 10% of the quadrant size and add those pixels to the left and top boundaries respectively in order to place the window 10% in from the two top left borders.  --->
	VarGlossaryX = ArQuad[VarGlossaryQuad][0] + (((ArQuad[VarGlossaryQuad][1] - ArQuad[VarGlossaryQuad][0]) * 10) / 100);
	VarGlossaryY = ArQuad[VarGlossaryQuad][2] + (((ArQuad[VarGlossaryQuad][3] - ArQuad[VarGlossaryQuad][2]) * 10) / 100);

	<!--- A new small window is opened (while keeping the parent window open) and the glossary definition is displayed.  The glossary window attributes are specified in a "features" variable, because the open function doesn't accept value variables in its expected comma delimited list of hardcoded features. --->

	VarFeatures =	"width=" + VarGlossaryWidth +
				 	",height=" + VarGlossaryHeight +
					",left=" + VarGlossaryX +
					",top=" + VarGlossaryY +
					",resizable=yes,scrollbars=yes";

	window.open(TheURL, "", VarFeatures);

} //End of DisplayGlossaryWindow function.




<!--- ----------------------------------------------------------------- --->
<!--- Define onkeypress Event Handler. --->

if (VarNetscapeBrowser)
{
	<!--- With Netscape, when someone presses a key, we have to capture the specific event type of Event.KEYPRESS (which is an event mask) instead of to the object on which it actually occurred. --->
   	document.captureEvents(Event.KEYPRESS);
}

<!--- Assign a function to the onKeyPress event handler so that when it is triggered later, (when someone has pressed a key), it will call the function.  The event object (e) is passed to the DetermineKey function implicitly by the onkeypress event.  In other words, we don't call the function specifying the parameter (object).  --->
document.onkeypress = DetermineKey;

<!--- End Glossary JavaScript --->
<!--- ----------------------------------------------------------------- --->

