var UPDATE_ERROR_1     = "Fehler: &Auml;nderung konnte nicht durchgef&uuml;hrt werden!";
var UPDATE_ERROR_2     = "Fehler: AJAX Fehler!";
var CHANGE_PWD_ERROR_1 = "Fehler: neue Passw&ouml;rter m&uuml;ssen mindestens 5 Stellen haben!";
var CHANGE_PWD_ERROR_2 = "Fehler: neue Passw&ouml;rter stimmen nicht &uuml;berein!";
var CHANGE_PWD_ERROR_3 = "Fehler: Sie haben nicht das richtige Passwort eingegeben!";
var CHANGE_PWD_ERROR_4 = "Fehler: Altes Passwort und neues stimmen &uuml;berein!";
var CHANGE_PWD_INFO_1  = "Passwort erfolgreich ge&auml;ndert.";
var CHECK_USER_ERROR_1 = "Fehler: Anfrage Fehlerhaft!";
var CHECK_USER_ERROR_2 = "Fehler: Daten&uuml;berpr&uuml;fung ergab, dass der Benutzer mit den aktuellen Daten nicht existiert. Dies deutet auf ein Fehler beim Abspeichern.";
var CHECK_USER_ERROR_3 = "Fehler: Benutzer kommt in der Datenbank &ouml;fters vor, damit ist ein Einloggen nicht m&ouml;glich. Bitte &auml;ndern Sie entweder Benutzernamen oder Passwort";
var CHECK_USER_ERROR_4 = "Fehler: AJAX Fehler!";
var CHECK_USER_INFO_1  = "&Uuml;berpr&uuml;fung der Benutzerdaten ergab keine Fehler!"
var CHECK_USER_SUCCESS = false;
var KEEP_PWWIN_CLOSE   = false;

// initialize inplace Editor
function extendIPE() {
  Ajax.InPlaceEditor.prototype.__initialize = Ajax.InPlaceEditor.prototype.initialize;
  Ajax.InPlaceEditor.prototype.__getText = Ajax.InPlaceEditor.prototype.getText;
  Ajax.InPlaceEditor.prototype.__onComplete = Ajax.InPlaceEditor.prototype.onComplete;
  Ajax.InPlaceEditor.prototype = Object.extend(Ajax.InPlaceEditor.prototype, {

    initialize: function(element, url, options){
        this.__initialize(element,url,options)
        this.setOptions(options);
        this._checkEmpty();
    },

    setOptions: function(options){
        this.options = Object.extend(Object.extend(this.options,{
            emptyText: emptyTextSetting,
            emptyClassName: 'inplaceeditor-empty'
        }),options||{});
    },

    _checkEmpty: function(){
        if( this.element.innerHTML.strip().length == 0 ){
            this.element.appendChild(
                Builder.node('span',{className:this.options.emptyClassName},this.options.emptyText));
        }
    },

    getText: function(){
        document.getElementsByClassName(this.options.emptyClassName,this.element).each(function(child){
            this.element.removeChild(child);
        }.bind(this));
        return this.__getText();
    },

    onComplete: function(transport){
        this._checkEmpty();
        this.__onComplete(transport);
    }
  });
}

/**
 * update single attribute of an user
 */
function update(field, value, title) {
  new Ajax.Request('editUser.php', {method: 'post',
    parameters: 'req=ajax&uid='+uidSetting+'&part=main&parameter=' + field + '&newValue=' + escape(value),
    onSuccess: function(result) { 
      if (result.responseText == value) {
        showMessageBox(title + ' ge&auml;ndert','message',1500);
      } else {
        showMessageBox(UPDATE_ERROR_1,'error',3500, true);
      }},
    onFailure: function() {
      showMessageBox(UPDATE_ERROR_2,'error',3500, true);
    }
    });
   
}

function updateSalutation() {
  update('user_sex',$F('salutation'), 'Anrede');
}


/**
 * test for password change for the user with the uid uid
 * @param int userid
 * @param string md5 encoded old password
 * @param string md5 encoded new password
 * @param string md5 encoded new password retype
 * @return boolean
 */
