var $a_error = [];
var $status = "true";
/*
$a_error['nickname'] = 1;
$a_error['email'] = 1;
$a_error['password'] = 1;
$a_error['terms'] = 0;
*/


function check_nickname($act) {
    //$('#nickname').focus();
    $('#nickname').blur(function() {
        do_it();
    });
    
    if ($act == "1") {
        do_it();
    }
    
    
    function do_it() {
         if ($("#nickname").val().length > 0) {
           
            $.getJSON('/user/registracija/default.html?ajax=1&t=n&val=' + $("#nickname").val(), function (data) {
                if (data['class'] == 'taken') {
                    $('#nickname_error').attr('innerHTML', $("#text1").attr('innerHTML'));
                    $('#nickname_error').show();
                    error_holder($a_error, "nickname", 1);
                    
                } else if (data['class'] == 'error') {
                    $('#nickname_error').attr('innerHTML', $("#text2").attr('innerHTML'));
                    $('#nickname_error').show();
                    error_holder($a_error, "nickname", 1);
                } else {
                    $('#nickname_error').attr('innerHTML', '&nbsp;');
                    error_holder($a_error, "nickname", 0);
                }
                error_show();        
            });
            
        } else {
            $('#nickname_error').attr('innerHTML', $("#text3").attr('innerHTML'));
            $('#nickname_error').show();            
            error_holder($a_error, "nickname", 1);
        }        
        
        error_show();        
    }
    
}

function check_email($act) {
    $('#emailI').blur(function() {
        do_it();
    });
    
    if ($act == "1") {
        do_it();
    }
    
    function do_it() {
        if ($("#emailI").val().length > 0) {
            $.getJSON('/user/registracija/default.html?ajax=1&t=m&val=' + $("#emailI").val(), function (data) {
                if (data['class'] == 'false') {
                    $('#email_error').attr('innerHTML', $("#email1").attr('innerHTML'));
                    error_holder($a_error, "email", 1);
                } else if (data['class'] == 'invalid') {
                    $('#email_error').attr('innerHTML', $("#email1").attr('innerHTML'));
                    error_holder($a_error, "email", 1);
                } else {
                    error_holder($a_error, "email", 0);
                    $('#email_error').attr('innerHTML', '&nbsp;');
                }
                error_show();        
            });
        } else {
            $('#email_error').attr('innerHTML', $("#email3").attr('innerHTML'));
            error_holder($a_error, "email", 1);
        }
        error_show();
        
    }
}

function check_password($act) {
    $('#retype_password').blur(function() {
        do_it();
    });
    
    if ($act == "1") {
        do_it();
    }

    function do_it() {
        $pass = $('#password1').val();
        $rpass = $('#retype_password').val();
        if ($rpass != $pass ) {
            error_holder($a_error, "password", 1);
            $('#password_error').attr('innerHTML', $("#pass1").attr('innerHTML'));
        } else if ($pass == "") {
            error_holder($a_error, "password", 1);
            $('#password_error').attr('innerHTML', $("#pass2").attr('innerHTML'));            
        } else if ($pass.length < 5) {
            error_holder($a_error, "password", 1);
            $('#password_error').attr('innerHTML', $("#pass3").attr('innerHTML'));
        } else {
            error_holder($a_error, "password", 0);
            $('#password_error').attr('innerHTML', '&nbsp;');
        }
        error_show();
    }
    
}

function check_captcha($act) {
    $('#code').blur(function() {
        do_it();
    });
    
    if ($act == "1") {
        do_it();
    }

    function do_it() {
        if ($("#code").val() == "") {
            error_holder($a_error, "captcha", 1);
            $('#captcha_error').attr('innerHTML', $("#captcha1").attr('innerHTML'));
        } else if ($("#code").val().length != 5) {
            error_holder($a_error, "captcha", 1);
            $('#captcha_error').attr('innerHTML', $("#captcha1").attr('innerHTML'));
        } else {
            $.getJSON('/user/registracija/default.html?ajax=1&t=c&val=' + $("#code").val(), function (data) {
                if (data['class'] != 'valid') {
                    error_holder($a_error, "captcha", 1);
                    $('#captcha_error').attr('innerHTML', $("#captcha1").attr('innerHTML'));
                } else {
                    error_holder($a_error, "captcha", 0);
                    $('#captcha_error').attr('innerHTML', '&nbsp;');
                }
            });
        }   
    }
}

function check_terms($act) {
    if ($('#terms').attr('checked')) {
        error_holder($a_error, "terms", 0);
        $('#terms_error').attr('innerHTML', '&nbsp;');
    } else {
        error_holder($a_error, "terms", 1);
        $('#terms_error').attr('innerHTML', $("#terms1").attr('innerHTML'));
    }
}

function show_help() {
    $('#show_help').click(function() {
        $(".registration_info").toggle();
        $("#show_help").toggle();
        $("#show_help2").toggle();
    });
    $('#show_help2').click(function() {
        $(".registration_info").toggle();
        $("#show_help").toggle();
        $("#show_help2").toggle();
    });
}

function error_holder($a_error, $type, $value) {
    if ($type == 'nickname') {
        $a_error['nickname'] = $value;
    } else if ($type == 'email') {
        $a_error['email'] = $value;
    } else if ($type == 'password') {
        $a_error['password'] = $value;
    } else if ($type == 'terms') {
        $a_error['terms'] = $value;
    } else if ($type == 'captcha') {
        $a_error['captcha'] = $value;        
    }
}

function error_check($a_error) {
    $n_error = 0;
    if (!isNaN($a_error['nickname'])) {
        $n_error = $n_error + $a_error['nickname'];
    }
    
    if (!isNaN($a_error['password'])) {    
        $n_error = $n_error + $a_error['password'];    
    }        
    
    if (!isNaN($a_error['email'])) {    
        $n_error = $n_error + $a_error['email'];    
    }    
    
    if (!isNaN($a_error['terms'])) {    
        $n_error = $n_error + $a_error['terms'];    
    }    

    if (!isNaN($a_error['captcha'])) {    
        $n_error = $n_error + $a_error['captcha'];    
    }    
    
    
    return $n_error;
}

function error_show() {
    if (error_check($a_error) >= 1) {
        submit_error("true");
    } else {
        submit_error("false");
    }
}

function submit_error(act) {
    if ($status != "false") {
        if (act == "true") {
            //$("#submitRegistration").hide();
            $("#submit_error").show();
        } else {
            //$("#submitRegistration").show();
            $("#submit_error").hide();        
        }
    }
}


function check_all() {
    $('#submitRegistration').click(function() {
        $status = "false";
        check_nickname("1");
        check_password("1");
        check_email("1");
        check_captcha("1");
        check_terms("1");
        $status = "true";
        error_show();
        if (error_check($a_error) >= 1) {
            return false; 
        } else {
            return true;
        }
        
    });
    
}

$(document).ready(function(){
    check_all();
    check_nickname();
    check_email();
    check_password();
    check_captcha();
    show_help();
    
});
