/*
 * Common JavaScript code for the Tool Repository 
 */
 
// Move browser to a new URL
function goto_url(url) 
{
	window.location=url;
}

// Callbak of the "Edit the selected individual" buttons: open JSP to edit individual
function edit_individual_in_field(individualIDFieldID) 
{
	var individualURI=document.getElementById(individualIDFieldID).value; // get field value
	if (individualURI != "No selection")
	{
		var url="edit_individual.jsp?individualURI="+individualURI;
		var url = url.replace("#", "!"); // Encode "#" character in URLs (CAAS logout page does not support %23)
		window.open(url); // open new window
	}
}

// Show AIM@SHAPE license and download tool
function show_license(license_url) 
{
	window.open(license_url, "License", "toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=yes, copyhistory=no, width=520, height=520, left=300, top=50"); 
}

// Callbak of the "delete" buttons: ask for confirmation and delete the instance
function delete_individual(delete_url) 
{
	var r=confirm("Do you really want to delete this instance?")
 	if (r==true)
   {
		// Go to http://.../do_delete.jsp?...
		window.location=delete_url;
   }
}

// Enable/disable a field
function enable(id) 
{
	var field=document.getElementById(id);
	field.disabled=false;	
}
//
function disable(id) 
{
	var field=document.getElementById(id);
	field.disabled=true;	
}

// Empty a field
function empty(id) 
{
	var field=document.getElementById(id);
	
	// If regular field
	if (field.type!="file") 
	{
		field.value="";	
	} 
	else // if file upload field
	{
		// If IE
		if (document.all) {
			field.clearAttributes();
		} else {
			// Netscape need privileges to modify input file fields
			if (document.layers && navigator.javaEnabled()) 
				netscape.security.PrivilegeManager.enablePrivilege('UniversalFileRead');

			field.value="";	
		}
	}
}

// Return the first element of the document 
// which has the given name and is not empty and not "No selection".
function getNonEmptyElementByName(name) 
{
	var elements=document.getElementsByName(name);
	for ( var i=0; i<elements.length; i++ ) 
	{
		var field = elements[i];
		if (field.value != "" && field.value != "No selection")
			return field;
	}
	 
	// element is empty if we reach this point
	return null;
}
 
