$(document).ready(function() {
    function clearErrors(formIdent) {
        $(formID + " .formErrors").empty();
    }

    function setError(field, error) {
        $(formID + " .formErrors_" + field).text(error);
    }


    var formID = '.contact-form form';
    $(formID).bind('submit', function(e) {
        $(this).ajaxSubmit({
            success:   showResponse,
            dataType:  'json'
            //url:       'default/index/contacts',
            //clearForm: true        // clear all form fields after successful submit
            //resetForm: true        // reset the form after successful submit
        });
        e.preventDefault();
        return false; // <-- important!
    });

    function showResponse(responseText, statusText)  {
		var res = responseText;
        clearErrors();
        if (res.success) {
            alert('Сообщение отправлено');
            window.location.reload();
        } else {
                var fields = res.errors;
                for (var field in fields) {
                    var errors = fields[field];
                    for (var errorId in errors) {
                        var errorMsg = errors[errorId];
                        setError(field, errorMsg);
                    }
                }
        }

	}

});