$(document).ready(function () {
	$("#contact-form input[type=text]").each(function () {
		$(this).val($(this).attr('rel'));
		$(this).focus(function () {
			if ($(this).val() == $(this).attr('rel')) {
				$(this).val('');
			}
		}).focusout(function () {
			if ($(this).val() == '') {
				$(this).val($(this).attr('rel'));
			}
		});
	});

	$("#contact-form").submit(function () {
		var error = false;
		var filterEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		$(this).find("input.required, textarea.required").each(function () {
			if ($(this).val() == '' || $(this).val() == $(this).attr('rel')) {
				$(this).addClass('error');
				error = true;
			} else {
				$(this).removeClass('error');
			}
		});

		$(this).find("input.email").each(function () {
			if (!filterEmail.test($(this).val())) {
				$(this).addClass('error');
				error = true;
			} else {
				$(this).removeClass('error');
			}
		});

		if (!error) {
			$(this).find(".send-info").hide();
			$(this).find(".send-wait").css("display", "inline-block");

			var response = $("#contact-form").sendForm();

			if (response.status == 'OK') {
				$(this).find(".send-info").hide();
				$(this).find(".send-ok").css("display", "inline-block");
				$(this).find("input[type=text], textarea").each(function () {
					$(this).val($(this).attr('rel'));
				});
			} else {
				$(this).find(".send-info").hide();
				$(this).find(".send-error").css("display", "inline-block");
			}

			setTimeout(function () {
				$(this).find(".send-info").fadeOut(500);
			}, 3000);
		}

		return false;
	});
});
(function($){
	$.fn.sendForm = function(options) {
		var settings = $.extend({
			url: $(this).attr('action')
		}, options);

		var url = settings.url;
		var formData = {};
		var returnJson = [];

		$(this).find(':input').not(':submit').each(function() {
		    if ( $(this).attr('name') != undefined ) {
		    	if ( $(this).attr('type') == 'checkbox' ) {
		    		if ( $(this).is(':checked') ) {
		    			formData[$(this).attr('name')] = $(this).val();
		    		}
		    		else {
		    			formData[$(this).attr('name')] = 0;
		    		}
		    	}
		    	else {
		    		formData[$(this).attr('name')] = $(this).val();
		    	}
			}
		});

		$.ajax({
			type: "post",
			url: url,
			cache: false,
			async: false,
			dataType: 'json',
			data: formData,
			beforeSend: function(){},
			success: function(oJsonObject) {
				returnJson = oJsonObject;
			}
		});

		return returnJson;
	};
})(jQuery);
