checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
this.check( elements[i] );
}
return this.valid();
},

// http://docs.jquery.com/Plugins/Valid...idator/element
element: function( element ) {
element = this.clean( element );
this.lastElement = element;
this.prepareElement( element );
this.currentElements = $(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 = this.toHide.add( this.containers );
}
this.showErrors();
return result;
},

// http://docs.jquery.com/Plugins/Valid...tor/showErrors
showErrors: function(errors) {
if(errors) {
// add items to error list and map
$.extend( this.errorMap, errors );
this.errorList = [];
for ( var name in errors ) {
this.errorList.push({
message: errors[name],
element: this.findByName(name)[0]
});
}
// remove items from success list
this.successList = $.grep( this.successList, function(element) {
return !(element.name in errors);
});
}
this.settings.showErrors
? this.settings.showErrors.call( this, this.errorMap, this.errorList )
: this.defaultShowErrors();
},

// http://docs.jquery.com/Plugins/Valid...ator/resetForm
resetForm: function() {
if ( $.fn.resetForm )
$( this.currentForm ).resetForm();
this.submitted = {};
this.prepareForm();
this.hideErrors();
this.elements().removeClass( this.settings.errorClass );
},

numberOfInvalids: function() {
return this.objectLength(this.invalid);
},

objectLength: function( obj ) {
var count = 0;
for ( var i in obj )
count++;
return count;
},

hideErrors: function() {
this.addWrapper( this.toHide ).hide();
},

valid: function() {
return this.size() == 0;
},

size: function() {
return this.errorList.length;
},

focusInvalid: function() {
if( this.settings.focusInvalid ) {
try {
$(this.findLastActive() || this.errorList.length && this.errorList[0].element || []).filter(":visible").focus();
} catch(e) {
// ignore IE throwing errors when focusing hidden elements
}
}
},

findLastActive: function() {
var lastActive = this.lastActive;
return lastActive && $.grep(this.errorList, function(n) {
return n.element.name == lastActive.name;
}).length == 1 && lastActive;
},

elements: function() {
var validator = this,
rulesCache = {};

// select all valid inputs inside the form (no submit or reset buttons)
// workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
return $([]).add(this.currentForm.elements)
.filter(":input")
.not(":submit, :reset, :image, [disabled]")
.not( this.settings.ignore )
.filter(function() {
!this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);

// select only the first element for each name, and only those with rules specified
if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
return false;

rulesCache[this.name] = true;
return true;
});
},

clean: function( selector ) {
return $( selector )[0];
},

errors: function() {
return $( this.settings.errorElement + "." + this.settings.errorClass, this.errorContext );
},

reset: function() {
this.successList = [];
this.errorList = [];
this.errorMap = {};
this.toShow = $([]);
this.toHide = $([]);
this.formSubmitted = false;
this.currentElements = $([]);
},

prepareForm: function() {
this.reset();
this.toHide = this.errors().add( this.containers );
},

prepareElement: function( element ) {
this.reset();
this.toHide = this.errorsFor(element);
},

check: function( element ) {
element = this.clean( element );

// if radio/checkbox, validate first element in group instead
if (this.checkable(element)) {
element = this.findByName( element.name )[0];
}

var rules = $(element).rules();
var dependencyMismatch = false;
for( method in rules ) {
var rule = { method: method, parameters: rules[method] };
try {
var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters );

// if a method indicates that the field is optional and therefore valid,
// don't mark it as valid when there are no other rules
if ( result == "dependency-mismatch" ) {
dependencyMismatch = true;
continue;
}
dependencyMismatch = false;

if ( result == "pending" ) {
this.toHide = this.toHide.not( this.errorsFor(element) );
return;
}

if( !result ) {
this.formatAndAdd( element, rule );
return false;
}
} catch(e) {
this.settings.debug && window.console && console.log("exception occured when checking element " + element.id
+ ", check the '" + rule.method + "' method");
throw e;
}
}
if (dependencyMismatch)
return;
if ( this.objectLength(rules) )
this.successList.push(element);
return true;
},

// return the custom message for the given element and validation method
// specified in the element's "messages" metadata
customMetaMessage: function(element, method) {
if (!$.metadata)
return;

var meta = this.settings.meta
? $(element).metadata()[this.settings.meta]
: $(element).metadata();

return meta && meta.messages && meta.messages[method];
},

// return the custom message for the given element name and validation method
customMessage: function( name, method ) {
var m = this.settings.messages[name];
return m && (m.constructor == String
? m
: m[method]);
},