var asAjax = false;

jQuery().ready(function() {

    // validate register form on keyup and submit
    jQuery("#registerForm").validate({
        rules: {
            nickname: {
                required: true,
                minlength: 2
            },
            firstName: {
                required: true
            },
            lastName: {
                required: true
            },
            password: {
                required: true,
                rangelength: [4,10]
            },
            confirmPassword: {
                required: true,
                equalTo: "#password"
            },
            email: {
                required: true,
                email: true
            },
            gender :{
                required: true
            },
            birthDay: {
                digits: true
            },
            birthMonth: {
                digits: true
            },
            birthYear: {
                digits: true
            },
            agree: "required",
            mobileNumber: {
                digits: true,
                minlength: 10,
                maxlength: 10
            }

        },
        messages: {
            nickname: {
                required: "יש למלא כינוי",
                minlength: "יש למלא כינוי, מינימום 2 תוים"
            },
            firstName: {
                required: "יש למלא שם פרטי"
            },
            lastName: {
                required: "יש למלא שם משפחה"
            },
            password: {
                required: "יש למלא סיסמה",
                rangelength: "יש למלא סיסמה בין 4 ל-10 תווים"
            },
            confirmPassword: {
                required: "ש למלא אימות סיסמה",
                equalTo: "יש לוודא שהסיסמה תואמת"
            },
            email: {
                required: "יש למלא שם משתמש",
                email: "יש למלא כתובת אימייל חוקית"
            },
            /*see the error msg in the jsp file
             gender :{
             required: "���� ����� ���"
             },*/
            agree: "יש לסמן Vבתיבה הימנית",
            mobileNumber:{
                digits : "יש להזין רק ספרות",
                minlength : "יש להזין 10 ספרות בדיוק",
                maxlength : "יש להזין 10 ספרות בדיוק"
            }

        }
    });//end form validation

    jQuery("#registerForm").submit(function() {
        jQuery('input[type=submit]', this).attr("disabled", 'disabled');
    });

    jQuery("#create").click(function() {
        jQuery("#serverErrorMsg").html("");
        var isValid = jQuery("#registerForm").valid();
        var doSubmit = !asAjax && isValid;
        if (isValid) {
            myBlockUI("create");
            if (asAjax) {
                //post form data by ajax action
                createUser();
            }
        }
        return doSubmit;
    });

});

function createUser() {
    var inputs = collectFormData();
    var url = jQuery('#registerForm').attr('action') + "&ajax=true";
    jQuery.ajax({
        type: "POST",
        url: url,
        data: inputs,
        complete: function(data, status) {
            createUserCallback(data, status);
        }
    });
}


function createUserCallback(data, status) {
    var SC_ACCEPTED = 202;
    var SC_OK = 200;
    if (data.status == SC_ACCEPTED) {
        //get the content of the serverErrorMsg div
        var error = parseErrorMsg(data.responseText);
        jQuery("#serverErrorMsg").html(error[1]);
        myUnBlockUI("create");
    }
    if (data.status == SC_OK) {
        //set new src to the cookie iframe
        setSrc(data.getResponseHeader("Location"), "progressScreen");
        //myUnBlockUI("create"); done in progressScreen.jsp page at the end of image loading
    }
}
