/*************************************************************************************
*		The following functions are for disabling and enabling fields that are relevant 
*		based on which radio button or section the user is completing.
*
*	History:	Created 5/17 by Andy Sabaka 
*************************************************************************************/

function disableThis(field) {
	field.disabled = true; 
	if (field.type != 'checkbox'){
		field.value = "";
	} 
	field.text = "";
	field.checked = 0;
	field.style.backgroundColor = '#C0C0C0';  // the color #C0C0C0 is silver
}

function enableThis(field) {
	field.disabled = false;
	//field.style.backgroundColor = 'window';
	field.style.backgroundColor = 'white';

}


//determines if element is in an array
//returns true or false
//JEM 3/7/08  
function isFound(ary, element){     
   for(var i=0; i<ary.length; i++) {   
      if(ary[i] == element){      
         return true;        
      }    
   }    
   return false;
}

//returns first form object whose name matches the given pattern, otherwise returns NULL
function getFormObject(pattern) {
	var object = null;
	var found = false;
	var i = 0;

	with (document.forms[0]) {
		while (!found && i<elements.length) {
//		   alert(elements[i].name + ' value=' + elements[i].value + ' checked=' + elements[i].checked);
			if (pattern.test(elements[i].name)) { //found object
//			alert('Match: '+fieldname+'; '+pattern);   
				object = elements[i];
				found = true;
			}
			else {
				i++;
			}
		}
	}		
	return object;
} //getFormObject

//returns first form object whose name matches the given pattern, otherwise returns NULL
function getFormObjectReverse(pattern) {
	var object = null;
	var found = false;
	
	with (document.forms[0]) {
		var i = elements.length-1;
		while (!found && i>0) {
			if (pattern.test(elements[i].name)) { //found object
//			alert('Match: '+fieldname+'; '+pattern);   
				object = elements[i];
				found = true;
			}
			else {
				i--;
			}
		}
	}	
	return object;
} //getFormObject

/*****************************************************************************
* Purpose: 	Get selected item from pop-up menu
* Params:		element - pop-up menu object
* Returns:	string containing selected item
* Author : 	Jim Moler
* Date:    	06/28/02
*****************************************************************************/
function getSelectedItem(element) {
	var i=0;
	var found = false;
	var selected = "";
	while (i<element.length && !found) {
		if (element.options[i].selected) {
			found = true;
			selected = element.options[i].value;
		}
		i++;
	}  	
	return selected;
} //getSelectedItem




/*****************************************************************************
* Purpose: 	Determines if any publication checkboxes are checked.  If not,
*           an alert is given.  If so, we ask for confirmation to link 
*           selected publications to the study.
* Params:	none
* Returns:	boolean
* Author : 	Jim Moler
* Date:    	08/14/08
*****************************************************************************/
function linkSelectedPubsToStudy() {
     
   var num_checked = 0;
   var i=0;
	while (i < document.forms[0].elements['pub_ids'].length) {
      if (document.forms[0].elements['pub_ids'][i].checked) { 
         num_checked++;
      }
      i++;
	} 
      
   if (num_checked==0) {
      alert('In order to link a publication to the study, you must select the publication by checking the appropriate box.');
      return false;
   }
   else {
      return confirm('Are you sure you want to link the selected publications to the study?');
   }
   		
} //linkSelectedPubsToStudy



/*****************************************************************************
* Purpose: 	opens the document defined by strDoc in a new window using the default settings
* Params:		strDoc - string containing URL to be displayed
* Returns:	none
* Author : 	Andy Sabaka
* Date:    	March 2005
*****************************************************************************/
function newWindow(strDoc)
{
	  var winTrack = window.open(strDoc);
	  winTrack.focus();
} //newWindow


function popUp(strDoc)
{
	  var winTrack = window.open(strDoc, "", "width=750,height=600,scrollbars=1,resizable=yes");	  
	  winTrack.focus();
}


function smallPopUp(strDoc)
{
	  var winTrack = window.open(strDoc, "", "width=400,height=350,scrollbars=1,resizable=yes");
	  winTrack.focus();
}


function isReady(CommentForm)
 {
	test='true';
	email=document.CommentForm.email.value;
	pos=email.search('@');
	if (pos==-1) {test='false';}
	if (document.CommentForm.email.value == '')
	  { alert("Please enter your email.");
   	    CommentForm.email.focus();	 
            return false;
          }
	else if (test == 'false')
	  { alert("Please enter a valid email.");
   	    CommentForm.email.focus();	 
            return false;
          }
        else return true;

}


