function sync_request(url)
{
  var request = false;
  if(window.XMLHttpRequest)
    request = new XMLHttpRequest();
  else if(window.ActiveXObject)
  {
    try
    {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch(e)
    {
      try
      {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e){}
    }
  }
  else
    return false;
  request.open('GET', url, false);
  request.send(null);
  if(request.status == 200)
  {
     return request.responseText;
  }
  else return false;
}
function password_remind()
{
  document.getElementById('ws').innerHTML = "<form method=\"POST\" action=\"external.html?type=email&do=remind\"><div class=\"window\" id=\"w1\"><div class=\"w_title\">Восстановить пароль</div><div class=\"w_content\">E-mail: <input class=\"textbox\" type=\"email\" name=\"email2\" maxlength=\"100\"></div><div class=\"w_buttons\"><input type=\"submit\" class=\"button\" value=\"Отправить\"> <input type=\"button\" class=\"button\" value=\"Отмена\" onClick=\"document.getElementById('ws').innerHTML = '';\"></div></div></form>";
  var thiswindow = document.getElementById("w1");
  var thiswidth = document.getElementById("w1").clientWidth / 2;
  var thisheight = document.getElementById("w1").clientHeight / 2;
  thiswindow.style.margin = "-" + thisheight.toString() + "px 0 0 -" + thiswidth.toString() + "px";
}
function forum_rules()
{
  document.getElementById('ws').innerHTML = "<div class=\"window\" id=\"w1\"><div class=\"w_title\">Правила форума</div><div class=\"w_content\" style=\"height: 480px; width: 640px; overflow: auto;\">" + sync_request('external.html?type=forum_rules') + "</div><div class=\"w_buttons\"><input type=\"submit\" class=\"button\" value=\"Подтвердить\" onClick=\"registration(0);\"> <input type=\"submit\" class=\"button\" value=\"Отмена\" onClick=\"document.getElementById('ws').innerHTML = '';\"></div></div>";
  var thiswindow = document.getElementById("w1");
  var thiswidth = document.getElementById("w1").clientWidth / 2;
  var thisheight = document.getElementById("w1").clientHeight / 2;
  thiswindow.style.margin = "-" + thisheight.toString() + "px 0 0 -" + thiswidth.toString() + "px";
}
function registration(time_of_read_rules)
{
  document.getElementById('ws').innerHTML = "<form action=\"registration.html\" method=\"POST\" onSubmit=\"return checkall();\"><div class=\"window\" id=\"w1\"><div class=\"w_title\">Регистрация нового пользователя</div><div class=\"w_content\" style=\"width: 480px\">" + sync_request('external.html?type=register_form') + "</div><div class=\"w_buttons\"><input type=\"submit\" class=\"button\" value=\"Зарегистрироваться\"> <input type=\"button\" class=\"button\" value=\"Отмена\" onClick=\"document.getElementById('ws').innerHTML = '';\"></div></div></form>";
  var thiswindow = document.getElementById("w1");
  var thiswidth = document.getElementById("w1").clientWidth / 2;
  var thisheight = document.getElementById("w1").clientHeight / 2;
  thiswindow.style.margin = "-" + thisheight.toString() + "px 0 0 -" + thiswidth.toString() + "px";
}
function checkall()
{
  var result = true;
  document.getElementById("check2").innerHTML = '';
  document.getElementById("check3").innerHTML = '';
  document.getElementById("check4").innerHTML = '';
  document.getElementById("check5").innerHTML = '';
  document.getElementById("checkp").innerHTML = '';
  if(checklogin(document.getElementById('rgl').value) == false)
  {
    result = false;
    //alert("1");
  }
  if(document.getElementById('rgp').value == "")
  {
    document.getElementById("check2").innerHTML = "<br><small style=\"color: gray;\">Введите пароль.</small>";
    result = false;
    //alert("2");
  }
  if(document.getElementById('rge').value == "")
  {
    document.getElementById("check3").innerHTML = "<br><small style=\"color: gray;\">Введите E-mail.</small>";
    result = false;
    //alert("3");
  }
  if(!document.getElementById('rgg0').checked && !document.getElementById('rgg1').checked)
  {
    document.getElementById("check4").innerHTML = "<br><small style=\"color: gray;\">Выберите пол.</small>";
    result = false;
    //alert("4");
  }
  if(!document.getElementById('rgp0').checked && !document.getElementById('rgp1').checked)
  {
    document.getElementById("checkp").innerHTML = "<br><small style=\"color: gray;\">Выберите приоритет.</small>";
    result = false;
    //alert("4");
  }
  if(document.getElementById('rgc').value == "")
  {
    document.getElementById("check5").innerHTML = "<br><small style=\"color: gray;\">Введите код проверки.</small>";
    result = false;
    //alert("5");
  }
  return result;
}
function checklogin(login)
{
  login = login.replace(" ", "+");
  if(login == "")
  {
    document.getElementById("check1").innerHTML = "<br><small style=\"color: gray;\">Введите ник.</small>";
    return false;
  }
  var result = sync_request("external.html?type=logincheck&login=" + login);
  if(result == "1")
  {
    document.getElementById("check1").innerHTML = "<br><small style=\"color: green;\">Ник свободен и вы можете использовать его для регистрации.</small>";
    return true;
  }
  else
  {
    document.getElementById("check1").innerHTML = "<br><small style=\"color: red;\">Ник занят или похож на уже существующие, придумайте другой.</small>";
    return false;
  }
}
