<!--

function validateLoginName(fld)
{
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'Desired login name' is empty.\n";
    } 
    else if ( fld.value.length < 4)
    {
        error = "The login name is too short.\n";
    }
    else if (fld.value.length > 14)
    {
        error = "The login name is too long.\n";
    }
    else if (illegalChars.test(fld.value)) 
    {
        error = "The 'Desired login name' field  contains illegal characters.\n";
    } 
    else 
    {
        fld.style.background = 'White';
    } 
    return error;
}

function validateFirstName(fld)
{
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'First name' is empty.\n";
    } 
    else if (illegalChars.test(fld.value)) 
    {
        error = "The 'First name' field contains illegal characters.\n";
    } 
    else 
    {
        fld.style.background = 'White';
    } 
    return error;
}

function validateLastName(fld)
{
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'Last name' is empty.\n";
    } 
    else if (illegalChars.test(fld.value)) 
    {
        error = "The 'Last name' field contains illegal characters.\n";
    } 
    else 
    {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePasswordExistence(fld)
{
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'Password' is empty.\n";
    } 
    else if (illegalChars.test(fld.value)) 
    {
        error = "The 'Password' field contains illegal characters.\n";
    } 
    else 
    {
        fld.style.background = 'White';
    } 
    return error;
}

function validateRetypePasswordExistence(fld)
{
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'Retype Password' is empty.\n";
    } 
    else if (illegalChars.test(fld.value)) 
    {
        error = "The 'Retype Password' field contains illegal characters.\n";
    } 
    else 
    {
        fld.style.background = 'White';
    } 
    return error;
}

function validateOldPasswordExistence(fld)
{
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
    var tfld = trim(fld.value); // value of field with whitespace trimmed off

    if (tfld == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'Old Password' is empty.\n";
    } 
    else if (illegalChars.test(tfld)) 
    {
        error = "The 'Old Password' field contains illegal characters.\n";
    } 
    else 
    {
        fld.style.background = 'White';
    } 
    return error;
}

function validateNewPasswordExistence(fld)
{
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'New Password' is empty.\n";
    } 
    else if (illegalChars.test(fld.value)) 
    {
        error = "The 'New Password' field contains illegal characters.\n";
    } 
    else 
    {
        fld.style.background = 'White';
    } 
    return error;
}

function validateRetypeNewPasswordExistence(fld)
{
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'Retype New Password' is empty.\n";
    } 
    else if (illegalChars.test(fld.value)) 
    {
        error = "The 'Retype New Password' field contains illegal characters.\n";
    } 
    else 
    {
        fld.style.background = 'White';
    } 
    return error;
}

function validateEmailExistence(fld)
{
    var error = "";
 
    if (fld.value == "") 
    {
        //fld.style.background = 'Yellow'; 
        error = "The field 'Email Address' is empty.\n";
    } 
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) 
{
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (!emailFilter.test(tfld)) 
    {              //test email for illegal characters
        error = "Please enter a valid email address!\n";
    } 
    else if (fld.value.match(illegalChars)) 
    {
        error = "The email address contains illegal characters!\n";
    } 
    else 
    {
        //fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld) 
{
  var error = "";
 
  if ((fld.value.length < 7) || (fld.value.length > 14)) 
  {
    error = "The password should be at least 7 characters long! \n";
    fld.style.background = 'Yellow';
  } 
     /*
  else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) 
  {
    error = "The password must contain at least one numeral.\n";
    fld.style.background = 'Yellow';
  } 
    */
  else 
  {
    //fld.style.background = 'White';
  }
  return error;
}   

function validateForm(theForm)
{
  var reason = "";

  reason += validateLoginName(theForm.login_name);
  reason += validateFirstName(theForm.first_name);
  reason += validateLastName(theForm.last_name);
  reason += validatePasswordExistence(theForm.desired_password);
  reason += validateRetypePasswordExistence(theForm.retype_password);
  reason += validateEmailExistence(theForm.email_address); 

  if (reason != "") {
    alert(reason + "\n\nPlease enter all required information!\n");
    return false;
  }

  reason += validateEmail(theForm.email_address); 

  if (reason != "") {
    alert("\n" + reason + "\n");
    return false;
  }

  reason += validatePassword(theForm.desired_password);

  if (reason != "") {
    alert("\n" + reason + "\n");
    return false;
  }

  //alert (theForm.desired_password.value)
  //alert (theForm.retype_password.value)
  if ( theForm.desired_password.value != theForm.retype_password.value)
  {
    alert("\nThe 'Password' and 'Retype password' fields are not equal!\n\nPlease retype the password!");
    return false;
  } 

  return true;
}

function validatePasswordForm(theForm)
{
  var reason = "";

  reason += validateOldPasswordExistence(theForm.old_password);
  reason += validateNewPasswordExistence(theForm.new_password);
  reason += validateRetypeNewPasswordExistence(theForm.retype_new_password);

  if (reason != "") {
    alert(reason + "\n\nPlease enter all required information!\n");
    return false;
  }

  reason += validatePassword(theForm.new_password);

  if (reason != "") {
    alert("\n" + reason + "\n");
    return false;
  }

  //alert (theForm.desired_password.value)
  //alert (theForm.retype_password.value)
  if ( theForm.new_password.value != theForm.retype_new_password.value)
  {
    alert("\nThe 'New Password' and 'Retype New password' fields are not equal!\n\nPlease retype the new password!");
    return false;
  } 

  return true;
}

//-->

