	function buildHTML(n,a){
		if(n!="text"){obj = document.createElement(n);for(var i in a)eval("obj."+i+"='"+a[i]+"'")}
		else obj = document.createTextNode(a);
		return obj;
	}
	function setClass(e, eClass) {
		e.setAttribute("class", eClass);
		e.setAttribute("className", eClass);
		return;
	} 
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
		var fromOK = false;
		var toOK = false;

    function load() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map"));
				map.addControl(new GSmallMapControl());
        //map.addControl(new GOverviewMapControl());
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        checkDirections(lang);
      }
    }
    
    function checkDirections(locale) {
			fromAddress = document.getElementById("from").value;
			toAddress = document.getElementById("to").value;
			var loc = new GClientGeocoder;
			loc.setBaseCountryCode("NO");
			
			if(fromOK == false) {
				phoneHome("geocode");
				loc.getLocations(fromAddress,  listFromLocations);
			}
			if(toOK == false && fromOK == true) {
				phoneHome("geocode");
				loc.getLocations(toAddress,  listToLocations);
			}
			
      if(fromOK && toOK) {
				fromOK = false;
				toOK = false;
				phoneHome("direction");
				setDirections(fromAddress, toAddress, locale);
			}
    }
		function setDirections(from, to, locale) {
			gdir.load("from: " + from + " to: " + to, { "locale": locale });
		}
		
		function listFromLocations(response) {
			var status = response.Status.code;
			if(status==200) {
				var places = new Array();
				var choicesFrom = document.getElementById("choicesFrom");
				var txtFrom = document.getElementById("from");
				if (response.Placemark.length > 1) { 
					for(var i=0; i<response.Placemark.length; i++) {
						var place = response.Placemark[i];
						var gAddress, gCounty, gCountry;
						gAddress = place.address;
						
						if(!!place.AddressDetails.Country.AdministrativeArea) {
							if(!!place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea) {
								gCounty = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
								var lastComma = gAddress.lastIndexOf(',');
								var result = gAddress.substring(0,lastComma) + ", " + gCounty + gAddress.substring(lastComma, gAddress.length);
							}
						}
						else{result=gAddress;}
						
						gCountry = place.AddressDetails.Country.CountryNameCode;
						var detailed = result.split(",");
						if (detailed.length > 1) {
							places.push([gCountry, result]);
						}
						gCounty = "";
						gCountry = "";
						
					}
						fromOK = false;
						//alert(places.join('\n'));
						fillFromChoices(places, choicesFrom, txtFrom);
				}
				else {
					fromOK = true;
					checkDirections(document.getElementById("locale").value);
				}
			}
			else {alertError(status, "");}
		}
		function fillFromChoices(places, choices, txtBox) {
			choices.innerHTML = "";
			setClass(choices, "show");
			var ul = buildHTML("ul");
			for(var i=0; i<places.length; i++) {
				var li = buildHTML("li");
				var a = buildHTML("a", {href:"#", innerHTML:places[i][1]});
				a.onclick = function() {
					txtBox.value = this.innerHTML;
					setClass(choices, "hide");
					choices.innerHTML = "";
					fromOK = true;
					checkDirections(document.getElementById("locale").value);
				}
				li.appendChild(a);
				ul.appendChild(li);
				if(i==places.length-1){
					var liClose = buildHTML("li");
					var aClose = buildHTML("a", {href:"#", innerHTML: "x Lukk", className: "boxClose"});
					aClose.onclick = function() {
						setClass(this.parentNode.parentNode.parentNode, "hide");
					}
					liClose.appendChild(aClose);
					ul.appendChild(liClose);
				}
			}
			choices.appendChild(ul);
		}
		function listToLocations(response) {
			var status = response.Status.code;
			if(status==200) {
				var places = new Array();
				var choicesTo = document.getElementById("choicesTo");
				var txtTo = document.getElementById("to");
				if (response.Placemark.length > 1) { 
					for(var i=0; i<response.Placemark.length; i++) {
						var place = response.Placemark[i];
						var gAddress, gCounty, gCountry;
						gAddress = place.address;
						if(!!place.AddressDetails.Country.AdministrativeArea) {
							if(!!place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea) {
								gCounty = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
								var lastComma = gAddress.lastIndexOf(',');
								var result = gAddress.substring(0,lastComma) + ", " + gCounty + gAddress.substring(lastComma, gAddress.length);
							}
						}
						else{result=gAddress;}
						gCountry = place.AddressDetails.Country.CountryNameCode;
						var detailed = result.split(",");
						if (detailed.length > 1) {
							places.push([gCountry, result]);
						}
						
						gCounty = "";
						gCountry = "";
					}
						toOK = false;
						fillToChoices(places, choicesTo, txtTo);
				}
				else {
					toOK = true;
					checkDirections(document.getElementById("locale").value);
				}
			}
			else {alertError(status, "");}
		}
		function fillToChoices(places, choices, txtBox) {
			choices.innerHTML = "";
			setClass(choices, "show");
			var ul = buildHTML("ul");
			for(var i=0; i<places.length; i++) {
				var li = buildHTML("li");
				var a = buildHTML("a", {href:"#", innerHTML:places[i][1]});
				a.onclick = function() {
					txtBox.value = this.innerHTML;
					setClass(choices, "hide");
					choices.innerHTML = "";
					toOK = true;
					checkDirections(document.getElementById("locale").value);
				}
				
				li.appendChild(a);
				ul.appendChild(li);
				if(i==places.length-1){
					var liClose = buildHTML("li");
					var aClose = buildHTML("a", {href:"#", innerHTML: "x Lukk", className: "boxClose"});
					aClose.onclick = function() {
						setClass(this.parentNode.parentNode.parentNode, "hide");
					}
					liClose.appendChild(aClose);
					ul.appendChild(liClose);
				}
			}
			choices.appendChild(ul);
		}
	function phoneHome(params) {
		var url = 'updateStats.aspx' + pParam + params;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		http_request.open('GET', url, false);
		http_request.send(null);
	}
  function handleErrors(){
	 if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) alertError(gdir.getStatus().code, errorMsg[0]);
	 else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) alertError(gdir.getStatus().code, errorMsg[1]);
	 else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) alertError(gdir.getStatus().code, errorMsg[2]);
	 else if (gdir.getStatus().code == G_GEO_BAD_KEY) alertError(gdir.getStatus().code, errorMsg[3]);
	 else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) alertError(gdir.getStatus().code, errorMsg[4]);
	 else alertError(gdir.getStatus().code, errorMsg[5]);
	}
	function center(element){
    try{
        element = $(element);
    }catch(e){
        return;
    }
    var my_width  = 0;
    var my_height = 0;
    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }else if ( document.documentElement && 
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body && 
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }
    element.style.position = 'absolute';
    element.style.zIndex   = 99;
    var scrollY = 0;
    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }
    var setX = ( my_width  - 300  ) / 2;
    var setY = ( my_height - 200 ) / 2 + scrollY;
    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;
    element.style.left = setX + "px";
    element.style.top  = setY + "px";
    //element.style.display  = 'block';
	}
	function alertError(code, message) {
		if (message=="") {
		 	if (code == G_GEO_UNKNOWN_ADDRESS) message = errorMsg[0];
		 	else if (code == G_GEO_SERVER_ERROR) message = errorMsg[1];
		 	else if (code == G_GEO_MISSING_QUERY) message = errorMsg[2];
		 	else if (code == G_GEO_BAD_KEY) message = errorMsg[3];
		 	else if (code == G_GEO_BAD_REQUEST) message = errorMsg[4];
		 	else message = errorMsg[5];
		}
		
		var b = document.getElementsByTagName("BODY")[0];
		var overlay = document.getElementById("overlay");
		var errorBox = document.getElementById("errorBox");
		var errorContent = buildHTML("div", {id: "errorContent"});
		var errorImg = buildHTML("img", {src: "img/alert.gif", alt: "Error"});
		var errorInfo = buildHTML("div", {innerHTML: message, id: "errorInfo"});
		var errorBtnContainer = buildHTML("div", {className: "apiClear", id: "errorBtnContainer"});
		var errorA = buildHTML("a", {href: "#"});
		var errorBtn = buildHTML("img", {src: "img/ok.gif", alt:"OK"});
		errorA.appendChild(errorBtn);
		errorA.onclick = removeError;
		errorBtnContainer.appendChild(errorA);
		errorContent.appendChild(errorImg);
		errorContent.appendChild(errorInfo);
		errorBox.appendChild(errorContent);
		errorBox.appendChild(errorBtnContainer);
		b.appendChild(overlay);
		b.appendChild(errorBox);
		overlay.style.height = document.body.clientHeight + 'px';
		center("errorBox");
		setClass(overlay, "show");
		setClass(errorBox, "show");
	}
	function removeError(){
		var errorBox = document.getElementById("errorBox");
		var overlay = document.getElementById("overlay");
		errorBox.innerHTML = "";
		setClass(errorBox, "hide");
		setClass(overlay, "hide");
	}
	function onGDirectionsLoad(){ 
		// gdir onload
	}