//-----------------------------------------------------------------------
// Module name   : FORMVAL.js
// Author        : Paul Battersby
// Creation Date : Nov 13/04
// Description   :
//  This module contains routines to support the form in the 85confreg.htm
//  file. This includes dynamically adding and adding up the cost of the
//  of the conference based on the number of tickets being ordered and
//  a routine to format a floating point number as a dollar amount
//
//  Example Usage:
//
//  var validateStruct = {
//    normalColor : "black",
//    errorColor : "red",
//    fields : {
//      Username        : ["lettersOnly", "UserNameTxt",     "required"],
//      UserEmail       : ["email",       "UserEmailTxt",    "required"],
//      UserTel         : ["numbers",     "UserTelTxt"],
//      UserFax         : ["numbers",     "UserFaxTxt"]
//    }
//  };
//
//  <FORM ACTION="jmail.asp" METHOD=POST id=form1 name=form1>
//    <table>
//      <tr>
//        <td class="formLabels" id="UserNameTxt" name="UserNameTxt">Name</td>
//        <td><input type="text" size="35" maxlength="256" name="UserName"
//          onchange="FORMVAL_validateOne(validateStruct,'UserName')"></td>
//      </tr>
//      <tr>
//        <td class="formLabels" id="UserEmailTxt" name="UserEmailTxt">E-mail</td>
//        <td><input type="text" size="35" maxlength="256" name="UserEmail"
//          onchange="FORMVAL_validateOne(validateStruct,'UserEmail')"></td>
//      </tr>
//      <tr>
//        <td class="formLabels" id="UserTelTxt" name="UserTelTxt">Tel</td>
//        <td><input type="text" size="35" maxlength="256" name="UserTel"
//         onchange="FORMVAL_validateOne(validateStruct,'UserTel')"></td>
//      </tr>
//      <tr>
//        <td class="formLabels" id="UserFAXTxt" name="UserFAXTxt">FAX</td>
//        <td><input type="text" size="35" maxlength="256" name="UserFAX"
//          onchange="FORMVAL_validateOne(validateStruct,'UserFAX')"></td>
//      </tr>
//    </table>
//    <p align="center"><input type="submit" value="Submit Comments"></p>
//  </form>
//------------------------------------------------------------------------*/

/* some constants for indexing into the validation structure */
var FORMVAL_VALIDATE_TYPE = 0;
var FORMVAL_ID_TO_CHANGE  = 1;
var FORMVAL_REQUIRED = 2;

/* determine the type of browser */
var FORMVAL_isDOM = (document.getElementById ? true : false);
var FORMVAL_isIE4 = ((document.all && !FORMVAL_isDOM) ? true : false);
var FORMVAL_isNS4 = (document.layers ? true : false);

var FORMVAL_errorMsg = {
  en : {
    lettersOnly    : "may only contain letters, spaces, dots, dashes",
    lettersNumbers : "may only contain letters, numbers, dots, spaces, dashes",
    numbers        : "may only contain numbers, spaces, dots, dashes",
    postalCode     : "is not a valid postal code",
    phone          : "is not a valid phone number",
    phoneStrictAreaCode : "must be in the form 111-222-3333",
    phoneDigits    : "must contain between 10 and 11 digits (including area code)",
    email          : "is not a valid email address",
    currency       : "may only contain numbers, spaces or a dash",
    currencyGt0    : "must not be 0",
    mastercard     : "is not a valid credit card number",
    numDigits      : "does not have the correct number of digits",
    mastercardNot  : "is not a valid Mastercard number",
    visacard       : "is not a valid credit card number",
    visacardNot    : "is not a valid Visacard number",
    noBlank        : "can not be left blank",
    mostChars      : "contains illegal character(s)",
    dateyyyymmdd   : "must be in yyyy-mm-dd format",
    dateText       : "must be in this format: Jan 17 2004",
    errorList      : "The following errors were found, please correct them before proceeding:"
  },

  fr : {
    lettersOnly    : "may only contain letters, spaces, dots, dashes",
    lettersNumbers : "may only contain letters, numbers, dots, spaces, dashes",
    numbers        : "may only contain numbers, spaces, dots, dashes",
    postalCode     : "n'est pas un code postal valide",
    phone          : "n'est pas un numéro de téléphone valide",
    phoneStrictAreaCode : "must be in the form 111-222-3333",
    phoneDigits    : "doit contenir entre 10 et 11 chiffres",
    email          : "n'est pas une adresse courriel valide",
    currency       : "ne doit contenir que des chiffres, des espaces ou un trait",
    currencyGt0    : "ne doit pas être un zéro",
    mastercard     : "n'est pas un numéro de carte de crédit valide",
    numDigits      : "ne contient pas le nombre exact de chiffres",
    mastercardNot  : "le numéro de Mastercard n'est pas valide",
    visacard       : "n'est pas un numéro de carte de crédit valide",
    visacardNot    : "le numéro de Visa n'est pas valide",
    noBlank        : "ne pas laissez en blanc",

    mostChars      : "contains illegal character(s)",
    dateyyyymmdd   : "must be in yyyy-mm-dd format",
    dateText       : "must be in this format: Jan 17 2004",

    errorList      : "Les erreurs suivantes ont été trouvées, s'il vous plaît, les corriger avant de continuer:"
  }
}

