$(document).ready(function(){
    // show a simple loading indicator
    var loader = jQuery('<div id="loader"><img src="images/loading.gif" alt="Отправка..." />Идет отправка</div>');
    //loader.hide();
            //.css({position: "relative", top: "1em", left: "25em"})
            //.appendTo("body")
            
    jQuery().ajaxStart(function() {
            $("#submittd").html(loader);
    }).ajaxStop(function() {
            $("#submittd").html('<input class="submit" type="submit" value="Отправить"/>');
    }).ajaxError(function(a, b, e) {
            throw e;
    });
    
    var v = jQuery("#sendform").validate({
            errorElement: "div",
            errorClass: "errordiv",
            submitHandler: function(form) {
                    jQuery(form).ajaxSubmit({
                            //target: "#result",
                            success:showResponse
                    });
            },
            errorPlacement: function(error, element) {
                error.insertAfter( element.next("span"));
            },
            messages: {
                name: {
                    required:"Пожалуйста, ведите имя.",
                    minlength: "Ваше имя слишком короткое."
                },
                email: {
                    required:"Пожалуйста, ведите e-mail.",
                    email: "Вы ввели неправильный e-mail."
                },
				phone: {
                    required:"Пожалуйста, введите контактный телефон.",
                    minlength: "Телефон слишком короткий."
                },
                captcha: "Код введен не верно! Попробуйте ввести снова, либо нажмите на изображение, чтобы получить другой код."
            },
            rules: {
                captcha: {
                    required: true,
                    remote: "process.php"
                },
                name: {
                    required: true
                },
                email: {
                    required: true,
                    email: true
                }

            }
        });
});

// post-submit callback 
function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
    $("#result").html($(responseText).find("#sendresult").html());
    /*alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.');*/
} 
