( function($) {
	$.fn.wafZonier = function(url, value) {
		var cp = this.eq(0);
		var ville = this.eq(1);
		var msg = this.eq(2);
		ville.hide();
		msg.hide();
		var onChangeCp = function() {
			if (cp.data("val") != cp.val())
			{
				cp.data("val", cp.val());
				if (cp.val().length == 5)
				{
					$.getJSON(url, {
						cp :cp.val()
					}, function(r, s) {
						if (r.length == 0) {
							ville.hide();
							ville.removeOption(/.*/);
							msg.show();
						} else {
							msg.hide();
							ville.removeOption(/.*/);
							for (i = 0; i < r.length; i ++) {
								ville.addOption(r[i], r[i]);
							}
							ville.find("option:first").attr("selected", "selected");
							if (value != undefined && value != null) {
								ville.selectOptions(value, true);
							}
							ville.show();
							ville.change();
						}
					});
				}
			}
		};
		cp.keyup(onChangeCp).change(onChangeCp);
		onChangeCp();

	};
})(jQuery);
