
jQuery.fn.accValidate = function(){
    
    var form = this;
    
    //The jQuery validation plug-in in action              
    $(form).validate({
        
        //Optional, but for demo purpose we only want to validate on submitting the form
        onfocusout: false,
        onkeyup: false,
        onclick: false,
        
        //We are going to focus on the first link in the error list so i have disabled 
        //the input error focus option in jQuery Validation
        focusInvalid: false,

        
        //Set the element which will wraps the inline error messages
	    errorElement: "strong",
        
        //Location for the inline error messages
        //In this case we will place them in the associated label element
        errorPlacement: function(error, element) {
            error.appendTo($('label[for="' + $(element).attr('id') + '"]', form));
        },
        
        //Create our error summary that will appear before the form
        showErrors: function(errorMap, errorList) {
            if (submitted && errorList) {
                    
                var $errorFormId = 'errors-' + form.attr('id');

                //Reset and remove error messages if the form 
                //has been validated once already
                summary = "";
                $('label .error', form).remove();                            
                
                //Create our container if one doesnt already exits
                //better than an empty div being in the HTML source
                if($('#' + $errorFormId).length === 0) {
                    $('<div id="' + $errorFormId + '" class="errorContainer"/>').insertBefore(form); 
                }
                
                //Generate our error summary list
                for (error in errorList) {
                    //get associated label text to be used for the error summary
                    var $errorLabel = $('label[for="' + $(errorList[error].element).attr('id') + '"]').text();
                    summary += '<li><a href="#' + errorList[error].element.id + '">' + $errorLabel + ': ' + errorList[error].message + '</a></li>';
                }
                
                //Output our error summary and place it in the error container
                $('#' + $errorFormId).html('<h2>Errors found in the form</h2><p><em>Whoops!</em> - There is a problem with the form, please check and correct the following:</p><ul>' + summary + '</ul>');
                
                //Focus on first error link in the error container 
                //Alternatively, you might want to use the Validation default option (focusInvalid)
                $('#' + $errorFormId + ' a:eq(0)').focus();
                
                //Move the focus to the associated input when error message link is triggered
                //a simple href anchor link doesnt seem to place focus inside the input
                $('#' + $errorFormId + ' a').click(function() {
                    $($(this).attr('href')).focus();
                    return false;
                });
            }
            this.defaultShowErrors();
                submitted = false;
        },                    
        invalidHandler: function(form, validator){
            submitted = true;
        }
    });                  
};
