// capitalize the first letter of each word and make all other letters lowercase
// (words are divided by the following characters: " ", "-", "'", and ".")
function FixNameCapitalization(textBox, spacesAllowed) {
  try {
    FixNameCapitalizationEachChar('', true, spacesAllowed);   // initialize function
    fieldval = String(textBox.value);
    var newfieldval = '';           // rebuild the original string to have correct capitalization
    for (i = 0; i < fieldval.length; i ++)
      newfieldval += FixNameCapitalizationEachChar(fieldval.charAt(i), false, spacesAllowed);
    if (textBox.value != newfieldval)    // don't screw up arrow key functionality in IE
      textBox.value = newfieldval;
    // force the field to be updated in some browsers that need it (IE 5.1 for MacOS 9)
    var dummyVariable = textBox.value;
  } catch (e) {
    // alert(e);
  }
}
function FixNameCapitalizationEachChar(character, reset, spacesAllowed) {
  if (reset) {
    wordlength = 0;
    wordsofar = '';
    return;
  }
  if (character == ' ' || character == '-' || character == "'" || character == '.') {
    wordlength = 0;
    wordsofar = '';
    // alert('character ' + character + ': found divider (word length reset to 0)');
  } else {
    wordlength ++;
    // alert('character ' + character + ': wordlength incremented to ' + wordlength);
  }
  if (wordlength == 1)
    character = character.toUpperCase();
  else if (wordsofar == 'Mc' || wordsofar == 'Mac')
    character = character;         // don't change case, in case of MacDonald or McCormick
  else if (wordlength > 1)
    character = character.toLowerCase();
  if (!isNaN(character) && character != ' ')     // remove digits
    character = '';
  if (character == ' ' && !spacesAllowed)        // remove spaces if desired
    character = '';
  // alert("new character: " + character);
  wordsofar += character;
  return character;
}
FixNameCapitalizationEachChar.wordlength = 0;    // create static variable named wordlength
FixNameCapitalizationEachChar.wordsofar = '';    // create static variable named wordsofar


// only allow numerical digits
function FixNumericalField(textBox) {
  try {
    fieldval = String(textBox.value);
    var newfieldval = '';    // rebuild the original string to only contain valid characters
    var digits = 0;         // count the number of numerical digits in the original string
    for (i = 0; i < fieldval.length; i ++) {
      character = fieldval.charAt(i);
      if (!isNaN(character) && character != " ")
        newfieldval += character;
    }
    if (textBox.value != newfieldval)
      textBox.value = newfieldval;
    // force the field to be updated in some browsers that need it (IE 5.1 for MacOS 9)
    var dummyVariable = textBox.value;
  } catch (e) {
    // alert(e);
  }
}


// prevent incorrect phone number characters from being input
// input should only contain digits, " ", and "-"; also, should contain <= 12 digits
function FixPhoneNumber(textBox) {
  try {
    fieldval = String(textBox.value);
    var newfieldval = '';    // rebuild the original string to only contain valid characters
    var digits = 0;         // count the number of numerical digits in the original string
    for (i = 0; i < fieldval.length; i ++) {
      character = fieldval.charAt(i);
      if (!isNaN(character) && character != " ")    // count digits
        digits ++;
      // if valid character, and there aren't more than 12 numerical digits, then concatenate
      if (!isNaN(character) || character == " " || character == "-")
        if (digits <= 12)
          newfieldval += character;
    }
    if (textBox.value != newfieldval)
      textBox.value = newfieldval;
    // force the field to be updated in some browsers that need it (IE 5.1 for MacOS 9)
    var dummyVariable = textBox.value;
  } catch (e) {
    // alert(e);
  }
}


// add thousands commas to a number
function AddCommas(nStr) {
  nStr += '';
  pieces = nStr.split('.');
  piece1 = pieces[0];
  piece2 = (pieces.length > 1) ? '.' + pieces[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(piece1)) {
    piece1 = piece1.replace(rgx, '$1' + ',' + '$2');
  }
  return piece1 + piece2;
}


// get the checked value of a group of radio buttons; works with zero, one, or more radio buttons;
// return the value of the radio button that is checked;
// return an empty string if none are checked, or there are no radio buttons
// e.g.:  radio_obj = document.mainform.elements['fields[SearchChoice]'];
//        radio_val = GetRadioCheckedValue(radio_obj);
function GetRadioCheckedValue(radioObj) {
  if (!radioObj)
    return "";
  var radioLength = radioObj.length;
  if (radioLength == undefined)
    if (radioObj.checked)
      return radioObj.value;
    else
      return "";
  for (var i = 0; i < radioLength; i++) {
    if(radioObj[i].checked) {
      return radioObj[i].value;
    }
  }
  return "";
}
