/*==================================================================================================

FILE : /rsc/js/molpages.js

CREATED : 31 March 2004

LAST MODIFIED : $Revision: 1.10 $ ($Date: 2006/11/22 22:07:53 $ by $Author: slyon $)

AUTHOR : Warren Hedley and Yuhong Ning

DESCRIPTION : This script file contains functions that are used throughout the Molecule Pages
  application.

  All function names start with 'mp' to help avoid naming conflicts.

==================================================================================================*/

/**
 * Opens a new window at the specified URI with the specified name (analgous to a target frame)
 * and of the specified width and height. The window has no decorations other than a status bar.
 * Width and height parameters can be omitted, in which case the new window should be the same
 * size as the current browser window.
 */
function mpOpenNewWindow(uri, name, width, height) {
  var params = 'menubar=no,toolbar=no,location=no,directories=no,status,scrollbars,resizable';
  if (typeof width != 'undefined') {
    params += ',width=' + width;
  }
  if (typeof height != 'undefined') {
    params += ',height=' + height;
  }
  var newWindow = window.open(uri, name, params);
  newWindow.focus();
  if (newWindow.opener == null) {
    newWindow.opener = self;
  }
} // mpOpenNewWindow()



/**
 * Checks that the specified form has a field called 'afcsid' which has a value specified,
 * returning true if it does, or alerting the user and returning false if not. This is used in
 * the AfCS and NPG tool lists in the forms that allow them to jump straight to an admin page.
 */
function mpVerifyAfcsIdSpecified(form)
{
  var afcsId = form.afcsid.value;
  var afcsIdSpecified = afcsId != null && afcsId != "";
  if (!afcsIdSpecified)
  {
    alert("You must specify a Molecule Page ID.");
  }
  return afcsIdSpecified;
} // function mpVerifyAfcsIdSpecified(form)



/**
 * Sets the value of all of the checkboxes with the specified element name in the specified form
 * to the specified value, which should be a boolean (e.g., true or false).
 */
function mpToggleCheckboxes(formName, elementName, newValue) {
  var form = document[formName];
  if (form != null) {
    for (var i=0,len=form.elements.length;len>i;i++) {
      var e = form.elements[i];
      if (e.name == elementName) {
        e.checked = newValue;
      }
    }
  }
  return false;
} // function mpToggleCheckboxes(formName, elementName, newValue)



/**
 * Submits the specified form after setting the value of the action parameter (presumably a
 * hidden input element) to the specified value.
 */
function mpSubmitFormWithAction(formName, actionValue) {
  mpFormSetAndSubmit(formName, 'action', actionValue);
} // function mpSubmitFormWithAction(formName, actionValue)



/**
 * Submits the named form after setting the value of the named parameter (presumably a
 * hidden input element) to the specified value.
 */
function mpFormSetAndSubmit(formName, paramName, paramValue) {
  var form = document[formName];
  if (form != null) {
    form.elements[paramName].value = paramValue;
    form.submit();
  }
  return false;
} // function mpFormSetAndSubmit(formName, paramName, paramValue)



/**
 * Used on the NPG review admin page to select two submissions to be compared.
 */
function diffSubmissions(anchor) {
  var form = document.npgsubtable;
  if (form == null) {
    alert("JAVASCRIPT ERROR: couldn't find form npgsubtable.");
    return false;
  }
  var scb1 = -1, scb2 = -1;
  for (var i=0,len=form.elements.length;len>i;i++) {
    var e = form.elements[i];
    if (e.name == 'subid' && e.checked) {
      if (scb1 == -1) {
        scb1 = e.value;
      }
      else if (scb2 == -1) {
        scb2 = e.value;
      }
      else {
        alert("You must select exactly two submissions to compare.");
        return false;
      }
    }
  }
  if (scb1 == -1 || scb2 == -1) {
    alert("You must select exactly two submissions to compare.");
    return false;
  }
  anchor.href = anchor.href + "&subid=" +
      scb1 + "&subid=" + scb2;
  return true;
}
