(function( $ ){

  $.fn.validate = function() {
		this.bind('submit', function(){
			valid = true;
			focus_element = null;
			
			$(this).find('input.required').each(function(){
				value = $(this).val();
				if (value == ''){
					valid = false;
					$(this).addClass('error');
					if (focus_element==null){focus_element = $(this);}
				}else{
					$(this).removeClass('error');
				}
			});
			
			$(this).find('select.required').each(function(){
				value = $(this).val();
				if (value == ''){
					valid = false;
					$(this).addClass('error');
					if (focus_element==null){focus_element = $(this);}
				}else{
					$(this).removeClass('error');
				}
			});
			
			$(this).find('input.required.email').each(function(){
				value = $(this).val();
				atpos=value.indexOf("@");
				dotpos=value.lastIndexOf(".");
				
				if (atpos<1 || dotpos<atpos+2 || dotpos+2>=value.length)
				{
					valid = false;
					$(this).addClass('error');
					if (focus_element==null){focus_element = $(this);}
				}else{
					$(this).removeClass('error');
				}
			});
			
			if (focus_element!=null){
				focus_element.focus();
			}
			
			return valid;
			
		});
  };

})( jQuery );
