// ----------------------------------------------------------------------
// Javascript form validation routines.
// Author: Stephen Poley
//
// File 2: checkboxes
// Uses the msg routine from formval.js
// ----------------------------------------------------------------------

var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
var emptyString = /^\s*$/

// -----------------------------------------
//                  msg
// Display warn/error message in HTML element
// commonCheck routine must have previously been called
// -----------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
}


// -----------------------------------------
//            commonCheck2
// Common code for checkbox validation routines to
// check for older / less-equipped browsers
// Returns true (validation passed) or
//         proceed (don't know yet)
// -----------------------------------------

var proceed = 2;

function commonCheck2 (ifld)   // id of element to receive info/error msg
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node  

  msg (ifld, "warn", "");  // clear any previous error message
  return proceed;
}



// -----------------------------------------
//            validateCheckbox
// Validate that the correct number of checkboxes has been checked.
// Returns true if valid (and also if could not be executed because 
// of old browser)
// -----------------------------------------

function validateCheckbox  (numstatements)   // checkboxes to be validated
{
  var nr = 2;
  var ifld = "inf_" + numstatements;
  var stat = commonCheck2(ifld);
  if (stat != proceed) return stat;
  
  // count how many boxes have been checked by the reader
  var count = 0;
  var element_name = "";
  var element_letters = new Array("", "a", "b", "c");
  for (var j=1; j<4; j++) {
    element_name = numstatements + element_letters[j];
    if (document.forms["resilience"].elements[element_name].checked) count++;
  }
  
  if (count<=nr && count>0) return true;

  // if we get here then the validation has failed
  
  var suffix='';
  if (count>1) suffix='es';

  var errorMsg;

  if (count>nr) errorMsg = '' + count + ' boxes checked: maximum ' + nr + ' allowed';
  if (count==0) errorMsg = 'No boxes checked: 1 or 2 required';

  msg (ifld, "error", errorMsg);
  return false;
}

