(function($) {
	$.fn.formPost = function(input, resolve) {
		return this.each(function()	{
			var self = this;
			var $this = $(this);
			
			if (resolve) {
				var i = $('input[type="text"]', this);
				
				$.each(i, function() {
					var t = $(this).attr('title');
					this.value = t;
					
					$(this).focus(function() {
					if (this.value == t){ 
						this.value = '';
					}
					});
					
					$(this).blur(function() {
						if ($.trim(this.value) == '') {
							this.value = t;
						}
					});
				});
			}
			
			var validateForm = function() {		
				var e = true;
				
				var validate_email = function(email) {
					var at = email.lastIndexOf("@");
					if (at < 1 || (at + 1) === email.length) return false;
					if (/(\.{2,})/.test(email)) return false;
					var local = email.substring(0, at);
					var domain = email.substring(at + 1);
					if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255) return false;
					if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain)) return false;
					if (!/^"(.+)"$/.test(local)) {
						if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local)) return false;
					}
					if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1) return false;	
					return true;
				}
				
				if (input != null) {
					var node = null;
					
					$.each(input, function(t, l) {
						switch (t) {
							case 'text':
								$.each(l, function() {
									node = $('[name="'+this+'"]', self);
									if (!node.val() || node.val().length < 3 || node.val() == node.attr('title')) {
										node.addClass('incorrect');
										e = "Popraw pola";
									}
									else {
										if (node.hasClass('incorrect')) {
											node.removeClass('incorrect');
										}
									}
								});
							break;
							case 'email':	
								if (e == true) {
									$.each(l, function() {
										node = $('[name="'+this+'"]', self);
										if (!validate_email(node.val())) {
											node.addClass('incorrect');
											e = "Popraw e-mail";
										}
										else {
											if (node.hasClass('incorrect')) {
												node.removeClass('incorrect');
											}
										}
									});
								}
							break;
							case 'repeat':
								if (e == true) {
									$.each(l, function() {
										node = $('[name=repeat_'+this+']', self);
										var node_pattern = $('[name='+this+']', self);

										if (node.val() == node_pattern.val()) {
											if (node.hasClass('incorrect')) {
												node.removeClass('incorrect');
											}
										}
										else {
											node.addClass('incorrect');
											e = "Popraw e-mail";
										}
									});
								}
							break;
						}
					});
				}

				return e;
			}
			
			$this.submit(function(e) {
				var send_status = true;
				var url = $(this).attr('action');
				var loader = $('<img src="images/preloader_send.gif" alt="" />');
				$infobox = $('.status', self);
				
				if (send_status) {
					$infobox.hide();
				}
				
				var validate = validateForm();
				
				if (validate === true && send_status == true) {
					$.ajax({
						type: "POST",
						url: url+'?type=json',
						data: $this.serialize(),
						dataType: "json",
						timeout : 5000,
						beforeSend: function() {
							$infobox.empty().append(loader).fadeIn(500);
						},
						success: function(response, status) {
							if (status == 'success') {
								if (response.error == 0) {									
									$infobox.empty().text(response.msg).fadeIn(500);
									send_status = false;
								}
								else {
									$infobox.empty().text(response.msg).fadeIn(500);	
									send_status = true;
								}
							}	
							
							$('.button', self).attr('disabled','disabled');
							$(':input', self).attr('disabled','disabled');							
						},
						error: function(response, status, e) {
							$infobox.empty().text('Wystąpił błąd').fadeIn(500);	
							send_status = true;
						}
					});
				}
				else {
					if (send_status) $infobox.empty().text(validate).fadeIn(500);
				}

				return false;
			});
		});
	}
})(jQuery);
