$.fn.validate = function(options) {
	
	

	$('select', this).live('change', function() {
		if($(this).val()=='')
		$(this).parent().find('.input_error_container').show();
		else
		$(this).parent().find('.input_error_container').hide();
	});

	$('input, textarea, select', options.container).not('.btn, [type=submit]').each(function() {		
		if(this.value=='' && inputs[this.name])
		this.value = inputs[this.name];
	})
		
	$('input, textarea, select', options.container).not('.btn, [type=submit]').blur(function() {		
		if(this.value=='' && inputs[this.name])
		this.value = inputs[this.name];
	})

	$('input, textarea, select', options.container).not('.btn, [type=submit]').focus(function() {		
		if(inputs[this.name] && this.value==inputs[this.name])
		this.value = '';
	})


	$(this).live('submit', function() {
		var $form = $(this);

		form_list = new Array;
		$('input, textarea, select', $form).not('.btn, [type=submit]').each(function() {
			if(inputs[this.name]==this.value) this.value='';
			form_list.push(this.name+'='+this.value);
		})
		form_list = form_list.join('&');

		action = router.module+'/'+$(this).attr('title');
		$('input.btn', $form).after('<div class="loader"><img src="media/core/loading.gif"></div>');
		$('input.btn', $form).hide();



		
		$.ajax({
			type: "POST",
			data: form_list,
			//dataType: 'json',
			url: 'ajax/send'+$form.attr('action'), 
			success: function(data) {
				
				$(options.container).html(data);

				$('input, textarea, select', options.container).not('.btn, [type=submit]').each(function() {		
					if(this.value=='' && inputs[this.name])
					this.value = inputs[this.name];
				})	
					
			
				$('input, textarea, select', options.container).not('.btn, [type=submit]').blur(function() {		
					if(this.value=='' && inputs[this.name])
					this.value = inputs[this.name];
				})

				$('input, textarea, select', options.container).not('.btn, [type=submit]').focus(function() {		
					if(inputs[this.name] && this.value==inputs[this.name])
					this.value = '';
				})
							
				/*for (element in data)
				{
					var name = data[element].name;
					var $input = $('input[name='+name+']', $form);
					$input.addClass('error').before('<div class="error">'+data[element]['errors'][0].text+'</div>');
					for(error in data[element]['errors'])
					{
						var text = data[element]['errors'][error].text;
						$('#errors').append('<b>Pole '+name+': - '+text+'</b><br/>');
					}
				}*/
				

				//error = data[0][0].error;
				//input = data[0][0].field;

				//$('input[name='+input+']', $form).before('<div class="error">'+error+'</div>');
				//$('.error', $form).fadeOut();
			}
		})
		return false;
	});
};
