//v 0.0.1

good_img_URL = 'images/good.png';
bad_img_URL = 'images/bad.png';

function is_email_valid(email){
  if(email == null){
    return false;
  }

  var atPos = email.indexOf("@");

  if(
    atPos < 1 ||
    email.indexOf(".", atPos) == -1
  ){
    return false
  }

  var login = email.substring(0, atPos);
  var domain = email.substring(atPos + 1, email.length);

  // Regexp declarations
    var atom = "\[^\\s\\(\\)><@,;:\\\\\\\"\\.\\[\\]\]+";
    var word = "(" + atom + "|(\"[^\"]*\"))";
    var loginRE = new RegExp("^" + word + "(\\." + word + ")*$");

    for (i = 0; i < login.length; i++){
        if (login.charCodeAt(i) > 127){
            return false;
        }
    }

    if (!login.match(loginRE)){
        return false;
    }

    return is_domain_valid(domain);
}


function is_domain_valid(domain){
  if(typeof(domain) != 'string'){
    return false;
  }

    for (i = 0; i < domain.length; i++){
        if (domain.charCodeAt(i) > 127){
            return false;
        }
    }

  var ipDigit = "(0?0?\\d|[01]?\\d\\d|2[0-4]\\d|25[0-6])";
  var ipRE = new RegExp("^" + ipDigit + "\\." + ipDigit + "\\." + ipDigit + "\\." + ipDigit + "$");

    if (ipRE.test(domain)) {
        return true;
    }

    var domains = domain.split(".");
    
    if (domains.length < 2) {
        return false;
    }

    for (i = 0; i < domains.length - 1; i++) {
        if (!(/^[a-zA-Z0-9\-]+$/).test(domains[i])) {
            return false;
        }
    }

    if(domains[domains.length-2].length < 2){
      return false;
    }

    if (!(/^[a-zA-Z]{2,}$/).test(domains[domains.length-1])){
        return false;
    }

    return true;
}

function s_up_f(Email_is_set, single_err_field)
{
  single_err_field = typeof(single_err_field) != 'undefined' ? single_err_field : 0;
  Check_Login(document.getElementById("sf_login").value, true, Email_is_set, single_err_field);
  return;
}

function val_s_up_f(Email_is_set, single_err_field)
{
    single_err_field = typeof(single_err_field) != 'undefined' ? single_err_field : 0;
    Check_Login(document.getElementById("sf_login").value, false, Email_is_set, single_err_field);
    return;
}

function Submit_Sign_up(Page_ID)
{
    var http_request2 = new Subsys_JsHttpRequest_Js(); 
    http_request2.onreadystatechange = function() { 
    if (http_request2.readyState == 4)
    {
    if (http_request2.status == 200) 
     {
      if  (http_request2.responseText == "ok")
      {       
        window.location="/welcome.html?aid=3";
      } else
      {
          document.getElementById("singup_errors").innerHTML=http_request2.responseText;
         }
      } else
      {
      document.getElementById("singup_errors").innerHTML="Ошибка обновления данных: "+http_request2.status;
      }
    }
    };
    request_string="/signup_check.asp"
    http_request2.justGiveMeHTML= true; 
    http_request2.open("GET",request_string , true);
    var email = '' + document.getElementById("email").value;
    var sf_login = '' + document.getElementById("sf_login").value;
    var password = '' + document.getElementById("password").value;
    var captcha = '';
    if (document.getElementById("captcha"))
    {
      captcha = '' + document.getElementById("captcha").value;
    }
    http_request2.send( { action:1, email: email , sf_login: sf_login, password: password, captchacode: captcha } );
}


function complete_Anonymous_registration()
{
    var http_request2 = new Subsys_JsHttpRequest_Js(); 
    http_request2.onreadystatechange = function() { 
    if (http_request2.readyState == 4)
    {
    if (http_request2.status == 200) 
     {
        if  (http_request2.responseText == "ok")
        {       
          window.location="/welcome.html?aid=2";
        } else
        {
            document.getElementById("singup_errors").innerHTML=http_request2.responseText;
        }
      } else
      {
      document.getElementById("singup_errors").innerHTML="Ошибка обновления данных: "+http_request2.status;
      }
    }
    };
    request_string="/signup_check_an.asp"
    http_request2.justGiveMeHTML= true; 
    http_request2.open("GET",request_string , true);
    var email = '' + document.getElementById("email").value;
    var sf_login = '' + document.getElementById("sf_login").value;
    var password = '' + document.getElementById("password").value;
    var captcha = '';
    if (document.getElementById("captcha"))
    {
      captcha = '' + document.getElementById("captcha").value;
    }
    http_request2.send( { action:1, email: email , sf_login: sf_login, password: password, captchacode: captcha } );
}


