// -------------------------------------------------- //
//	EXTERNAL JAVASCRIPT FILE
//  IRB PROTOTYPE
// -------------------------------------------------- //


// GLOBAL VARIABLES
var returnField;
var fileReturnField;
var fileNameReturnField;
var maxCharacters;
var URL;
var NetscapeSpecial;
var YES = 'Y';
var POPUP_FLAG = "popup_flag";

/*********************************************************
 * FUNCTION checkAll
 * Given a set of check boxes with the same name,
 * checks all of them
*********************************************************/
		function checkAll(field) {
			if (!field)
				return;
			if (field.checked==false) {
				field.click();
				return;
			}			
			for (i=0; i<field.length; i++) {
		     if (field[i].checked==false)
				field[i].click();
			}
		}

		/*********************************************************
		 * FUNCTION clearAll
		 * Given a set of check boxes with the same name,
		 * clears all of them
		*********************************************************/   
		function clearAll(field) {
			if (!field)
				return;
			if (field.checked==true) {
				field.click();
				return;
			}			
			for (i=0; i<field.length; i++) {
		     if (field[i].checked==true)
				field[i].click();
			}
		}


		/*********************************************************
		 * FUNCTION getRadioValue
		 * Determines the value of the radio button object.
		 * If there are multiple radio buttons with the same name
		 * this will find out which one is selected
		*********************************************************/
		function getRadioValue( radioObject )
		{
			var i;

			for (i=0; i<radioObject.length; i++) {
				if (radioObject[i].checked)
					return radioObject[i].value;
			}
			return radioObject.length;
		}

		/*********************************************************
		 * FUNCTION getNumSelected
		 * Determines the number of selections of a checkbox field
       * or a radio button field
		*********************************************************/
		function getNumSelected( checkboxObject )
		{
			var i;
			var count = 0;
			
			if (checkboxObject.checked)
			   return 1;

			for (i=0; i<checkboxObject.length; i++) {
				if (checkboxObject[i].checked)
					count++;
			}
			return count;
		}

		// Netscape Browsers before version 6 do not refresh the size of menu boxes
		if ((navigator.appName == "Netscape") && (navigator.appVersion < '6' )){
			NetscapeSpecial = true;
		}
		else {
			NetscapeSpecial = false;
		}

		


		/*********************************************************
		 * FUNCTION toggleURL
		 * Shows or hides the URL for a given link based on a selection
		 * theLink: which link (an array) to work with
		 * list: the select object to work with
		 * showValue: the value that must be selected in the list
		 *			in order to show the URL
		*********************************************************/
		function toggleURL (theLink, list, showValue) 
		{
			if (list.options[list.selectedIndex].value == showValue)
				openDataWindow(theLink);
			else {
				// find index of element with value showValue
				for(var i=0; i<list.options.length; i++) {
					if(list.options[i].value == showValue) {
						break;
					}
				}
				alert('You must select \"'+list.options[i].text+'\" before clicking this link.');
			}
		}


		/***************************************************
		 * FUNCTION switchSelectAndGoToURL
		 * Changes the Select Box value to the value passed in
		 * and go to the designated URL
		 * theLink: which link (an array) to work with
		 * list: the select object to work with
		 * showValue: the value that must be selected in the list
		 *			in order to show the URL
		****************************************************/
		function switchSelectAndGoToURL(theLink, list, showValue) {
			for (var i=0; i<list.options.length; i++){
				if (list.options[i].value == showValue){
					list.options[i].selected = true;
					break;
				}
			}
			openDataWindow(theLink);
		}

		
		/************************************************************
		 * FUNCTION popUpOnDDLChange
		 * Pop-up window when correct value has been selected in 
		 * the drop down list
		 *
		 * theLink: which link (an array) to work with
		 * theDDL: the drop down list to work with
		 * showValue: the value that must be selected in the list 
		 *			       in order to show the URL
		*************************************************************/
		function popUpOnDDLChange(theLink, list, showValue){
			if (list.options[list.selectedIndex].value == showValue){
				openDataWindow(theLink);
			}
		}
		    
		    
		 /************************************************************
		 * FUNCTION popUpOnDDLChange
		 * Pop-up window when correct value has been selected in 
		 * the drop down list; allows for two different selection
		 * to pop up different pages
		 *
		 * theLink: which link (an array) to work with
		 * theDDL: the drop down list to work with
		 * showValue: the value that must be selected in the list 
		 *			       in order to show the URL
		 * theLink2: the second link to work with
		 * showValue2: the second value that must be selected in the
		 *			       list in order to show second URL
		*************************************************************/
		function popUpOnDDLChange(theLink, list, showValue, theLink2, showValue2){
			if (list.options[list.selectedIndex].value == showValue){
				openDataWindow(theLink);
			} else if (list.options[list.selectedIndex].value == showValue2) {
				openDataWindow(theLink2);
			}
		}    


		/************************************************************
		 * FUNCTION popUpWithParamOnDDLChange
		 * Pop-up window when correct value has been selected in 
		 * the drop down list; allows for two different selection
		 * to pop up different pages.  The value in the drop down list
		 * is passed to the popup as a parameter
		 *
		 * theLink: which link (an array) to work with
		 * theDDL: the drop down list to work with
		 * showValue: the value that must be selected in the list 
		 *			       in order to show the URL
		 * theLink2: the second link to work with
		 * showValue2: the second value that must be selected in the
		 *			       list in order to show second URL
		*************************************************************/
		function popUpWithParamOnDDLChange(theLink, list, paramName, showValue, theLink2, showValue2){
			if (list.options[list.selectedIndex].value == showValue){
				openParamWindow(theLink, paramName, list);
			} else if (list.options[list.selectedIndex].value == showValue2) {
				openParamWindow(theLink2, paramName, list);
			}
		}
		
		
		function getFormIndex (form, fieldName) {
			
			var i;
			
		  for (i = 0; i < form.elements.length; i++)
			if (form.elements[i].name == fieldName) {
			  return i;
			 }
		  return -1;
		}			



		/*********************************************************
		 * FUNCTION jumpPage
		 * Enables the user to jump from one page to another
		 * based on their selection in a dropdown menu
		*********************************************************/		
		 function jumpPage(newLoc){
			newPage = newLoc.options[newLoc.selectedIndex].value;
						
			if (newPage != ""){
			  window.location.href = newPage;
			  return true;
			}
			else return false;
		  }



		/*********************************************************
		 * FUNCTION openWindow:
		 *	Helper function to open a new window
		 *  URL:		URL to be loaded in the new window
		 *	windowName: What to call the new window
		 *  windowFeatures: Attributes of the new window
		 ********************************************************/
		function openWindow( URL, windowName, windowFeatures )
		{
			if(URL.indexOf('?') == -1) {
				URL = URL+'?';
			}
			else {
				URL = URL+'&';
			}
			URL = URL + POPUP_FLAG+'='+YES;
			var newWindow = open( URL, windowName, windowFeatures );
			newWindow.moveTo(12,12);
		if ( newWindow.opener == null ){
		      newWindow.opener = window;
		}
        newWindow.focus();
        
		return newWindow;
		} // end openWindow
		
		/*********************************************************
		 * FUNCTION openExternalWindow:
		 *	Helper function to open a new window to an external page
		 *  URL:		URL to be loaded in the new window
		 *	windowName: What to call the new window
		 *  windowFeatures: Attributes of the new window
		 ********************************************************/
		function openExternalWindow( URL )
		{
			var newWindow = open( URL, "external", "screenX=12,screenY=12,height=450,width=650,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no,z-lock=no" );
			//newWindow.moveTo(12,12);
		if ( newWindow.opener == null ){
		      newWindow.opener = window;
		}
		newWindow.focus();
		
		return newWindow;
		} // end openExternalWindow


		/*********************************************************
		 * FUNCTION openSecondWindow:
		 *	Helper function to open a new window at screen location 24, 24
		 *  URL:		URL to be loaded in the new window
		 *	windowName: What to call the new window
		 *  windowFeatures: Attributes of the new window
		 ********************************************************/
		function openSecondWindow( URL, windowName, windowFeatures )
		{
			if(URL.indexOf('?') == -1) {
				URL = URL+'?';
			}
			else {
				URL = URL+'&';
			}
			URL = URL + POPUP_FLAG+'='+YES;
			var newWindow = open( URL, windowName, windowFeatures );
			newWindow.moveTo(24,24);
		if ( newWindow.opener == null ){
		      newWindow.opener = window;
		}
        newWindow.focus();
        
		return newWindow;
		} // end openWindow
		 
		/*********************************************************
		 * FUNCTION openTextWindow:
		 *  Creates a new window where the user can edit text
		 *	fieldName:	the name of the original form field where 
		 *				the text resides
		 ********************************************************/		
		function openTextWindow( URL, fieldName, maxchar )
		{
			var textWindow;

         maxCharacters = maxchar;
			returnField = fieldName;
			textWindow = openWindow( URL, 'textWindow', "screenX=10,screenY=10,height=600,width=700,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no,z-lock=no" );
			textWindow.focus();
		} // end openTextWindow
		
		/*********************************************************
		 * FUNCTION openHTMLWindow:
		 *  Creates a new window where the user can edit HTML
		 *	fieldName:	the name of the original form field where 
		 *				the text resides
		 ********************************************************/		
		function openHTMLWindow( URL, fieldName, maxchar )
		{
			var textWindow;

         maxCharacters = maxchar;
			returnField = fieldName;
			textWindow = openWindow( URL, 'htmlWindow', "screenX=10,screenY=10,height=600,width=700,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no,z-lock=no" );
			textWindow.focus();
		} // end openTextWindow		


		/*********************************************************
		 * FUNCTION openReturnWindow:
		 *  Creates a new window where the user can edit text
		 *	fieldName:	the name of the original form field where 
		 *				the text resides
		 ********************************************************/		
		function openReturnWindow( URL, fieldName )
		{
			var returnWindow;
			
			returnField = fieldName;
			returnWindow = openWindow( URL, 'returnWindow', "screenX=10,screenY=10,height=600,width=700,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no,z-lock=no" );
			returnWindow.focus();

		} // end openTextWindow 	


		/*********************************************************
	// FUNCTION openDataWindow
	// Creates and opens a new data manipulation page window.
	//   URL:	URL to be loaded in the new window
		*********************************************************/
	function openDataWindow( URL )
		{
			var dataWindow;

			if (URL != "#") {
			dataWindow = openWindow( URL, 'dataWindow', "screenX=10,screenY=10,height=500,width=790,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no,z-lock=no" );
				dataWindow.focus();
			}
	} // end openDataWindow


	/*********************************************************
	// FUNCTION openSecondDataWindow
	// Creates and opens a new data manipulation page window, without affecting existing data windows.
	//   URL:	URL to be loaded in the new window
	*********************************************************/
	function openSecondDataWindow( URL )
		{
			var dataWindow;
			
			if (URL != "#") {
				dataWindow = openSecondWindow( URL, 'dataWindow2', "screenX=20,screenY=20,height=500,width=790,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no,z-lock=no" );
				dataWindow.focus();
			}
	} // end openSecondDataWindow
	
	

		/*********************************************************
	// FUNCTION openFileWindow
	// Creates and opens a new upload file w/ comments window
	//   URL:	URL to be loaded in the new window
		*********************************************************/
	function openFileWindow( URL, fieldName, fileNameField )
		{
			var fileWindow;
			fileReturnField = fieldName;
			fileNameReturnField = fileNameField;
			if (URL != "#") {
               var newURL = URL.concat(fieldName.value);
			   fileWindow = openWindow( newURL, 'fileWindow', "screenX=10,screenY=10,height=250,width=770,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no,z-lock=no" );
			   fileWindow.focus();
			}
	} // end openDataWindow	

		/*********************************************************
	// FUNCTION openReviewWindow
	// Creates and opens a new submission review window
	//   URL:	URL to be loaded in the new window
		*********************************************************/
	function openReviewWindow( URL, fieldName, checkName )
		{

			var revWindow;	
			if (URL != "#") {
               	   revWindow = openWindow( URL, 'reviewWindow', "screenX=10,screenY=10,height=350,width=770,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no,z-lock=no" );
			   revWindow.focus();
			}
	} // end openReviewWindow

		/*********************************************************
	// FUNCTION openInfoWindow
	// Creates and opens a new information (read-only) window.
	//   URL:	URL to be loaded in the new window
		*********************************************************/
	function openInfoWindow( URL )
		{ 
			var infoWindow;
		
			if (URL != "#") {
			infoWindow = openWindow( URL, 'infoWindow', "screenX=10,screenY=10,height=400,width=600,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no,z-lock=no" );
				infoWindow.focus();
			}			   
	} // end openInfoWindow


	/*********************************************************
	// FUNCTION openInfoWindow
	// Creates and opens a new small information (read-only) window.
	//   URL:	URL to be loaded in the new window
	*********************************************************/
	function openSmallInfoWindow( URL )
		{ 
			var infoWindow;
		
			if (URL != "#") {
			infoWindow = openWindow( URL, 'infoWindow', "screenX=10,screenY=10,height=300,width=700,alwaysLowered=no,alwaysRaised=no,channelmode=no,dependent=no,directories=no,fullscreen=no,hotkeys=yes,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no,z-lock=no" );
				infoWindow.focus();
			}			   
	} // end openInfoWindow

	

		function textCounter(field, maxlimit) {
			if (field.value.length > maxlimit) { 
				field.value = field.value.substring(0, maxlimit-1);
				alert('You have reached the limit of ' + maxlimit + ' characters.');
			}
		}





     

		function closeWindow(){
		window.close();
		}




		/*********************************************************
		 * Moves Menu Items in one Menu Box to another Menu Box
		 *
		 *  @param fbox the menu box feild from which an item should be removed
		 *  @param tbox the menu box feild to which an item should be placed
		*********************************************************/
		function move(fbox, tbox) {
			var arrFbox = new Array();
			var arrTbox = new Array();
			var arrLookup = new Array();
			var i;
			for (i = 0; i < tbox.options.length; i++) {
				arrLookup[tbox.options[i].text] = tbox.options[i].value;
			arrTbox[i] = tbox.options[i].text;
			}
			var fLength = 0;
			var tLength = arrTbox.length;
			for(i = 0; i < fbox.options.length; i++) {
				arrLookup[fbox.options[i].text] = fbox.options[i].value;
				if (fbox.options[i].selected && fbox.options[i].value != "") {
					arrTbox[tLength] = fbox.options[i].text;
					tLength++;
				}
				else {
					arrFbox[fLength] = fbox.options[i].text;
					fLength++;
				}
			}
			arrFbox.sort();
			arrTbox.sort();
			fbox.length = 0;
			tbox.length = 0;
			var c;
			for(c = 0; c < arrFbox.length; c++) {
				var no = new Option();
				no.value = arrLookup[arrFbox[c]];
				no.text = arrFbox[c];
				fbox[c] = no;
			}
			for(c = 0; c < arrTbox.length; c++) {
				var no = new Option();
				no.value = arrLookup[arrTbox[c]];
				no.text = arrTbox[c];
				tbox[c] = no;
			}
			// Tim 1/15/02 - commented this out.  
			//if (NetscapeSpecial == true) history.go(0);
		}


	/*********************************************************
	 * Opens a data window with a parameter from the given form element
	 *
	 *  @param url The base url to use
	 *  @param key A string; the key to use for the parameter
	 *  @param valueElement A form element; the value will be passed to the popup window
	*********************************************************/
	function openParamWindow(url, key, valueElement) {
		
		returnField = valueElement;
		
		if(url.indexOf('?') == -1) {
			url += '?';
		}
		else {
			url += '&';
		}
		url += key;
		url += '='
		url += valueElement.value;
		openDataWindow(url);
		return false;
	}

	/*********************************************************
	 * Opens a data window with a parameter from the given form element
	 * The parameter value cannot be null
	 *
	 *  @param url The base url to use
	 *  @param key A string; the key to use for the parameter
	 *  @param valueElement A form element; the value will be passed to the popup window
	 *  @param itemName User-friendly name of the value in valueElement 
	 * 	(used for error messages)
	*********************************************************/
	function openValidParamWindow(url, key, valueElement, itemName) {
	
		if(valueElement.value == null || valueElement.value == '') {
			alert('You must select a '+itemName);
			return false;
		}
		return openParamWindow(url, key, valueElement);
	}
