window.password =
  {
    //ERRORS: BEGIN
    errors : [],
    //ERRORS: END

    flag : function(item, flag, title)
      {
        if (title.length > 0)
          {
            document.getElementById(item).style.cursor = 'help';
            document.getElementById(item).title = title;
          }
        else
          {
            document.getElementById(item).style.cursor = '';
            document.getElementById(item).title = '';
          }

             if (flag == 'ok')            document.getElementById(item).src = '/templates/images/password/ok.gif';
        else if (flag == 'error')         document.getElementById(item).src = '/templates/images/password/error.gif';
        else if (flag == 'not_available') document.getElementById(item).src = '/templates/images/password/not_available.gif';
        else if (flag == 'unknown')       document.getElementById(item).src = '/templates/images/password/unknown.gif';
        else                              document.getElementById(item).src = '/templates/images/password/null.gif';
      },

    all : function()
      {
        this.errors = [];

        this.password_old();
        this.password_new();
        this.password_confirm();

        if (this.errors.length > 0)
          {
            var message = ((this.errors.length == 1)?'Error:\r\n':'Errors:\r\n');
            for (var i = 0; i < this.errors.length; ++i)
              {
                message += '\r\n * ' + this.errors[i];
              }
            alert(message);

            return false;
          }
        else
          {
            return true;
          }
      },

    password_old : function()
      {
        if (document.getElementById('password_password_old').value.length == 0)
          {
            this.errors.push('Old password is required');
            this.flag('password_password_old_flag', 'error', 'Old password is required');
            return false;
          }
        else if (document.getElementById('password_password_old').value.length < 6)
          {
            this.errors.push('Your old password must be at least 6 characters');
            this.flag('password_password_old_flag', 'error', 'Your old password must be at least 6 characters');
            return false;
          }
        else if (document.getElementById('password_password_old').value.length > 32)
          {
            this.errors.push('Your old password is too long; 32 characters max');
            this.flag('password_password_old_flag', 'error', 'Your old password is too long; 32 characters max');
            return false;
          }
        else
          {
            this.flag('password_password_old_flag', 'null', '');
            return true;
          }
      },

    password_new : function()
      {
        var regexp_password_new = /^[A-Za-z0-9]+$/;

        if (document.getElementById('password_password_new').value.length == 0)
          {
            this.errors.push('New password is required');
            this.flag('password_password_new_flag', 'error', 'New password is required');
            return false;
          }
        else if (document.getElementById('password_password_new').value == document.getElementById('password_password_old').value)
          {
            this.errors.push('Your new password and old password cannot be the same');
            this.flag('password_password_new_flag', 'error', 'Your new password and old password cannot be the same');
            return false;
          }
        else if (!regexp_password_new.test(document.getElementById('password_password_new').value))
          {
            this.errors.push('Only Latin letters and numbers can be used for your new password');
            this.flag('password_password_new_flag', 'error', 'Only Latin letters and numbers can be used for your new password');
            return false;
          }
        else if (document.getElementById('password_password_new').value.length < 6)
          {
            this.errors.push('Your new password must be at least 6 characters');
            this.flag('password_password_new_flag', 'error', 'Your new password must be at least 6 characters');
            return false;
          }
        else if (document.getElementById('password_password_new').value.length > 32)
          {
            this.errors.push('Your new password is too long; 32 characters max');
            this.flag('password_password_new_flag', 'error', 'Your new password is too long; 32 characters max');
            return false;
          }
        else
          {
            this.flag('password_password_new_flag', 'ok', 'OK');
            return true;
          }
      },

    password_confirm : function()
      {
        if (document.getElementById('password_password_confirm').value.length == 0)
          {
            this.errors.push('New password confirmation is required');
            this.flag('password_password_confirm_flag', 'error', 'New password confirmation is required');
            return false;
          }
        else if (document.getElementById('password_password_new').value != document.getElementById('password_password_confirm').value)
          {
            this.errors.push('Your new password and confirmation are not the same');
            this.flag('password_password_confirm_flag', 'error', 'Your password and confirmation are not the same');
            return false;
          }
        else
          {
            this.flag('password_password_confirm_flag', 'ok', 'OK');
            return true;
          }
      }
  }