	window.addEvent('domready', function() {
		
	        if ($('id_postal_area')) {
                $('id_postal_area').set('readonly', true);	
            }

			if ($('id_postal_code') != null) {
				
		 		$('id_postal_code').addEvent('keyup', function(event) {
     	
					areaCode.getData(event.target.value); 
		 			//alert($('id_postal_code').value);
			     });
			     
			     if ($('iid_postal_code').value != '') {
			     	areaCode.getData($('id_postal_code').value); 
                    //alert($('id_postal_code').value);
			     }
		     
			}
			

	});				


	var areaCode = {
		
		getData: function(areaCode) {
			
			// Do a request for postal area and countyname
			var myHTMLRequest = new Request.HTML({
				onComplete: function(responseTree, responseElements, responseText, responseJavaScript) {
								 		
					if (responseText != 'null') {
											
						var content = responseText.split('::');
											
						//$('area_id').set('value', content[0]);
						$('id_postal_area').set('value', content[2]);
						//$('county').set('value', content[3]);
											
						if (content[1] == 1804) {
							if ( confirm('Bestilling av kulturkort i Bodø kommune, må gjøres gjennom www.ungdomma.no. Ønsker du å gå dit nå?')) {
						        window.location="http://www.ungdomma.no/?a_id=361";
                            }
                        }

                        if (content[1].substr(0, 2) != 18) {
                            alert('Dette postnummeret er utenfor Nordland');
                        }
											
					} else {
						if (areaCode.length == 4) {
							$('id_postal_area').set('value', 'Ukjent postnummer');
							//$('county').set('value', '');
						} else {
							$('id_postal_area').set('value', '');
							//$('county').set('value', '');
						}
					}
								 		
				}

			}).get('/media/sys/areacode.php?ac=' + areaCode);
							 
		}
		
	};
