﻿
function ajaxFormValidation(myForm) {
    var isValid = true;

    // E-Mail-Adresse
    function email(value) {
        var regExEmail = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
        return regExEmail.test(value);
    }

    // Nicht leer
    function required(value) {
        return (value != "");
    }

    $(myForm + " input.required," + myForm + " textarea.required").each(function () {
        if (required($(this).val())) {
            $(this).removeClass("active");
        } else {
            $(this).addClass("active");
            isValid = false;
        }
    });

    $(myForm + " input.email").each(function () {
        if (email($(this).val())) {
            $(this).removeClass("active");
        } else {
            $(this).addClass("active");
            isValid = false;
        }
    });

    return isValid;
}

// .net Formular via Ajax absenden
function submitFormViaAjax(myForm) {
    if (ajaxFormValidation(myForm)) {
        // Event-Target manuell setzen (ID des Senden-Linkbuttons)
        $("#__EVENTTARGET").val("ctl00$cphContent$lnkSenden");
        // Action
        var url = $(myForm).attr("action");
        // Input Values serialisieren
        var params = $(myForm).serialize();
        // Postback via Ajax
        $.ajax({
            type: 'POST',
            url: url,
            data: params,
            success: function (data) {
                // Ergebnis anzeigen                        
                var content = $(data).filter("#masterframe").html();
                $("#masterframe").html(content);
            }, error: function (xhr, textStatus, errorThrown) {
                alert(xhr.responseText);
            }
        });
    }
}


// Formular :focus-Effekte
function prepareForm() {
    /* Input Felder Focus/Blur */
//    jQuery(".frmInq input, .frmInq textarea").first().each(function () {
//        if (jQuery(this).focus()) {
//            if (jQuery(this).parent().find("label:first").length != 0) {
//                jQuery(this).parent().find("label:first").css("display", "none");
//            } else {
//                jQuery(this).parent().parent().find("label:first").css("display", "none");
//            }
//        }
//    });

//    jQuery("select").each(function () {
//        if (jQuery(this).parent().find("label:first").length != 0) {
//            jQuery(this).parent().find("label:first").css("display", "none");
//        } else {
//            jQuery(this).parent().parent().find("label:first").css("display", "none");
//        }
//    });


//    $(".frmInq input, .frmInq textarea").each(function () {
//        // OnLoad
//        if ($(this).val() != "") {
//            $(this).parent().find("label:first").hide();
//        }

//        //  OnFocus
//        $(this).bind("focus", function () {
//            if ($(this).parent().find("label:first").length != 0) {
//                $(this).parent().find("label:first").fadeOut(150);
//            } else {
//                $(this).parent().parent().find("label:first").fadeOut(150);
//            }
//        });

//        // OnBlur
//        $(this).bind("blur", function () {
//            if ($(this).val() == "") {
//                if ($(this).parent().find("label:first").length != 0) {
//                    $(this).parent().find("label:first").fadeIn(150);
//                } else {
//                    $(this).parent().parent().find("label:first").fadeIn(150);
//                }
//            }
//        });
//    });
}
