﻿var registeruser = {

    accountname:"",
    password:"",
    email:"",
    
    checkedName:false,
    validName:false,
    checkedEmail:false,
    validEmail:false,

    working: false,

    byear: null,
    bmonth: null,
    bday:null,
    
    submit: function()
    {
        
        
        registeruser.checkName();
        registeruser.checkEmail();
        
        registeruser.trySubmit();

        
    },
    
    trySubmit: function() {
    
        if(registeruser.checkedName && registeruser.checkedEmail)
            registeruser.executeSubmit();
        else
            setTimeout(registeruser.trySubmit,100);
    
    },
        
    executeSubmit: function() {
    
    
        var name = document.getElementById("registeruser_name").value;
        var password  = document.getElementById("registeruser_password").value;
        var email  = document.getElementById("registeruser_email").value;
        
        var gender = document.getElementById("registeruser_male").checked ? "male" : "female";

        var accept = document.getElementById("registeruser_accept").checked;
        
        var error = false;

        $("#registeruser_error_accept").hide();
        $("#registeruser_error_name").hide();
        $("#registeruser_error_password").hide();
        $("#registeruser_error_email").hide();
        $("#registeruser_error_year").hide();
        
        //alert($("#registeruser_name").css("background-color"));
       
        if(!registeruser.validName)
            error=true;
            
        if(!registeruser.validEmail)
            error=true;
        
        if(!password.length>2 || password==registeruser.password)
        {
            $("#registeruser_password").css("background-color","red");
            error = true;
        }
        else
            $("#registeruser_password").css("background-color","");
        
        
        if(this.byear==null)
        {
            $("#registeruser_birthyear").css("background-color","red");
            error = true;
        }
        else
            $("#registeruser_birthyear").css("background-color","white");
            
        if(!accept)
        {
            $("#registeruser_tos").css("color","red");
            error = true;
        }
        else
            $("#registeruser_tos").css("color","");    
        
        if(!error)
        {
            popup.show("captcha",true);
            $("#registeruser_error_captcha").hide();
        }
    },
    
    setBday: function(bday,button)
    {
        $("#register_bdays").hide();
        $("#register_bday").html($(button).html());
 		this.bday=bday;
    },
    
     setBmonth: function(bmonth,button)
    {
        $("#register_bmonths").hide();
        $("#register_bmonth").html($(button).html());
 		this.bmonth=bmonth;
    },
    
     setByear: function(byear,button)
    {
        $("#register_byears").hide();
        $("#register_byear").html($(button).html());
 		this.byear=byear;
    },
    
    createUser: function() {
    
        if(!registeruser.working)
        {
    
            var postdata = "action=createaccount";
            
            postdata += "&name=" + main.fixString(document.getElementById("registeruser_name").value);
            postdata += "&password=" + main.fixString(document.getElementById("registeruser_password").value);
            
            
            if(document.getElementById("registeruser_male").checked)
                postdata += "&sex=male";
            else
                postdata += "&sex=female";
         
            postdata += "&email=" + main.fixString(document.getElementById("registeruser_email").value);
            
            postdata += "&birthday=" + this.bday;
            postdata += "&birthmonth=" + this.bmonth;
            postdata += "&birthyear=" + this.byear;
            
            postdata += "&captcha=" + document.getElementById("captcha_input").value;
            
            registeruser.working=true;
            
            jQuery.post('Ajax/Accounts.aspx',postdata,function(data) {
            
                if (data.success)
                {
                    location.href = "Welcome.aspx";
                }
                else
                {
                    popup.hide("captcha");
                    
                    if(data.reason=="captcha")
                        $("#registeruser_error_captcha").show();

                }
                
                registeruser.working=false;
                
            },"json");
        }
    },
    
    checkName: function() {
    
        registeruser.checkedName=false;
    
        var name = document.getElementById("registeruser_name").value;
    
        var postdata = "action=getbyname&name=" + main.fixString(name);
        var nameRegEx = /^\w{3,40}$/i;
        
        jQuery.post('Ajax/Accounts.aspx',postdata,function(data) {
            if (!data.success && nameRegEx.test(name) && name!= registeruser.accountname)
            {
                $("#registeruser_name").css("background-color","");
                registeruser.validName=true;
            }
            else
            {
                $("#registeruser_name").css("background-color","red");
                registeruser.validName=false;
            }
            
            registeruser.checkedName=true;
	    },"json");
    },
    
    checkEmail: function() {
    
        registeruser.checkedEmail=false;
    
        var email = document.getElementById("registeruser_email").value;
    
        var postdata = "action=getbyemail&email=" + main.fixString(email);
        
        var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
        
        jQuery.post('Ajax/Accounts.aspx',postdata,function(data) {
            if (!data.success && email.search(emailRegEx)!=-1 && email!= registeruser.email)
            {
                $("#registeruser_email").css("background-color","");
                registeruser.validEmail=true;
            }
            else
            {
                $("#registeruser_email").css("background-color","red");
                registeruser.validEmail=false;
            }
            
            registeruser.checkedEmail=true;
	    },"json");
    }
    
};