/**
 * Extends field "#ln_form_plz", "#ln_form_ort", "#ln_form_bundesland" in forms.
 * To make use of it:
 * - create a form with fields #ln_form_plz and #ln_form_ort/#ln_form_bundesland (both optional)
 * - include this file
 *
 * This makes sure that jQuery is loaded (lazily) and then attaches events to the
 * fields, so that changing the PLZ pre-fills any city/state fields.
 */


var ln_orig_load = window.onload;
window.onload = function(e) {
	/* State properties */
	var ln_form = ({
		bundlesland_changed: false,
		ort_changed: false,
		ort_old_val: '',
		ort_has_focus: false
	});

	var callback_host = "www.lexisnexis.de";

	// Load jQuery, if not yet available.
	if( typeof jQuery == "undefined" )
	{
		var head = document.getElementsByTagName("head")[0] || document.documentElement,
			script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "//" + callback_host + "/js/jquery.min.js";
		head.insertBefore( script, head.firstChild );
	}

	// Callback timer, to check when jQuery has been loaded and attach handlers.
	var callbackTimer = setInterval(function() {
		if( typeof jQuery !== "undefined" ) {
			clearInterval(callbackTimer);

			if( jQuery('#ln_form_plz').length == 0
				|| ( jQuery('#ln_form_ort').length + jQuery('#ln_form_bundesland').length == 0 ) )
			{ // no need to do anything
				return;
			}

			ln_form.ort_old_val = jQuery("#ln_form_ort").val();

			jQuery("#ln_form_plz").change( function() {
				if( ! ( ln_form.bundesland_changed && ln_form.ort_changed ) ) {
					jQuery.getJSON("//" + callback_host + "/js/callback.php?callback=?",
						{ get: "plz2blort", "plz": this.value },
						/* Callback: */
						function(data) {
							if( ! ln_form.bundesland_changed && data.bundesland) {
								jQuery("#ln_form_bundesland").val(data.bundesland);
							}

							if( ! ln_form.ort_changed && data.ort) {
								jQuery("#ln_form_ort").val(data.ort);
								ln_form.ort_old_val = data.ort;

								// if the field has focus (i.e. from tabbing just into it, select the new value, not stepping into the users typing)
								if( ln_form.ort_has_focus ) {
									jQuery("#ln_form_ort").select();
								}
							}
						} );
				}
			} );

			// Remember when the "Bundesland" field got changed.
			jQuery("#ln_form_bundesland").change( function() { ln_form.bundesland_changed = true; } );

			// Bind events on "Ort". We use "keydown" rather than "change", since already starting
			// to type into the field should prevent it from being overwritten.
			jQuery("#ln_form_ort").keydown( function() {
					if( ln_form.ort_old_val != this.value ) {
						ln_form.ort_old_val = this.value;
						ln_form.ort_changed = true;
					}
				})
				// Observe focus state of the field, used for automatically selecting the auto-value.
				.focus( function () { ln_form.ort_has_focus = true; } )
				.blur( function () { ln_form.ort_has_focus = false; } );
		}
	}, 100);

	// Call the orig onload event.
	if( typeof(ln_orig_load) === "function" )
		ln_orig_load(e);

};
