
function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str))
}

$.fn.sendAjax = function() {
	
	$('.btn', $form).hide();

	$form = $(this);
	build = new Array;
	build.push('type='+router.module);
	$('input, select, textarea', this).not('.btn').each(function() {
		if(inputs[this.name]==this.value) this.value='';
		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_form', $form).blink('Błąd podczas wysyłania. Spróbuj ponownie!')
				$('.btn', $form).fadeIn();

			}
		}
 });
}

$.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)});
}

$(document).ready(function() {

	$('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')]);
	});

	function elementError(req, error)
	{
		$req = $(req)
		if(error==false)
		{
			$req.removeClass('error');
			$req.parent('div,td').removeClass('error');


			if(req.type=='select-one' && typeof(select_color)!='undefined')
			$req.css('backgroundColor', select_color);
		}
		else
		{
			$req.addClass('error');
			$req.parent('div,td').addClass('error');
			
			if(req.type=='select-one' && typeof(select_color_error)!='undefined')
			$req.css('backgroundColor', select_color_error);
		}
	
	}

	if(typeof(select_color)!='undefined')
	$('select').css('backgroundColor', select_color);


	if($('form.validate').length>0)
	{

		$('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';
					error = true;
					elementError(this, true);
				}
				else
				elementError(this, false);
			});

			if($('input[name=email]', $form).length>0)
			{
				$req = $('input[name=email]', $form);
				value = $req.val();

				if(value!==inputs['email'] && !checkemail(value))
				{
					if(error==false)
					text = 'Błędny adres e-mail';

					elementError($req, true);
					error = true;
				}
				
			}

			$form.myerror = error;
			if (jQuery.isFunction(jQuery.fn.checkFormExtras)) { 
				$form.checkFormExtras();
			}

			if(error==true)
			{
				$('.message_form', $form).blink(text);
				return false;
			}

			if($form.hasClass('ajax'))
			{
				$form.sendAjax();
				return false;
			}

			if($form.hasClass('nosend'))
			return false;
		});
	}
});