function changePassword(uid, old, new1, new2) {
  if (new1.length < 5) {
    showMessageBox(CHANGE_PWD_ERROR_1,'error',3500, true);
    return false;
  }
  if (new1 != new2) {
    showMessageBox(CHANGE_PWD_ERROR_2,'error',3500, true);
    return false;
  }
  if(old != $F('old_enc_password')) {
    showMessageBox(CHANGE_PWD_ERROR_3,'error',3500, true);
    return false;
  }
  if(old == new1) {
    showMessageBox(CHANGE_PWD_ERROR_4,'error',3500, true);
    return false;
  }
  update('user_password', new1, 'Passwort');
  if (checkUser(uid, false) === true) {
    hidePasswordInput();
    $('old_enc_password').value = new1;
  }
  
}

/**
 *  initialize in place editor fields for the main user fields
 *  part are main or profile to indicate which part of the user should be saved
 *  @param string attribte name that should be edited
 *  @param int userid
 */
function mkEditableMain(name, uid, title) {
  if (name == 'user_title') {
    extendIPE();
  }
  new Ajax.InPlaceEditor('user_admin_'+ name, 'editUser.php', {
    emptyText: 'Bitte hier klicken, um Wert zu editieren',
    clickToEditText: 'Bitte hier klicken, um Wert zu &amul;ndern',
    cancelText: 'abbrechen',
    formClassName: 'inplaceeditor_form',
    okText: 'ok',
    cancelButton : true,
    cancelLink : false,  
    callback: function(form, value) {
      showMessageBox(title + ' ge&auml;ndert','message', 1500); 
      return "req=ajax&uid=" + uid + "&part=main&parameter=" + name + "&newValue=" + value;
    }, 
    savingText: 'Speichere ...'});
  if (name == 'user_name') {
    checkUser(uid);
  }
}

/**
 * initialize in place editor fields for the profile user fields
 * similar to mkEditableMain
 */
function mkEditableProfile(name, uid, title) {
  new Ajax.InPlaceEditor('user_admin_'+ name, 'editUser.php', {
    emptyText: 'Bitte hier klicken, um Wert zu editieren',
    clickToEditText: 'Bitte hier klicken, um Wert zu editieren',
    cancelText: 'abbrechen',
    formClassName: 'inplaceeditor_form',
    okText: 'ok',
    cancelButton : true,
    cancelLink : false,  
    callback: function(form, value) { 
      showMessageBox(title + ' ge&auml;ndert','message',1500);
      return "req=ajax&uid=" + uid + "&part=profile&entry=0&parameter=" + name + "&newValue=" + value
    }, 
    savingText: 'Speichere ...'});  
}

/**
 * check user for integrity
 */
function checkUser(uid, showInfo) {
  if (showInfo !== false) {
    showInfo = true;
  }
  new Ajax.Request('editUser.php', {method: 'post',
    parameters: 'req=ajax&uid='+uid+'&part=usercheck',
    onSuccess: function(result) { 
      users = Number(result.responseText)
      if (isNaN(users)) {
        showMessageBox(CHECK_USER_ERROR_1,'error',3000, true); 
        CHECK_USER_SUCCESS = false;
      } else if(users == 0) {
        showMessageBox(CHECK_USER_ERROR_2,'error',3000, true); 
        CHECK_USER_SUCCESS = false;
      } else if(users > 1) {
        showMessageBox(CHECK_USER_ERROR_3,'error',3000, true);
        CHECK_USER_SUCCESS = false;
      } else {
        if (showInfo) {
          showMessageBox(CHECK_USER_INFO_1,'message',3500);
        }
        CHECK_USER_SUCCESS = true;
      }
      return;
    },
    onFailure: function() {
      showMessageBox(CHECK_USER_ERROR_4,'error',3000, true);
      CHECK_USER_SUCCESS = false;}
    });
  return CHECK_USER_SUCCESS;
}

/**
 * show password input area
 */
function passwordDisplay() {
  if ($('passwordDisplay').innerHTML != '***************' || KEEP_PWWIN_CLOSE) return;
  $('passwordDisplay').update($('passwordChange').innerHTML);
  $('old_password').focus();
}


/**
 * hide password input area
 */
function hidePasswordInput() {
  KEEP_PWWIN_CLOSE = true;
  $('passwordDisplay').innerHTML = '***************'
  setTimeout("KEEP_PWWIN_CLOSE = false;", 250);
}

function wait() {
}