(function ($) {
    $.validation =
        function (options) {
            var defaults = {
                selector: 'form',
                err: 'This is required',
                errEmail: 'Valid email address is required',
                errUrl: 'Valid URL is required',
                errPhone: 'Phone number is required',
                notValidClass: 'invalid'
            };

            function check(obj) {
                if ($(obj).val() == '' || checkLabel(obj)) {
                    var errormsg = ($(obj).attr('title') != '') ? $(obj).attr('title') : options.err;
                    error(obj, errormsg)
                }
            };

            function checkRegEx(obj, type) {
                var regEx, err;
                switch (type) {
                case 'url':
                    regEx = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
                    err = options.errUrl;
                    break;
                case 'phone':
                    var regEx = /[\d\s_-]/;
                    err = options.errPhone;
                    break;
                default:
                    regEx = /^[^@]+@[^@]+.[a-z]{2,}$/;
                    err = options.errEmail
                };
                var val = $(obj).val();
                if (val.search(regEx) == -1 || checkLabel(obj)) {
                    var errormsg = ($(obj).attr('title') != '') ? $(obj).attr('title') : err;
                    error(obj, errormsg)
                }
            };

            function checkLabel(obj) {
                var text = $('label[for=' + $(obj).attr('id') + ']').text();
                return (text == $(obj).val())
            };

            function error(obj, errormsg) {
                var parent = $(obj).parent();
                parent.append('<span class="error">' + errormsg + '</span>');
                $('span.error', parent).hide().fadeIn('fast');
                $(obj).addClass(options.notValidClass);
                valid = false
            };
            $('input.label,textarea.label').each(function () {
                var text = $('label[for=' + $(this).attr('id') + ']').text();
                $('label[for=' + $(this).attr('id') + ']').css('display', 'none');
                $(this).val(text);
                $(this).focus(function () {
                    if ($(this).val() == text) $(this).val('')
                });
                $(this).blur(function () {
                    if ($(this).val() == '') $(this).val(text)
                })
            });
            if (typeof options == 'string') defaults.selector = options;
            var options = $.extend(defaults, options);
            return $(options.selector).each(function () {
                $(this).submit(function () {
                    $('.error', this).remove();
                    $('.' + options.notValidClass, this).removeClass(options.notValidClass);
                    valid = true;
                    $(':text.required', this).each(function () {
                        if ($(this).hasClass('email')) {
                            checkRegEx(this, 'email')
                        } else if ($(this).hasClass('url')) {
                            checkRegEx(this, 'url')
                        } else if ($(this).hasClass('phone')) {
                            checkRegEx(this, 'phone')
                        } else {
                            check(this)
                        }
                    });
                    $(':password.required', this).each(function () {
                        check(this)
                    });
                    $('textarea.required', this).each(function () {
                        check(this)
                    });
                    $(':checkbox.required', this).each(function () {
                        if (!$(this).attr('checked')) {
                            var errormsg = ($(this).attr('title') != '') ? $(this).attr('title') : options.err;
                            error(this, errormsg)
                        }
                    });
                    return valid
                })
            })
    }
})(jQuery);

