/**
 * Blocks
 * Usage: $(<selector>).validator<name>();
 */

jQuery.fn.extend({
	validatorCheckbox: function(){		
		result = false;
		if($(this).attr('checked') == true || $(this).attr('checked') == 'checked')
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	
	validatorLatin: function (){
		result = true;
		var pat=/^[\w-\d]+$/i;
		
		if(pat.test($(this).val()))
		{	
			result = true;
			$(this).validatorError(result);
			$(this).validatorLength(3,10);
		}
		else 
		{
			result = false;
			$(this).validatorError(result);
		}
		
		return result;
	},
	
	validatorMail: function (){
		result = true;
		var pat=/^[\w-+\.]+@([\w-]+\.)+[\w-]{2,}$/i;
		
		if(pat.test($(this).val()))
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	
	
	validatorDate: function (){
		result = true;
		var pat=/^\d{2}[\/\.-]\d{2}[\/\.-]\d{2,4}$/i;
		
		if(pat.test($(this).val()))
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	
	validatorEqual: function (equalto){
		result = true;
		
		if($(this).val() == equalto)
		{
			if(equalto == '')
			{
				result = false;
			}
			else 
			{
				result = true;
			}
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	
	validatorPhone: function (){
		result = true;
		var pat=/^[\d +-]+$/i;
		if(pat.test($(this).val()))
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	
	validatorNumber: function (){
		result = false;
		var pat=/^[\d]+$/i;
		if(pat.test($(this).val()))
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	
	validatorLength: function(minlenght, maxlenght)
	{		
		maxlenght = (typeof maxlenght == 'undefined') ? 0 : maxlenght;
		minlenght = (typeof minlenght == 'undefined') ? 0 : minlenght;
		
		result = true;
										
		var value = $(this).val();
		value = (typeof value == 'undefined') ? '' : value;			
				
		if(minlenght > 0)
		{
			if ($.trim(value).length == 0)
			{
				result = false;
			}
		}
		if(minlenght > 0)
		{
			if ($.trim(value).length < minlenght) 
			{
				result = false;
			}
		}
		if(maxlenght > 0)
		{
			if ($.trim(value).length > maxlenght) 
			{
				result = false;
			}
		}
		
		if(minlenght == maxlenght)
		{
			$(this).validatorError(result);
		}
		else
		{
			$(this).validatorError(result);
		}
		
		return result;
	},
	
	validatorError: function(resulterror) {
		if(resulterror)
		{
			$(this).removeClass('error');
		}
		else 
		{
			$(this).addClass('error');
		}
	}
});
