$(document).ready(function(){
    $("#contact-div").load("data/contact.php", function(response, status, xhr) {
        if (status == "error") {
            var msg = "Przepraszamy, wystąpił błąd: ";
            alert(msg + xhr.status + " " + xhr.statusText);
        }else{
            $("#contact-form").validationEngine({
                promptPosition: "centerRight",
                success: function(){
                    wyslij();
                    return false;
                },
                unbindEngine: false
            });
        }
    });


})

function clearForm(){
    $(':input','#contact-form')
         .not(':button, :submit, :reset, :hidden')
         .val('')
         .removeAttr('checked')
         .removeAttr('selected');
         $.validationEngine.closePrompt('.formError',true);
}

function wyslij(){
    if(true){
        var action = "send";
        var email = $("#contact-email").val();
        var message = $("#contact-message").val();
        var name = $("#contact-name").val();
        var telefon = $("#contact-telefon").val();
        var temat = $("#temat option:selected").val();
        var cc = 0;
        if ($("#contact-cc").is(':checked')){
            cc = 1;
        }

        $.ajax({
            type: "POST",
            url: 'data/contact.php',
            data: "action=" + action + "&email=" + email + "&message=" + message + "&name=" + name + "&telefon=" + telefon + "&temat=" + temat + "&cc=" + cc,
            error: function(xhr, status, text){
                alert(xhr.status + " - " + xhr.statusText);
                return null;
            },
            success: function(text, status, xhr){
                alert(text);
                clearForm();
                return null;
            }

        })
    }

    return null;
}
