// register box management functions

var fieldId;
var id = "";
var registerForm;
var secondPassword = '';
var registerForm = new Array();

addDependentCallback( function() {
  initRegisterBox();
}, 'ajax,forms,constants,settings');

// form submit response callback function
function registerFormResponseCallbackFunction( response )
{
  if ( response == RESPONSE_REQUEST_PROCEEDED_SUCCESSFULLY )
  {
    if ( CHECK_USER_EMAIL_ON_REGISTRATION )
    {
      if ( LANGUAGE == 'ru' )
      {
        alert( "Вам на почту отправлено письмо с подтверждением регистрации, пожалуйста перейдите по ссылке, подтверждающей Вашу регистрацию." );
      }
      else
      {
        alert( "A registration confirmation e-mail have been sent to your address, please follow the link in it to confirm your registration." );
      }
    }
    else
    {
      if ( LANGUAGE == 'ru' )
      {
        alert( "Операция выполнена успешно. Теперь введите те же имя пользователя и пароль в поля чуть выше (чтобы не забыть)." );
      }
      else
      {
        alert( "The operation is performed successfully. Now enter the same user name and the password in fields above (not to forget)." );
      }
    }
  }
  else  if ( response == RESPONSE_CAPTCHA_INVALID_CAPTCHA )
  {
    if ( LANGUAGE == 'ru' )
    {
      alert( "Неверно введён код защиты от автоматических регистраций (captcha), пожалуйста введите верный код." );
    }
    else
    {
      alert( "Incorrect auto-registration protection code (captcha), please enter the correct code." );
    }
  }
  else
  {
    alert( response );
  }
}

// performs an initialization
function initRegisterBox()
{
  fieldId;
  id = "registerForm";
  registerForm;
  secondPassword = PARAM_PASSWORD+"_";
  registerForm = new Array();


  registerForm = createFormDescription
  ( 
    id, 
    sendRegisterRequestUrl,
    PARAM_EDIT_DOCUMENT_TITLE, 
    (LANGUAGE=='ru'?"Введите информацию о себе":"Please provide some information about you"), 
    function() 
    { 
      // checking the passwords to match
      if ( forms[registerForm['id']][PARAM_PASSWORD] != 
           forms[registerForm['id']][secondPassword] )
      {
        if ( LANGUAGE == 'ru' )
        {
          alert( 'Пароли не совпадают' );
        }
        else
        {
          alert( 'Passwords do not match' );
        }
        return false;
      };
      return confirm( LANGUAGE=='ru' ? 'Выполнить регистрацию?' : 'Perform a registration?' ) 
    }, 
    registerFormResponseCallbackFunction,
    (LANGUAGE=='ru'?"Зарегистрироваться":"Register")
  );
  registerForm['displayIfTrue'] = "ALLOW_AUTOMATIC_USERS_REGISTRATIONS && USER_ID == DEFAULT_USER_ID";

  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, 
    '',
    (LANGUAGE=='ru'?'Имя пользователя (login, лучше выбрать короткое&nbsp;имя на&nbsp;английском языке, чтобы проще было потом вводить,&nbsp;&mdash; отображаемое&nbsp;имя можно будет позже настроить):':'The user name (login, it&nbsp;is recommended to&nbsp;choose a&nbsp;short name in&nbsp;English to&nbsp;input it&nbsp;easier later, the display name might be&nbsp;set&nbsp;up later):'),
    MAX_LOGIN_LENGTH
  );
  
  fieldId = PARAM_PASSWORD;
  registerForm.passwordControlAdd
  ( 
    fieldId, 
    '',
    (LANGUAGE=='ru'?'Пароль:':'Password:'),
    MAX_PASSWORD_LENGTH
  );
  
  fieldId = secondPassword;
  registerForm.passwordControlAdd
  ( 
    fieldId, 
    '',
    (LANGUAGE=='ru'?'Повторите пароль:':'Confirm Password:'),
    MAX_PASSWORD_LENGTH
  );
  
  fieldId = PARAM_EMAIL;
  registerForm.textControlAdd
  ( 
    fieldId, 
    '',
    (LANGUAGE=='ru'?'E-mail:':'E-Mail'),
    MAX_EMAIL_LENGTH
  );
  
  fieldId = PARAM_CAPTCHA;
  registerForm.captchaControlAdd
  ( 
    fieldId, 
    '',
    (LANGUAGE=='ru'?'Код защиты (щёлкните на нём, если плохо видно):':'The protection code (click on it if it is hard to be read)'),
    MAX_CAPTCHA_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 += "<"+(registerBoxDisplayType=='inline'?'span':'div')+" id='"+registerForm['id']+"'>";
    output += (registerBoxDisplayType=='inline'?"| (":"")+"<a\n";
    output += "  onmouseover='javascript: if ( siteHelperPanelOpened || registerBoxDisplayType==\"inline\" ) placeElementWithContentOn"+registerBoxHintsPosition+"FromMe( 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 += " >"+(LANGUAGE=='ru'?'Регистрация':'Registration')+"</a>"+(registerBoxDisplayType=='inline'?")":"");
    output += "</"+(registerBoxDisplayType=='inline'?'span':'div')+">";
  };
  
  // updating the management box content
  if ( output != "" )
    registerBox.innerHTML = output;
  else
    registerBox.innerHTML = "";
}

// preinitialization is performed in login box
//updateRegisterBox( "registerbox" );

