jQuery.fn.validate = function(options){
// if nothing is selected, return nothing; can't chain anyway
	if (!this.length) {
		options && options.debug && window.console && console.warn( "nothing selected, can't validate, returning nothing" );
		return;
	}
	
	// check if a validator for this form was already created
	var validator = jQuery.data(this[0], 'validator');
	if ( validator ) {
		return validator;
	}
	
	validator = new jQuery.validator( options, this[0] );
	jQuery.data(this[0], 'validator', validator); 
	validator.cancelSubmit = false;
	if ( validator.settings.onsubmit ) {
		
		// allow suppresing validation by adding a cancel class to the submit button
		this.find("input.cancel:submit").click(function() {
			validator.cancelSubmit = true;
		});
	
		// validate the form on submit
		this.submit( function( event ) {
			validator.clickedSubmit = true;
			if ( validator.settings.debug )
				// prevent form submit to be able to see console output
				event.preventDefault();
				
			function handle() {
				if ( validator.settings.submitHandler ) {
					validator.settings.submitHandler.call( validator, validator.currentForm );
					return false;
				}
				$("button, input[type=submit], input[type=button], input[type=image]").click(
					function(){
						return false;
					}
				);
				return true;
			}
				
			// prevent submit for invalid forms or custom submit handlers
			if ( validator.cancelSubmit ) {
				validator.cancelSubmit = false;
				return handle();
			}
			if ( validator.form() ) {
				if ( validator.pendingRequest ) {
					validator.formSubmitted = true;
					return false;
				}
				return handle();
			} else {
				validator.focusInvalid();
				return false;
			}
		});
	}
	
	return validator;
}

jQuery.validator.prototype.element = function( element ) {
	element = this.clean( element );
	this.lastElement = element;
	this.prepareElement( element );
	var result = this.check( element );
	if ( result ) {
		delete this.invalid[element.name];
	} else {
		this.invalid[element.name] = true;
	}
	if ( !this.numberOfInvalids() ) {
		// Hide error containers on last error
		this.toHide.push( this.containers );
		if(!this.displayOK && this.clickedSubmit){
			$("#formErrors").after($("<div id='validForm'>Errors have been corrected, please resubmit the form</div>"));
			this.displayOK = true;
		}
	} else {
		$("#validForm").remove();
		this.displayOK = false;
	}
	
	this.showErrors();
	return result;
}

jQuery.validator.prototype.defaultShowErrors = function() {
	for ( var i = 0; this.errorList[i]; i++ ) {
		var error = this.errorList[i];
		this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass );
		this.showLabel( error.element, error.message );
	}
	if( this.errorList.length ) {
		this.toShow.push( this.containers );
		$("#validForm").remove();
	}
	if (this.settings.success) {
		for ( var i = 0; this.successList[i]; i++ ) {
			this.showLabel( this.successList[i] );
		}
	}
	this.toHide = this.toHide.not( this.toShow );
	this.hideErrors();
	this.addWrapper( this.toShow ).show();
}