jQuery(function(){
	jQuery('form#contactform').setup_nifty_contact_validation();
});


jQuery.fn.setup_nifty_contact_validation = function(){
	jQuery(this).each(function(){
		var $form = jQuery(this);
		jQuery(this).validate({
			invalidHandler: function(form, validator) {
				jQuery.each(validator.invalid, function(key, value){
					jQuery('[name='+key+']').parents('p').addClass('error');
					
				});
				
				if(validator.numberOfInvalids()) {
					if($form.find('.error-notice').length < 1)
						$form.find('fieldset').prepend('<p class="error-notice">Please correct the errors below.</p>').find('p').eq(0).attr('tabIndex', -1).focus();
					else
						$form.find('.error-notice').attr('tabIndex', -1).focus()
				}
			},
			success: function(e) {
				jQuery(e).parents('p').removeClass('error')
			},
			focusInvalid: false,
			submitHandler: function(form) {
				form.submit();
			},
			errorElement: "em",
			messages: {
				'contact[email]': {
					required: "Please enter a valid email address, example: you@yourdomain.com",
					email: "Please enter a valid email address, example: you@yourdomain.com"
				}
			},
			debug:true
		});
	});
}