function initMapService(mapid, hiddenid, onsubmit) {
	var listboxid = mapid + "listbox";
	var infoboxid = mapid + "infobox";
	
	if (mapservice) {
		prepareCountyZipLists(regionArr, mapservice.mapCountyPostCodes);
		
		//end added
		mapservice.infoboxDisplayed = true;
		
		mapservice.displayInfoBoxSelect = function () {
			if (this.regionId != "0") {
				this.infoboxDisplayed = true;
			}
			if (this.regionId == "0" && this.municipalityId == "0" && !this.infoboxDisplayed) {
				var infobox = $('#'+infoboxid);
				
				if (infobox.length == 1) {
					infobox = infobox[0];
					infobox.style.display = "block";
					infobox.style.top = "5px";
					infobox.style.width = "170px";
					infobox.innerHTML = infobox.inforegion;
					this.infoboxDisplayed = true;
				}
			}
			else if (!this.infoboxPostalDisplayed) {
				this.hideInfoBox();
			}
			this.infoboxPostalDisplayed = false;
		};
		mapservice.displayInfoBoxZoom = function () {
			this.hideInfoBox();
			var infobox = $('#'+infoboxid);
			if (infobox.length == 1) {
				infobox = infobox[0];
				if (this.regionId != "0" && this.municipalityId != "0") {
					infobox.innerHTML = infobox.infopostnummer;
					infobox.style.display = "block";
					infobox.style.top = "25px";
					infobox.style.width = "138px";
					this.infoboxPostalDisplayed = true;
				}
			}
		}
		mapservice.hideInfoBox = function () {
			this.infoboxDisplayed = true;
			var infobox = $('#'+infoboxid);
			if (infobox.length == 1)
				infobox.hide();
		}
		
		mapservice.data = {
			Landsdele: {},
			Kommuner: {},
			PostNumre: {}
		};
		
		mapservice.hierarchyTree = {};
		
		mapservice.imagePrefix = '';
		
		mapservice.serialize = function () {
			var text = '';
			
			var keys = new Array('Landsdele', 'Kommuner', 'PostNumre');
			for (var i = 0; i < keys.length; i++) {
				var list = this.data[keys[i]];
				var text2 = '';
				for (var ID in list) {
					var datum = list[ID];
					text2 += (datum.ID + '|' + datum.Text + '[]');
				}
				if (text2.length > 2)
					text2 = text2.substring(0, text2.length - 2);
				text += (text2 + '[2]');
			}
			if (text.length > 3)
				text = text.substring(0, text.length - 3);
			
			
			text += '[1]';
			
			for (var key in this.hierarchyTree)
				text += key + '|' + this.hierarchyTree[key] + '[]';
			
			text = text.substring(0, text.length - 2);
			
			
			return text;
		};
		
		mapservice.deserialize = function (text) {
			
			var parts = text.split('[1]');
			
			if (parts.length != 2)
				return;
			
			this.regionId = 0;
			this.municipalityId = 0;
			this.postalId = 0;
			this.currentRegionId = 0;
			this.currentMunicipalityId = 0;
			this.currentPostalId = 0;
			
			text = parts[0];
			var a = text.split('[2]');
			var list;
			for (var ia = 0; ia < 3; ia++) {
				switch (ia) {
					case 0: list = this.data.Landsdele; break;
					case 1: list = this.data.Kommuner; break;
					case 2: list = this.data.PostNumre; break;
				}
				var b = a[ia].split('[]');
				for (var ib = 0; ib < b.length; ib++) {
					if (b[ib].length > 0) {
						var c = b[ib].split('|');
						list[c[0]] = { ID: c[0], Text: c[1] };
					}
				}
			}
			
			
			var l1 = parts[1].split('[]');
			
			for (var i in l1)
			{
				if (l1[i].length > 1)
				{
					var l2 = l1[i].split('|');
					this.hierarchyTree[l2[0]] = l2[1];
				}
			}
			
			this.setSelected();
			this.showMap();
			mapservice.infoboxDisplayed = false;
		};
		
		mapservice.setSelected = function () {
			var keys = new Array('Landsdele', 'Kommuner', 'PostNumre');
			var keys2 = new Array('regions', 'municipalities', 'postals');
			for (var i = 0; i < keys.length; i++) {
				var arr = this.selected[keys2[i]] = new Array();
				var list = this.data[keys[i]];
				for (var ID in list) {
					var datum = list[ID];
					arr[arr.length] = datum.ID;
				}
			}
			
			this.updateListBox();
		};
		
		mapservice.removeSelected = function (obj) {
			var listBox = $('#'+listboxid);
			if (listBox.children().length > 0) {
				var value = obj.className;
				switch (value.substring(0, 1)) {
					case "l":
						delete this.data.Landsdele[value.substring(1)];
						break;
					case "k":
						delete this.data.Kommuner[value.substring(1)];
						break;
					case "p":
						delete this.data.PostNumre[value.substring(1)];
						break;
				}
				$(obj, listBox).remove();
			}
			
			if (onsubmit) {
				onsubmit();
			}
			this.setSelected();
			this.showMap();
		};
		
		mapservice.removeAll = function () {
			this.data.Landsdele = {};
			this.data.Kommuner = {};
			this.data.PostNumre = {};
			if (onsubmit)
				onsubmit();
			this.setSelected();
			this.showMap();
		};
		
		mapservice.updateListBox = function () {
			var listBox = $('#'+listboxid);
			if (listBox.length == 1) {
				listBox = listBox[0];
				
				listBox.innerHTML = "";
				var selRegioner = '';
				for (var LandsdelID in this.data.Landsdele) {
					if (!mapservice.hasChildren(LandsdelID))
					{
						listBox.innerHTML += "<a class='" + "l" + this.data.Landsdele[LandsdelID].ID + "' href='javascript:void(0)' onclick='javascript:mapservice.removeSelected(this)' >" + this.data.Landsdele[LandsdelID].Text + " </a>";
						selRegioner += this.data.Landsdele[LandsdelID].ID + ","
					}
				}
				
				$('form:visible input[name="regionids"]').val(selRegioner.length > 0 ? selRegioner.substr(0, selRegioner.length - 1) : '');
				
				var selKommuner = '';
				for (var KommuneID in this.data.Kommuner) {
					if (!mapservice.hasChildren(KommuneID))
					{
						listBox.innerHTML += "<a class='" + "k" + this.data.Kommuner[KommuneID].ID + "' href='javascript:void(0)' onclick='javascript:mapservice.removeSelected(this)' >" + this.data.Kommuner[KommuneID].Text + " </a>";
						selKommuner += this.data.Kommuner[KommuneID].ID + ","
					}
				}
				
				$('form:visible input[name="kommuneids"]').val(selKommuner.length > 0 ? selKommuner.substr(0, selKommuner.length - 1) : '');
				
				var selPostnumre = '';
				for (var PostNummer in this.data.PostNumre) {
					if (!mapservice.hasChildren(PostNummer))
					{
						listBox.innerHTML += "<a class='" + "p" + this.data.PostNumre[PostNummer].ID + "'id='" + "p" + this.data.PostNumre[PostNummer].ID + "' href='javascript:void(0)' onclick='javascript:mapservice.removeSelected(this)' >" + this.data.PostNumre[PostNummer].Text + " </a>";
						selPostnumre += this.data.PostNumre[PostNummer].ID + ",";
					}
				}
				
				$('form:visible input[name="postnrids"]').val(selPostnumre.length > 0 ? selPostnumre.substr(0, selPostnumre.length - 1) : '');
			}
			
			
			var hidden = $('#'+hiddenid);
			
			if (hidden.length == 1)
				hidden.val(this.serialize());
		};
		
		var hidden = $('#'+hiddenid);
		
		if (hidden.length == 1) {
			mapservice.deserialize(hidden.val());
			var back = $('#mapback');
			if (mapservice.regionId != 0) {
				if (mapservice.municipalityId != 0) {
					back.html('Tilbage til Landsdelskort');
				}
				else {
					back.html('Tilbage til Danmarkskort');
				}
			}
			if (back.length == 1)
			{
				if (mapservice.regionId == 0)
					back.hide();
				else
					back.show();
			}
		}

		
		mapservice.callbackZoom = function (regionId, municipalityId, postalId, areaText) {
			this.hideInfoBox();
			var back = $('#mapback');
			if (regionId != 0) {
				if (municipalityId != 0) {
					back.html('Tilbage til Landsdelskort');
				}
				else {
					back.html('Tilbage til Danmarkskort');
				}
			}
			if (back.length == 1)
			{
				if (regionId == 0)
					back.hide();
				else
					back.show();
			}
			
			if (municipalityId > 0 && !(this.municipalityId > 0)) {
				this.data.Kommuner[municipalityId] = { ID: municipalityId, Text: areaText };
			}
			else if (regionId > 0 && !(this.region > 0)) {
				this.data.Landsdele[regionId] = { ID: regionId, Text: areaText };
			}
			else if (postalId > 0 && !(this.postalId > 0)) {
				this.data.PostNumre[postalId] = { ID: postalId, Text: areaText };
			}
			
			this.regionId = regionId;
			this.municipalityId = municipalityId;
			this.postalId = postalId;
			
			this.setSelected();
			if (onsubmit)
				onsubmit();
			
			this.showMap();
		};
		mapservice.goback = function () {
			this.hideInfoBox();
			this.back();
			var back = $('#mapback');
			if (back.length == 1)
			{
				if (this.regionId == 0)
					back.hide();
				else
					back.show();
			}
			if (mapservice.regionId != 0) {
				if (mapservice.municipalityId != 0) {
					back.html('Tilbage til Landsdelskort');
				}
				else {
					back.html('Tilbage til Danmarkskort');
				}
			}
		};
		mapservice.callbackSelect = function (regionId, municipalityId, postalId, areaText) {
			this.displayInfoBoxSelect();
			
			if (postalId > 0) {
				this.data.PostNumre[postalId] = { ID: postalId, Text: areaText };
			}
			else if (municipalityId > 0) {
				this.data.Kommuner[municipalityId] = { ID: municipalityId, Text: areaText };
			}
			else if (regionId > 0) {
				this.data.Landsdele[regionId] = { ID: regionId, Text: areaText };
			}
			
			if (onsubmit)
				onsubmit();
			
			this.setSelected();
		};
		
		mapservice.callbackDeselect = function (regionId, municipalityId, postalId) {
			this.displayInfoBoxSelect();
			
			if (regionId > 0) {
				delete this.data.Landsdele[regionId];
			}
			else if (municipalityId > 0) {
				delete this.data.Kommuner[municipalityId];
			}
			else if (postalId > 0) {
				delete this.data.PostNumre[postalId];
			}
			
			if (onsubmit)
				onsubmit();
			
			this.setSelected();
		};
		
	}
}
