/*

	LOG
	--------------------------------------------------------------------------------------------------------------------------	
	
	Version 1.1:
	Issue with isNumber() function fixed. The parameter was omitted from the isString() function and it was causing it not to work as expected
	
	--------------------------------------------------------------------------------------------------------------------------	
	
	Version 1.2:
	Typo in the radio and checkboxes causing bug fixed. 


*/


(function($){
	
	function isString(oArray)//Checks if paramenter is a string and not empty
	{
		if(oArray.length == 0){
			return false;
		}
		
		for(var i = 0, x = oArray.length; i < x; i++){
			if(typeof oArray[i] != 'string' || $.trim(oArray[i]).length == 0){
				return false;
			}
		}
		
		
		return true;			
	}
	
	function isEmail(oArray)//Checks if string is an email address
	{
		if(!isString(oArray)){
			return false;
		}
		
		var b=/^.+@.+\..{2,6}$/;//RegExp to check if it is valid email address
		var e=/[\(\)\<\>\,\;\:\\\"\[\]]/;//RegExp to check if there are illegal characters
		
		for(var i = 0, x = oArray.length; i < x; i++){
			if(!(b.test(oArray[i])))return false;
			if(oArray[i].match(e))return false;
		}
		
		return true;
	}
	
	function isNumber(oArray)//Checks if it is a number
	{
		if(!isString(oArray)){
			return false;
		}
		
		for(var i = 0, x = oArray.length; i < x; i++){
			if(isNaN(oArray[i])){
				return false;//Check if it is a string
			}
		}
		return true;		
	}
	
	function isEnumerated(oArray,a)
	{
		if(!isString(oArray)){
			return false;
		}
		
		for(var i = 0, x = oArray.length; i < x; i++){
			for(var y = 0, z = a.length; y < z; y++){
				if(oArray[i] == a[y])return true;
			}
		}
		
		return false;
	}

	
	$.fn.validField = function(options){
		
		var o = $.extend({
			varType: 'string', //Types supported: string, email, number, enumerated, enumeratedNegated
			enumerated: []
		},options);
		
		switch(true)//this switch is used to extract the value from the field
		{
			case this.is('input[type="text"]'):
			case this.is('textarea'):
			case this.is('select'):
				var val = [$.trim($(this).val())];
				break;
			case this.is('input[type="checkbox"]'):
				
				var boxes = this.closest('form').find('input[name=' + this.attr('name') + ']:checked');
				if(boxes.length > 0)
				{
					var oArray = new Array();
					boxes.each(function(){
						oArray.push($.trim($(this).val()));	
					});
					var val = oArray;
				} else {
					var val = [];	
				}
				break;	
			case this.is('input[type="radio"]'):
				var boxes = this.closest('form').find('input[name=' + this.attr('name') + ']:checked');
				if(boxes.length > 0)
				{
					var val = [$.trim(boxes.val())];	
				} else {
					var val = [];	
				}
				break;	
				break;
			default:
				var val = [$.trim($(this).val())];		
		}
		
		
			
		
		if(typeof o.varType == 'undefined')
		{
			return isString(val);		
		} else {
			
			switch(true)
			{
				case o.varType.match(/enumeratedNegated/gi) != null:
					return (isString(val) && !isEnumerated(val,o.enumerated));
					break;
				case o.varType.match(/enumerated/gi) != null:
					return isEnumerated(val,o.enumerated);
					break;
				case o.varType.match(/email/gi) != null:
					return isEmail(val);
					break;
				case o.varType.match(/number/gi) != null:
					return isNumber(val);
					break;
				default:
					return isString(val);	
			}		
		}
		
	};
	
	
	
})(jQuery);
