var reg_form;
var login;
var email;
var password;
var confirmPassword;
var captcha;


if ('undefined' == typeof String.prototype.trim) {
    String.prototype.trim = function() {
        return this.replace(/^\s+/, '').replace(/\s+$/, '');
    }
}

function ProcessForm() {
    reg_form = document.getElementById("regform");
    login = reg_form.login.value.trim();
    email = reg_form.email.value.trim();
    password = reg_form.password.value.trim();
    confirmPassword = reg_form.confirm_password.value.trim();
    captcha = reg_form.captcha.value.trim();
    checkFieldsFullness();

    return false;
}

function checkFieldsFullness() {
   if (login.length == 0 || email.length == 0 || password.length == 0 || confirmPassword.length == 0 || captcha.length == 0) {
       document.getElementById("fields_empty").style.display = "block";
       refreshCaptcha('captchaimg');
   }
   else {
      if (document.getElementById("fields_empty").style.display == "block")  document.getElementById("fields_empty").style.display = "none";
      checkByCyrillicCharacters();
   }
}

function checkByCyrillicCharacters() {
    pattern = /[а-яА-Я]/;
    if (pattern.test(login)) {
       document.getElementById("cyr_char").style.display = "block";
       refreshCaptcha('captchaimg');
    }
    else {
        if (document.getElementById("cyr_char").style.display == "block")  document.getElementById("cyr_char").style.display = "none";
        checkPassByCyrillicCharacters();     
    }
}

function checkPassByCyrillicCharacters() {
    pattern = /[а-яА-Я]/;
    if (pattern.test(password)) {
       document.getElementById("cyr_char_in_password").style.display = "block";
       refreshCaptcha('captchaimg');
    }
    else {
        if (document.getElementById("cyr_char_in_password").style.display == "block")  document.getElementById("cyr_char_in_password").style.display = "none";
        emailValidation();
    }
}

function emailValidation() {
    pattern = /^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$/;
    if (!pattern.test(email))  {
        document.getElementById("email_invalid").style.display = "block";
        refreshCaptcha('captchaimg');
    }
    else {
      if (document.getElementById("email_invalid").style.display == "block")  document.getElementById("email_invalid").style.display = "none";
        checkPasswordLength();
    }
}

function checkPasswordLength() {
    if (password.length < 5)  {
        document.getElementById("short_password").style.display = "block";
        refreshCaptcha('captchaimg');
    }
    else {
      if (document.getElementById("short_password").style.display == "block")  document.getElementById("short_password").style.display = "none";
        checkPasswordConfirm();
    }
}

function checkPasswordConfirm() {
    if (password != confirmPassword)  {
        document.getElementById("not_coincide").style.display = "block";
        refreshCaptcha('captchaimg');
    }
    else {
     if (document.getElementById("not_coincide").style.display == "block")   document.getElementById("not_coincide").style.display = "none";
        checkLogin();
    }
}

function checkLogin() {
    RegistrationForm.checkLoginExisting(login, function(exists) {
         if (exists) {
            document.getElementById("login_exist").style.display = "block";
            refreshCaptcha('captchaimg');
         }
         else {
          if (document.getElementById("login_exist").style.display == "block")   document.getElementById("login_exist").style.display = "none";
             checkEmailEx();
         }
    });
}

function checkEmailEx() {
    RegistrationForm.checkEmailExisting(email, function(exists) {
         if (exists) {
            document.getElementById("email_exist").style.display = "block";
            refreshCaptcha('captchaimg');
         }
          else {
            if (document.getElementById("email_exist").style.display == "block")  document.getElementById("email_exist").style.display = "none";
             checkCaptcha();
         }
    });
}

function checkCaptcha() {
    RegistrationForm.checkCaptcha(captcha, function(valid) {
         if (!valid) {
             document.getElementById("invalid_captcha").style.display = "block";
             refreshCaptcha('captchaimg');
         }
        else {
           if (document.getElementById("invalid_captcha").style.display == "block")  document.getElementById("invalid_captcha").style.display = "none";
             createUser();
         }
    });
}

function refreshCaptcha(captchaId) {
    var path = document.getElementById(captchaId).src;
    var newpath = path.substring(0, path.lastIndexOf("/"));
    newpath = newpath + "/Captcha.jpg?date=" + new Date();
    document.getElementById(captchaId).src = newpath;
}