// 20031001 - Thomas Yandell
// Copyright Highbury House Communications PLC 2003

// subscriptions validation library

// the validate is just an example of how to use the other functions (based on what was sent through)
// it is always a good idea to pass the form in (onsubmit="validate(this);" or similar)
// as the forms are generated, it would be a good idea to remove validate from this file and generate it for each form
// the rest of this file can then be called in via a <script type="text/javascript" src="subs.js"></script>

/**
 * validate - example function to validate a form
 *  takes:
 *   form - the HTMLFormElement object to perform the validation
 *  returns true if the form is valid
 */
function validate (form) {
  if (is_empty(form.elements.posttown))
    return form_error('Please enter your Post Town / City *', form.elements.posttown);

  if (is_empty(form.elements.demo1))
    return form_error('Please enter your Which of the following best describes your job function?*', form.elements.demo1);

  if (! validate_boxes(form, 'demoCN_5'))
    return form_error('Please select which types of products and service and/or purchasing are you involved with', form.elements.demoCN_5A);

  // i couldn't find an example of radio buttons on the site, but i include one here for completeness
  if (! validate_radios(form.elements.aRadioButton))
    return form_error('Please select a radio button', form.elements.aRadioButton);
}

// sepatating this is a good idea cos it allows you to do extra checks without modifying lots of code

/**
 * is_empty - check a form element does not contain just whitespace or is empty (this should probably be implemented with a regex)
 *  takes:
 *   form_element - the form element to check
 *  return true if the form element returns a valid value
 */
function is_empty (form_element) {
  return form_element.value == '' || form_element.value == ' ';
}

/**
 * form_error - util function to show an error message and highlight a form field when validation fails
 *  takes:
 *   message - the error message to alert the user with
 *   form_element - the form element to highlight
 *  returns false as a convenience to the calling validation function
 */
function form_error (message, form_element) {
  alert(message);
  if (form_element.focus)
    form_element.focus();
  if (form_element.select)
    form_element.select();
  return false;
}

/**
 * validate_boxes - makes sure at least on checkbox with the specified prefix is checked
 *  takes:
 *   form - the HTMLFormElement object which contains the checkboxes
 *   prefix - the first few chars of the checkboxes in the naming convention
 *  returns a true if they have checked at least one of the checkboxes
 */
function validate_boxes (form, prefix) {
  for (var i = 0; i < form.elements.length; i++) {
    if (form.elements[i].name.indexOf(prefix) == 0 && form.elements[i].checked) {
      return true;
    }
  }
  return false;
}

/**
 * validate_radios - makes sure that the user has selected a radio button
 *  takes:
 *   radio - the radio button object
 *  returns true if they have checked one of the radio buttons
 */
function validate_radios (radio) {
  for (var i = 0; i < radio.length; i++) {
    if (radio[i].checked) {
      return true;
    }
  }
  return false;
}

