/**
* Form Validator
*
* @created 5/1/2010
* @modified 5/26/2010
*
* @author Justin Kimbrell
* @version 0.1.2
*/
jQuery.fn.extend({
	
	regex: function(type) {
		
		if(typeof type != "object")
			var regex = $.getRegex(type);
		else
			var regex = type;
				
		return regex.test($(this).val());
	},
	
	check: function(obj) {
		
		var elem = $(this);
		var value = elem.val();
		var toPass = true;
		
		if(elem.length > 0) {
			if(obj.type)
				var regex = obj.type;
			
			if(obj.regex)
				var regex = obj.regex;
			
			if(obj.required)
				obj.require = obj.required;
			
			this.each(function() {
		
				if(regex && !elem.regex(regex))
					toPass = false;
					
				if(obj.isChecked && !elem.isChecked(obj.isChecked))
					toPass = false;
								
				if(obj.length && !elem.isLength(obj.length))
					toPass = false;
				
				if(obj.require && obj.require == true && !elem.isNotEmpty())
					toPass = false;
							
				if(obj.match && !elem.match(obj.match))
					toPass = false;
									
				if(obj.doNotMatch && elem.match(obj.doNotMatch))
					toPass = false;
					
				if(obj.fail && typeof obj.fail == "function" && toPass == false)
					obj.fail()
					
				if(obj.success && typeof obj.success == "function" && toPass == true)
					obj.success();
			});
		} else {
			alert('There is an error within the validation template. The element that is being validated does not exist within DOM. Compare your validation template against the form being validated for errors.');
		}
		
		return toPass;
	},
	
	isChecked: function(value) {
		var obj = $(this);
		var toPass = false;
		
		if(obj.is(":checked") == true)
			toPass = true;
		
		return toPass;
	}, 
	
	isLength: function(length) {
		
		 var obj = $(this);
		
		if(obj.val().length == length)
			return true;
		
		return false;
		
	},
	
	isNotEmpty: function() {
		var obj = $(this);
		
	    var filter = /^\s*$/;
	    
		if(filter.test(obj.val()))
			return false;
		
		return true;
	},

	
	isValidDate: function(filter) {
		
		var obj = $(this);
		
		if(!filter)
			var filter = /([0-9]{2})+\/+([0-9]{2})+\/+([0-9]{4})/;
		
		if(!filter.test(obj.val()))
			return false;
		
		return true;
	},
	
	match: function(obj2) {
		if($(this).val() != $(obj2).val())
			return false;
			
		return true;
	},
	
	placeholder: function(bind) {
		
		var obj = $(this);
		var placeholder = obj.attr("placeholder");
		
		if(bind == true) {
			obj.focus(function() {
				$(this).placeholder();
			});
			
			obj.blur(function() {
				$(this).placeholder();
			});
		}
		
		if(obj.val() == placeholder && obj.hasClass("default")) {
			obj.val("");
		} else if(obj.val() == "") {
			obj.addClass("default");
			obj.val(placeholder);
		}
	},
	
	validate: function(obj, action) {
		var toSubmit = true;
		
		$.each(obj, function(index, value) {
			$(index).check(value)
			
			if(toSubmit == true)
				toSubmit = $(index).check(value);
			else
				toSubmit = false;
		});	
		
		if(toSubmit == true && action) { 
			$(this).attr("action", action);
			
			return this;
		} else {
			return false;
		}
		
	}
	
});

jQuery.extend({	
	getRegex: function(type) {
		
		var regex = new Array();
		
		regex["alpha"] = /^[a-zA-Z]*$/;
		regex["abc"] = regex["alpha"];
		regex["numeric"] = /^[0-9]*$/;
		regex["123"] = regex["numeric"];
		regex["alphaNumeric"] = /[0-9][a-zA-Z]*$/;
		regex["abc123"] = regex["alphaNumeric"];
		regex["address"] = /[^0-9][a-zA-Z]$/;
		regex["123abc"] = regex["address"];
		regex["email"] = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		regex["m/d/Y"] = /^(([0]?[1-9]|1[0-2])\/([0-2]?[0-9]|3[0-1])\/[1-2]\d{3})? ?((([0-1]?\d)|(2[0-3])):[0-5]\d)?(:[0-5]\d)? ?(AM|am|PM|pm)?$/
		
		if(!regex[type])
			alert("The data type '"+type+"' is not valid.");
		
		return regex[type];
	}
});
