(function($) {
    $.fn.lsvalidate = function(options) {
        //append error style to head
        var image1 = $('<img />').attr('src', '/include/jquery/images/busy.gif');
        lsvalStyles = '<style type="text/css">'
        lsvalStyles += 'tr.error td { border-style: solid none solid none; border-width: 1px; border-color: #6E1422!important; background-color: #FEC7AF!important; border-collapse: separate!important; }'
        lsvalStyles += '';
        lsvalStyles += '</style>';
        $('head').append(lsvalStyles);
        //Define regular expressions for various input types
        emailReg = /^([a-zA-Z0-9_.-.'.+])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
        dateReg = /^\d{2}(\/)\d{2}\1\d{4}$/;
        usPhoneReg = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
        isNumericReg = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
        usZipReg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
        isDateReg = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
        //Get the form ID
        theFormID = "#" + this.get()[0].id;
        //Start the Check on submition 
        $(theFormID).submit(function(event) {
            $("#submitErrors").remove();
            formIDInputs = $(theFormID + " :input");
            var firstAlert = "";
            var finalAlert = "";
            $.each(formIDInputs, function(i, n) {
                var elemTitle = this.title;
                var elemID = '#' + this.id;
                var elemVal = $(elemID).val();
                //Validate that it has a value (required if title is present)
                if (elemTitle != '') {
                    if (!elemVal) {
                        if (!firstAlert) { firstAlert = elemID; }
                        finalAlert = finalAlert + "<li>" + elemTitle + "</li>";
                        errorItem(elemID);
                    }
                }
                //Validate Email format
                if (($(this).hasClass("validemail") || $(this).hasClass("valid_email")) && !emailReg.test(elemVal) && elemVal) {
                    if (!firstAlert) { firstAlert = elemID; }
                    finalAlert = finalAlert + "<li>Valid Email (example joe@site.com)</li>";
                    errorItem(elemID);
                }
                //Validate Date format
                if (($(this).hasClass("validdate") || $(this).hasClass("valid_date")) && !isDateReg.test(elemVal) && elemVal) {
                    if (!firstAlert) { firstAlert = elemID; }
                    finalAlert = finalAlert + "<li>Valid Date (example: 07/01/2009)</li>";
                    errorItem(elemID);
                }
                //Validate Zip Code
                if (($(this).hasClass("validezip") || $(this).hasClass("valid_zip")) && !usZipReg.test(elemVal) && elemVal) {
                    if (!firstAlert) { firstAlert = elemID; }
                    finalAlert = finalAlert + "<li>Valid Zip Code Format</li>";
                    errorItem(elemID);
                }
            });
            if (finalAlert) {
                //stop the Submit
                event.preventDefault();
                var divOffset = $(firstAlert).offset().top - 20;
                $('html,body').animate({ scrollTop: divOffset }, 1000);
                finalAlert = "The following highlighed areas require at least one selection or valid value:<ul>" + finalAlert + "</ul>";
                finalAlertfooter = "<p style='color:red;'>Remember to <strong>RE-SUBMIT</strong> your form after you have made the highlighted corrections.</p>";
                finalAlert = "<div id='submitErrors' title='Submission Errors' style='dispaly:none;top:0px;' >" + finalAlert + finalAlertfooter + "</div>";
                $("body").append(finalAlert);
                $("#submitErrors").dialog({ resizable: false, position: [480, 'top'] });
                $('.ui-dialog').css({ 'top': divOffset + 50 });
            }
            else {
                processingDialog = "<div id='processingDialog' title='Processing your form...'><p><img src='/include/jquery/images/busy.gif' /> Please do not click the back button or reload this page...</p></div>"
                $("body").append(processingDialog);
                $("#processingDialog").dialog({ resizable: false, modal: true, closeOnEscape: false, draggable: false, resizable: false, height: 50 });
                $('.ui-dialog-titlebar-close').css({ 'visibility': 'hidden' });
            }
            return true;
        });
        function errorItem(elemID) {
            $(elemID).parent().parent().addClass("error");
            $(elemID).focus(function() { $(this).parent().parent().removeClass("error"); $("#submitErrors").dialog('destroy'); })
        }

    };

})(jQuery);