function Submit_Anonymous_Sign_up(Email_is_set)
{//TODO Email_is_set reaction
    //Disable play button
    document.getElementById("buttons").innerHTML='<a href="#" class="button1 iePNG"></a><a class="button2 iePNG"></a><a href="#" class="button3 iePNG"></a>';    
    var http_request2 = new Subsys_JsHttpRequest_Js(); 
    http_request2.onreadystatechange = function() { 
    if (http_request2.readyState == 4)
    {
    if (http_request2.status == 200) 
     {
      if  (http_request2.responseText == "ok")
      {       
       window.location="/welcome.html?aid=1";
        } else
      {
          //window.location="/signup.html?"+http_request2.responseText;
          window.location = "/signup.html";
         }
      } else
      {
        //window.location="/signup.html?"+http_request2.status;
        window.location = "/signup.html";
      }
    }
    };
    request_string="/signup_check.asp"
    http_request2.justGiveMeHTML= true; 
    http_request2.open("GET",request_string , true);
    var email = '';
    var sf_login = '';
    var password = '';
    var captcha = '';
    http_request2.send( { action:2, email: email , sf_login: sf_login, password: password, captchacode: captcha } );
}

function show_error(std, custom, msg, single_err_field)
{
  if (single_err_field == 1)
  {
    document.getElementById(std).innerHTML = msg;
  } else
  {
    document.getElementById(custom).innerHTML = msg;
  }

}


function Check_Login(sf_login, is_do_signup, Email_is_set, single_err_field)
{    
    if(sf_login=="")
    {
     show_error("singup_errors", "login_err", "Введите свой логин (никнейм)", single_err_field);
     document.getElementById("sf_login_c").innerHTML='<img alt="" src="'+bad_img_URL+'" />'
     return false;
    }
     else
     {
        if(sf_login.length<=5)
        {
         show_error("singup_errors", "login_err", "Этот логин слишком короткий!", single_err_field);
         document.getElementById("sf_login_c").innerHTML='<img alt="" src="'+bad_img_URL+'" />'
         return false;
        }
         else
         {
            var http_request2 = new Subsys_JsHttpRequest_Js(); 
            http_request2.onreadystatechange = function() { 
            if (http_request2.readyState == 4)
            {
            if (http_request2.status == 200) 
             {
                  if  (http_request2.responseText == "ok")
                  {   
                      show_error("singup_errors", "login_err", "", single_err_field);    
                      document.getElementById("sf_login_c").innerHTML='<img alt="" src="'+good_img_URL+'" />'
                      return Check_other_fields(is_do_signup, Email_is_set, single_err_field);                  
                  } else
                  {
                      show_error("singup_errors", "login_err", http_request2.responseText, single_err_field);                      
                      document.getElementById("sf_login_c").innerHTML='<img alt="" src="'+bad_img_URL+'" />'
                  }
             } else
                  {
                      show_error("singup_errors", "login_err", "Ошибка обновления данных: "+http_request2.status, single_err_field);
                      document.getElementById("sf_login_c").innerHTML='<img alt="" src="'+bad_img_URL+'" />'
                  }
            }
            };
            request_string="/login_check.asp"
            http_request2.justGiveMeHTML= true; 
            http_request2.open("GET",request_string , true);
            var sf_login = '' + document.getElementById("sf_login").value;
            http_request2.send( { AID:1, sf_login: sf_login} );
         }
     }
}


function Check_other_fields(is_do_signup, Email_is_set, single_err_field)
{


if (Email_is_set==0) {
  if(document.getElementById("email").value=="")
  {
    show_error("singup_errors", "email_err", "Введите адрес электронной почты", single_err_field);
    document.getElementById("email_c").innerHTML='<img alt="" src="'+bad_img_URL+'" />'
    return false;
  }
   else
   {
    !is_email_valid(document.getElementById("email").value)
    show_error("singup_errors", "email_err", "", single_err_field);
    document.getElementById("email_c").innerHTML='<img alt="" src="'+good_img_URL+'" />'
   } 
   
   if(!is_email_valid(document.getElementById("email").value))
  {
    show_error("singup_errors", "email_err", "Такого адреса просто не может быть!", single_err_field);
    document.getElementById("email_c").innerHTML='<img alt="" src="'+bad_img_URL+'" />'
    return false;
  }
   else
   {
    show_error("singup_errors", "email_err", "", single_err_field);
    document.getElementById("email_c").innerHTML='<img alt="" src="'+good_img_URL+'" />'
   }
}
 
 
if(document.getElementById("password").value=="")
{
 document.getElementById("singup_errors").innerHTML='Без пароля украдут все ваши деньги';
 //document.getElementById("password").focus();
 document.getElementById("password_c").innerHTML='<img alt="" src="'+bad_img_URL+'" />'
 return false;
}
 else
 {
  document.getElementById("password_c").innerHTML='<img alt="" src="'+good_img_URL+'" />'
 }
 
 if(document.getElementById("password").value.length<=3)
{
 document.getElementById("singup_errors").innerHTML='Этот пароль слишком короткий!';
 document.getElementById("password_c").innerHTML='<img alt="" src="'+bad_img_URL+'" />'
 return false;
}
 else
 {
  document.getElementById("password_c").innerHTML='<img alt="" src="'+good_img_URL+'" />'
 }

    if (document.getElementById("captcha"))
    {
        if (document.getElementById("captcha").value.length<5)
        {
            document.getElementById("singup_errors").innerHTML='Осталось только ввести текст на картинке =)';       
            return false;
        } 
    }
 
 if (is_do_signup)
 {
     document.getElementById("singup_errors").innerHTML='Производим регистрацию...';
    Submit_Sign_up(66);
 }

 return true;
}


function RefreshImage(valImageId) {
  var objImage = document.images[valImageId];
  if (objImage == undefined) {
    return;
  }
  var now = new Date();
  objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}
