inputs = new Array;

inputs['body'] = 'Treść Twojej wiadomości';
inputs['email'] = 'Adres e-mail';
inputs['person_name'] = 'Imię i Nazwisko';


function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str))
}

$.fn.sendAjax = function() {
	$form = $(this);
	build = new Array;
	build.push('type='+router.module);
	$('input, select, textarea', this).not('.btn').each(function() {
		build.push(this.name+'='+this.value);
	});
	data = build.join('&');

	$.ajax({
		type: "POST",
		url: "/_extras/ajax.php",
		data: data,
		timeout: 5000,
		success: function(msg){
			data = msg.split('||');
			
			if(data[0]=='ok')
			$form.fadeOut('normal', function() {$form.after('<div class="response">'+data[1]+'</div>');});
			else
			$('#message').blink('Błąd podczas wysyłania. Spróbuj ponownie!')
		}
 });
}

$.fn.blink = function(text) {
	$blinked = $(this);
	if($blinked.attr('blocked')!='true')
	$blinked.attr('blocked', true).text(text).fadeIn().delay(1500).fadeOut('normal', function(){$blinked.attr('blocked', false)});
}

$('input, textarea').each(function() {
	if($(this).val()=='')
	$(this).val(inputs[$(this).attr('name')]);
});

$('input, textarea').focus(function() {
	if($(this).val()== inputs[$(this).attr('name')])
		$(this).val('');
});

$('input, textarea').blur(function() {
	if($(this).val()=='')
		$(this).val(inputs[$(this).attr('name')]);
});

$('form.validate').submit(function() {
	$form = $(this);
	error = false;
	text = 'Pole oznaczone na czerwono nie może być puste';
	counter = 0;

	$('.req:enabled', this).each(function() {
		$req = $(this);
		name = $req.attr('name');
		value = $req.val();


		
		if(value=='' || value==inputs[name])
		{
			counter++;
			if(counter>1)
			text = 'Pola oznaczone na czerwono nie mogą być puste';

			$req.addClass('error');
			$req.parent('div').addClass('error');
			error = true;
		}
		else
		{
			$req.removeClass('error');
			$req.parent('div').removeClass('error');
		}
	});

	if($('input[name=email]').length>0)
	{
		$req = $('input[name=email]', $form);
		value = $req.val();

		if(!checkemail(value))
		{
			
			if(error==false)
			text = 'Błędny adres e-mail';

			$req.addClass('error');
			$req.parent('div').addClass('error');
			error = true;
		}
	}

	if (jQuery.isFunction(jQuery.fn.checkFormExtras)) { 
		$form.checkFormExtras();
	}

	if(error==true)
	{
		$('#message').blink(text);
		return false;
	}

	if($form.hasClass('ajax'))
	{
		$form.sendAjax();
		return false;
	}
});