﻿$(document).ready(function() {
    $(".valFormSubmit, .Submit").click(function() {
        return validateForm(this);

    });


    $('.Validated').blur(function() {
        validate(this);
    });
    $('.Validated').change(function() {
        validate(this);
    });

    function validate(ctl) {
        if ($(ctl).val() == '') {
            $(ctl).addClass("invalid");
            return false;
        }
        else {
            $(ctl).removeClass("invalid");
            return true;
        }
    }

    function validateForm(sender) {

        var classid = $(sender).attr('class').split(' ').slice(-1);
        var selector = '';
        if (classid != 'Submit') {
            selector = '.' + classid;
        }

        var valid = true;
        $('.Validated' + selector).each(function() {
            if (valid) {
                valid = validate(this);
            }
            else {
                validate(this);
            }
        });
        if (!valid) {
            $('#errorText').html('At least one field is needs to be corrected. Please correct the fields in red above before submitting.');
            $('#errorText').css('color', 'red');
            $('#errorText').css('padding', '5px');
            $('#errorText').css('display', 'block');
        }
        else {
            $('#errorText').css('display', 'none');
        }
        return valid;
    }


});