var FORMVAL_lang = "en"; /* language to use is english by default */

//************************************************************************
// Name   : FORMVAL_setLang
// Author : Paul Battersby
// Description :
//  This allows the caller to set the language (english or french)
//  that is to be used for error messages
//
// Pre    :
//  "lang" - one of {"en","fr"} indicates the desired language for error
//           reporting. english or french
//
// Post   :
//  the language for error reporting has been set
//
// Returns :
//  (nothing)
//*************************************************************************/
function FORMVAL_setLang(lang)
{
  FORMVAL_lang = lang;
} /* end of FORMVAL_setLang */

//************************************************************************
// Name   : FORMVAL_getRefById
// Author : Paul Battersby
// Description :
//  In a browser independant way, this returns a reference to the HTML
//  element that contains the given "id" value
//
// Pre    :
//  FORMVAL_isDOM, FORMVAL_isIE4, FORMVAL_isNS4 have all been defined
//
//  "id" = the value of the id field in an HTML element
//
// Post   :
//  (nothing)
//
// Returns:
//  reference to an HTML element
//*************************************************************************/
function FORMVAL_getRefById(id)
{
  if (FORMVAL_isDOM) return document.getElementById(id);
  if (FORMVAL_isIE4) return document.all[id];
  if (FORMVAL_isNS4) return document.layers[id];
} /* end of FORMVAL_getRefById */

//************************************************************************
// Name   : FORMVAL_textLimit
// Author : Third Party
// Description :
//  This limits the amount of text that can be entered into a text area
//
// Pre    :
//  "field" - the text area who's max number of characters is to be limited
//  "maxlen" - max number of characters allowed in the text area
//
//    example:   onkeyup="FORMAL_limitChars(this,20)";
//
// Post   :
//    if a cut and paste operation has been performed that exceeds the
//    character limit, a message informing the user that their text has
//    been truncated has been displayed
//
// Returns :
//  (nothing)
//*************************************************************************/
function FORMVAL_textLimit(field, maxlen)
{
  if (field.value.length > maxlen + 1) {
    alert('your input has been truncated!');
  }
  if (field.value.length > maxlen) {
    field.value = field.value.substring(0, maxlen);
  }

} /* end of FORMVAL_textLimit */


//************************************************************************
// Name   : FORMVAL_getCheckedVal
// Author : Paul Battersby
// Description :
//  This determines the value of the checked radio button
//
// Pre    :
//  "radioGroup" - the name of a group of radio buttons
//
// Post   :
//  (nothing)
//
// Returns :
//  the value of the checked radio button
//*************************************************************************/
function FORMVAL_getCheckedVal(radioGroup)
{
  var selectedRadioValue = "";
  var i;

  /* loop through all the radio buttons */
  for ( i = 0; i < radioGroup.length; i++ )
  {
    /* if we've found the one that is currently checked */
    if ( radioGroup[i].checked )
    {
      selectedRadioValue = radioGroup[i].value;
      break;
    }
  }
  return selectedRadioValue;
} /* end of FORMVAL_getCheckedVal */

//************************************************************************
// Name   : FORMVAL_getEntryText
// Author : Paul Battersby
// Description :
//
// Pre    :
//  "entryId" - id of the form entry whose currently selected text is to be returned
//                example:  document.forms.myform.myentry
// Post   :
//  (nothing)
//
// Returns :
//  the currently selected text for the given form entry
//*************************************************************************/
function FORMVAL_getEntryText(entryId)
{
  return entryId.options[entryId.selectedIndex].text;
} /* end of FORMVAL_getEntryText */

