
/*
 * Example:
 *********************************************************************************************
 *
 * <script type="text/javascript" src="validate.js"></script>
 * <body>
 *   <form onSubmit="return fieldCheck()">
 *     <input type="text" name="name">
 *     <input type="text" name="age">
 *     <input type="text" name="email">
 *     <input type="submit">
 *   </form>
 * </body>
 *
 *********************************************************************************************
 *
 * Notable (external) functions:
 *   fieldCheck()
 *   checkMail()
 *
 * Internal functions:
 */

/*
 * fieldCheck()
 *
 *  Input:
 *
 *  Output:
 *    true or false
 *
 *  Description: Checks fields for emptyness and also check whether email
 *               is correct.
 */
function fieldCheck(email_check)
{
  /*
   * Name of all fields, which are checked against emptyness ;)
   * Format: new Array('name1', 'name2', name3');
   */
  var emptyFields = new Array('name', 'email', 'comment'); /* 'name', 'age' */
  /*
   *  This is related to emptyFields... Readable name of field, used by
   *  alert() (language compatibility)...
   */
  var emptyFieldsNames = new Array('Name', 'E-mail address', 'Comment'); /* 'Your Name', 'Your Age' - in the same order as emptyFields! */
  /*
   * Name of email field.
   */
  var emailField = 'email';
  /*
   * Consecutive number of our form (usually 0).
   */
  var formID = 0;

  /*
   * Internal vars (noerros at the beginning; no mistaken fields).
   */
  var noerrors = true;
  var fields = '';

  /*
   * Loop all fields and check if they are empty.
   */
  check_email_further = true;
  for (i = 0; i < emptyFields.length; i++) {
    if (document.forms[formID].elements[emptyFields[i]].value.length <= 0) {
      noerrors = false;

      fields = fields + emptyFieldsNames[i];
      fields = fields + ', ';

      if (emptyFields[i] == emailField) {
        check_email_further = false;
      }
    }
  }

  /*
   * Tell the client the empty fields.
   */
  if (noerrors == false) {
    alert('The following fields are required but are still empty: ' + fields.substring(0, fields.length-2)); /* The following required fields are empty: */
  }


  /*
   * Email check.
   */
  if (check_email_further == true &&
      checkMail(document.forms[formID].elements[emailField].value) == false) {
    alert('You entered an invalid e-mail address!'); /* Wrong email address entered. */
    noerrors = false;
  }


  /*
   * Is Comment too long?
   */
  maxlength = 512;
  if(document.forms[formID].comment.value.length >= maxlength) {
    alert('Your comments must be 512 characters or less.');
    noerrors = false;
  }

  document.forms[formID].elements['form_submit'].value = 'y'+'E'+"s"+'p'+"l"+'s' + document.forms[formID].elements['subject'].value;

  /*
   * This will halt our submit if it returns false.
   */
  return noerrors;
}

/*
 * checkMail()
 *
 *  Input:
 *    email = email address (string)
 *
 *  Output:
 *    true or false
 *
 *  Description: Validates the given email address.
 */
function checkMail(email)
{
  /*
   * Regex check ;)
   */
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;

  if (filter.test(email))
    return true;

  return false;
}

