(function($){
	$.fn.validator = function() {
		var color;
		var color2;
		var data;
		var comunicat = $('.submit', this).attr('title');
		$('.submit', this).attr('title', '');
		
		$('.data', this).each(function(){
			color = $(this).css('border-color');
			color2 = $(this).css('color');
			data = $(this).attr('title').split(':');
			$(this).attr('alt', $(this).attr('value'));	
			$(this).attr('title', $(this).attr('value'));
			$(this).attr('requair', data[0]=='requair'?1:0);
			$(this).attr('kind', data[1]);
			$(this).attr('text', data[2]);
		});
		
		$('.data', this).click(function() {
			if($(this).attr('value') == $(this).attr('alt'))
			{
				$(this).attr('value', '');
				$(this).css('font-style', 'normal');
				$(this).css('color', '#000');
			}
		});
		$('.data', this).focus(function() {
			if($(this).attr('value') == $(this).attr('alt'))
			{
				$(this).attr('value', '');
				$(this).css('font-style', 'normal');
				$(this).css('color', '#000');
			}
		});
		$('.data', this).blur(function() {
			if($(this).attr('value') == '')
			{
				$(this).attr('value', $(this).attr('alt'));
				$(this).css('font-style', 'italic');
				$(this).css('color', color2);
			}
		})
		
		$('form', this).submit(function() {
		    var error = false;
		    var text = comunicat+"\n";
		    if($(this).attr('name') == 'zumi')setZumiData($(this));
			$('.data', $(this)).each(function(){
				if($(this).attr('requair') != 1 || $(this).attr('value') != $(this).attr('alt'))
				{
					if(checkEmail($(this).attr('kind'), $(this).attr('value')))
					{
						$(this).css('border-color', color);
					}
					else
					{
						$(this).css('border-color', '#F00');
						text += $(this).attr('text')+"\n";
						error = true;
					}	
				}
				else
				{
					$(this).css('border-color', '#F00');
					text += $(this).attr('text')+"\n";
					error = true;
				}
			});
			if(error)
				alert(text);
			else
				$('.data', $(this)).each(function(){ if($(this).attr('requair') != 1 && ($(this).attr('value') == $(this).attr('alt')))$(this).attr('value', '-'); });
			return !error;
		});
	}
})(jQuery);

function checkEmail(type, value)
{
	if(type != 'email')return true;
	var pattern = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return pattern.test(value);
}

function setZumiData(object)
{
	$('#poczatektrasy', object).attr('value', $('.input[name="miasto"]', object).attr('value')+','+$('.input[name="ulica"]', object).attr('value'));
}
