Form validation jQuery
<p>I have this piece of code I use to validate a form:</p>

<pre><code>$("form[name='aar_form']").submit(function()
        {
            var values = new Array();
            $(".tipsy").remove();
            show_loading();
            for(var i in fields)
            {
                if(i &lt; 9)
                {
                    values[i] = $("input[name='" + fields[i] + "']").val();
                }
                else if(i &lt; 10)
                {
                    values[i] = $("select[name='" + fields[i] + "']").val();
                }
                else
                {
                    var $i = $("#" + fields[i] + "_div input:checked");
                    if($i.length &lt; 1)
                    {
                        values[i] = "";
                    }
                    else if($i.length == 1)
                    {
                        values[i] = $i.val();
                    }
                    else
                    {
                        values[i] = new Array();
                        $i.each(function()
                        {
                            values[i].push($(this).val());
                        });
                    }
                }
            }
            window.no_errors = false;
            ajax_request({act: "aar_validate", values: values}, function(response)
            {
                response = eval("(" + response + ")");
                window.no_errors = response.errors ===  true ? false : true;
                if(response.errors === true)
                {
                    for(var i in response.error_fields)
                    {
                        $(response.error_selector[i] + "[name='" + response.error_fields[i] + "']").attr("title", response.error_msgs[i]).tipsy({trigger: "manual", gravity: response.error_gravity[i]}).tipsy("show");
                    }
                    hide_loading();
                    $("html, body").animate({scrollTop: $(response.error_selector[0] + "[name='" + response.error_fields[0] + "']").offset().top - 80}, "slow");
                }
            });
            console.log(window.no_errors);
            setTimeout("console.log('k: ' + window.no_errors)", 100);
            return false;
        });
</code></pre>

<p>The PHP script on the back-end does all the validation. The problem that occurs is that the window.no_errors variable gets updated too late. Now, the first log that I take to console gives me false (because it is like that by default), but the second one, set with a timeout of 100 milliseconds returns true, as it should. If I write it likes this:</p>

<pre><code>setTimeout("return window.no_errors", 100);
</code></pre>

<p>the form would submit no matter of what the no_errors variable is.</p>

<p>Any tips?</p>
