/*
* jQuery adresLookup
* @copyright Leon Mondria http://www.netivity.nl
* @version 1.2.8
* @minified using http://fmarcia.info/jsmin/test.html
*/

(function ($) {
	$.fn.extend({ adreslookup: function (o) {
		var options = $.extend({ handler: '/includes/AJAX/request-handler.asp', data: { action_var: 'strActie', action_value: 'GetAdresgegevensJSON', postcode_value_var: 'strPostcode', huisnummer_value_var: 'strHuisnummer' }, nederland: 'NL', loadingText: 'bezig met zoeken...', loadingTextTimeout: 200, errorlabels: { straat: 'Uw adres is niet gevonden.', woonplaats: '' }, errorplaceholders: null, editable: false, inputs: { land: '.land', postcode: '.postcode', huisnummer: '.huisnummer', toevoeging: '.toevoeging', straat: '.straat', plaats: '.plaats' }, placeholders: null, lookupOnload: false, complete: null, success: null, error: null }, o); return this.each(function () {
			var self = $(this); var land_val, postcode_val, huisnummer_val, toevoeging_val; var straat_placeholder_el, plaats_placeholder_el, placeholders_arr, land_el; var postcode_el, postcode_letters_el; if (typeof (options.inputs.postcode) === "object") { postcode_el = self.find(options.inputs.postcode[0]); postcode_letters_el = self.find(options.inputs.postcode[1]); }
			else { postcode_el = self.find(options.inputs.postcode); }
			var huisnummer_el = self.find(options.inputs.huisnummer); var toevoeging_el = self.find(options.inputs.toevoeging); var straat_input_el = self.find(options.inputs.straat); var plaats_input_el = self.find(options.inputs.plaats); var straat_en_plaats = self.find(options.inputs.straat + "," + options.inputs.plaats); var targets; if (typeof (options.inputs.postcode) !== "object") { targets = self.find(options.inputs.postcode + "," + options.inputs.huisnummer + "," + options.inputs.toevoeging); }
			else { targets = self.find(options.inputs.postcode[0] + "," + options.inputs.postcode[1] + "," + options.inputs.huisnummer + "," + options.inputs.toevoeging); }
			var placeholders = false; var search = true; postcode_val = $.trim(postcode_el.val()); if (typeof (options.inputs.postcode) === "object") { postcode_val += $.trim(postcode_letters_el.val()); }
			huisnummer_val = $.trim(huisnummer_el.val()); toevoeging_val = $.trim(toevoeging_el.val()); postcode_el.data('prev_postcode', postcode_val).data('role', 'postcode'); huisnummer_el.data('prev_huisnummer', huisnummer_val).data('role', 'huisnummer'); toevoeging_el.data('prev_toevoeging', toevoeging_val).data('role', 'toevoeging'); if (options.inputs.land != null) { land_el = self.find(options.inputs.land); land_val = land_el.val(); land_el.change(function () { land_change(this.value); }); land_change(land_val); }
			if (!options.editable) {
				if (options.placeholders != null) { placeholders = true; straat_placeholder_el = self.find(options.placeholders.straat); plaats_placeholder_el = self.find(options.placeholders.plaats); placeholders_arr = self.find(options.placeholders.straat + "," + options.placeholders.plaats); }
				else { straat_placeholder_el = $('<span id="straat_placeholder"></span>').insertAfter(straat_input_el); plaats_placeholder_el = $('<span id="plaats_placeholder"></span>').insertAfter(plaats_input_el); placeholders_arr = self.find("#straat_placeholder,#plaats_placeholder"); }
				if (straat_input_el.val() != '')
					straat_placeholder_el.text(straat_input_el.val()); if (plaats_input_el.val() != '')
					plaats_placeholder_el.text(plaats_input_el.val());
			}
			if (!options.editable || (options.inputs.land != null && land_val != options.nederland)) { straat_input_el.hide(); plaats_input_el.hide(); if (placeholders) { placeholders_arr.show(); } }
			targets.bind('keyup change', lookup); self.bind('adreslookup.lookup', lookup); if (options.lookupOnload) { lookup(null, false); }
			function lookup(e, validate_previous) {
				var input = $(this); if (!search)
					return; postcode_val = $.trim(postcode_el.val()); if (typeof (options.inputs.postcode) === "object") { postcode_val += $.trim(postcode_letters_el.val()); }
				huisnummer_val = $.trim(huisnummer_el.val()); toevoeging_val = $.trim(toevoeging_el.val()); if (input.data('role') == 'postcode') {
					this.value = this.value.toUpperCase(); if (this.value.indexOf(" ") > 0)
						this.value = this.value.replace(" ", ""); if (!self.validPostcode(this.value))
						return; if (this.value.length < 6)
						return;
				}
				var validatePrevious = true; if (validate_previous !== 'undefined')
					validatePrevious = validate_previous; if (!validatePrevious || (validateprevious && (postcode_val != postcode_el.data('prev_postcode') || huisnummer_val != huisnummer_el.data('prev_huisnummer')))) {
					if (postcode_val != "" && huisnummer_val != "") {
						var ajax_data = options.data.action_var + "=" + options.data.action_value + "&" + options.data.postcode_value_var + "=" + escape(postcode_val.replace(' ', '')) + "&" + options.data.huisnummer_value_var + "=" + escape(huisnummer_val); var doShowLoadingText = true; setTimeout(function () {
							if (doShowLoadingText) {
								straat_en_plaats.attr("readonly", true); straat_en_plaats.addClass("readonly"); if (!options.editable)
									placeholders_arr.text(options.loadingText); else
									straat_en_plaats.val(options.loadingText);
							} 
						}, options.loadingTextTimeout); $.ajax({ url: options.handler, data: ajax_data, dataType: 'json', error: function (xhr) {
							doShowLoadingText = false; if (xhr.statusText == 'Not Found')
								alert("Handler niet gevonden: " + options.handler); if (options.error == null || (options.error != null && options.error.call() !== false)) {
								straat_input_el.val(""); plaats_input_el.val(""); if (options.editable) { straat_en_plaats.attr("readonly", false); straat_en_plaats.removeClass("readonly"); }
								else { straat_placeholder_el.text(""); plaats_placeholder_el.text(""); }
								display_error();
							}
							if (options.complete != null && typeof (options.complete) === 'function') { options.complete.call(); } 
						}, success: function (result, status) {
							doShowLoadingText = false; if (result == null) {
								display_error(); if (options.complete != null && typeof (options.complete) === 'function') { options.complete.call(); }
								return;
							}
							if (typeof (result) !== 'object') {
								display_error(); if (options.complete != null && typeof (options.complete) === 'function') { options.complete.call(); }
								return;
							}
							if (options.success == null || (options.success != null && options.success.call(result) !== false)) {
								straat_input_el.val(result.Straatnaam); plaats_input_el.val(result.Woonplaats); straat_en_plaats.attr("readonly", false).removeClass("readonly"); if (!options.editable) { straat_placeholder_el.text(result.Straatnaam).show(); plaats_placeholder_el.text(result.Woonplaats).show(); }
								if (result.Straatnaam == '' && result.Woonplaats == '') {
									if (!options.editable) { placeholders_arr.text(""); }
									display_error();
								}
								else { remove_error(); } 
							}
							if (options.complete != null && typeof (options.complete) === 'function') { options.complete.call(); } 
						} 
						});
					} 
				}
				postcode_el.data('prev_postcode', postcode_val); huisnummer_el.data('prev_huisnummer', huisnummer_val);
			}
			function land_change(val) {
				if (val.toString().toUpperCase() != options.nederland.toString().toUpperCase()) { search = false; straat_input_el.show(); plaats_input_el.show(); placeholders_arr.hide(); postcode_el.val(""); huisnummer_el.val(""); toevoeging_el.val(""); straat_input_el.val(""); plaats_input_el.val(""); }
				else { search = true; if (!options.editable) { straat_input_el.hide(); plaats_input_el.hide(); if (placeholders) { placeholders_arr.show(); } } } 
			}
			function display_error() {
				var err1 = $("<span class='invalid'>" + options.errorlabels.straat + "</span>"); var err2 = $("<span class='invalid'>" + options.errorlabels.woonplaats + "</span>"); if (options.errorplaceholders) {
					if (options.errorlabels.straat != '') { self.find(options.errorplaceholders.straat).html(err1); }
					if (options.errorlabels.woonplaats != '') { self.find(options.errorplaceholders.woonplaats).html(err2); } 
				}
				else {
					if (options.editable) { if (!straat_input_el.parent().find(".invalid").length) { err1.insertAfter(straat_input_el); err2.insertAfter(plaats_input_el); } }
					else { if (!straat_placeholder_el.parent().find(".invalid").length) { err1.insertAfter(straat_placeholder_el); err2.insertAfter(plaats_placeholder_el); } } 
				}
				straat_input_el.val(""); plaats_input_el.val(""); if (options.editable) { straat_en_plaats.attr("readonly", false); straat_en_plaats.removeClass("readonly"); }
				if (placeholders || !options.editable)
					placeholders_arr.hide();
			}
			function remove_error() {
				if (options.errorplaceholders) { self.find(options.errorplaceholders.straat).html(""); self.find(options.errorplaceholders.woonplaats).html(""); }
				if (options.editable) { if (!straat_input_el.closest(".invalid").length) { straat_input_el.next(".invalid").remove(); plaats_input_el.next(".invalid").remove(); } }
				else { if (!straat_placeholder_el.closest(".invalid").length) { straat_placeholder_el.next(".invalid").remove(); plaats_placeholder_el.next(".invalid").remove(); } } 
			} 
		});
	}, debug: function ($obj) {
		if (window.console && window.console.log)
			window.console.log('hilight selection count: ' + $obj.size());
	}, validPostcode: function (val) {
		if (val == '') { return false; }
		val = $.trim(val); if (val.length != 6) { return false; }
		if ((isNaN(val.charAt(0))) || (isNaN(val.charAt(1))) || (isNaN(val.charAt(2))) || (isNaN(val.charAt(3)))) { return false; }
		function isLetter(c) { return (c >= 'A' && c <= 'Z'); }
		if ((!isLetter(val.charAt(4))) || (!isLetter(val.charAt(5)))) { return false; }
		return true;
	} 
	});
})(jQuery);