//************************************************************************
// Name   : FORMVAL_getRadioText
// Author : Paul Battersby
// Description :
//  This returns the text of the selected radio button to the caller
//
// Pre    :
//  radioId - the id of the radio group
//              ex document.forms.myform.myRadioButtons
// Post   :
//  (nothing)
//
// Returns :
//  Text of the clicked radion button or "" if no radio button has been selected
//*************************************************************************/
function FORMVAL_getRadioText(radioId)
{
  /* loop through the radio buttons */
  for (i=0; i<radioId.length; i++) {

    /* if we found the one that is checked */
    if (radioId[i].checked) {

      /* return the value to the caller */
      return radioId[i].value;
    }; /* end for */
  }; /* end for */

  /* no radio button is checked */
  return "";

} /* end of FORMVAL_getRadioText */

//************************************************************************
// Name   : FORMVAL_replaceSingleQuote
// Author : Paul Battersby
// Description :
//  This takes all "'" characters in the given string and replaces them
//  with "`"
//
// Pre    :
//    "oldString" - any string
//
// Post   :
//  (nothing)
//
// Returns :
//  the given string with all single quotes replaced with "`"
//*************************************************************************/
function FORMVAL_replaceSingleQuote(oldString)
{
  newString = oldString.replace(/\'/g,"`");
  return newString;
} /* end of FORMVAL_replaceSingleQuote */

//************************************************************************
// Name   : FORMVAL_getCheckedIndex
// Author : Paul Battersby
// Description :
//  This determines the index of the checked radio button
//
// Pre    :
//  "radioGroup" - the name of a group of radio buttons
//
// Post   :
//  (nothing)
//
// Returns :
//  the index of the checked radio button
//*************************************************************************/
function FORMVAL_getCheckedIndex(radioGroup)
{
  var selectedRadioIndex = 0;
  var i;

  /* loop through all the radio buttons */
  for ( i = 0; i < radioGroup.length; i++ )
  {
    /* if we've found the one that is currently checked */
    if ( radioGroup[i].checked )
    {
      selectedRadioIndex = i;
      break;
    }; /* endif */
  }; /* end for */

  return selectedRadioIndex;
} /* end of FORMVAL_getCheckedVal */

//************************************************************************
// Name   : FORMVAL_countDigits
// Author : Paul Battersby
// Description :
//  This counts the number of digits in the given string excluding characters,
//  spaces etc.
//
// Pre    :
//  "number" - a number whose digits are to be counted
//
// Post   :
//  (nothing)
//
// Returns :
//  number of digits in "number"
//*************************************************************************/
function FORMVAL_countDigits(number) {
  var matchesList = new Array();

  /* count the digits */
  matchesList = number.match(/\d/g);

  /* if there are no digits at all */
  if ( matchesList == null ) {
    return 0;

  /* return the length of the array returned by the match() method */
  /* which corresponds to the number of digits in the number */
  } else {
    return matchesList.length;
  }; /* endif */

}  /* end of FORMVAL_countDigits */

//************************************************************************
// Name   : FORMVAL_formatError
// Author : Paul Battersby
// Description :
//  This formats and error message and returns it to the caller
//
// Pre    :
//  - "FORMVAL_lang" has been defined
//
//
//  "field" - the text representing the field that about which an error
//            is being reported. In the example that follows, this would
//            be "(first name)"
//
//  "errorId" - the id of the error string that is to be reported. This is
//              used as an index into FORMVAL_errorMsg[]
//
// Post   :
//  (nothing)
//
// Returns:
//  An error message
//
//   ex: "- (first name): may only contain letters, spaces or a dash"
//*************************************************************************/
function FORMVAL_formatError(field,errorId)
{
  var errorString = new String();

  /* if this is not the error list message */
  if ( errorId != "errorList" ) {
    errorString = "\"" + field + "\"- " + FORMVAL_errorMsg[FORMVAL_lang][errorId] + "\n";

  /* this is the error list message */
  } else {
    errorString = FORMVAL_errorMsg[FORMVAL_lang][errorId] + "\n\n";
  }; /* endif */

  return errorString;

} /* end of FORMVAL_formatError */

//************************************************************************
// Name   : FORMVAL_validateOne
// Author : Paul Battersby
// Description :
//    This determines if a single form element contains valid information
//    If the indicated form element is not correct, the color of the label
//    accompanying that form element is set to an error color and an error
//    message is returned to the caller. Otherwise, the form element is set
//    to a "normal" color and an empty string is returned to the caller
//
// Pre    :
//  "validateStruct" - a structure that looks like this:
//
//     var validationStruct = {
//       normalColor : "black",
//       errorColor : "red",
//       fields : {
//         surname        : ["lettersOnly",    "surnameTxt",     "required"],
//         firstName      : ["lettersOnly",    "firstNameTxt",   "required"],
//         affiliation    : ["lettersNumbers", "affiliationTxt"            ],
//         address        : ["lettersNumbers", "addressTxt",     "required"],
//         city           : ["lettersOnly",    "cityTxt",        "required"]
//       }
//     };
//
//     Where:
//        normalColor - the color to use when the form element contents
//                      passes the validation
//        errorColor  - the color to use when the form element contents
//                      fails the validation
//        fields      - an array of id names of form elements to be validated. This
//                      array indicates the type of information that is acceptable
//                      for the form element, the id of the label that accompanies
//                      the form element and an indication as to whether the form
//                      element is required (may not be left blank)
//
//  "id" - the id of the form element to be validated (also an index into the fields
//         array described above
// Post   :
//  the color of the label associated with the form element has either been
//  set to "normalColor" color or "errorColor"
//
// Returns :
//  error string if an error occurred, an empty string otherwise
//*************************************************************************/
function FORMVAL_validateOne(validateStruct,id) {
    var value = new String();
    var error = "";

    var idToChange = validateStruct.fields[id][FORMVAL_ID_TO_CHANGE];
    var validateType = validateStruct.fields[id][FORMVAL_VALIDATE_TYPE]
    var errorColor = validateStruct.errorColor;
    var normalColor = validateStruct.normalColor;

    value = FORMVAL_getRefById(id).value;

    /* get the actual text belonging to this element */
    /* for error reporting */
    text = FORMVAL_getRefById(idToChange).innerHTML;

    /* if this is a required field */
    if (validateStruct.fields[id][FORMVAL_REQUIRED] == "required") {

      /* if this field is left blank */
      if ((value.length == 0) || (!value.match(/[^ ]/))) {
        error = FORMVAL_formatError(text,"noBlank");

        /* change the color of given text to indicate an error */
        FORMVAL_getRefById(idToChange).style.color = errorColor;
        return error;
      }; /* endif */

    /* this is not a required field */
    } else if (value.length == 0){
      /* restore the normal color incase a bad field has been erased */
      /* from a not required field */
      FORMVAL_getRefById(idToChange).style.color = normalColor;
      return "";
    }; /* endif */

    switch (validateType) {
      case "lettersOnly":
        if ( value.match(/[^a-zA-Z ]/)) {
          error = FORMVAL_formatError(text,"lettersOnly");
        }; /* endif */
        break;

      case "lettersNumbers" :
        if ( value.match(/[^a-zA-Z 0-9]/)) {
          error = FORMVAL_formatError(text,"lettersNumbers");
        }; /* endif */
        break;

      case "mostChars" :
        if ( value.match(/[^a-zA-Z 0-9\~\`\!\@\#\$\%\^\&\*\(\)\_\-\+\=\|\\\{\}\[\]\:\;\'\?\/\>\<\,\.]/) ) {
          error = FORMVAL_formatError(text,"mostChars");
        }; /* endif */
        break;

      case "numbers" :
        if ( value.match(/[^0-9\-\.]/)) {
          error = FORMVAL_formatError(text,"numbers");
        }; /* endif */
        break;

      case "postalCode" :
        if (!value.match(/[a-zA-Z]\d[a-zA-Z]\s*\d[a-zA-z]\d/)) {
          error = FORMVAL_formatError(text,"postalCode");
        }; /* endif */
        break;

      case "phone" :
        /* if the phone number contains invalid characters */
        if ( !value.match(/^\+?[0-9 ()-]+[0-9]$/) ) {
          error = FORMVAL_formatError(text,"phone");
        } else {

          /* if there are too few or too many digits */
          numDigits = FORMVAL_countDigits(value);
          if ( numDigits < 10 || numDigits > 11) {
            error = FORMVAL_formatError(text,"phoneDigits");
          }; /* endif */
        }; /* endif */
        break;

      case "phoneStrictAreaCode" :
        /* if the phone number is not in this format */
        /* 1-111-222-3333 */
        /* or 1 111 222 3333 */
        /* or 111-222-3333 */
        /* or 111 222 3333 */
        if ( !value.match(/^(1[- ])?\d{3}[- ]\d{3}[- ]\d{4}$/) ) {
          error = FORMVAL_formatError(text,"phoneStrictAreaCode");
        }; /* endif */
        break;

      case "email":
        /* if basic email validation fails */
        if ( !value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/)) {
          error = FORMVAL_formatError(text,"email");
        }; /* endif */
        break;

      case "currency":
        if ( !value.match(/\$?[0-9-\.]/)) {
          error = FORMVAL_formatError(text,"currency");
        }; /* endif */
        break;

      case "currencyGt0":
        if ( !value.match(/\$?[0-9-\.]/)) {
          error = FORMVAL_formatError(text,"currency");

        } else if (value <= 0){
          error = FORMVAL_formatError(text,"currencyGt0");
        }; /* endif */
        break;

      case "mastercard":
        /* if the mastercard is the wrong length */
        if (FORMVAL_countDigits(value) != 16 ) {
          error = FORMVAL_formatError(text,"numDigits");

        /* if the number does not start with the correct digits */
        } else if (!value.match(/^(51|52|53|54|55)/)) {
            error = FORMVAL_formatError(text,"mastercardNot");

        } else {
          /* if the number fails the credit card math */
          if (!FORMVAL_validateLuhnFormula(value)) {
            error = FORMVAL_formatError(text,"mastercard");
          }; /* endif */
        }; /* endif */
        break;

      case "visacard":
        /* if the visacard is the wrong length */
        var numDigits = FORMVAL_countDigits(value);

        /* if the card does not have the correct number of digits */
        if (numDigits != 16 && numDigits != 13) {
          error = FORMVAL_formatError(text,"numDigits");

        /* number does  not start with the correct starting digit */
        } else if (!value.match(/^4/)) {
          error = FORMVAL_formatError(text,"visacardNot");

        } else {
          /* if the number fails the credit card math */
          if (!FORMVAL_validateLuhnFormula(value)) {
            error = FORMVAL_formatError(text,"visacard");
          }; /* endif */
        }; /* endif */
        break;

      case "dateyyyymmdd":
        /* allowable formats yyyymmdd yyyy.mm.dd yyyy-mm-dd yyyy/mm/dd yyyy\mm\dd */
        if ( !value.match(/^[0-9]{4}[.-\/\\]?[0-9]{2}[.-\/\\]?[0-9]{2}/)) {
          error = FORMVAL_formatError(text,"dateyyyymmdd");
        }; /* endif */
        break;

      case "dateText":
        /* allowable formats "Jan 1 2004" "Jan 01 2004" "Jan 1, 2004" "Jan 01, 2004" */
        if ( !value.match(/^[a-zA-Z]{3}[, ]?[0-9]{1,2}[ ,]?[0-9]{4}/)) {
          error = FORMVAL_formatError(text,"dateText");
        }; /* endif */
        break;

      default :
        error = "FORMVAL_validateOne: " + validateType + " is an unknown validation type";

    }; /* end switch */

    /* if there was no error */
    if ( error == "" ) {
      /* make sure the color of this is normal to indicate no error */
      FORMVAL_getRefById(idToChange).style.color = normalColor;

    /* there was an error */
    } else {
        /* change the color of given text to indicate an error */
        FORMVAL_getRefById(idToChange).style.color = errorColor;
    }; /* endif */

    return error;

}  /* end of FORMVAL_validateOne */

//************************************************************************
// Name   : FORMVAL_validate
// Author : Paul Battersby
// Description :
//  Using the same structure described in FORMVAL_validateOne, this
//  calls FORMVAL_validateOne() for every field in the "fields" array
//  and builds a list of errors which is then placed in an alert()
//  message
//
// Pre    :
//  "validateStruct" - see FORMVAL_validateOne()
//
// Post   :
//  if FORMVAL_validateOne returned any error messages, they have been formatted
//  and placed in an alert() box
//
// Returns :
//  false - validation failed (errors were reported)
//  true  - validation succeeded (no errors reported)
//*************************************************************************/
function FORMVAL_validate(validateStruct) {
  var i;
  var errors = new String();

  /* loop through the list of fields to be validated */
  for ( id in validateStruct.fields ) {

    /* if the id doesn't exist, skip it */
    /* this allows the validateStruct to contain entries for which */
    /* some JavaScript has not yet created an HTML element         */
    if ( FORMVAL_getRefById(id) == null ) {
      continue;
    }; /* endif */

    errors += FORMVAL_validateOne(validateStruct,id);
  }; /* end for */

  /* if at least one error was detected */
  if (errors.length > 0) {
    alert(FORMVAL_formatError(text,"errorList") + errors);
  }; /* endif */

  if (errors.length > 0) {
    return false;
  } else {
    return true;
  };
}  /* end of FORMVAL_validate */

//************************************************************************
// Name   : FORMVAL_validateLuhnFormula
// Author : copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd
//          (reformatted/repackaged by Paul Battersby)
//
// Description :
//  This performs the Luhn Formula on a credit card number to determine
//  it if is a valid Visa or Mastercard number
//
//  Based on ANSI X4.13, the LUHN formula (also known as the modulus 10 -- or
//  mod 10 -- algorithm ) is used to generate and/or validate and verify the
//  accuracy of credit-card numbers.
//
//  Most credit cards contain a check digit, which is the digit at the end of the
//  credit card number. The first part of the credit-card number identifies the
//  type of credit card (Visa, MasterCard, American Express, etc.), and the middle
//  digits identify the bank and customer.
//
//  To generate the check digit, the LUHN formula is applied to the number. To
//  validate the credit-card number, the check digit is figured into the formula.
//
//  Here's how the algorithm works for verifying credit cards; the math is quite
//  simple:
//
//  1) Starting with the second to last digit and moving left, double the value of
//  all the alternating digits.
//
//  2) Starting from the left, take all the unaffected digits and add them to the
//  results of all the individual digits from step 1. If the results from any of
//  the numbers from step 1 are double digits, make sure to add the two numbers
//  first (i.e. 18 would yield 1+8). Basically, your equation will look like a
//  regular addition problem that adds every single digit.
//
//  3) The total from step 2 must end in zero for the credit-card number to be
//  valid.
//
//  The LUHN formula was created in the late 1960s by a group of mathematicians.
//  Shortly thereafter, credit card companies adopted it. Because the algorithm is
//  in the public domain, it can be used by anyone.
//
//  The LUHN formula is also used to check Canadian Social Insurance Number (SIN)
//  validity. In fact, the LUHN formula is widely used to generate the check
//  digits of many different primary account numbers. Almost all institutions that
//  create and require unique account or identification numbers use the Mod 10
//  algorithm.
//
// Pre    :
//  "s" - the credit card number that is to be checked
//
// Post   :
//  (nothing)
//
// Returns :
//  true  - credit card number passes the mathematical check
//  false - credit card fails the mathematical check
//*************************************************************************/
function FORMVAL_validateLuhnFormula(s) {

  var v = "0123456789";
  var w = "";
  for (var i=0; i < s.length; i++) {
    x = s.charAt(i);
    if (v.indexOf(x,0) != -1)
    w += x;
  }
  var j = w.length / 2;
  if (j < 6.5 || j > 8 || j == 7) return false;
  var k = Math.floor(j);
  var m = Math.ceil(j) - k;
  var c = 0;
  for (var i=0; i<k; i++) {
    a = w.charAt(i*2+m) * 2;
    c += a > 9 ? Math.floor(a/10 + a%10) : a;
  }
  for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
  return (c%10 == 0);
} /* end of FORMVAL_validateLuhnFormula */

//************************************************************************
// Name   : FORMVAL_handleMutEx
// Author : Paul Battersby
// Description :
//  If the given form element contains data, this sets all other form elements
//  in the list to disabled
//
//  If the given form element is empty, it sets all other form elements in
//  the list to enabled
//
// Pre    :
//  "muExList" - the list of form elements that are to be considered mutually
//               exclusive.
//  "id" - the id of the form element that was just changed either by text being
//         added or completely deleted
//
// Post   :
//  if the given form element is now empty, all the form elements in the
//  list have been enabled.
//
//  if the given form element is not empty, all the form elements in the list
//  (except the given form element) are now disabled
//
// Returns:
//  (nothing)
//*************************************************************************/
function FORMVAL_handleMutEx(mutExList,id)
{
  /* if this form element became blank */
  if (FORMVAL_getRefById(id).value == "") {
    /* enable the other forms from the list */
    for ( i in mutExList ) {
      FORMVAL_getRefById(mutExList[i]).disabled = false;
    }; /* end for */

  /* text was entered */
  } else {
    /* disable the other forms elements from the list */
    for ( i in mutExList ) {

      /* disable only if not the selected element */
      if ( mutExList[i] != id ) {
        FORMVAL_getRefById(mutExList[i]).disabled = true;
      }
    }; /* end for */

  }; /* endif */
} /* end of FORMVAL_handleMutEx */
