/**
 *  @class  The Username object contains properities and methods to validate
 *          usernames and to display messages on error.
 *
 *  @constructor
 *  @requires String
 *  @param  {String}  id      id of the username form element
 *  @param  {String}  value   username as input by the user
 *  @param  {String}  errId   id of error-display element
 *  @type void
 */
function Username(id, value, errId)
{
  this.id = id;
  this.value = value.stripSpaces();
  this.errId = errId;
}

/**
 *  ID of element containing the username.
 *  @type String
 */
Username.prototype.id = null;

/**
 *  Value of form element containing the username.
 *  @type String
 */
Username.prototype.value = null;

/**
 *  Is the field in this instance required?
 *  @type boolean
 */
Username.prototype.required = false;

/**
 *  Id of element in which to display error messages.
 *  @type String
 */
Username.prototype.errId = null;

/**
 *  Error messages; set by validate() method.
 *  @type String
 */
Username.prototype.errMsg = null;

/**
 *  Marks the field as required by setting this.required to true.
 *
 *  @type void
 */
Username.prototype.setRequired = function()
{
  this.required = true;
}

/**
 *  The format() method does nothing.
 *
 *  @type String
 */
Username.prototype.format = function()
{
  return this.value;
};

/**
 *  Validates a username. Input must not be blank and must match regx format
 *  <code>/[^a-z0-9_]/i</code>.
 *
 *  @type boolean
 */
Username.prototype.validate = function()
{
  if (this.required && this.value == "") {
    this.errMsg = "Valid username is required.";
    return false;
  }
  if (this.value.length > 0 && this.value.length < 7) {
    this.errMsg = "Your username must be at least seven characters long";
    return false;
  }
  if (/[^a-z0-9_]/i.test(this.value)) {
    this.errMsg = "Your username has characters which are not allowed. " +
      "Please try again using only letters, numbers, or the underscore.";
    return false;
  }
  return true;
};

/**
 *  Verifies username and reports invalid input. Calls validate(), format(),
 *  and, conditionally, showError().
 *
 *  @requires ValidateInput validateInput
 *  @return if username ok then return it else false.
 *  @type mixed
 */
Username.prototype.verify = function()
{
  if (this.validate()) {
    return this.format();
  }
  validateInput.showError(this.errId, this.errMsg);
  validateInput.newFocus = this.errId;
  return false;
};

