
// ----------------------------------------------------------------------
/// Set the sort column for this page and submit the form.  Positive
/// numbers indicate an ascending sort, negative numbers a descending
/// sort.
// ----------------------------------------------------------------------

function setSortOrder(f, id)
{
  var current = f.sort_column.value;
  f.sort_column.value = ( "" != current && id == current
                          ? - id
                          : id );
  f.submit();
}  // setSortOrder(()

// ----------------------------------------------------------------------
/// Set the page operation for the news archive.  Current operations are
/// 'first', 'last', 'next', and 'prev'.
// ----------------------------------------------------------------------

function setPageOperation(f, operation)
{
  f.operation.value = operation;
  f.submit();
}  // setPageOperation(()

// ----------------------------------------------------------------------
/// Open a window to display an article
// ----------------------------------------------------------------------

function openWindow(url, title)
{
  var win = window.open(url, title, 'toolbar=no,width=700,height=500,scrollbars=yes');
  win.focus();
} // openWindow()

// ----------------------------------------------------------------------
/// Display an alert of more than the specified number of checkboxes have
/// been selected.
///
/// @param f The form object
/// @param max The upper limit on the number of checks before an alert
///   is issued
/// @param prefix The prefix to be matched against the checkbox name
// ----------------------------------------------------------------------

function countChecks(f, max, prefix)
{
  var count = 0;

  for (var i=0; i < f.length; ++i) {
    if (f.elements[i].type == 'checkbox') {
      if ( 0 == f.elements[i].name.indexOf(prefix) &&
           true == f.elements[i].checked )
      {
        count++;
      }
    }
  }  // for (var i=0; i < f.length; ++i)

  if ( count > max )
  {
    alert("You have selected " + count + " items for preview.  We do " +
          "not recommend more than " + max + " items.");
  }

  return true;

} // countChecks()


// ----------------------------------------------------------------------
/// Dynamically generate potential answer and question form fields for
/// the radio button matrix that will be placed in the appropriate
/// <div> tags.  Any information that was previously entered into the
/// form is saved if the user changes the number of questions or
/// answers.
///
/// @param f The form object
// ----------------------------------------------------------------------

function display_matrix_answer_fields(f)
{
  // Initialize

  var numAnswers = f.num_answers.value;
  var numQuestions = f.num_questions.value;
  var str = "";
  var newDesignator = "new_";

  // ----------------------------------------------------------------------
  // Generate the potential answer html

  str = "<span class='heading_med_l'> Potential Answers </span><br><br>";
  str += "<table border=0>\n";

  for ( i=1; i <= numAnswers; i++ )
  {
    // If answerIdList is not empty then we are editing an existing
    // question.  Usse the corresponding id from the list or if there
    // are more form fields than answers use an identifier that lets
    // us know we need to create new answers.

    var answerId = ( i > answerIdList.length ? newDesignator + i : answerIdList[i-1] );

    // Save any existing form values

    var val = "";
    evalStr = "val = ( null == f.answer_" + answerId + " ? '' : f.answer_" + answerId + ".value )";
    eval( evalStr );
    
    // Using the syntax name='answer[xxx]' will cause PHP to create an
    // array called "answer" with an index for each "xxx" holding the
    // corresponding value.  Since it's a pain to access the
    // answer[xxx] via javascript without looping through all of the
    // form elements, we'll mirror a copy of the element value in a
    // hidden element with a simpler name.

    str += "<tr><td class='label_med_l'>Answer " + i + ":</td><td>" +
      "<textarea name='answer[" + answerId + "]' rows=3 cols=80 onBlur='answer_" + answerId + ".value = this.value;'>" + val + "</textarea>" +
      "<input type='hidden' name='answer_" + answerId + "' value='" + val + "'>" +
      "</td></tr>\n";
  }

  str += "</table>\n";
  document.getElementById('answers').innerHTML = str;

  // ----------------------------------------------------------------------
  // Generate the question html

  str = "<span class='heading_med_l'> Questions </span><br><br>";
  str += "<table border=0>\n";

  for ( i=1; i <= numQuestions; i++ )
  {
    // If questionIdList is not empty then we are editing an existing
    // question.  Usse the corresponding id from the list or if there
    // are more form fields than questions use an identifier that lets
    // us know we need to create new questions.

    var questionId = ( i > questionIdList.length ? newDesignator + i : questionIdList[i-1] );

    // Save any existing form values

    var val = "";
    evalStr = "val = ( null == f.question_" + questionId + " ? '' : f.question_" + questionId + ".value )";
    eval( evalStr );

    // Using the syntax name='answer[xxx]' will cause PHP to create an
    // array called "answer" with an index for each "xxx" holding the
    // corresponding value.  Since it's a pain to access the
    // answer[xxx] via javascript without looping through all of the
    // form elements, we'll mirror a copy of the element value in a
    // hidden element with a simpler name.

    str += "<tr><td class='label_med_l'>Question " + i + ":</td><td>" +
      "<textarea name='question[" + questionId + "]' rows=3 cols=80 onBlur='question_" + questionId + ".value = this.value;'>" + val + "</textarea>" +
      "<input type='hidden' name='question_" + questionId + "' value='" + val + "'>" +
      "</td></tr>\n";
  }

  str += "</table>\n";
  document.getElementById('questions').innerHTML = str;

}  // display_matrix_answer_fields()


