function lightclose() {
    $("#contact-form .field .control").removeClass('error');
    $("#contact-form .errormsg").text('');    
    $("#kontakt .form").show();
    $("#kontakt .thanks").hide();
    $("#lightframe .light-content").hide();
    $("#lightframe").hide();
    $("#lightbox").hide();
    $("#flashvideo-swf").show();
}
function lightshow(id) {
    $("#lightbox").show();
    $("#lightframe").show();
    $("#lightframe #"+id).show();
    $("#flashvideo-swf").hide();
}

function videoFinished() {
    $("#flashvideo-swf").hide();
    $("#flashvideo").hide();
    $("#page .adv-block-left").show();
}

function videoSart() {
    $("#flashvideo").show();
    $("#flashvideo-swf").show();
    $("#page .adv-block-left").hide();
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

function isPLZCorrect(PLZ) {
    var pattern = new RegExp(/[0-9]{5}/i);
    return pattern.test(PLZ);
}


$(document).ready(function() {
    var errors = Array();
    errors['vorname_form'] = 'Bitte geben Sie Ihren Vornamen ein';
    errors['nachname_form'] = 'Bitte geben Sie Ihren Nachnamen ein';
    errors['vorname'] = 'Bitte geben Sie Ihren Vornamen ein';
    errors['nachname'] = 'Bitte geben Sie Ihren Nachnamen ein';
    errors['email'] = 'Bitte geben Sie Ihre E-Mail-Adresse ein';
    errors['email_wrong'] = 'Bitte geben Sie Ihre E-Mail-Adresse ein';
    errors['strasse'] = 'Bitte geben Sie Ihre Straße ein';
    errors['hausnr'] = 'Bitte geben Sie Ihre Hausnummer ein';
    errors['plz'] = 'Bitte geben Sie Ihre PLZ ein';
    errors['plz_wrong'] = 'Bitte überprüfen Sie die Postleitzahl';
    errors['ort'] = 'Bitte geben Sie Ihren Ort ein';
    errors['einverstaendniserklaerung'] = 'Bitte erklären Sie sich mit den Datenschutzbedingungen einverstanden, indem Sie das Kästchen links des Textes anklicken.';
	errors['capture'] = 'Bitte geben Sie die nebenstehenden Buchstaben ein';

    $("#contact-form").submit(function() {

        var ok = true;
        var first = true;
        
        $.each($("#contact-form .checkit"), function() {
            if ($(this).val() == '') {
                $(this).closest(".control").addClass('error');
                if (!first) {
                    $("#contact-form .errormsg").append("<br/>" + errors[$(this).attr('id')]);
                } else {
                    $("#contact-form .errormsg").text(errors[$(this).attr('id')]);
                    first = false;
                }
                ok = false;
            } else if($(this).attr('id') == 'email') {
                if(!isValidEmailAddress($(this).val())) {
                    $(this).closest(".control").addClass('error');
                    if (!first) {
                        $("#contact-form .errormsg").append("<br/>" + errors['email_wrong']);
                    } else {
                        $("#contact-form .errormsg").text(errors['email_wrong']);
                        first = false;
                    }
                    ok = false;
                } else {
                    $(this).closest(".control").removeClass('error');
                }
            } else {
                $(this).closest(".control").removeClass('error');
            }
        });
        
        if (ok) {
            $.post("/fileadmin/templates/microsite/mailme.php", $("#contact-form").serialize(), function(data){
                $("#kontakt .form").hide();
                $("#kontakt .thanks").show();
            });
        }

        return false;
    });

    $("#test-form").submit(function() {

        var ok = true;
        
        $.each($("#test-form .checkit"), function() {
            if ($(this).attr('type') != 'checkbox') {
                if ($(this).val() == '') {
                    $(this).addClass('error');
                    $("#test-form #error-"+$(this).attr('id')).text(errors[$(this).attr('id')]);
                    ok = false;
                } else if($(this).attr('id') == 'plz') {
                    if(!isPLZCorrect($(this).val())) {
                        $(this).addClass('error');
                        $("#test-form #error-"+$(this).attr('id')).text(errors['plz_wrong']);
                        ok = false;
                    } else {
                        $(this).removeClass('error');
                        $("#test-form #error-"+$(this).attr('id')).text('');
                    }
                } else {
                    $("#test-form #error-"+$(this).attr('id')).text('');
                    $(this).removeClass('error');
                }
            } else {
                if ($(this).attr('checked') != true) {
                    $(this).addClass('error');
                    $("#test-form #error-"+$(this).attr('id')).text(errors[$(this).attr('id')]);
                    ok = false;
                } else {
                    $("#test-form #error-"+$(this).attr('id')).text('');
                    $(this).removeClass('error');
                }
            }
        });
        
        if (ok) {
            /*$.post("mailme.php", $("#test-form").serialize(), function(data){
                $("#content .form").hide();
                $("#content .thanks").show();
            });*/
            
            return true;
        }
        
        return false;
    });
});

