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

jQuery.fn.extend({
	validatorCheckbox: function(){		
		if($(this).attr('checked') == true || $(this).attr('checked') == 'checked')
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	validatorMail: function (){
		var result = true;
		var pat=/^[\w-+\.]+@([\w-]+\.)+[\w-]{2,}$/i;
		
		if(pat.test($(this).val()))
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	validatorEqual: function (equalto){
		var result = true;
		
		if($(this).val() == equalto)
		{
			if(equalto == '')
			{
				result = false;
			}
			else 
			{
				result = true;
			}
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	validatorNumber: function (){
		var pat=/[\d]+$/;
		if(pat.test($(this).val()))
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	validatorDate: function (){
		var pat=/^\d{2}[-]\d{2}[-]\d{4}$/;
		if(pat.test($(this).val()))
		{
			result = true;
		}
		else 
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	validatorPhone: function (){
		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;
		
		var result = true;
		$(this).val($.trim($(this).val()));				
		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;
			}
		}
		
		$(this).validatorError(result);
		return result;
	},
	validatorFile: function()
	{		
		var result = true;
		var value = $.trim($(this).val());
		value = (typeof value == 'undefined') ? '' : value;			
				
		if ($.trim(value).length == 0)
		{
			result = false;
		}
		
		$(this).validatorError(result);
		return result;
	},
	validatorTextCounter: function(countfield, maxlimit) {
		var value = $(this).val();
		value = (typeof value == 'undefined') ? '' : value;	
		
		if (value.length > maxlimit)
		{
			$(this).val($.trim(value).substring(0, maxlimit));
			$(countfield).html(0);
			return false;
		}
		else
		{
			$(countfield).html(maxlimit - $.trim(value).length);
			return true;
		}
	},
	validatorError: function(result) {
		if(result)
		{
			$(this).removeClass('alert');
		}
		else 
		{
			$(this).addClass('alert');
		}
	}
});
