/*
Required Fields - Set all element under the attached form must contain value/be checked
Works for IE7, FF3, Chrome, Safari, Opera
By: Chonla
Create Date: 14 August 2009
URL: http://blog.chonla.com
*/

(function($) {
	$.fn.requiredfields = function(options)
	{
		var defaults = {
			message : "Some required field is missing.",
			elements : ""
		};
		options = $.extend(defaults, options);
		if (options.elements == "") options.elements = "[required=yes]";
		this.submit(function(){
			var submit_break = false;
			$(this).find(options.elements).each(function(){
				switch (this.tagName.toLowerCase())
				{
					case "input" :
						switch ($(this).attr("type").toLowerCase())
						{
							case "text":
							case "password":
								if ($(this).val() == "")
								{
									submit_break = true;
									alert(options.message);
									return false;
								}
								break;
							case "checkbox":
								if (!$("input[type=checkbox][name=" + $(this).attr("name") + "]").is(":checked"))
								{
									submit_break = true;
									alert(options.message);
									return false;
								}
								break;
						}
						break;
					case "textarea" :
						if ($(this).val() == "")
						{
							submit_break = true;
							alert(options.message);
							return false;
						}
						break;
				}
			});
			if (submit_break)
			{
				return false;
			}
			return true;
		});
	};
})(jQuery);

