// -------------------------------------------------------------------- //
function formset(field) {
  var formfld = document.getElementById(field);
  formfld.focus();
}


// -------------------------------------------------------------------- //
function validatelogon() {
  validations = new Array();
  validations[0] = ["userid", "required", "", "User id"];
  validations[1] = ["password", "required", "", "Password"];
  return validateform(validations);
}


// -------------------------------------------------------------------- //
function validatesearch() {
  validations = new Array();
  validations[0] = ["searchkey", "required", "", "Search key"];
  return validateform(validations);
}


// -------------------------------------------------------------------- //
function validatemaillist() {
  validations = new Array();
  validations[0] = ["name", "required", "", "Name"];
  validations[1] = ["email", "required", "email", "Email Address"];
  return validateform(validations);
}



// -------------------------------------------------------------------- //
function validateform(validations) {
// The validations array contains the following information about each form field to be validated:
// 0) form field name,
// 1) field Type (required or not)
// 2) field format (email, integer, number, etc)
// 3) prompt to be used in the error message

  var i;
  for (i=0; i < validations.length; i++)
  {
    if (notvalidfield(validations[i][0], validations[i][1], validations[i][2], validations[i][3])) {
     return false;
    }
  }
  return true;
}



// -------------------------------------------------------------------- //
function notvalidfield(field, field_type, field_format, field_prompt) {
  var formfld = document.getElementById(field);

  if  ((field_type == "required") && (formfld.value == "")) {
    alert(field_prompt + " is needed.");
    formfld.focus();
    return true;
  }
  
  switch (field_format) {

    case "email":
      if (formfld.value != "") {
        var email_format = /^([a-z\._-]+)@([a-z\.]+[\.][a-z]{2,3})$/;
        if (!email_format.test(formfld.value)) {
          alert(field_prompt + ' is not in a valid email address format.');
          formfld.select();
          return true;
        }
      }
      break;

    case "phone number":
      if (formfld.value != "") {
        var phone_format = /^\+?([0-9]*)?[\s-]?\(?([0-9]*)\)?[\s-]?([0-9]{3,})[\s-]?([0-9]{3,})$/;
        if (!phone_format.test(formfld.value)) {
          alert(field_prompt + ' is not in a valid phone number format.\n' + 
                'Valid formats are:\n' +
                ' +99 (99) 123 1234\n' +
                ' +99 (99) 123-1234\n' +
                ' (99) 123 1234\n'     +
                ' (99) 123-1234\n'     +
                ' 123 1234\n'          +
                ' 123-1234\n'          +
                ' 999 999 9999\n'      +
                ' 999-999-9999\n'      +
                ' 9999999999');
          formfld.select();
          return true;
        }
      }
      break;

  }

   return false;
}


