(function($) {
	
	function set_field(e, url, value, complete_fn) {
		// Postback our value
		$(e).parent().addClass("Loading");	
		var return_data = null;

		$.ajax(
			{    cache : false
			,     data : { value : value }
			, dataType : "json"
			,    error :
				function(XMLHttpRequest, textStatus, errorThrown) {
					$(e).parent().addClass("Error");
				}
			,  success :
				function(data, textStatus) {
					if (data[1] == '') {
						$(e).parent().removeClass("Error");
						// no error
						if (complete_fn != undefined)
							complete_fn(data[0]);
					} else {
						$(e).parent().addClass("Error");
						alert(data[1]);
					}
				}
			,  timeout : 15000
			,     type : "post"
			,      url : url
			, complete : 
				function() {
					$(e).parent().removeClass("Loading");
				}
			});
	}
	
	function get_field(e, url, complete_fn) {
		$(e).parent().addClass("Loading");

		$.ajax(
			{    cache : false
			, dataType : "json"
			,    error :
				function(XMLHttpRequest, textStatus, errorThrown) {
					$(e).parent().addClass("Error");
				}
			,  success :
				function(data, textStatus) {
					if (data[1] == '') {
						$(e).parent().removeClass("Error");
						// no error
						if (complete_fn != undefined)
							complete_fn(data[0]);
					} else {
						$(e).parent().addClass("Error");
						alert(data[1]);
					}
				}
			,  timeout : 15000
			,     type : "get"
			,      url : url
			, complete : 
				function() {
					$(e).parent().removeClass("Loading");
				}
			});
	}

	$.fn.ajax_field = function(options) {
		options = jQuery.extend(
			{ "confirm" : false
			, submitOnBlur : false
			}, options);
			
		$(this).each(function(i, e) {	
			$(e).initVal = '';

			$(e).find("input")
				.keyup(function(event) {
					if (!event.which)
						return;

					$(e).removeClass("Error");
					input = $(e).find("input");

					if (input.val() != $(e).initVal)
						$(e).addClass("Dirty");	
					else
						$(e).removeClass("Dirty");
						
					// return key
					if (event.which == 13 && input.val() != $(e).initVal) {
						set_field(input, options.set, input.val(), function(data) {
							$(e).find("input").val(data).end()
								.removeClass("Dirty");
							$(e).initVal = data;
						});
					}	
				})
				.focus(function() {
					// The field is focused - get the value before we allow updating
					input = $(e).find("input");
					$(e).initVal = input.val();
					get_field(input, options.get, function(data) {
						if (data == null) data = '';
						if ($(e).initVal != data)
							$(e).find("input").val(data);
						$(e).initVal = data;
					});
				})// keypress 
				.blur(function() {
					input = $(e).find("input");
					input.val($(e).initVal);
				}) // blur
		});
	};

	$.fn.ajax_select = function(options) {
		$(this).each(function(idx, e) {
			$(e).find("select")
				.change(function() {
					$(e).addClass("Dirty");
					s = $(e).find("select");
					set_field(s, options.set, s.find(":selected").val(), function(data) {
						$(e)
							.find("select").val(data).end()
							.removeClass("Dirty").removeClass("Loading");
					})
				})
				.focus(function() {
					// The field is focused - get the value before we allow updating
					input = $(e).find("select");
					$(e).initVal = input.val();
					get_field(input, options.get, function(data) {
						if (data == null) data = '';
						if ($(e).initVal != data)
							$(e).find("select").val(data);
						$(e).initVal = data;
					});
				})// keypress 
				.blur(function() {
					input = $(e).find("select");
					input.val($(e).initVal);
				}) // blur
		});
	};

})(jQuery);
