(function($){

	var validated = new Array();
	
	$.fn.Validate = function (options) {
		
		var settings = {
			clickValidate: false,
			type: false
		};
		
		if (options) { 
			$.extend(settings, options);
		}
		
		var emailRegex = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		
		var focus_input = "focus";
		var focus_area = "focus";
		var focus_select = "focus";
		var valid_input = "valid";
		var valid_area = "valid";
		var valid_select = "valid";
		var req_input = "invalid";
		var req_area = "invalid";
		var req_select = "invalid";
		
		if ($(this).attr("type") === "radio") {
			var type_ = "radio";
			var focus_ = focus_select;
			var valid_ = valid_select;
			var req_ = req_select;
		} else if ($(this)[0].tagName === "TEXTAREA") {
			var type_ = "textarea";
			var focus_ = focus_area;
			var valid_ = valid_area;
			var req_ = req_area;
		} else if ($(this)[0].tagName === "INPUT") {
			var type_ = "input";
			var focus_ = focus_input;
			var valid_ = valid_input;
			var req_ = req_input;
		} else if ($(this)[0].tagName === "SELECT") {
			var type_ = "select";
			var focus_ = focus_select;
			var valid_ = valid_select;
			var req_ = req_select;
		}
		
		validated.push($(this).attr("id"));
		
		if ($(this).attr("type") === "radio") {
			var name = $(this).attr("name");
			var checked = false;
			$("input[name="+name+"]").each(function(){
				$(this).bind("change", function(event) {
					$(this).radioValidate();
				});
				
				if ($(this).is(':checked')) {
			   		checked = true;
				}
			});
			if (checked) {
				var id = $(this).attr("id");
				validated = jQuery.grep(validated, function(value) {
					return value != id;
				});					
			}				
		} else if (($.trim($(this).val()) !== "") && ($(this).val() != "default") && (!$(this).hasClass("default"))) {
			var id = $(this).attr("id");
			validated = jQuery.grep(validated, function(value) {
				return value != id;
			});	
		}
		
		$(this).focus(function(event) { $(this).addClass(focus_); });
		$(this).blur(function(event) { $(this).removeClass(focus_); });
		
		$.fn.textareaValidate = function () {
			if ($.trim($(this).val()) !== "") {
				$(this).removeClass(req_area).addClass(valid_area);
				var id = $(this).attr("id");
				validated = jQuery.grep(validated, function(value) {
					return value != id;
				});
			} else if ($.trim($(this).val()) === "") {
				$(this).removeClass(valid_area).addClass(req_area);
				validated.push($(this).attr("id"));
			}
		}
		
		$.fn.inputValidate = function (options) {
			var prop = {
				type: false
			};			
			if (options) { 
				$.extend(prop, options);
			}
			
			if ($.trim($(this).val()) !== "") {	
				if (prop.type === "email") {
					var value = $(this).val();
					if(emailRegex.test(value)) {
						$(this).removeClass(req_input).addClass(valid_input);
						var id = $(this).attr("id");
						validated = jQuery.grep(validated, function(value) {
							return value != id;
						});
					} else {
						$(this).removeClass(valid_input).addClass(req_input);
						validated.push($(this).attr("id"));
					}
				} else {
					$(this).removeClass(req_input).addClass(valid_input);
					var id = $(this).attr("id");
					validated = jQuery.grep(validated, function(value) {
						return value != id;
					});
				}
			} else if ($.trim($(this).val()) === "") {
				$(this).removeClass(valid_input).addClass(req_input);
				validated.push($(this).attr("id"));
			}
		}
		
		$.fn.selectValidate = function () {
			if (($.trim($(this).val()) !== "") && ($(this).val() != "default")) {
				$(this).next(".fakeSelect").removeClass(req_select).addClass(valid_select);
				var id = $(this).attr("id");
				validated = jQuery.grep(validated, function(value) {
					return value != id;
				});
			} else if ($.trim($(this).val()) === "") {
				$(this).next(".fakeSelect").removeClass(valid_select).addClass(req_select);
				validated.push($(this).attr("id"));
			}
		}
		
		$.fn.radioValidate = function () {
			var name = $(this).attr("name");
			var checked = false;
			$("input[name="+name+"]").each(function(){
				if ($(this).is(':checked')) {
			   		checked = true;
				}
			});
			if (checked) {
				var id;
				$("input[name="+name+"]").each(function(){
					if ($(this).attr("id")) { id = $(this).attr("id"); }
					$(this).parent().removeClass(req_select).addClass(valid_select);
				});
				validated = jQuery.grep(validated, function(value) {
					return value != id;
				});					
			} else {
				$("input[name="+name+"]").each(function(){
					$(this).parent().removeClass(valid_select).addClass(req_select);
				});
				validated.push($(this).attr("id"));
			}
		}
		
		$.fn.clickValidate = function() {
			$(this).removeClass(req_input).addClass(valid_input);
			var id = $(this).data("id");
			validated = jQuery.grep(validated, function(value) {
				return value != id;
			});
		}
		
		$(this).change(function() {
			if (type_ === "input") {
				$(this).inputValidate(settings);
			} else if (type_ === "textarea") {
				$(this).textareaValidate();	
			} else if (type_ === "select") {
				$(this).selectValidate();	
			}
		});
		
	}
	
	$.fn.unValidate = function () {
		var id = $(this).attr("id");
		validated = jQuery.grep(validated, function(value) {
			return value != id;
		});
	}
	
	$.checkValidate = function () {
		return validated;
	}

})(jQuery);
