//  Functions related to events that are automatically triggered

//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//-------------------------------------------------------------------
function isDigit (num) {
  if (num.length > 1) {
    return false;
  }
  var string = "1234567890";
  if (string.indexOf (num) != -1) {
    return true;
  }
  return false;
}

  //  Check all checkboxes
function checkAll (chk) {
  for (i = 0; i < chk.length; i++) {
    chk[i].checked = true ;
  }
}

//  Uncheck all checkboxes
function unCheckAll (chk) {
  for (i = 0; i < chk.length; i++) {
    chk[i].checked = false ;
  }
}


// Set the given radio button
function setCheckedValue (radioObj, newValue) {
  if (!radioObj) {
    return;
  }
  var radioLength = radioObj.length;
  if(radioLength == undefined) {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
  }
  for(var i = 0; i < radioLength; i++) {
    radioObj[i].checked = false;
   if (radioObj[i].value == newValue.toString()) {
     radioObj[i].checked = true;
   }
  }
}


//  Filter range values so that only digits, hyphens, and commas are permitted
function filterRange (id) {
  var str = document.getElementById (id).value;
  var len = str.length;
  var i = 0;
  var newstr = "";
  for (i = 0; i < len; i++) {
    if (isDigit (str[i]) || str[i] == ',' || str[i] == '-') {
      newstr = newstr + str[i];
    }
  }
  document.getElementById (id).value = newstr;
}


//  Filter range values so that only digits are permitted
function filterDigits (id) {
  var str = document.getElementById (id).value;
  var len = str.length;
  var i = 0;
  var newstr = "";
  for (i = 0; i < len; i++) {
    if (isDigit (str[i])) {
      newstr = newstr + str[i];
    }
  }
  document.getElementById (id).value = newstr;
}


//  Filter values so that only digits are permitted.  + and - permitted
//  only as the first character
function filterSignedDigits (id) {
  var str = document.getElementById (id).value;
  var len = str.length;
  var i = 0;
  var newstr = "";
  for (i = 0; i < len; i++) {
    if ((i == 0) && (str[i] == '+' || str[i] == '-')) {
      newstr = newstr + str[i];
    }
    else if (isDigit (str[i])) {
      newstr = newstr + str[i];
    }
  }
  document.getElementById (id).value = newstr;
}

//  Filter range values so that only digits and the decimal point are permitted
function filterDigitsFloat (id) {
  var str = document.getElementById (id).value;
  var len = str.length;
  var i = 0;
  var newstr = "";
  var pt_count = 0;  //  Count the number of decimal points
  for (i = 0; i < len; i++) {
    if ((str[i] == '.') && (pt_count == 0))  {
      newstr = newstr + str[i];
      pt_count++;
    }
    else if (isDigit (str[i])) {
      newstr = newstr + str[i];
    }
  }
  document.getElementById (id).value = newstr;
}
