// register box management functions

var fieldId;
var id = "registerForm";
var registerForm;
var secondPassword = PARAM_PASSWORD+"_";

registerForm = createFormDescription
( 
  id, 
  sendRegisterRequestUrl,
  PARAM_EDIT_DOCUMENT_TITLE, 
  "Введите информацию о себе", 
  function() 
  { 
    // checking the passwords to match
    if ( forms[registerForm['id']][PARAM_PASSWORD] != 
         forms[registerForm['id']][secondPassword] )
    {
      alert( 'Пароли не совпадают' );
      return false;
    };
    return confirm( 'Выполнить регистрацию?' ) 
  }, 
  registerFormResponseCallbackFunction,
  "Зарегистрироваться" 
);
registerForm['displayIfTrue'] = "ALLOW_AUTOMATIC_USERS_REGISTRATIONS && USER_ID == DEFAULT_USER_ID";

// form submit response callback function
function registerFormResponseCallbackFunction( response )
{
  if ( response == RESPONSE_REQUEST_PROCEEDED_SUCCESSFULLY )
  {
    alert( "Операция выполнена успешно. Теперь введите те же имя пользователя и пароль в поля чуть выше (чтобы не забыть)." );
  }
  else
    alert( response );
}

  fieldId = PARAM_PREVIOUS_DOCUMENT_URL;
  registerForm.hiddenControlAdd
  ( 
    fieldId, 
    DOCUMENT_URL 
  );
  
  fieldId = PARAM_DOCUMENT_PATH_STRING;
  registerForm.hiddenControlAdd
  ( 
    fieldId, 
    DOCUMENT_PATH_STRING 
  );
  
  fieldId = PARAM_FUNCTION;
  registerForm.hiddenControlAdd
  ( 
    fieldId, 
    PARAM_FUNCTION_REGISTER 
  );
  
  fieldId = PARAM_USER_ID;
  registerForm.textControlAdd
  ( 
    fieldId, 
    '',
    'Имя пользователя (login, лучше выбрать короткое&nbsp;имя на&nbsp;английском языке, чтобы проще было потом вводить,&nbsp;&mdash; отображаемое&nbsp;имя можно будет позже настроить):',
    MAX_DOCUMENT_TITLE_LENGTH
  );
  
  fieldId = PARAM_PASSWORD;
  registerForm.passwordControlAdd
  ( 
    fieldId, 
    '',
    'Пароль:',
    MAX_DOCUMENT_SUMMARY_LENGTH
  );
  
  fieldId = secondPassword;
  registerForm.passwordControlAdd
  ( 
    fieldId, 
    '',
    'Повторите пароль:',
    MAX_DOCUMENT_SUMMARY_LENGTH
  );
    

  
// generates the document management buttons and fills the
// element with given id with them
function updateRegisterBox( registerBoxId )
{
  registerBox = obtainElementById( registerBoxId );
  if ( !registerBox )
    return;
  
  // generating buttons
  var output = "";
  
  // skipping if there's not enough rights for this
  var allowed = false;
  eval( "allowed = " + registerForm['displayIfTrue'] );  
  if ( allowed )
  {
    // the hint form content
    // this value is stored in management buttons
    try
    {
      forms.add( registerForm );
      registerForm['html'] = forms[registerForm['id']].generateHtml();
    }
    catch (e)
    {
      alert( e.message );
    };

    // the button itself
    output += "<div id='"+registerForm['id']+"'>\n";
    output += "<a\n";
    output += "  onmouseover='javascript: if ( siteHelperPanelOpened ) placeElementWithContentOnTopFromMe( this, \"hintconstrain\", \"hintcontent\", registerForm[\"html\"], 0, 0, hintHidingDelay ); populateFormControls(\""+registerForm['id']+"\"); focusFormPreferredControl(\""+registerForm['id']+"\");'\n";
    output += "  onmouseout='javascript: startHidingLastElement(\"hintconstrain\");'"
    output += "  onclick='javascript: submitForm(\""+registerForm['id']+"\")'"
    output += " >Регистрация</a>";
    output += "</div>";
  };
  
  // updating the management box content
  if ( output != "" )
    registerBox.innerHTML = output;
  else
    registerBox.innerHTML = "";
}

// preinitialization is performed in login box
//updateRegisterBox( "registerbox" );
