$(document).ready(function(){

    $(":input").attr({
        autocomplete: "off"
    });

    $("#imieNazwisko").focus(function() {
        if ($(this).val()=="Imię i Nazwisko")
            $(this).val('');
    }).blur(function() {
        if ($(this).val()=='')
            $(this).val("Imię i Nazwisko");
    });

    $("#telefon").focus(function() {
        if ($(this).val()=="Nr telefonu")
            $(this).val('');
    }).blur(function() {
        if ($(this).val()=='')
            $(this).val("Nr telefonu");
    });

    $("#mail").focus(function() {
        if ($(this).val()=="Adres email")
            $(this).val('');
    }).blur(function() {
        if ($(this).val()=='')
            $(this).val("Adres email");
    });



    $(":submit").click(function(){
        var isInvalid   = false;
        var isMail      = false;
        var isTelefon   = false;
        var isAccepted  = false;

        if (!$('#regulaminCheck').is(':checked')) {
            $("li.zgoda").addClass('error');
            $("li.zgoda p").fadeIn(400);
            isInvalid = true;

        } else {
            $("li.zgoda p").fadeOut();
            $("li.zgoda").removeClass('error');
            isAccepted = true;
        }

        // email
        var mail = $("#mail").val();
        if (mail !=="Adres email"){
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail)) {
                $("#mail").next("p").fadeOut();
                isMail = true;
                $("#mail").parent().removeClass("error");
            }
            else {
                $("#mail").next("p").fadeIn(400);
                $("#mail").parent().addClass("error");
                isInvalid = true;
            }
        } else {
            $("#mail").parent().removeClass("error");
            $("#mail").next("p").hide();
        }

        //telefon
        var telefon = $("#telefon").val();
        if (telefon !=="Nr telefonu"){
            var match = telefon.replace(/[a-z]+/gi);
            if (match != telefon)
            {
                isInvalid = true;
                $("#telefon").next("p").fadeIn(400);
                $("#telefon").parent().addClass("error");
            }
            else
            {
                var matchlenght = match.match(/[0-9]/g);
                if (matchlenght.length > 8)
                {
                    $("#telefon").parent().removeClass("error");
                    $("#telefon").next("p").fadeOut('slow');
                    isTelefon = true;
                }
                else
                {
                    $("#telefon").next("p").fadeIn(400);
                    $("#telefon").parent().addClass("error");
                    isInvalid = true;
                }
            }
        } else {
            $("#telefon").parent().removeClass("error");
            $("#telefon").next("p").hide();
        }

        if (isAccepted && (isInvalid == false) && (isMail == true || isTelefon == true))
        {
            $.post("form.php", {
                mail: $("#mail").val(),
                imieNazwisko: $("#imieNazwisko").val(),
                telefon: $("#telefon").val(),
		newsletterCheck: $("#newsletterCheck").attr('checked')
            },
            function(data){
                $("#formularz").fadeOut('slow', function(){
                    $("#thanx").fadeIn('slow');
                });

            });
            return false;
        } else
        {
            if (isMail == false && isTelefon == false) {
                $("#mail").parent().addClass("error");
                $("#mail").next("p").fadeIn(400);
                $("#telefon").parent().addClass("error");
                $("#telefon").next("p").fadeIn(400);
            }
        }
        return false;
    });
});
