// JavaScript Document

// this function is called when the user hits the submit button
// for a MailMan subcription form
// and it returns true to finish the submit and false to stop

function
ValidateMailManSubscriptionForm( form )
{
  // First validate the email address the user supplied
  if ( ! ValidateEmailAddress( form, "Subscribe" ) ) {
    return false;
  }

  // Now create and fill in the password fields
  if ( ! FillMailManPassword( form ) ) {
    return false;
  }

  return true;
}


// This function validates the email address before shipping it
// off to the subscription form in the mailman pages
// All it really does is make sure that there is at least one
// '@' and one '.' and that they appear in the correct order

function
ValidateEmailAddress( form, buttonName ) {

  var atLocation = form.email.value.indexOf("@");
  var periodLocation = form.email.value.lastIndexOf(".");

  if ( form.email.value == "" ) {

  alert("Please supply an email address before hitting the '" + buttonName + "' button.");
  return false;

  } else if ( form.email.value == "yourname@host.com" ) {

  alert("Please enter your email address before hitting the '" + buttonName + "' button.");
  return false;

  } else if ( ( atLocation == -1 )
    || ( periodLocation == -1 )
    || ( atLocation > periodLocation ) ) {

    alert("Your email address does not appear to be in proper email address format."
        + " All email addresses have an '@' character and"
        + " at least one '.' after the '@'." );
    return false;

  } else {
    return true;
  }
}


// The function FillMailManPassword() fills the form password fields with values

function
FillMailManPassword( form )
{
  form.pw.value = MakePassword();
  form["pw-conf"].value = form.pw.value;
  // alert("form.pw.value = " + form.pw.value);
  // return false;
  return true;
}


// This function MakePassword() returns a randomly created password each time it
// is called. You can set the password length and the which of two
// types of passwords

function
MakePassword()
{
  // define simple to true or false to select which password generator you
  // want to use
  var simple = true;
  // define var below to set the password length
  var passwordLength = 8;
  var charInt = 0;
  var newPassword = "";
  var ascii = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  for ( var i = 0; i < passwordLength; i++ ) {

    if ( simple ) {
      // This code selects a character from the string above
      charInt = ( ( Math.random() * 10000 ) % 62 );
      newPassword += ascii.charAt( charInt );
    } else {
      // this code selects a character that starts at ascii 48 (40, '0')
      // and ends at ascii 126 (7e, '~') so has punctuation
      charInt = ( ( Math.random() * 10000 ) % 79 ) + 48;
      newPassword += String.fromCharCode( charInt );
    }
  }
  return newPassword;
}