// ----------------------------------------------------------------------
/// Dynamically generate the potential answer form fields for radio
/// and checkbox list questions that will be placed in the appropriate
/// <div> tags.  Any information that was previously entered into the
/// form is saved if the user changes the number of questions or
/// answers.
///
/// @param f The form object
// ----------------------------------------------------------------------

function display_list_answer_fields(f)
{
  // Initialize

  var numAnswers = f.num_answers.value;
  var str = "";
  var newDesignator = "new_";

  // ----------------------------------------------------------------------
  // Generate the potential answer html

  str = "<span class='heading_med_l'> Answers </span><br><br>";
  str += "<table border=0>\n";

  for ( i=1; i <= numAnswers; i++ )
  {
    // If answerIdList is not empty then we are editing an existing
    // question.  Usse the corresponding id from the list or if there
    // are more form fields than answers use an identifier that lets
    // us know we need to create new answers.

    var answerId = ( i > answerIdList.length ? newDesignator + i : answerIdList[i-1] );

    // Save any existing form values

    var val = "";
    evalStr = "answerVal = ( null == f.answer_" + answerId + " ? '' : f.answer_" + answerId + ".value )";
    eval( evalStr );
    evalStr = "noteVal = ( null == f.answer_note_" + answerId + " ? '' : f.answer_note_" + answerId + ".value )";
    eval( evalStr );
    
    // Using the syntax name='answer[xxx]' will cause PHP to create an
    // array called "answer" with an index for each "xxx" holding the
    // corresponding value.  Since it's a pain to access the
    // answer[xxx] via javascript without looping through all of the
    // form elements, we'll mirror a copy of the element value in a
    // hidden element with a simpler name.

    str += "<tr><td class='label_med_l'>Answer " + i + ":</td><td>" +
      "<textarea name='answer[" + answerId + "]' rows=3 cols=80 onBlur='answer_" + answerId + ".value = this.value;'>" + answerVal + "</textarea>" +
      "<input type='hidden' name='answer_" + answerId + "' value='" + answerVal + "'>" +
      "</td></tr>\n";
    str += "<tr><td class='label_med_l'>Note " + i + ":</td><td>" +
      "<textarea name='answer_note[" + answerId + "]' rows=3 cols=80 onBlur='answer_note_" + answerId + ".value = this.value;'>" + noteVal + "</textarea>" +
      "<input type='hidden' name='answer_note_" + answerId + "' value='" + noteVal + "'>" +
      "</td></tr>\n";
  }

  str += "</table>\n";
  document.getElementById('answers').innerHTML = str;

}  // display_list_answer_fields()


