zlg = {};
zlg.initialize = function() {
	var node = jQuery(".form_subscribtion");
	if (node) {
		var prefixes = ["", "recipient_", "legal_", "different_"];
		for (var i = 0; i < prefixes.length; i++) {
			new zlg.formplace({container : node, prefix : prefixes[i]});
		}
	}
}

zlg.formplace = function(options) {
	
	this.options = options;
	this.container = jQuery(this.options.container);
	this.nodeCity = this.container.find("select[name=" + this.getFieldName("city_id") + "]");	
	this.nodePlace = this.container.find("select[name=" + this.getFieldName("place_id") + "]");
	this.nodeStreet = this.container.find("input[name=" + this.getFieldName("address") + "]");
	this.nodeHouse = this.container.find("input[name=" + this.getFieldName("house") + "]");
	this.nodePostCode = this.container.find("input[name=" + this.getFieldName("post_code") + "]");
	this.nodeCity.change(jQuery.proxy(this.reset, this));
	this.nodePlace.change(jQuery.proxy(this.reset, this));
	this.nodeStreet.keyup(jQuery.proxy(this.reset, this));
	this.nodeHouse.keyup(jQuery.proxy(this.reset, this));
	this.uri = this.container.attr("uri_places");
	this.reset();
	
}

zlg.formplace.prototype.getFieldPrefix = function(name) {
	var prefix = (this.options.prefix ? this.options.prefix : "");
	if (prefix === "legal_" && name === "address") {
		prefix = "reg_";
	}
	return prefix;
}

zlg.formplace.prototype.getFieldName = function(name) {
	return this.getFieldPrefix(name) + name;
}

zlg.formplace.prototype.getFieldNameWithoutPrefix = function(name) {
	var prefix = this.getFieldPrefix("");
	return name.split(prefix).join("").split("_reg").join("");
}

zlg.formplace.prototype.reset = function() {
	var enableStreet = this.nodeCity.val() > 0 || this.nodePlace.val() > 0;
	this.setAutocompleteEnabled(this.nodeStreet, enableStreet, 2);
	this.setAutocompleteEnabled(this.nodeHouse, this.nodeStreet.val().length > 0 && enableStreet);
	this.updatePostCode();
}

zlg.formplace.prototype.setAutocompleteEnabled = function(node, enable, minLength) {
	var options = "destroy";
	if (enable) {
		var listener = {node : node, parent : this};
		listener.updateAutocomplete = function(request, response) {
			this.response = response;
			jQuery.get(
				this.parent.uri + this.parent.getFieldNameWithoutPrefix(node.attr("name")) +
					",city." + this.parent.nodeCity.val() + ",place." + this.parent.nodePlace.get(0).value,
				{term : request.term, street : this.parent.nodeStreet.val()},
				jQuery.proxy(this.updatedAutocomplete, this)
			);
		}
		listener.updatedAutocomplete = function(data) {
			this.response(eval(data));
		}
		options = {
			source: jQuery.proxy(listener.updateAutocomplete, listener), 
			minLength: minLength ? minLength : 1,
			change : jQuery.proxy(this.reset, this),
			select : jQuery.proxy(this.reset, this),
			close : jQuery.proxy(this.reset, this)
		};
	}
	node.autocomplete(options);
}

zlg.formplace.prototype.updatePostCode = function() {
	if (this.nodeCity.val() > 0 || this.nodePlace.val() > 0) {
		jQuery.get(
			this.uri + this.getFieldNameWithoutPrefix(this.nodePostCode.attr("name")) +
				",city." + this.nodeCity.val() + ",place." + this.nodePlace.val(),
			{
				term : this.nodePostCode.val(), house : this.nodeHouse.val(), 
				street : this.nodeStreet.val()
			},
			jQuery.proxy(this.updatedPostCode, this)
		);
	}
}

zlg.formplace.prototype.updatedPostCode = function(response) {
	if (response) {
		this.nodePostCode.val(response);
	}
}

function setSubscriptionLineVisible(name, show) {
	var el = $("__row_" + name + "__");
	if (!el) {
		return false;
	}
	el.style.display = $("__row_" + name + "_hr__").style.display = show ? "" : "none";
}

function getLocationField(name, prefix) {
	if (prefix === undefined) {
		if ($("legal").checked) {
			prefix = "legal_";
		} else if ($("notme").checked) {
			prefix = "recipient_";
		}
	}
	return $(prefix + name);
}

function onChangeCity(prefix, only_label) {
	var el = getLocationField("city_id", prefix);
	var elLabelBottom = getLocationField("_label_b__", prefix);
	var elLabel = getLocationField("_label__", prefix);
	elLabel.style.display = elLabelBottom.style.display = el.value ? "" : "none";
	if (only_label) {
		return false;
	}
	if (el.value) {
		var oldValue = el.value;
		var elRegion = getLocationField("region_id", prefix);
		elRegion.value = "";
		elRegion.onchange();
		el.value = oldValue;
	}
}

function onChangeRegion(uri, prefix) {
	listener = {elParent : getLocationField("region_id", prefix)};
	listener.url = uri + listener.elParent.value;
	listener.prefix = prefix;
	listener.onComplete = function(response) {
		var rowset = eval(response);
		var l = rowset.length;
		var el = getLocationField("place_id", this.prefix);
		el.options.length = 2;
		for (var i = 0; i < l; i++) {
			var row = rowset[i];			
			el.options[i + 2] = new Option(row[1], row[0], row[2] == 1, row[2] == 1);
		}
		el = $(el);
		if (el && el.hasAttribute && el.hasAttribute("default_value")) {
			el.value = el.getAttribute("default_value");
		}
		onChangePlace(undefined, this.prefix);
	}
	setSubscriptionLineVisible("place_" + prefix, listener.elParent.value);
	if (listener.elParent.value) {
		var el = getLocationField("city_id", prefix);
		el.value = "";
		onChangeCity(prefix, true);
	}
	listener.onComplete = listener.onComplete.bind(listener);
	if (listener.elParent.value) {
		(new Request(listener)).send();
	} else {
		listener.onComplete("[]");
	}
}

function onChangePlace(value, prefix) {
	var el = getLocationField("place_id", prefix);
	if (!el) {
		return false;
	}
	if (value) {
		getLocationField("region_id", prefix).onchange();
		el.setAttribute("default_value", value);
	} else {
		el = $(el);
		if (el && el.hasAttribute && el.hasAttribute("default_value")) {
			el.removeAttribute("default_value");
		}
		setSubscriptionLineVisible("custom_" + el.id, el.value == -1);
	}
}
jQuery(document).ready(zlg.initialize);
