$(document).ready(function(){
    $("#likbezForm").validate(
    {
        rules: {},
        submitHandler: function(form) 
        {
            $(form).ajaxSubmit(
            {
               success: AddQuestionSuccess, 
               beforeSubmit: AddQuestionPrepareRequest, 
               dataType: "json",
               type: "POST"
            });
            return false;
        }
    });
});

function likbezAJAXResponseHandler(data, status)
{
    if (status == 'success')
    {
        if (data.error)
        {
            $('#likbezForm').children('.block').children('.error').text(data.error);
            $('#likbezForm').children('.block').children('.error').show();
            return false;
        }
    }
    else
    {
        $.jGrowl("Ошибка запроса");
        return false;
    }
    return true;
}

function AddQuestionSuccess(data, status)
{
    $("#ajaxLoading").remove();
    if (!likbezAJAXResponseHandler(data, status)) return;
    if (data.msg == "ok") 
    {
        $('#likbezForm textarea').val('');
        $('#orderSuccess').fadeIn('slow');
    };
}

function AddQuestionPrepareRequest(form)
{
    //$("input:submit").attr("disabled", true);
    form.push({name:"async",value:"1"});
    form.push({name:$("#likbezForm input:submit").attr("name"),value:"1"});
    
    $('#orderSuccess').hide();
    $('#likbezForm .error').text('').hide();
    $("#likbezForm").children('div.block').children('div.buttons').append('<span id="ajaxLoading"><img src="/images/icons/ajax-loader.gif" /></span>');
}

