$(document).ready(function () {
    var validators = [/.+/, /\w+@\w+\.\w+/, /[[\(\d{3}\)|\d{3}][\.\-\/\s]?\d{3}[\.\-\/\s]?\d{4}|\d{10}]/, /\d{5}[\-\d{4}]?/, /^(0?[1-9]|1[0-2])[\-\/]((0?|[1-2])[1-9]|3[0-1])[\-\/](\d{4}$|\d{2}$)/, /\d+/, /^\d{4}$/, /^\d{5}$/];

    function validate(inputs, tests, optional, event) {
        var isValid = true;
        var pos = 0;
        inputs.each(function () {
            var val = $(this).val();
            var valid = (optional[pos] == 1 && (val == "" || val == $(this).attr("placeholder"))) || (val != "" && val != $(this).attr("placeholder") && validators[tests[pos]].test(val));
            if (!valid) {
                isValid = false;
                $(this).addClass("errorSource");
            }
            pos++;
        });
        if (!isValid) {
            event.preventDefault();
        }
        return isValid;
    }
  
    $("input, textarea").focus(function () {
        $(this).removeClass("errorSource");
    });
    
    var submit = "";

    $("form#ContactPageForm").submit(function (event) {
        var tests = [0, 0, 0, 1, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0];
        var optionals = [0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1];
        var inputs = $(this).find("input[type='text'], textarea, select");
        return validate($(inputs), tests, optionals, event);
    });
  
    $("form#aspnetForm").submit(function (event) {
        var tests = [1];
        var optionals = [0];
        var inputs = $(this).find("input[type='text']");
        return validate($(inputs), tests, optionals, event);
    });
});
