(function($) {
	$.fn.asyncForm = function() {
		// prepare input fields
		this.each(function () {
			var $this = $(this);
			
			$this.find(":text").each(function (index, el) {
				$(el).addClass("idleField");
				$(el).focus(function() {
					$(this).removeClass("idleField").addClass("focusField");
		
					if (this.value == $(this).attr("alt")) {
						this.value = '';
					}
					if (this.value != $(this).attr("alt")) {
						$(this).select();
					}
				});
				$(el).blur(function() {
					$(this).removeClass("focusField").addClass("idleField");
					if ($.trim(this.value) == '') {
						this.value = $(this).attr("alt");
					}
				});
		
				$(el).val($(el).attr('alt'));
			});
		
			$this.submit(function (event) {
				if (event.preventDefault) 
				{
					event.preventDefault(); 
				} 
				else 
				{
					event.returnValue = false; 
				}
				
				var postargs = {"ts" : event.timeStamp};
				
				$(this).find(":text").each(function (index, el) {
					var value = jQuery.trim($(el).val());
					
					if ($(el).attr('alt') && value == $(el).attr('alt')) {
						value = "";
					}
					
					postargs[$(el).attr("name")] = value;
				});

				$(this).find(".form-submit-messages").first().hide();
				
				$(this).find(":submit").first().hide();
				$(this).find(".form-submit-status").first().spin("tiny").show();

				var action = $(this).attr("action");
				$thisform = $(this);
				
				$.post(action, postargs, function(data) {
					$thisform.find(".form-submit-status").first().spin(false).hide();
					$thisform.find(":submit").first().show();
					$thisform.find(".form-submit-messages").first().html(data).fadeIn();
				});
				
				return false;
			});
		});
	};
})(jQuery);
