function validateEmail(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);

}


$(document).ready(function() {
    $(".sideMenu ul").find("ul").hide();
    $(".sideMenu .selected").parents("ul").show();

    $('a[href^="http://"]').each(function() {
        if ($(this).attr("href").indexOf("http://" + document.domain) != 0) {
            $(this).attr("target", "_blank");
        }
    });

    $("a[href*=.pdf]").each(function() {
        $(this).attr("target","_blank");
    });

    var a = $(".sideMenu .selected").parents("li").first();
    a.children("ul").show();

    $(".faqText").hide();
    $(".faqTitle").click(function() {
        if (!$(this).next().is(":visible")) {
            $(".faqText").slideUp();
            $(this).next().slideDown();
        }
    });

    $(".faqFilter").focus(function() {
        $(this).select();
    });

    $(".faqNav span").click(function() {
        var val = $(this).html().toLowerCase();

        $(".faqText").hide();
        $(".faqFilter").val($(this).html());
        $(".faqTitle").each(function() {
            if ($(this).html().toLowerCase().indexOf(val) == 0) {
                $(this).show();
            } else {
                $(this).hide();
            }
        });
    });

    $(".faqFilter").keyup(function() {
        var val = $(this).val().toLowerCase();
        $(".faqText").hide();
        if (val.length > 0) {
            $(".faqTitle").each(function() {

                if ($(this).html().toLowerCase().indexOf(val) >= 0 || $(this).next().html().toLowerCase().indexOf(val) >= 0) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        } else {
            $(".faqTitle").show();
        }

    });

    $(".content form").submit(function() {
        var form = $(this);
        var valid = true;
        var validatorMessage = "Prosim izpolnite vsa polja.";
        $("input[type=\"text\"].req", form).each(function(i, elm) {
            if ($(elm).val().length == 0) {
                valid = false;
            }
            if ($(this).hasClass("email")) {
                if (!validateEmail($(this).val())) {
                    valid = false;
                    validatorMessage = "Prosim vnesite pravilen postni naslov.";
                }
            }
        });
        if (!valid) {
            alert(validatorMessage);
        } else {
            $.post("form.php", $(this).serialize(), function(data){
                form.replaceWith($(data));
            });
        }



        return false;
    });

});
