(function ($) {
    $.fn.ivalidator = function (options) {
        var defaults = {
            message: "Management",
            color: "#F30000"
        };
        var options = $.extend(defaults, options);
        var thecolor = options.color;
        var message = options.message;
        var errs = 0;
        var req = 0;
        var targetForm = this;
        var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _-";
        var numeric = "1234567890 -+";
        var phone = "1234567890 -().";
        var alphaNumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890 -+_!.,'\"";
        targetForm.find(".required, .alpha, .numeric, .alphanumeric, .email").css("border-color", "");
        targetForm.find(".errors").remove();
        targetForm.find(".required").each(function () {
            if ($(this).val() == "" || $(this).find(":selected").val() == "") {
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;This is a required field<br /></span>").insertBefore($(this));
                errs += 1;
                req += 1;
            }
        });
        targetForm.find("*[maxlength]").each(function () {
            if ($(this).val().length > $(this).attr("maxlength")) {
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;Max length for this field is " + $(this).attr("maxlength") + "<br /></span>").insertBefore($(this));
                errs += 1;
            }
        });
        targetForm.find("*[minlength]").each(function () {
            if ($(this).val().length < $(this).attr("minlength")) {
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;Minimum length for this field is " + $(this).attr("minlength") + "<br /></span>").insertBefore($(this));
                errs += 1;
            }
        });
        targetForm.find(".email").each(function () {
            var checkemail = $(this).val();
            if (checkemail.length > 50) { // Max length email
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;Email Address must be 50 characters or less<br /></span>").insertBefore($(this));
                errs += 1;
            }
            if (checkemail.indexOf("@") == -1 || checkemail.indexOf(".") == -1) { // Email valid (contains @ and .)
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;Email Address must contain both '@' and '.' characters<br /></span>").insertBefore($(this));
                errs += 1;
            }
        });
        targetForm.find(".alpha").each(function () {
            var boom = $(this).val().split("");
            var track = 0;
            $.each(boom, function () {
                if (alpha.indexOf(this) == -1) {
                    errs += 1;
                    track += 1;
                }
            });
            if (track > 0) {
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;This field may only contain letters<br /></span>").insertBefore($(this));
            }
        });
        targetForm.find(".numeric").each(function () {
            var boom = $(this).val().split("");
            var track = 0;
            $.each(boom, function () {
                if (numeric.indexOf(this) == -1) {
                    errs += 1;
                    track += 1;
                }
            });
            if (track > 0) {
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;This field may only contain numbers<br /></span>").insertBefore($(this));
            }
        });
        targetForm.find(".alphanumeric").each(function () {
            var boom = $(this).val().split("");
            var track = 0;
            $.each(boom, function () {
                if (alphaNumeric.indexOf(this) == -1) {
                    errs += 1;
                    track += 1;
                }
            });
            if (track > 0) {
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;This field may only contain letters and numbers<br /></span>").insertBefore($(this));
            }
        });
        targetForm.find(".phone").each(function () {
            var boom = $(this).val().split("");
            var track = 0;
            $.each(boom, function () {
                if (phone.indexOf(this) == -1) {
                    errs += 1;
                    track += 1;
                }
            });
            if (track > 0) {
                $(this).css("border-color", thecolor);
                $("<span class='errors'>&darr;Must be a valid phone number<br /></span>").insertBefore($(this));
            }
        });
        if (errs > 0) {
            alert("There were errors in your form.\nPlease look it over and submit again.\n\nThank You,\n" + message);
            return false;
        } else {
            return true;
        }
    };
})(jQuery);
