/* IE FIX */
      if(typeof(DOMParser) == 'undefined') {
       DOMParser = function() {}
       DOMParser.prototype.parseFromString = function(str, contentType) {
        if(typeof(ActiveXObject) != 'undefined') {
         var xmldata = new ActiveXObject('MSXML.DomDocument');
         xmldata.async = false;
         xmldata.loadXML(str);
         return xmldata;
        } else if(typeof(XMLHttpRequest) != 'undefined') {
         var xmldata = new XMLHttpRequest;
         if(!contentType) {
          contentType = 'application/xml';
         }
         xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
         if(xmldata.overrideMimeType) {
          xmldata.overrideMimeType(contentType);
         }
         xmldata.send(null);
         return xmldata.responseXML;
        }
       }
      }



var googlemaps_xmlhttp;
var googlemaps_xmlhttp_select;

function getAjaxMapData(country, admarea, city, category) {
	googlemaps_xmlhttp=GetXmlHttpObject();
	if (googlemaps_xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	var url="typo3conf/ext/df_googlemaps/pi2/class.tx_dfgooglemaps_pi2_ajax.php";
	url=url+"?country="+country;
	if ( admarea != '') {
		url=url+"&admarea="+escape(admarea);
	}
	url=url+"&city="+city;
	if (category != '')
		url=url+"&category="+category;
	url=url+"&pid="+document.getElementById('profile_pid').value;
	googlemaps_xmlhttp.onreadystatechange=googlemaps_stateChanged;
	googlemaps_xmlhttp.open("GET",url,true);
	googlemaps_xmlhttp.send(null);
}
function getAjaxCities(country, admarea) {
	googlemaps_xmlhttp_select=GetXmlHttpObject();
	if (googlemaps_xmlhttp_select==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	var url="typo3conf/ext/df_googlemaps/pi2/class.tx_dfgooglemaps_pi2_ajax.php";
	url=url+"?country="+country;
	if ( admarea != '') {
		url=url+"&admarea="+escape(admarea);
	}
	url=url+"&pid="+document.getElementById('profile_pid').value;
	url=url+"&updateSelect=true";
	googlemaps_xmlhttp_select.onreadystatechange=googlemaps_stateChanged1;
	googlemaps_xmlhttp_select.open("GET",url,true);
	googlemaps_xmlhttp_select.send(null);
}

function googlemaps_stateChanged() {
	if (googlemaps_xmlhttp.readyState==4) {
  		var xmldoc = (new DOMParser()).parseFromString(googlemaps_xmlhttp.responseText, "text/xml");
  		if (xmldoc) {
          	updateMarkers(xmldoc);
		}
    }
 }
 function googlemaps_stateChanged1() {
    if (googlemaps_xmlhttp_select.readyState==4) {
    	var xmldoc1 = (new DOMParser()).parseFromString(googlemaps_xmlhttp_select.responseText, "text/xml");
		if (xmldoc1) {
			updateCitySelect(xmldoc1);
		}
	}
}


function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		return new XMLHttpRequest();
  	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function updateCitySelect(xmldoc) {
	
	var select = document.getElementById('selectcity');
    for(i=select.options.length-1;i>=0;i--)
    {
        select.remove(i);
    }
	for (i = 0; i < xmldoc.getElementsByTagName('cities')[0].getElementsByTagName('city').length; i++)
	{
		var option = document.createElement("OPTION");
		try {
			option.text = xmldoc.getElementsByTagName('cities')[0].getElementsByTagName('city')[i].firstChild.nodeValue;
			option.value = xmldoc.getElementsByTagName('cities')[0].getElementsByTagName('city')[i].firstChild.nodeValue;
	    	select.options.add(option);
  		} catch (err){}
    }
}

/**
*	Update the map with data from xml file
*/
function updateMarkers(XMLmapData) {
	    df_googlemaps_map.clearOverlays();
	    for (i = 0; i < XMLmapData.getElementsByTagName('markers')[0].getElementsByTagName('marker').length; i++)
	    {
	    	var lat = XMLmapData.getElementsByTagName('markers')[0].getElementsByTagName('marker')[i].getElementsByTagName('lat')[0].firstChild.nodeValue;
	    	var lng = XMLmapData.getElementsByTagName('markers')[0].getElementsByTagName('marker')[i].getElementsByTagName('lng')[0].firstChild.nodeValue;
	    	var html = XMLmapData.getElementsByTagName('markers')[0].getElementsByTagName('marker')[i].getElementsByTagName('html')[0].firstChild.nodeValue;
	    	//var icon = XMLmapData.getElementsByTagName('markers')[0].getElementsByTagName('marker')[i].getElementsByTagName('icon')[0].firstChild.nodeValue;
	        var icon = '';
	        
		var updateMarkers_point = new GLatLng(lat,lng);
	        var updateMarkers_marker = createMarker(updateMarkers_point, html, icon);
	        df_googlemaps_map.addOverlay(updateMarkers_marker);
	    }
	
	
	
	var admarea = document.getElementById('selectadmarea').options[document.getElementById('selectadmarea').selectedIndex].value;
	getAjaxCities('Sverige', admarea);
}