// ----------------------------------------------------------------------
/// Dynamically generate the matrix column label form fields for
/// matrix questions.  Form fields will be placed in the appropriate
/// <div> tags.  Any information that was previously entered into the
/// form is saved if the user changes the number of questions or
/// answers.
///
/// @param f The form object
// ----------------------------------------------------------------------

function display_matrix_label_fields(f, labelText)
{
  // Initialize

  var numColumns = f.num_cols.value;
  var str = "";
  var newDesignator = "new_";

  // ----------------------------------------------------------------------
  // Generate the potential answer html

  str = "<span class='heading_med_l'> " + labelText + " Labels </span><br><br>";
  str += "<table border=0>\n";

  for ( i=1; i <= numColumns; i++ )
  {
    // If columnIdList is not empty then we are editing an existing
    // question.  Usse the corresponding id from the list or if there
    // are more form fields than answers use an identifier that lets
    // us know we need to create new answers.

    var columnId = ( i > columnIdList.length ? newDesignator + i : columnIdList[i-1] );

    // Save any existing form values

    var val = "";
    evalStr = "labelVal = ( null == f.label_" + columnId + " ? '' : f.label_" + columnId + ".value )";
    eval( evalStr );
    
    // Using the syntax name='label[xxx]' will cause PHP to create an
    // array called "label" with an index for each "xxx" holding the
    // corresponding value.  Since it's a pain to access the
    // label[xxx] via javascript without looping through all of the
    // form elements, we'll mirror a copy of the element value in a
    // hidden element with a simpler name.

    str += "<tr><td class='label_med_l'>" + labelText + " " + i + " Label:</td><td>" +
      "<textarea name='label[" + columnId + "]' rows=3 cols=80 onBlur='label_" + columnId + ".value = this.value;'>" + labelVal + "</textarea>" +
      "<input type='hidden' name='label_" + columnId + "' value='" + labelVal + "'>" +
      "</td></tr>\n";
  }  // for ( i=1; i <= numColumns; i++ )

  str += "</table>\n";
  document.getElementById('labels').innerHTML = str;

}  // display_matrix_label_fields()

// ----------------------------------------------------------------------
// Confirm deletion of several items in a list of checkboxes.  It is
// assumed that the form has an element called "operation" and that
// this element will have a value of "delete" if the user clicked the
// delete button.
//
// @param f The form
// @param elementName The name of the html element (without the '[]')
// @param name The name of the list to be used in dialog boxes.
// ----------------------------------------------------------------------

function confirmDelete(f, elementName, displayName)
{
  var msg = "";
  var numChecked = 0;

  // Skip this routine if the delete button was not checked.

  if ("delete" != f.operation.value) return true;

  deleteArray = f.elements[elementName + '[]'];

  // If only 1 element was defined for the del_id[] array then the value
  // will be a single checkbox.

  if ( deleteArray.length == undefined ) {
    if (deleteArray.checked) { numChecked++; }
  } else {
    for (var i=0; i < deleteArray.length; i++) {
      if (deleteArray[i].checked) { numChecked++; }
    }
  }

  if ( numChecked > 0 ) {
    return confirm("Are you sure that you want to delete " + numChecked +
                   " " + displayName + (numChecked == 1 ? "" : "s") + "?");
  } else {
    alert("No " + displayName + "s were selected for deletion.");
    return false;
  }
}  // confirmDelete()

// ----------------------------------------------------------------------
// When a user clicks on a sortable hearder column in a Pear
// Structures_DataGrid submit the form and sort the DataGrid on that
// column.  It is assumed that the form method is POST and that the
// same page that generated the form is able to handle the sort (hence
// setting the form action to '#').
//
// @param f The form to submit
// @param sortCol The Structures_DataGrid sort column
// ----------------------------------------------------------------------

function submitDataGridSort(f, sortCol)
{
  f.action = null;
  f.sort.value = sortCol;
  f.submit();
}  // submitDataGridSort()
